Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9e826f72ac57496839cec43dabefcae7080a800f
[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   INFO0("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 void gras_init(int *argc, char **argv)
72 {
73
74   gras_procdata_t *pd;
75   gras_msg_procdata_t msg_pd;
76   VERB0("Initialize GRAS");
77
78   xbt_getpid = gras_os_getpid;
79   /* First initialize the XBT */
80   xbt_init(argc, argv);
81
82   /* module registrations:
83    *    - declare process specific data we need (without creating them)
84    */
85   if (gras_running_process == 0) {
86     /* Connect our log channels: that must be done manually under windows */
87     XBT_LOG_CONNECT(gras_ddt, gras);
88     XBT_LOG_CONNECT(gras_ddt_cbps, gras_ddt);
89     XBT_LOG_CONNECT(gras_ddt_convert, gras_ddt);
90     XBT_LOG_CONNECT(gras_ddt_create, gras_ddt);
91     XBT_LOG_CONNECT(gras_ddt_exchange, gras_ddt);
92     XBT_LOG_CONNECT(gras_ddt_lexer, gras_ddt_parse);
93     XBT_LOG_CONNECT(gras_ddt_parse, gras_ddt);
94
95     XBT_LOG_CONNECT(gras_modules, gras);
96
97     XBT_LOG_CONNECT(gras_msg, gras);
98     XBT_LOG_CONNECT(gras_msg_read, gras_msg);
99     XBT_LOG_CONNECT(gras_msg_rpc, gras_msg);
100
101     XBT_LOG_CONNECT(gras_timer, gras);
102
103     XBT_LOG_CONNECT(gras_trp, gras);
104     XBT_LOG_CONNECT(gras_trp_meas, gras_trp);
105
106     XBT_LOG_CONNECT(gras_virtu, gras);
107     XBT_LOG_CONNECT(gras_virtu_emul, gras_virtu);
108     XBT_LOG_CONNECT(gras_virtu_process, gras_virtu);
109
110     gras_trp_register();
111     gras_msg_register();
112   }
113
114   /*
115    * Initialize the process specific stuff
116    */
117   gras_process_init();          /* calls procdata_init, which creates process specific data for each module */
118
119   /*
120    * Initialize the global stuff if it's not the first process created
121    */
122   if (gras_running_process++ == 0) {
123     gras_emul_init();
124     gras_msg_init();
125     gras_trp_init();
126     gras_datadesc_init();
127 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
128 # ifdef SIGUSR1
129     signal(SIGUSR1, gras_sigusr_handler);
130 # endif
131     signal(SIGINT, gras_sigint_handler);
132 #endif
133   }
134
135   /* and then init amok */
136   amok_init();
137
138   /* And finally, launch the listener thread */
139   pd = gras_procdata_get();
140   msg_pd = gras_libdata_by_name("gras_msg");
141   pd->listener = gras_msg_listener_launch(msg_pd->msg_received);
142 }
143
144 void gras_exit(void)
145 {
146   gras_procdata_t *pd;
147   INFO0("Exiting GRAS");
148   amok_exit();
149   gras_moddata_leave();
150   pd = gras_procdata_get();
151   gras_msg_listener_shutdown();
152   gras_process_exit();
153   if (--gras_running_process == 0) {
154     gras_msg_exit();
155     gras_trp_exit();
156     gras_datadesc_exit();
157     gras_emul_exit();
158     gras_moddata_exit();
159   }
160 }
161
162 const char *hexa_str(unsigned char *data, int size, int downside)
163 {
164   static char *buff = NULL;
165   static int buffsize = 0;
166   int i, pos = 0;
167   int begin, increment;
168
169   if (buffsize < 5 * (size + 1)) {
170     if (buff)
171       free(buff);
172     buffsize = 5 * (size + 1);
173     buff = xbt_malloc(buffsize);
174   }
175
176
177   if (downside) {
178     begin = size - 1;
179     increment = -1;
180   } else {
181     begin = 0;
182     increment = 1;
183   }
184
185   for (i = begin; 0 <= i && i < size; i += increment) {
186     if (data[i] < 32 || data[i] > 126)
187       sprintf(buff + pos, ".");
188     else
189       sprintf(buff + pos, "%c", data[i]);
190     while (buff[++pos]);
191   }
192   sprintf(buff + pos, "(");
193   while (buff[++pos]);
194   for (i = begin; 0 <= i && i < size; i += increment) {
195     sprintf(buff + pos, "%02x", data[i]);
196     while (buff[++pos]);
197   }
198   sprintf(buff + pos, ")");
199   while (buff[++pos]);
200   buff[pos] = '\0';
201   return buff;
202 }
203
204 void hexa_print(const char *name, unsigned char *data, int size)
205 {
206   printf("%s: %s\n", name, hexa_str(data, size, 0));
207 }