Ruby: debug_counter.c Source File

Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)

debug_counter.c
1/**********************************************************************
2
3 debug_counter.c -
4
5 created at: Tue Feb 21 16:51:18 2017
6
7 Copyright (C) 2017 Koichi Sasada
8
9**********************************************************************/
10
11#include "debug_counter.h"
12#include "internal.h"
13#include <stdio.h>
14#include <locale.h>
15#include "ruby/thread_native.h"
16
17#if USE_DEBUG_COUNTER
18
19const char *const rb_debug_counter_names[] = {
20#define DEBUG_COUNTER_NAME_EMPTY "" /* Suppress -Wstring-concatenation */
21 DEBUG_COUNTER_NAME_EMPTY
22#undef DEBUG_COUNTER_NAME_EMPTY
23#define RB_DEBUG_COUNTER(name) #name,
24#include "debug_counter.h"
25#undef RB_DEBUG_COUNTER
26};
27
28size_t rb_debug_counter[numberof(rb_debug_counter_names)];
29void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add);
30
31static rb_nativethread_lock_t debug_counter_lock;
32
33 __attribute__((constructor))
34static void
35debug_counter_setup(void)
36{
37 rb_nativethread_lock_initialize(&debug_counter_lock);
38}
39
40void
41rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add)
42{
43 rb_nativethread_lock_lock(&debug_counter_lock);
44 {
45 rb_debug_counter[(int)type] += add;
46 }
47 rb_nativethread_lock_unlock(&debug_counter_lock);
48}
49
50static int debug_counter_disable_show_at_exit = 0;
51
52// note that this operation is not atomic.
53void
54ruby_debug_counter_reset(void)
55{
56 for (int i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
57 rb_debug_counter[i] = 0;
58 }
59}
60
61// note that this operation is not atomic.
62size_t
63ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
64{
65 int i;
66 if (names_ptr != NULL) {
67 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
68 names_ptr[i] = rb_debug_counter_names[i];
69 }
70 }
71 if (counters_ptr != NULL) {
72 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
73 counters_ptr[i] = rb_debug_counter[i];
74 }
75 }
76
77 return RB_DEBUG_COUNTER_MAX;
78}
79
80void
81ruby_debug_counter_show_at_exit(int enable)
82{
83 debug_counter_disable_show_at_exit = !enable;
84}
85
86void
87rb_debug_counter_show_results(const char *msg)
88{
89 const char *env = getenv("RUBY_DEBUG_COUNTER_DISABLE");
90
91 setlocale(LC_NUMERIC, "");
92
93 if (env == NULL || strcmp("1", env) != 0) {
94 int i;
95 fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%d %s\n", getpid(), msg);
96 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
97 fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%-30s\t%'14"PRIuSIZE"\n",
98 rb_debug_counter_names[i],
99 rb_debug_counter[i]);
100 }
101 }
102}
103
104 VALUE
105rb_debug_counter_show(RB_UNUSED_VAR(VALUE klass))
106{
107 rb_debug_counter_show_results("show_debug_counters");
108 ruby_debug_counter_show_at_exit(FALSE);
109 return Qnil;
110}
111
112 VALUE
113rb_debug_counter_reset(RB_UNUSED_VAR(VALUE klass))
114{
115 ruby_debug_counter_reset();
116 return Qnil;
117}
118
119 __attribute__((destructor))
120static void
121debug_counter_show_results_at_exit(void)
122{
123 if (debug_counter_disable_show_at_exit == 0) {
124 rb_debug_counter_show_results("normal exit.");
125 }
126}
127
128#else
129
130void
131rb_debug_counter_show_results(const char *msg)
132{
133}
134
135size_t
136ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
137{
138 return 0;
139}
140void
141ruby_debug_counter_reset(void)
142{
143}
144
145void
146ruby_debug_counter_show_at_exit(int enable)
147{
148}
149
150#endif /* USE_DEBUG_COUNTER */
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
thread_native.h
rb_nativethread_lock_lock
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
Blocks until the current thread obtains a lock.
Definition thread.c:300
rb_nativethread_lock_unlock
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
Releases a lock.
Definition thread.c:306
rb_nativethread_lock_initialize
void rb_nativethread_lock_initialize(rb_nativethread_lock_t *lock)
Fills the passed lock with an initial value.
Definition thread.c:288
__attribute__
Definition vm_insnhelper.c:2292
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40