rework architecture specific timer code
[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 #ifdef HAVE_REGEX_H
43 #define FIRM_HAVE_REGEX
44 #endif
45
46 #if defined HAVE_LIBZ && defined HAVE_ZLIB_H
47 #define FIRM_HAVE_LIBZ
48 #endif
49
50
51 #define MAX_TIMER 256
52
53 #ifdef FIRM_HAVE_LIBZ
54 #include <zlib.h>
55
56 #define mfprintf   gzprintf
57 static gzFile*     stat_ev_file  = NULL;
58
59 #else
60
61 #define mfprintf   fprintf
62 static FILE*       stat_ev_file  = NULL;
63
64 #endif /* FIRM_HAVE_LIBZ */
65
66 int            stat_ev_enabled = 0;
67 int            stat_ev_timer_sp = 0;
68 timing_ticks_t stat_ev_timer_elapsed[MAX_TIMER];
69 timing_ticks_t stat_ev_timer_start[MAX_TIMER];
70
71 #ifdef FIRM_HAVE_REGEX
72 #include <regex.h>
73 static regex_t regex;
74 static regex_t *filter = NULL;
75 static inline int key_matches(const char *key)
76 {
77         if (!filter)
78                 return 1;
79
80         return regexec(filter, key, 0, NULL, 0) == 0;
81 }
82
83 #else
84 static char filter[128] = { '\0' };
85 static inline int key_matches(const char *key)
86 {
87         int i = 0;
88
89         for (i = 0; filter[i] != '\0'; ++i) {
90                 if (key[i] != filter[i])
91                         return 0;
92         }
93
94         return 1;
95 }
96 #endif /* FIRM_HAVE_REGEX */
97
98
99 void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
100 {
101         if (!key_matches(key))
102                 return;
103
104         mfprintf(stat_ev_file, "%c;%s", ev, key);
105         if (fmt != NULL) {
106                 char buf[256];
107                 va_list args;
108
109                 va_start(args, fmt);
110                 ir_vsnprintf(buf, sizeof(buf), fmt, args);
111                 va_end(args);
112                 mfprintf(stat_ev_file, ";%s", buf);
113         }
114         mfprintf(stat_ev_file, "\n");
115 }
116
117 void stat_ev_begin(const char *prefix, const char *filt)
118 {
119         char buf[512];
120
121 #ifdef FIRM_HAVE_LIBZ
122         snprintf(buf, sizeof(buf), "%s.ev.gz", prefix);
123         stat_ev_file    = gzopen(buf, "wt9");
124 #else
125         snprintf(buf, sizeof(buf), "%s.ev", prefix);
126         stat_ev_file    = fopen(buf, "wt");
127 #endif
128
129         if (filt && filt[0] != '\0') {
130 #ifdef FIRM_HAVE_REGEX
131                 filter = NULL;
132                 if (regcomp(&regex, filt, REG_EXTENDED) == 0)
133                         filter = &regex;
134 #else
135                 strncpy(filter, filt, sizeof(filter) - sizeof(filter[0]));
136 #endif /* FIRM_HAVE_REGEX */
137         }
138
139         stat_ev_enabled = stat_ev_file != NULL;
140 }
141
142 void stat_ev_end(void)
143 {
144         if (stat_ev_file) {
145 #ifdef FIRM_HAVE_LIBZ
146                 gzflush(stat_ev_file, 1);
147                 gzclose(stat_ev_file);
148 #else
149                 fclose(stat_ev_file);
150 #endif
151         }
152 }