Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Try to fix #8852 (SimGrid won't compile on Ubuntu 9.10) by betterly checking my argum...
[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 <stdio.h>              /* sprintf */
11 #include "gras/emul.h"
12 #include "gras/Virtu/virtu_sg.h"
13 #include "gras_modinter.h"
14
15 #include "xbt/xbt_os_time.h"    /* timers */
16 #include "xbt/dict.h"
17 #include "xbt/ex.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_emul, gras_virtu,
20                                 "Emulation support");
21 /*** CPU burning */
22 void gras_cpu_burn(double flops) {
23   smx_action_t act;
24   smx_cond_t cond;
25   smx_mutex_t mutex;
26
27   if (flops>0) {
28     cond = SIMIX_cond_init();
29     mutex = SIMIX_mutex_init();
30
31     SIMIX_mutex_lock(mutex);
32     act =
33         SIMIX_action_execute(SIMIX_host_self(), "task", flops);
34
35     SIMIX_register_action_to_condition(act, cond);
36     SIMIX_cond_wait(cond, mutex);
37     SIMIX_unregister_action_to_condition(act, cond);
38
39     SIMIX_action_destroy(act);
40     SIMIX_mutex_unlock(mutex);
41
42     SIMIX_cond_destroy(cond);
43     SIMIX_mutex_destroy(mutex);
44   }
45 }
46 /*** Timing macros ***/
47 static xbt_os_timer_t timer;
48 static int benchmarking = 0;
49 static xbt_dict_t benchmark_set = NULL;
50 static double reference = .00000000523066250047108838;  /* FIXME: we should benchmark host machine to set this; unit=s/flop */
51 static double duration = 0.0;
52
53 static char *locbuf = NULL;
54 static unsigned int locbufsize;
55
56 void gras_emul_init(void)
57 {
58   if (!benchmark_set) {
59     benchmark_set = xbt_dict_new();
60     timer = xbt_os_timer_new();
61   }
62 }
63
64 void gras_emul_exit(void)
65 {
66   if (locbuf)
67     free(locbuf);
68   xbt_dict_free(&benchmark_set);
69   xbt_os_timer_free(timer);
70 }
71
72
73 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
74 {
75   double *ir;
76
77   ir = xbt_dict_get_or_null(dict, key);
78   if (!ir) {
79     ir = xbt_new0(double, 1);
80     xbt_dict_set(dict, key, ir, xbt_free_f);
81   }
82   *ir = value;
83 }
84
85 static double get_from_dict(xbt_dict_t dict, const char *key)
86 {
87   double *ir = xbt_dict_get(dict, key);
88
89   return *ir;
90 }
91
92 int gras_bench_always_begin(const char *location, int line)
93 {
94   xbt_assert0(!benchmarking, "Already benchmarking");
95   benchmarking = 1;
96
97   if (!timer)
98     xbt_os_timer_start(timer);
99   return 0;
100 }
101
102 int gras_bench_always_end(void) {
103   xbt_assert0(benchmarking, "Not benchmarking yet");
104   benchmarking = 0;
105   xbt_os_timer_stop(timer);
106   duration = xbt_os_timer_elapsed(timer);
107
108   gras_cpu_burn(duration/reference);
109
110   return 0;
111 }
112
113 int gras_bench_once_begin(const char *location, int line)
114 {
115   double *ir = NULL;
116   xbt_assert0(!benchmarking, "Already benchmarking");
117   benchmarking = 1;
118
119   if (!locbuf || locbufsize < strlen(location) + 64) {
120     locbufsize = strlen(location) + 64;
121     locbuf = xbt_realloc(locbuf, locbufsize);
122   }
123   sprintf(locbuf, "%s:%d", location, line);
124
125   ir = xbt_dict_get_or_null(benchmark_set, locbuf);
126   if (!ir) {
127     DEBUG1("%s", locbuf);
128     duration = 1;
129     xbt_os_timer_start(timer);
130     return 1;
131   } else {
132     duration = -1.0;
133     return 0;
134   }
135 }
136
137 int gras_bench_once_end(void) {
138   xbt_assert0(benchmarking, "Not benchmarking yet");
139   benchmarking = 0;
140   if (duration > 0) {
141     xbt_os_timer_stop(timer);
142     duration = xbt_os_timer_elapsed(timer);
143     store_in_dict(benchmark_set, locbuf, duration);
144   } else {
145     duration = get_from_dict(benchmark_set, locbuf);
146   }
147   DEBUG2("Simulate the run of a task of %f sec for %s", duration, locbuf);
148   gras_cpu_burn(duration/reference);
149   return 0;
150 }
151
152
153 /*** Conditional execution support ***/
154
155 int gras_if_RL(void)
156 {
157   return 0;
158 }
159
160 int gras_if_SG(void)
161 {
162   return 1;
163 }