Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove gras from the main documentation
[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 #include "xbt/socket.h"
15
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 */
21
22 #include "gras.h"
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 */
27
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)
33 {
34   XBT_INFO("SIGUSR1 received. Display the backtrace");
35   xbt_backtrace_display_current();
36 }
37
38 static void gras_sigint_handler(int sig)
39 {
40   static double lastone = 0;
41   if (lastone == 0 || xbt_os_time() - lastone > 5) {
42     if (gras_if_RL())
43       xbt_backtrace_display_current();
44     else
45       SIMIX_display_process_status();
46     fprintf(stderr,
47             "\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
48     lastone = xbt_os_time();
49   } else {
50     exit(1);
51   }
52 }
53 #endif
54
55 /**
56  * @ingroup GRAS_API
57  * \brief Initialize the gras mechanisms.
58  */
59 void gras_init(int *argc, char **argv)
60 {
61   int first = 0;
62   gras_procdata_t *pd;
63   gras_msg_procdata_t msg_pd;
64
65   xbt_getpid = gras_os_getpid;
66   /* First initialize the XBT */
67   xbt_init(argc, argv);
68
69   XBT_VERB("Initialize GRAS");
70
71   /* module registrations:
72    *    - declare process specific data we need (without creating them)
73    */
74   if (gras_running_process == 0) {
75     first = 1;
76
77     if (!getenv("GRAS_NO_WARN_EXPERIMENTAL"))
78       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");
79
80     gras_trp_register();
81     gras_msg_register();
82
83     xbt_trp_plugin_new("file", gras_trp_file_setup);
84     if (gras_if_SG()) {
85       xbt_trp_plugin_new("sg", gras_trp_sg_setup);
86     }
87     /* the TCP plugin (used in RL mode) is automatically loaded by XBT */
88   }
89   gras_running_process++;
90
91   /*
92    * Initialize the process specific stuff
93    */
94   gras_process_init();          /* calls procdata_init, which creates process specific data for each module */
95
96   /*
97    * Initialize the global stuff if it's not the first process created
98    */
99   if (first) {
100     gras_emul_init();
101     gras_msg_init();
102 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
103 # ifdef SIGUSR1
104     signal(SIGUSR1, gras_sigusr_handler);
105 # endif
106     signal(SIGINT, gras_sigint_handler);
107 #endif
108   }
109
110   /* and then init amok */
111   amok_init();
112
113   /* And finally, launch the listener thread */
114   pd = gras_procdata_get();
115   msg_pd = gras_libdata_by_name("gras_msg");
116   pd->listener = gras_msg_listener_launch(msg_pd->msg_received);
117 }
118
119 /**
120  * @ingroup GRAS_API
121  * @brief Finalize the gras mechanisms.
122  * */
123 void gras_exit(void)
124 {
125   XBT_INFO("Exiting GRAS");
126   amok_exit();
127   gras_moddata_leave();
128   gras_msg_listener_shutdown();
129   gras_process_exit();
130   if (--gras_running_process == 0) {
131     gras_msg_exit();
132     gras_emul_exit();
133     gras_moddata_exit();
134   }
135 }
136
137 const char *hexa_str(unsigned char *data, int size, int downside)
138 {
139   static char *buff = NULL;
140   static int buffsize = 0;
141   int i, pos = 0;
142   int begin, increment;
143
144   if (buffsize < 5 * (size + 1)) {
145     free(buff);
146     buffsize = 5 * (size + 1);
147     buff = xbt_malloc(buffsize);
148   }
149
150
151   if (downside) {
152     begin = size - 1;
153     increment = -1;
154   } else {
155     begin = 0;
156     increment = 1;
157   }
158
159   for (i = begin; 0 <= i && i < size; i += increment) {
160     if (data[i] < 32 || data[i] > 126)
161       sprintf(buff + pos, ".");
162     else
163       sprintf(buff + pos, "%c", data[i]);
164     while (buff[++pos]);
165   }
166   sprintf(buff + pos, "(");
167   while (buff[++pos]);
168   for (i = begin; 0 <= i && i < size; i += increment) {
169     sprintf(buff + pos, "%02x", data[i]);
170     while (buff[++pos]);
171   }
172   sprintf(buff + pos, ")");
173   while (buff[++pos]);
174   buff[pos] = '\0';
175   return buff;
176 }
177
178 void hexa_print(const char *name, unsigned char *data, int size)
179 {
180   printf("%s: %s\n", name, hexa_str(data, size, 0));
181 }