Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f985c86f3176a8b32ec3a24ffdf70d3360da2373
[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->mutex = SIMIX_mutex_init();
64         trp_pd->cond = SIMIX_cond_init();
65         trp_pd->mutex_meas = SIMIX_mutex_init();
66         trp_pd->cond_meas = SIMIX_cond_init();
67         trp_pd->active_socket = xbt_fifo_new();
68         trp_pd->active_socket_meas = xbt_fifo_new();
69
70   VERB2("Creating process '%s' (%d)",
71            SIMIX_process_get_name(SIMIX_process_self()),
72            gras_os_getpid());
73 }
74
75 void
76 gras_process_exit() {
77         xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
78   gras_socket_t sock_iter;
79   int cursor;
80   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
81   gras_procdata_t *pd=(gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
82
83   gras_msg_procdata_t msg_pd=(gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
84   gras_trp_procdata_t trp_pd=(gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
85
86         SIMIX_mutex_destroy(trp_pd->mutex);
87         SIMIX_cond_destroy(trp_pd->cond);
88         xbt_fifo_free(trp_pd->active_socket);
89         SIMIX_mutex_destroy(trp_pd->mutex_meas);
90         SIMIX_cond_destroy(trp_pd->cond_meas);
91         xbt_fifo_free(trp_pd->active_socket_meas);
92
93
94   xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
95
96   VERB2("GRAS: Finalizing process '%s' (%d)",
97         SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
98
99   if (xbt_dynar_length(msg_pd->msg_queue))
100     WARN1("process %d terminated, but some messages are still queued",
101           gras_os_getpid());
102
103         /* if each process has its sockets list, we need to close them when the process finish */
104         xbt_dynar_foreach(sockets,cursor,sock_iter) {
105                 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
106                                 sock_iter);
107                 gras_socket_close(sock_iter);
108         }
109   if ( ! --(hd->refcount)) {
110     xbt_dynar_free(&hd->ports);
111     free(hd);
112   }
113   gras_procdata_exit();
114   free(pd);
115 }
116
117 /* **************************************************************************
118  * Process data (public interface)
119  * **************************************************************************/
120
121 gras_procdata_t *gras_procdata_get(void) {
122   gras_procdata_t *pd=
123     (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
124
125   xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
126
127   return pd;
128 }
129 void *
130 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
131   gras_procdata_t *pd=
132     (gras_procdata_t *)SIMIX_process_get_data(p);
133
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)));
136    
137   return gras_libdata_by_name_from_procdata(name, pd);
138 }   
139
140 /* **************************************************************************
141  * OS virtualization function
142  * **************************************************************************/
143
144 const char* xbt_procname(void) {
145   const char *res = NULL;
146   smx_process_t process = SIMIX_process_self();
147   if ((process != NULL) && (process->simdata))
148     res = SIMIX_process_get_name(process);
149   if (res) 
150     return res;
151   else
152     return "";
153 }
154
155 int gras_os_getpid(void) {
156
157   smx_process_t process = SIMIX_process_self();
158         
159   if ((process != NULL) && (process->data))
160      return ((gras_procdata_t*)process->data)->pid;
161   else
162     return 0;
163 }
164
165 /* **************************************************************************
166  * Interface with SIMIX
167  * **************************************************************************/
168
169 void gras_global_init(int *argc,char **argv) {
170    return SIMIX_global_init(argc,argv);
171 }
172 void gras_create_environment(const char *file) {
173    return SIMIX_create_environment(file);
174 }
175 void gras_function_register(const char *name, xbt_main_func_t code) {
176    return SIMIX_function_register(name, code);
177 }
178 void gras_main() {
179    smx_cond_t cond = NULL;
180    smx_action_t smx_action;
181    xbt_fifo_t actions_done = xbt_fifo_new();
182    xbt_fifo_t actions_failed = xbt_fifo_new();
183    
184
185    /* Clean IO before the run */
186    fflush(stdout);
187    fflush(stderr);
188    
189    
190    while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
191
192       while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
193          
194          
195          DEBUG1("** %s failed **",smx_action->name);
196          while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
197             SIMIX_cond_broadcast(cond);
198                         }
199          /* action finished, destroy it */
200          //     SIMIX_action_destroy(smx_action);
201       }
202       
203       while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
204          
205          DEBUG1("** %s done **",smx_action->name);
206          while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
207             SIMIX_cond_broadcast(cond);
208          }
209          /* action finished, destroy it */
210          //SIMIX_action_destroy(smx_action);
211       }
212    }
213    xbt_fifo_free(actions_failed);
214    xbt_fifo_free(actions_done);
215    return;   
216 }
217 void gras_launch_application(const char *file) {
218    return SIMIX_launch_application(file);
219 }
220 void gras_clean() {
221    return SIMIX_clean();
222 }
223
224