1 /* process_sg - GRAS process handling on simulator */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
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 */
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
22 static long int PID = 1;
25 void gras_agent_spawn(const char *name, void *data,
26 xbt_main_func_t code, int argc, char *argv[],
27 xbt_dict_t properties)
30 SIMIX_process_create(name, code,
31 data, gras_os_myname(), argc, argv, properties);
34 /* **************************************************************************
35 * Process constructor/destructor (semi-public interface)
36 * **************************************************************************/
38 void gras_process_init()
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;
45 SIMIX_process_set_data(SIMIX_process_self(), (void *) pd);
51 /* First process on this host */
52 hd = xbt_new(gras_hostdata_t, 1);
54 hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t), NULL);
55 SIMIX_host_set_data(SIMIX_host_self(), (void *) hd);
60 trp_pd = (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
63 if (SIMIX_process_self() != NULL) {
64 pd->ppid = gras_os_getpid();
68 trp_pd->msg_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
70 trp_pd->meas_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
72 VERB2("Creating process '%s' (%d)",
73 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
76 void gras_process_exit()
79 ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
80 gras_socket_t sock_iter;
83 (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
85 (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
87 gras_msg_procdata_t msg_pd =
88 (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
89 gras_trp_procdata_t trp_pd =
90 (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
92 xbt_queue_free(&trp_pd->msg_selectable_sockets);
94 xbt_queue_free(&trp_pd->meas_selectable_sockets);
97 xbt_assert0(hd, "Run gras_process_init (ie, gras_init)!!");
99 VERB2("GRAS: Finalizing process '%s' (%d)",
100 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
102 if (xbt_dynar_length(msg_pd->msg_queue)) {
105 WARN2("process %d terminated, but %ld messages are still queued. Message list:",
106 gras_os_getpid(),xbt_dynar_length(msg_pd->msg_queue));
107 xbt_dynar_foreach(msg_pd->msg_queue,cpt, msg) {
108 WARN5(" Message %s (%s) from %s@%s:%d",msg.type->name,e_gras_msg_kind_names[msg.kind],
109 gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
113 /* if each process has its sockets list, we need to close them when the
115 xbt_dynar_foreach(sockets, cursor, sock_iter) {
116 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
118 gras_socket_close(sock_iter);
120 if (!--(hd->refcount)) {
121 xbt_dynar_free(&hd->ports);
124 gras_procdata_exit();
128 /* **************************************************************************
129 * Process data (public interface)
130 * **************************************************************************/
132 gras_procdata_t *gras_procdata_get(void)
134 gras_procdata_t *pd =
135 (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
137 xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
142 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
144 gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
147 "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
148 SIMIX_process_get_name(p),
149 SIMIX_host_get_name(SIMIX_process_get_host(p)));
151 return gras_libdata_by_name_from_procdata(name, pd);
154 /** @brief retrieve the value of a given process property (or NULL if not defined) */
155 const char *gras_process_property_value(const char *name)
158 xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()),
162 /** @brief retrieve the process properties dictionnary
163 * @warning it's the original one, not a copy. Don't mess with it
165 xbt_dict_t gras_process_properties(void)
167 return SIMIX_process_get_properties(SIMIX_process_self());
170 /* **************************************************************************
171 * OS virtualization function
172 * **************************************************************************/
174 const char *xbt_procname(void)
176 smx_process_t process = SIMIX_process_self();
177 /*FIXME: maestro used not have a simix process, now it does so
178 SIMIX_process_self will return something different to NULL. This breaks
179 the old xbt_log logic that assumed that NULL was equivalent to maestro,
180 thus when printing it searches for maestro host name (which doesn't exists)
181 and breaks the logging.
182 As a hack we check for maestro by looking to the assigned host, if it is
183 NULL then we are sure is maestro
185 if (process != NULL && SIMIX_process_get_host(process))
186 return SIMIX_process_get_name(process);
191 int gras_os_getpid(void)
193 gras_procdata_t *data;
194 smx_process_t process = SIMIX_process_self();
196 if (process != NULL){
197 data = (gras_procdata_t *)SIMIX_process_get_data(process);
205 /** @brief retrieve the value of a given host property (or NULL if not defined) */
206 const char *gras_os_host_property_value(const char *name)
209 xbt_dict_get_or_null(SIMIX_host_get_properties
210 (SIMIX_process_get_host(SIMIX_process_self())),
214 /** @brief retrieve the host properties dictionnary
215 * @warning it's the original one, not a copy. Don't mess with it
217 xbt_dict_t gras_os_host_properties(void)
220 SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
223 /* **************************************************************************
224 * Interface with SIMIX
225 * (these functions are called by the stuff generated by gras_stub_generator)
226 * **************************************************************************/
228 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
229 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
231 void gras_global_init(int *argc, char **argv)
233 XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
234 SIMIX_global_init(argc, argv);
237 void gras_create_environment(const char *file)
239 SIMIX_create_environment(file);
242 void gras_function_register(const char *name, xbt_main_func_t code)
244 SIMIX_function_register(name, code);
247 void gras_function_register_default(xbt_main_func_t code)
249 SIMIX_function_register_default(code);
254 /* Clean IO before the run */
259 while (SIMIX_solve(NULL, NULL) != -1.0);
264 void gras_launch_application(const char *file)
266 SIMIX_launch_application(file);
269 void gras_load_environment_script(const char* script_file)
272 lua_State *L = lua_open();
275 if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
276 printf("error: %s\n", lua_tostring(L, -1));
280 xbt_die("Lua is not available!! to call gras_load_environment_script, lua should be available...");