Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
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
17 static 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 
25 size_t rb_debug_counter[numberof(debug_counter_names)];
27 
28 int debug_counter_disable_show_at_exit = 0;
29 
30 // note that this operation is not atomic.
31 void
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.
50 size_t
51 ruby_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 
65  return RB_DEBUG_COUNTER_MAX;
66 }
67 
68 void
70 {
71  debug_counter_disable_show_at_exit = !enable;
72 }
73 
74 void
75 rb_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 
92 VALUE
93 rb_debug_counter_show(RB_UNUSED_VAR(VALUE klass))
94 {
95  rb_debug_counter_show_results("method call");
96  return Qnil;
97 }
98 
99 VALUE
100 rb_debug_counter_reset(RB_UNUSED_VAR(VALUE klass))
101 {
103  return Qnil;
104 }
105 
106 __attribute__((destructor))
107 static void
108 debug_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
115 void
117 {
118 }
119 
120 size_t
121 ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
122 {
123  return 0;
124 }
125 void
127 {
128 }
129 
130 void
132 {
133 }
134 
135 #endif /* USE_DEBUG_COUNTER */
strcmp
int strcmp(const char *, const char *)
__attribute__
unsigned int UINT8 __attribute__((__mode__(__QI__)))
Definition: ffi_common.h:110
i
uint32_t i
Definition: rb_mjit_min_header-2.7.1.h:5464
ruby_debug_counter_get
size_t ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
Definition: debug_counter.c:121
klass
VALUE klass
Definition: rb_mjit_min_header-2.7.1.h:13259
env
#define env
RB_DEBUG_COUNTER_mjit_length_active_units
@ RB_DEBUG_COUNTER_mjit_length_active_units
Definition: rb_mjit_min_header-2.7.1.h:11703
MJIT_SYMBOL_EXPORT_END
#define MJIT_SYMBOL_EXPORT_END
Definition: defines.h:398
RB_DEBUG_COUNTER_mjit_length_compact_units
@ RB_DEBUG_COUNTER_mjit_length_compact_units
Definition: rb_mjit_min_header-2.7.1.h:11704
VALUE
unsigned long VALUE
Definition: ruby.h:102
getenv
#define getenv(name)
Definition: win32.c:73
getpid
pid_t getpid(void)
NULL
#define NULL
Definition: _sdbm.c:101
RB_UNUSED_VAR
#define RB_UNUSED_VAR(x)
Definition: ruby.h:591
PRIuSIZE
#define PRIuSIZE
Definition: ruby.h:208
ruby_debug_counter_reset
void ruby_debug_counter_reset(void)
Definition: debug_counter.c:126
RB_DEBUG_COUNTER_MAX
@ RB_DEBUG_COUNTER_MAX
Definition: debug_counter.h:351
RB_DEBUG_COUNTER_mjit_length_stale_units
@ RB_DEBUG_COUNTER_mjit_length_stale_units
Definition: rb_mjit_min_header-2.7.1.h:11705
rb_debug_counter_show_results
void rb_debug_counter_show_results(const char *msg)
Definition: debug_counter.c:116
internal.h
debug_counter.h
MJIT_SYMBOL_EXPORT_BEGIN
#define MJIT_SYMBOL_EXPORT_BEGIN
Definition: defines.h:397
stderr
#define stderr
Definition: rb_mjit_min_header-2.7.1.h:1485
Qnil
#define Qnil
Definition: ruby.h:469
ruby_debug_counter_show_at_exit
void ruby_debug_counter_show_at_exit(int enable)
Definition: debug_counter.c:131
numberof
#define numberof(array)
Definition: etc.c:618
RB_DEBUG_COUNTER_mjit_length_unit_queue
@ RB_DEBUG_COUNTER_mjit_length_unit_queue
Definition: rb_mjit_min_header-2.7.1.h:11702
fprintf
int fprintf(FILE *__restrict, const char *__restrict,...) __attribute__((__format__(__printf__