Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
* Cleanup the DTD by renaming:
[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 "xbt/dict.h"
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 */
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
18
19 static long int PID = 1;
20
21
22 void gras_agent_spawn(const char *name, void *data, 
23                       xbt_main_func_t code, int argc, char *argv[], xbt_dict_t properties) {
24    
25    SIMIX_process_create(name, code,
26                         data,
27                         gras_os_myname(), 
28                         argc, argv, properties);
29 }
30
31 /* **************************************************************************
32  * Process constructor/destructor (semi-public interface)
33  * **************************************************************************/
34
35 void
36 gras_process_init() {
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;
40
41   SIMIX_process_set_data(SIMIX_process_self(),(void*)pd);
42
43
44   gras_procdata_init();
45
46   if (!hd) {
47     /* First process on this host */
48     hd=xbt_new(gras_hostdata_t,1);
49     hd->refcount = 1;
50     hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL);
51                 SIMIX_host_set_data(SIMIX_host_self(),(void*)hd);
52   } else {
53     hd->refcount++;
54   }
55
56   trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
57   pd->pid = PID++;
58   
59   if (SIMIX_process_self() != NULL ) {
60     pd->ppid = gras_os_getpid();
61   }
62   else pd->ppid = -1; 
63   
64   trp_pd->msg_selectable_sockets = xbt_queue_new(0,sizeof(gras_socket_t));
65   
66   trp_pd->meas_selectable_sockets = xbt_queue_new(0,sizeof(gras_socket_t));
67   
68   VERB2("Creating process '%s' (%d)",
69         SIMIX_process_get_name(SIMIX_process_self()),
70         gras_os_getpid());
71 }
72
73 void
74 gras_process_exit() {
75         xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
76   gras_socket_t sock_iter;
77   int cursor;
78   gras_hostdata_t *hd=
79     (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
80   gras_procdata_t *pd=
81     (gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
82
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");
87
88   xbt_queue_free(&trp_pd->msg_selectable_sockets);
89
90   xbt_queue_free(&trp_pd->meas_selectable_sockets);
91
92
93   xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
94
95   VERB2("GRAS: Finalizing process '%s' (%d)",
96         SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
97
98   if (xbt_dynar_length(msg_pd->msg_queue))
99     WARN1("process %d terminated, but some messages are still queued",
100           gras_os_getpid());
101   
102   /* if each process has its sockets list, we need to close them when the
103            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  * \brief Returns the value of a property for the current gras process
142  *
143  * \return the value of the property
144  */
145 const char* gras_process_property_value(char* name)
146 {
147  return xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()), name);
148 }
149
150 /**
151  * \brief Returns the dictionary of properties for the current gras process
152  *
153  * \return the dictionary
154  */
155 xbt_dict_t gras_process_properties(void)
156 {
157   return SIMIX_process_get_properties(SIMIX_process_self());
158 }
159
160 /* **************************************************************************
161  * OS virtualization function
162  * **************************************************************************/
163
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);
169   if (res) 
170     return res;
171   else
172     return "";
173 }
174
175 int gras_os_getpid(void) {
176
177   smx_process_t process = SIMIX_process_self();
178         
179   if ((process != NULL) && (process->data))
180      return ((gras_procdata_t*)process->data)->pid;
181   else
182     return 0;
183 }
184
185 /**
186  * \brief Returns the value of a property for the current gras os
187  *
188  * \return the value of the property
189  */
190 const char* gras_os_property_value(char* name)
191 {
192  return xbt_dict_get_or_null(SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self())), name);
193 }
194
195 /**
196  * \brief Returns the dictionary of properties for the gras host
197  *
198  * \return the dictionary
199  */
200 xbt_dict_t gras_os_host_properties(void)
201 {
202   return SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
203 }
204
205 /* **************************************************************************
206  * Interface with SIMIX
207  * **************************************************************************/
208
209 void gras_global_init(int *argc,char **argv) {
210    SIMIX_global_init(argc,argv);
211 }
212 void gras_create_environment(const char *file) {
213    SIMIX_create_environment(file);
214 }
215 void gras_function_register(const char *name, xbt_main_func_t code) {
216    SIMIX_function_register(name, code);
217 }
218
219 void gras_main() {
220   smx_cond_t cond = NULL;
221   smx_action_t action;
222   xbt_fifo_t actions_done = xbt_fifo_new();
223   xbt_fifo_t actions_failed = xbt_fifo_new();
224    
225   /* Clean IO before the run */
226   fflush(stdout);
227   fflush(stderr);
228
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);
234       }
235       /* action finished, destroy it */
236       //        SIMIX_action_destroy(action);
237     }
238     
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);
243       }
244       /* action finished, destroy it */
245       //SIMIX_action_destroy(action);
246     }
247   }
248   xbt_fifo_free(actions_failed);
249   xbt_fifo_free(actions_done);
250   return;   
251 }
252
253 void gras_launch_application(const char *file) {
254    SIMIX_launch_application(file);
255 }
256
257 void gras_clean() {
258    SIMIX_clean();
259 }
260
261