Fix typos [ci skip]
[ruby-80x24.org.git] / debug_counter.c
blobe7b0bb0acd9635b0e41a3c1c0785eca2207519b6
1 /**********************************************************************
3 debug_counter.c -
5 created at: Tue Feb 21 16:51:18 2017
7 Copyright (C) 2017 Koichi Sasada
9 **********************************************************************/
11 #include "debug_counter.h"
12 #include "internal.h"
13 #include <stdio.h>
14 #include <locale.h>
15 #include "ruby/thread_native.h"
17 #if USE_DEBUG_COUNTER
19 static const char *const debug_counter_names[] = {
21 #define RB_DEBUG_COUNTER(name) #name,
22 #include "debug_counter.h"
23 #undef RB_DEBUG_COUNTER
26 MJIT_SYMBOL_EXPORT_BEGIN
27 size_t rb_debug_counter[numberof(debug_counter_names)];
28 void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add);
29 MJIT_SYMBOL_EXPORT_END
31 rb_nativethread_lock_t debug_counter_lock;
33 __attribute__((constructor))
34 static void
35 debug_counter_setup(void)
37 rb_nativethread_lock_initialize(&debug_counter_lock);
40 void
41 rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add)
43 rb_nativethread_lock_lock(&debug_counter_lock);
45 rb_debug_counter[(int)type] += add;
47 rb_nativethread_lock_unlock(&debug_counter_lock);
50 int debug_counter_disable_show_at_exit = 0;
52 // note that this operation is not atomic.
53 void
54 ruby_debug_counter_reset(void)
56 for (int i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
57 switch (i) {
58 case RB_DEBUG_COUNTER_mjit_length_unit_queue:
59 case RB_DEBUG_COUNTER_mjit_length_active_units:
60 case RB_DEBUG_COUNTER_mjit_length_compact_units:
61 case RB_DEBUG_COUNTER_mjit_length_stale_units:
62 // These counters may be decreased and should not be reset.
63 break;
64 default:
65 rb_debug_counter[i] = 0;
66 break;
71 // note that this operation is not atomic.
72 size_t
73 ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
75 int i;
76 if (names_ptr != NULL) {
77 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
78 names_ptr[i] = debug_counter_names[i];
81 if (counters_ptr != NULL) {
82 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
83 counters_ptr[i] = rb_debug_counter[i];
87 return RB_DEBUG_COUNTER_MAX;
90 void
91 ruby_debug_counter_show_at_exit(int enable)
93 debug_counter_disable_show_at_exit = !enable;
96 void
97 rb_debug_counter_show_results(const char *msg)
99 const char *env = getenv("RUBY_DEBUG_COUNTER_DISABLE");
101 setlocale(LC_NUMERIC, "");
103 if (env == NULL || strcmp("1", env) != 0) {
104 int i;
105 fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%d %s\n", getpid(), msg);
106 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
107 fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%-30s\t%'14"PRIuSIZE"\n",
108 debug_counter_names[i],
109 rb_debug_counter[i]);
114 VALUE
115 rb_debug_counter_show(RB_UNUSED_VAR(VALUE klass))
117 rb_debug_counter_show_results("show_debug_counters");
118 ruby_debug_counter_show_at_exit(FALSE);
119 return Qnil;
122 VALUE
123 rb_debug_counter_reset(RB_UNUSED_VAR(VALUE klass))
125 ruby_debug_counter_reset();
126 return Qnil;
129 __attribute__((destructor))
130 static void
131 debug_counter_show_results_at_exit(void)
133 if (debug_counter_disable_show_at_exit == 0) {
134 rb_debug_counter_show_results("normal exit.");
138 #else
140 void
141 rb_debug_counter_show_results(const char *msg)
145 size_t
146 ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
148 return 0;
150 void
151 ruby_debug_counter_reset(void)
155 void
156 ruby_debug_counter_show_at_exit(int enable)
160 #endif /* USE_DEBUG_COUNTER */