Logo AND Algorithmique Numérique Distribuée

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