Logo AND Algorithmique Numérique Distribuée

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