Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
48cd2732ecaebd8765150db0a77a7fbf7918b90a
[simgrid.git] / src / gras / Virtu / sg_process.c
1 /* $Id$ */
2
3 /* process_sg - GRAS process handling on simulator                          */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
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. */
9
10 #include "xbt/ex.h"
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 */
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
17
18 static long int PID = 1;
19
20
21 void gras_agent_spawn(const char *name, void *data, 
22                       xbt_main_func_t code, int argc, char *argv[]) {
23    
24    SIMIX_process_create(name, code,
25                         data,
26                         gras_os_myname(), 
27                         argc, argv);
28 }
29
30 /* **************************************************************************
31  * Process constructor/destructor (semi-public interface)
32  * **************************************************************************/
33
34 void
35 gras_process_init() {
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;
39
40   SIMIX_process_set_data(SIMIX_process_self(),(void*)pd);
41
42
43   gras_procdata_init();
44
45   if (!hd) {
46     /* First process on this host */
47     hd=xbt_new(gras_hostdata_t,1);
48     hd->refcount = 1;
49     hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL);
50                 SIMIX_host_set_data(SIMIX_host_self(),(void*)hd);
51   } else {
52     hd->refcount++;
53   }
54
55   trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
56   pd->pid = PID++;
57   
58   if (SIMIX_process_self() != NULL ) {
59     pd->ppid = gras_os_getpid();
60   }
61   else pd->ppid = -1; 
62   
63   trp_pd->msg_selectable_sockets = xbt_fifo_new();
64   trp_pd->msg_select_mutex = SIMIX_mutex_init();
65   trp_pd->msg_select_cond = SIMIX_cond_init();
66   
67   trp_pd->meas_selectable_sockets = xbt_fifo_new();
68   trp_pd->meas_select_mutex = SIMIX_mutex_init();
69   trp_pd->meas_select_cond = SIMIX_cond_init();
70   
71   VERB2("Creating process '%s' (%d)",
72         SIMIX_process_get_name(SIMIX_process_self()),
73         gras_os_getpid());
74 }
75
76 void
77 gras_process_exit() {
78         xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
79   gras_socket_t sock_iter;
80   int cursor;
81   gras_hostdata_t *hd=
82     (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
83   gras_procdata_t *pd=
84     (gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
85
86   gras_msg_procdata_t msg_pd=
87     (gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
88   gras_trp_procdata_t trp_pd=
89     (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
90
91   SIMIX_mutex_destroy(trp_pd->msg_select_mutex);
92   SIMIX_cond_destroy(trp_pd->msg_select_cond);
93   xbt_fifo_free(trp_pd->msg_selectable_sockets);
94
95   SIMIX_mutex_destroy(trp_pd->meas_select_mutex);
96   SIMIX_cond_destroy(trp_pd->meas_select_cond);
97   xbt_fifo_free(trp_pd->meas_selectable_sockets);
98
99
100   xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
101
102   VERB2("GRAS: Finalizing process '%s' (%d)",
103         SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
104
105   if (xbt_dynar_length(msg_pd->msg_queue))
106     WARN1("process %d terminated, but some messages are still queued",
107           gras_os_getpid());
108   
109   /* if each process has its sockets list, we need to close them when the
110            process finish */
111   xbt_dynar_foreach(sockets,cursor,sock_iter) {
112     VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
113           sock_iter);
114     gras_socket_close(sock_iter);
115   }
116   if ( ! --(hd->refcount)) {
117     xbt_dynar_free(&hd->ports);
118     free(hd);
119   }
120   gras_procdata_exit();
121   free(pd);
122 }
123
124 /* **************************************************************************
125  * Process data (public interface)
126  * **************************************************************************/
127
128 gras_procdata_t *gras_procdata_get(void) {
129   gras_procdata_t *pd=
130     (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
131
132   xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
133
134   return pd;
135 }
136 void *
137 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
138   gras_procdata_t *pd=
139     (gras_procdata_t *)SIMIX_process_get_data(p);
140
141   xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)", 
142               SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
143    
144   return gras_libdata_by_name_from_procdata(name, pd);
145 }   
146
147 /* **************************************************************************
148  * OS virtualization function
149  * **************************************************************************/
150
151 const char* xbt_procname(void) {
152   const char *res = NULL;
153   smx_process_t process = SIMIX_process_self();
154   if ((process != NULL) && (process->simdata))
155     res = SIMIX_process_get_name(process);
156   if (res) 
157     return res;
158   else
159     return "";
160 }
161
162 int gras_os_getpid(void) {
163
164   smx_process_t process = SIMIX_process_self();
165         
166   if ((process != NULL) && (process->data))
167      return ((gras_procdata_t*)process->data)->pid;
168   else
169     return 0;
170 }
171
172 /* **************************************************************************
173  * Interface with SIMIX
174  * **************************************************************************/
175
176 void gras_global_init(int *argc,char **argv) {
177    return SIMIX_global_init(argc,argv);
178 }
179 void gras_create_environment(const char *file) {
180    return SIMIX_create_environment(file);
181 }
182 void gras_function_register(const char *name, xbt_main_func_t code) {
183    return SIMIX_function_register(name, code);
184 }
185 void gras_main() {
186    smx_cond_t cond = NULL;
187    smx_action_t smx_action;
188    xbt_fifo_t actions_done = xbt_fifo_new();
189    xbt_fifo_t actions_failed = xbt_fifo_new();
190    
191
192    /* Clean IO before the run */
193    fflush(stdout);
194    fflush(stderr);
195    
196    
197    while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
198
199       while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
200          
201          
202          DEBUG1("** %s failed **",smx_action->name);
203          while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
204             SIMIX_cond_broadcast(cond);
205                         }
206          /* action finished, destroy it */
207          //     SIMIX_action_destroy(smx_action);
208       }
209       
210       while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
211          
212          DEBUG1("** %s done **",smx_action->name);
213          while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
214             SIMIX_cond_broadcast(cond);
215          }
216          /* action finished, destroy it */
217          //SIMIX_action_destroy(smx_action);
218       }
219    }
220    xbt_fifo_free(actions_failed);
221    xbt_fifo_free(actions_done);
222    return;   
223 }
224 void gras_launch_application(const char *file) {
225    return SIMIX_launch_application(file);
226 }
227 void gras_clean() {
228    return SIMIX_clean();
229 }
230
231