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[], xbt_dict_t properties) {
25 SIMIX_process_create(name, code,
28 argc, argv, properties);
31 /* **************************************************************************
32 * Process constructor/destructor (semi-public interface)
33 * **************************************************************************/
37 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
38 gras_procdata_t *pd=xbt_new0(gras_procdata_t,1);
39 gras_trp_procdata_t trp_pd;
41 SIMIX_process_set_data(SIMIX_process_self(),(void*)pd);
47 /* First process on this host */
48 hd=xbt_new(gras_hostdata_t,1);
50 hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL);
51 SIMIX_host_set_data(SIMIX_host_self(),(void*)hd);
56 trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
59 if (SIMIX_process_self() != NULL ) {
60 pd->ppid = gras_os_getpid();
64 trp_pd->msg_selectable_sockets = xbt_queue_new(0,sizeof(gras_socket_t));
66 trp_pd->meas_selectable_sockets = xbt_queue_new(0,sizeof(gras_socket_t));
68 VERB2("Creating process '%s' (%d)",
69 SIMIX_process_get_name(SIMIX_process_self()),
75 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
76 gras_socket_t sock_iter;
79 (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
81 (gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
83 gras_msg_procdata_t msg_pd=
84 (gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
85 gras_trp_procdata_t trp_pd=
86 (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
88 xbt_queue_free(&trp_pd->msg_selectable_sockets);
90 xbt_queue_free(&trp_pd->meas_selectable_sockets);
93 xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
95 VERB2("GRAS: Finalizing process '%s' (%d)",
96 SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
98 if (xbt_dynar_length(msg_pd->msg_queue))
99 WARN1("process %d terminated, but some messages are still queued",
102 /* if each process has its sockets list, we need to close them when the
104 xbt_dynar_foreach(sockets,cursor,sock_iter) {
105 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
107 gras_socket_close(sock_iter);
109 if ( ! --(hd->refcount)) {
110 xbt_dynar_free(&hd->ports);
113 gras_procdata_exit();
117 /* **************************************************************************
118 * Process data (public interface)
119 * **************************************************************************/
121 gras_procdata_t *gras_procdata_get(void) {
123 (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
125 xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
130 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
132 (gras_procdata_t *)SIMIX_process_get_data(p);
134 xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
135 SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
137 return gras_libdata_by_name_from_procdata(name, pd);
141 * \brief Returns the value of a property for the current gras process
143 * \return the value of the property
145 const char* gras_process_property_value(char* name)
147 return xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()), name);
151 * \brief Returns the dictionary of properties for the current gras process
153 * \return the dictionary
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) {
165 const char *res = NULL;
166 smx_process_t process = SIMIX_process_self();
167 if ((process != NULL) && (process->simdata))
168 res = SIMIX_process_get_name(process);
175 int gras_os_getpid(void) {
177 smx_process_t process = SIMIX_process_self();
179 if ((process != NULL) && (process->data))
180 return ((gras_procdata_t*)process->data)->pid;
186 * \brief Returns the value of a property for the current gras os
188 * \return the value of the property
190 const char* gras_os_property_value(char* name)
192 return xbt_dict_get_or_null(SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self())), name);
196 * \brief Returns the dictionary of properties for the gras host
198 * \return the dictionary
200 xbt_dict_t gras_os_host_properties(void)
202 return SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
205 /* **************************************************************************
206 * Interface with SIMIX
207 * **************************************************************************/
209 void gras_global_init(int *argc,char **argv) {
210 SIMIX_global_init(argc,argv);
212 void gras_create_environment(const char *file) {
213 SIMIX_create_environment(file);
215 void gras_function_register(const char *name, xbt_main_func_t code) {
216 SIMIX_function_register(name, code);
220 smx_cond_t cond = NULL;
222 xbt_fifo_t actions_done = xbt_fifo_new();
223 xbt_fifo_t actions_failed = xbt_fifo_new();
225 /* Clean IO before the run */
229 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
230 while ( (action = xbt_fifo_pop(actions_failed)) ) {
231 DEBUG1("** %s failed **",action->name);
232 while ( (cond = xbt_fifo_pop(action->cond_list)) ) {
233 SIMIX_cond_broadcast(cond);
235 /* action finished, destroy it */
236 // SIMIX_action_destroy(action);
239 while ( (action = xbt_fifo_pop(actions_done)) ) {
240 DEBUG1("** %s done **",action->name);
241 while ( (cond = xbt_fifo_pop(action->cond_list)) ) {
242 SIMIX_cond_broadcast(cond);
244 /* action finished, destroy it */
245 //SIMIX_action_destroy(action);
248 xbt_fifo_free(actions_failed);
249 xbt_fifo_free(actions_done);
253 void gras_launch_application(const char *file) {
254 SIMIX_launch_application(file);