minimize number of build variants: always enable statistics
[libfirm] / ir / stat / statev.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Statistic events.
23  * @author      Sebastian Hack
24  * @date        17.06.2007
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include <assert.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34
35 #include "util.h"
36 #include "stat_timing.h"
37 #include "irprintf.h"
38 #include "statev.h"
39
40 #include "config.h"
41
42 #ifndef DISABLE_STATEV
43
44 #ifdef HAVE_REGEX_H
45 #define FIRM_HAVE_REGEX
46 #endif
47
48 #if defined HAVE_LIBZ && defined HAVE_ZLIB_H
49 #define FIRM_HAVE_LIBZ
50 #endif
51
52
53 #define MAX_TIMER 256
54
55 #ifdef FIRM_HAVE_LIBZ
56 #include <zlib.h>
57
58 #define mfprintf   gzprintf
59 static gzFile      stat_ev_file  = NULL;
60
61 #else
62
63 #define mfprintf   fprintf
64 static FILE*       stat_ev_file  = NULL;
65
66 #endif /* FIRM_HAVE_LIBZ */
67
68 int            stat_ev_enabled = 0;
69 int            stat_ev_timer_sp = 0;
70 timing_ticks_t stat_ev_timer_elapsed[MAX_TIMER];
71 timing_ticks_t stat_ev_timer_start[MAX_TIMER];
72
73 #ifdef FIRM_HAVE_REGEX
74 #include <regex.h>
75 static regex_t regex;
76 static regex_t *filter = NULL;
77 static inline int key_matches(const char *key)
78 {
79         if (!filter)
80                 return 1;
81
82         return regexec(filter, key, 0, NULL, 0) == 0;
83 }
84
85 #else
86 static char filter[128] = { '\0' };
87 static inline int key_matches(const char *key)
88 {
89         int i = 0;
90
91         for (i = 0; filter[i] != '\0'; ++i) {
92                 if (key[i] != filter[i])
93                         return 0;
94         }
95
96         return 1;
97 }
98 #endif /* FIRM_HAVE_REGEX */
99
100
101 void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
102 {
103         if (!key_matches(key))
104                 return;
105
106         mfprintf(stat_ev_file, "%c;%s", ev, key);
107         if (fmt != NULL) {
108                 char buf[256];
109                 va_list args;
110
111                 va_start(args, fmt);
112                 ir_vsnprintf(buf, sizeof(buf), fmt, args);
113                 va_end(args);
114                 mfprintf(stat_ev_file, ";%s", buf);
115         }
116         mfprintf(stat_ev_file, "\n");
117 }
118
119 void stat_ev_begin(const char *prefix, const char *filt)
120 {
121         char buf[512];
122
123 #ifdef FIRM_HAVE_LIBZ
124         snprintf(buf, sizeof(buf), "%s.ev.gz", prefix);
125         stat_ev_file = gzopen(buf, "wt9");
126 #else
127         snprintf(buf, sizeof(buf), "%s.ev", prefix);
128         stat_ev_file = fopen(buf, "wt");
129 #endif
130
131         if (filt && filt[0] != '\0') {
132 #ifdef FIRM_HAVE_REGEX
133                 filter = NULL;
134                 if (regcomp(&regex, filt, REG_EXTENDED) == 0)
135                         filter = &regex;
136 #else
137                 strncpy(filter, filt, sizeof(filter) - sizeof(filter[0]));
138 #endif /* FIRM_HAVE_REGEX */
139         }
140
141         stat_ev_enabled = stat_ev_file != NULL;
142 }
143
144 void stat_ev_end(void)
145 {
146         if (stat_ev_file) {
147 #ifdef FIRM_HAVE_LIBZ
148                 gzflush(stat_ev_file, 1);
149                 gzclose(stat_ev_file);
150 #else
151                 fclose(stat_ev_file);
152 #endif
153         }
154 }
155
156 #endif