Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix another race in log initializations.
[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 #include "xbt/socket.h"
15
16 #include "Virtu/virtu_interface.h"      /* Module mechanism FIXME: deplace&rename */
17 #include "Virtu/virtu_private.h"
18 #include "gras_modinter.h"      /* module init/exit */
19 #include "amok/amok_modinter.h" /* module init/exit */
20 #include "xbt_modinter.h"       /* module init/exit */
21
22 #include "gras.h"
23 #include "gras/process.h"       /* FIXME: killme and put process_init in modinter */
24 #include "gras/transport.h"
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   XBT_INFO("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_modules);
56 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
57 XBT_LOG_EXTERNAL_CATEGORY(gras_msg_read);
58 XBT_LOG_EXTERNAL_CATEGORY(gras_msg_rpc);
59 XBT_LOG_EXTERNAL_CATEGORY(gras_timer);
60 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu);
61 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu_emul);
62 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu_process);
63
64 /**
65  * @ingroup GRAS_API
66  * \brief Initialize the gras mechanisms.
67  */
68 void gras_init(int *argc, char **argv)
69 {
70   int first = 0;
71   gras_procdata_t *pd;
72   gras_msg_procdata_t msg_pd;
73   XBT_VERB("Initialize GRAS");
74
75   xbt_getpid = gras_os_getpid;
76   /* First initialize the XBT */
77   xbt_init(argc, argv);
78
79   /* module registrations:
80    *    - declare process specific data we need (without creating them)
81    */
82   if (gras_running_process == 0) {
83     first = 1;
84     /* Connect our log channels: that must be done manually under windows */
85
86     XBT_LOG_CONNECT(gras_modules, gras);
87
88     XBT_LOG_CONNECT(gras_msg, gras);
89     XBT_LOG_CONNECT(gras_msg_read, gras_msg);
90     XBT_LOG_CONNECT(gras_msg_rpc, gras_msg);
91
92     XBT_LOG_CONNECT(gras_timer, gras);
93
94     XBT_LOG_CONNECT(gras_virtu, gras);
95     XBT_LOG_CONNECT(gras_virtu_emul, gras_virtu);
96     XBT_LOG_CONNECT(gras_virtu_process, gras_virtu);
97
98     gras_trp_register();
99     gras_msg_register();
100
101     xbt_trp_plugin_new("file", gras_trp_file_setup);
102     if (gras_if_SG()) {
103       xbt_trp_plugin_new("sg", gras_trp_sg_setup);
104     }
105     /* the TCP plugin (used in RL mode) is automatically loaded by XBT */
106   }
107   gras_running_process++;
108
109   /*
110    * Initialize the process specific stuff
111    */
112   gras_process_init();          /* calls procdata_init, which creates process specific data for each module */
113
114   /*
115    * Initialize the global stuff if it's not the first process created
116    */
117   if (first) {
118     gras_emul_init();
119     gras_msg_init();
120 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
121 # ifdef SIGUSR1
122     signal(SIGUSR1, gras_sigusr_handler);
123 # endif
124     signal(SIGINT, gras_sigint_handler);
125 #endif
126   }
127
128   /* and then init amok */
129   amok_init();
130
131   /* And finally, launch the listener thread */
132   pd = gras_procdata_get();
133   msg_pd = gras_libdata_by_name("gras_msg");
134   pd->listener = gras_msg_listener_launch(msg_pd->msg_received);
135 }
136
137 /**
138  * @ingroup GRAS_API
139  * @brief Finalize the gras mechanisms.
140  * */
141 void gras_exit(void)
142 {
143   XBT_INFO("Exiting GRAS");
144   amok_exit();
145   gras_moddata_leave();
146   gras_msg_listener_shutdown();
147   gras_process_exit();
148   if (--gras_running_process == 0) {
149     gras_msg_exit();
150     gras_emul_exit();
151     gras_moddata_exit();
152   }
153 }
154
155 const char *hexa_str(unsigned char *data, int size, int downside)
156 {
157   static char *buff = NULL;
158   static int buffsize = 0;
159   int i, pos = 0;
160   int begin, increment;
161
162   if (buffsize < 5 * (size + 1)) {
163     free(buff);
164     buffsize = 5 * (size + 1);
165     buff = xbt_malloc(buffsize);
166   }
167
168
169   if (downside) {
170     begin = size - 1;
171     increment = -1;
172   } else {
173     begin = 0;
174     increment = 1;
175   }
176
177   for (i = begin; 0 <= i && i < size; i += increment) {
178     if (data[i] < 32 || data[i] > 126)
179       sprintf(buff + pos, ".");
180     else
181       sprintf(buff + pos, "%c", data[i]);
182     while (buff[++pos]);
183   }
184   sprintf(buff + pos, "(");
185   while (buff[++pos]);
186   for (i = begin; 0 <= i && i < size; i += increment) {
187     sprintf(buff + pos, "%02x", data[i]);
188     while (buff[++pos]);
189   }
190   sprintf(buff + pos, ")");
191   while (buff[++pos]);
192   buff[pos] = '\0';
193   return buff;
194 }
195
196 void hexa_print(const char *name, unsigned char *data, int size)
197 {
198   printf("%s: %s\n", name, hexa_str(data, size, 0));
199 }