summaryrefslogtreecommitdiff
path: root/debug_counter.c
blob: 60cc2c3ac14803b88a3f39083c5e7d3bd2392552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**********************************************************************

  debug_counter.c -

  created at: Tue Feb 21 16:51:18 2017

  Copyright (C) 2017 Koichi Sasada

**********************************************************************/

#include "debug_counter.h"
#if USE_DEBUG_COUNTER
#include <stdio.h>
#include <locale.h>
#include "internal.h"

static const char *const debug_counter_names[] = {
    ""
#define RB_DEBUG_COUNTER(name) #name,
#include "debug_counter.h"
#undef RB_DEBUG_COUNTER
};

MJIT_SYMBOL_EXPORT_BEGIN
size_t rb_debug_counter[numberof(debug_counter_names)];
MJIT_SYMBOL_EXPORT_END

void
rb_debug_counter_show_results(const char *msg)
{
    const char *env = getenv("RUBY_DEBUG_COUNTER_DISABLE");

    setlocale(LC_NUMERIC, "");

    if (env == NULL || strcmp("1", env) != 0) {
	int i;
        fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%d %s\n", getpid(), msg);
	for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
            fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%-30s\t%'14"PRIuSIZE"\n",
		    debug_counter_names[i],
		    rb_debug_counter[i]);
	}
    }
}

VALUE
rb_debug_counter_show(RB_UNUSED_VAR(VALUE klass))
{
    rb_debug_counter_show_results("method call");
    return Qnil;
}

VALUE
rb_debug_counter_reset(RB_UNUSED_VAR(VALUE klass))
{
    for (int i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
        switch (i) {
          case RB_DEBUG_COUNTER_mjit_length_unit_queue:
          case RB_DEBUG_COUNTER_mjit_length_active_units:
          case RB_DEBUG_COUNTER_mjit_length_compact_units:
          case RB_DEBUG_COUNTER_mjit_length_stale_units:
            // These counters may be decreased and should not be reset.
            break;
          default:
            rb_debug_counter[i] = 0;
            break;
        }
    }
    return Qnil;
}

__attribute__((destructor))
static void
debug_counter_show_results_at_exit(void)
{
    rb_debug_counter_show_results("normal exit.");
}
#else
void
rb_debug_counter_show_results(const char *msg)
{
}
#endif /* USE_DEBUG_COUNTER */