Logo AND Algorithmique Numérique Distribuée

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