updated Header
[libfirm] / ir / be / bestatevent.c
1 /*
2  * Copyright (C) 1995-2007 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  * Statistic events
22  * @date    3.9.2006
23  * @author  Sebastian Hack
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <assert.h>
31 #include <string.h>
32
33 #include "bestatevent.h"
34
35 #define STACK_SIZE 16
36
37 typedef struct {
38         FILE *f;
39         char tag[512];
40 } ev_env_t;
41
42 static ev_env_t envs[STACK_SIZE];
43 static unsigned sp = 0;
44
45 void be_stat_ev_push(const char **tags, int n_tags, FILE *f)
46 {
47         int i;
48         ev_env_t *env;
49
50         assert(sp < STACK_SIZE && "stat event stack full");
51         env = &envs[sp++];
52
53         env->tag[0] = '\0';
54         for(i = 0; i < n_tags; ++i) {
55                 strncat(env->tag, tags[i], sizeof(env->tag));
56                 strncat(env->tag, ";", sizeof(env->tag));
57         }
58         env->tag[sizeof(env->tag) - 1] = '\0';
59         env->f = f;
60 }
61
62 void be_stat_ev_pop(void)
63 {
64         if (sp == 0)
65                 return;
66
67         envs[--sp].f = NULL;
68 }
69
70 void be_stat_ev(const char *ev, int value)
71 {
72         ev_env_t *env;
73
74         if (sp == 0)
75                 return;
76
77         env = &envs[sp - 1];
78         if (env->f == NULL)
79                 return;
80
81         fprintf(env->f, "%s%s;%d\n", env->tag, ev, value);
82 }
83
84 void be_stat_ev_l(const char *ev, long value)
85 {
86         ev_env_t *env;
87
88         if (sp == 0)
89                 return;
90
91         env = &envs[sp - 1];
92         if (env->f == NULL)
93                 return;
94
95         fprintf(env->f, "%s%s;%ld\n", env->tag, ev, value);
96 }
97
98 void be_stat_ev_dbl(const char *ev, double value)
99 {
100         ev_env_t *env;
101
102         if (sp == 0)
103                 return;
104
105         env = &envs[sp - 1];
106         if (env->f == NULL)
107                 return;
108
109         fprintf(env->f, "%s%s;%f\n", env->tag, ev, value);
110 }
111
112 void be_stat_ev_ull(const char *ev, ulong64 value)
113 {
114         ev_env_t *env;
115
116         if (sp == 0)
117                 return;
118
119         env = &envs[sp - 1];
120         if (env->f == NULL)
121                 return;
122
123         fprintf(env->f, "%s%s;%" ULL_FMT "\n", env->tag, ev, value);
124 }
125
126 int be_stat_ev_is_active(void)
127 {
128         return sp > 0 && envs[sp - 1].f != NULL;
129 }