Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into async-wait
[simgrid.git] / contrib / benchmarking_code_block / bench.h
1 /* Copyright (c) 2013-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* Copy to src/include/xbt/ folder  */
8
9 /* Benchmarking a code block */
10
11 /* Use functions from bench.h to benchmark execution time of the desired block,
12  * then Rhist.R script to read all timings and produce histograms
13  * and finally inject.h to inject values instead of executing block*/
14
15 #ifndef __BENCH_H__
16 #define __BENCH_H__
17
18 #include <time.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 /* Structure that has all benchmarking information for the block*/
23 typedef struct bench {
24         struct timespec start_time;
25         struct timespec end_time;
26         int benchmarking;
27         char block_id[256];
28         char suffix[100];
29         FILE* output;
30 }*bench_t;
31
32 extern bench_t get_mybench(void);
33 typedef bench_t (*get_bench_func_t)(void);
34
35 /* In order to divide nanoseconds and get result in seconds */
36 #define BILLION  1000000000L
37
38 /* Macros for benchmarking */
39 #define BENCH_BLOCK(block_id) for(bench_begin_block();bench_end_block(block_id);)
40 #define BENCH_EXTEND(block_id) xbt_bench_extend(block_id)
41
42 static inline void xbt_bench_init(char *tracefile);
43 static inline void bench_init_starpu(char *tracefile, bench_t *bench);
44
45 static inline void bench_begin_block();
46 static inline int bench_end_block(char* block_id);
47
48 static inline void xbt_bench_begin(char* block_id);
49 static inline int xbt_bench_end(char* block_id);
50
51 static inline void xbt_bench_extend(char* block_id);
52
53 /* Additional functions in order to manipulate with struct timespec */
54 static inline void xbt_diff_time(struct timespec* start, struct timespec* end, struct timespec* result_time);
55 static inline double xbt_get_time(struct timespec* timer);
56
57 /* Initializing SMPI benchmarking */
58 static inline void xbt_bench_init(char *tracefile)
59 {
60         bench_t mybench = get_mybench();
61         mybench->output = fopen(tracefile, "a+");
62         if (mybench->output == NULL)
63                 printf("Error while opening the tracefile");
64
65 }
66
67 /* Initializing StarPU benchmarking */
68 static inline void bench_init_starpu(char *tracefile, bench_t *bench)
69 {
70   *bench = (bench_t) malloc(sizeof(**bench));
71   bench_t mybench = *bench;
72   mybench->output = fopen(tracefile, "a+");
73   if (mybench->output == NULL)
74                 printf("Error while opening the tracefile");
75
76 }
77
78 /* Start benchmarking using macros */
79 static inline void bench_begin_block()
80 {
81         bench_t mybench = get_mybench();
82         clock_gettime(CLOCK_REALTIME, &mybench->start_time);
83         mybench->benchmarking = 1; // Only benchmarking once
84 }
85
86 /* End benchmarking using macros */
87 static inline int bench_end_block(char* block_id)
88 {
89         bench_t mybench = get_mybench();
90         if (mybench->benchmarking > 0)
91         {
92                 mybench->benchmarking--;
93                 return 1;
94         }
95         else
96         {
97                 clock_gettime(CLOCK_REALTIME, &mybench->end_time);
98                 struct timespec interval;
99                 xbt_diff_time(&mybench->start_time, &mybench->end_time, &interval);
100                 fprintf(mybench->output, "%s %f %f %f\n", block_id, xbt_get_time(&mybench->start_time), xbt_get_time(&mybench->end_time), xbt_get_time(&interval));
101                 return 0;
102         }
103 }
104
105 /* Start SMPI benchmarking */
106 static inline void xbt_bench_begin(char* block_id)
107 {
108         bench_t mybench = get_mybench();
109         if(block_id != NULL)
110                 strcpy (mybench->block_id, block_id);
111         else
112                 strcpy (mybench->block_id, "");
113         clock_gettime(CLOCK_REALTIME, &mybench->start_time);
114         mybench->benchmarking = 1; // Only benchmarking once
115 }
116
117 /* End SMPI benchmarking */
118 static inline int xbt_bench_end(char* block_id)
119 {
120         bench_t mybench = get_mybench();
121
122         clock_gettime(CLOCK_REALTIME, &mybench->end_time);
123         struct timespec interval;
124         xbt_diff_time(&mybench->start_time, &mybench->end_time, &interval);
125
126         if(mybench->suffix != NULL)
127         {
128                 strcat (mybench->block_id, mybench->suffix);
129                 strcpy (mybench->suffix, "");
130         }
131         if(block_id != NULL)
132                 strcat (mybench->block_id, block_id);
133         if(mybench->block_id == NULL)
134                 strcat (mybench->block_id, "NONAME");
135
136         fprintf(mybench->output, "%s %f %f %f\n", mybench->block_id, xbt_get_time(&mybench->start_time), xbt_get_time(&mybench->end_time), xbt_get_time(&interval));
137         return 0;
138 }
139
140 /* Extending the block_id name*/
141 static inline void xbt_bench_extend(char* block_id)
142 {
143         bench_t mybench = get_mybench();
144         strcpy (mybench->suffix, block_id);
145 }
146
147 /* Calculating time difference */
148 static inline void xbt_diff_time(struct timespec* start, struct timespec* end,  struct timespec* result_time)
149 {
150         if ((end->tv_nsec - start->tv_nsec) < 0)
151         {
152                 result_time->tv_sec = end->tv_sec - start->tv_sec - 1;
153                 result_time->tv_nsec = (double) BILLION + end->tv_nsec - start->tv_nsec;
154         }
155         else
156         {
157                 result_time->tv_sec = end->tv_sec - start->tv_sec;
158                 result_time->tv_nsec = end->tv_nsec - start->tv_nsec;
159         }
160 }
161
162 /* Printing time in "double" format */
163 static inline double xbt_get_time(struct timespec* timer)
164 {
165         return timer->tv_sec + (double) (timer->tv_nsec / (double) BILLION);
166 }
167
168 #endif //__BENCH_H__