1 /* process - GRAS process handling (common code for RL and SG) */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
12 #include "gras/transport.h"
13 #include "xbt/datadesc.h"
14 #include "gras/messages.h"
15 #include "gras_modinter.h"
17 #include "gras/Virtu/virtu_private.h"
19 XBT_LOG_NEW_SUBCATEGORY(gras_virtu, gras, "Virtualization code");
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_process, gras_virtu,
21 "Process manipulation code");
23 /* Functions to handle gras_procdata_t->libdata cells*/
26 pvoid_f_void_t constructor;
27 void_f_pvoid_t destructor;
28 } s_gras_procdata_fabric_t, *gras_procdata_fabric_t;
30 static xbt_dynar_t _gras_procdata_fabrics = NULL; /* content: s_gras_procdata_fabric_t */
32 static void gras_procdata_fabric_free(void *fab)
34 free(((gras_procdata_fabric_t) fab)->name);
37 /** @brief declare the functions in charge of creating/destructing the procdata of a module
39 * This is intended to be called from the gras_<module>_register function.
40 * This returns the module ID you can use for gras_libdata_by_id()
42 int gras_procdata_add(const char *name, pvoid_f_void_t constructor,
43 void_f_pvoid_t destructor)
46 gras_procdata_fabric_t fab;
48 if (!_gras_procdata_fabrics) {
49 /* create the dynar if needed */
50 _gras_procdata_fabrics =
51 xbt_dynar_new(sizeof(s_gras_procdata_fabric_t),
52 gras_procdata_fabric_free);
55 fab = xbt_dynar_push_ptr(_gras_procdata_fabrics);
57 fab->name = xbt_strdup(name);
58 fab->constructor = constructor;
59 fab->destructor = destructor;
61 return xbt_dynar_length(_gras_procdata_fabrics) - 1;
64 /* **************************************************************************
66 * **************************************************************************/
67 void *gras_userdata_get(void)
69 gras_procdata_t *pd = gras_procdata_get();
73 void *gras_userdata_set(void *ud)
75 gras_procdata_t *pd = gras_procdata_get();
81 void *gras_libdata_by_name(const char *name)
83 gras_procdata_t *pd = gras_procdata_get();
84 return gras_libdata_by_name_from_procdata(name, pd);
87 /* this function is splitted from previous to allow SG to get libdata from remote hosts */
88 void *gras_libdata_by_name_from_procdata(const char *name,
92 if (xbt_set_length(pd->libdata) <
93 xbt_dynar_length(_gras_procdata_fabrics)) {
94 /* Damn, some new modules were added since procdata_init(). Amok? */
99 res = xbt_set_get_by_name(pd->libdata, name);
102 RETHROWF("Cannot retrieve the libdata associated to %s: %s", name);
107 void *gras_libdata_by_id(int id)
109 gras_procdata_t *pd = gras_procdata_get();
110 if (xbt_set_length(pd->libdata) <
111 xbt_dynar_length(_gras_procdata_fabrics)) {
112 /* Damn, some new modules were added since procdata_init(). Amok? */
114 gras_procdata_init();
116 return xbt_set_get_by_id(pd->libdata, id);
120 void gras_procdata_init()
122 gras_procdata_t *pd = gras_procdata_get();
123 s_gras_procdata_fabric_t fab;
131 pd->libdata = xbt_set_new();
134 xbt_dynar_foreach(_gras_procdata_fabrics, cursor, fab) {
136 if (cursor + 1 <= xbt_set_length(pd->libdata)) {
137 XBT_DEBUG("Skip fabric %u: there is already %lu libdata",
138 cursor, xbt_set_length(pd->libdata));
139 continue; /* allow to recall this function to get recently added fabrics */
141 XBT_DEBUG("Go ahead for cursor %u, there is %lu libdata",
142 cursor, xbt_set_length(pd->libdata));
144 xbt_assert(fab.name, "Name of fabric #%u is NULL!", cursor);
145 XBT_DEBUG("Create the procdata for %s", fab.name);
146 /* Check for our own errors */
148 if (xbt_set_get_by_name_or_null(pd->libdata, fab.name) != NULL)
149 THROWF(unknown_error, 0,
150 "MayDay: two modules use '%s' as libdata name", fab.name);
152 /* Add the data in place, after some more sanity checking */
153 elem = fab.constructor();
154 if (elem->name_len && elem->name_len != strlen(elem->name)) {
155 elem->name_len = strlen(elem->name);
157 ("Module '%s' constructor is borken: it does not set elem->name_len",
160 xbt_set_add(pd->libdata, elem, fab.destructor);
164 void gras_procdata_exit()
167 gras_procdata_t *pd = gras_procdata_get();
169 xbt_set_free(&(pd->libdata));
171 /* Remove procdata in reverse order wrt creation */
172 while ((len = xbt_dynar_length(_gras_procdata_fabrics))) {
173 xbt_dynar_remove_at(_gras_procdata_fabrics, len - 1, NULL);
175 xbt_dynar_free(&_gras_procdata_fabrics);
179 const char *gras_os_hostport()
181 static char *res = NULL;
182 free(res); /* my port may have changed */
183 res = bprintf("%s:%d", gras_os_myname(), gras_os_myport());
184 return (const char *) res;