Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
569fd3ab1a634f832d1c719b0a492e15da81b91a
[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     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->sockets_to_close = xbt_dynar_new(sizeof(gras_socket_t),NULL);
65
66   VERB2("Creating process '%s' (%d)",
67         SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
68 }
69
70 void gras_process_exit()
71 {
72   xbt_dynar_t sockets =
73     ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
74   gras_socket_t sock_iter;
75   unsigned int cursor;
76   gras_hostdata_t *hd =
77     (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
78   gras_procdata_t *pd =
79     (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
80
81   gras_msg_procdata_t msg_pd =
82     (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
83   gras_trp_procdata_t trp_pd =
84     (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
85
86   xbt_dynar_free(&trp_pd->sockets_to_close);
87
88
89   xbt_assert0(hd, "Run gras_process_init (ie, gras_init)!!");
90
91   VERB2("GRAS: Finalizing process '%s' (%d)",
92         SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
93
94   if (xbt_dynar_length(msg_pd->msg_queue)) {
95     unsigned int cpt;
96     s_gras_msg_t msg;
97     WARN2("process %d terminated, but %ld messages are still queued. Message list:",
98           gras_os_getpid(),xbt_dynar_length(msg_pd->msg_queue));
99     xbt_dynar_foreach(msg_pd->msg_queue,cpt, msg) {
100       WARN5("   Message %s (%s) from %s@%s:%d",msg.type->name,e_gras_msg_kind_names[msg.kind],
101           gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
102     }
103   }
104
105   /* if each process has its sockets list, we need to close them when the
106      process finish */
107   xbt_dynar_foreach(sockets, cursor, sock_iter) {
108     VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
109           sock_iter);
110     gras_socket_close(sock_iter);
111   }
112   if (!--(hd->refcount)) {
113     free(hd);
114   }
115   gras_procdata_exit();
116   free(pd);
117 }
118
119 /* **************************************************************************
120  * Process data (public interface)
121  * **************************************************************************/
122
123 gras_procdata_t *gras_procdata_get(void)
124 {
125   gras_procdata_t *pd =
126     (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
127
128   xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
129
130   return pd;
131 }
132
133 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
134 {
135   gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
136
137   xbt_assert2(pd,
138               "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
139               SIMIX_process_get_name(p),
140               SIMIX_host_get_name(SIMIX_process_get_host(p)));
141
142   return gras_libdata_by_name_from_procdata(name, pd);
143 }
144
145 /** @brief retrieve the value of a given process property (or NULL if not defined) */
146 const char *gras_process_property_value(const char *name)
147 {
148   return
149     xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()),
150                          name);
151 }
152
153 /** @brief retrieve the process properties dictionnary
154  *  @warning it's the original one, not a copy. Don't mess with it
155  */
156 xbt_dict_t gras_process_properties(void)
157 {
158   return SIMIX_process_get_properties(SIMIX_process_self());
159 }
160
161 /* **************************************************************************
162  * OS virtualization function
163  * **************************************************************************/
164
165 const char *xbt_procname(void)
166 {
167   smx_process_t process = SIMIX_process_self();
168   /*FIXME: maestro used not have a simix process, now it does so 
169     SIMIX_process_self will return something different to NULL. This breaks
170     the old xbt_log logic that assumed that NULL was equivalent to maestro,
171     thus when printing it searches for maestro host name (which doesn't exists)
172     and breaks the logging.
173     As a hack we check for maestro by looking to the assigned host, if it is
174     NULL then we are sure is maestro
175   */
176   if (process != NULL && SIMIX_host_self())
177     return SIMIX_process_get_name(process);
178
179   return "";
180 }
181
182 int gras_os_getpid(void)
183 {
184   gras_procdata_t *data;
185   smx_process_t process = SIMIX_process_self();
186   
187   if (process != NULL){
188     data = (gras_procdata_t *)SIMIX_process_get_data(process);
189     if(data != NULL)
190       return data->pid;
191   }
192   
193   return 0;
194 }
195
196 /** @brief retrieve the value of a given host property (or NULL if not defined) */
197 const char *gras_os_host_property_value(const char *name)
198 {
199   return
200     xbt_dict_get_or_null(SIMIX_host_get_properties
201                          (SIMIX_process_get_host(SIMIX_process_self())),
202                          name);
203 }
204
205 /** @brief retrieve the host properties dictionnary
206  *  @warning it's the original one, not a copy. Don't mess with it
207  */
208 xbt_dict_t gras_os_host_properties(void)
209 {
210   return
211     SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
212 }
213
214 /* **************************************************************************
215  * Interface with SIMIX
216  * (these functions are called by the stuff generated by gras_stub_generator)
217  * **************************************************************************/
218
219 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
220 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
221
222 void gras_global_init(int *argc, char **argv)
223 {
224   XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
225   SIMIX_global_init(argc, argv);
226 }
227
228 void gras_create_environment(const char *file)
229 {
230   SIMIX_create_environment(file);
231 }
232
233 void gras_function_register(const char *name, xbt_main_func_t code)
234 {
235   SIMIX_function_register(name, code);
236 }
237
238 void gras_main()
239 {
240   /* Clean IO before the run */
241   fflush(stdout);
242   fflush(stderr);
243   SIMIX_init();
244
245   while (SIMIX_solve(NULL, NULL) != -1.0);
246   
247   return;
248 }
249
250 void gras_launch_application(const char *file)
251 {
252   SIMIX_launch_application(file);
253 }
254
255 void gras_clean()
256 {
257   SIMIX_clean();
258 }