Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Functions gras_trp_sg_chunk_recv and send added.
[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
27   SIMIX_process_set_data(SIMIX_process_self(),(void*)pd);
28
29
30   gras_procdata_init();
31
32   if (!hd) {
33     /* First process on this host */
34     hd=xbt_new(gras_hostdata_t,1);
35     hd->refcount = 1;
36     hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL);
37
38                 for (i=0;i<65536;i++) {
39                         hd->cond_port[i] =NULL;
40                         hd->mutex_port[i] =NULL;
41                 }
42                 SIMIX_host_set_data(SIMIX_host_self(),(void*)hd);
43   } else {
44     hd->refcount++;
45   }
46
47         trp_pd = (gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
48         trp_pd->pid = PID++;
49         /*TODO*/
50         if (SIMIX_process_self() != NULL ) {
51         //      trp_pd->ppid = gras_os_getpid();
52                 trp_pd->ppid = -1;
53         }
54         else trp_pd->ppid = -1; 
55         trp_pd->mutex = SIMIX_mutex_init();
56         trp_pd->cond = SIMIX_cond_init();
57         trp_pd->active_socket = xbt_fifo_new();
58
59   VERB2("Creating process '%s' (%ld)",
60            SIMIX_process_get_name(SIMIX_process_self()),
61            gras_os_getpid());
62 }
63
64 void
65 gras_process_exit() {
66         int i;
67         xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
68   gras_socket_t sock_iter;
69   int cursor;
70   gras_hostdata_t *hd=(gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
71   gras_procdata_t *pd=(gras_procdata_t*)SIMIX_process_get_data(SIMIX_process_self());
72
73   gras_msg_procdata_t msg_pd=(gras_msg_procdata_t)gras_libdata_by_name("gras_msg");
74   gras_trp_procdata_t trp_pd=(gras_trp_procdata_t)gras_libdata_by_name("gras_trp");
75
76         SIMIX_mutex_destroy(trp_pd->mutex);
77         SIMIX_cond_destroy(trp_pd->cond);
78         xbt_fifo_free(trp_pd->active_socket);
79   //int myPID=gras_os_getpid();
80   //int cpt;
81   //gras_sg_portrec_t pr;
82
83   xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
84
85   VERB2("GRAS: Finalizing process '%s' (%ld)",
86         SIMIX_process_get_name(SIMIX_process_self()),gras_os_getpid());
87
88   if (xbt_dynar_length(msg_pd->msg_queue))
89     WARN1("process %ld terminated, but some messages are still queued",
90           gras_os_getpid());
91
92         /* if each process has its sockets list, we need to close them when the process finish */
93         xbt_dynar_foreach(sockets,cursor,sock_iter) {
94                 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
95                                 sock_iter);
96                 gras_socket_close(sock_iter);
97         }
98   if ( ! --(hd->refcount)) {
99     xbt_dynar_free(&hd->ports);
100                 for (i=0; i<65536; i++) {
101                         if(hd->mutex_port[i] != NULL) {
102                                 SIMIX_mutex_destroy(hd->mutex_port[i]);
103                                 SIMIX_cond_destroy(hd->cond_port[i]);
104                                 hd->mutex_port[i] = NULL;
105                                 hd->cond_port[i] = NULL;
106                         }
107                 }
108     free(hd);
109   }
110   gras_procdata_exit();
111   free(pd);
112 }
113
114 /* **************************************************************************
115  * Process data
116  * **************************************************************************/
117
118 gras_procdata_t *gras_procdata_get(void) {
119   gras_procdata_t *pd=
120     (gras_procdata_t *)SIMIX_process_get_data(SIMIX_process_self());
121
122   xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
123
124   return pd;
125 }
126 void *
127 gras_libdata_by_name_from_remote(const char *name, smx_process_t p) {
128   gras_procdata_t *pd=
129     (gras_procdata_t *)SIMIX_process_get_data(p);
130
131   xbt_assert2(pd,"process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)", 
132               SIMIX_process_get_name(p),SIMIX_host_get_name(SIMIX_process_get_host(p)));
133    
134   return gras_libdata_by_name_from_procdata(name, pd);
135 }   
136   
137 const char* xbt_procname(void) {
138   const char *res = NULL;
139   smx_process_t process = SIMIX_process_self();
140   if ((process != NULL) && (process->simdata))
141     res = SIMIX_process_get_name(process);
142   if (res) 
143     return res;
144   else
145     return "";
146 }
147
148 long int gras_os_getpid(void) {
149
150   smx_process_t process = SIMIX_process_self();
151         
152   if ((process != NULL) && (process->data))
153     return 1;
154   else
155     return 0;
156 }