2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Statistic events.
9 * @author Sebastian Hack
22 #include "stat_timing.h"
30 int (stat_ev_enabled) = 0;
32 static FILE *stat_ev_file = NULL;
33 static int stat_ev_timer_sp = 0;
34 static timing_ticks_t stat_ev_timer_elapsed[MAX_TIMER];
35 static timing_ticks_t stat_ev_timer_start[MAX_TIMER];
38 static regex_t *filter = NULL;
39 static inline int key_matches(const char *key)
44 return regexec(filter, key, 0, NULL, 0) == 0;
47 static void stat_ev_vprintf(char ev, const char *key, const char *fmt, va_list ap)
49 if (!key_matches(key))
52 fprintf(stat_ev_file, "%c;%s", ev, key);
56 ir_vsnprintf(buf, sizeof(buf), fmt, ap);
57 fprintf(stat_ev_file, ";%s", buf);
59 fprintf(stat_ev_file, "\n");
62 static void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
66 stat_ev_vprintf(ev, key, fmt, ap);
70 void stat_ev_tim_push(void)
73 int sp = stat_ev_timer_sp++;
76 timing_enter_max_prio();
78 timing_ticks_sub(temp, stat_ev_timer_start[sp - 1]);
79 timing_ticks_add(stat_ev_timer_elapsed[sp - 1], temp);
81 timing_ticks_init(stat_ev_timer_elapsed[sp]);
82 timing_ticks(stat_ev_timer_start[sp]);
85 void stat_ev_tim_pop(const char *name)
90 sp = --stat_ev_timer_sp;
91 timing_ticks_sub(temp, stat_ev_timer_start[sp]);
92 timing_ticks_add(stat_ev_timer_elapsed[sp], temp);
93 if (name != NULL && stat_ev_enabled)
94 stat_ev_printf('E', name, "%g", timing_ticks_dbl(stat_ev_timer_elapsed[sp]));
96 timing_leave_max_prio();
98 timing_ticks(stat_ev_timer_start[sp - 1]);
102 void do_stat_ev_ctx_push_vfmt(const char *key, const char *fmt, va_list ap)
105 stat_ev_vprintf('P', key, fmt, ap);
106 stat_ev_tim_pop(NULL);
109 void (stat_ev_ctx_push_fmt)(const char *key, const char *fmt, ...)
111 if (!stat_ev_enabled)
116 do_stat_ev_ctx_push_vfmt(key, fmt, ap);
120 void (stat_ev_ctx_push_str)(const char *key, const char *str)
122 stat_ev_ctx_push_str_(key, str);
125 void do_stat_ev_ctx_pop(const char *key)
128 stat_ev_printf('O', key, NULL);
129 stat_ev_tim_pop(NULL);
132 void (stat_ev_ctx_pop)(const char *key)
134 stat_ev_ctx_pop_(key);
137 void do_stat_ev_dbl(const char *name, double value)
140 stat_ev_printf('E', name, "%g", value);
141 stat_ev_tim_pop(NULL);
144 void (stat_ev_dbl)(const char *name, double value)
146 stat_ev_dbl_(name, value);
149 void do_stat_ev_int(const char *name, int value)
152 stat_ev_printf('E', name, "%d", value);
153 stat_ev_tim_pop(NULL);
156 void (stat_ev_int)(const char *name, int value)
158 stat_ev_int_(name, value);
161 void do_stat_ev_ull(const char *name, unsigned long long value)
164 stat_ev_printf('E', name, "%llu", value);
165 stat_ev_tim_pop(NULL);
168 void (stat_ev_ull)(const char *name, unsigned long long value)
170 stat_ev_ull_(name, value);
173 void do_stat_ev(const char *name)
176 stat_ev_printf('E', name, "0.0");
177 stat_ev_tim_pop(NULL);
180 void (stat_ev)(const char *name)
185 void stat_ev_begin(const char *prefix, const char *filt)
189 snprintf(buf, sizeof(buf), "%s.ev", prefix);
190 stat_ev_file = fopen(buf, "wt");
192 if (filt && filt[0] != '\0') {
194 if (regcomp(®ex, filt, REG_EXTENDED) == 0)
198 stat_ev_enabled = stat_ev_file != NULL;
201 void stat_ev_end(void)
204 fclose(stat_ev_file);