bepeephole: Inline be_peephole_new_node() into its only caller.
[libfirm] / ir / stat / statev.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Statistic events.
9  * @author      Sebastian Hack
10  * @date        17.06.2007
11  */
12 #include "config.h"
13
14 #include <assert.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <regex.h>
20
21 #include "util.h"
22 #include "stat_timing.h"
23 #include "irprintf.h"
24 #include "statev_t.h"
25
26 #include "config.h"
27
28 #define MAX_TIMER 256
29
30 int (stat_ev_enabled) = 0;
31
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];
36
37 static regex_t  regex;
38 static regex_t *filter = NULL;
39 static inline int key_matches(const char *key)
40 {
41         if (!filter)
42                 return 1;
43
44         return regexec(filter, key, 0, NULL, 0) == 0;
45 }
46
47 static void stat_ev_vprintf(char ev, const char *key, const char *fmt, va_list ap)
48 {
49         if (!key_matches(key))
50                 return;
51
52         fprintf(stat_ev_file, "%c;%s", ev, key);
53         if (fmt != NULL) {
54                 char buf[256];
55
56                 ir_vsnprintf(buf, sizeof(buf), fmt, ap);
57                 fprintf(stat_ev_file, ";%s", buf);
58         }
59         fprintf(stat_ev_file, "\n");
60 }
61
62 static void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
63 {
64         va_list ap;
65         va_start(ap, fmt);
66         stat_ev_vprintf(ev, key, fmt, ap);
67         va_end(ap);
68 }
69
70 void stat_ev_tim_push(void)
71 {
72         timing_ticks_t temp;
73         int sp = stat_ev_timer_sp++;
74         timing_ticks(temp);
75         if (sp == 0) {
76                 timing_enter_max_prio();
77         } else {
78                 timing_ticks_sub(temp, stat_ev_timer_start[sp - 1]);
79                 timing_ticks_add(stat_ev_timer_elapsed[sp - 1], temp);
80         }
81         timing_ticks_init(stat_ev_timer_elapsed[sp]);
82         timing_ticks(stat_ev_timer_start[sp]);
83 }
84
85 void stat_ev_tim_pop(const char *name)
86 {
87         int sp;
88         timing_ticks_t temp;
89         timing_ticks(temp);
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]));
95         if (sp == 0) {
96                 timing_leave_max_prio();
97         } else {
98                 timing_ticks(stat_ev_timer_start[sp - 1]);
99         }
100 }
101
102 void do_stat_ev_ctx_push_vfmt(const char *key, const char *fmt, va_list ap)
103 {
104         stat_ev_tim_push();
105         stat_ev_vprintf('P', key, fmt, ap);
106         stat_ev_tim_pop(NULL);
107 }
108
109 void (stat_ev_ctx_push_fmt)(const char *key, const char *fmt, ...)
110 {
111         if (!stat_ev_enabled)
112                 return;
113
114         va_list ap;
115         va_start(ap, fmt);
116         do_stat_ev_ctx_push_vfmt(key, fmt, ap);
117         va_end(ap);
118 }
119
120 void (stat_ev_ctx_push_str)(const char *key, const char *str)
121 {
122         stat_ev_ctx_push_str_(key, str);
123 }
124
125 void do_stat_ev_ctx_pop(const char *key)
126 {
127         stat_ev_tim_push();
128         stat_ev_printf('O', key, NULL);
129         stat_ev_tim_pop(NULL);
130 }
131
132 void (stat_ev_ctx_pop)(const char *key)
133 {
134         stat_ev_ctx_pop_(key);
135 }
136
137 void do_stat_ev_dbl(const char *name, double value)
138 {
139         stat_ev_tim_push();
140         stat_ev_printf('E', name, "%g", value);
141         stat_ev_tim_pop(NULL);
142 }
143
144 void (stat_ev_dbl)(const char *name, double value)
145 {
146         stat_ev_dbl_(name, value);
147 }
148
149 void do_stat_ev_int(const char *name, int value)
150 {
151         stat_ev_tim_push();
152         stat_ev_printf('E', name, "%d", value);
153         stat_ev_tim_pop(NULL);
154 }
155
156 void (stat_ev_int)(const char *name, int value)
157 {
158         stat_ev_int_(name, value);
159 }
160
161 void do_stat_ev_ull(const char *name, unsigned long long value)
162 {
163         stat_ev_tim_push();
164         stat_ev_printf('E', name, "%llu", value);
165         stat_ev_tim_pop(NULL);
166 }
167
168 void (stat_ev_ull)(const char *name, unsigned long long value)
169 {
170         stat_ev_ull_(name, value);
171 }
172
173 void do_stat_ev(const char *name)
174 {
175         stat_ev_tim_push();
176         stat_ev_printf('E', name, "0.0");
177         stat_ev_tim_pop(NULL);
178 }
179
180 void (stat_ev)(const char *name)
181 {
182         stat_ev_(name);
183 }
184
185 void stat_ev_begin(const char *prefix, const char *filt)
186 {
187         char buf[512];
188
189         snprintf(buf, sizeof(buf), "%s.ev", prefix);
190         stat_ev_file = fopen(buf, "wt");
191
192         if (filt && filt[0] != '\0') {
193                 filter = NULL;
194                 if (regcomp(&regex, filt, REG_EXTENDED) == 0)
195                         filter = &regex;
196         }
197
198         stat_ev_enabled = stat_ev_file != NULL;
199 }
200
201 void stat_ev_end(void)
202 {
203         if (stat_ev_file) {
204                 fclose(stat_ev_file);
205         }
206         if (filter != NULL)
207                 regfree(filter);
208 }