Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initial structure to implement gras using simix.
[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   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
23   gras_procdata_t *pd=xbt_new0(gras_procdata_t,1);
24   gras_trp_procdata_t trp_pd;
25   //gras_sg_portrec_t prmeas,pr;
26   //int i;
27   
28   SIMIX_process_set_data(SIMIX_process_self(),(void*)pd);
29
30         trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
31         trp_pd->pid = PID++;
32         if (SIMIX_process_self() != NULL ) {
33                 trp_pd->ppid = gras_os_getpid();
34         }
35         else trp_pd->ppid = -1; 
36
37   gras_procdata_init();
38
39   if (!hd) {
40     /* First process on this host */
41     hd=xbt_new(gras_hostdata_t,1);
42     hd->refcount = 1;
43     hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL);
44
45   //  memset(hd->proc, 0, sizeof(hd->proc[0]) * XBT_MAX_CHANNEL); 
46
47         SIMIX_host_set_data(SIMIX_host_self(),(void*)hd);
48   } else {
49     hd->refcount++;
50   }
51   
52   /* take a free channel for this process */
53   /*
54         trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
55   for (i=0; i<XBT_MAX_CHANNEL && hd->proc[i]; i++);
56   if (i == XBT_MAX_CHANNEL) 
57     THROW2(system_error,0,
58            "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.",
59             MSG_host_get_name(MSG_host_self()),XBT_MAX_CHANNEL);
60
61   trp_pd->chan = i;
62   hd->proc[ i ] = MSG_process_self_PID();
63 */
64   /* regiter it to the ports structure */
65  // pr.port = -1;
66   //pr.tochan = i;
67   //pr.meas = 0;
68   //xbt_dynar_push(hd->ports,&pr);
69
70   /* take a free meas channel for this process */
71   /*
72         for (i=0; i<XBT_MAX_CHANNEL && hd->proc[i]; i++);
73   if (i == XBT_MAX_CHANNEL) {
74     THROW2(system_error,0,
75            "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.",
76             MSG_host_get_name(MSG_host_self()),XBT_MAX_CHANNEL);
77   }
78   trp_pd->measChan = i;
79
80   hd->proc[ i ] = MSG_process_self_PID();
81 */
82   /* register it to the ports structure */
83   //prmeas.port = -1;
84   //prmeas.tochan = i;
85   //prmeas.meas = 1;
86   //xbt_dynar_push(hd->ports,&prmeas);
87
88   VERB2("Creating process '%s' (%ld)",
89            SIMIX_process_get_name(SIMIX_process_self()),
90            gras_os_getpid());
91 }
92
93 void
94 gras_process_exit() {
95   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
96   gras_procdata_t *pd=(gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
97
98   gras_msg_procdata_t msg_pd=(gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
99   //gras_trp_procdata_t trp_pd=(gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
100   //int myPID=gras_os_getpid();
101   //int cpt;
102   //gras_sg_portrec_t pr;
103
104   xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
105
106   VERB2("GRAS: Finalizing process '%s' (%ld)",
107         SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
108
109   if (xbt_dynar_length(msg_pd->msg_queue))
110     WARN1("process %ld terminated, but some messages are still queued",
111           gras_os_getpid());
112
113 /*
114   for (cpt=0; cpt< XBT_MAX_CHANNEL; cpt++)
115     if (myPID == hd->proc[cpt])
116       hd->proc[cpt] = 0;
117 */
118
119 /* remove ports from host, maybe i can do it on the socket destroy function */
120   /*
121         xbt_dynar_foreach(hd->ports, cpt, pr) {
122     if (pr.port == trp_pd->chan || pr.port == trp_pd->measChan) {
123       xbt_dynar_cursor_rm(hd->ports, &cpt);
124     }
125   }*/
126
127   if ( ! --(hd->refcount)) {
128     xbt_dynar_free(&hd->ports);
129     free(hd);
130   }
131   gras_procdata_exit();
132   free(pd);
133 }
134
135 /* **************************************************************************
136  * Process data
137  * **************************************************************************/
138
139 gras_procdata_t *gras_procdata_get(void) {
140   gras_procdata_t *pd=
141     (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
142
143   xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
144
145   return pd;
146 }
147 void *
148 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
149   gras_procdata_t *pd=
150     (gras_procdata_t *)SIMIX_process_get_data(p);
151
152   xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)", 
153               SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
154    
155   return gras_libdata_by_name_from_procdata(name, pd);
156 }   
157   
158 const char* xbt_procname(void) {
159   const char *res = NULL;
160   smx_process_t process = SIMIX_process_self();
161   if ((process != NULL) && (process->simdata))
162     res = SIMIX_process_get_name(process);
163   if (res) 
164     return res;
165   else
166     return "";
167 }
168
169 long int gras_os_getpid(void) {
170
171   smx_process_t process = SIMIX_process_self();
172   if ((process != NULL) && (process->data))
173     return ((gras_procdata_t*)process->data)->pid;
174   else
175     return 0;
176 }