Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2e2ed98e57822a675f3ba97a2f28183df0482de6
[simgrid.git] / src / gras / Virtu / sg_process.c
1 /* process_sg - GRAS process handling on simulator                          */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 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/dict.h"
11 #include "gras_modinter.h"      /* module initialization interface */
12 #include "gras/Virtu/virtu_sg.h"
13 #include "gras/Msg/msg_interface.h"     /* For some checks at simulation end */
14 #include "gras/Transport/transport_interface.h" /* For some checks at simulation end */
15 #if HAVE_LUA
16 #include <lua.h>
17 #include <lauxlib.h>
18 #include <lualib.h>
19 #endif
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
21
22 static long int PID = 1;
23
24
25 void gras_agent_spawn(const char *name,
26                       xbt_main_func_t code, int argc, char *argv[],
27                       xbt_dict_t properties)
28 {
29
30   SIMIX_process_create(name, code, NULL,
31                        gras_os_myname(), argc, argv, properties);
32 }
33
34 /* **************************************************************************
35  * Process constructor/destructor (semi-public interface)
36  * **************************************************************************/
37
38 void gras_process_init()
39 {
40   gras_hostdata_t *hd =
41       (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
42   gras_procdata_t *pd = xbt_new0(gras_procdata_t, 1);
43   gras_trp_procdata_t trp_pd;
44
45   SIMIX_process_set_data(SIMIX_process_self(), (void *) pd);
46
47
48   gras_procdata_init();
49
50   if (!hd) {
51     /* First process on this host */
52     hd = xbt_new(gras_hostdata_t, 1);
53     hd->refcount = 1;
54     hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t), NULL);
55     SIMIX_host_set_data(SIMIX_host_self(), (void *) hd);
56   } else {
57     hd->refcount++;
58   }
59
60   trp_pd = (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
61   pd->pid = PID++;
62
63   if (SIMIX_process_self() != NULL) {
64     pd->ppid = gras_os_getpid();
65   } else
66     pd->ppid = -1;
67
68   trp_pd->msg_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
69
70   trp_pd->meas_selectable_sockets =
71       xbt_queue_new(0, sizeof(gras_socket_t));
72
73   VERB2("Creating process '%s' (%d)",
74         SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
75 }
76
77 void gras_process_exit()
78 {
79   xbt_dynar_t sockets =
80       ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
81   gras_socket_t sock_iter;
82   unsigned int cursor;
83   gras_hostdata_t *hd =
84       (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
85   gras_procdata_t *pd =
86       (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
87
88   gras_msg_procdata_t msg_pd =
89       (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
90   gras_trp_procdata_t trp_pd =
91       (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
92
93   xbt_queue_free(&trp_pd->msg_selectable_sockets);
94
95   xbt_queue_free(&trp_pd->meas_selectable_sockets);
96
97
98   xbt_assert0(hd, "Run gras_process_init (ie, gras_init)!!");
99
100   VERB2("GRAS: Finalizing process '%s' (%d)",
101         SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
102
103   if (xbt_dynar_length(msg_pd->msg_queue)) {
104     unsigned int cpt;
105     s_gras_msg_t msg;
106     WARN2
107         ("process %d terminated, but %ld messages are still queued. Message list:",
108          gras_os_getpid(), xbt_dynar_length(msg_pd->msg_queue));
109     xbt_dynar_foreach(msg_pd->msg_queue, cpt, msg) {
110       WARN5("   Message %s (%s) from %s@%s:%d", msg.type->name,
111             e_gras_msg_kind_names[msg.kind],
112             gras_socket_peer_proc(msg.expe),
113             gras_socket_peer_name(msg.expe),
114             gras_socket_peer_port(msg.expe));
115     }
116   }
117
118   /* if each process has its sockets list, we need to close them when the
119      process finish */
120   xbt_dynar_foreach(sockets, cursor, sock_iter) {
121     VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
122           sock_iter);
123     gras_socket_close(sock_iter);
124   }
125   if (!--(hd->refcount)) {
126     xbt_dynar_free(&hd->ports);
127     free(hd);
128   }
129   gras_procdata_exit();
130   free(pd);
131 }
132
133 /* **************************************************************************
134  * Process data (public interface)
135  * **************************************************************************/
136
137 gras_procdata_t *gras_procdata_get(void)
138 {
139   gras_procdata_t *pd =
140       (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
141
142   xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
143
144   return pd;
145 }
146
147 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
148 {
149   gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
150
151   xbt_assert2(pd,
152               "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
153               SIMIX_process_get_name(p),
154               SIMIX_host_get_name(SIMIX_process_get_host(p)));
155
156   return gras_libdata_by_name_from_procdata(name, pd);
157 }
158
159 /** @brief retrieve the value of a given process property (or NULL if not defined) */
160 const char *gras_process_property_value(const char *name)
161 {
162   return xbt_dict_get_or_null(
163                SIMIX_process_get_properties(SIMIX_process_self()), 
164                                             name);
165 }
166
167 /** @brief retrieve the process properties dictionnary
168  *  @warning it's the original one, not a copy. Don't mess with it
169  */
170 xbt_dict_t gras_process_properties(void)
171 {
172   return SIMIX_process_get_properties(SIMIX_process_self());
173 }
174
175 /* **************************************************************************
176  * OS virtualization function
177  * **************************************************************************/
178
179 const char *xbt_procname(void)
180 {
181   smx_process_t process = SIMIX_process_self();
182   /*FIXME: maestro used not have a simix process, now it does so 
183      SIMIX_process_self will return something different to NULL. This breaks
184      the old xbt_log logic that assumed that NULL was equivalent to maestro,
185      thus when printing it searches for maestro host name (which doesn't exists)
186      and breaks the logging.
187      As a hack we check for maestro by looking to the assigned host, if it is
188      NULL then we are sure is maestro
189    */
190   if (process != NULL && SIMIX_process_get_host(process))
191     return SIMIX_process_get_name(process);
192
193   return "";
194 }
195
196 int gras_os_getpid(void)
197 {
198   gras_procdata_t *data;
199   smx_process_t process = SIMIX_process_self();
200
201   if (process != NULL) {
202     data = (gras_procdata_t *) SIMIX_process_get_data(process);
203     if (data != NULL)
204       return data->pid;
205   }
206
207   return 0;
208 }
209
210 /** @brief retrieve the value of a given host property (or NULL if not defined) */
211 const char *gras_os_host_property_value(const char *name)
212 {
213   return
214       xbt_dict_get_or_null(SIMIX_host_get_properties
215                            (SIMIX_process_get_host(SIMIX_process_self())),
216                            name);
217 }
218
219 /** @brief retrieve the host properties dictionnary
220  *  @warning it's the original one, not a copy. Don't mess with it
221  */
222 xbt_dict_t gras_os_host_properties(void)
223 {
224   return
225       SIMIX_host_get_properties(SIMIX_process_get_host
226                                 (SIMIX_process_self()));
227 }
228
229 /* **************************************************************************
230  * Interface with SIMIX
231  * (these functions are called by the stuff generated by gras_stub_generator)
232  * **************************************************************************/
233
234 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
235 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
236
237 void gras_global_init(int *argc, char **argv)
238 {
239   XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
240   SIMIX_global_init(argc, argv);
241 }
242
243 void gras_create_environment(const char *file)
244 {
245   SIMIX_create_environment(file);
246 }
247
248 void gras_function_register(const char *name, xbt_main_func_t code)
249 {
250   SIMIX_function_register(name, code);
251 }
252
253 void gras_function_register_default(xbt_main_func_t code)
254 {
255   SIMIX_function_register_default(code);
256 }
257
258 void gras_main()
259 {
260   /* Clean IO before the run */
261   fflush(stdout);
262   fflush(stderr);
263   SIMIX_init();
264
265   while (SIMIX_solve(NULL, NULL) != -1.0);
266
267   return;
268 }
269
270 void gras_launch_application(const char *file)
271 {
272   SIMIX_launch_application(file);
273 }
274
275 void gras_load_environment_script(const char *script_file)
276 {
277 #ifdef HAVE_LUA
278   lua_State *L = lua_open();
279   luaL_openlibs(L);
280
281   if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
282     printf("error: %s\n", lua_tostring(L, -1));
283     return;
284   }
285 #else
286   xbt_die
287       ("Lua is not available!! to call gras_load_environment_script, lua should be available...");
288 #endif
289   return;
290 }
291
292 void gras_clean()
293 {
294   SIMIX_clean();
295 }