Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0f1889e23d51a61926951293fcc2e8d429a8037a
[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[],
24                       xbt_dict_t properties)
25 {
26
27   SIMIX_process_create(name, code,
28                        data, gras_os_myname(), argc, argv, properties);
29 }
30
31 /* **************************************************************************
32  * Process constructor/destructor (semi-public interface)
33  * **************************************************************************/
34
35 void gras_process_init()
36 {
37   gras_hostdata_t *hd =
38     (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
39   gras_procdata_t *pd = xbt_new0(gras_procdata_t, 1);
40   gras_trp_procdata_t trp_pd;
41
42   SIMIX_process_set_data(SIMIX_process_self(), (void *) pd);
43
44
45   gras_procdata_init();
46
47   if (!hd) {
48     /* First process on this host */
49     hd = xbt_new(gras_hostdata_t, 1);
50     hd->refcount = 1;
51     hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t), NULL);
52     SIMIX_host_set_data(SIMIX_host_self(), (void *) hd);
53   } else {
54     hd->refcount++;
55   }
56
57   trp_pd = (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
58   pd->pid = PID++;
59
60   if (SIMIX_process_self() != NULL) {
61     pd->ppid = gras_os_getpid();
62   } else
63     pd->ppid = -1;
64
65   trp_pd->msg_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
66
67   trp_pd->meas_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
68
69   VERB2("Creating process '%s' (%d)",
70         SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
71 }
72
73 void gras_process_exit()
74 {
75   xbt_dynar_t sockets =
76     ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
77   gras_socket_t sock_iter;
78   unsigned int cursor;
79   gras_hostdata_t *hd =
80     (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
81   gras_procdata_t *pd =
82     (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
83
84   gras_msg_procdata_t msg_pd =
85     (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
86   gras_trp_procdata_t trp_pd =
87     (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
88
89   xbt_queue_free(&trp_pd->msg_selectable_sockets);
90
91   xbt_queue_free(&trp_pd->meas_selectable_sockets);
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     unsigned int cpt;
101     s_gras_msg_t msg;
102     WARN2("process %d terminated, but %ld messages are still queued. Message list:",
103           gras_os_getpid(),xbt_dynar_length(msg_pd->msg_queue));
104     xbt_dynar_foreach(msg_pd->msg_queue,cpt, msg) {
105       WARN5("   Message %s (%s) from %s@%s:%d",msg.type->name,e_gras_msg_kind_names[msg.kind],
106           gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
107     }
108   }
109
110   /* if each process has its sockets list, we need to close them when the
111      process finish */
112   xbt_dynar_foreach(sockets, cursor, sock_iter) {
113     VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
114           sock_iter);
115     gras_socket_close(sock_iter);
116   }
117   if (!--(hd->refcount)) {
118     xbt_dynar_free(&hd->ports);
119     free(hd);
120   }
121   gras_procdata_exit();
122   free(pd);
123 }
124
125 /* **************************************************************************
126  * Process data (public interface)
127  * **************************************************************************/
128
129 gras_procdata_t *gras_procdata_get(void)
130 {
131   gras_procdata_t *pd =
132     (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
133
134   xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
135
136   return pd;
137 }
138
139 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
140 {
141   gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
142
143   xbt_assert2(pd,
144               "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
145               SIMIX_process_get_name(p),
146               SIMIX_host_get_name(SIMIX_process_get_host(p)));
147
148   return gras_libdata_by_name_from_procdata(name, pd);
149 }
150
151 /** @brief retrieve the value of a given process property (or NULL if not defined) */
152 const char *gras_process_property_value(const char *name)
153 {
154   return
155     xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()),
156                          name);
157 }
158
159 /** @brief retrieve the process properties dictionnary
160  *  @warning it's the original one, not a copy. Don't mess with it
161  */
162 xbt_dict_t gras_process_properties(void)
163 {
164   return SIMIX_process_get_properties(SIMIX_process_self());
165 }
166
167 /* **************************************************************************
168  * OS virtualization function
169  * **************************************************************************/
170
171 const char *xbt_procname(void)
172 {
173   const char *res = NULL;
174   smx_process_t process = SIMIX_process_self();
175   if ((process != NULL) && (process->simdata))
176     res = SIMIX_process_get_name(process);
177   if (res)
178     return res;
179   else
180     return "";
181 }
182
183 int gras_os_getpid(void)
184 {
185
186   smx_process_t process = SIMIX_process_self();
187
188   if ((process != NULL) && (process->data))
189     return ((gras_procdata_t *) process->data)->pid;
190   else
191     return 0;
192 }
193
194
195 /** @brief retrieve the value of a given host property (or NULL if not defined) */
196 const char *gras_os_host_property_value(const char *name)
197 {
198   return
199     xbt_dict_get_or_null(SIMIX_host_get_properties
200                          (SIMIX_process_get_host(SIMIX_process_self())),
201                          name);
202 }
203
204 /** @brief retrieve the host properties dictionnary
205  *  @warning it's the original one, not a copy. Don't mess with it
206  */
207 xbt_dict_t gras_os_host_properties(void)
208 {
209   return
210     SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
211 }
212
213 /* **************************************************************************
214  * Interface with SIMIX
215  * (these functions are called by the stuff generated by gras_stub_generator)
216  * **************************************************************************/
217
218 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
219 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
220
221 void gras_global_init(int *argc, char **argv)
222 {
223   XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
224   SIMIX_global_init(argc, argv);
225 }
226
227 void gras_create_environment(const char *file)
228 {
229   SIMIX_create_environment(file);
230 }
231
232 void gras_function_register(const char *name, xbt_main_func_t code)
233 {
234   SIMIX_function_register(name, code);
235 }
236
237 void gras_main()
238 {
239   smx_cond_t cond = NULL;
240   smx_action_t action;
241   xbt_fifo_t actions_done = xbt_fifo_new();
242   xbt_fifo_t actions_failed = xbt_fifo_new();
243
244   /* Clean IO before the run */
245   fflush(stdout);
246   fflush(stderr);
247   SIMIX_init();
248
249   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
250     while ((action = xbt_fifo_pop(actions_failed))) {
251       DEBUG1("** %s failed **", action->name);
252       while ((cond = xbt_fifo_pop(action->cond_list))) {
253         SIMIX_cond_broadcast(cond);
254       }
255       /* action finished, destroy it */
256       //        SIMIX_action_destroy(action);
257     }
258
259     while ((action = xbt_fifo_pop(actions_done))) {
260       DEBUG1("** %s done **", action->name);
261       while ((cond = xbt_fifo_pop(action->cond_list))) {
262         SIMIX_cond_broadcast(cond);
263       }
264       /* action finished, destroy it */
265       //SIMIX_action_destroy(action);
266     }
267   }
268   xbt_fifo_free(actions_failed);
269   xbt_fifo_free(actions_done);
270   return;
271 }
272
273 void gras_launch_application(const char *file)
274 {
275   SIMIX_launch_application(file);
276 }
277
278 void gras_clean()
279 {
280   SIMIX_clean();
281 }