1 /* Copyright (c) 2013-2022. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /* Copy to src/include/xbt/ folder */
9 /* Benchmarking a code block */
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*/
22 /* Structure that has all benchmarking information for the block*/
23 typedef struct bench {
24 struct timespec start_time;
25 struct timespec end_time;
32 extern bench_t get_mybench(void);
33 typedef bench_t (*get_bench_func_t)(void);
35 /* In order to divide nanoseconds and get result in seconds */
36 #define BILLION 1000000000L
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)
42 static inline void xbt_bench_init(char *tracefile);
43 static inline void bench_init_starpu(char *tracefile, bench_t *bench);
45 static inline void bench_begin_block();
46 static inline int bench_end_block(char* block_id);
48 static inline void xbt_bench_begin(char* block_id);
49 static inline int xbt_bench_end(char* block_id);
51 static inline void xbt_bench_extend(char* block_id);
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);
57 /* Initializing SMPI benchmarking */
58 static inline void xbt_bench_init(char *tracefile)
60 bench_t mybench = get_mybench();
61 mybench->output = fopen(tracefile, "a+");
62 if (mybench->output == NULL)
63 printf("Error while opening the tracefile");
66 /* Initializing StarPU benchmarking */
67 static inline void bench_init_starpu(char *tracefile, bench_t *bench)
69 *bench = (bench_t) malloc(sizeof(**bench));
70 bench_t mybench = *bench;
71 mybench->output = fopen(tracefile, "a+");
72 if (mybench->output == NULL)
73 printf("Error while opening the tracefile");
76 /* Start benchmarking using macros */
77 static inline void bench_begin_block()
79 bench_t mybench = get_mybench();
80 clock_gettime(CLOCK_REALTIME, &mybench->start_time);
81 mybench->benchmarking = 1; // Only benchmarking once
84 /* End benchmarking using macros */
85 static inline int bench_end_block(char* block_id)
87 bench_t mybench = get_mybench();
88 if (mybench->benchmarking > 0)
90 mybench->benchmarking--;
95 clock_gettime(CLOCK_REALTIME, &mybench->end_time);
96 struct timespec interval;
97 xbt_diff_time(&mybench->start_time, &mybench->end_time, &interval);
98 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));
103 /* Start SMPI benchmarking */
104 static inline void xbt_bench_begin(char* block_id)
106 bench_t mybench = get_mybench();
108 strcpy (mybench->block_id, block_id);
110 strcpy (mybench->block_id, "");
111 clock_gettime(CLOCK_REALTIME, &mybench->start_time);
112 mybench->benchmarking = 1; // Only benchmarking once
115 /* End SMPI benchmarking */
116 static inline int xbt_bench_end(char* block_id)
118 bench_t mybench = get_mybench();
120 clock_gettime(CLOCK_REALTIME, &mybench->end_time);
121 struct timespec interval;
122 xbt_diff_time(&mybench->start_time, &mybench->end_time, &interval);
124 if(mybench->suffix != NULL)
126 strcat (mybench->block_id, mybench->suffix);
127 strcpy (mybench->suffix, "");
130 strcat (mybench->block_id, block_id);
131 if(mybench->block_id == NULL)
132 strcat (mybench->block_id, "NONAME");
134 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));
138 /* Extending the block_id name*/
139 static inline void xbt_bench_extend(char* block_id)
141 bench_t mybench = get_mybench();
142 strcpy (mybench->suffix, block_id);
145 /* Calculating time difference */
146 static inline void xbt_diff_time(struct timespec* start, struct timespec* end, struct timespec* result_time)
148 if ((end->tv_nsec - start->tv_nsec) < 0)
150 result_time->tv_sec = end->tv_sec - start->tv_sec - 1;
151 result_time->tv_nsec = (double) BILLION + end->tv_nsec - start->tv_nsec;
155 result_time->tv_sec = end->tv_sec - start->tv_sec;
156 result_time->tv_nsec = end->tv_nsec - start->tv_nsec;
160 /* Printing time in "double" format */
161 static inline double xbt_get_time(struct timespec* timer)
163 return timer->tv_sec + (double) (timer->tv_nsec / (double) BILLION);