Added statistic events
authorSebastian Hack <hack@ipd.info.uni-karlsruhe.de>
Thu, 21 Jun 2007 16:35:56 +0000 (16:35 +0000)
committerSebastian Hack <hack@ipd.info.uni-karlsruhe.de>
Thu, 21 Jun 2007 16:35:56 +0000 (16:35 +0000)
[r14699]

ir/stat/statev.c [new file with mode: 0644]
ir/stat/statev.h [new file with mode: 0644]

diff --git a/ir/stat/statev.c b/ir/stat/statev.c
new file mode 100644 (file)
index 0000000..060fd4f
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * 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$
+ */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <libcore/lc_timing.h>
+
+#include "util.h"
+#include "hashptr.h"
+#include "irprintf.h"
+
+#define MAX_CTX 128
+
+typedef struct {
+       char key[32];
+       char value[96];
+       unsigned hash;
+} ctx_t;
+
+static ctx_t ctx_stack[MAX_CTX];
+
+static unsigned long time_in_ev = 0;
+static int ctx_sp     = -1;
+static FILE *file_ev  = NULL;
+
+static lc_timer_t *timer = NULL;
+
+int stat_ev_enabled = 0;
+
+#define get_time() lc_timer_elapsed_msec(timer)
+
+void stat_ev_ctx_push(const char *key, const char *value)
+{
+       if (file_ev) {
+               unsigned long start = get_time();
+               ctx_t *ctx    = &ctx_stack[ctx_sp + 1];
+               unsigned hash = firm_fnv_hash_str(key);
+
+               hash = HASH_COMBINE(hash, firm_fnv_hash_str(value));
+               if (ctx_sp >= 0)
+                       hash = HASH_COMBINE(hash, ctx_stack[ctx_sp].hash);
+
+               strncpy(ctx->key, key, array_size(ctx->key));
+               strncpy(ctx->value, value, array_size(ctx->key));
+               ctx->hash  = hash | 1;
+               ++ctx_sp;
+
+               fprintf(file_ev, "P %10x %30s %30s\n", ctx->hash, key, value);
+
+               time_in_ev += get_time() - start;
+       }
+}
+
+void stat_ev_ctx_push_fobj(const char *key, const void *firm_object)
+{
+       char buf[96];
+       ir_snprintf(buf, sizeof(buf), "%+F", firm_object);
+       stat_ev_ctx_push(key, buf);
+}
+
+void stat_ev_ctx_pop(void)
+{
+       if (ctx_sp >= 0) {
+               if (file_ev)
+                       fprintf(file_ev, "O %10x\n", ctx_stack[ctx_sp].hash);
+               --ctx_sp;
+       }
+}
+
+void stat_ev_emit(const char *name, double value)
+{
+       if (file_ev) {
+               unsigned long start = get_time();
+               unsigned         id = ctx_sp >= 0 ? ctx_stack[ctx_sp].hash : 0;
+
+               fprintf(file_ev, "E %10x %30s %30f %10ld %10ld\n", id, name, value, start, time_in_ev);
+               time_in_ev += get_time() - start;
+       }
+}
+
+void stat_ev_begin(const char *prefix)
+{
+       char buf[512];
+
+       snprintf(buf, sizeof(buf), "%s.ev", prefix);
+
+       stat_ev_enabled = 1;
+       ctx_sp     = -1;
+       time_in_ev = 0;
+       file_ev    = fopen(buf, "wt");
+       timer      = lc_timer_register("stat_ev", "firm stat event timer");
+
+       lc_timer_start(timer);
+}
+
+void stat_ev_end(void)
+{
+       if (timer)
+               lc_timer_stop(timer);
+       if (file_ev)
+               fclose(file_ev);
+}
diff --git a/ir/stat/statev.h b/ir/stat/statev.h
new file mode 100644 (file)
index 0000000..032a693
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * 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$
+ */
+
+#ifndef FIRM_STATEVENT_H
+#define FIRM_STATEVENT_H
+
+#ifndef FIRM_STATISTICS
+
+#define stat_ev_cnt_decl(var)
+#define stat_ev_cnt_inc(var)
+#define stat_ev_cnt_done(name, var)
+#define stat_ev(name)
+
+#define stat_ev_ctx_push_fobj(key, firm_object)
+#define stat_ev_ctx_push(key, value)
+#define stat_ev_ctx_pop()
+#define stat_ev_begin(prefix, blub)
+#define stat_ev_end()
+
+#else
+
+extern int stat_ev_enabled;
+
+#define stat_ev_do(expr)            (stat_ev_enabled ? ((expr), 1) : 0)
+
+#define stat_ev_dbl(name, val)      if (stat_ev_enabled) { stat_ev_emit(name, val); }
+
+#define stat_ev_cnt_decl(var)       int stat_ev_cnt_var_ ## var = 0
+#define stat_ev_cnt_inc(var)        (void) ++stat_ev_cnt_var_ ## var
+#define stat_ev_cnt_done(var, name) stat_ev_dbl((name), stat_ev_cnt_var_ ## var)
+
+#define stat_ev(name)               stat_ev_dbl((name), 0.0)
+
+void stat_ev_ctx_push_fobj(const char *key, const void *firm_object);
+void stat_ev_ctx_push(const char *key, const char *value);
+void stat_ev_ctx_pop(void);
+void stat_ev_emit(const char *name, double val);
+void stat_ev_begin(const char *prefix);
+void stat_ev_end(void);
+
+#endif
+
+#endif /* FIRM_STATEVENT_H */