scripts: Generate accessor functions for nodes with a variable/dynamic number of...
[libfirm] / ir / stat / statev.c
index e3d694a..972935a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
  * @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 <stdlib.h>
-
-#include <libcore/lc_timing.h>
+#include <stdarg.h>
+#include <regex.h>
 
 #include "util.h"
-#include "hashptr.h"
+#include "stat_timing.h"
 #include "irprintf.h"
-#include "xmalloc.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;
+#include "statev_t.h"
 
-int stat_ev_enabled = 0;
+#include "config.h"
 
-typedef struct print_ev_t {
-       char               filter[128];
-       struct print_ev_t *next;
-} print_ev_t;
+#define MAX_TIMER 256
 
-static print_ev_t *print_events                = NULL;
-static int         ctx_switch_since_last_print = 0;
+int (stat_ev_enabled) = 0;
 
-#define get_time() lc_timer_elapsed_usec(timer)
+static FILE          *stat_ev_file     = NULL;
+static int            stat_ev_timer_sp = 0;
+static timing_ticks_t stat_ev_timer_elapsed[MAX_TIMER];
+static timing_ticks_t stat_ev_timer_start[MAX_TIMER];
 
-void stat_ev_ctx_push(const char *key, const char *value)
+static regex_t  regex;
+static regex_t *filter = NULL;
+static inline int key_matches(const char *key)
 {
-       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);
+       if (!filter)
+               return 1;
 
-               snprintf(ctx->key, array_size(ctx->key), "%s", key);
-               snprintf(ctx->value, array_size(ctx->value), "%s", value);
-               ctx->hash  = hash | 1;
-               ++ctx_sp;
+       return regexec(filter, key, 0, NULL, 0) == 0;
+}
 
-               fprintf(file_ev, "P %10x %30s %30s\n", ctx->hash, ctx->key, ctx->value);
+static void stat_ev_vprintf(char ev, const char *key, const char *fmt, va_list ap)
+{
+       if (!key_matches(key))
+               return;
 
-               ctx_switch_since_last_print = 1;
+       fprintf(stat_ev_file, "%c;%s", ev, key);
+       if (fmt != NULL) {
+               char buf[256];
 
-               time_in_ev += get_time() - start;
+               ir_vsnprintf(buf, sizeof(buf), fmt, ap);
+               fprintf(stat_ev_file, ";%s", buf);
        }
+       fprintf(stat_ev_file, "\n");
 }
 
-void stat_ev_ctx_push_fobj(const char *key, const void *firm_object)
+static void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
 {
-       if (file_ev) {
-               char buf[96];
-               ir_snprintf(buf, sizeof(buf), "%+F", firm_object);
-               stat_ev_ctx_push(key, buf);
+       va_list ap;
+       va_start(ap, fmt);
+       stat_ev_vprintf(ev, key, fmt, ap);
+       va_end(ap);
+}
+
+void stat_ev_tim_push(void)
+{
+       timing_ticks_t temp;
+       int sp = stat_ev_timer_sp++;
+       timing_ticks(temp);
+       if (sp == 0) {
+               timing_enter_max_prio();
+       } else {
+               timing_ticks_sub(temp, stat_ev_timer_start[sp - 1]);
+               timing_ticks_add(stat_ev_timer_elapsed[sp - 1], temp);
        }
+       timing_ticks_init(stat_ev_timer_elapsed[sp]);
+       timing_ticks(stat_ev_timer_start[sp]);
 }
 
-void stat_ev_ctx_pop(void)
+void stat_ev_tim_pop(const char *name)
 {
-       if (ctx_sp >= 0) {
-               if (file_ev) {
-                       fprintf(file_ev, "O %10x\n", ctx_stack[ctx_sp].hash);
-                       ctx_switch_since_last_print = 1;
-               }
-               --ctx_sp;
+       int sp;
+       timing_ticks_t temp;
+       timing_ticks(temp);
+       sp = --stat_ev_timer_sp;
+       timing_ticks_sub(temp, stat_ev_timer_start[sp]);
+       timing_ticks_add(stat_ev_timer_elapsed[sp], temp);
+       if (name != NULL && stat_ev_enabled)
+               stat_ev_printf('E', name, "%g", timing_ticks_dbl(stat_ev_timer_elapsed[sp]));
+       if (sp == 0) {
+               timing_leave_max_prio();
+       } else {
+               timing_ticks(stat_ev_timer_start[sp - 1]);
        }
 }
 
-static void maybe_print_context(void)
+void do_stat_ev_ctx_push_vfmt(const char *key, const char *fmt, va_list ap)
 {
-       int i;
+       stat_ev_tim_push();
+       stat_ev_vprintf('P', key, fmt, ap);
+       stat_ev_tim_pop(NULL);
+}
 
-       if(!ctx_switch_since_last_print)
+void (stat_ev_ctx_push_fmt)(const char *key, const char *fmt, ...)
+{
+       if (!stat_ev_enabled)
                return;
 
-       for(i = 0; i <= ctx_sp; ++i) {
-               if(i > 0)
-                       fputc(':', stderr);
-               fputs(ctx_stack[i].value, stderr);
-       }
-       fputc('\n', stderr);
-       ctx_switch_since_last_print = 0;
+       va_list ap;
+       va_start(ap, fmt);
+       do_stat_ev_ctx_push_vfmt(key, fmt, ap);
+       va_end(ap);
 }
 
-void stat_ev_emit(const char *name, double value)
+void (stat_ev_ctx_push_str)(const char *key, const char *str)
 {
-       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);
-
-               if(print_events != NULL) {
-                       print_ev_t *print_ev = print_events;
-                       while(print_ev != NULL) {
-                               /* maybe wanna use regexes instead of strcmp? */
-                               if(strstr(name, print_ev->filter) != NULL) {
-                                       maybe_print_context();
-                                       fprintf(stderr, "\t%20s  %30f\n", name, value);
-                               }
+       stat_ev_ctx_push_str_(key, str);
+}
 
-                               print_ev = print_ev->next;
-                       }
-               }
+void do_stat_ev_ctx_pop(const char *key)
+{
+       stat_ev_tim_push();
+       stat_ev_printf('O', key, NULL);
+       stat_ev_tim_pop(NULL);
+}
 
-               time_in_ev += get_time() - start;
-       }
+void (stat_ev_ctx_pop)(const char *key)
+{
+       stat_ev_ctx_pop_(key);
 }
 
