Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
debug_counter.c
Go to the documentation of this file.
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 "internal.h"
12#include "debug_counter.h"
13#include <stdio.h>
14#include <locale.h>
15
16#if USE_DEBUG_COUNTER
17static const char *const debug_counter_names[] = {
18 ""
19#define RB_DEBUG_COUNTER(name) #name,
20#include "debug_counter.h"
21#undef RB_DEBUG_COUNTER
22};
23
25size_t rb_debug_counter[numberof(debug_counter_names)];
27
28int debug_counter_disable_show_at_exit = 0;
29
30// note that this operation is not atomic.
31void
33{
34 for (int i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
35 switch (i) {
40 // These counters may be decreased and should not be reset.
41 break;
42 default:
43 rb_debug_counter[i] = 0;
44 break;
45 }
46 }
47}
48
49// note that this operation is not atomic.
50size_t
51ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
52{
53 int i;
54 if (names_ptr != NULL) {
55 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
56 names_ptr[i] = debug_counter_names[i];
57 }
58 }
59 if (counters_ptr != NULL) {
60 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
61 counters_ptr[i] = rb_debug_counter[i];
62 }
63 }
64
66}
67
68void
70{
71 debug_counter_disable_show_at_exit = !enable;
72}
73
74void
75rb_debug_counter_show_results(const char *msg)
76{
77 const char *env = getenv("RUBY_DEBUG_COUNTER_DISABLE");
78
79 setlocale(LC_NUMERIC, "");
80
81 if (env == NULL || strcmp("1", env) != 0) {
82 int i;
83 fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%d %s\n", getpid(), msg);
84 for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
85 fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%-30s\t%'14"PRIuSIZE"\n",
86 debug_counter_names[i],
87 rb_debug_counter[i]);
88 }
89 }
90}
91
93rb_debug_counter_show(RB_UNUSED_VAR(VALUE klass))
94{
95 rb_debug_counter_show_results("method call");
96 return Qnil;
97}
98
100rb_debug_counter_reset(RB_UNUSED_VAR(VALUE klass))
101{
103 return Qnil;
104}
105
106__attribute__((destructor))
107static void
108debug_counter_show_results_at_exit(void)
109{
110 if (debug_counter_disable_show_at_exit == 0) {
111 rb_debug_counter_show_results("normal exit.");
112 }
113}
114#else
115void
117{
118}
119
120size_t
121ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
122{
123 return 0;
124}
125void
127{
128}
129
130void
132{
133}
134
135#endif /* USE_DEBUG_COUNTER */
void rb_debug_counter_show_results(const char *msg)
size_t ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
void ruby_debug_counter_show_at_exit(int enable)
void ruby_debug_counter_reset(void)
@ RB_DEBUG_COUNTER_mjit_length_compact_units
@ RB_DEBUG_COUNTER_mjit_length_active_units
@ RB_DEBUG_COUNTER_mjit_length_stale_units
@ RB_DEBUG_COUNTER_mjit_length_unit_queue
@ RB_DEBUG_COUNTER_MAX
#define NULL
int strcmp(const char *, const char *)
#define MJIT_SYMBOL_EXPORT_END
#define PRIuSIZE
#define RB_UNUSED_VAR(x)
#define numberof(array)
int fprintf(FILE *__restrict__, const char *__restrict__,...) __attribute__((__format__(__printf__
#define stderr
uint32_t i
#define Qnil
pid_t getpid(void)
rb_control_frame_t * __attribute__((__fastcall__)) *rb_insn_func_t)(rb_execution_context_t *
#define MJIT_SYMBOL_EXPORT_BEGIN
unsigned long VALUE
Definition: ruby.h:102
#define getenv(name)
Definition: win32.c:73
#define env