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 "gras/emul.h"
11 #include "gras/Virtu/virtu_sg.h"
12 #include "gras_modinter.h"
14 #include "xbt/xbt_portability.h" /* timers */
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(emul,gras,"Emulation support");
19 /*** Timing macros ***/
20 static xbt_os_timer_t timer;
21 static int benchmarking = 0;
22 static xbt_dict_t benchmark_set = NULL;
23 static double reference = .00523066250047108838; /* FIXME: we should benchmark host machine to set this */
24 static double duration = 0.0;
26 static char* locbuf = NULL;
27 static int locbufsize;
29 void gras_emul_init(void)
32 benchmark_set = xbt_dict_new();
33 timer = xbt_os_timer_new();
37 void gras_emul_exit(void) {
38 if (locbuf) free(locbuf);
39 xbt_dict_free(&benchmark_set);
40 xbt_os_timer_free(timer);
44 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
48 xbt_dict_get(dict, key, (void *) &ir);
50 ir = xbt_new0(double,1);
51 xbt_dict_set(dict, key, ir, free);
56 static double get_from_dict(xbt_dict_t dict, const char *key)
60 xbt_dict_get(dict, key, (void *) &ir);
65 int gras_bench_always_begin(const char *location,int line)
67 xbt_assert0(!benchmarking,"Already benchmarking");
71 xbt_os_timer_start(timer);
75 int gras_bench_always_end(void)
79 xbt_assert0(benchmarking,"Not benchmarking yet");
81 xbt_os_timer_stop(timer);
82 duration = xbt_os_timer_elapsed(timer);
83 task = MSG_task_create("task", (duration)/reference, 0 , NULL);
84 MSG_task_execute(task);
85 MSG_task_destroy(task);
89 int gras_bench_once_begin(const char *location,int line)
92 xbt_assert0(!benchmarking,"Already benchmarking");
95 if (!locbuf || locbufsize < strlen(location) + 64) {
96 locbufsize = strlen(location) + 64;
97 locbuf = xbt_realloc(locbuf,locbufsize);
99 sprintf(locbuf,"%s:%d",location, line);
101 xbt_dict_get(benchmark_set, locbuf, (void *) &ir);
105 xbt_os_timer_start(timer);
113 int gras_bench_once_end(void)
115 m_task_t task = NULL;
117 xbt_assert0(benchmarking,"Not benchmarking yet");
120 xbt_os_timer_stop(timer);
121 duration = xbt_os_timer_elapsed(timer);
122 store_in_dict(benchmark_set, locbuf, duration);
124 duration = get_from_dict(benchmark_set,locbuf);
126 DEBUG2("Simulate the run of a task of %f sec for %s",duration,locbuf);
127 task = MSG_task_create("task", (duration)/reference, 0 , NULL);
128 MSG_task_execute(task);
129 MSG_task_destroy(task);
134 /*** Conditional execution support ***/
136 int gras_if_RL(void) {
140 int gras_if_SG(void) {