added statistics events
[libfirm] / ir / be / bestatevent.c
1 /**
2 * Statistic events
3 * @date 3.9.2006
4 * @author Sebastian Hack
5 */
6
7 #include <assert.h>
8 #include <string.h>
9
10 #include "bestatevent.h"
11
12 #define STACK_SIZE 16
13
14 typedef struct {
15         FILE *f;
16         char tag[512];
17 } ev_env_t;
18
19 static ev_env_t envs[STACK_SIZE];
20 static int sp = 0;
21
22 void be_stat_ev_push(const char **tags, int n_tags, FILE *f)
23 {
24         int i;
25         ev_env_t *env;
26
27         assert(sp < STACK_SIZE && "stat event stack full");
28         env = &envs[sp++];
29
30         env->tag[0] = '\0';
31         for(i = 0; i < n_tags; ++i) {
32                 strncat(env->tag, tags[i], sizeof(env->tag));
33                 strncat(env->tag, ";", sizeof(env->tag));
34         }
35         env->tag[sizeof(env->tag) - 1] = '\0';
36         env->f = f;
37 }
38
39 void be_stat_ev_pop(void)
40 {
41         if(sp > 0) {
42                 envs[--sp].f = NULL;
43         }
44 }
45
46 void be_stat_ev(const char *ev, int value)
47 {
48         if(sp > 0) {
49                 ev_env_t *env = &envs[sp - 1];
50                 if(env->f)
51                         fprintf(env->f, "%s%s;%d\n", env->tag, ev, value);
52         }
53 }
54
55 int be_stat_ev_is_active(void)
56 {
57         return sp > 0 && envs[sp - 1].f;
58 }