Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fea5400ad2f486bdc44b8a84c4bdfd7c67ac39b5
[simgrid.git] / src / gras / Virtu / process.c
1 /* $Id$ */
2
3 /* process - GRAS process handling (common code for RL and SG)              */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/ex.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "gras/transport.h"
14 #include "gras/datadesc.h"
15 #include "gras/messages.h"
16 #include "gras_modinter.h"
17
18 #include "gras/Virtu/virtu_private.h"
19
20 XBT_LOG_NEW_SUBCATEGORY(gras_virtu, gras, "Virtualization code");
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_process, gras_virtu,
22                                 "Process manipulation code");
23
24 /* Functions to handle gras_procdata_t->libdata cells*/
25 typedef struct {
26   char *name;
27   pvoid_f_void_t constructor;
28   void_f_pvoid_t destructor;
29 } s_gras_procdata_fabric_t, *gras_procdata_fabric_t;
30
31 static xbt_dynar_t _gras_procdata_fabrics = NULL;       /* content: s_gras_procdata_fabric_t */
32
33 static void gras_procdata_fabric_free(void *fab)
34 {
35   free(((gras_procdata_fabric_t) fab)->name);
36 }
37
38 /** @brief declare the functions in charge of creating/destructing the procdata of a module
39  *  
40  *  This is intended to be called from the gras_<module>_register function.
41  *  This returns the module ID you can use for gras_libdata_by_id()
42  */
43 int gras_procdata_add(const char *name, pvoid_f_void_t constructor,
44                       void_f_pvoid_t destructor)
45 {
46
47   gras_procdata_fabric_t fab;
48
49   if (!_gras_procdata_fabrics) {
50     /* create the dynar if needed */
51     _gras_procdata_fabrics = 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) < xbt_dynar_length(_gras_procdata_fabrics)) {
94     /* Damn, some new modules were added since procdata_init(). Amok? */
95     /* Get 'em all */
96     gras_procdata_init();
97   }
98   TRY {
99     res = xbt_set_get_by_name(pd->libdata, name);
100   }
101   CATCH(e) {
102     RETHROW1("Cannot retrieve the libdata associated to %s: %s", name);
103   }
104   return res;
105 }
106
107 void *gras_libdata_by_id(int id)
108 {
109   gras_procdata_t *pd = gras_procdata_get();
110   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
111     /* Damn, some new modules were added since procdata_init(). Amok? */
112     /* Get 'em all */
113     gras_procdata_init();
114   }
115   return xbt_set_get_by_id(pd->libdata, id);
116 }
117
118
119 void gras_procdata_init()
120 {
121   gras_procdata_t *pd = gras_procdata_get();
122   s_gras_procdata_fabric_t fab;
123
124   unsigned int cursor;
125
126   xbt_ex_t e;
127   xbt_set_elm_t elem;
128
129   if (!pd->libdata) {
130     pd->userdata = NULL;
131     pd->libdata = xbt_set_new();
132   }
133
134   xbt_dynar_foreach(_gras_procdata_fabrics, cursor, fab) {
135     volatile int found = 0;
136
137     if (cursor + 1 <= xbt_set_length(pd->libdata)) {
138       DEBUG2("Skip fabric %d: there is already %ld libdata",
139              cursor, xbt_set_length(pd->libdata));
140       continue;                 /* allow to recall this function to get recently added fabrics */
141     }
142     DEBUG2("Go ahead for cursor %d, there is %ld libdata",
143            cursor, xbt_set_length(pd->libdata));
144
145     xbt_assert1(fab.name, "Name of fabric #%d is NULL!", cursor);
146     DEBUG1("Create the procdata for %s", fab.name);
147     /* Check for our own errors */
148     TRY {
149       xbt_set_get_by_name(pd->libdata, fab.name);
150       found = 1;
151     }
152     CATCH(e) {
153       xbt_ex_free(e);
154       found = 0;
155     }
156     if (found)
157       THROW1(unknown_error, 0, "MayDay: two modules use '%s' as libdata name",
158              fab.name);
159
160     /* Add the data in place, after some more sanity checking */
161     elem = (*(fab.constructor)) ();
162     if (elem->name_len && elem->name_len != strlen(elem->name)) {
163       elem->name_len = strlen(elem->name);
164       WARN1
165         ("Module '%s' constructor is borken: it does not set elem->name_len",
166          fab.name);
167     }
168     xbt_set_add(pd->libdata, elem, fab.destructor);
169   }
170 }
171
172 void gras_procdata_exit()
173 {
174   int len;
175   gras_procdata_t *pd = gras_procdata_get();
176
177   xbt_set_free(&(pd->libdata));
178
179   /* Remove procdata in reverse order wrt creation */
180   while ((len = xbt_dynar_length(_gras_procdata_fabrics))) {
181     xbt_dynar_remove_at(_gras_procdata_fabrics, len - 1, NULL);
182   }
183   xbt_dynar_free(&_gras_procdata_fabrics);
184 }
185
186
187 const char *gras_os_hostport()
188 {
189   static char *res = NULL;
190   if (res)
191     free(res);                  /* my port may have changed */
192   res = bprintf("%s:%d", gras_os_myname(), gras_os_myport());
193   return (const char *) res;
194 }