Ruby 3.5.0dev (2025-01-10 revision 5fab31b15e32622c4b71d1d347a41937e9f9c212)
debug_counter.h (5fab31b15e32622c4b71d1d347a41937e9f9c212)
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 = 0)
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)
110
111/*
112 * control frame push counts.
113 *
114 * * frame_push: frame push counts.
115 * * frame_push_*: frame push counts per each type.
116 * * frame_R2R: Ruby frame to Ruby frame
117 * * frame_R2C: Ruby frame to C frame
118 * * frame_C2C: C frame to C frame
119 * * frame_C2R: C frame to Ruby frame
120 */
121RB_DEBUG_COUNTER(frame_push)
122RB_DEBUG_COUNTER(frame_push_method)
123RB_DEBUG_COUNTER(frame_push_block)
124RB_DEBUG_COUNTER(frame_push_class)
125RB_DEBUG_COUNTER(frame_push_top)
126RB_DEBUG_COUNTER(frame_push_cfunc)
127RB_DEBUG_COUNTER(frame_push_ifunc)
128RB_DEBUG_COUNTER(frame_push_eval)
129RB_DEBUG_COUNTER(frame_push_rescue)
130RB_DEBUG_COUNTER(frame_push_dummy)
131
132RB_DEBUG_COUNTER(frame_R2R)
133RB_DEBUG_COUNTER(frame_R2C)
134RB_DEBUG_COUNTER(frame_C2C)
135RB_DEBUG_COUNTER(frame_C2R)
136
137/* instance variable counts */
138RB_DEBUG_COUNTER(ivar_get_obj_hit) // Only T_OBJECT hits
139RB_DEBUG_COUNTER(ivar_get_obj_miss) // Only T_OBJECT misses
140RB_DEBUG_COUNTER(ivar_get_ic_hit) // All hits
141RB_DEBUG_COUNTER(ivar_get_ic_miss) // All misses
142RB_DEBUG_COUNTER(ivar_set_ic_hit) // All hits
143RB_DEBUG_COUNTER(ivar_set_obj_hit) // Only T_OBJECT hits
144RB_DEBUG_COUNTER(ivar_set_obj_miss) // Only T_OBJECT misses
145RB_DEBUG_COUNTER(ivar_set_ic_miss) // All misses
146RB_DEBUG_COUNTER(ivar_set_ic_miss_noobject) // Miss because non T_OBJECT
147RB_DEBUG_COUNTER(ivar_get_base) // Calls to `rb_ivar_get` (very slow path)
148RB_DEBUG_COUNTER(ivar_set_base) // Calls to `ivar_set` (very slow path)
149RB_DEBUG_COUNTER(ivar_get_ic_miss_set) // Misses on IV reads where the cache was wrong
150RB_DEBUG_COUNTER(ivar_get_cc_miss_set) // Misses on attr_reader where the cache was wrong
151RB_DEBUG_COUNTER(ivar_get_ic_miss_unset) // Misses on IV read where the cache wasn't set
152RB_DEBUG_COUNTER(ivar_get_cc_miss_unset) // Misses on attr_reader where the cache wasn't set
153
154/* local variable counts
155 *
156 * * lvar_get: total lvar get counts (VM insn)
157 * * lvar_get_dynamic: lvar get counts if accessing upper env (VM insn)
158 * * lvar_set*: same as "get"
159 * * lvar_set_slowpath: counts using vm_env_write_slowpath()
160 */
161RB_DEBUG_COUNTER(lvar_get)
162RB_DEBUG_COUNTER(lvar_get_dynamic)
163RB_DEBUG_COUNTER(lvar_set)
164RB_DEBUG_COUNTER(lvar_set_dynamic)
165RB_DEBUG_COUNTER(lvar_set_slowpath)
166
167/* GC counts:
168 *
169 * * count: simple count
170 * * _minor: minor gc
171 * * _major: major gc
172 * * other suffix is corresponding to last_gc_info or
173 * gc_profile_record_flag in gc.c.
174 */
175RB_DEBUG_COUNTER(gc_count)
176RB_DEBUG_COUNTER(gc_minor_newobj)
177RB_DEBUG_COUNTER(gc_minor_malloc)
178RB_DEBUG_COUNTER(gc_minor_method)
179RB_DEBUG_COUNTER(gc_minor_capi)
180RB_DEBUG_COUNTER(gc_minor_stress)
181RB_DEBUG_COUNTER(gc_major_nofree)
182RB_DEBUG_COUNTER(gc_major_oldgen)
183RB_DEBUG_COUNTER(gc_major_shady)
184RB_DEBUG_COUNTER(gc_major_force)
185RB_DEBUG_COUNTER(gc_major_oldmalloc)
186
187RB_DEBUG_COUNTER(gc_enter_start)
188RB_DEBUG_COUNTER(gc_enter_continue)
189RB_DEBUG_COUNTER(gc_enter_rest)
190RB_DEBUG_COUNTER(gc_enter_finalizer)
191
192RB_DEBUG_COUNTER(gc_isptr_trial)
193RB_DEBUG_COUNTER(gc_isptr_range)
194RB_DEBUG_COUNTER(gc_isptr_align)
195RB_DEBUG_COUNTER(gc_isptr_maybe)
196
197/* object allocation counts:
198 *
199 * * obj_newobj: newobj counts
200 * * obj_newobj_slowpath: newobj with slowpath counts
201 * * obj_newobj_wb_unprotected: newobj for wb_unprotected.
202 * * obj_free: obj_free() counts
203 * * obj_promote: promoted counts (oldgen)
204 * * obj_wb_unprotect: wb unprotect counts
205 *
206 * * obj_[type]_[attr]: *free'ed counts* for each type.
207 * Note that it is not a allocated counts.
208 * * [type]
209 * * _obj: T_OBJECT
210 * * _str: T_STRING
211 * * _ary: T_ARRAY
212 * * _xxx: T_XXX (hash, struct, ...)
213 *
214 * * [attr]
215 * * _ptr: R?? is not embed.
216 * * _embed: R?? is embed.
217 * * type specific attr.
218 * * str_shared: str is shared.
219 * * str_nofree: nofree
220 * * str_fstr: fstr
221 * * hash_empty: hash is empty
222 * * hash_1_4: has 1 to 4 entries
223 * * hash_5_8: has 5 to 8 entries
224 * * hash_g8: has n entries (n>8)
225 * * match_under4: has under 4 oniguruma regions allocated
226 * * match_ge4: has n regions allocated (4<=n<8)
227 * * match_ge8: has n regions allocated (8<=n)
228 * * data_empty: T_DATA but no memory free.
229 * * data_xfree: free'ed by xfree().
230 * * data_imm_free: free'ed immediately.
231 * * data_zombie: free'ed with zombie.
232 * * imemo_*: T_IMEMO with each type.
233 */
234RB_DEBUG_COUNTER(obj_newobj)
235RB_DEBUG_COUNTER(obj_newobj_slowpath)
236RB_DEBUG_COUNTER(obj_newobj_wb_unprotected)
237RB_DEBUG_COUNTER(obj_free)
238RB_DEBUG_COUNTER(obj_promote)
239RB_DEBUG_COUNTER(obj_wb_unprotect)
240
241RB_DEBUG_COUNTER(obj_obj_embed)
242RB_DEBUG_COUNTER(obj_obj_ptr)
243RB_DEBUG_COUNTER(obj_obj_too_complex)
244
245RB_DEBUG_COUNTER(obj_str_ptr)
246RB_DEBUG_COUNTER(obj_str_embed)
247RB_DEBUG_COUNTER(obj_str_shared)
248RB_DEBUG_COUNTER(obj_str_nofree)
249RB_DEBUG_COUNTER(obj_str_fstr)
250
251RB_DEBUG_COUNTER(obj_ary_embed)
252RB_DEBUG_COUNTER(obj_ary_ptr)
253RB_DEBUG_COUNTER(obj_ary_extracapa)
254/*
255 ary_shared_create: shared ary by Array#dup and so on.
256 ary_shared: finished in shard.
257 ary_shared_root_occupied: shared_root but has only 1 refcnt.
258 The number (ary_shared - ary_shared_root_occupied) is meaningful.
259 */
260RB_DEBUG_COUNTER(obj_ary_shared_create)
261RB_DEBUG_COUNTER(obj_ary_shared)
262RB_DEBUG_COUNTER(obj_ary_shared_root_occupied)
263
264RB_DEBUG_COUNTER(obj_hash_empty)
265RB_DEBUG_COUNTER(obj_hash_1)
266RB_DEBUG_COUNTER(obj_hash_2)
267RB_DEBUG_COUNTER(obj_hash_3)
268RB_DEBUG_COUNTER(obj_hash_4)
269RB_DEBUG_COUNTER(obj_hash_5_8)
270RB_DEBUG_COUNTER(obj_hash_g8)
271
272RB_DEBUG_COUNTER(obj_hash_null)
273RB_DEBUG_COUNTER(obj_hash_ar)
274RB_DEBUG_COUNTER(obj_hash_st)
275RB_DEBUG_COUNTER(obj_hash_force_convert)
276
277RB_DEBUG_COUNTER(obj_struct_embed)
278RB_DEBUG_COUNTER(obj_struct_ptr)
279
280RB_DEBUG_COUNTER(obj_data_empty)
281RB_DEBUG_COUNTER(obj_data_xfree)
282RB_DEBUG_COUNTER(obj_data_imm_free)
283RB_DEBUG_COUNTER(obj_data_zombie)
284
285RB_DEBUG_COUNTER(obj_match_under4)
286RB_DEBUG_COUNTER(obj_match_ge4)
287RB_DEBUG_COUNTER(obj_match_ge8)
288RB_DEBUG_COUNTER(obj_match_ptr)
289
290RB_DEBUG_COUNTER(obj_iclass_ptr)
291RB_DEBUG_COUNTER(obj_class_ptr)
292RB_DEBUG_COUNTER(obj_module_ptr)
293
294RB_DEBUG_COUNTER(obj_bignum_ptr)
295RB_DEBUG_COUNTER(obj_bignum_embed)
296RB_DEBUG_COUNTER(obj_float)
297RB_DEBUG_COUNTER(obj_complex)
298RB_DEBUG_COUNTER(obj_rational)
299
300RB_DEBUG_COUNTER(obj_regexp_ptr)
301RB_DEBUG_COUNTER(obj_file_ptr)
302RB_DEBUG_COUNTER(obj_symbol)
303
304RB_DEBUG_COUNTER(obj_imemo_ment)
305RB_DEBUG_COUNTER(obj_imemo_iseq)
306RB_DEBUG_COUNTER(obj_imemo_env)
307RB_DEBUG_COUNTER(obj_imemo_tmpbuf)
308RB_DEBUG_COUNTER(obj_imemo_ast)
309RB_DEBUG_COUNTER(obj_imemo_cref)
310RB_DEBUG_COUNTER(obj_imemo_svar)
311RB_DEBUG_COUNTER(obj_imemo_throw_data)
312RB_DEBUG_COUNTER(obj_imemo_ifunc)
313RB_DEBUG_COUNTER(obj_imemo_memo)
314RB_DEBUG_COUNTER(obj_imemo_parser_strterm)
315RB_DEBUG_COUNTER(obj_imemo_callinfo)
316RB_DEBUG_COUNTER(obj_imemo_callcache)
317RB_DEBUG_COUNTER(obj_imemo_constcache)
318
319/* ar_table */
320RB_DEBUG_COUNTER(artable_hint_hit)
321RB_DEBUG_COUNTER(artable_hint_miss)
322RB_DEBUG_COUNTER(artable_hint_notfound)
323
324/* heap function counts
325 *
326 * * heap_xmalloc/realloc/xfree: call counts
327 */
328RB_DEBUG_COUNTER(heap_xmalloc)
329RB_DEBUG_COUNTER(heap_xrealloc)
330RB_DEBUG_COUNTER(heap_xfree)
331
332// VM sync
333RB_DEBUG_COUNTER(vm_sync_lock)
334RB_DEBUG_COUNTER(vm_sync_lock_enter)
335RB_DEBUG_COUNTER(vm_sync_lock_enter_nb)
336RB_DEBUG_COUNTER(vm_sync_lock_enter_cr)
337RB_DEBUG_COUNTER(vm_sync_barrier)
338
339/* load (not implemented yet) */
340/*
341RB_DEBUG_COUNTER(load_files)
342RB_DEBUG_COUNTER(load_path_is_not_realpath)
343*/
344#endif
345
346#ifndef RUBY_DEBUG_COUNTER_H
347#define RUBY_DEBUG_COUNTER_H 1
348
349#include "ruby/internal/config.h"
350#include <stddef.h> /* for size_t */
351#include "ruby/ruby.h" /* for VALUE */
352
353#if !defined(__GNUC__) && USE_DEBUG_COUNTER
354#error "USE_DEBUG_COUNTER is not supported by other than __GNUC__"
355#endif
356
357enum rb_debug_counter_type {
358#define RB_DEBUG_COUNTER(name) RB_DEBUG_COUNTER_##name,
359#include "debug_counter.h"
360 RB_DEBUG_COUNTER_MAX
361#undef RB_DEBUG_COUNTER
362};
363
364#if USE_DEBUG_COUNTER
365extern size_t rb_debug_counter[];
366RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor;
367RUBY_EXTERN void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add);
368
369inline static int
370rb_debug_counter_add(enum rb_debug_counter_type type, int add, int cond)
371{
372 if (cond) {
373 if (ruby_single_main_ractor != NULL) {
374 rb_debug_counter[(int)type] += add;
375 }
376 else {
377 rb_debug_counter_add_atomic(type, add);
378 }
379 }
380 return cond;
381}
382
383inline static int
384rb_debug_counter_max(enum rb_debug_counter_type type, unsigned int num)
385{
386 // TODO: sync
387 if (rb_debug_counter[(int)type] < num) {
388 rb_debug_counter[(int)type] = num;
389 return 1;
390 }
391 else {
392 return 0;
393 }
394}
395
396VALUE rb_debug_counter_reset(VALUE klass);
397VALUE rb_debug_counter_show(VALUE klass);
398
399#define RB_DEBUG_COUNTER_INC(type) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, 1)
400#define RB_DEBUG_COUNTER_INC_UNLESS(type, cond) (!rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, !(cond)))
401#define RB_DEBUG_COUNTER_INC_IF(type, cond) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, !!(cond))
402#define RB_DEBUG_COUNTER_ADD(type, num) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, (num), 1)
403#define RB_DEBUG_COUNTER_SETMAX(type, num) rb_debug_counter_max(RB_DEBUG_COUNTER_##type, (unsigned int)(num))
404
405#else
406#define RB_DEBUG_COUNTER_INC(type) ((void)0)
407#define RB_DEBUG_COUNTER_INC_UNLESS(type, cond) (!!(cond))
408#define RB_DEBUG_COUNTER_INC_IF(type, cond) (!!(cond))
409#define RB_DEBUG_COUNTER_ADD(type, num) ((void)0)
410#define RB_DEBUG_COUNTER_SETMAX(type, num) 0
411#endif
412
413void rb_debug_counter_show_results(const char *msg);
414
415RUBY_SYMBOL_EXPORT_BEGIN
416
417size_t ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr);
418void ruby_debug_counter_reset(void);
419void ruby_debug_counter_show_at_exit(int enable);
420
421RUBY_SYMBOL_EXPORT_END
422
423#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