3 /* sg_chrono.c - code benchmarking for emulation */
5 /* Copyright (c) 2005 Martin Quinson, Arnaud Legrand. 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 "xbt/sysdep.h"
12 #include "gras/chrono.h"
14 #include "xbt/xbt_portability.h"
16 #include "gras_modinter.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(chrono,gras,"Benchmarking used code");
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 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
33 xbt_dict_get(dict, key, (void *) &ir);
35 ir = xbt_new0(double,1);
36 xbt_dict_set(dict, key, ir, free);
41 static double get_from_dict(xbt_dict_t dict, const char *key)
45 xbt_dict_get(dict, key, (void *) &ir);
50 int gras_bench_always_begin(const char *location,int line)
52 xbt_assert0(!benchmarking,"Already benchmarking");
56 xbt_os_timer_start(timer);
60 int gras_bench_always_end(void)
64 xbt_assert0(benchmarking,"Not benchmarking yet");
66 xbt_os_timer_stop(timer);
67 duration = xbt_os_timer_elapsed(timer);
68 task = MSG_task_create("task", (duration)/reference, 0 , NULL);
69 MSG_task_execute(task);
70 MSG_task_destroy(task);
74 int gras_bench_once_begin(const char *location,int line)
77 xbt_assert0(!benchmarking,"Already benchmarking");
80 if (!locbuf || locbufsize < strlen(location) + 64) {
81 locbufsize = strlen(location) + 64;
82 locbuf = xbt_realloc(locbuf,locbufsize);
84 sprintf(locbuf,"%s:%d",location, line);
86 xbt_dict_get(benchmark_set, locbuf, (void *) &ir);
90 xbt_os_timer_start(timer);
98 int gras_bench_once_end(void)
100 m_task_t task = NULL;
102 xbt_assert0(benchmarking,"Not benchmarking yet");
105 xbt_os_timer_stop(timer);
106 duration = xbt_os_timer_elapsed(timer);
107 store_in_dict(benchmark_set, locbuf, duration);
109 duration = get_from_dict(benchmark_set,locbuf);
111 DEBUG2("Simulate the run of a task of %f sec for %s",duration,locbuf);
112 task = MSG_task_create("task", (duration)/reference, 0 , NULL);
113 MSG_task_execute(task);
114 MSG_task_destroy(task);
118 void gras_chrono_init(void)
121 benchmark_set = xbt_dict_new();
122 timer = xbt_os_timer_new();
126 void gras_chrono_exit(void) {
127 if (locbuf) free(locbuf);
128 xbt_dict_free(&benchmark_set);
129 xbt_os_timer_free(timer);