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 void gras_cpu_burn(double flops) {
28 cond = SIMIX_cond_init();
29 mutex = SIMIX_mutex_init();
31 SIMIX_mutex_lock(mutex);
33 SIMIX_action_execute(SIMIX_host_self(), "task", flops);
35 SIMIX_register_action_to_condition(act, cond);
36 SIMIX_cond_wait(cond, mutex);
37 SIMIX_unregister_action_to_condition(act, cond);
39 SIMIX_action_destroy(act);
40 SIMIX_mutex_unlock(mutex);
42 SIMIX_cond_destroy(cond);
43 SIMIX_mutex_destroy(mutex);
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;
53 static char *locbuf = NULL;
54 static unsigned int locbufsize;
56 void gras_emul_init(void)
59 benchmark_set = xbt_dict_new();
60 timer = xbt_os_timer_new();
64 void gras_emul_exit(void)
68 xbt_dict_free(&benchmark_set);
69 xbt_os_timer_free(timer);
73 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
77 ir = xbt_dict_get_or_null(dict, key);
79 ir = xbt_new0(double, 1);
80 xbt_dict_set(dict, key, ir, xbt_free_f);
85 static double get_from_dict(xbt_dict_t dict, const char *key)
87 double *ir = xbt_dict_get(dict, key);
92 int gras_bench_always_begin(const char *location, int line)
94 xbt_assert0(!benchmarking, "Already benchmarking");
98 xbt_os_timer_start(timer);
102 int gras_bench_always_end(void) {
103 xbt_assert0(benchmarking, "Not benchmarking yet");
105 xbt_os_timer_stop(timer);
106 duration = xbt_os_timer_elapsed(timer);
108 gras_cpu_burn(duration/reference);
113 int gras_bench_once_begin(const char *location, int line)
116 xbt_assert0(!benchmarking, "Already benchmarking");
119 if (!locbuf || locbufsize < strlen(location) + 64) {
120 locbufsize = strlen(location) + 64;
121 locbuf = xbt_realloc(locbuf, locbufsize);
123 sprintf(locbuf, "%s:%d", location, line);
125 ir = xbt_dict_get_or_null(benchmark_set, locbuf);
127 DEBUG1("%s", locbuf);
129 xbt_os_timer_start(timer);
137 int gras_bench_once_end(void) {
138 xbt_assert0(benchmarking, "Not benchmarking yet");
141 xbt_os_timer_stop(timer);
142 duration = xbt_os_timer_elapsed(timer);
143 store_in_dict(benchmark_set, locbuf, duration);
145 duration = get_from_dict(benchmark_set, locbuf);
147 DEBUG2("Simulate the run of a task of %f sec for %s", duration, locbuf);
148 gras_cpu_burn(duration/reference);
153 /*** Conditional execution support ***/