3 /* gras.c -- generic functions not fitting anywhere else */
5 /* Copyright (c) 2003, 2004 Martin Quinson. */
6 /* All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "xbt/virtu.h" /* set the XBT virtualization to use GRAS */
13 #include "xbt/module.h" /* xbt_init/exit */
14 #include "xbt/xbt_os_time.h" /* xbt_os_time */
15 #include "xbt/synchro.h"
17 #include "Virtu/virtu_interface.h" /* Module mechanism FIXME: deplace&rename */
18 #include "Virtu/virtu_private.h"
19 #include "gras_modinter.h" /* module init/exit */
20 #include "amok/amok_modinter.h" /* module init/exit */
21 #include "xbt_modinter.h" /* module init/exit */
24 #include "gras/process.h" /* FIXME: killme and put process_init in modinter */
25 #include "gras/Msg/msg_private.h"
26 #include "portable.h" /* hexa_*(); signalling stuff */
28 XBT_LOG_NEW_DEFAULT_CATEGORY(gras,"All GRAS categories (cf. section \ref GRAS_API)");
29 static int gras_running_process = 0;
30 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
31 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) {
37 static double lastone = 0;
38 if (lastone == 0 || xbt_os_time() - lastone > 5) {
40 xbt_backtrace_display_current();
42 SIMIX_display_process_status();
44 "\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
45 lastone = xbt_os_time();
52 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt);
53 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_cbps);
54 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_convert);
55 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_create);
56 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_exchange);
57 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_lexer);
58 XBT_LOG_EXTERNAL_CATEGORY(gras_ddt_parse);
59 XBT_LOG_EXTERNAL_CATEGORY(gras_modules);
60 XBT_LOG_EXTERNAL_CATEGORY(gras_msg);
61 XBT_LOG_EXTERNAL_CATEGORY(gras_msg_read);
62 XBT_LOG_EXTERNAL_CATEGORY(gras_msg_rpc);
63 XBT_LOG_EXTERNAL_CATEGORY(gras_timer);
64 XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
65 XBT_LOG_EXTERNAL_CATEGORY(gras_trp_meas);
66 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu);
67 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu_emul);
68 XBT_LOG_EXTERNAL_CATEGORY(gras_virtu_process);
70 void gras_init(int *argc,char **argv) {
73 gras_msg_procdata_t msg_pd;
74 VERB0("Initialize GRAS");
76 xbt_getpid = gras_os_getpid;
77 /* First initialize the XBT */
80 /* module registrations:
81 * - declare process specific data we need (without creating them)
83 if (gras_running_process == 0) {
84 /* Connect our log channels: that must be done manually under windows */
85 XBT_LOG_CONNECT(gras_ddt, gras);
86 XBT_LOG_CONNECT(gras_ddt_cbps, gras_ddt);
87 XBT_LOG_CONNECT(gras_ddt_convert, gras_ddt);
88 XBT_LOG_CONNECT(gras_ddt_create, gras_ddt);
89 XBT_LOG_CONNECT(gras_ddt_exchange, gras_ddt);
90 XBT_LOG_CONNECT(gras_ddt_lexer, gras_ddt_parse);
91 XBT_LOG_CONNECT(gras_ddt_parse, gras_ddt);
93 XBT_LOG_CONNECT(gras_modules, gras);
95 XBT_LOG_CONNECT(gras_msg, gras);
96 XBT_LOG_CONNECT(gras_msg_read, gras_msg);
97 XBT_LOG_CONNECT(gras_msg_rpc, gras_msg);
99 XBT_LOG_CONNECT(gras_timer, gras);
101 XBT_LOG_CONNECT(gras_trp, gras);
102 XBT_LOG_CONNECT(gras_trp_meas, gras_trp);
104 XBT_LOG_CONNECT(gras_virtu, gras);
105 XBT_LOG_CONNECT(gras_virtu_emul, gras_virtu);
106 XBT_LOG_CONNECT(gras_virtu_process, gras_virtu);
113 * Initialize the process specific stuff
115 gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
118 * Initialize the global stuff if it's not the first process created
120 if (gras_running_process++ == 0) {
124 gras_datadesc_init();
125 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
127 signal(SIGUSR1,gras_sigusr_handler);
129 signal(SIGINT,gras_sigint_handler);
133 /* and then init amok */
136 /* And finally, launch the listener thread */
137 pd = gras_procdata_get();
138 msg_pd = gras_libdata_by_name("gras_msg");
139 pd->listener = gras_msg_listener_launch(msg_pd->msg_received);
142 void gras_exit(void) {
144 INFO0("Exiting GRAS");
146 gras_moddata_leave();
147 pd = gras_procdata_get();
148 gras_msg_listener_shutdown(pd->listener);
150 if (--gras_running_process == 0) {
153 gras_datadesc_exit();
160 const char *hexa_str(unsigned char *data, int size, int downside) {
161 static char*buff=NULL;
162 static int buffsize=0;
166 if (buffsize<5*(size+1)) {
170 buff=xbt_malloc(buffsize);
182 for (i=begin; 0<=i && i<size ; i+=increment) {
183 if (data[i]<32 || data[i]>126)
184 sprintf(buff+pos,".");
186 sprintf(buff+pos,"%c",data[i]);
189 sprintf(buff+pos,"(");
191 for (i=begin; 0<=i && i<size ; i+=increment) {
192 sprintf(buff+pos,"%02x",data[i]);
195 sprintf(buff+pos,")");
200 void hexa_print(const char*name, unsigned char *data, int size) {
201 printf("%s: %s\n", name,hexa_str(data,size,0));