Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d6497b0418902e86e52adc59fe538d2ee4491122
[simgrid.git] / src / gras / Virtu / sg_process.c
1 /* process_sg - GRAS process handling on simulator                          */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/ex.h"
10 #include "xbt/dict.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                       xbt_dict_t properties)
24 {
25
26   SIMIX_process_create(name, code,
27                        data, gras_os_myname(), argc, argv, properties);
28 }
29
30 /* **************************************************************************
31  * Process constructor/destructor (semi-public interface)
32  * **************************************************************************/
33
34 void gras_process_init()
35 {
36   gras_hostdata_t *hd =
37     (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   } else
62     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()), gras_os_getpid());
70 }
71
72 void gras_process_exit()
73 {
74   xbt_dynar_t sockets =
75     ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
76   gras_socket_t sock_iter;
77   unsigned 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     unsigned int cpt;
100     s_gras_msg_t msg;
101     WARN2("process %d terminated, but %ld messages are still queued. Message list:",
102           gras_os_getpid(),xbt_dynar_length(msg_pd->msg_queue));
103     xbt_dynar_foreach(msg_pd->msg_queue,cpt, msg) {
104       WARN5("   Message %s (%s) from %s@%s:%d",msg.type->name,e_gras_msg_kind_names[msg.kind],
105           gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
106     }
107   }
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 {
130   gras_procdata_t *pd =
131     (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
132
133   xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
134
135   return pd;
136 }
137
138 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
139 {
140   gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
141
142   xbt_assert2(pd,
143               "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
144               SIMIX_process_get_name(p),
145               SIMIX_host_get_name(SIMIX_process_get_host(p)));
146
147   return gras_libdata_by_name_from_procdata(name, pd);
148 }
149
150 /** @brief retrieve the value of a given process property (or NULL if not defined) */
151 const char *gras_process_property_value(const char *name)
152 {
153   return
154     xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()),
155                          name);
156 }
157
158 /** @brief retrieve the process properties dictionnary
159  *  @warning it's the original one, not a copy. Don't mess with it
160  */
161 xbt_dict_t gras_process_properties(void)
162 {
163   return SIMIX_process_get_properties(SIMIX_process_self());
164 }
165
166 /* **************************************************************************
167  * OS virtualization function
168  * **************************************************************************/
169
170 const char *xbt_procname(void)
171 {
172   smx_process_t process = SIMIX_process_self();
173   /*FIXME: maestro used not have a simix process, now it does so 
174     SIMIX_process_self will return something different to NULL. This breaks
175     the old xbt_log logic that assumed that NULL was equivalent to maestro,
176     thus when printing it searches for maestro host name (which doesn't exists)
177     and breaks the logging.
178     As a hack we check for maestro by looking to the assigned host, if it is
179     NULL then we are sure is maestro
180   */
181   if (process != NULL && SIMIX_process_get_host(process))
182     return SIMIX_process_get_name(process);
183
184   return "";
185 }
186
187 int gras_os_getpid(void)
188 {
189   gras_procdata_t *data;
190   smx_process_t process = SIMIX_process_self();
191   
192   if (process != NULL){
193     data = (gras_procdata_t *)SIMIX_process_get_data(process);
194     if(data != NULL)
195       return data->pid;
196   }
197   
198   return 0;
199 }
200
201 /** @brief retrieve the value of a given host property (or NULL if not defined) */
202 const char *gras_os_host_property_value(const char *name)
203 {
204   return
205     xbt_dict_get_or_null(SIMIX_host_get_properties
206                          (SIMIX_process_get_host(SIMIX_process_self())),
207                          name);
208 }
209
210 /** @brief retrieve the host properties dictionnary
211  *  @warning it's the original one, not a copy. Don't mess with it
212  */
213 xbt_dict_t gras_os_host_properties(void)
214 {
215   return
216     SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
217 }
218
219 /* **************************************************************************
220  * Interface with SIMIX
221  * (these functions are called by the stuff generated by gras_stub_generator)
222  * **************************************************************************/
223
224 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
225 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
226
227 void gras_global_init(int *argc, char **argv)
228 {
229   XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
230   SIMIX_global_init(argc, argv);
231 }
232
233 void gras_create_environment(const char *file)
234 {
235   SIMIX_create_environment(file);
236 }
237
238 void gras_function_register(const char *name, xbt_main_func_t code)
239 {
240   SIMIX_function_register(name, code);
241 }
242
243 void gras_main()
244 {
245   /* Clean IO before the run */
246   fflush(stdout);
247   fflush(stderr);
248   SIMIX_init();
249
250   while (SIMIX_solve(NULL, NULL) != -1.0);
251   
252   return;
253 }
254
255 void gras_launch_application(const char *file)
256 {
257   SIMIX_launch_application(file);
258 }
259
260 void gras_clean()
261 {
262   SIMIX_clean();
263 }