Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Functionnal benchmarking macros :
[simgrid.git] / src / gras / chrono.c
1 /*      $Id$     */
2
3 /* chrono.c - code benchmarking for emulation                               */
4
5 /* Copyright (c) 2005 Martin Quinson, Arnaud Legrand. All rights reserved.  */
6
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. */
9
10 #include "xbt/sysdep.h"
11 #include "xbt/dict.h"
12 #include "gras/chrono.h"
13 #include "msg/msg.h"
14 #include "portable.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(chrono,gras,"Benchmarking used code");
17
18 static double sys_time(void) {
19 #ifdef HAVE_GETTIMEOFDAY
20   struct timeval tv;
21
22   gettimeofday(&tv, NULL);
23
24   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
25 #else
26   /* Poor resolution */
27   return (double)(time(NULL)); 
28 #endif /* HAVE_GETTIMEOFDAY? */ 
29         
30 }
31
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;
38
39 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
40 {
41   double *ir = NULL;
42
43   xbt_dict_get(dict, key, (void *) &ir);
44   if (!ir) {
45     ir = calloc(1, sizeof(double));
46     xbt_dict_set(dict, key, ir, free);
47   }
48   *ir = value;
49 }
50
51 static double get_from_dict(xbt_dict_t dict, const char *key)
52 {
53   double *ir = NULL;
54
55   xbt_dict_get(dict, key, (void *) &ir);
56
57   return *ir;
58 }
59
60 int gras_bench_always_begin(const char *location, int line)
61 {
62   xbt_assert0(!benchmarking,"Already benchmarking");
63   benchmarking = 1;
64
65   timer = sys_time();
66   return 0;
67 }
68
69 int gras_bench_always_end(void)
70 {
71   m_task_t task = NULL;
72   
73   xbt_assert0(benchmarking,"Not benchmarking yet");
74   benchmarking = 0;
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);
80   return 0;
81 }
82
83 int gras_bench_once_begin(const char *location, int line)
84 {
85   double *ir = NULL;
86   xbt_assert0(!benchmarking,"Already benchmarking");
87   benchmarking = 1;
88
89   __location__=location;
90   xbt_dict_get(benchmark_set, __location__, (void *) &ir);
91   if(!ir) {
92 /*     printf("%s:%d\n",location,line); */
93     duration = sys_time();
94     return 1;
95   } else {
96     duration = -1.0;
97     return 0;
98   }
99 }
100
101 int gras_bench_once_end(void)
102 {
103   m_task_t task = NULL;
104
105   xbt_assert0(benchmarking,"Not benchmarking yet");
106   benchmarking = 0;
107   if(duration>0) {
108     duration = sys_time()-duration;
109     store_in_dict(benchmark_set, __location__, duration);
110   } else {
111     duration = get_from_dict(benchmark_set,__location__);
112   }
113   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
114   MSG_task_execute(task);
115   MSG_task_destroy(task);
116   return 0;
117 }
118
119 void gras_chrono_init(void)
120 {
121   if(!benchmark_set)
122     benchmark_set = xbt_dict_new();
123 }