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 INFO0("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);
71 void gras_init(int *argc, char **argv)
75 gras_msg_procdata_t msg_pd;
76 VERB0("Initialize GRAS");
78 xbt_getpid = gras_os_getpid;
79 /* First initialize the XBT */
82 /* module registrations:
83 * - declare process specific data we need (without creating them)
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);
95 XBT_LOG_CONNECT(gras_modules, gras);
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);
101 XBT_LOG_CONNECT(gras_timer, gras);
103 XBT_LOG_CONNECT(gras_trp, gras);
104 XBT_LOG_CONNECT(gras_trp_meas, gras_trp);
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);
115 * Initialize the process specific stuff
117 gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
120 * Initialize the global stuff if it's not the first process created
122 if (gras_running_process++ == 0) {
126 gras_datadesc_init();
127 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
129 signal(SIGUSR1, gras_sigusr_handler);
131 signal(SIGINT, gras_sigint_handler);
135 /* and then init amok */
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);
147 INFO0("Exiting GRAS");
149 gras_moddata_leave();
150 pd = gras_procdata_get();
151 gras_msg_listener_shutdown();
153 if (--gras_running_process == 0) {
156 gras_datadesc_exit();
162 const char *hexa_str(unsigned char *data, int size, int downside)
164 static char *buff = NULL;
165 static int buffsize = 0;
167 int begin, increment;
169 if (buffsize < 5 * (size + 1)) {
172 buffsize = 5 * (size + 1);
173 buff = xbt_malloc(buffsize);
185 for (i = begin; 0 <= i && i < size; i += increment) {
186 if (data[i] < 32 || data[i] > 126)
187 sprintf(buff + pos, ".");
189 sprintf(buff + pos, "%c", data[i]);
192 sprintf(buff + pos, "(");
194 for (i = begin; 0 <= i && i < size; i += increment) {
195 sprintf(buff + pos, "%02x", data[i]);
198 sprintf(buff + pos, ")");
204 void hexa_print(const char *name, unsigned char *data, int size)
206 printf("%s: %s\n", name, hexa_str(data, size, 0));