1 /* sg_emul - Emulation support (simulation) */
3 /* Copyright (c) 2003-5 Arnaud Legrand, Martin Quinson. All rights reserved.*/
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. */
8 #include <stdio.h> /* sprintf */
10 #include "gras/Virtu/virtu_sg.h"
11 #include "gras_modinter.h"
13 #include "xbt/xbt_os_time.h" /* timers */
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_emul, gras_virtu,
20 void gras_cpu_burn(double flops) {
26 cond = SIMIX_cond_init();
27 mutex = SIMIX_mutex_init();
29 SIMIX_mutex_lock(mutex);
31 SIMIX_action_execute(SIMIX_host_self(), "task", flops);
33 SIMIX_register_action_to_condition(act, cond);
34 SIMIX_cond_wait(cond, mutex);
35 SIMIX_unregister_action_to_condition(act, cond);
37 SIMIX_action_destroy(act);
38 SIMIX_mutex_unlock(mutex);
40 SIMIX_cond_destroy(cond);
41 SIMIX_mutex_destroy(mutex);
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;
51 static char *locbuf = NULL;
52 static unsigned int locbufsize;
54 void gras_emul_init(void)
57 benchmark_set = xbt_dict_new();
58 timer = xbt_os_timer_new();
62 void gras_emul_exit(void)
66 xbt_dict_free(&benchmark_set);
67 xbt_os_timer_free(timer);
71 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
75 ir = xbt_dict_get_or_null(dict, key);
77 ir = xbt_new0(double, 1);
78 xbt_dict_set(dict, key, ir, xbt_free_f);
83 static double get_from_dict(xbt_dict_t dict, const char *key)
85 double *ir = xbt_dict_get(dict, key);
90 int gras_bench_always_begin(const char *location, int line)
92 xbt_assert0(!benchmarking, "Already benchmarking");
96 xbt_os_timer_start(timer);
100 int gras_bench_always_end(void) {
101 xbt_assert0(benchmarking, "Not benchmarking yet");
103 xbt_os_timer_stop(timer);
104 duration = xbt_os_timer_elapsed(timer);
106 gras_cpu_burn(duration/reference);
111 int gras_bench_once_begin(const char *location, int line)
114 xbt_assert0(!benchmarking, "Already benchmarking");
117 if (!locbuf || locbufsize < strlen(location) + 64) {
118 locbufsize = strlen(location) + 64;
119 locbuf = xbt_realloc(locbuf, locbufsize);
121 sprintf(locbuf, "%s:%d", location, line);
123 ir = xbt_dict_get_or_null(benchmark_set, locbuf);
125 DEBUG1("%s", locbuf);
127 xbt_os_timer_start(timer);
135 int gras_bench_once_end(void) {
136 xbt_assert0(benchmarking, "Not benchmarking yet");
139 xbt_os_timer_stop(timer);
140 duration = xbt_os_timer_elapsed(timer);
141 store_in_dict(benchmark_set, locbuf, duration);
143 duration = get_from_dict(benchmark_set, locbuf);
145 DEBUG2("Simulate the run of a task of %f sec for %s", duration, locbuf);
146 gras_cpu_burn(duration/reference);
151 /*** Conditional execution support ***/