diff options
author | Timo Dritschler <timo.dritschler@kit.edu> | 2014-04-23 18:24:12 +0200 |
---|---|---|
committer | Timo Dritschler <timo.dritschler@kit.edu> | 2014-04-23 18:24:12 +0200 |
commit | 7fcbf0c689566dbe5a58eac0be7693b839cd5444 (patch) | |
tree | f1e8ba5ce169a003e47f960d0129cab5f250cb92 | |
download | kiro-7fcbf0c689566dbe5a58eac0be7693b839cd5444.tar.gz kiro-7fcbf0c689566dbe5a58eac0be7693b839cd5444.tar.bz2 kiro-7fcbf0c689566dbe5a58eac0be7693b839cd5444.tar.xz kiro-7fcbf0c689566dbe5a58eac0be7693b839cd5444.zip |
Initial Commit for "KIT Infiniband Remote Objects" (KIRO) Library
Created KiroTrb (KIRO Transmittable Ring Buffer) class
Wrote short test for KiroTrb class
-rw-r--r-- | Makefile | 26 | ||||
-rw-r--r-- | kiro-trb.c | 215 | ||||
-rw-r--r-- | kiro-trb.h | 111 | ||||
-rw-r--r-- | test.c | 50 |
4 files changed, 402 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5505df9 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -g -gdwarf-2 $(shell pkg-config --cflags gobject-2.0) +LDFLAGS= -lrdmacm -libverbs -lpthread $(shell pkg-config --libs gobject-2.0) + + +all: base + +base: kiro-trb.o + +kiro-cbr.o: kiro-trb.c kiro-trb.h + $(CC) $(CFLAGS) $(LDFLAGS) -c kiro-trb.c -o kiro-trb.o + + +test-trb: test + +test: kiro-trb.o test.c + $(CC) $(CFLAGS) $(LDFLAGS) test.c kiro-trb.o -o test-tbr + + +clean: + rm -f *.o test-trb + + + +rebuild: clean all + diff --git a/kiro-trb.c b/kiro-trb.c new file mode 100644 index 0000000..3718102 --- /dev/null +++ b/kiro-trb.c @@ -0,0 +1,215 @@ +/* Copyright (C) 2014 Timo Dritschler <timo.dritschler@kit.edu> + (Karlsruhe Institute of Technology) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2.1 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License along + with this library; if not, write to the Free Software Foundation, Inc., 51 + Franklin St, Fifth Floor, Boston, MA 02110, USA +*/ + +/** + * SECTION: kiro-trb + * @Short_description: KIRO 'Transmittable Ring Buffer' + * @Title: KiroTrb + * + * KiroTrb implements a 'Transmittable Ring Buffer' that holds all necessary information + * about its content inside itself, so its data can be exchanged between different + * instances of the KiroTrb Class and/or sent over a network. + */ + +#include <stdio.h> + +#include <stdlib.h> +#include <string.h> +#include <glib.h> +#include "kiro-trb.h" + + +/* + * Definition of 'private' structures and members and macro to access them + */ + +#define KIRO_TRB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KIRO_TYPE_TRB, KiroTrbPrivate)) + +struct _KiroTrbPrivate { + + /* Properties */ + // PLACEHOLDER // + + /* 'Real' private structures */ + /* (Not accessible by properties) */ + int initialized; // 1 if Buffer is Valid, 0 otherwise + void* mem; // Access to the actual buffer in Memory + void* frame_top; // First byte of the buffer storage + void* current; // Pointer to the current fill state + uint64_t element_size; + uint64_t max_elements; + uint64_t iteration; // How many times the buffer has wraped around + + /* easy access */ + uint64_t buff_size; +}; + + +G_DEFINE_TYPE_WITH_PRIVATE (KiroTrb, kiro_trb, G_TYPE_OBJECT); + + +static +void kiro_trb_init (KiroTrb *self) +{ + KiroTrbPrivate *priv = KIRO_TRB_GET_PRIVATE(self); + priv->initialized = 0; +} + +static void +kiro_trb_finalize (GObject *object) +{ + KiroTrb *self = KIRO_TRB(object); + KiroTrbPrivate *priv = KIRO_TRB_GET_PRIVATE(self); + if(priv->mem) + free(priv->mem); +} + +static void +kiro_trb_class_init (KiroTrbClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + gobject_class->finalize = kiro_trb_finalize; +} + + +/* Privat functions */ + +void write_header (KiroTrbPrivate* priv) +{ + if(!priv) + return; + struct KiroTrbInfo* tmp_info = (struct KiroTrbInfo*)priv->mem; + tmp_info->buffer_size_bytes = priv->buff_size; + tmp_info->element_size = priv->element_size; + tmp_info->offset = (priv->iteration * priv->max_elements) + ((priv->current - priv->frame_top) / priv->element_size); + memcpy(priv->mem, tmp_info, sizeof(struct KiroTrbInfo)); +} + + + +/* TRB functions */ + +uint64_t kiro_trb_get_element_size (KiroTrb* self) +{ + KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self); + if(priv->initialized != 1) + return 0; + return priv->element_size; +} + + +uint64_t kiro_trb_get_max_elements (KiroTrb* self) +{ + KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self); + if(priv->initialized != 1) + return 0; + return priv->max_elements; +} + + +uint64_t kiro_trb_get_raw_size (KiroTrb* self) +{ + KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self); + if(priv->initialized != 1) + return 0; + return priv->buff_size; +} + + +void* kiro_trb_get_raw_buffer (KiroTrb* self) +{ + KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self); + if(priv->initialized != 1) + return NULL; + write_header(priv); + return priv->mem; +} + + + +void* kiro_trb_get_element (KiroTrb* self, uint64_t element) +{ + KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self); + if(priv->initialized != 1) + return NULL; + + uint64_t relative = 0; + if(priv->iteration == 0) + relative = element * priv->element_size; + else + relative = ((priv->current - priv->frame_top) + (priv->element_size * element)) % (priv->buff_size - sizeof(struct KiroTrbInfo)); + + return priv->frame_top + relative; +} + + +void kiro_trb_flush (KiroTrb *self) +{ + KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self); + priv->iteration = 0; + priv->current = priv->frame_top; +} + + +int kiro_trb_reshape (KiroTrb *self, uint64_t element_size, uint64_t element_count) +{ + size_t new_size = (element_size * element_count) + sizeof(struct KiroTrbInfo); + void* newmem = malloc(new_size); + if(!newmem) + return -1; + ((struct KiroTrbInfo *)newmem)->buffer_size_bytes = new_size; + ((struct KiroTrbInfo *)newmem)->element_size = element_size; + ((struct KiroTrbInfo *)newmem)->offset = 0; + kiro_trb_ingest(self, newmem); + return 0; +} + + +int kiro_trb_push (KiroTrb *self, void *element_in) +{ + KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self); + if(priv->initialized != 1) + return -1; + if((priv->current + priv->element_size) > (priv->mem + priv->buff_size)) + return -1; + memcpy(priv->current, element_in, priv->element_size); + priv->current += priv->element_size; + if(priv->current >= priv->frame_top + (priv->element_size * priv->max_elements)) + { + priv->current = priv->frame_top; + priv->iteration++; + } + return 0; +} + + +void kiro_trb_ingest (KiroTrb *self, void *buff_in) +{ + KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self); + struct KiroTrbInfo *tmp = (struct KiroTrbInfo *)buff_in; + if(priv->mem) + free(priv->mem); + priv->mem = buff_in; + priv->buff_size = tmp->buffer_size_bytes; + priv->element_size = tmp->element_size; + priv->max_elements = (tmp->buffer_size_bytes - sizeof(struct KiroTrbInfo)) / tmp->element_size; + priv->iteration = tmp->offset / priv->max_elements; + priv->frame_top = buff_in + sizeof(struct KiroTrbInfo); + priv->current = priv->frame_top + ((tmp->offset % priv->max_elements) * priv->element_size); + priv->initialized = 1; +}
\ No newline at end of file diff --git a/kiro-trb.h b/kiro-trb.h new file mode 100644 index 0000000..db0cd8e --- /dev/null +++ b/kiro-trb.h @@ -0,0 +1,111 @@ +/* Copyright (C) 2014 Timo Dritschler <timo.dritschler@kit.edu> + (Karlsruhe Institute of Technology) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2.1 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License along + with this library; if not, write to the Free Software Foundation, Inc., 51 + Franklin St, Fifth Floor, Boston, MA 02110, USA +*/ + +/** + * SECTION: kiro-trb + * @Short_description: KIRO 'Clever Ring Buffer' + * @Title: KiroTrb + * + * KiroTrb implements a 'Transmittable Ring Buffer' that holds all necessary information + * about its content inside itself, so its data can be exchanged between different + * instances of the KiroTrb Class and/or sent over a network. + */ + +#ifndef __KIRO_TRB_H +#define __KIRO_CBR_H + +#include <stdint.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define KIRO_TYPE_TRB (kiro_trb_get_type()) +#define KIRO_TRB(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), KIRO_TYPE_TRB, KiroTrb)) +#define KIRO_IS_TRB(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), KIRO_TYPE_TRB)) +#define KIRO_TRB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KIRO_TYPE_TRB, KiroTrbClass)) +#define KIRO_IS_TRB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KIRO_TYPE_TRB)) +#define KIRO_TRB_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), KIRO_TYPE_TRB, KiroTrbClass)) + + +typedef struct _KiroTrb KiroTrb; +typedef struct _KiroTrbClass KiroTrbClass; +typedef struct _KiroTrbPrivate KiroTrbPrivate; + + +struct _KiroTrb { + + GObject parent; + + /*< private >*/ + KiroTrbPrivate *priv; +}; + + +/** + * IbvConnectorInterface: + * + * Base interface for IbvConnectors. + */ + +struct _KiroTrbClass { + + GObjectClass parent_class; + +}; + + +struct KiroTrbInfo { + + /* internal information about the buffer */ + uint64_t buffer_size_bytes; // Size in bytes INCLUDING this header + uint64_t element_size; // Size in bytes of one single element + uint64_t offset; // Current Offset to access the 'oldest' element (in element count!) + +} __attribute__((packed)); + + +/* GObject and GType functions */ +GType kiro_trb_get_type (void); + +GObject kiro_trb_new (void); + +/* trb functions */ + +uint64_t kiro_trb_get_element_count (KiroTrb*); + +uint64_t kiro_trb_get_element_size (KiroTrb*); + +uint64_t kiro_trb_get_max_elements (KiroTrb*); + +uint64_t kiro_trb_get_raw_size (KiroTrb*); + +void* kiro_trb_get_raw_buffer (KiroTrb*); + +void* kiro_trb_get_element (KiroTrb*, uint64_t); + +void kiro_trb_flush (KiroTrb*); + +int kiro_trb_reshape (KiroTrb*, uint64_t, uint64_t); + +int kiro_trb_push (KiroTrb*, void*); + +void kiro_trb_ingest (KiroTrb*, void*); + +G_END_DECLS + +#endif //__KIRO_TRB_H
\ No newline at end of file @@ -0,0 +1,50 @@ +#include <time.h> +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> +#include "kiro-trb.h" + +struct test { + uint32_t zahl; + uint8_t buchstabe; +} __attribute__((packed)); + + +int main(void) +{ + /* + void* ptr = malloc(sizeof(struct test) + sizeof(uint64_t)); + memset(ptr, 0xFA, sizeof(struct test) + sizeof(uint64_t)); + struct test foo; + foo.zahl = 42; + foo.buchstabe = 'R'; + memcpy(ptr, &foo, sizeof(foo)); + + struct test *tmp = (struct test *)ptr; + printf("Zahl = %d\n",tmp->zahl); + printf("Buchstabe = %c\n", tmp->buchstabe); + printf("Remaining = %x\n", *((uint64_t *)(ptr+sizeof(struct test)))); + */ + + KiroTrb *rb = g_object_new(KIRO_TYPE_TRB, NULL); + kiro_trb_reshape(rb, sizeof(uint64_t), 3); + void *buffer = kiro_trb_get_raw_buffer(rb); + uint64_t foo = 0xAFFED00F; + uint64_t bar = 0x1337BEEF; + kiro_trb_push(rb, &foo); + kiro_trb_push(rb, &foo); + kiro_trb_push(rb, &foo); + kiro_trb_push(rb, &bar); + kiro_trb_push(rb, &foo); + kiro_trb_push(rb, &foo); + uint64_t *maman = kiro_trb_get_element(rb, 3); + printf("Stored in old: %x\n", *maman); + KiroTrb *rb2 = g_object_new(KIRO_TYPE_TRB, NULL); + kiro_trb_ingest(rb2, kiro_trb_get_raw_buffer(rb)); + maman = kiro_trb_get_element(rb2, 3); + printf("Stored in New: %x\n", *maman); + sleep(1); + g_object_unref(rb); + + return 0; +}
\ No newline at end of file |