Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New thread to receive messages. Not working yet.
[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_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"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 void gras_init(int *argc,char **argv) {
50
51         gras_procdata_t *pd;
52         gras_msg_procdata_t msg_pd;
53   VERB0("Initialize GRAS");
54
55   xbt_getpid = &gras_os_getpid;
56   /* First initialize the XBT */
57   xbt_init(argc,argv);
58    
59   /* module registrations: 
60    *    - declare process specific data we need (without creating them) 
61    */
62   if (gras_running_process == 0) {
63      gras_trp_register();
64      gras_msg_register();
65   }
66    
67   /*
68    * Initialize the process specific stuff
69    */
70   gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
71   
72   /*
73    * Initialize the global stuff if it's not the first process created
74    */
75   if (gras_running_process++ == 0) {
76     gras_emul_init();
77     gras_msg_init();
78     gras_trp_init();
79     gras_datadesc_init();
80 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
81 # ifdef SIGUSR1
82     signal(SIGUSR1,gras_sigusr_handler);
83 # endif
84     signal(SIGINT,gras_sigint_handler);
85 #endif     
86   }
87    
88   /* and then init amok */
89   amok_init();
90         pd = gras_procdata_get();
91         msg_pd = gras_libdata_by_name("gras_msg");
92         pd->listener = gras_msg_listener_launch(msg_pd->msg_received);
93 }
94
95 void gras_exit(void) {
96         gras_procdata_t *pd;
97   INFO0("Exiting GRAS");
98   amok_exit();
99   gras_moddata_leave();
100         pd = gras_procdata_get();
101         gras_msg_listener_shutdown(pd->listener);
102   gras_process_exit();
103   if (--gras_running_process == 0) {
104     gras_msg_exit();
105     gras_trp_exit();
106     gras_datadesc_exit();
107     gras_emul_exit();
108     gras_moddata_exit();
109   }
110   xbt_exit();
111 }
112
113 const char *hexa_str(unsigned char *data, int size, int downside) {
114   static char*buff=NULL;
115   static int buffsize=0;
116   int i,pos=0;
117   int begin,increment;  
118   
119   if (buffsize<5*(size+1)) {
120     if (buff)
121       free(buff);
122     buffsize=5*(size+1);
123     buff=xbt_malloc(buffsize);
124   }
125    
126
127   if (downside) {
128      begin=size-1;
129      increment=-1;
130   } else {
131      begin=0;
132      increment=1;
133   }
134    
135   for (i=begin; 0<=i && i<size ; i+=increment)  {
136     if (data[i]<32 || data[i]>126)
137       sprintf(buff+pos,".");
138     else
139       sprintf(buff+pos,"%c",data[i]);
140     while (buff[++pos]);
141    }
142   sprintf(buff+pos,"(");
143   while (buff[++pos]);
144   for (i=begin; 0<=i && i<size ; i+=increment)  {
145     sprintf(buff+pos,"%02x",data[i]);
146     while (buff[++pos]);
147    }
148   sprintf(buff+pos,")");
149   while (buff[++pos]);
150   buff[pos]='\0';  
151   return buff;
152 }
153 void hexa_print(const char*name, unsigned char *data, int size) {
154    printf("%s: %s\n", name,hexa_str(data,size,0));
155 }
156