Logo AND Algorithmique Numérique Distribuée

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