+static void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
+{
+ 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_tim_pop(const char *name)
+{
+ 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]);
+ }
+}
+
+void do_stat_ev_ctx_push_vfmt(const char *key, const char *fmt, va_list ap)
+{
+ stat_ev_tim_push();
+ stat_ev_vprintf('P', key, fmt, ap);
+ stat_ev_tim_pop(NULL);
+}
+
+void (stat_ev_ctx_push_fmt)(const char *key, const char *fmt, ...)
+{
+ if (!stat_ev_enabled)
+ return;
+
+ va_list ap;
+ va_start(ap, fmt);
+ do_stat_ev_ctx_push_vfmt(key, fmt, ap);
+ va_end(ap);
+}
+
+void (stat_ev_ctx_push_str)(const char *key, const char *str)
+{
+ stat_ev_ctx_push_str_(key, str);
+}
+
+void do_stat_ev_ctx_pop(const char *key)
+{
+ stat_ev_tim_push();
+ stat_ev_printf('O', key, NULL);
+ stat_ev_tim_pop(NULL);
+}
+
+void (stat_ev_ctx_pop)(const char *key)
+{
+ stat_ev_ctx_pop_(key);
+}
+
+void do_stat_ev_dbl(const char *name, double value)
+{
+ stat_ev_tim_push();
+ stat_ev_printf('E', name, "%g", value);
+ stat_ev_tim_pop(NULL);
+}
+
+void (stat_ev_dbl)(const char *name, double value)
+{
+ stat_ev_dbl_(name, value);
+}
+
+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);
+}
+
+void (stat_ev_int)(const char *name, int value)
+{
+ stat_ev_int_(name, value);
+}
+
+void do_stat_ev_ull(const char *name, unsigned long long value)
+{
+ stat_ev_tim_push();
+ stat_ev_printf('E', name, "%llu", value);
+ stat_ev_tim_pop(NULL);
+}
+
+void (stat_ev_ull)(const char *name, unsigned long long value)
+{
+ stat_ev_ull_(name, value);
+}
+
+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)(const char *name)
+{
+ stat_ev_(name);
+}
+