3 /* process_sg - GRAS process handling on simulator */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
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. */
12 #include "gras_modinter.h" /* module initialization interface */
13 #include "gras/Virtu/virtu_sg.h"
14 #include "gras/Msg/msg_interface.h" /* For some checks at simulation end */
15 #include "gras/Transport/transport_interface.h" /* For some checks at simulation end */
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
19 static long int PID = 1;
22 void gras_agent_spawn(const char *name, void *data,
23 xbt_main_func_t code, int argc, char *argv[],
24 xbt_dict_t properties)
27 SIMIX_process_create(name, code,
28 data, gras_os_myname(), argc, argv, properties);
31 /* **************************************************************************
32 * Process constructor/destructor (semi-public interface)
33 * **************************************************************************/
35 void gras_process_init()
38 (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
39 gras_procdata_t *pd = xbt_new0(gras_procdata_t, 1);
40 gras_trp_procdata_t trp_pd;
42 SIMIX_process_set_data(SIMIX_process_self(), (void *) pd);
48 /* First process on this host */
49 hd = xbt_new(gras_hostdata_t, 1);
51 hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t), NULL);
52 SIMIX_host_set_data(SIMIX_host_self(), (void *) hd);
57 trp_pd = (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
60 if (SIMIX_process_self() != NULL) {
61 pd->ppid = gras_os_getpid();
65 trp_pd->msg_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
67 trp_pd->meas_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
69 VERB2("Creating process '%s' (%d)",
70 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
73 void gras_process_exit()
76 ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
77 gras_socket_t sock_iter;
80 (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
82 (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
84 gras_msg_procdata_t msg_pd =
85 (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
86 gras_trp_procdata_t trp_pd =
87 (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
89 xbt_queue_free(&trp_pd->msg_selectable_sockets);
91 xbt_queue_free(&trp_pd->meas_selectable_sockets);
94 xbt_assert0(hd, "Run gras_process_init (ie, gras_init)!!");
96 VERB2("GRAS: Finalizing process '%s' (%d)",
97 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
99 if (xbt_dynar_length(msg_pd->msg_queue))
100 WARN1("process %d terminated, but some messages are still queued",
103 /* if each process has its sockets list, we need to close them when the
105 xbt_dynar_foreach(sockets, cursor, sock_iter) {
106 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
108 gras_socket_close(sock_iter);
110 if (!--(hd->refcount)) {
111 xbt_dynar_free(&hd->ports);
114 gras_procdata_exit();
118 /* **************************************************************************
119 * Process data (public interface)
120 * **************************************************************************/
122 gras_procdata_t *gras_procdata_get(void)
124 gras_procdata_t *pd =
125 (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
127 xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
132 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
134 gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
137 "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
138 SIMIX_process_get_name(p),
139 SIMIX_host_get_name(SIMIX_process_get_host(p)));
141 return gras_libdata_by_name_from_procdata(name, pd);
144 /** @brief retrieve the value of a given process property (or NULL if not defined) */
145 const char *gras_process_property_value(const char *name)
148 xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()),
152 /** @brief retrieve the process properties dictionnary
153 * @warning it's the original one, not a copy. Don't mess with it
155 xbt_dict_t gras_process_properties(void)
157 return SIMIX_process_get_properties(SIMIX_process_self());
160 /* **************************************************************************
161 * OS virtualization function
162 * **************************************************************************/
164 const char *xbt_procname(void)
166 const char *res = NULL;
167 smx_process_t process = SIMIX_process_self();
168 if ((process != NULL) && (process->simdata))
169 res = SIMIX_process_get_name(process);
176 int gras_os_getpid(void)
179 smx_process_t process = SIMIX_process_self();
181 if ((process != NULL) && (process->data))
182 return ((gras_procdata_t *) process->data)->pid;
188 /** @brief retrieve the value of a given host property (or NULL if not defined) */
189 const char *gras_os_host_property_value(const char *name)
192 xbt_dict_get_or_null(SIMIX_host_get_properties
193 (SIMIX_process_get_host(SIMIX_process_self())),
197 /** @brief retrieve the host properties dictionnary
198 * @warning it's the original one, not a copy. Don't mess with it
200 xbt_dict_t gras_os_host_properties(void)
203 SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
206 /* **************************************************************************
207 * Interface with SIMIX
208 * (these functions are called by the stuff generated by gras_stub_generator)
209 * **************************************************************************/
211 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
212 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
214 void gras_global_init(int *argc, char **argv)
216 XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
217 SIMIX_global_init(argc, argv);
220 void gras_create_environment(const char *file)
222 SIMIX_create_environment(file);
225 void gras_function_register(const char *name, xbt_main_func_t code)
227 SIMIX_function_register(name, code);
232 smx_cond_t cond = NULL;
234 xbt_fifo_t actions_done = xbt_fifo_new();
235 xbt_fifo_t actions_failed = xbt_fifo_new();
237 /* Clean IO before the run */
242 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
243 while ((action = xbt_fifo_pop(actions_failed))) {
244 DEBUG1("** %s failed **", action->name);
245 while ((cond = xbt_fifo_pop(action->cond_list))) {
246 SIMIX_cond_broadcast(cond);
248 /* action finished, destroy it */
249 // SIMIX_action_destroy(action);
252 while ((action = xbt_fifo_pop(actions_done))) {
253 DEBUG1("** %s done **", action->name);
254 while ((cond = xbt_fifo_pop(action->cond_list))) {
255 SIMIX_cond_broadcast(cond);
257 /* action finished, destroy it */
258 //SIMIX_action_destroy(action);
261 xbt_fifo_free(actions_failed);
262 xbt_fifo_free(actions_done);
266 void gras_launch_application(const char *file)
268 SIMIX_launch_application(file);