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"
14 #include "xbt/socket.h"
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 */
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 */
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)
34 XBT_INFO("SIGUSR1 received. Display the backtrace");
35 xbt_backtrace_display_current();
38 static void gras_sigint_handler(int sig)
40 static double lastone = 0;
41 if (lastone == 0 || xbt_os_time() - lastone > 5) {
43 xbt_backtrace_display_current();
45 SIMIX_display_process_status();
47 "\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
48 lastone = xbt_os_time();
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);
66 * \brief Initialize the gras mechanisms.
68 void gras_init(int *argc, char **argv)
72 gras_msg_procdata_t msg_pd;
74 xbt_getpid = gras_os_getpid;
75 /* First initialize the XBT */
78 XBT_VERB("Initialize GRAS");
80 /* module registrations:
81 * - declare process specific data we need (without creating them)
83 if (gras_running_process == 0) {
85 /* Connect our log channels: that must be done manually under windows */
87 XBT_LOG_CONNECT(gras_modules, gras);
89 XBT_LOG_CONNECT(gras_msg, gras);
90 XBT_LOG_CONNECT(gras_msg_read, gras_msg);
91 XBT_LOG_CONNECT(gras_msg_rpc, gras_msg);
93 XBT_LOG_CONNECT(gras_timer, gras);
95 XBT_LOG_CONNECT(gras_virtu, gras);
96 XBT_LOG_CONNECT(gras_virtu_emul, gras_virtu);
97 XBT_LOG_CONNECT(gras_virtu_process, gras_virtu);
99 if (!getenv("GRAS_NO_WARN_EXPERIMENTAL"))
100 XBT_WARN("GRAS is not well maintained anymore. We consider it to be experimental (and not stable anymore) since SimGrid 3.6. Sorry about it, please consider contributing to improve this situation");
105 xbt_trp_plugin_new("file", gras_trp_file_setup);
107 xbt_trp_plugin_new("sg", gras_trp_sg_setup);
109 /* the TCP plugin (used in RL mode) is automatically loaded by XBT */
111 gras_running_process++;
114 * Initialize the process specific stuff
116 gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
119 * Initialize the global stuff if it's not the first process created
124 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
126 signal(SIGUSR1, gras_sigusr_handler);
128 signal(SIGINT, gras_sigint_handler);
132 /* and then init amok */
135 /* And finally, launch the listener thread */
136 pd = gras_procdata_get();
137 msg_pd = gras_libdata_by_name("gras_msg");
138 pd->listener = gras_msg_listener_launch(msg_pd->msg_received);
143 * @brief Finalize the gras mechanisms.
147 XBT_INFO("Exiting GRAS");
149 gras_moddata_leave();
150 gras_msg_listener_shutdown();
152 if (--gras_running_process == 0) {
159 const char *hexa_str(unsigned char *data, int size, int downside)
161 static char *buff = NULL;
162 static int buffsize = 0;
164 int begin, increment;
166 if (buffsize < 5 * (size + 1)) {
168 buffsize = 5 * (size + 1);
169 buff = xbt_malloc(buffsize);
181 for (i = begin; 0 <= i && i < size; i += increment) {
182 if (data[i] < 32 || data[i] > 126)
183 sprintf(buff + pos, ".");
185 sprintf(buff + pos, "%c", data[i]);
188 sprintf(buff + pos, "(");
190 for (i = begin; 0 <= i && i < size; i += increment) {
191 sprintf(buff + pos, "%02x", data[i]);
194 sprintf(buff + pos, ")");
200 void hexa_print(const char *name, unsigned char *data, int size)
202 printf("%s: %s\n", name, hexa_str(data, size, 0));