2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Statistic events.
23 * @author Sebastian Hack
36 #include "stat_timing.h"
44 int (stat_ev_enabled) = 0;
46 static FILE *stat_ev_file = NULL;
47 static int stat_ev_timer_sp = 0;
48 static timing_ticks_t stat_ev_timer_elapsed[MAX_TIMER];
49 static timing_ticks_t stat_ev_timer_start[MAX_TIMER];
52 static regex_t *filter = NULL;
53 static inline int key_matches(const char *key)
58 return regexec(filter, key, 0, NULL, 0) == 0;
61 static void stat_ev_vprintf(char ev, const char *key, const char *fmt, va_list ap)
63 if (!key_matches(key))
66 fprintf(stat_ev_file, "%c;%s", ev, key);
70 ir_vsnprintf(buf, sizeof(buf), fmt, ap);
71 fprintf(stat_ev_file, ";%s", buf);
73 fprintf(stat_ev_file, "\n");
76 static void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
80 stat_ev_vprintf(ev, key, fmt, ap);
84 void stat_ev_tim_push(void)
87 int sp = stat_ev_timer_sp++;
90 timing_enter_max_prio();
92 timing_ticks_sub(temp, stat_ev_timer_start[sp - 1]);
93 timing_ticks_add(stat_ev_timer_elapsed[sp - 1], temp);
95 timing_ticks_init(stat_ev_timer_elapsed[sp]);
96 timing_ticks(stat_ev_timer_start[sp]);
99 void stat_ev_tim_pop(const char *name)
104 sp = --stat_ev_timer_sp;
105 timing_ticks_sub(temp, stat_ev_timer_start[sp]);
106 timing_ticks_add(stat_ev_timer_elapsed[sp], temp);
107 if (name != NULL && stat_ev_enabled)
108 stat_ev_printf('E', name, "%g", timing_ticks_dbl(stat_ev_timer_elapsed[sp]));
110 timing_leave_max_prio();
112 timing_ticks(stat_ev_timer_start[sp - 1]);
116 void do_stat_ev_ctx_push_vfmt(const char *key, const char *fmt, va_list ap)
119 stat_ev_vprintf('P', key, fmt, ap);
120 stat_ev_tim_pop(NULL);
123 void (stat_ev_ctx_push_fmt)(const char *key, const char *fmt, ...)
125 if (!stat_ev_enabled)
130 do_stat_ev_ctx_push_vfmt(key, fmt, ap);
134 void (stat_ev_ctx_push_str)(const char *key, const char *str)
136 stat_ev_ctx_push_str_(key, str);
139 void do_stat_ev_ctx_pop(const char *key)
142 stat_ev_printf('O', key, NULL);
143 stat_ev_tim_pop(NULL);
146 void (stat_ev_ctx_pop)(const char *key)
148 stat_ev_ctx_pop_(key);
151 void do_stat_ev_dbl(const char *name, double value)
154 stat_ev_printf('E', name, "%g", value);
155 stat_ev_tim_pop(NULL);
158 void (stat_ev_dbl)(const char *name, double value)
160 stat_ev_dbl_(name, value);
163 void do_stat_ev_int(const char *name, int value)
166 stat_ev_printf('E', name, "%d", value);
167 stat_ev_tim_pop(NULL);
170 void (stat_ev_int)(const char *name, int value)
172 stat_ev_int_(name, value);
175 void do_stat_ev_ull(const char *name, unsigned long long value)
178 stat_ev_printf('E', name, "%llu", value);
179 stat_ev_tim_pop(NULL);
182 void (stat_ev_ull)(const char *name, unsigned long long value)
184 stat_ev_ull_(name, value);
187 void do_stat_ev(const char *name)
190 stat_ev_printf('E', name, "0.0");
191 stat_ev_tim_pop(NULL);
194 void (stat_ev)(const char *name)
199 void stat_ev_begin(const char *prefix, const char *filt)
203 snprintf(buf, sizeof(buf), "%s.ev", prefix);
204 stat_ev_file = fopen(buf, "wt");
206 if (filt && filt[0] != '\0') {
208 if (regcomp(®ex, filt, REG_EXTENDED) == 0)
212 stat_ev_enabled = stat_ev_file != NULL;
215 void stat_ev_end(void)
218 fclose(stat_ev_file);