statev.c 3.06 KB
Newer Older
Sebastian Hack's avatar
Sebastian Hack committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Sebastian Hack's avatar
Sebastian Hack committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief       Statistic events.
 * @author      Sebastian Hack
 * @date        17.06.2007
 * @version     $Id$
 */
#include "config.h"

#include <assert.h>
#include <string.h>
#include <stdio.h>
32
#include <stdlib.h>
Sebastian Hack's avatar
Sebastian Hack committed
33
#include <stdarg.h>
Sebastian Hack's avatar
Sebastian Hack committed
34
35

#include "util.h"
36
#include "stat_timing.h"
Sebastian Hack's avatar
Sebastian Hack committed
37
#include "irprintf.h"
Sebastian Hack's avatar
Sebastian Hack committed
38
#include "statev.h"
Sebastian Hack's avatar
Sebastian Hack committed
39

Sebastian Hack's avatar
Sebastian Hack committed
40
#include "config.h"
Sebastian Hack's avatar
Sebastian Hack committed
41

Sebastian Hack's avatar
Sebastian Hack committed
42
43
44
#ifdef HAVE_REGEX_H
#define FIRM_HAVE_REGEX
#endif
Sebastian Hack's avatar
Sebastian Hack committed
45

Sebastian Hack's avatar
Sebastian Hack committed
46
47
48
#if defined HAVE_LIBZ && defined HAVE_ZLIB_H
#define FIRM_HAVE_LIBZ
#endif
Sebastian Hack's avatar
Sebastian Hack committed
49
50


Sebastian Hack's avatar
Sebastian Hack committed
51
#define MAX_TIMER 256
52

Sebastian Hack's avatar
Sebastian Hack committed
53
54
#ifdef FIRM_HAVE_LIBZ
#include <zlib.h>
55

Sebastian Hack's avatar
Sebastian Hack committed
56
57
#define mfprintf   gzprintf
static gzFile*     stat_ev_file  = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
58

Sebastian Hack's avatar
Sebastian Hack committed
59
#else
Sebastian Hack's avatar
Sebastian Hack committed
60

Sebastian Hack's avatar
Sebastian Hack committed
61
62
#define mfprintf   fprintf
static FILE*       stat_ev_file  = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
63

Sebastian Hack's avatar
Sebastian Hack committed
64
#endif /* FIRM_HAVE_LIBZ */
Sebastian Hack's avatar
Sebastian Hack committed
65

66
67
68
69
int            stat_ev_enabled = 0;
int            stat_ev_timer_sp = 0;
timing_ticks_t stat_ev_timer_elapsed[MAX_TIMER];
timing_ticks_t stat_ev_timer_start[MAX_TIMER];
70

Sebastian Hack's avatar
Sebastian Hack committed
71
72
73
74
#ifdef FIRM_HAVE_REGEX
#include <regex.h>
static regex_t regex;
static regex_t *filter = NULL;
75
static inline int key_matches(const char *key)
Sebastian Hack's avatar
Sebastian Hack committed
76
77
78
{
	if (!filter)
		return 1;
Sebastian Hack's avatar
Sebastian Hack committed
79

Sebastian Hack's avatar
Sebastian Hack committed
80
	return regexec(filter, key, 0, NULL, 0) == 0;
Sebastian Hack's avatar
Sebastian Hack committed
81
82
}

Sebastian Hack's avatar
Sebastian Hack committed
83
84
#else
static char filter[128] = { '\0' };
85
static inline int key_matches(const char *key)
Sebastian Hack's avatar
Sebastian Hack committed
86
{
Sebastian Hack's avatar
Sebastian Hack committed
87
	int i = 0;
Sebastian Hack's avatar
Sebastian Hack committed
88

Sebastian Hack's avatar
Sebastian Hack committed
89
90
91
	for (i = 0; filter[i] != '\0'; ++i) {
		if (key[i] != filter[i])
			return 0;
Sebastian Hack's avatar
Sebastian Hack committed
92
	}
Sebastian Hack's avatar
Sebastian Hack committed
93
94

	return 1;
Sebastian Hack's avatar
Sebastian Hack committed
95
}
Sebastian Hack's avatar
Sebastian Hack committed
96
#endif /* FIRM_HAVE_REGEX */
Sebastian Hack's avatar
Sebastian Hack committed
97

98

Sebastian Hack's avatar
Sebastian Hack committed
99
100
void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
{
101
	if (!key_matches(key))
102
103
		return;

Sebastian Hack's avatar
Sebastian Hack committed
104
105
106
107
	mfprintf(stat_ev_file, "%c;%s", ev, key);
	if (fmt != NULL) {
		char buf[256];
		va_list args;
108

Sebastian Hack's avatar
Sebastian Hack committed
109
110
111
112
		va_start(args, fmt);
		ir_vsnprintf(buf, sizeof(buf), fmt, args);
		va_end(args);
		mfprintf(stat_ev_file, ";%s", buf);
Sebastian Hack's avatar
Sebastian Hack committed
113
	}
Sebastian Hack's avatar
Sebastian Hack committed
114
	mfprintf(stat_ev_file, "\n");
Sebastian Hack's avatar
Sebastian Hack committed
115
116
}

Sebastian Hack's avatar
Sebastian Hack committed
117
void stat_ev_begin(const char *prefix, const char *filt)
Sebastian Hack's avatar
Sebastian Hack committed
118
119
120
{
	char buf[512];

Sebastian Hack's avatar
Sebastian Hack committed
121
122
123
124
#ifdef FIRM_HAVE_LIBZ
	snprintf(buf, sizeof(buf), "%s.ev.gz", prefix);
	stat_ev_file    = gzopen(buf, "wt9");
#else
Sebastian Hack's avatar
Sebastian Hack committed
125
	snprintf(buf, sizeof(buf), "%s.ev", prefix);
Sebastian Hack's avatar
Sebastian Hack committed
126
127
	stat_ev_file    = fopen(buf, "wt");
#endif
Sebastian Hack's avatar
Sebastian Hack committed
128

Sebastian Hack's avatar
Sebastian Hack committed
129
130
131
132
133
134
135
136
	if (filt && filt[0] != '\0') {
#ifdef FIRM_HAVE_REGEX
		filter = NULL;
		if (regcomp(&regex, filt, REG_EXTENDED) == 0)
			filter = &regex;
#else
		strncpy(filter, filt, sizeof(filter) - sizeof(filter[0]));
#endif /* FIRM_HAVE_REGEX */
137
	}
Sebastian Hack's avatar
Sebastian Hack committed
138

Sebastian Hack's avatar
Sebastian Hack committed
139
	stat_ev_enabled = stat_ev_file != NULL;
Sebastian Hack's avatar
Sebastian Hack committed
140
}
141

Sebastian Hack's avatar
Sebastian Hack committed
142
void stat_ev_end(void)
143
{
Sebastian Hack's avatar
Sebastian Hack committed
144
145
146
147
148
149
150
	if (stat_ev_file) {
#ifdef FIRM_HAVE_LIBZ
		gzflush(stat_ev_file, 1);
		gzclose(stat_ev_file);
#else
		fclose(stat_ev_file);
#endif
151
152
	}
}