4d5c9e16f62e37369314ad0e3cf943f16292a17b
[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 #define MAX_TIMER 256
49
50 static FILE* stat_ev_file  = NULL;
51
52 int            stat_ev_enabled = 0;
53 int            stat_ev_timer_sp = 0;
54 timing_ticks_t stat_ev_timer_elapsed[MAX_TIMER];
55 timing_ticks_t stat_ev_timer_start[MAX_TIMER];
56
57 #ifdef FIRM_HAVE_REGEX
58 #include <regex.h>
59 static regex_t regex;
60 static regex_t *filter = NULL;
61 static inline int key_matches(const char *key)
62 {
63         if (!filter)
64                 return 1;
65
66         return regexec(filter, key, 0, NULL, 0) == 0;
67 }
68
69 #else
70 static char filter[128] = { '\0' };
71 static inline int key_matches(const char *key)
72 {
73         int i = 0;
74
75         for (i = 0; filter[i] != '\0'; ++i) {
76                 if (key[i] != filter[i])
77                         return 0;
78         }
79
80         return 1;
81 }
82 #endif /* FIRM_HAVE_REGEX */
83
84
85 void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
86 {
87         if (!key_matches(key))
88                 return;
89
90         fprintf(stat_ev_file, "%c;%s", ev, key);
91         if (fmt != NULL) {
92                 char buf[256];
93                 va_list args;
94
95                 va_start(args, fmt);
96                 ir_vsnprintf(buf, sizeof(buf), fmt, args);
97                 va_end(args);
98                 fprintf(stat_ev_file, ";%s", buf);
99         }
100         fprintf(stat_ev_file, "\n");
101 }
102
103 void stat_ev_begin(const char *prefix, const char *filt)
104 {
105         char buf[512];
106
107         snprintf(buf, sizeof(buf), "%s.ev", prefix);
108         stat_ev_file = fopen(buf, "wt");
109
110         if (filt && filt[0] != '\0') {
111 #ifdef FIRM_HAVE_REGEX
112                 filter = NULL;
113                 if (regcomp(&regex, filt, REG_EXTENDED) == 0)
114                         filter = &regex;
115 #else
116                 strncpy(filter, filt, sizeof(filter) - sizeof(filter[0]));
117 #endif /* FIRM_HAVE_REGEX */
118         }
119
120         stat_ev_enabled = stat_ev_file != NULL;
121 }
122
123 void stat_ev_end(void)
124 {
125         if (stat_ev_file) {
126                 fclose(stat_ev_file);
127         }
128 }
129
130 #endif