Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
If it is a win plateform we have to use wenvironment against environment.
[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 = xbt_dynar_new(sizeof(s_gras_procdata_fabric_t),
51                                            gras_procdata_fabric_free);
52   }
53
54   fab = xbt_dynar_push_ptr(_gras_procdata_fabrics);
55
56   fab->name = xbt_strdup(name);
57   fab->constructor = constructor;
58   fab->destructor = destructor;
59
60   return xbt_dynar_length(_gras_procdata_fabrics) - 1;
61 }
62
63 /* **************************************************************************
64  * Process data
65  * **************************************************************************/
66 void *gras_userdata_get(void)
67 {
68   gras_procdata_t *pd = gras_procdata_get();
69   return pd->userdata;
70 }
71
72 void *gras_userdata_set(void *ud)
73 {
74   gras_procdata_t *pd = gras_procdata_get();
75
76   pd->userdata = ud;
77   return ud;
78 }
79
80 void *gras_libdata_by_name(const char *name)
81 {
82   gras_procdata_t *pd = gras_procdata_get();
83   return gras_libdata_by_name_from_procdata(name, pd);
84 }
85
86 /* this function is splitted from previous to allow SG to get libdata from remote hosts */
87 void *gras_libdata_by_name_from_procdata(const char *name,
88                                          gras_procdata_t * pd)
89 {
90   void *res = NULL;
91   xbt_ex_t e;
92   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
93     /* Damn, some new modules were added since procdata_init(). Amok? */
94     /* Get 'em all */
95     gras_procdata_init();
96   }
97   TRY {
98     res = xbt_set_get_by_name(pd->libdata, name);
99   }
100   CATCH(e) {
101     RETHROW1("Cannot retrieve the libdata associated to %s: %s", name);
102   }
103   return res;
104 }
105
106 void *gras_libdata_by_id(int id)
107 {
108   gras_procdata_t *pd = gras_procdata_get();
109   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
110     /* Damn, some new modules were added since procdata_init(). Amok? */
111     /* Get 'em all */
112     gras_procdata_init();
113   }
114   return xbt_set_get_by_id(pd->libdata, id);
115 }
116
117
118 void gras_procdata_init()
119 {
120   gras_procdata_t *pd = gras_procdata_get();
121   s_gras_procdata_fabric_t fab;
122
123   unsigned int cursor;
124
125   xbt_ex_t e;
126   xbt_set_elm_t elem;
127
128   if (!pd->libdata) {
129     pd->userdata = NULL;
130     pd->libdata = xbt_set_new();
131   }
132
133   xbt_dynar_foreach(_gras_procdata_fabrics, cursor, fab) {
134     volatile int found = 0;
135
136     if (cursor + 1 <= xbt_set_length(pd->libdata)) {
137       DEBUG2("Skip fabric %d: there is already %ld libdata",
138              cursor, xbt_set_length(pd->libdata));
139       continue;                 /* allow to recall this function to get recently added fabrics */
140     }
141     DEBUG2("Go ahead for cursor %d, there is %ld libdata",
142            cursor, xbt_set_length(pd->libdata));
143
144     xbt_assert1(fab.name, "Name of fabric #%d is NULL!", cursor);
145     DEBUG1("Create the procdata for %s", fab.name);
146     /* Check for our own errors */
147     TRY {
148       xbt_set_get_by_name(pd->libdata, fab.name);
149       found = 1;
150     }
151     CATCH(e) {
152       xbt_ex_free(e);
153       found = 0;
154     }
155     if (found)
156       THROW1(unknown_error, 0, "MayDay: two modules use '%s' as libdata name",
157              fab.name);
158
159     /* Add the data in place, after some more sanity checking */
160     elem = (*(fab.constructor)) ();
161     if (elem->name_len && elem->name_len != strlen(elem->name)) {
162       elem->name_len = strlen(elem->name);
163       WARN1
164         ("Module '%s' constructor is borken: it does not set elem->name_len",
165          fab.name);
166     }
167     xbt_set_add(pd->libdata, elem, fab.destructor);
168   }
169 }
170
171 void gras_procdata_exit()
172 {
173   int len;
174   gras_procdata_t *pd = gras_procdata_get();
175
176   xbt_set_free(&(pd->libdata));
177
178   /* Remove procdata in reverse order wrt creation */
179   while ((len = xbt_dynar_length(_gras_procdata_fabrics))) {
180     xbt_dynar_remove_at(_gras_procdata_fabrics, len - 1, NULL);
181   }
182   xbt_dynar_free(&_gras_procdata_fabrics);
183 }
184
185
186 const char *gras_os_hostport()
187 {
188   static char *res = NULL;
189   if (res)
190     free(res);                  /* my port may have changed */
191   res = bprintf("%s:%d", gras_os_myname(), gras_os_myport());
192   return (const char *) res;
193 }