Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ONGOING work on exceptions plus minor cleanups.
[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 #include "xbt/ex.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(emul,gras,"Emulation support");
19
20 /*** Timing macros ***/
21 static xbt_os_timer_t timer;
22 static int benchmarking = 0;
23 static xbt_dict_t benchmark_set = NULL;
24 static double reference = .00523066250047108838; /* FIXME: we should benchmark host machine to set this */
25 static double duration = 0.0;
26
27 static char* locbuf = NULL;
28 static int locbufsize;
29
30 void gras_emul_init(void)
31 {
32   if(!benchmark_set) {
33     benchmark_set = xbt_dict_new();
34     timer = xbt_os_timer_new();
35   }
36 }
37
38 void gras_emul_exit(void) {
39   if (locbuf) free(locbuf);
40   xbt_dict_free(&benchmark_set);
41   xbt_os_timer_free(timer);
42 }
43
44
45 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
46 {
47   double *ir;
48   xbt_ex_t e;
49
50   TRY {
51     ir = xbt_dict_get(dict, key);
52   } CATCH(e) {
53     if (e.category == mismatch_error) {
54       ir = NULL;
55       xbt_ex_free(e);
56     } else {
57       RETHROW;
58     }
59   }
60   if (!ir) {
61     ir = xbt_new0(double,1);
62     xbt_dict_set(dict, key, ir, free);
63   }
64   *ir = value;
65 }
66
67 static double get_from_dict(xbt_dict_t dict, const char *key) {
68   double *ir = xbt_dict_get(dict, key);
69
70   return *ir;
71 }
72
73 int gras_bench_always_begin(const char *location,int line)
74 {
75   xbt_assert0(!benchmarking,"Already benchmarking");
76   benchmarking = 1;
77
78   if (!timer)
79   xbt_os_timer_start(timer);
80   return 0;
81 }
82
83 int gras_bench_always_end(void)
84 {
85   m_task_t task = NULL;
86   
87   xbt_assert0(benchmarking,"Not benchmarking yet");
88   benchmarking = 0;
89   xbt_os_timer_stop(timer);
90   duration = xbt_os_timer_elapsed(timer);
91   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
92   MSG_task_execute(task);
93   MSG_task_destroy(task);
94   return 0;
95 }
96
97 int gras_bench_once_begin(const char *location,int line) { 
98   double *ir = NULL;
99   xbt_ex_t e;
100   xbt_assert0(!benchmarking,"Already benchmarking");
101   benchmarking = 1;
102
103   if (!locbuf || locbufsize < strlen(location) + 64) {
104      locbufsize = strlen(location) + 64;
105      locbuf = xbt_realloc(locbuf,locbufsize);
106   }
107   sprintf(locbuf,"%s:%d",location, line);
108    
109   TRY {
110     ir = xbt_dict_get(benchmark_set, locbuf);
111   } CATCH(e) {
112     if (e.category == mismatch_error) {
113       xbt_ex_free(e);
114       ir = NULL;
115     } else {
116       RETHROW;
117     }
118   }
119   if(!ir) {
120     DEBUG1("%s",locbuf); 
121     duration = 1;
122     xbt_os_timer_start(timer);
123     return 1;
124   } else {
125     duration = -1.0;
126     return 0;
127   }
128 }
129
130 int gras_bench_once_end(void)
131 {
132   m_task_t task = NULL;
133
134   xbt_assert0(benchmarking,"Not benchmarking yet");
135   benchmarking = 0;
136   if(duration>0) {
137     xbt_os_timer_stop(timer);
138     duration = xbt_os_timer_elapsed(timer);
139     store_in_dict(benchmark_set, locbuf, duration);
140   } else {
141     duration = get_from_dict(benchmark_set,locbuf);
142   }
143   DEBUG2("Simulate the run of a task of %f sec for %s",duration,locbuf);
144   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
145   MSG_task_execute(task);
146   MSG_task_destroy(task);
147   return 0;
148 }
149
150
151 /*** Conditional execution support ***/
152
153 int gras_if_RL(void) {
154    return 0;
155 }
156
157 int gras_if_SG(void) {
158    return 1;
159 }
160
161