1 /**********************************************************************
5 created at: Tue Feb 21 16:51:18 2017
7 Copyright (C) 2017 Koichi Sasada
9 **********************************************************************/
11 #include "debug_counter.h"
15 #include "ruby/thread_native.h"
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
))
35 debug_counter_setup(void)
37 rb_nativethread_lock_initialize(&debug_counter_lock
);
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.
54 ruby_debug_counter_reset(void)
56 for (int i
= 0; i
< RB_DEBUG_COUNTER_MAX
; 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.
65 rb_debug_counter
[i
] = 0;
71 // note that this operation is not atomic.
73 ruby_debug_counter_get(const char **names_ptr
, size_t *counters_ptr
)
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
;
91 ruby_debug_counter_show_at_exit(int enable
)
93 debug_counter_disable_show_at_exit
= !enable
;
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) {
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
]);
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
);
123 rb_debug_counter_reset(RB_UNUSED_VAR(VALUE klass
))
125 ruby_debug_counter_reset();
129 __attribute__((destructor
))
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.");
141 rb_debug_counter_show_results(const char *msg
)
146 ruby_debug_counter_get(const char **names_ptr
, size_t *counters_ptr
)
151 ruby_debug_counter_reset(void)
156 ruby_debug_counter_show_at_exit(int enable
)
160 #endif /* USE_DEBUG_COUNTER */