Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert the change inducing that GRAS processes keep quiet when leaving on simulator...
[simgrid.git] / src / gras / gras.c
1 /* $Id$ */
2
3 /* gras.c -- generic functions not fitting anywhere else                    */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson.                                 */
6 /* All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "xbt/log.h"
12 #include "xbt/virtu.h"          /* set the XBT virtualization to use GRAS */
13 #include "xbt/module.h"         /* xbt_init/exit */
14 #include "xbt/xbt_os_time.h"    /* xbt_os_time */
15 #include "xbt/synchro.h"
16
17 #include "Virtu/virtu_interface.h"      /* Module mechanism FIXME: deplace&rename */
18 #include "Virtu/virtu_private.h"
19 #include "gras_modinter.h"      /* module init/exit */
20 #include "amok/amok_modinter.h" /* module init/exit */
21 #include "xbt_modinter.h"       /* module init/exit */
22
23 #include "gras.h"
24 #include "gras/process.h"       /* FIXME: killme and put process_init in modinter */
25 #include "gras/Msg/msg_private.h"
26 #include "portable.h"           /* hexa_*(); signalling stuff */
27
28 XBT_LOG_NEW_DEFAULT_CATEGORY(gras,
29                              "All GRAS categories (cf. section \ref GRAS_API)");
30 static int gras_running_process = 0;
31 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
32 static void gras_sigusr_handler(int sig)
33 {
34   INFO0("SIGUSR1 received. Display the backtrace");
35   xbt_backtrace_display_current();
36 }
37
38 static void gras_sigint_handler(int sig)
39 {
40   static double lastone = 0;
41   if (lastone == 0 || xbt_os_time() - lastone > 5) {
42     if (gras_if_RL())
43       xbt_backtrace_display_current();
44     else
45       SIMIX_display_process_status();
46     fprintf(stderr,
47             "\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
48     lastone = xbt_os_time();
49   } else {
50     exit(1);
51   }
52 }
53 #endif
54
55 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt);
56 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_cbps);
57 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_convert);
58 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_create);
59 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_exchange);
60 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_lexer);
61 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_parse);
62 XBT_LOG_EXTERNAL_CATEGORY(gras_modules);
63 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
64 XBT_LOG_EXTERNAL_CATEGORY(gras_msg_read);
65 XBT_LOG_EXTERNAL_CATEGORY(gras_msg_rpc);
66 XBT_LOG_EXTERNAL_CATEGORY(gras_timer);
67 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
68 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_meas);
69 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu);
70 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu_emul);
71 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu_process);
72
73 void gras_init(int *argc, char **argv)
74 {
75
76   gras_procdata_t *pd;
77   gras_msg_procdata_t msg_pd;
78   VERB0("Initialize GRAS");
79
80   xbt_getpid = gras_os_getpid;
81   /* First initialize the XBT */
82   xbt_init(argc, argv);
83
84   /* module registrations:
85    *    - declare process specific data we need (without creating them)
86    */
87   if (gras_running_process == 0) {
88     /* Connect our log channels: that must be done manually under windows */
89     XBT_LOG_CONNECT(gras_ddt, gras);
90     XBT_LOG_CONNECT(gras_ddt_cbps, gras_ddt);
91     XBT_LOG_CONNECT(gras_ddt_convert, gras_ddt);
92     XBT_LOG_CONNECT(gras_ddt_create, gras_ddt);
93     XBT_LOG_CONNECT(gras_ddt_exchange, gras_ddt);
94     XBT_LOG_CONNECT(gras_ddt_lexer, gras_ddt_parse);
95     XBT_LOG_CONNECT(gras_ddt_parse, gras_ddt);
96
97     XBT_LOG_CONNECT(gras_modules, gras);
98
99     XBT_LOG_CONNECT(gras_msg, gras);
100     XBT_LOG_CONNECT(gras_msg_read, gras_msg);
101     XBT_LOG_CONNECT(gras_msg_rpc, gras_msg);
102
103     XBT_LOG_CONNECT(gras_timer, gras);
104
105     XBT_LOG_CONNECT(gras_trp, gras);
106     XBT_LOG_CONNECT(gras_trp_meas, gras_trp);
107
108     XBT_LOG_CONNECT(gras_virtu, gras);
109     XBT_LOG_CONNECT(gras_virtu_emul, gras_virtu);
110     XBT_LOG_CONNECT(gras_virtu_process, gras_virtu);
111
112     gras_trp_register();
113     gras_msg_register();
114   }
115
116   /*
117    * Initialize the process specific stuff
118    */
119   gras_process_init();          /* calls procdata_init, which creates process specific data for each module */
120
121   /*
122    * Initialize the global stuff if it's not the first process created
123    */
124   if (gras_running_process++ == 0) {
125     gras_emul_init();
126     gras_msg_init();
127     gras_trp_init();
128     gras_datadesc_init();
129 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
130 # ifdef SIGUSR1
131     signal(SIGUSR1, gras_sigusr_handler);
132 # endif
133     signal(SIGINT, gras_sigint_handler);
134 #endif
135   }
136
137   /* and then init amok */
138   amok_init();
139
140   /* And finally, launch the listener thread */
141   pd = gras_procdata_get();
142   msg_pd = gras_libdata_by_name("gras_msg");
143   pd->listener = gras_msg_listener_launch(msg_pd->msg_received);
144 }
145
146 void gras_exit(void)
147 {
148   gras_procdata_t *pd;
149   INFO0("Exiting GRAS");
150   amok_exit();
151   gras_moddata_leave();
152   pd = gras_procdata_get();
153   gras_msg_listener_shutdown(pd->listener);
154   gras_process_exit();
155   if (--gras_running_process == 0) {
156     gras_msg_exit();
157     gras_trp_exit();
158     gras_datadesc_exit();
159     gras_emul_exit();
160     gras_moddata_exit();
161   }
162   xbt_exit();
163 }
164
165 const char *hexa_str(unsigned char *data, int size, int downside)
166 {
167   static char *buff = NULL;
168   static int buffsize = 0;
169   int i, pos = 0;
170   int begin, increment;
171
172   if (buffsize < 5 * (size + 1)) {
173     if (buff)
174       free(buff);
175     buffsize = 5 * (size + 1);
176     buff = xbt_malloc(buffsize);
177   }
178
179
180   if (downside) {
181     begin = size - 1;
182     increment = -1;
183   } else {
184     begin = 0;
185     increment = 1;
186   }
187
188   for (i = begin; 0 <= i && i < size; i += increment) {
189     if (data[i] < 32 || data[i] > 126)
190       sprintf(buff + pos, ".");
191     else
192       sprintf(buff + pos, "%c", data[i]);
193     while (buff[++pos]);
194   }
195   sprintf(buff + pos, "(");
196   while (buff[++pos]);
197   for (i = begin; 0 <= i && i < size; i += increment) {
198     sprintf(buff + pos, "%02x", data[i]);
199     while (buff[++pos]);
200   }
201   sprintf(buff + pos, ")");
202   while (buff[++pos]);
203   buff[pos] = '\0';
204   return buff;
205 }
206
207 void hexa_print(const char *name, unsigned char *data, int size)
208 {
209   printf("%s: %s\n", name, hexa_str(data, size, 0));
210 }