Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new function: gras_socket_meas_accept (mandatory on meas sockets, forbiden on others...
[simgrid.git] / src / gras / Virtu / sg_emul.c
1 /* $Id$ */
2
3 /* sg_emul - Emulation support (simulation)                                 */
4
5 /* Copyright (c) 2003-5 Arnaud Legrand, Martin Quinson. All rights reserved.*/
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "gras/emul.h"
11 #include "gras/Virtu/virtu_sg.h"
12 #include "gras_modinter.h"
13
14 #include "xbt/xbt_portability.h" /* timers */
15 #include "xbt/dict.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(emul,gras,"Emulation support");
18
19 /*** Timing macros ***/
20 static xbt_os_timer_t timer;
21 static int benchmarking = 0;
22 static xbt_dict_t benchmark_set = NULL;
23 static double reference = .00523066250047108838; /* FIXME: we should benchmark host machine to set this */
24 static double duration = 0.0;
25
26 static char* locbuf = NULL;
27 static int locbufsize;
28
29 void gras_emul_init(void)
30 {
31   if(!benchmark_set) {
32     benchmark_set = xbt_dict_new();
33     timer = xbt_os_timer_new();
34   }
35 }
36
37 void gras_emul_exit(void) {
38   if (locbuf) free(locbuf);
39   xbt_dict_free(&benchmark_set);
40   xbt_os_timer_free(timer);
41 }
42
43
44 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
45 {
46   double *ir = NULL;
47
48   xbt_dict_get(dict, key, (void *) &ir);
49   if (!ir) {
50     ir = xbt_new0(double,1);
51     xbt_dict_set(dict, key, ir, free);
52   }
53   *ir = value;
54 }
55
56 static double get_from_dict(xbt_dict_t dict, const char *key)
57 {
58   double *ir = NULL;
59
60   xbt_dict_get(dict, key, (void *) &ir);
61
62   return *ir;
63 }
64
65 int gras_bench_always_begin(const char *location,int line)
66 {
67   xbt_assert0(!benchmarking,"Already benchmarking");
68   benchmarking = 1;
69
70   if (!timer)
71   xbt_os_timer_start(timer);
72   return 0;
73 }
74
75 int gras_bench_always_end(void)
76 {
77   m_task_t task = NULL;
78   
79   xbt_assert0(benchmarking,"Not benchmarking yet");
80   benchmarking = 0;
81   xbt_os_timer_stop(timer);
82   duration = xbt_os_timer_elapsed(timer);
83   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
84   MSG_task_execute(task);
85   MSG_task_destroy(task);
86   return 0;
87 }
88
89 int gras_bench_once_begin(const char *location,int line)
90 {
91   double *ir = NULL;
92   xbt_assert0(!benchmarking,"Already benchmarking");
93   benchmarking = 1;
94
95   if (!locbuf || locbufsize < strlen(location) + 64) {
96      locbufsize = strlen(location) + 64;
97      locbuf = xbt_realloc(locbuf,locbufsize);
98   }
99   sprintf(locbuf,"%s:%d",location, line);
100    
101   xbt_dict_get(benchmark_set, locbuf, (void *) &ir);
102   if(!ir) {
103     DEBUG1("%s",locbuf); 
104     duration = 1;
105     xbt_os_timer_start(timer);
106     return 1;
107   } else {
108     duration = -1.0;
109     return 0;
110   }
111 }
112
113 int gras_bench_once_end(void)
114 {
115   m_task_t task = NULL;
116
117   xbt_assert0(benchmarking,"Not benchmarking yet");
118   benchmarking = 0;
119   if(duration>0) {
120     xbt_os_timer_stop(timer);
121     duration = xbt_os_timer_elapsed(timer);
122     store_in_dict(benchmark_set, locbuf, duration);
123   } else {
124     duration = get_from_dict(benchmark_set,locbuf);
125   }
126   DEBUG2("Simulate the run of a task of %f sec for %s",duration,locbuf);
127   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
128   MSG_task_execute(task);
129   MSG_task_destroy(task);
130   return 0;
131 }
132
133
134 /*** Conditional execution support ***/
135
136 int gras_if_RL(void) {
137    return 0;
138 }
139
140 int gras_if_SG(void) {
141    return 1;
142 }
143
144