Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use xbt_os_time
[simgrid.git] / src / gras / Virtu / sg_chrono.c
1 /*      $Id$     */
2
3 /* sg_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 timer = 0.0;
19 static int benchmarking = 0;
20 static xbt_dict_t benchmark_set = NULL;
21 static double reference = .00523066250047108838;
22 static double duration = 0.0;
23 static const char* __location__ = NULL;
24
25 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
26 {
27   double *ir = NULL;
28
29   xbt_dict_get(dict, key, (void *) &ir);
30   if (!ir) {
31     ir = calloc(1, sizeof(double));
32     xbt_dict_set(dict, key, ir, free);
33   }
34   *ir = value;
35 }
36
37 static double get_from_dict(xbt_dict_t dict, const char *key)
38 {
39   double *ir = NULL;
40
41   xbt_dict_get(dict, key, (void *) &ir);
42
43   return *ir;
44 }
45
46 int gras_bench_always_begin(const char *location, int line)
47 {
48   xbt_assert0(!benchmarking,"Already benchmarking");
49   benchmarking = 1;
50
51   timer = xbt_os_time();
52   return 0;
53 }
54
55 int gras_bench_always_end(void)
56 {
57   m_task_t task = NULL;
58   
59   xbt_assert0(benchmarking,"Not benchmarking yet");
60   benchmarking = 0;
61   duration = xbt_os_time()-timer;
62   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
63   MSG_task_execute(task);
64   /*   printf("---> %lg <--- \n", xbt_os_time()-timer); */
65   MSG_task_destroy(task);
66   return 0;
67 }
68
69 int gras_bench_once_begin(const char *location, int line)
70 {
71   double *ir = NULL;
72   xbt_assert0(!benchmarking,"Already benchmarking");
73   benchmarking = 1;
74
75   __location__=location;
76   xbt_dict_get(benchmark_set, __location__, (void *) &ir);
77   if(!ir) {
78 /*     printf("%s:%d\n",location,line); */
79     duration = xbt_os_time();
80     return 1;
81   } else {
82     duration = -1.0;
83     return 0;
84   }
85 }
86
87 int gras_bench_once_end(void)
88 {
89   m_task_t task = NULL;
90
91   xbt_assert0(benchmarking,"Not benchmarking yet");
92   benchmarking = 0;
93   if(duration>0) {
94     duration = xbt_os_time()-duration;
95     store_in_dict(benchmark_set, __location__, duration);
96   } else {
97     duration = get_from_dict(benchmark_set,__location__);
98   }
99   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
100   MSG_task_execute(task);
101   MSG_task_destroy(task);
102   return 0;
103 }
104
105 void gras_chrono_init(void)
106 {
107   if(!benchmark_set)
108     benchmark_set = xbt_dict_new();
109 }