Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
void functions don't return
[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 "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    
24    SIMIX_process_create(name, code,
25                         data,
26                         gras_os_myname(), 
27                         argc, argv);
28 }
29
30 /* **************************************************************************
31  * Process constructor/destructor (semi-public interface)
32  * **************************************************************************/
33
34 void
35 gras_process_init() {
36   gras_hostdata_t *hd=(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   }
61   else 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()),
69         gras_os_getpid());
70 }
71
72 void
73 gras_process_exit() {
74         xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
75   gras_socket_t sock_iter;
76   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     WARN1("process %d terminated, but some messages are still queued",
99           gras_os_getpid());
100   
101   /* if each process has its sockets list, we need to close them when the
102            process finish */
103   xbt_dynar_foreach(sockets,cursor,sock_iter) {
104     VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
105           sock_iter);
106     gras_socket_close(sock_iter);
107   }
108   if ( ! --(hd->refcount)) {
109     xbt_dynar_free(&hd->ports);
110     free(hd);
111   }
112   gras_procdata_exit();
113   free(pd);
114 }
115
116 /* **************************************************************************
117  * Process data (public interface)
118  * **************************************************************************/
119
120 gras_procdata_t *gras_procdata_get(void) {
121   gras_procdata_t *pd=
122     (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
123
124   xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
125
126   return pd;
127 }
128 void *
129 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
130   gras_procdata_t *pd=
131     (gras_procdata_t *)SIMIX_process_get_data(p);
132
133   xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)", 
134               SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
135    
136   return gras_libdata_by_name_from_procdata(name, pd);
137 }   
138
139 /* **************************************************************************
140  * OS virtualization function
141  * **************************************************************************/
142
143 const char* xbt_procname(void) {
144   const char *res = NULL;
145   smx_process_t process = SIMIX_process_self();
146   if ((process != NULL) && (process->simdata))
147     res = SIMIX_process_get_name(process);
148   if (res) 
149     return res;
150   else
151     return "";
152 }
153
154 int gras_os_getpid(void) {
155
156   smx_process_t process = SIMIX_process_self();
157         
158   if ((process != NULL) && (process->data))
159      return ((gras_procdata_t*)process->data)->pid;
160   else
161     return 0;
162 }
163
164 /* **************************************************************************
165  * Interface with SIMIX
166  * **************************************************************************/
167
168 void gras_global_init(int *argc,char **argv) {
169    SIMIX_global_init(argc,argv);
170 }
171 void gras_create_environment(const char *file) {
172    SIMIX_create_environment(file);
173 }
174 void gras_function_register(const char *name, xbt_main_func_t code) {
175    SIMIX_function_register(name, code);
176 }
177
178 void gras_main() {
179   smx_cond_t cond = NULL;
180   smx_action_t action;
181   xbt_fifo_t actions_done = xbt_fifo_new();
182   xbt_fifo_t actions_failed = xbt_fifo_new();
183    
184   /* Clean IO before the run */
185   fflush(stdout);
186   fflush(stderr);
187
188   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
189     while ( (action = xbt_fifo_pop(actions_failed)) ) {
190       DEBUG1("** %s failed **",action->name);
191       while ( (cond = xbt_fifo_pop(action->cond_list)) ) {
192         SIMIX_cond_broadcast(cond);
193       }
194       /* action finished, destroy it */
195       //        SIMIX_action_destroy(action);
196     }
197     
198     while ( (action = xbt_fifo_pop(actions_done)) ) {
199       DEBUG1("** %s done **",action->name);
200       while ( (cond = xbt_fifo_pop(action->cond_list)) ) {
201         SIMIX_cond_broadcast(cond);
202       }
203       /* action finished, destroy it */
204       //SIMIX_action_destroy(action);
205     }
206   }
207   xbt_fifo_free(actions_failed);
208   xbt_fifo_free(actions_done);
209   return;   
210 }
211
212 void gras_launch_application(const char *file) {
213    SIMIX_launch_application(file);
214 }
215
216 void gras_clean() {
217    SIMIX_clean();
218 }
219
220