1 /* gras.c -- generic functions not fitting anywhere else */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
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"
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 */
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 */
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)
32 XBT_INFO("SIGUSR1 received. Display the backtrace");
33 xbt_backtrace_display_current();
36 static void gras_sigint_handler(int sig)
38 static double lastone = 0;
39 if (lastone == 0 || xbt_os_time() - lastone > 5) {
41 xbt_backtrace_display_current();
43 SIMIX_display_process_status();
45 "\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
46 lastone = xbt_os_time();
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);
73 * \brief Initialize the gras mechanisms.
75 void gras_init(int *argc, char **argv)
79 gras_msg_procdata_t msg_pd;
80 XBT_VERB("Initialize GRAS");
82 xbt_getpid = gras_os_getpid;
83 /* First initialize the XBT */
86 /* module registrations:
87 * - declare process specific data we need (without creating them)
89 if (gras_running_process == 0) {
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);
100 XBT_LOG_CONNECT(gras_modules, gras);
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);
106 XBT_LOG_CONNECT(gras_timer, gras);
108 XBT_LOG_CONNECT(gras_trp, gras);
109 XBT_LOG_CONNECT(gras_trp_meas, gras_trp);
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);
118 gras_running_process++;
121 * Initialize the process specific stuff
123 gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
126 * Initialize the global stuff if it's not the first process created
132 gras_datadesc_init();
133 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
135 signal(SIGUSR1, gras_sigusr_handler);
137 signal(SIGINT, gras_sigint_handler);
141 /* and then init amok */
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);
152 * @brief Finalize the gras mechanisms.
156 XBT_INFO("Exiting GRAS");
158 gras_moddata_leave();
159 gras_msg_listener_shutdown();
161 if (--gras_running_process == 0) {
164 gras_datadesc_exit();
170 const char *hexa_str(unsigned char *data, int size, int downside)
172 static char *buff = NULL;
173 static int buffsize = 0;
175 int begin, increment;
177 if (buffsize < 5 * (size + 1)) {
180 buffsize = 5 * (size + 1);
181 buff = xbt_malloc(buffsize);
193 for (i = begin; 0 <= i && i < size; i += increment) {
194 if (data[i] < 32 || data[i] > 126)
195 sprintf(buff + pos, ".");
197 sprintf(buff + pos, "%c", data[i]);
200 sprintf(buff + pos, "(");
202 for (i = begin; 0 <= i && i < size; i += increment) {
203 sprintf(buff + pos, "%02x", data[i]);
206 sprintf(buff + pos, ")");
212 void hexa_print(const char *name, unsigned char *data, int size)
214 printf("%s: %s\n", name, hexa_str(data, size, 0));