Logo AND Algorithmique Numérique Distribuée

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