1 /* sg_emul - Emulation support (simulation) */
3 /* Copyright (c) 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <stdio.h> /* sprintf */
10 #include "gras/emul.h"
11 #include "gras/Virtu/virtu_sg.h"
12 #include "gras_modinter.h"
14 #include "xbt/xbt_os_time.h" /* timers */
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_emul, gras_virtu,
21 void gras_cpu_burn(double flops)
23 smx_action_t execution;
26 execution = simcall_host_execute("task", SIMIX_host_self(), flops, 1);
27 simcall_host_execution_wait(execution);
31 /*** Timing macros ***/
32 static xbt_os_timer_t timer;
33 static int benchmarking = 0;
34 static xbt_dict_t benchmark_set = NULL;
35 static double reference = .00000000523066250047108838; /* FIXME: we should benchmark host machine to set this; unit=s/flop */
36 static double duration = 0.0;
38 static char *locbuf = NULL;
39 static unsigned int locbufsize;
41 void gras_emul_init(void)
44 benchmark_set = xbt_dict_new_homogeneous(xbt_free_f);
45 timer = xbt_os_timer_new();
49 void gras_emul_exit(void)
52 xbt_dict_free(&benchmark_set);
53 xbt_os_timer_free(timer);
57 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
61 ir = xbt_dict_get_or_null(dict, key);
63 ir = xbt_new0(double, 1);
64 xbt_dict_set(dict, key, ir, NULL);
69 static double get_from_dict(xbt_dict_t dict, const char *key)
71 double *ir = xbt_dict_get(dict, key);
76 int gras_bench_always_begin(const char *location, int line)
78 xbt_assert(!benchmarking, "Already benchmarking");
82 xbt_os_timer_start(timer);
86 int gras_bench_always_end(void)
88 xbt_assert(benchmarking, "Not benchmarking yet");
90 xbt_os_timer_stop(timer);
91 duration = xbt_os_timer_elapsed(timer);
93 gras_cpu_burn(duration / reference);
98 int gras_bench_once_begin(const char *location, int line)
101 xbt_assert(!benchmarking, "Already benchmarking");
104 if (!locbuf || locbufsize < strlen(location) + 64) {
105 locbufsize = strlen(location) + 64;
106 locbuf = xbt_realloc(locbuf, locbufsize);
108 sprintf(locbuf, "%s:%d", location, line);
110 ir = xbt_dict_get_or_null(benchmark_set, locbuf);
112 XBT_DEBUG("%s", locbuf);
114 xbt_os_timer_start(timer);
122 int gras_bench_once_end(void)
124 xbt_assert(benchmarking, "Not benchmarking yet");
127 xbt_os_timer_stop(timer);
128 duration = xbt_os_timer_elapsed(timer);
129 store_in_dict(benchmark_set, locbuf, duration);
131 duration = get_from_dict(benchmark_set, locbuf);
133 XBT_DEBUG("Simulate the run of a task of %f sec for %s", duration, locbuf);
134 gras_cpu_burn(duration / reference);
139 /*** Conditional execution support ***/