Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
781ab579f263580a7449e9c67485862e06e8fb0a
[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   smx_process_t process;
31   simcall_process_create(&process, name, code, NULL,
32                            gras_os_myname(), argc, argv, properties);
33 }
34
35 /* **************************************************************************
36  * Process constructor/destructor (semi-public interface)
37  * **************************************************************************/
38
39 void gras_process_init()
40 {
41   smx_process_t self = SIMIX_process_self();
42   gras_hostdata_t *hd =
43       (gras_hostdata_t *) SIMIX_host_self_get_data();
44   gras_procdata_t *pd = xbt_new0(gras_procdata_t, 1);
45   gras_trp_procdata_t trp_pd;
46   long int pid = PID++; /* make sure the first process gets the first id */
47
48   if (!hd) {
49     /* First process on this host (FIXME: does not work if the SIMIX user contexts are truly parallel) */
50     hd = xbt_new(gras_hostdata_t, 1);
51     hd->refcount = 1;
52     hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t), NULL);
53     SIMIX_host_self_set_data((void *) hd);
54   } else {
55     hd->refcount++;
56   }
57
58   SIMIX_process_self_set_data(self, (void *) pd);
59   gras_procdata_init();
60
61   trp_pd = (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
62   pd->pid = pid;
63
64   if (self != NULL) {
65     pd->ppid = gras_os_getpid();
66   } else
67     pd->ppid = -1;
68
69   trp_pd->msg_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
70
71   trp_pd->meas_selectable_sockets =
72       xbt_queue_new(0, sizeof(gras_socket_t));
73
74   XBT_VERB("Creating process '%s' (%d)", SIMIX_process_self_get_name(),
75       gras_os_getpid());
76 }
77
78 void gras_process_exit()
79 {
80   xbt_dynar_t sockets =
81       ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
82   gras_socket_t sock_iter;
83   unsigned int cursor;
84   gras_hostdata_t *hd =
85       (gras_hostdata_t *) SIMIX_host_self_get_data();
86   gras_procdata_t *pd =
87       (gras_procdata_t *) simcall_process_get_data(SIMIX_process_self());
88
89   gras_msg_procdata_t msg_pd =
90       (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
91   gras_trp_procdata_t trp_pd =
92       (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
93
94   xbt_queue_free(&trp_pd->msg_selectable_sockets);
95
96   xbt_queue_free(&trp_pd->meas_selectable_sockets);
97
98
99   xbt_assert(hd, "Run gras_process_init (ie, gras_init)!!");
100
101   XBT_VERB("GRAS: Finalizing process '%s' (%d)",
102         simcall_process_get_name(SIMIX_process_self()), gras_os_getpid());
103
104   if (!xbt_dynar_is_empty(msg_pd->msg_queue)) {
105     unsigned int cpt;
106     s_gras_msg_t msg;
107     XBT_WARN
108         ("process %d terminated, but %ld messages are still queued. Message list:",
109          gras_os_getpid(), xbt_dynar_length(msg_pd->msg_queue));
110     xbt_dynar_foreach(msg_pd->msg_queue, cpt, msg) {
111       XBT_WARN("   Message %s (%s) from %s@%s:%d", msg.type->name,
112             e_gras_msg_kind_names[msg.kind],
113             gras_socket_peer_proc(msg.expe),
114             gras_socket_peer_name(msg.expe),
115             gras_socket_peer_port(msg.expe));
116     }
117   }
118
119   /* if each process has its sockets list, we need to close them when the
120      process finish */
121   xbt_dynar_foreach(sockets, cursor, sock_iter) {
122     XBT_VERB("Closing the socket %p left open on exit. Maybe a socket leak?",
123           sock_iter);
124     gras_socket_close(sock_iter);
125   }
126   if (!--(hd->refcount)) {
127     xbt_dynar_free(&hd->ports);
128     free(hd);
129   }
130   gras_procdata_exit();
131   free(pd);
132 }
133
134 /* **************************************************************************
135  * Process data (public interface)
136  * **************************************************************************/
137
138 gras_procdata_t *gras_procdata_get(void)
139 {
140   gras_procdata_t *pd =
141       (gras_procdata_t *) simcall_process_get_data(SIMIX_process_self());
142
143   xbt_assert(pd, "Run gras_process_init! (ie, gras_init)");
144
145   return pd;
146 }
147
148 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
149 {
150   gras_procdata_t *pd = (gras_procdata_t *) simcall_process_get_data(p);
151
152   xbt_assert(pd,
153               "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
154               simcall_process_get_name(p),
155               simcall_host_get_name(simcall_process_get_host(p)));
156
157   return gras_libdata_by_name_from_procdata(name, pd);
158 }
159
160 /** @brief retrieve the value of a given process property (or NULL if not defined) */
161 const char *gras_process_property_value(const char *name)
162 {
163   return xbt_dict_get_or_null(simcall_process_get_properties
164                              (SIMIX_process_self()), 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 simcall_process_get_properties(SIMIX_process_self());
173 }
174
175 /* **************************************************************************
176  * OS virtualization function
177  * **************************************************************************/
178
179
180 int gras_os_getpid(void)
181 {
182   gras_procdata_t *data;
183   data = (gras_procdata_t *) SIMIX_process_self_get_data(SIMIX_process_self());
184   if (data != NULL)
185     return data->pid;
186
187   return 0;
188 }
189
190 /** @brief retrieve the value of a given host property (or NULL if not defined) */
191 const char *gras_os_host_property_value(const char *name)
192 {
193   return
194       xbt_dict_get_or_null(simcall_host_get_properties
195                            (simcall_process_get_host(SIMIX_process_self())),
196                            name);
197 }
198
199 /** @brief retrieve the host properties dictionary
200  *  @warning it's the original one, not a copy. Don't mess with it
201  */
202 xbt_dict_t gras_os_host_properties(void)
203 {
204   return
205       simcall_host_get_properties(simcall_process_get_host
206                                 (SIMIX_process_self()));
207 }
208
209 /* **************************************************************************
210  * Interface with SIMIX
211  * (these functions are called by the stuff generated by gras_stub_generator)
212  * **************************************************************************/
213
214 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
215 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
216
217 void gras_global_init(int *argc, char **argv)
218 {
219   XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
220   SIMIX_global_init(argc, argv);
221 }
222
223 void gras_create_environment(const char *file)
224 {
225   SIMIX_create_environment(file);
226 }
227
228 void gras_function_register(const char *name, xbt_main_func_t code)
229 {
230   SIMIX_function_register(name, code);
231 }
232
233 void gras_function_register_default(xbt_main_func_t code)
234 {
235   SIMIX_function_register_default(code);
236 }
237
238 void gras_main()
239 {
240   /* Clean IO before the run */
241   fflush(stdout);
242   fflush(stderr);
243   SIMIX_run();
244
245   return;
246 }
247
248 void gras_launch_application(const char *file)
249 {
250   SIMIX_launch_application(file);
251 }
252
253 void gras_load_environment_script(const char *script_file)
254 {
255 #ifdef HAVE_LUA
256   lua_State *L = lua_open();
257   luaL_openlibs(L);
258
259   if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
260     printf("error: %s\n", lua_tostring(L, -1));
261     return;
262   }
263 #else
264   xbt_die
265       ("Lua is not available!! to call gras_load_environment_script, lua should be available...");
266 #endif
267   return;
268 }
269
270 void gras_clean()
271 {
272   SIMIX_clean();
273 }