Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correct some bugs
[simgrid.git] / src / gras_simix / Virtu / gras_simix_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_simix/Virtu/gras_simix_virtu_sg.h"
13 #include "gras_simix/Msg/gras_simix_msg_interface.h" /* For some checks at simulation end */
14 #include "gras_simix/Transport/gras_simix_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 void
21 gras_process_init() {
22         int i;
23   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
24   gras_procdata_t *pd=xbt_new0(gras_procdata_t,1);
25   gras_trp_procdata_t trp_pd;
26   //gras_sg_portrec_t prmeas,pr;
27   //int i;
28   
29   SIMIX_process_set_data(SIMIX_process_self(),(void*)pd);
30
31
32
33   gras_procdata_init();
34
35   if (!hd) {
36     /* First process on this host */
37     hd=xbt_new(gras_hostdata_t,1);
38     hd->refcount = 1;
39     hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL);
40
41   //  memset(hd->proc, 0, sizeof(hd->proc[0]) * XBT_MAX_CHANNEL); 
42         
43                 for (i=0;i<65536;i++) {
44                         hd->cond_port[i] =NULL;
45                         hd->mutex_port[i] =NULL;
46                 }
47                 SIMIX_host_set_data(SIMIX_host_self(),(void*)hd);
48   } else {
49     hd->refcount++;
50   }
51
52         trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
53         trp_pd->pid = PID++;
54         /*TODO*/
55         if (SIMIX_process_self() != NULL ) {
56         //      trp_pd->ppid = gras_os_getpid();
57                 trp_pd->ppid = -1;
58         }
59         else trp_pd->ppid = -1; 
60         trp_pd->mutex = SIMIX_mutex_init();
61         trp_pd->cond = SIMIX_cond_init();
62         trp_pd->active_socket = xbt_fifo_new();
63   /* take a free channel for this process */
64   /*
65         trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
66   for (i=0; i<XBT_MAX_CHANNEL && hd->proc[i]; i++);
67   if (i == XBT_MAX_CHANNEL) 
68     THROW2(system_error,0,
69            "Can't add a new process on %s, because all channels are already in use. Please increase MAX CHANNEL (which is %d for now) and recompile GRAS.",
70             MSG_host_get_name(MSG_host_self()),XBT_MAX_CHANNEL);
71
72   trp_pd->chan = i;
73   hd->proc[ i ] = MSG_process_self_PID();
74 */
75   /* regiter it to the ports structure */
76  // pr.port = -1;
77   //pr.tochan = i;
78   //pr.meas = 0;
79   //xbt_dynar_push(hd->ports,&pr);
80
81   /* take a free meas channel for this process */
82   /*
83         for (i=0; i<XBT_MAX_CHANNEL && hd->proc[i]; i++);
84   if (i == XBT_MAX_CHANNEL) {
85     THROW2(system_error,0,
86            "Can't add a new process on %s, because all channels are already in use. Please increase MAX CHANNEL (which is %d for now) and recompile GRAS.",
87             MSG_host_get_name(MSG_host_self()),XBT_MAX_CHANNEL);
88   }
89   trp_pd->measChan = i;
90
91   hd->proc[ i ] = MSG_process_self_PID();
92 */
93   /* register it to the ports structure */
94   //prmeas.port = -1;
95   //prmeas.tochan = i;
96   //prmeas.meas = 1;
97   //xbt_dynar_push(hd->ports,&prmeas);
98
99   VERB2("Creating process '%s' (%ld)",
100            SIMIX_process_get_name(SIMIX_process_self()),
101            gras_os_getpid());
102 }
103
104 void
105 gras_process_exit() {
106         int i;
107   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
108   gras_procdata_t *pd=(gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
109
110   gras_msg_procdata_t msg_pd=(gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
111   gras_trp_procdata_t trp_pd=(gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
112
113         SIMIX_mutex_destroy(trp_pd->mutex);
114         SIMIX_cond_destroy(trp_pd->cond);
115         xbt_fifo_free(trp_pd->active_socket);
116   //int myPID=gras_os_getpid();
117   //int cpt;
118   //gras_sg_portrec_t pr;
119
120   xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
121
122   VERB2("GRAS: Finalizing process '%s' (%ld)",
123         SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
124
125   if (xbt_dynar_length(msg_pd->msg_queue))
126     WARN1("process %ld terminated, but some messages are still queued",
127           gras_os_getpid());
128
129 /*
130   for (cpt=0; cpt< XBT_MAX_CHANNEL; cpt++)
131     if (myPID == hd->proc[cpt])
132       hd->proc[cpt] = 0;
133 */
134
135 /* remove ports from host, maybe i can do it on the socket destroy function */
136   /*
137         xbt_dynar_foreach(hd->ports, cpt, pr) {
138     if (pr.port == trp_pd->chan || pr.port == trp_pd->measChan) {
139       xbt_dynar_cursor_rm(hd->ports, &cpt);
140     }
141   }*/
142         
143   if ( ! --(hd->refcount)) {
144     xbt_dynar_free(&hd->ports);
145                 for (i=0; i<65536; i++) {
146                         if(hd->mutex_port[i] != NULL) {
147                                 SIMIX_mutex_destroy(hd->mutex_port[i]);
148                                 SIMIX_cond_destroy(hd->cond_port[i]);
149                                 hd->mutex_port[i] = NULL;
150                                 hd->cond_port[i] = NULL;
151                         }
152                 }
153     free(hd);
154   }
155   gras_procdata_exit();
156   free(pd);
157 }
158
159 /* **************************************************************************
160  * Process data
161  * **************************************************************************/
162
163 gras_procdata_t *gras_procdata_get(void) {
164   gras_procdata_t *pd=
165     (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
166
167   xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
168
169   return pd;
170 }
171 void *
172 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
173   gras_procdata_t *pd=
174     (gras_procdata_t *)SIMIX_process_get_data(p);
175
176   xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)", 
177               SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
178    
179   return gras_libdata_by_name_from_procdata(name, pd);
180 }   
181   
182 const char* xbt_procname(void) {
183   const char *res = NULL;
184   smx_process_t process = SIMIX_process_self();
185   if ((process != NULL) && (process->simdata))
186     res = SIMIX_process_get_name(process);
187   if (res) 
188     return res;
189   else
190     return "";
191 }
192
193 long int gras_os_getpid(void) {
194
195   smx_process_t process = SIMIX_process_self();
196         
197   if ((process != NULL) && (process->data))
198     return 1;
199   else
200     return 0;
201 }