Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
39615deceb9fdfff3f612f67294fc09bfa28a9e7
[simgrid.git] / src / gras / Virtu / process.c
1 /* process - GRAS process handling (common code for RL and SG)              */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/ex.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "gras/transport.h"
13 #include "gras/datadesc.h"
14 #include "gras/messages.h"
15 #include "gras_modinter.h"
16
17 #include "gras/Virtu/virtu_private.h"
18
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");
22
23 /* Functions to handle gras_procdata_t->libdata cells*/
24 typedef struct {
25   char *name;
26   pvoid_f_void_t constructor;
27   void_f_pvoid_t destructor;
28 } s_gras_procdata_fabric_t, *gras_procdata_fabric_t;
29
30 static xbt_dynar_t _gras_procdata_fabrics = NULL;       /* content: s_gras_procdata_fabric_t */
31
32 static void gras_procdata_fabric_free(void *fab)
33 {
34   free(((gras_procdata_fabric_t) fab)->name);
35 }
36
37 /** @brief declare the functions in charge of creating/destructing the procdata of a module
38  *  
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()
41  */
42 int gras_procdata_add(const char *name, pvoid_f_void_t constructor,
43                       void_f_pvoid_t destructor)
44 {
45
46   gras_procdata_fabric_t fab;
47
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);
53   }
54
55   fab = xbt_dynar_push_ptr(_gras_procdata_fabrics);
56
57   fab->name = xbt_strdup(name);
58   fab->constructor = constructor;
59   fab->destructor = destructor;
60
61   return xbt_dynar_length(_gras_procdata_fabrics) - 1;
62 }
63
64 /* **************************************************************************
65  * Process data
66  * **************************************************************************/
67 void *gras_userdata_get(void)
68 {
69   gras_procdata_t *pd = gras_procdata_get();
70   return pd->userdata;
71 }
72
73 void *gras_userdata_set(void *ud)
74 {
75   gras_procdata_t *pd = gras_procdata_get();
76
77   pd->userdata = ud;
78   return ud;
79 }
80
81 void *gras_libdata_by_name(const char *name)
82 {
83   gras_procdata_t *pd = gras_procdata_get();
84   return gras_libdata_by_name_from_procdata(name, pd);
85 }
86
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,
89                                          gras_procdata_t * pd)
90 {
91   void *res = NULL;
92   xbt_ex_t e;
93   if (xbt_set_length(pd->libdata) <
94       xbt_dynar_length(_gras_procdata_fabrics)) {
95     /* Damn, some new modules were added since procdata_init(). Amok? */
96     /* Get 'em all */
97     gras_procdata_init();
98   }
99   TRY {
100     res = xbt_set_get_by_name(pd->libdata, name);
101   }
102   CATCH(e) {
103     RETHROWF("Cannot retrieve the libdata associated to %s: %s", name);
104   }
105   return res;
106 }
107
108 void *gras_libdata_by_id(int id)
109 {
110   gras_procdata_t *pd = gras_procdata_get();
111   if (xbt_set_length(pd->libdata) <
112       xbt_dynar_length(_gras_procdata_fabrics)) {
113     /* Damn, some new modules were added since procdata_init(). Amok? */
114     /* Get 'em all */
115     gras_procdata_init();
116   }
117   return xbt_set_get_by_id(pd->libdata, id);
118 }
119
120
121 void gras_procdata_init()
122 {
123   gras_procdata_t *pd = gras_procdata_get();
124   s_gras_procdata_fabric_t fab;
125
126   unsigned int cursor;
127
128   xbt_ex_t e;
129   xbt_set_elm_t elem;
130
131   if (!pd->libdata) {
132     pd->userdata = NULL;
133     pd->libdata = xbt_set_new();
134   }
135
136   xbt_dynar_foreach(_gras_procdata_fabrics, cursor, fab) {
137     volatile int found = 0;
138
139     if (cursor + 1 <= xbt_set_length(pd->libdata)) {
140       XBT_DEBUG("Skip fabric %d: there is already %ld libdata",
141              cursor, xbt_set_length(pd->libdata));
142       continue;                 /* allow to recall this function to get recently added fabrics */
143     }
144     XBT_DEBUG("Go ahead for cursor %d, there is %ld libdata",
145            cursor, xbt_set_length(pd->libdata));
146
147     xbt_assert1(fab.name, "Name of fabric #%d is NULL!", cursor);
148     XBT_DEBUG("Create the procdata for %s", fab.name);
149     /* Check for our own errors */
150     TRY {
151       xbt_set_get_by_name(pd->libdata, fab.name);
152       found = 1;
153     }
154     CATCH(e) {
155       xbt_ex_free(e);
156       found = 0;
157     }
158     if (found)
159       THROWF(unknown_error, 0,
160              "MayDay: two modules use '%s' as libdata name", fab.name);
161
162     /* Add the data in place, after some more sanity checking */
163     elem = (*(fab.constructor)) ();
164     if (elem->name_len && elem->name_len != strlen(elem->name)) {
165       elem->name_len = strlen(elem->name);
166       XBT_WARN
167           ("Module '%s' constructor is borken: it does not set elem->name_len",
168            fab.name);
169     }
170     xbt_set_add(pd->libdata, elem, fab.destructor);
171   }
172 }
173
174 void gras_procdata_exit()
175 {
176   int len;
177   gras_procdata_t *pd = gras_procdata_get();
178
179   xbt_set_free(&(pd->libdata));
180
181   /* Remove procdata in reverse order wrt creation */
182   while ((len = xbt_dynar_length(_gras_procdata_fabrics))) {
183     xbt_dynar_remove_at(_gras_procdata_fabrics, len - 1, NULL);
184   }
185   xbt_dynar_free(&_gras_procdata_fabrics);
186 }
187
188
189 const char *gras_os_hostport()
190 {
191   static char *res = NULL;
192   if (res)
193     free(res);                  /* my port may have changed */
194   res = bprintf("%s:%d", gras_os_myname(), gras_os_myport());
195   return (const char *) res;
196 }