Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
58c80455519175b87f5e9a700d37bdeb6fae144c
[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/module.h" /* xbt_init/exit */
13
14 #include "Virtu/virtu_interface.h" /* Module mechanism FIXME: deplace&rename */
15 #include "gras_modinter.h"   /* module init/exit */
16 #include "amok/amok_modinter.h"   /* module init/exit */
17 #include "xbt_modinter.h"   /* module init/exit */
18
19 #include "gras.h"
20 #include "gras/process.h" /* FIXME: killme and put process_init in modinter */
21   
22 #include "portable.h" /* hexa_*(); signalling stuff */
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories (cf. section \ref GRAS_API)");
25 static int gras_running_process = 0;
26
27 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
28 static void gras_sigusr_handler(int sig) {
29    INFO0("SIGUSR1 received. Display the backtrace");
30    xbt_backtrace_display();
31 }
32
33 static void gras_sigint_handler(int sig) {
34    static double lastone = 0;
35    if (lastone == 0 || gras_os_time() - lastone > 5) {
36       lastone = gras_os_time();
37       xbt_backtrace_display();
38       fprintf(stderr,"\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
39    } else {
40       exit(1);
41    }
42 }
43 #endif
44
45 void gras_init(int *argc,char **argv) {
46
47   VERB0("Initialize GRAS");
48   
49   /* First initialize the XBT */
50   xbt_init(argc,argv);
51    
52   /* module registrations: 
53    *    - declare process specific data we need (without creating them) 
54    */
55   if (gras_running_process == 0) {
56      gras_trp_register();
57      gras_msg_register();
58   }
59    
60   /*
61    * Initialize the process specific stuff
62    */
63   gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
64   
65   /*
66    * Initialize the global stuff if it's not the first process created
67    */
68   if (gras_running_process++ == 0) {
69     gras_emul_init();
70     gras_msg_init();
71     gras_trp_init();
72     gras_datadesc_init();
73 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
74 # ifdef SIGUSR1
75     signal(SIGUSR1,gras_sigusr_handler);
76 # endif
77     signal(SIGINT,gras_sigint_handler);
78 #endif     
79   }
80    
81   /* and then init amok */
82   amok_init();
83 }
84
85 void gras_exit(void) {
86   INFO0("Exiting GRAS");
87   amok_exit();
88   gras_moddata_leave();
89   if (--gras_running_process == 0) {
90     gras_msg_exit();
91     gras_trp_exit();
92     gras_datadesc_exit();
93     gras_emul_exit();
94     gras_moddata_exit();
95   }
96   gras_process_exit();
97   xbt_exit();
98 }
99
100 const char *hexa_str(unsigned char *data, int size, int downside) {
101   static char*buff=NULL;
102   static int buffsize=0;
103   int i,pos=0;  
104   
105   if (buffsize<5*(size+1)) {
106     if (buff)
107       free(buff);
108     buffsize=5*(size+1);
109     buff=xbt_malloc(buffsize);
110   }
111    
112   int begin,increment;   
113   if (downside) {
114      begin=size-1;
115      increment=-1;
116   } else {
117      begin=0;
118      increment=1;
119   }
120    
121   for (i=begin; 0<=i && i<size ; i+=increment)  {
122     if (data[i]<32 || data[i]>126)
123       sprintf(buff+pos,".");
124     else
125       sprintf(buff+pos,"%c",data[i]);
126     while (buff[++pos]);
127    }
128   sprintf(buff+pos,"(");
129   while (buff[++pos]);
130   for (i=begin; 0<=i && i<size ; i+=increment)  {
131     sprintf(buff+pos,"%02x",data[i]);
132     while (buff[++pos]);
133    }
134   sprintf(buff+pos,")");
135   while (buff[++pos]);
136   buff[pos]='\0';  
137   return buff;
138 }
139 void hexa_print(const char*name, unsigned char *data, int size) {
140    printf("%s: %s\n", name,hexa_str(data,size,0));
141 }
142