Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simdata cleanup from smx_action_t. Now all SIMIX's data structures [Cristian]
[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
22 /*** Timing macros ***/
23 static xbt_os_timer_t timer;
24 static int benchmarking = 0;
25 static xbt_dict_t benchmark_set = NULL;
26 static double reference = .00000000523066250047108838;  /* FIXME: we should benchmark host machine to set this; unit=s/flop */
27 static double duration = 0.0;
28
29 static char *locbuf = NULL;
30 static unsigned int locbufsize;
31
32 void gras_emul_init(void)
33 {
34   if (!benchmark_set) {
35     benchmark_set = xbt_dict_new();
36     timer = xbt_os_timer_new();
37   }
38 }
39
40 void gras_emul_exit(void)
41 {
42   if (locbuf)
43     free(locbuf);
44   xbt_dict_free(&benchmark_set);
45   xbt_os_timer_free(timer);
46 }
47
48
49 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
50 {
51   double *ir;
52
53   ir = xbt_dict_get_or_null(dict, key);
54   if (!ir) {
55     ir = xbt_new0(double, 1);
56     xbt_dict_set(dict, key, ir, xbt_free_f);
57   }
58   *ir = value;
59 }
60
61 static double get_from_dict(xbt_dict_t dict, const char *key)
62 {
63   double *ir = xbt_dict_get(dict, key);
64
65   return *ir;
66 }
67
68 int gras_bench_always_begin(const char *location, int line)
69 {
70   xbt_assert0(!benchmarking, "Already benchmarking");
71   benchmarking = 1;
72
73   if (!timer)
74     xbt_os_timer_start(timer);
75   return 0;
76 }
77
78 int gras_bench_always_end(void)
79 {
80   smx_action_t act;
81   smx_cond_t cond;
82   smx_mutex_t mutex;
83
84   xbt_assert0(benchmarking, "Not benchmarking yet");
85   benchmarking = 0;
86   xbt_os_timer_stop(timer);
87   duration = xbt_os_timer_elapsed(timer);
88
89   cond = SIMIX_cond_init();
90   mutex = SIMIX_mutex_init();
91
92   SIMIX_mutex_lock(mutex);
93   act =
94     SIMIX_action_execute(SIMIX_host_self(), "task", (duration) / reference);
95
96   SIMIX_register_action_to_condition(act, cond);
97   SIMIX_cond_wait(cond, mutex);
98   SIMIX_unregister_action_to_condition(act, cond);
99
100   SIMIX_action_destroy(act);
101   SIMIX_mutex_unlock(mutex);
102
103   SIMIX_cond_destroy(cond);
104   SIMIX_mutex_destroy(mutex);
105
106   return 0;
107 }
108
109 int gras_bench_once_begin(const char *location, int line)
110 {
111   double *ir = NULL;
112   xbt_assert0(!benchmarking, "Already benchmarking");
113   benchmarking = 1;
114
115   if (!locbuf || locbufsize < strlen(location) + 64) {
116     locbufsize = strlen(location) + 64;
117     locbuf = xbt_realloc(locbuf, locbufsize);
118   }
119   sprintf(locbuf, "%s:%d", location, line);
120
121   ir = xbt_dict_get_or_null(benchmark_set, locbuf);
122   if (!ir) {
123     DEBUG1("%s", locbuf);
124     duration = 1;
125     xbt_os_timer_start(timer);
126     return 1;
127   } else {
128     duration = -1.0;
129     return 0;
130   }
131 }
132
133 int gras_bench_once_end(void)
134 {
135   smx_action_t act;
136   smx_cond_t cond;
137   smx_mutex_t mutex;
138
139   xbt_assert0(benchmarking, "Not benchmarking yet");
140   benchmarking = 0;
141   if (duration > 0) {
142     xbt_os_timer_stop(timer);
143     duration = xbt_os_timer_elapsed(timer);
144     store_in_dict(benchmark_set, locbuf, duration);
145   } else {
146     duration = get_from_dict(benchmark_set, locbuf);
147   }
148   DEBUG2("Simulate the run of a task of %f sec for %s", duration, locbuf);
149   cond = SIMIX_cond_init();
150   mutex = SIMIX_mutex_init();
151
152   SIMIX_mutex_lock(mutex);
153   act =
154     SIMIX_action_execute(SIMIX_host_self(), "task", (duration) / reference);
155
156   SIMIX_register_action_to_condition(act, cond);
157   SIMIX_cond_wait(cond, mutex);
158   SIMIX_unregister_action_to_condition(act, cond);
159
160   SIMIX_action_destroy(act);
161   SIMIX_mutex_unlock(mutex);
162
163   SIMIX_cond_destroy(cond);
164   SIMIX_mutex_destroy(mutex);
165   return 0;
166 }
167
168
169 /*** Conditional execution support ***/
170
171 int gras_if_RL(void)
172 {
173   return 0;
174 }
175
176 int gras_if_SG(void)
177 {
178   return 1;
179 }