Ruby 4.1.0dev (2026-11-16 revision 67814cf9b0f3a4bad6f7369b0c8e6fc69220de64)
debug_counter.h (67814cf9b0f3a4bad6f7369b0c8e6fc69220de64)
1/**********************************************************************
2
3 debug_counter.h -
4
5 created at: Tue Feb 21 16:51:18 2017
6
7 Copyright (C) 2017 Koichi Sasada
8
9**********************************************************************/
10
11#ifndef USE_DEBUG_COUNTER
12#define USE_DEBUG_COUNTER 0
13#endif
14
15#ifdef RB_DEBUG_COUNTER
16
17// method cache (IMC: inline method cache)
18RB_DEBUG_COUNTER(mc_inline_hit) // IMC hit
19RB_DEBUG_COUNTER(mc_inline_miss_klass) // IMC miss by different class
20RB_DEBUG_COUNTER(mc_inline_miss_invalidated) // IMC miss by invalidated ME
21RB_DEBUG_COUNTER(mc_inline_miss_empty) // IMC miss because prev is empty slot
22RB_DEBUG_COUNTER(mc_inline_miss_same_cc) // IMC miss, but same CC
23RB_DEBUG_COUNTER(mc_inline_miss_same_cme) // IMC miss, but same CME
24RB_DEBUG_COUNTER(mc_inline_miss_same_def) // IMC miss, but same definition
25RB_DEBUG_COUNTER(mc_inline_miss_diff) // IMC miss, different methods
26
27RB_DEBUG_COUNTER(cvar_write_inline_hit) // cvar cache hit on write
28RB_DEBUG_COUNTER(cvar_read_inline_hit) // cvar cache hit on read
29RB_DEBUG_COUNTER(cvar_inline_miss) // miss inline cache
30RB_DEBUG_COUNTER(cvar_class_invalidate) // invalidate cvar cache when define a cvar that's defined on a subclass
31RB_DEBUG_COUNTER(cvar_include_invalidate) // invalidate cvar cache on module include or prepend
32
33RB_DEBUG_COUNTER(mc_cme_complement) // number of acquiring complement CME
34RB_DEBUG_COUNTER(mc_cme_complement_hit) // number of cache hit for complemented CME
35
36RB_DEBUG_COUNTER(mc_search) // count for method lookup in class tree
37RB_DEBUG_COUNTER(mc_search_notfound) // method lookup, but not found
38RB_DEBUG_COUNTER(mc_search_super) // total traversed classes
39
40// callinfo
41RB_DEBUG_COUNTER(ci_packed) // number of packed CI
42RB_DEBUG_COUNTER(ci_kw) // non-packed CI w/ keywords
43RB_DEBUG_COUNTER(ci_nokw) // non-packed CI w/o keywords
44RB_DEBUG_COUNTER(ci_runtime) // creating temporary CI
45
46// callcache
47RB_DEBUG_COUNTER(cc_new) // number of CC
48RB_DEBUG_COUNTER(cc_temp) // dummy CC (stack-allocated)
49RB_DEBUG_COUNTER(cc_found_in_ccs) // count for CC lookup success in CCS
50RB_DEBUG_COUNTER(cc_not_found_in_ccs) // count for CC lookup success in CCS
51
52RB_DEBUG_COUNTER(cc_ent_invalidate) // count for invalidating cc (cc->klass = Qundef)
53RB_DEBUG_COUNTER(cc_cme_invalidate) // count for invalidating CME
54
55RB_DEBUG_COUNTER(cc_invalidate_leaf) // count for invalidating klass if klass has no-subclasses
56RB_DEBUG_COUNTER(cc_invalidate_leaf_ccs) // corresponding CCS
57RB_DEBUG_COUNTER(cc_invalidate_leaf_callable) // complimented cache (no-subclasses)
58RB_DEBUG_COUNTER(cc_invalidate_tree) // count for invalidating klass if klass has subclasses
59RB_DEBUG_COUNTER(cc_invalidate_tree_cme) // cme if cme is found in this class or superclasses
60RB_DEBUG_COUNTER(cc_invalidate_tree_callable) // complimented cache (subclasses)
61RB_DEBUG_COUNTER(cc_invalidate_negative) // count for invalidating negative cache
62
63RB_DEBUG_COUNTER(ccs_free) // count for free'ing ccs
64RB_DEBUG_COUNTER(ccs_maxlen) // maximum length of ccs
65RB_DEBUG_COUNTER(ccs_found) // count for finding corresponding ccs on method lookup
66RB_DEBUG_COUNTER(ccs_not_found) // count for not found corresponding ccs on method lookup
67
68// vm_eval.c
69RB_DEBUG_COUNTER(call0_public)
70RB_DEBUG_COUNTER(call0_other)
71RB_DEBUG_COUNTER(gccct_hit)
72RB_DEBUG_COUNTER(gccct_miss)
73RB_DEBUG_COUNTER(gccct_null)
74
75// iseq
76RB_DEBUG_COUNTER(iseq_num) // number of total created iseq
77RB_DEBUG_COUNTER(iseq_cd_num) // number of total created cd (call_data)
78
79/*
80 * call cache fastpath usage
81 */
82RB_DEBUG_COUNTER(ccf_general)
83RB_DEBUG_COUNTER(ccf_iseq_setup)
84RB_DEBUG_COUNTER(ccf_iseq_setup_0start)
85RB_DEBUG_COUNTER(ccf_iseq_setup_tailcall_0start)
86RB_DEBUG_COUNTER(ccf_iseq_fix) /* several functions created with tool/mk_call_iseq_optimized.rb */
87RB_DEBUG_COUNTER(ccf_iseq_opt) /* has_opt == TRUE (has optional parameters), but other flags are FALSE */
88RB_DEBUG_COUNTER(ccf_iseq_kw1) /* vm_call_iseq_setup_kwparm_kwarg() */
89RB_DEBUG_COUNTER(ccf_iseq_kw2) /* vm_call_iseq_setup_kwparm_nokwarg() */
90RB_DEBUG_COUNTER(ccf_cfunc)
91RB_DEBUG_COUNTER(ccf_cfunc_with_frame)
92RB_DEBUG_COUNTER(ccf_ivar) /* attr_reader */
93RB_DEBUG_COUNTER(ccf_attrset) /* attr_writer */
94RB_DEBUG_COUNTER(ccf_method_missing)
95RB_DEBUG_COUNTER(ccf_zsuper)
96RB_DEBUG_COUNTER(ccf_bmethod)
97RB_DEBUG_COUNTER(ccf_opt_send)
98RB_DEBUG_COUNTER(ccf_opt_call)
99RB_DEBUG_COUNTER(ccf_opt_block_call)
100RB_DEBUG_COUNTER(ccf_opt_struct_aref)
101RB_DEBUG_COUNTER(ccf_opt_struct_aset)
102RB_DEBUG_COUNTER(ccf_super_method)
103RB_DEBUG_COUNTER(ccf_cfunc_other)
104RB_DEBUG_COUNTER(ccf_cfunc_only_splat)
105RB_DEBUG_COUNTER(ccf_cfunc_only_splat_kw)
106RB_DEBUG_COUNTER(ccf_iseq_bmethod)
107RB_DEBUG_COUNTER(ccf_noniseq_bmethod)
108RB_DEBUG_COUNTER(ccf_opt_send_complex)
109RB_DEBUG_COUNTER(ccf_opt_send_simple)
110RB_DEBUG_COUNTER(ccf_uncached)
111
112/*
113 * control frame push counts.
114 *
115 * * frame_push: frame push counts.
116 * * frame_push_*: frame push counts per each type.
117 * * frame_R2R: Ruby frame to Ruby frame
118 * * frame_R2C: Ruby frame to C frame
119 * * frame_C2C: C frame to C frame
120 * * frame_C2R: C frame to Ruby frame
121 */
122RB_DEBUG_COUNTER(frame_push)
123RB_DEBUG_COUNTER(frame_push_method)
124RB_DEBUG_COUNTER(frame_push_block)
125RB_DEBUG_COUNTER(frame_push_class)
126RB_DEBUG_COUNTER(frame_push_top)
127RB_DEBUG_COUNTER(frame_push_cfunc)
128RB_DEBUG_COUNTER(frame_push_ifunc)
129RB_DEBUG_COUNTER(frame_push_eval)
130RB_DEBUG_COUNTER(frame_push_rescue)
131RB_DEBUG_COUNTER(frame_push_dummy)
132
133RB_DEBUG_COUNTER(frame_R2R)
134RB_DEBUG_COUNTER(frame_R2C)
135RB_DEBUG_COUNTER(frame_C2C)
136RB_DEBUG_COUNTER(frame_C2R)
137
138/* instance variable counts */
139RB_DEBUG_COUNTER(ivar_get_obj_hit) // Only T_OBJECT hits
140RB_DEBUG_COUNTER(ivar_get_obj_miss) // Only T_OBJECT misses
141RB_DEBUG_COUNTER(ivar_get_ic_hit) // All hits
142RB_DEBUG_COUNTER(ivar_get_ic_miss) // All misses
143RB_DEBUG_COUNTER(ivar_set_ic_hit) // All hits
144RB_DEBUG_COUNTER(ivar_set_obj_hit) // Only T_OBJECT hits
145RB_DEBUG_COUNTER(ivar_set_obj_miss) // Only T_OBJECT misses
146RB_DEBUG_COUNTER(ivar_set_ic_miss) // All misses
147RB_DEBUG_COUNTER(ivar_set_ic_miss_noobject) // Miss because non T_OBJECT
148RB_DEBUG_COUNTER(ivar_get_base) // Calls to `rb_ivar_get` (very slow path)
149RB_DEBUG_COUNTER(ivar_set_base) // Calls to `ivar_set` (very slow path)
150RB_DEBUG_COUNTER(ivar_get_ic_miss_set) // Misses on IV reads where the cache was wrong
151RB_DEBUG_COUNTER(ivar_get_cc_miss_set) // Misses on attr_reader where the cache was wrong
152RB_DEBUG_COUNTER(ivar_get_ic_miss_unset) // Misses on IV read where the cache wasn't set
153RB_DEBUG_COUNTER(ivar_get_cc_miss_unset) // Misses on attr_reader where the cache wasn't set
154
155/* local variable counts
156 *
157 * * lvar_get: total lvar get counts (VM insn)
158 * * lvar_get_dynamic: lvar get counts if accessing upper env (VM insn)
159 * * lvar_set*: same as "get"
160 * * lvar_set_slowpath: counts using vm_env_write_slowpath()
161 */
162RB_DEBUG_COUNTER(lvar_get)
163RB_DEBUG_COUNTER(lvar_get_dynamic)
164RB_DEBUG_COUNTER(lvar_set)
165RB_DEBUG_COUNTER(lvar_set_dynamic)
166RB_DEBUG_COUNTER(lvar_set_slowpath)
167
168/* GC counts:
169 *
170 * * count: simple count
171 * * _minor: minor gc
172 * * _major: major gc
173 * * other suffix is corresponding to last_gc_info or
174 * gc_profile_record_flag in gc.c.
175 */
176RB_DEBUG_COUNTER(gc_count)
177RB_DEBUG_COUNTER(gc_minor_newobj)
178RB_DEBUG_COUNTER(gc_minor_malloc)
179RB_DEBUG_COUNTER(gc_minor_method)
180RB_DEBUG_COUNTER(gc_minor_capi)
181RB_DEBUG_COUNTER(gc_minor_stress)
182RB_DEBUG_COUNTER(gc_major_nofree)
183RB_DEBUG_COUNTER(gc_major_oldgen)
184RB_DEBUG_COUNTER(gc_major_shady)
185RB_DEBUG_COUNTER(gc_major_force)
186RB_DEBUG_COUNTER(gc_major_oldmalloc)
187
188RB_DEBUG_COUNTER(gc_enter_start)
189RB_DEBUG_COUNTER(gc_enter_continue)
190RB_DEBUG_COUNTER(gc_enter_rest)
191RB_DEBUG_COUNTER(gc_enter_finalizer)
192
193RB_DEBUG_COUNTER(gc_isptr_trial)
194RB_DEBUG_COUNTER(gc_isptr_range)
195RB_DEBUG_COUNTER(gc_isptr_align)
196RB_DEBUG_COUNTER(gc_isptr_maybe)
197
198/* object allocation counts:
199 *
200 * * obj_newobj: newobj counts
201 * * obj_newobj_slowpath: newobj with slowpath counts
202 * * obj_newobj_wb_unprotected: newobj for wb_unprotected.
203 * * obj_free: obj_free() counts
204 * * obj_promote: promoted counts (oldgen)
205 * * obj_wb_unprotect: wb unprotect counts
206 *
207 * * obj_[type]_[attr]: *free'ed counts* for each type.
208 * Note that it is not a allocated counts.
209 * * [type]
210 * * _obj: T_OBJECT
211 * * _str: T_STRING
212 * * _ary: T_ARRAY
213 * * _xxx: T_XXX (hash, struct, ...)
214 *
215 * * [attr]
216 * * _ptr: R?? is not embed.
217 * * _embed: R?? is embed.
218 * * type specific attr.
219 * * str_shared: str is shared.
220 * * str_nofree: nofree
221 * * str_fstr: fstr
222 * * hash_empty: hash is empty
223 * * hash_1_4: has 1 to 4 entries
224 * * hash_5_8: has 5 to 8 entries
225 * * hash_g8: has n entries (n>8)
226 * * match_under4: has under 4 oniguruma regions allocated
227 * * match_ge4: has n regions allocated (4<=n<8)
228 * * match_ge8: has n regions allocated (8<=n)
229 * * data_empty: T_DATA but no memory free.
230 * * data_xfree: free'ed by xfree().
231 * * data_imm_free: free'ed immediately.
232 * * data_zombie: free'ed with zombie.
233 * * imemo_*: T_IMEMO with each type.
234 */
235RB_DEBUG_COUNTER(obj_newobj)
236RB_DEBUG_COUNTER(obj_newobj_slowpath)
237RB_DEBUG_COUNTER(obj_newobj_wb_unprotected)
238RB_DEBUG_COUNTER(obj_free)
239RB_DEBUG_COUNTER(obj_promote)
240RB_DEBUG_COUNTER(obj_wb_unprotect)
241
242RB_DEBUG_COUNTER(obj_obj_embed)
243RB_DEBUG_COUNTER(obj_obj_ptr)
244RB_DEBUG_COUNTER(obj_obj_complex)
245
246RB_DEBUG_COUNTER(obj_str_ptr)
247RB_DEBUG_COUNTER(obj_str_embed)
248RB_DEBUG_COUNTER(obj_str_shared)
249RB_DEBUG_COUNTER(obj_str_nofree)
250RB_DEBUG_COUNTER(obj_str_fstr)
251
252RB_DEBUG_COUNTER(obj_ary_embed)
253RB_DEBUG_COUNTER(obj_ary_ptr)
254RB_DEBUG_COUNTER(obj_ary_extracapa)
255/*
256 ary_shared_create: shared ary by Array#dup and so on.
257 ary_shared: finished in shard.
258 ary_shared_root_occupied: shared_root but has only 1 refcnt.
259 The number (ary_shared - ary_shared_root_occupied) is meaningful.
260 */
261RB_DEBUG_COUNTER(obj_ary_shared_create)
262RB_DEBUG_COUNTER(obj_ary_shared)
263RB_DEBUG_COUNTER(obj_ary_shared_root_occupied)
264
265RB_DEBUG_COUNTER(obj_hash_empty)
266RB_DEBUG_COUNTER(obj_hash_1)
267RB_DEBUG_COUNTER(obj_hash_2)
268RB_DEBUG_COUNTER(obj_hash_3)
269RB_DEBUG_COUNTER(obj_hash_4)
270RB_DEBUG_COUNTER(obj_hash_5_8)
271RB_DEBUG_COUNTER(obj_hash_g8)
272
273RB_DEBUG_COUNTER(obj_hash_null)
274RB_DEBUG_COUNTER(obj_hash_ar)
275RB_DEBUG_COUNTER(obj_hash_st)
276RB_DEBUG_COUNTER(obj_hash_force_convert)
277
278RB_DEBUG_COUNTER(obj_struct_embed)
279RB_DEBUG_COUNTER(obj_struct_ptr)
280
281RB_DEBUG_COUNTER(obj_data_empty)
282RB_DEBUG_COUNTER(obj_data_xfree)
283RB_DEBUG_COUNTER(obj_data_imm_free)
284RB_DEBUG_COUNTER(obj_data_zombie)
285
286RB_DEBUG_COUNTER(obj_match_under4)
287RB_DEBUG_COUNTER(obj_match_ge4)
288RB_DEBUG_COUNTER(obj_match_ge8)
289RB_DEBUG_COUNTER(obj_match_ptr)
290
291RB_DEBUG_COUNTER(obj_iclass_ptr)
292RB_DEBUG_COUNTER(obj_class_ptr)
293RB_DEBUG_COUNTER(obj_module_ptr)
294
295RB_DEBUG_COUNTER(obj_bignum_ptr)
296RB_DEBUG_COUNTER(obj_bignum_embed)
297RB_DEBUG_COUNTER(obj_float)
298RB_DEBUG_COUNTER(obj_complex)
299RB_DEBUG_COUNTER(obj_rational)
300
301RB_DEBUG_COUNTER(obj_regexp_ptr)
302RB_DEBUG_COUNTER(obj_file_ptr)
303RB_DEBUG_COUNTER(obj_symbol)
304
305RB_DEBUG_COUNTER(obj_imemo_ment)
306RB_DEBUG_COUNTER(obj_imemo_iseq)
307RB_DEBUG_COUNTER(obj_imemo_env)
308RB_DEBUG_COUNTER(obj_imemo_tmpbuf)
309RB_DEBUG_COUNTER(obj_imemo_cvar_entry)
310RB_DEBUG_COUNTER(obj_imemo_cref)
311RB_DEBUG_COUNTER(obj_imemo_svar)
312RB_DEBUG_COUNTER(obj_imemo_throw_data)
313RB_DEBUG_COUNTER(obj_imemo_ifunc)
314RB_DEBUG_COUNTER(obj_imemo_memo)
315RB_DEBUG_COUNTER(obj_imemo_callinfo)
316RB_DEBUG_COUNTER(obj_imemo_callcache)
317RB_DEBUG_COUNTER(obj_imemo_constcache)
318RB_DEBUG_COUNTER(obj_imemo_fields)
319RB_DEBUG_COUNTER(obj_imemo_subclasses)
320RB_DEBUG_COUNTER(obj_imemo_cdhash)
321
322RB_DEBUG_COUNTER(opt_new_hit)
323RB_DEBUG_COUNTER(opt_new_miss)
324
325/* ar_table */
326RB_DEBUG_COUNTER(artable_hint_hit)
327RB_DEBUG_COUNTER(artable_hint_miss)
328RB_DEBUG_COUNTER(artable_hint_notfound)
329
330/* heap function counts
331 *
332 * * heap_xmalloc/realloc/xfree: call counts
333 */
334RB_DEBUG_COUNTER(heap_xmalloc)
335RB_DEBUG_COUNTER(heap_xrealloc)
336RB_DEBUG_COUNTER(heap_xfree)
337
338// VM sync
339RB_DEBUG_COUNTER(vm_sync_lock)
340RB_DEBUG_COUNTER(vm_sync_lock_enter)
341RB_DEBUG_COUNTER(vm_sync_lock_enter_nb)
342RB_DEBUG_COUNTER(vm_sync_lock_enter_cr)
343RB_DEBUG_COUNTER(vm_sync_barrier)
344
345/* load (not implemented yet) */
346/*
347RB_DEBUG_COUNTER(load_files)
348RB_DEBUG_COUNTER(load_path_is_not_realpath)
349*/
350#endif
351
352#ifndef RUBY_DEBUG_COUNTER_H
353#define RUBY_DEBUG_COUNTER_H 1
354
355#include "ruby/internal/config.h"
356#include <stddef.h> /* for size_t */
357#include "ruby/ruby.h" /* for VALUE */
358
359#if !defined(__GNUC__) && USE_DEBUG_COUNTER
360#error "USE_DEBUG_COUNTER is not supported by other than __GNUC__"
361#endif
362
363enum rb_debug_counter_type {
364#define RB_DEBUG_COUNTER(name) RB_DEBUG_COUNTER_##name,
365#include "debug_counter.h"
366 RB_DEBUG_COUNTER_MAX
367#undef RB_DEBUG_COUNTER
368};
369
370#if USE_DEBUG_COUNTER
371extern size_t rb_debug_counter[];
372RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor;
373RUBY_EXTERN void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add);
374
375inline static int
376rb_debug_counter_add(enum rb_debug_counter_type type, int add, int cond)
377{
378 if (cond) {
379 if (ruby_single_main_ractor != NULL) {
380 rb_debug_counter[(int)type] += add;
381 }
382 else {
383 rb_debug_counter_add_atomic(type, add);
384 }
385 }
386 return cond;
387}
388
389inline static int
390rb_debug_counter_max(enum rb_debug_counter_type type, unsigned int num)
391{
392 // TODO: sync
393 if (rb_debug_counter[(int)type] < num) {
394 rb_debug_counter[(int)type] = num;
395 return 1;
396 }
397 else {
398 return 0;
399 }
400}
401
402VALUE rb_debug_counter_reset(VALUE klass);
403VALUE rb_debug_counter_show(VALUE klass);
404
405#define RB_DEBUG_COUNTER_INC(type) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, 1)
406#define RB_DEBUG_COUNTER_INC_UNLESS(type, cond) (!rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, !(cond)))
407#define RB_DEBUG_COUNTER_INC_IF(type, cond) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, !!(cond))
408#define RB_DEBUG_COUNTER_ADD(type, num) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, (num), 1)
409#define RB_DEBUG_COUNTER_SETMAX(type, num) rb_debug_counter_max(RB_DEBUG_COUNTER_##type, (unsigned int)(num))
410
411#else
412#define RB_DEBUG_COUNTER_INC(type) ((void)0)
413#define RB_DEBUG_COUNTER_INC_UNLESS(type, cond) (!!(cond))
414#define RB_DEBUG_COUNTER_INC_IF(type, cond) (!!(cond))
415#define RB_DEBUG_COUNTER_ADD(type, num) ((void)0)
416#define RB_DEBUG_COUNTER_SETMAX(type, num) 0
417#endif
418
419void rb_debug_counter_show_results(const char *msg);
420
421RUBY_SYMBOL_EXPORT_BEGIN
422
423size_t ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr);
424void ruby_debug_counter_reset(void);
425void ruby_debug_counter_show_at_exit(int enable);
426
427RUBY_SYMBOL_EXPORT_END
428
429#endif /* RUBY_DEBUG_COUNTER_H */
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
VALUE type(ANYARGS)
ANYARGS-ed function type.
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40