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. */
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 */
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
18 static long int PID = 1;
21 void gras_agent_spawn(const char *name, void *data,
22 xbt_main_func_t code, int argc, char *argv[]) {
24 SIMIX_process_create(name, code,
28 /* no cleanup, thx, users will call gras_exit() */NULL);
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->mutex = SIMIX_mutex_init();
65 trp_pd->cond = SIMIX_cond_init();
66 trp_pd->mutex_meas = SIMIX_mutex_init();
67 trp_pd->cond_meas = SIMIX_cond_init();
68 trp_pd->active_socket = xbt_fifo_new();
69 trp_pd->active_socket_meas = xbt_fifo_new();
71 VERB2("Creating process '%s' (%d)",
72 SIMIX_process_get_name(SIMIX_process_self()),
78 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
79 gras_socket_t sock_iter;
81 gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
82 gras_procdata_t *pd=(gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
84 gras_msg_procdata_t msg_pd=(gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
85 gras_trp_procdata_t trp_pd=(gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
87 SIMIX_mutex_destroy(trp_pd->mutex);
88 SIMIX_cond_destroy(trp_pd->cond);
89 xbt_fifo_free(trp_pd->active_socket);
90 SIMIX_mutex_destroy(trp_pd->mutex_meas);
91 SIMIX_cond_destroy(trp_pd->cond_meas);
92 xbt_fifo_free(trp_pd->active_socket_meas);
95 xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
97 VERB2("GRAS: Finalizing process '%s' (%d)",
98 SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
100 if (xbt_dynar_length(msg_pd->msg_queue))
101 WARN1("process %d terminated, but some messages are still queued",
104 /* if each process has its sockets list, we need to close them when the process finish */
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 *)SIMIX_process_get_data(SIMIX_process_self());
126 xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
131 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
133 (gras_procdata_t *)SIMIX_process_get_data(p);
135 xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
136 SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
138 return gras_libdata_by_name_from_procdata(name, pd);
141 /* **************************************************************************
142 * OS virtualization function
143 * **************************************************************************/
145 const char* xbt_procname(void) {
146 const char *res = NULL;
147 smx_process_t process = SIMIX_process_self();
148 if ((process != NULL) && (process->simdata))
149 res = SIMIX_process_get_name(process);
156 int gras_os_getpid(void) {
158 smx_process_t process = SIMIX_process_self();
160 if ((process != NULL) && (process->data))
161 return ((gras_procdata_t*)process->data)->pid;
166 /* **************************************************************************
167 * Interface with SIMIX
168 * **************************************************************************/
170 void gras_global_init(int *argc,char **argv) {
171 return SIMIX_global_init(argc,argv);
173 void gras_create_environment(const char *file) {
174 return SIMIX_create_environment(file);
176 void gras_function_register(const char *name, xbt_main_func_t code) {
177 return SIMIX_function_register(name, code);
180 smx_cond_t cond = NULL;
181 smx_action_t smx_action;
182 xbt_fifo_t actions_done = xbt_fifo_new();
183 xbt_fifo_t actions_failed = xbt_fifo_new();
186 /* Clean IO before the run */
191 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
193 while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
196 DEBUG1("** %s failed **",smx_action->name);
197 while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
198 SIMIX_cond_broadcast(cond);
200 /* action finished, destroy it */
201 // SIMIX_action_destroy(smx_action);
204 while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
206 DEBUG1("** %s done **",smx_action->name);
207 while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
208 SIMIX_cond_broadcast(cond);
210 /* action finished, destroy it */
211 //SIMIX_action_destroy(smx_action);
214 xbt_fifo_free(actions_failed);
215 xbt_fifo_free(actions_done);
218 void gras_launch_application(const char *file) {
219 return SIMIX_launch_application(file);
222 return SIMIX_clean();