diff options
author | Timo Dritschler <timo.dritschler@kit.edu> | 2015-02-06 20:23:43 +0100 |
---|---|---|
committer | Timo Dritschler <timo.dritschler@kit.edu> | 2015-03-09 17:05:14 +0100 |
commit | f16d62f41eac5c715eba309d79d23f3e0273cdfb (patch) | |
tree | 3d8656f663d4d072048add7a07f116fb91798146 | |
parent | 74bb04dedeb66ca1175360cb77d0596b3e3630b5 (diff) | |
download | kiro-f16d62f41eac5c715eba309d79d23f3e0273cdfb.tar.gz kiro-f16d62f41eac5c715eba309d79d23f3e0273cdfb.tar.bz2 kiro-f16d62f41eac5c715eba309d79d23f3e0273cdfb.tar.xz kiro-f16d62f41eac5c715eba309d79d23f3e0273cdfb.zip |
Added new "Kiro Sync Buffer" (KiroSb) class.
Added first prototype implementation of KiroSb
Fixed some typos
-rw-r--r-- | src/CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/kiro-sb.c | 243 | ||||
-rw-r--r-- | src/kiro-sb.h | 157 | ||||
-rw-r--r-- | src/kiro-trb.h | 7 |
4 files changed, 408 insertions, 5 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c8150c4..a9e5d96 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,12 +1,12 @@ -add_library(kiro SHARED kiro-trb.c kiro-client.c kiro-server.c) +add_library(kiro SHARED kiro-trb.c kiro-client.c kiro-server.c kiro-sb.c) set_target_properties(kiro PROPERTIES VERSION "${LIBKIRO_VERSION_MAJOR}.${LIBKIRO_VERSION_MINOR}" SOVERSION ${LIBKIRO_VERSION_PATCH} ) target_link_libraries(kiro m rdmacm ibverbs pthread) -install(FILES kiro-rdma.h kiro-trb.h kiro-client.h kiro-server.h DESTINATION - ${KIRO_INCLUDEDIR}/kiro) +install(FILES kiro-rdma.h kiro-trb.h kiro-client.h kiro-server.h kiro-sb.h + DESTINATION ${KIRO_INCLUDEDIR}/kiro) install(TARGETS kiro LIBRARY DESTINATION ${KIRO_LIBDIR} diff --git a/src/kiro-sb.c b/src/kiro-sb.c new file mode 100644 index 0000000..10a36b0 --- /dev/null +++ b/src/kiro-sb.c @@ -0,0 +1,243 @@ +/* Copyright (C) 2014-2015 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-sb + * @Short_description: KIRO 'Synchronizing Buffer' + * @Title: KiroSb + * + * KiroSb implements a 'Synchronizing Buffer' that automatically keeps the local + * memory content up to date by mirroring the remote SyncBuffers memory content + * automatically without any required user interaction + */ + +#include <stdio.h> + +#include <stdlib.h> +#include <string.h> +#include <glib.h> +#include "kiro-sb.h" +#include "kiro-trb.h" +#include "kiro-server.h" +#include "kiro-client.h" + + +/* + * Definition of 'private' structures and members and macro to access them + */ + +#define KIRO_SB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KIRO_TYPE_SB, KiroSbPrivate)) + +struct _KiroSbPrivate { + + /* Properties */ + // PLACEHOLDER // + + /* 'Real' private structures */ + /* (Not accessible by properties) */ + int initialized; // 0 if uninitialized, 1 if server, 2 if client + KiroServer* server; // KIRO Server component to serve + KiroClient* client; // KIRO Client component to clone + KiroTrb* trb; // KIRO Ring Buffer to hold and exchange data + + GThread *main_thread; // Main thread for the main_loop + GMainLoop *main_loop; // main_loop *duh* + guint close_signal; // Used to signal shutdown of the main_loop +}; + + +G_DEFINE_TYPE (KiroSb, kiro_sb, G_TYPE_OBJECT); + + +KiroSb * +kiro_sb_new (void) +{ + return g_object_new (KIRO_TYPE_SB, NULL); +} + + +void +kiro_sb_free (KiroSb *sb) +{ + g_return_if_fail (sb != NULL); + if (KIRO_IS_SB (sb)) + g_object_unref (sb); + else + g_warning ("Trying to use kiro_sb_free on an object which is not a KIRO SB. Ignoring..."); +} + + +static void +kiro_sb_init (KiroSb *self) +{ + g_return_if_fail (self != NULL); + KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self); + priv->initialized = 0; + priv->trb = NULL; + priv->server = NULL; + priv->client = NULL; +} + + +static void +kiro_sb_finalize (GObject *object) +{ + g_return_if_fail (object != NULL); + KiroSb *self = KIRO_SB (object); + KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self); + + if (priv->trb) { + kiro_trb_purge (priv->trb, FALSE); + kiro_trb_free (priv->trb); + } + + if (priv->server) + kiro_server_free (priv->server); + + if (priv->client) + kiro_client_free (priv->client); + + priv->trb = NULL; + priv->server = NULL; + priv->client = NULL; + + G_OBJECT_CLASS (kiro_sb_parent_class)->finalize (object); +} + + +static void +kiro_sb_class_init (KiroSbClass *klass) +{ + g_return_if_fail (klass != NULL); + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + gobject_class->finalize = kiro_sb_finalize; + g_type_class_add_private (klass, sizeof (KiroSbPrivate)); +} + + +gpointer +start_main_loop (GMainLoop *loop) +{ + g_main_loop_run (loop); + /* wait for mai loop to finish*/ + g_main_loop_unref (loop); + return NULL; +} + + +gboolean +idle_func (KiroSbPrivate *priv) +{ + if (priv->close_signal) { + g_main_loop_quit (priv->main_loop); + /*main_thread will do the unref upon exit*/ + priv->main_loop = NULL; + g_debug ("Main loop quit"); + return G_SOURCE_REMOVE; + } + + struct KiroTrbInfo *header = (struct KiroTrbInfo *)kiro_trb_get_raw_buffer (priv->trb); + gulong old_offset = header->offset; + kiro_client_sync_partial (priv->client, 0, sizeof(struct KiroTrbInfo), 0); + kiro_trb_refresh (priv->trb); + if (old_offset != header->offset) { + g_debug ("Fetching new element"); + gulong offset = (gulong) (kiro_trb_get_element (priv->trb, -1) - kiro_trb_get_raw_buffer (priv->trb)); + kiro_client_sync_partial (priv->client, offset, kiro_trb_get_element_size (priv->trb), offset); + /*INVOKE callback*/ + } + + return G_SOURCE_CONTINUE; +} + + +gboolean +kiro_sb_serve (KiroSb *self, gulong size) +{ + g_return_val_if_fail (self != NULL, FALSE); + + KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self); + g_return_val_if_fail (priv->initialized == 0, FALSE); + + g_return_val_if_fail ((priv->trb = kiro_trb_new ()), FALSE); + + if (0 > kiro_trb_reshape (priv->trb, size, 3)) { + g_debug ("Failed to create KIRO ring buffer"); + kiro_trb_free (priv->trb); + return FALSE; + } + + void *buff = kiro_trb_get_raw_buffer (priv->trb); + gulong b_size = kiro_trb_get_raw_size (priv->trb); + + priv->server = kiro_server_new (); + if (0 > kiro_server_start (priv->server, NULL, "60010", buff, b_size)) { + g_debug ("Failed to start KIRO Server"); + kiro_server_free (priv->server); + kiro_trb_free (priv->trb); + return FALSE; + } + + priv->initialized = 1; + g_message ("SyncBuffer ready"); + + return TRUE; +} + + +gboolean +kiro_sb_clone (KiroSb *self, const gchar* address, const gchar* port) +{ + g_return_val_if_fail (self != NULL, FALSE); + + KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self); + g_return_val_if_fail (priv->initialized == 0, FALSE); + + g_return_val_if_fail ((priv->trb = kiro_trb_new ()), FALSE); + + priv->client = kiro_client_new (); + if (0 > kiro_client_connect (priv->client, address, port)) { + g_debug ("Failed to connect to remote Sync Buffer"); + kiro_trb_free (priv->trb); + kiro_client_free (priv->client); + return FALSE; + } + + kiro_client_sync (priv->client); + kiro_trb_adopt (priv->trb, kiro_client_get_memory (priv->client)); + + priv->main_loop = g_main_loop_new (NULL, FALSE); + g_idle_add ((GSourceFunc)idle_func, priv); + priv->main_thread = g_thread_new ("KIRO SB Main Loop", (GThreadFunc)start_main_loop, priv->main_loop); + + return TRUE; +} + + +gulong +kiro_sb_get_size (KiroSb *self) +{ + g_return_val_if_fail (self != NULL, 0); + + KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self); + g_return_val_if_fail (priv->initialized != 0, 0); + + return kiro_trb_get_element_size (priv->trb); +} +/* Privat functions */ + diff --git a/src/kiro-sb.h b/src/kiro-sb.h new file mode 100644 index 0000000..45fcf0c --- /dev/null +++ b/src/kiro-sb.h @@ -0,0 +1,157 @@ +/* Copyright (C) 2014-2015 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-sync-buffer + * @Short_description: KIRO 'Synchronizing Buffer' + * @Title: KiroSb + * + * KiroSb implements a 'Synchronizing Buffer' that automatically keeps the local + * memory content up to date by mirroring the remote SyncBuffers memory content + * automatically without any required user interaction + */ + +#ifndef __KIRO_SB_H +#define __KIRO_SB_H + +#include <stdint.h> +#include <glib-object.h> + +G_BEGIN_DECLS + +#define KIRO_TYPE_SB (kiro_sb_get_type()) +#define KIRO_SB(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), KIRO_TYPE_SB, KiroSb)) +#define KIRO_IS_SB(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), KIRO_TYPE_SB)) +#define KIRO_SB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KIRO_TYPE_SB, KiroSbClass)) +#define KIRO_IS_SB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KIRO_TYPE_SB)) +#define KIRO_SB_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), KIRO_TYPE_SB, KiroSbClass)) + + +typedef struct _KiroSb KiroSb; +typedef struct _KiroSbClass KiroSbClass; +typedef struct _KiroSbPrivate KiroSbPrivate; + + +struct _KiroSb { + + GObject parent; + +}; + + +/** + * IbvConnectorInterface: + * + * Base interface for IbvConnectors. + */ + +struct _KiroSbClass { + + GObjectClass parent_class; + +}; + + +/* GObject and GType functions */ +/** + * kiro_sb_get_type: (skip) + * Returns: GType of #KiroSb + */ +GType kiro_sb_get_type (void); + + +/** + * kiro_sb_new - Creates a new #KiroSb + * Returns: (transfer full): A pointer to a new #KiroSb + * Description: + * Creates a new #KiroSb and returns a pointer to it. + * See also: + * kiro_sb_free + */ +KiroSb* kiro_sb_new (void); + + +/** + * kiro_sb_free - 'Destroys' the given #KiroSb + * @trb: (transfer none): The #KiroSb that is to be freed + * Description: + * Clears all underlying memory and frees the object memory. + * Note: + * The internal memory is also freed when calling this function. If you want + * to continue using the raw @sb memory after you call this function, you need + * to memcpy() its content using the information optained from + * kiro_sb_get_element() + * See also: + * kiro_sb_new, kiro_sb_get_element + */ +void kiro_sb_free (KiroSb *sb); + + +/** + * kiro_sb_serve - Allow remote KiroSbs to clone this buffers memory + * Returns: A gboolean. TRUE = success. FALSE = fail. + * @sb: (transfer none): The #KiroSb to perform this operation on + * @size: Size in bytes of the content that will be served + * Description: + * Allows other remote #KiroSbs to connect to this #KiroSb and clone its + * memory. + * Note: + * A #KiroSb that already 'serves' its content can no longer clone + * other remote #KiroSbs. + * See also: + * + */ +gboolean kiro_sb_serve (KiroSb *sb, gulong size); + + +/** + * kiro_sb_clone - Clone the content of a remote #KiroSb + * Returns: A gboolean. TRUE = connection successful. FALSE = connection failed. + * @sb: (transfer none): The #KiroSb to perform this operation on + * @address: The InfiniBand address of the remote #KiroSb which should be cloned + * @port: The InfiniBand port of the remote #KiroSb which should be cloned + * Description: + * Connects to the remote #KiroSb given by @address and @port and + * continuousely clones its content into the local #KiroSb + * Note: + * A #KiroSb that clones a remote #KiroSb can no longer start to 'serve' its + * content to other remote #KiroSbs + * See also: + * + */ +gboolean kiro_sb_clone (KiroSb *sb, const gchar *address, const gchar *port); + +/** + * kiro_sb_get_size - Get the size in bytes of the managed memory + * Returns: A gulong giving the size of the managed memory in bytes + * @sb: (transfer none): The #KiroSb to perform this operation on + * Description: + * Returns the size in bytes of the content that is being served and/or cloned + * from. + * Note: + * Since #KiroSb uses an internal triple buffer, the value gained from this + * function only gives the size of one element from that buffer. The size of + * the entire data structure will be different. + * See also: + * + */ +gulong kiro_sb_get_size (KiroSb *sb); + +G_END_DECLS + +#endif //__kiro_sb_H diff --git a/src/kiro-trb.h b/src/kiro-trb.h index 6620191..bec8626 100644 --- a/src/kiro-trb.h +++ b/src/kiro-trb.h @@ -286,6 +286,8 @@ int kiro_trb_is_setup (KiroTrb *trb); * @trb: #KiroTrb to perform the operation on * @element_size: Individual size of the elements to store in bytes * @element_count: Maximum number of elements to be stored + * Returns: + * integer: < 0 for error, >= 0 for success * Description: * (Re)Allocates internal memory for the given ammount of elements * at the given individual size @@ -310,7 +312,7 @@ int kiro_trb_reshape (KiroTrb *trb, uint64_t element_size, uint64_t element_coun * tries to copy that memory into its own. * Notes: * The given memory is treated as a correct KIRO TRB memory block, - * including a consistend memory header. That header is read and + * including a consistent memory header. That header is read and * then cloned into the internal memory according to the headers * information. * If the given memory is not a consistent KIRO TRB memory block, @@ -373,13 +375,14 @@ void kiro_trb_refresh (KiroTrb *trb); * takes ownership over the memory. * Notes: * The given memory is treated as a correct KIRO TRB memory block, - * including a consistend memory header. That header is read and + * including a consistent memory header. That header is read and * the TRB sets up all internal structures in accordance to that * header. * If the given memory is not a consistent KIRO TRB memory block, * the behavior of this function is undefined. * The TRB takes full ownership of the given memory and may free * it at will. + * Any previously owned memory is freed. * See also: * kiro_trb_clone, kiro_trb_reshape */ |