-void stat_ev_begin(const char *prefix)
+void do_stat_ev_dbl(const char *name, double value)
 {
-       char buf[512];
+       stat_ev_tim_push();
+       stat_ev_printf('E', name, "%g", value);
+       stat_ev_tim_pop(NULL);
+}
 
-       snprintf(buf, sizeof(buf), "%s.ev", prefix);
+void (stat_ev_dbl)(const char *name, double value)
+{
+       stat_ev_dbl_(name, value);
+}
 
-       stat_ev_enabled = 1;
-       ctx_sp          = -1;
-       time_in_ev      = 0;
-       print_events    = NULL;
-       file_ev         = fopen(buf, "wt");
-       timer           = lc_timer_register("stat_ev", "firm stat event timer");
+void do_stat_ev_int(const char *name, int value)
+{
+       stat_ev_tim_push();
+       stat_ev_printf('E', name, "%d", value);
+       stat_ev_tim_pop(NULL);
+}
 
-       lc_timer_start(timer);
+void (stat_ev_int)(const char *name, int value)
+{
+       stat_ev_int_(name, value);
 }
 
-void stat_ev_end(void)
+void do_stat_ev_ull(const char *name, unsigned long long value)
 {
-       print_ev_t *print_ev, *next;
+       stat_ev_tim_push();
+       stat_ev_printf('E', name, "%llu", value);
+       stat_ev_tim_pop(NULL);
+}
 
-       if (timer)
-               lc_timer_stop(timer);
-       if (file_ev)
-               fclose(file_ev);
+void (stat_ev_ull)(const char *name, unsigned long long value)
+{
+       stat_ev_ull_(name, value);
+}
 
-       for (print_ev = print_events; print_ev != NULL; print_ev = next) {
-               next = print_ev->next;
-               free(print_ev);
-       }
+void do_stat_ev(const char *name)
+{
+       stat_ev_tim_push();
+       stat_ev_printf('E', name, "0.0");
+       stat_ev_tim_pop(NULL);
 }
 
-void stat_ev_flush(void)
+void (stat_ev)(const char *name)
 {
-       unsigned long start = get_time();
-       if (file_ev)
-               fflush(file_ev);
-       time_in_ev += get_time() - start;
+       stat_ev_(name);
 }
 
-void stat_ev_print(const char *filter)
+void stat_ev_begin(const char *prefix, const char *filt)
 {
-       size_t len;
-       print_ev_t *print_ev = xmalloc(sizeof(print_ev[0]));
+       char buf[512];
 
-       memset(print_ev, 0, sizeof(print_ev[0]));
+       snprintf(buf, sizeof(buf), "%s.ev", prefix);
+       stat_ev_file = fopen(buf, "wt");
 
-       len = strlen(filter) + 1;
-       if (len >= sizeof(print_ev->filter)) {
-               fprintf(stderr, "Warning: capping event filter (too long)");
-               len = sizeof(print_ev->filter);
+       if (filt && filt[0] != '\0') {
+               filter = NULL;
+               if (regcomp(&regex, filt, REG_EXTENDED) == 0)
+                       filter = &regex;
        }
-       memcpy(print_ev->filter, filter, len);
-       print_ev->filter[len-1] = 0;
 
-       print_ev->next = print_events;
-       print_events   = print_ev;
+       stat_ev_enabled = stat_ev_file != NULL;
+}
+
+void stat_ev_end(void)
+{
+       if (stat_ev_file) {
+               fclose(stat_ev_file);
+       }
+       if (filter != NULL)
+               regfree(filter);
 }