Ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e0ba6b95ab71a441357ed5484e33498)
debug_counter.h
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)
103
104/*
105 * control frame push counts.
106 *
107 * * frame_push: frame push counts.
108 * * frame_push_*: frame push counts per each type.
109 * * frame_R2R: Ruby frame to Ruby frame
110 * * frame_R2C: Ruby frame to C frame
111 * * frame_C2C: C frame to C frame
112 * * frame_C2R: C frame to Ruby frame
113 */
114RB_DEBUG_COUNTER(frame_push)
115RB_DEBUG_COUNTER(frame_push_method)
116RB_DEBUG_COUNTER(frame_push_block)
117RB_DEBUG_COUNTER(frame_push_class)
118RB_DEBUG_COUNTER(frame_push_top)
119RB_DEBUG_COUNTER(frame_push_cfunc)
120RB_DEBUG_COUNTER(frame_push_ifunc)
121RB_DEBUG_COUNTER(frame_push_eval)
122RB_DEBUG_COUNTER(frame_push_rescue)
123RB_DEBUG_COUNTER(frame_push_dummy)
124
125RB_DEBUG_COUNTER(frame_R2R)
126RB_DEBUG_COUNTER(frame_R2C)
127RB_DEBUG_COUNTER(frame_C2C)
128RB_DEBUG_COUNTER(frame_C2R)
129
130/* instance variable counts
131 *
132 * * ivar_get_ic_hit/miss: ivar_get inline cache (ic) hit/miss counts (VM insn)
133 * * ivar_get_ic_miss_serial: ivar_get ic miss reason by serial (VM insn)
134 * * ivar_get_ic_miss_unset: ... by unset (VM insn)
135 * * ivar_get_ic_miss_noobject: ... by "not T_OBJECT" (VM insn)
136 * * ivar_set_...: same counts with ivar_set (VM insn)
137 * * ivar_get/set_base: call counts of "rb_ivar_get/set()".
138 * because of (1) ic miss.
139 * (2) direct call by C extensions.
140 */
141RB_DEBUG_COUNTER(ivar_get_ic_hit)
142RB_DEBUG_COUNTER(ivar_get_ic_miss)
143RB_DEBUG_COUNTER(ivar_get_ic_miss_serial)
144RB_DEBUG_COUNTER(ivar_get_ic_miss_unset)
145RB_DEBUG_COUNTER(ivar_get_ic_miss_noobject)
146RB_DEBUG_COUNTER(ivar_set_ic_hit)
147RB_DEBUG_COUNTER(ivar_set_ic_miss)
148RB_DEBUG_COUNTER(ivar_set_ic_miss_serial)
149RB_DEBUG_COUNTER(ivar_set_ic_miss_unset)
150RB_DEBUG_COUNTER(ivar_set_ic_miss_iv_hit)
151RB_DEBUG_COUNTER(ivar_set_ic_miss_noobject)
152RB_DEBUG_COUNTER(ivar_get_base)
153RB_DEBUG_COUNTER(ivar_set_base)
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_mark_continue)
190RB_DEBUG_COUNTER(gc_enter_sweep_continue)
191RB_DEBUG_COUNTER(gc_enter_rest)
192RB_DEBUG_COUNTER(gc_enter_finalizer)
193
194RB_DEBUG_COUNTER(gc_isptr_trial)
195RB_DEBUG_COUNTER(gc_isptr_range)
196RB_DEBUG_COUNTER(gc_isptr_align)
197RB_DEBUG_COUNTER(gc_isptr_maybe)
198
199/* object allocation counts:
200 *
201 * * obj_newobj: newobj counts
202 * * obj_newobj_slowpath: newobj with slowpath counts
203 * * obj_newobj_wb_unprotected: newobj for wb_unprotected.
204 * * obj_free: obj_free() counts
205 * * obj_promote: promoted counts (oldgen)
206 * * obj_wb_unprotect: wb unprotect counts
207 *
208 * * obj_[type]_[attr]: *free'ed counts* for each type.
209 * Note that it is not a allocated counts.
210 * * [type]
211 * * _obj: T_OBJECT
212 * * _str: T_STRING
213 * * _ary: T_ARRAY
214 * * _xxx: T_XXX (hash, struct, ...)
215 *
216 * * [attr]
217 * * _ptr: R?? is not embed.
218 * * _embed: R?? is embed.
219 * * _transient: R?? uses transient heap.
220 * * type specific attr.
221 * * str_shared: str is shared.
222 * * str_nofree: nofree
223 * * str_fstr: fstr
224 * * hash_empty: hash is empty
225 * * hash_1_4: has 1 to 4 entries
226 * * hash_5_8: has 5 to 8 entries
227 * * hash_g8: has n entries (n>8)
228 * * match_under4: has under 4 oniguruma regions allocated
229 * * match_ge4: has n regions allocated (4<=n<8)
230 * * match_ge8: has n regions allocated (8<=n)
231 * * data_empty: T_DATA but no memory free.
232 * * data_xfree: free'ed by xfree().
233 * * data_imm_free: free'ed immediately.
234 * * data_zombie: free'ed with zombie.
235 * * imemo_*: T_IMEMO with each type.
236 */
237RB_DEBUG_COUNTER(obj_newobj)
238RB_DEBUG_COUNTER(obj_newobj_slowpath)
239RB_DEBUG_COUNTER(obj_newobj_wb_unprotected)
240RB_DEBUG_COUNTER(obj_free)
241RB_DEBUG_COUNTER(obj_promote)
242RB_DEBUG_COUNTER(obj_wb_unprotect)
243
244RB_DEBUG_COUNTER(obj_obj_embed)
245RB_DEBUG_COUNTER(obj_obj_transient)
246RB_DEBUG_COUNTER(obj_obj_ptr)
247
248RB_DEBUG_COUNTER(obj_str_ptr)
249RB_DEBUG_COUNTER(obj_str_embed)
250RB_DEBUG_COUNTER(obj_str_shared)
251RB_DEBUG_COUNTER(obj_str_nofree)
252RB_DEBUG_COUNTER(obj_str_fstr)
253
254RB_DEBUG_COUNTER(obj_ary_embed)
255RB_DEBUG_COUNTER(obj_ary_transient)
256RB_DEBUG_COUNTER(obj_ary_ptr)
257RB_DEBUG_COUNTER(obj_ary_extracapa)
258/*
259 ary_shared_create: shared ary by Array#dup and so on.
260 ary_shared: finished in shard.
261 ary_shared_root_occupied: shared_root but has only 1 refcnt.
262 The number (ary_shared - ary_shared_root_occupied) is meaningful.
263 */
264RB_DEBUG_COUNTER(obj_ary_shared_create)
265RB_DEBUG_COUNTER(obj_ary_shared)
266RB_DEBUG_COUNTER(obj_ary_shared_root_occupied)
267
268RB_DEBUG_COUNTER(obj_hash_empty)
269RB_DEBUG_COUNTER(obj_hash_1)
270RB_DEBUG_COUNTER(obj_hash_2)
271RB_DEBUG_COUNTER(obj_hash_3)
272RB_DEBUG_COUNTER(obj_hash_4)
273RB_DEBUG_COUNTER(obj_hash_5_8)
274RB_DEBUG_COUNTER(obj_hash_g8)
275
276RB_DEBUG_COUNTER(obj_hash_null)
277RB_DEBUG_COUNTER(obj_hash_ar)
278RB_DEBUG_COUNTER(obj_hash_st)
279RB_DEBUG_COUNTER(obj_hash_transient)
280RB_DEBUG_COUNTER(obj_hash_force_convert)
281
282RB_DEBUG_COUNTER(obj_struct_embed)
283RB_DEBUG_COUNTER(obj_struct_transient)
284RB_DEBUG_COUNTER(obj_struct_ptr)
285
286RB_DEBUG_COUNTER(obj_data_empty)
287RB_DEBUG_COUNTER(obj_data_xfree)
288RB_DEBUG_COUNTER(obj_data_imm_free)
289RB_DEBUG_COUNTER(obj_data_zombie)
290
291RB_DEBUG_COUNTER(obj_match_under4)
292RB_DEBUG_COUNTER(obj_match_ge4)
293RB_DEBUG_COUNTER(obj_match_ge8)
294RB_DEBUG_COUNTER(obj_match_ptr)
295
296RB_DEBUG_COUNTER(obj_iclass_ptr)
297RB_DEBUG_COUNTER(obj_class_ptr)
298RB_DEBUG_COUNTER(obj_module_ptr)
299
300RB_DEBUG_COUNTER(obj_bignum_ptr)
301RB_DEBUG_COUNTER(obj_bignum_embed)
302RB_DEBUG_COUNTER(obj_float)
303RB_DEBUG_COUNTER(obj_complex)
304RB_DEBUG_COUNTER(obj_rational)
305
306RB_DEBUG_COUNTER(obj_regexp_ptr)
307RB_DEBUG_COUNTER(obj_file_ptr)
308RB_DEBUG_COUNTER(obj_symbol)
309
310RB_DEBUG_COUNTER(obj_imemo_ment)
311RB_DEBUG_COUNTER(obj_imemo_iseq)
312RB_DEBUG_COUNTER(obj_imemo_env)
313RB_DEBUG_COUNTER(obj_imemo_tmpbuf)
314RB_DEBUG_COUNTER(obj_imemo_ast)
315RB_DEBUG_COUNTER(obj_imemo_cref)
316RB_DEBUG_COUNTER(obj_imemo_svar)
317RB_DEBUG_COUNTER(obj_imemo_throw_data)
318RB_DEBUG_COUNTER(obj_imemo_ifunc)
319RB_DEBUG_COUNTER(obj_imemo_memo)
320RB_DEBUG_COUNTER(obj_imemo_parser_strterm)
321RB_DEBUG_COUNTER(obj_imemo_callinfo)
322RB_DEBUG_COUNTER(obj_imemo_callcache)
323RB_DEBUG_COUNTER(obj_imemo_constcache)
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/* transient_heap */
339RB_DEBUG_COUNTER(theap_alloc)
340RB_DEBUG_COUNTER(theap_alloc_fail)
341RB_DEBUG_COUNTER(theap_evacuate)
342
343// VM sync
344RB_DEBUG_COUNTER(vm_sync_lock)
345RB_DEBUG_COUNTER(vm_sync_lock_enter)
346RB_DEBUG_COUNTER(vm_sync_lock_enter_nb)
347RB_DEBUG_COUNTER(vm_sync_lock_enter_cr)
348RB_DEBUG_COUNTER(vm_sync_barrier)
349
350/* mjit_exec() counts */
351RB_DEBUG_COUNTER(mjit_exec)
352RB_DEBUG_COUNTER(mjit_exec_not_added)
353RB_DEBUG_COUNTER(mjit_exec_not_ready)
354RB_DEBUG_COUNTER(mjit_exec_not_compiled)
355RB_DEBUG_COUNTER(mjit_exec_call_func)
356
357/* MJIT enqueue / unload */
358RB_DEBUG_COUNTER(mjit_add_iseq_to_process)
359RB_DEBUG_COUNTER(mjit_unload_units)
360
361/* MJIT <-> VM frame push counts */
362RB_DEBUG_COUNTER(mjit_frame_VM2VM)
363RB_DEBUG_COUNTER(mjit_frame_VM2JT)
364RB_DEBUG_COUNTER(mjit_frame_JT2JT)
365RB_DEBUG_COUNTER(mjit_frame_JT2VM)
366
367/* MJIT cancel counters */
368RB_DEBUG_COUNTER(mjit_cancel)
369RB_DEBUG_COUNTER(mjit_cancel_ivar_inline)
370RB_DEBUG_COUNTER(mjit_cancel_exivar_inline)
371RB_DEBUG_COUNTER(mjit_cancel_send_inline)
372RB_DEBUG_COUNTER(mjit_cancel_opt_insn) /* CALL_SIMPLE_METHOD */
373RB_DEBUG_COUNTER(mjit_cancel_invalidate_all)
374RB_DEBUG_COUNTER(mjit_cancel_leave)
375
376/* rb_mjit_unit_list length */
377RB_DEBUG_COUNTER(mjit_length_unit_queue)
378RB_DEBUG_COUNTER(mjit_length_active_units)
379RB_DEBUG_COUNTER(mjit_length_compact_units)
380RB_DEBUG_COUNTER(mjit_length_stale_units)
381
382/* Other MJIT counters */
383RB_DEBUG_COUNTER(mjit_compile_failures)
384
385/* load (not implemented yet) */
386/*
387RB_DEBUG_COUNTER(load_files)
388RB_DEBUG_COUNTER(load_path_is_not_realpath)
389*/
390#endif
391
392#ifndef RUBY_DEBUG_COUNTER_H
393#define RUBY_DEBUG_COUNTER_H 1
394
395#include "ruby/internal/config.h"
396#include <stddef.h> /* for size_t */
397#include "ruby/ruby.h" /* for VALUE */
398
399#if !defined(__GNUC__) && USE_DEBUG_COUNTER
400#error "USE_DEBUG_COUNTER is not supported by other than __GNUC__"
401#endif
402
403enum rb_debug_counter_type {
404#define RB_DEBUG_COUNTER(name) RB_DEBUG_COUNTER_##name,
405#include __FILE__
406 RB_DEBUG_COUNTER_MAX
407#undef RB_DEBUG_COUNTER
408};
409
410#if USE_DEBUG_COUNTER
411extern size_t rb_debug_counter[];
412RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor;
413RUBY_EXTERN void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add);
414
415inline static int
416rb_debug_counter_add(enum rb_debug_counter_type type, int add, int cond)
417{
418 if (cond) {
419 if (ruby_single_main_ractor != NULL) {
420 rb_debug_counter[(int)type] += add;
421 }
422 else {
423 rb_debug_counter_add_atomic(type, add);
424 }
425 }
426 return cond;
427}
428
429inline static int
430rb_debug_counter_max(enum rb_debug_counter_type type, unsigned int num)
431{
432 // TODO: sync
433 if (rb_debug_counter[(int)type] < num) {
434 rb_debug_counter[(int)type] = num;
435 return 1;
436 }
437 else {
438 return 0;
439 }
440}
441
442VALUE rb_debug_counter_reset(VALUE klass);
443VALUE rb_debug_counter_show(VALUE klass);
444
445#define RB_DEBUG_COUNTER_INC(type) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, 1)
446#define RB_DEBUG_COUNTER_INC_UNLESS(type, cond) (!rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, !(cond)))
447#define RB_DEBUG_COUNTER_INC_IF(type, cond) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, !!(cond))
448#define RB_DEBUG_COUNTER_ADD(type, num) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, (num), 1)
449#define RB_DEBUG_COUNTER_SETMAX(type, num) rb_debug_counter_max(RB_DEBUG_COUNTER_##type, (unsigned int)(num))
450
451#else
452#define RB_DEBUG_COUNTER_INC(type) ((void)0)
453#define RB_DEBUG_COUNTER_INC_UNLESS(type, cond) (!!(cond))
454#define RB_DEBUG_COUNTER_INC_IF(type, cond) (!!(cond))
455#define RB_DEBUG_COUNTER_ADD(type, num) ((void)0)
456#define RB_DEBUG_COUNTER_SETMAX(type, num) 0
457#endif
458
459void rb_debug_counter_show_results(const char *msg);
460
461RUBY_SYMBOL_EXPORT_BEGIN
462
463size_t ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr);
464void ruby_debug_counter_reset(void);
465void ruby_debug_counter_show_at_exit(int enable);
466
467RUBY_SYMBOL_EXPORT_END
468
469#endif /* RUBY_DEBUG_COUNTER_H */
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:47
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56