3 /* sg_emul - Emulation support (simulation) */
5 /* Copyright (c) 2003-5 Arnaud Legrand, Martin Quinson. All rights reserved.*/
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. */
10 #include <stdio.h> /* sprintf */
11 #include "gras/emul.h"
12 #include "gras/Virtu/virtu_sg.h"
13 #include "gras_modinter.h"
15 #include "xbt/xbt_os_time.h" /* timers */
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_emul, gras_virtu,
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;
29 static char *locbuf = NULL;
30 static unsigned int locbufsize;
32 void gras_emul_init(void)
35 benchmark_set = xbt_dict_new();
36 timer = xbt_os_timer_new();
40 void gras_emul_exit(void)
44 xbt_dict_free(&benchmark_set);
45 xbt_os_timer_free(timer);
49 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
53 ir = xbt_dict_get_or_null(dict, key);
55 ir = xbt_new0(double, 1);
56 xbt_dict_set(dict, key, ir, xbt_free_f);
61 static double get_from_dict(xbt_dict_t dict, const char *key)
63 double *ir = xbt_dict_get(dict, key);
68 int gras_bench_always_begin(const char *location, int line)
70 xbt_assert0(!benchmarking, "Already benchmarking");
74 xbt_os_timer_start(timer);
78 int gras_bench_always_end(void)
84 xbt_assert0(benchmarking, "Not benchmarking yet");
86 xbt_os_timer_stop(timer);
87 duration = xbt_os_timer_elapsed(timer);
89 cond = SIMIX_cond_init();
90 mutex = SIMIX_mutex_init();
92 SIMIX_mutex_lock(mutex);
94 SIMIX_action_execute(SIMIX_host_self(), "task", (duration) / reference);
96 SIMIX_register_action_to_condition(act, cond);
97 SIMIX_cond_wait(cond, mutex);
98 SIMIX_unregister_action_to_condition(act, cond);
100 SIMIX_action_destroy(act);
101 SIMIX_mutex_unlock(mutex);
103 SIMIX_cond_destroy(cond);
104 SIMIX_mutex_destroy(mutex);
109 int gras_bench_once_begin(const char *location, int line)
112 xbt_assert0(!benchmarking, "Already benchmarking");
115 if (!locbuf || locbufsize < strlen(location) + 64) {
116 locbufsize = strlen(location) + 64;
117 locbuf = xbt_realloc(locbuf, locbufsize);
119 sprintf(locbuf, "%s:%d", location, line);
121 ir = xbt_dict_get_or_null(benchmark_set, locbuf);
123 DEBUG1("%s", locbuf);
125 xbt_os_timer_start(timer);
133 int gras_bench_once_end(void)
139 xbt_assert0(benchmarking, "Not benchmarking yet");
142 xbt_os_timer_stop(timer);
143 duration = xbt_os_timer_elapsed(timer);
144 store_in_dict(benchmark_set, locbuf, duration);
146 duration = get_from_dict(benchmark_set, locbuf);
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();
152 SIMIX_mutex_lock(mutex);
154 SIMIX_action_execute(SIMIX_host_self(), "task", (duration) / reference);
156 SIMIX_register_action_to_condition(act, cond);
157 SIMIX_cond_wait(cond, mutex);
158 SIMIX_unregister_action_to_condition(act, cond);
160 SIMIX_action_destroy(act);
161 SIMIX_mutex_unlock(mutex);
163 SIMIX_cond_destroy(cond);
164 SIMIX_mutex_destroy(mutex);
169 /*** Conditional execution support ***/