1 /* process_sg - GRAS process handling on simulator */
3 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "gras_modinter.h" /* module initialization interface */
11 #include "gras/Virtu/virtu_sg.h"
12 #include "gras/Msg/msg_interface.h" /* For some checks at simulation end */
13 #include "gras/Transport/transport_interface.h" /* For some checks at simulation end */
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
17 static long int PID = 1;
20 void gras_agent_spawn(const char *name, void *data,
21 xbt_main_func_t code, int argc, char *argv[],
22 xbt_dict_t properties)
25 SIMIX_process_create(name, code,
26 data, gras_os_myname(), argc, argv, properties);
29 /* **************************************************************************
30 * Process constructor/destructor (semi-public interface)
31 * **************************************************************************/
33 void gras_process_init()
36 (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
37 gras_procdata_t *pd = xbt_new0(gras_procdata_t, 1);
38 gras_trp_procdata_t trp_pd;
40 SIMIX_process_set_data(SIMIX_process_self(), (void *) pd);
46 /* First process on this host */
47 hd = xbt_new(gras_hostdata_t, 1);
49 hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t), NULL);
50 SIMIX_host_set_data(SIMIX_host_self(), (void *) hd);
55 trp_pd = (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
58 if (SIMIX_process_self() != NULL) {
59 pd->ppid = gras_os_getpid();
63 trp_pd->msg_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
65 trp_pd->meas_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
67 VERB2("Creating process '%s' (%d)",
68 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
71 void gras_process_exit()
74 ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
75 gras_socket_t sock_iter;
78 (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
80 (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
82 gras_msg_procdata_t msg_pd =
83 (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
84 gras_trp_procdata_t trp_pd =
85 (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
87 xbt_queue_free(&trp_pd->msg_selectable_sockets);
89 xbt_queue_free(&trp_pd->meas_selectable_sockets);
92 xbt_assert0(hd, "Run gras_process_init (ie, gras_init)!!");
94 VERB2("GRAS: Finalizing process '%s' (%d)",
95 SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
97 if (xbt_dynar_length(msg_pd->msg_queue)) {
100 WARN2("process %d terminated, but %ld messages are still queued. Message list:",
101 gras_os_getpid(),xbt_dynar_length(msg_pd->msg_queue));
102 xbt_dynar_foreach(msg_pd->msg_queue,cpt, msg) {
103 WARN5(" Message %s (%s) from %s@%s:%d",msg.type->name,e_gras_msg_kind_names[msg.kind],
104 gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
108 /* if each process has its sockets list, we need to close them when the
110 xbt_dynar_foreach(sockets, cursor, sock_iter) {
111 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
113 gras_socket_close(sock_iter);
115 if (!--(hd->refcount)) {
116 xbt_dynar_free(&hd->ports);
119 gras_procdata_exit();
123 /* **************************************************************************
124 * Process data (public interface)
125 * **************************************************************************/
127 gras_procdata_t *gras_procdata_get(void)
129 gras_procdata_t *pd =
130 (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
132 xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
137 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
139 gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
142 "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
143 SIMIX_process_get_name(p),
144 SIMIX_host_get_name(SIMIX_process_get_host(p)));
146 return gras_libdata_by_name_from_procdata(name, pd);
149 /** @brief retrieve the value of a given process property (or NULL if not defined) */
150 const char *gras_process_property_value(const char *name)
153 xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()),
157 /** @brief retrieve the process properties dictionnary
158 * @warning it's the original one, not a copy. Don't mess with it
160 xbt_dict_t gras_process_properties(void)
162 return SIMIX_process_get_properties(SIMIX_process_self());
165 /* **************************************************************************
166 * OS virtualization function
167 * **************************************************************************/
169 const char *xbt_procname(void)
171 smx_process_t process = SIMIX_process_self();
172 /*FIXME: maestro used not have a simix process, now it does so
173 SIMIX_process_self will return something different to NULL. This breaks
174 the old xbt_log logic that assumed that NULL was equivalent to maestro,
175 thus when printing it searches for maestro host name (which doesn't exists)
176 and breaks the logging.
177 As a hack we check for maestro by looking to the assigned host, if it is
178 NULL then we are sure is maestro
180 if (process != NULL && SIMIX_process_get_host(process))
181 return SIMIX_process_get_name(process);
186 int gras_os_getpid(void)
188 gras_procdata_t *data;
189 smx_process_t process = SIMIX_process_self();
191 if (process != NULL){
192 data = (gras_procdata_t *)SIMIX_process_get_data(process);
200 /** @brief retrieve the value of a given host property (or NULL if not defined) */
201 const char *gras_os_host_property_value(const char *name)
204 xbt_dict_get_or_null(SIMIX_host_get_properties
205 (SIMIX_process_get_host(SIMIX_process_self())),
209 /** @brief retrieve the host properties dictionnary
210 * @warning it's the original one, not a copy. Don't mess with it
212 xbt_dict_t gras_os_host_properties(void)
215 SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
218 /* **************************************************************************
219 * Interface with SIMIX
220 * (these functions are called by the stuff generated by gras_stub_generator)
221 * **************************************************************************/
223 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
224 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
226 void gras_global_init(int *argc, char **argv)
228 XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
229 SIMIX_global_init(argc, argv);
232 void gras_create_environment(const char *file)
234 SIMIX_create_environment(file);
237 void gras_function_register(const char *name, xbt_main_func_t code)
239 SIMIX_function_register(name, code);
244 /* Clean IO before the run */
249 while (SIMIX_solve(NULL, NULL) != -1.0);
254 void gras_launch_application(const char *file)
256 SIMIX_launch_application(file);