Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add an unused (for the moment) module file
[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
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 */
20
21 #include "gras.h"
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 */
25
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)
31 {
32   INFO0("SIGUSR1 received. Display the backtrace");
33   xbt_backtrace_display_current();
34 }
35
36 static void gras_sigint_handler(int sig)
37 {
38   static double lastone = 0;
39   if (lastone == 0 || xbt_os_time() - lastone > 5) {
40     if (gras_if_RL())
41       xbt_backtrace_display_current();
42     else
43       SIMIX_display_process_status();
44     fprintf(stderr,
45             "\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
46     lastone = xbt_os_time();
47   } else {
48     exit(1);
49   }
50 }
51 #endif
52
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);
70
71 void gras_init(int *argc, char **argv)
72 {
73   int first = 0;
74   gras_procdata_t *pd;
75   gras_msg_procdata_t msg_pd;
76   VERB0("Initialize GRAS");
77
78   xbt_getpid = gras_os_getpid;
79   /* First initialize the XBT */
80   xbt_init(argc, argv);
81
82   /* module registrations:
83    *    - declare process specific data we need (without creating them)
84    */
85   if (gras_running_process == 0) {
86     first = 1;
87     /* Connect our log channels: that must be done manually under windows */
88     XBT_LOG_CONNECT(gras_ddt, gras);
89     XBT_LOG_CONNECT(gras_ddt_cbps, gras_ddt);
90     XBT_LOG_CONNECT(gras_ddt_convert, gras_ddt);
91     XBT_LOG_CONNECT(gras_ddt_create, gras_ddt);
92     XBT_LOG_CONNECT(gras_ddt_exchange, gras_ddt);
93     XBT_LOG_CONNECT(gras_ddt_lexer, gras_ddt_parse);
94     XBT_LOG_CONNECT(gras_ddt_parse, gras_ddt);
95
96     XBT_LOG_CONNECT(gras_modules, gras);
97
98     XBT_LOG_CONNECT(gras_msg, gras);
99     XBT_LOG_CONNECT(gras_msg_read, gras_msg);
100     XBT_LOG_CONNECT(gras_msg_rpc, gras_msg);
101
102     XBT_LOG_CONNECT(gras_timer, gras);
103
104     XBT_LOG_CONNECT(gras_trp, gras);
105     XBT_LOG_CONNECT(gras_trp_meas, gras_trp);
106
107     XBT_LOG_CONNECT(gras_virtu, gras);
108     XBT_LOG_CONNECT(gras_virtu_emul, gras_virtu);
109     XBT_LOG_CONNECT(gras_virtu_process, gras_virtu);
110
111     gras_trp_register();
112     gras_msg_register();
113   }
114   gras_running_process++;
115
116   /*
117    * Initialize the process specific stuff
118    */
119   gras_process_init();          /* calls procdata_init, which creates process specific data for each module */
120
121   /*
122    * Initialize the global stuff if it's not the first process created
123    */
124   if (first) {
125     gras_emul_init();
126     gras_msg_init();
127     gras_trp_init();
128     gras_datadesc_init();
129 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
130 # ifdef SIGUSR1
131     signal(SIGUSR1, gras_sigusr_handler);
132 # endif
133     signal(SIGINT, gras_sigint_handler);
134 #endif
135   }
136
137   /* and then init amok */
138   amok_init();
139
140   /* And finally, launch the listener thread */
141   pd = gras_procdata_get();
142   msg_pd = gras_libdata_by_name("gras_msg");
143   pd->listener = gras_msg_listener_launch(msg_pd->msg_received);
144 }
145
146 void gras_exit(void)
147 {
148   gras_procdata_t *pd;
149   INFO0("Exiting GRAS");
150   amok_exit();
151   gras_moddata_leave();
152   pd = gras_procdata_get();
153   gras_msg_listener_shutdown();
154   gras_process_exit();
155   if (--gras_running_process == 0) {
156     gras_msg_exit();
157     gras_trp_exit();
158     gras_datadesc_exit();
159     gras_emul_exit();
160     gras_moddata_exit();
161   }
162 }
163
164 const char *hexa_str(unsigned char *data, int size, int downside)
165 {
166   static char *buff = NULL;
167   static int buffsize = 0;
168   int i, pos = 0;
169   int begin, increment;
170
171   if (buffsize < 5 * (size + 1)) {
172     if (buff)
173       free(buff);
174     buffsize = 5 * (size + 1);
175     buff = xbt_malloc(buffsize);
176   }
177
178
179   if (downside) {
180     begin = size - 1;
181     increment = -1;
182   } else {
183     begin = 0;
184     increment = 1;
185   }
186
187   for (i = begin; 0 <= i && i < size; i += increment) {
188     if (data[i] < 32 || data[i] > 126)
189       sprintf(buff + pos, ".");
190     else
191       sprintf(buff + pos, "%c", data[i]);
192     while (buff[++pos]);
193   }
194   sprintf(buff + pos, "(");
195   while (buff[++pos]);
196   for (i = begin; 0 <= i && i < size; i += increment) {
197     sprintf(buff + pos, "%02x", data[i]);
198     while (buff[++pos]);
199   }
200   sprintf(buff + pos, ")");
201   while (buff[++pos]);
202   buff[pos] = '\0';
203   return buff;
204 }
205
206 void hexa_print(const char *name, unsigned char *data, int size)
207 {
208   printf("%s: %s\n", name, hexa_str(data, size, 0));
209 }