Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / src / gras / Virtu / sg_process.c
1 /* process_sg - GRAS process handling on simulator                          */
2
3 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/ex.h"
9 #include "xbt/dict.h"
10 #include "gras_modinter.h"      /* module initialization interface */
11 #include "gras/Virtu/virtu_sg.h"
12 #include "gras/Msg/msg_interface.h"     /* For some checks at simulation end */
13 #include "gras/Transport/transport_interface.h" /* For some checks at simulation end */
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
16
17 static long int PID = 1;
18
19
20 void gras_agent_spawn(const char *name, void *data,
21                       xbt_main_func_t code, int argc, char *argv[],
22                       xbt_dict_t properties)
23 {
24
25   SIMIX_process_create(name, code,
26                        data, gras_os_myname(), argc, argv, properties);
27 }
28
29 /* **************************************************************************
30  * Process constructor/destructor (semi-public interface)
31  * **************************************************************************/
32
33 void gras_process_init()
34 {
35   gras_hostdata_t *hd =
36     (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   } else
61     pd->ppid = -1;
62
63   trp_pd->msg_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
64
65   trp_pd->meas_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
66
67   VERB2("Creating process '%s' (%d)",
68         SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
69 }
70
71 void gras_process_exit()
72 {
73   xbt_dynar_t sockets =
74     ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
75   gras_socket_t sock_iter;
76   unsigned int cursor;
77   gras_hostdata_t *hd =
78     (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
79   gras_procdata_t *pd =
80     (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
81
82   gras_msg_procdata_t msg_pd =
83     (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
84   gras_trp_procdata_t trp_pd =
85     (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
86
87   xbt_queue_free(&trp_pd->msg_selectable_sockets);
88
89   xbt_queue_free(&trp_pd->meas_selectable_sockets);
90
91
92   xbt_assert0(hd, "Run gras_process_init (ie, gras_init)!!");
93
94   VERB2("GRAS: Finalizing process '%s' (%d)",
95         SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
96
97   if (xbt_dynar_length(msg_pd->msg_queue)) {
98     unsigned int cpt;
99     s_gras_msg_t msg;
100     WARN2("process %d terminated, but %ld messages are still queued. Message list:",
101           gras_os_getpid(),xbt_dynar_length(msg_pd->msg_queue));
102     xbt_dynar_foreach(msg_pd->msg_queue,cpt, msg) {
103       WARN5("   Message %s (%s) from %s@%s:%d",msg.type->name,e_gras_msg_kind_names[msg.kind],
104           gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
105     }
106   }
107
108   /* if each process has its sockets list, we need to close them when the
109      process finish */
110   xbt_dynar_foreach(sockets, cursor, sock_iter) {
111     VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
112           sock_iter);
113     gras_socket_close(sock_iter);
114   }
115   if (!--(hd->refcount)) {
116     xbt_dynar_free(&hd->ports);
117     free(hd);
118   }
119   gras_procdata_exit();
120   free(pd);
121 }
122
123 /* **************************************************************************
124  * Process data (public interface)
125  * **************************************************************************/
126
127 gras_procdata_t *gras_procdata_get(void)
128 {
129   gras_procdata_t *pd =
130     (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
131
132   xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
133
134   return pd;
135 }
136
137 void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
138 {
139   gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
140
141   xbt_assert2(pd,
142               "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
143               SIMIX_process_get_name(p),
144               SIMIX_host_get_name(SIMIX_process_get_host(p)));
145
146   return gras_libdata_by_name_from_procdata(name, pd);
147 }
148
149 /** @brief retrieve the value of a given process property (or NULL if not defined) */
150 const char *gras_process_property_value(const char *name)
151 {
152   return
153     xbt_dict_get_or_null(SIMIX_process_get_properties(SIMIX_process_self()),
154                          name);
155 }
156
157 /** @brief retrieve the process properties dictionnary
158  *  @warning it's the original one, not a copy. Don't mess with it
159  */
160 xbt_dict_t gras_process_properties(void)
161 {
162   return SIMIX_process_get_properties(SIMIX_process_self());
163 }
164
165 /* **************************************************************************
166  * OS virtualization function
167  * **************************************************************************/
168
169 const char *xbt_procname(void)
170 {
171   smx_process_t process = SIMIX_process_self();
172   /*FIXME: maestro used not have a simix process, now it does so 
173     SIMIX_process_self will return something different to NULL. This breaks
174     the old xbt_log logic that assumed that NULL was equivalent to maestro,
175     thus when printing it searches for maestro host name (which doesn't exists)
176     and breaks the logging.
177     As a hack we check for maestro by looking to the assigned host, if it is
178     NULL then we are sure is maestro
179   */
180   if (process != NULL && SIMIX_process_get_host(process))
181     return SIMIX_process_get_name(process);
182
183   return "";
184 }
185
186 int gras_os_getpid(void)
187 {
188   gras_procdata_t *data;
189   smx_process_t process = SIMIX_process_self();
190   
191   if (process != NULL){
192     data = (gras_procdata_t *)SIMIX_process_get_data(process);
193     if(data != NULL)
194       return data->pid;
195   }
196   
197   return 0;
198 }
199
200 /** @brief retrieve the value of a given host property (or NULL if not defined) */
201 const char *gras_os_host_property_value(const char *name)
202 {
203   return
204     xbt_dict_get_or_null(SIMIX_host_get_properties
205                          (SIMIX_process_get_host(SIMIX_process_self())),
206                          name);
207 }
208
209 /** @brief retrieve the host properties dictionnary
210  *  @warning it's the original one, not a copy. Don't mess with it
211  */
212 xbt_dict_t gras_os_host_properties(void)
213 {
214   return
215     SIMIX_host_get_properties(SIMIX_process_get_host(SIMIX_process_self()));
216 }
217
218 /* **************************************************************************
219  * Interface with SIMIX
220  * (these functions are called by the stuff generated by gras_stub_generator)
221  * **************************************************************************/
222
223 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
224 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
225
226 void gras_global_init(int *argc, char **argv)
227 {
228   XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
229   SIMIX_global_init(argc, argv);
230 }
231
232 void gras_create_environment(const char *file)
233 {
234   SIMIX_create_environment(file);
235 }
236
237 void gras_function_register(const char *name, xbt_main_func_t code)
238 {
239   SIMIX_function_register(name, code);
240 }
241
242 void gras_main()
243 {
244   /* Clean IO before the run */
245   fflush(stdout);
246   fflush(stderr);
247   SIMIX_init();
248
249   while (SIMIX_solve(NULL, NULL) != -1.0);
250   
251   return;
252 }
253
254 void gras_launch_application(const char *file)
255 {
256   SIMIX_launch_application(file);
257 }
258
259 void gras_clean()
260 {
261   SIMIX_clean();
262 }