Logo AND Algorithmique Numérique Distribuée

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