Skip to content

Commit

Permalink
Merge pull request #10 from yosefe/topic/add-ucs-component
Browse files Browse the repository at this point in the history
UCS/TEST: Add 'component' service for global initialization.
  • Loading branch information
yosefe committed Oct 30, 2014
2 parents eb7e84e + 0ed83ba commit dbcbed3
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

ACLOCAL_AMFLAGS = -I config/m4

SUBDIRS = src/uct src/ucs test/gtest
SUBDIRS = src/ucs src/uct test/gtest

EXTRA_DIST =
EXTRA_DIST += m4/gtest.m4
Expand Down
5 changes: 5 additions & 0 deletions config/m4/ib.m4
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# $HEADER$
#

with_ib=no

#
# RC Support
Expand All @@ -16,5 +17,9 @@ AC_ARG_WITH([rc],
AM_CONDITIONAL([HAVE_TL_RC], [test "x$with_rc" != xno])
AS_IF([test "x$with_rc" != xno],
[AC_DEFINE([HAVE_TL_RC], 1, [RC transport support])
with_ib=yes
transports="${transports},rc"])

AM_CONDITIONAL([HAVE_IB], [test "x$with_ib" != xno])
AS_IF([test "x$with_ib" != xno],
[AC_DEFINE([HAVE_IB], 1, [IB support])])
2 changes: 2 additions & 0 deletions src/ucs/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ noinst_HEADERS = \
time/timerq.h \
type/status.h \
type/callback.h \
type/component.h \
type/spinlock.h

libucs_la_SOURCES = \
Expand All @@ -73,6 +74,7 @@ libucs_la_SOURCES = \
time/timer_wheel.c \
time/timerq.c \
type/callback.c \
type/component.c \
type/status.c

libucstest_la_SOURCES = \
Expand Down
61 changes: 61 additions & 0 deletions src/ucs/type/component.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2012. ALL RIGHTS RESERVED.
*
* $COPYRIGHT$
* $HEADER$
*/

#include "component.h"

#include <ucs/datastruct/list.h>


void __ucs_component_add(ucs_list_link_t *list, size_t base_size, ucs_component_t *comp)
{
comp->offset = __ucs_components_total_size(list, base_size);
ucs_list_add_tail(list, &comp->list);
}

size_t __ucs_components_total_size(ucs_list_link_t *list, size_t base_size)
{
ucs_component_t *last;

if (ucs_list_is_empty(list)) {
return base_size;
}

last = ucs_list_tail(list, ucs_component_t, list);
return last->offset + last->size;
}

void __ucs_components_cleanup(ucs_list_link_t *start, ucs_list_link_t *end, void *base_ptr)
{
ucs_list_link_t *iter;

iter = start;
while (iter->next != end) {
ucs_list_head(iter, ucs_component_t, list)->cleanup(base_ptr);
iter = iter->next;
}
}

ucs_status_t __ucs_components_init_all(ucs_list_link_t *list, void *base_ptr)
{
ucs_component_t *comp;
ucs_status_t status;

ucs_list_for_each(comp, list, list) {
status = comp->init(base_ptr);
if (status != UCS_OK) {
__ucs_components_cleanup(list, comp->list.next, base_ptr);
return status;
}
}
return UCS_OK;
}

void __ucs_components_cleanup_all(ucs_list_link_t *list, void *base_ptr)
{
__ucs_components_cleanup(list, list, base_ptr);
}

144 changes: 144 additions & 0 deletions src/ucs/type/component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2012. ALL RIGHTS RESERVED.
*
* $COPYRIGHT$
* $HEADER$
*/


#ifndef UCS_COMPONENT_H_
#define UCS_COMPONENT_H_

#include <ucs/sys/preprocessor.h>
#include <ucs/datastruct/list.h>
#include <ucs/type/status.h>


/*
* Component definition - used internally.
*/
typedef ucs_status_t (*ucs_component_init_cb_t)(void *base_ptr);
typedef void (*ucs_component_cleanup_cb_t)(void *base_ptr);
typedef struct ucs_component {
const char *name;
ucs_component_init_cb_t init;
ucs_component_cleanup_cb_t cleanup;
size_t size;
size_t offset;
ucs_list_link_t list;
} ucs_component_t;


/*
* Helper macros
*/
#define _UCS_COMPONENT_LIST_NAME(_base_type) \
ucs ## _base_type ## _component_list
#define _UCS_COMPONENT_LIST_EXTERN(_base_type) \
extern ucs_list_link_t _UCS_COMPONENT_LIST_NAME(_base_type)


/**
* Define a list of components for specific base type.
*
* @param _base_type Type to add components to.
*/
#define UCS_COMPONENT_LIST_DEFINE(_base_type) \
UCS_LIST_HEAD(_UCS_COMPONENT_LIST_NAME(_base_type))


/**
* Define a component for specific base type.
*
* @param _base_type Type to add components to.
* @param _name Component name.
* @param _init Initialization function.
* @param _cleanup Cleanup function.
* @param _type Component context type to add to base type.
*/
#define UCS_COMPONENT_DEFINE(_base_type, _name, _init, _cleanup, _type) \
\
size_t ucs_##_name##_component_offset; \
\
void UCS_F_CTOR UCS_PP_APPEND_UNIQUE_ID(ucs_component_##_name##_register)() { \
static ucs_component_t comp = { \
#_name, \
(ucs_component_init_cb_t)_init, \
(ucs_component_cleanup_cb_t)_cleanup, \
sizeof(_type)}; \
\
_UCS_COMPONENT_LIST_EXTERN(_base_type); \
__ucs_component_add(&_UCS_COMPONENT_LIST_NAME(_base_type), \
sizeof(_base_type), &comp); \
ucs_##_name##_component_offset = comp.offset; \
}


/**
* @param _base Components base type.
* @return How much room is required for all components of this base.
*/
#define ucs_components_total_size(_base_type) \
({ \
_UCS_COMPONENT_LIST_EXTERN(_base_type); \
__ucs_components_total_size(&_UCS_COMPONENT_LIST_NAME(_base_type),\
sizeof(_base_type)); \
})


/**
* Initialize all components of a specific base type.
*
* @param _base_type Base type to initialize components for.
* @param _base_ptr Pointer to base type instance, to pass to components
* initialization functions.
*
* @return UCS_OK if all components were successfully initialized, otherwise the
* error from the first failed component.
*/
#define ucs_components_init_all(_base_type, _base_ptr) \
({ \
_UCS_COMPONENT_LIST_EXTERN(_base_type); \
__ucs_components_init_all(&_UCS_COMPONENT_LIST_NAME(_base_type), _base_ptr); \
})


/**
* Cleanup all components of a specific base type.
*
* @param _base_type Class whose components to cleanup.
* @param _base_ptr Pointer to base type instance, to pass to components
* cleanup functions.
*/
#define ucs_components_cleanup_all(_base_type, _base_ptr) \
{ \
_UCS_COMPONENT_LIST_EXTERN(_base_type); \
__ucs_components_cleanup_all(&_UCS_COMPONENT_LIST_NAME(_base_type), _base_ptr); \
}


/**
* Get a component context from base type pointer..
*
* @param _base_ptr Pointer to base type instance.
* @param _type Type of component context.
*
* @return Pointer to component context.
*
* @note: Cannot be used from library constructors.
*/
#define ucs_component_get(_base_ptr, _name, _type) \
({ \
extern size_t ucs_##_name##_component_offset; \
((_type*)( (char*)(_base_ptr) + ucs_##_name##_component_offset )); \
})


void __ucs_component_add(ucs_list_link_t *list, size_t base_size, ucs_component_t *comp);
size_t __ucs_components_total_size(ucs_list_link_t *list, size_t base_size);
ucs_status_t __ucs_components_init_all(ucs_list_link_t *list, void *base_ptr);
void __ucs_components_cleanup_all(ucs_list_link_t *list, void *base_ptr);


#endif /* COMPONENT_H_ */

15 changes: 13 additions & 2 deletions src/uct/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ lib_LTLIBRARIES = libuct.la

libuct_la_CPPFLAGS = -I$(abs_top_srcdir)/src -I$(abs_top_builddir)/src
libuct_la_LDFLAGS = -ldl -version-info $(SOVERSION)
libuct_la_LIBADD = $(LIBM)
libuct_la_LIBADD = $(LIBM) ../ucs/libucs.la
libuct_ladir = $(includedir)

nobase_dist_libuct_la_HEADERS = \
Expand All @@ -18,7 +18,18 @@ nobase_dist_libuct_la_HEADERS = \
api/uct.h \
api/version.h

noinst_HEADERS =
noinst_HEADERS = \
tl/context.h

libuct_la_SOURCES = \
tl/context.c \
tl/tl.c


if HAVE_IB
noinst_HEADERS += \
ib/ib_context.h

libuct_la_SOURCES += \
ib/ib_context.c
endif
Empty file removed src/uct/ib/dummy
Empty file.
24 changes: 24 additions & 0 deletions src/uct/ib/ib_context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2014. ALL RIGHTS RESERVED.
*
* $COPYRIGHT$
* $HEADER$
*/

#include "ib_context.h"

#include <uct/tl/context.h>
#include <ucs/type/component.h>


ucs_status_t uct_ib_init(uct_context_t *context)
{
return UCS_OK;
}

void uct_ib_cleanup(uct_context_t *context)
{
}

UCS_COMPONENT_DEFINE(uct_context_t, ib, uct_ib_init, uct_ib_cleanup, uct_ib_context_t)

16 changes: 16 additions & 0 deletions src/uct/ib/ib_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2014. ALL RIGHTS RESERVED.
*
* $COPYRIGHT$
* $HEADER$
*/

#ifndef UCT_IB_CONTEXT_H_
#define UCT_IB_CONTEXT_H_


typedef struct uct_ib_context {
} uct_ib_context_t;


#endif
35 changes: 35 additions & 0 deletions src/uct/tl/context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2014. ALL RIGHTS RESERVED.
*
* $COPYRIGHT$
* $HEADER$
*/

#include "context.h"

#include <uct/api/uct.h>
#include <ucs/debug/memtrack.h>

UCS_COMPONENT_LIST_DEFINE(uct_context_t);

ucs_status_t uct_init(uct_context_h *context_p)
{
ucs_status_t status;
uct_context_t *context;

context = ucs_malloc(ucs_components_total_size(uct_context_t), "uct context");

status = ucs_components_init_all(uct_context_t, context);
if (status != UCS_OK) {
return status;
}

*context_p = context;
return UCS_OK;
}

void uct_cleanup(uct_context_h context)
{
ucs_components_cleanup_all(uct_context_t, context);
ucs_free(context);
}
25 changes: 25 additions & 0 deletions src/uct/tl/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2014. ALL RIGHTS RESERVED.
*
* $COPYRIGHT$
* $HEADER$
*/

#ifndef UCT_CONTEXT_H
#define UCT_CONTEXT_H

#include <uct/api/uct.h>
#include <ucs/type/component.h>
#include <ucs/type/callback.h>


typedef struct uct_context uct_context_t;
struct uct_context {
ucs_notifier_chain_t progress_chain;
UCS_STATS_NODE_DECLARE(stats);
};

#define uct_component_get(_context, _name) \
ucs_component_get(_context, _name, uct_context_t) \

#endif
9 changes: 0 additions & 9 deletions src/uct/tl/tl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,3 @@

#include <uct/api/uct.h>


ucs_status_t uct_init(uct_context_h *context_p)
{
return UCS_OK;
}

void uct_cleanup(uct_context_h context)
{
}
Loading

0 comments on commit dbcbed3

Please sign in to comment.