Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move smx_process related functions from sg_emul.c to sg_process.c; cosmetics; + add...
[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                         /* no cleanup, thx, users will call gras_exit() */NULL);
29 }
30
31 /* **************************************************************************
32  * Process constructor/destructor (semi-public interface)
33  * **************************************************************************/
34
35 void
36 gras_process_init() {
37   gras_hostdata_t *hd=(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         }
62         else pd->ppid = -1; 
63
64         trp_pd->mutex = SIMIX_mutex_init();
65         trp_pd->cond = SIMIX_cond_init();
66         trp_pd->mutex_meas = SIMIX_mutex_init();
67         trp_pd->cond_meas = SIMIX_cond_init();
68         trp_pd->active_socket = xbt_fifo_new();
69         trp_pd->active_socket_meas = xbt_fifo_new();
70
71   VERB2("Creating process '%s' (%d)",
72            SIMIX_process_get_name(SIMIX_process_self()),
73            gras_os_getpid());
74 }
75
76 void
77 gras_process_exit() {
78         xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
79   gras_socket_t sock_iter;
80   int cursor;
81   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
82   gras_procdata_t *pd=(gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
83
84   gras_msg_procdata_t msg_pd=(gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
85   gras_trp_procdata_t trp_pd=(gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
86
87         SIMIX_mutex_destroy(trp_pd->mutex);
88         SIMIX_cond_destroy(trp_pd->cond);
89         xbt_fifo_free(trp_pd->active_socket);
90         SIMIX_mutex_destroy(trp_pd->mutex_meas);
91         SIMIX_cond_destroy(trp_pd->cond_meas);
92         xbt_fifo_free(trp_pd->active_socket_meas);
93
94
95   xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
96
97   VERB2("GRAS: Finalizing process '%s' (%d)",
98         SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
99
100   if (xbt_dynar_length(msg_pd->msg_queue))
101     WARN1("process %d terminated, but some messages are still queued",
102           gras_os_getpid());
103
104         /* if each process has its sockets list, we need to close them when the process finish */
105         xbt_dynar_foreach(sockets,cursor,sock_iter) {
106                 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
107                                 sock_iter);
108                 gras_socket_close(sock_iter);
109         }
110   if ( ! --(hd->refcount)) {
111     xbt_dynar_free(&hd->ports);
112     free(hd);
113   }
114   gras_procdata_exit();
115   free(pd);
116 }
117
118 /* **************************************************************************
119  * Process data (public interface)
120  * **************************************************************************/
121
122 gras_procdata_t *gras_procdata_get(void) {
123   gras_procdata_t *pd=
124     (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
125
126   xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
127
128   return pd;
129 }
130 void *
131 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
132   gras_procdata_t *pd=
133     (gras_procdata_t *)SIMIX_process_get_data(p);
134
135   xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)", 
136               SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
137    
138   return gras_libdata_by_name_from_procdata(name, pd);
139 }   
140
141 /* **************************************************************************
142  * OS virtualization function
143  * **************************************************************************/
144
145 const char* xbt_procname(void) {
146   const char *res = NULL;
147   smx_process_t process = SIMIX_process_self();
148   if ((process != NULL) && (process->simdata))
149     res = SIMIX_process_get_name(process);
150   if (res) 
151     return res;
152   else
153     return "";
154 }
155
156 int gras_os_getpid(void) {
157
158   smx_process_t process = SIMIX_process_self();
159         
160   if ((process != NULL) && (process->data))
161      return ((gras_procdata_t*)process->data)->pid;
162   else
163     return 0;
164 }
165
166 /* **************************************************************************
167  * Interface with SIMIX
168  * **************************************************************************/
169
170 void gras_global_init(int *argc,char **argv) {
171    return SIMIX_global_init(argc,argv);
172 }
173 void gras_create_environment(const char *file) {
174    return SIMIX_create_environment(file);
175 }
176 void gras_function_register(const char *name, xbt_main_func_t code) {
177    return SIMIX_function_register(name, code);
178 }
179 void gras_main() {
180    smx_cond_t cond = NULL;
181    smx_action_t smx_action;
182    xbt_fifo_t actions_done = xbt_fifo_new();
183    xbt_fifo_t actions_failed = xbt_fifo_new();
184    
185
186    /* Clean IO before the run */
187    fflush(stdout);
188    fflush(stderr);
189    
190    
191    while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
192
193       while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
194          
195          
196          DEBUG1("** %s failed **",smx_action->name);
197          while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
198             SIMIX_cond_broadcast(cond);
199                         }
200          /* action finished, destroy it */
201          //     SIMIX_action_destroy(smx_action);
202       }
203       
204       while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
205          
206          DEBUG1("** %s done **",smx_action->name);
207          while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
208             SIMIX_cond_broadcast(cond);
209          }
210          /* action finished, destroy it */
211          //SIMIX_action_destroy(smx_action);
212       }
213    }
214    xbt_fifo_free(actions_failed);
215    xbt_fifo_free(actions_done);
216    return;   
217 }
218 void gras_launch_application(const char *file) {
219    return SIMIX_launch_application(file);
220 }
221 void gras_clean() {
222    return SIMIX_clean();
223 }
224
225