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,
30 /* **************************************************************************
31 * Process constructor/destructor (semi-public interface)
32 * **************************************************************************/
36 gras_hostdata_t *hd=(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()),
74 xbt_dynar_t sockets = ((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))
98 WARN1("process %d terminated, but some messages are still queued",
101 /* if each process has its sockets list, we need to close them when the
103 xbt_dynar_foreach(sockets,cursor,sock_iter) {
104 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
106 gras_socket_close(sock_iter);
108 if ( ! --(hd->refcount)) {
109 xbt_dynar_free(&hd->ports);
112 gras_procdata_exit();
116 /* **************************************************************************
117 * Process data (public interface)
118 * **************************************************************************/
120 gras_procdata_t *gras_procdata_get(void) {
122 (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
124 xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
129 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
131 (gras_procdata_t *)SIMIX_process_get_data(p);
133 xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
134 SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
136 return gras_libdata_by_name_from_procdata(name, pd);
139 /* **************************************************************************
140 * OS virtualization function
141 * **************************************************************************/
143 const char* xbt_procname(void) {
144 const char *res = NULL;
145 smx_process_t process = SIMIX_process_self();
146 if ((process != NULL) && (process->simdata))
147 res = SIMIX_process_get_name(process);
154 int gras_os_getpid(void) {
156 smx_process_t process = SIMIX_process_self();
158 if ((process != NULL) && (process->data))
159 return ((gras_procdata_t*)process->data)->pid;
164 /* **************************************************************************
165 * Interface with SIMIX
166 * **************************************************************************/
168 void gras_global_init(int *argc,char **argv) {
169 return SIMIX_global_init(argc,argv);
171 void gras_create_environment(const char *file) {
172 return SIMIX_create_environment(file);
174 void gras_function_register(const char *name, xbt_main_func_t code) {
175 return SIMIX_function_register(name, code);
179 smx_cond_t cond = NULL;
181 xbt_fifo_t actions_done = xbt_fifo_new();
182 xbt_fifo_t actions_failed = xbt_fifo_new();
184 /* Clean IO before the run */
188 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
189 while ( (action = xbt_fifo_pop(actions_failed)) ) {
190 DEBUG1("** %s failed **",action->name);
191 while ( (cond = xbt_fifo_pop(action->cond_list)) ) {
192 SIMIX_cond_broadcast(cond);
194 /* action finished, destroy it */
195 // SIMIX_action_destroy(action);
198 while ( (action = xbt_fifo_pop(actions_done)) ) {
199 DEBUG1("** %s done **",action->name);
200 while ( (cond = xbt_fifo_pop(action->cond_list)) ) {
201 SIMIX_cond_broadcast(cond);
203 /* action finished, destroy it */
204 //SIMIX_action_destroy(action);
207 xbt_fifo_free(actions_failed);
208 xbt_fifo_free(actions_done);
212 void gras_launch_application(const char *file) {
213 return SIMIX_launch_application(file);
217 return SIMIX_clean();