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