3 /* 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"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(chrono,gras,"Benchmarking used code");
18 static double sys_time(void) {
19 #ifdef HAVE_GETTIMEOFDAY
22 gettimeofday(&tv, NULL);
24 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
27 return (double)(time(NULL));
28 #endif /* HAVE_GETTIMEOFDAY? */
32 static double timer = 0.0;
33 static int benchmarking = 0;
34 static xbt_dict_t benchmark_set = NULL;
35 static double reference = .01019;
36 static double duration = 0.0;
37 static const char* __location__ = NULL;
39 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
43 xbt_dict_get(dict, key, (void *) &ir);
45 ir = calloc(1, sizeof(double));
46 xbt_dict_set(dict, key, ir, free);
51 static double get_from_dict(xbt_dict_t dict, const char *key)
55 xbt_dict_get(dict, key, (void *) &ir);
60 int gras_bench_always_begin(const char *location, int line)
62 xbt_assert0(!benchmarking,"Already benchmarking");
69 int gras_bench_always_end(void)
73 xbt_assert0(benchmarking,"Not benchmarking yet");
75 duration = sys_time()-timer;
76 task = MSG_task_create("task", (duration)/reference, 0 , NULL);
77 MSG_task_execute(task);
78 /* printf("---> %lg <--- \n", sys_time()-timer); */
79 MSG_task_destroy(task);
83 int gras_bench_once_begin(const char *location, int line)
86 xbt_assert0(!benchmarking,"Already benchmarking");
89 __location__=location;
90 xbt_dict_get(benchmark_set, __location__, (void *) &ir);
92 /* printf("%s:%d\n",location,line); */
93 duration = sys_time();
101 int gras_bench_once_end(void)
103 m_task_t task = NULL;
105 xbt_assert0(benchmarking,"Not benchmarking yet");
108 duration = sys_time()-duration;
109 store_in_dict(benchmark_set, __location__, duration);
111 duration = get_from_dict(benchmark_set,__location__);
113 task = MSG_task_create("task", (duration)/reference, 0 , NULL);
114 MSG_task_execute(task);
115 MSG_task_destroy(task);
119 void gras_chrono_init(void)
122 benchmark_set = xbt_dict_new();