fix uninitialized variable
[libfirm] / ir / stat / statev.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  * @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
35 #include <libcore/lc_timing.h>
36
37 #include "util.h"
38 #include "hashptr.h"
39 #include "irprintf.h"
40
41 #define MAX_CTX 128
42
43 typedef struct {
44         char key[32];
45         char value[96];
46         unsigned hash;
47 } ctx_t;
48
49 static ctx_t ctx_stack[MAX_CTX];
50
51 static unsigned long time_in_ev = 0;
52 static int ctx_sp     = -1;
53 static FILE *file_ev  = NULL;
54
55 static lc_timer_t *timer = NULL;
56
57 int stat_ev_enabled = 0;
58
59 #define get_time() lc_timer_elapsed_msec(timer)
60
61 void stat_ev_ctx_push(const char *key, const char *value)
62 {
63         if (file_ev) {
64                 unsigned long start = get_time();
65                 ctx_t *ctx    = &ctx_stack[ctx_sp + 1];
66                 unsigned hash = firm_fnv_hash_str(key);
67
68                 hash = HASH_COMBINE(hash, firm_fnv_hash_str(value));
69                 if (ctx_sp >= 0)
70                         hash = HASH_COMBINE(hash, ctx_stack[ctx_sp].hash);
71
72                 strncpy(ctx->key, key, array_size(ctx->key));
73                 strncpy(ctx->value, value, array_size(ctx->key));
74                 ctx->hash  = hash | 1;
75                 ++ctx_sp;
76
77                 fprintf(file_ev, "P %10x %30s %30s\n", ctx->hash, key, value);
78
79                 time_in_ev += get_time() - start;
80         }
81 }
82
83 void stat_ev_ctx_push_fobj(const char *key, const void *firm_object)
84 {
85         char buf[96];
86         ir_snprintf(buf, sizeof(buf), "%+F", firm_object);
87         stat_ev_ctx_push(key, buf);
88 }
89
90 void stat_ev_ctx_pop(void)
91 {
92         if (ctx_sp >= 0) {
93                 if (file_ev)
94                         fprintf(file_ev, "O %10x\n", ctx_stack[ctx_sp].hash);
95                 --ctx_sp;
96         }
97 }
98
99 void stat_ev_emit(const char *name, double value)
100 {
101         if (file_ev) {
102                 unsigned long start = get_time();
103                 unsigned         id = ctx_sp >= 0 ? ctx_stack[ctx_sp].hash : 0;
104
105                 fprintf(file_ev, "E %10x %30s %30f %10ld %10ld\n", id, name, value, start, time_in_ev);
106                 time_in_ev += get_time() - start;
107         }
108 }
109
110 void stat_ev_begin(const char *prefix)
111 {
112         char buf[512];
113
114         snprintf(buf, sizeof(buf), "%s.ev", prefix);
115
116         stat_ev_enabled = 1;
117         ctx_sp     = -1;
118         time_in_ev = 0;
119         file_ev    = fopen(buf, "wt");
120         timer      = lc_timer_register("stat_ev", "firm stat event timer");
121
122         lc_timer_start(timer);
123 }
124
125 void stat_ev_end(void)
126 {
127         if (timer)
128                 lc_timer_stop(timer);
129         if (file_ev)
130                 fclose(file_ev);
131 }