Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
debug_counter.h
Go to the documentation of this file.
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(mc_cme_complement) // number of acquiring complement CME
28RB_DEBUG_COUNTER(mc_cme_complement_hit) // number of cache hit for complemented CME
29
30RB_DEBUG_COUNTER(mc_search) // count for method lookup in class tree
31RB_DEBUG_COUNTER(mc_search_notfound) // method lookup, but not found
32RB_DEBUG_COUNTER(mc_search_super) // total traversed classes
33
34// callinfo
35RB_DEBUG_COUNTER(ci_packed) // number of packed CI
36RB_DEBUG_COUNTER(ci_kw) // non-packed CI w/ keywords
37RB_DEBUG_COUNTER(ci_nokw) // non-packed CI w/o keywords
38RB_DEBUG_COUNTER(ci_runtime) // creating temporary CI
39
40// callcache
41RB_DEBUG_COUNTER(cc_new) // number of CC
42RB_DEBUG_COUNTER(cc_temp) // dummy CC (stack-allocated)
43RB_DEBUG_COUNTER(cc_found_in_ccs) // count for CC lookup success in CCS
44RB_DEBUG_COUNTER(cc_not_found_in_ccs) // count for CC lookup success in CCS
45
46RB_DEBUG_COUNTER(cc_ent_invalidate) // count for invalidating cc (cc->klass = 0)
47RB_DEBUG_COUNTER(cc_cme_invalidate) // count for invalidating CME
48
49RB_DEBUG_COUNTER(cc_invalidate_leaf) // count for invalidating klass if klass has no-sublcasses
50RB_DEBUG_COUNTER(cc_invalidate_leaf_ccs) // corresponding CCS
51RB_DEBUG_COUNTER(cc_invalidate_leaf_callable) // complimented cache (no-subclasses)
52RB_DEBUG_COUNTER(cc_invalidate_tree) // count for invalidating klass if klass has sublcasses
53RB_DEBUG_COUNTER(cc_invalidate_tree_cme) // cme if cme is found in this class or superclasses
54RB_DEBUG_COUNTER(cc_invalidate_tree_callable) // complimented cache (subclasses)
55RB_DEBUG_COUNTER(cc_invalidate_negative) // count for invalidating negative cache
56
57RB_DEBUG_COUNTER(ccs_free) // count for free'ing ccs
58RB_DEBUG_COUNTER(ccs_maxlen) // maximum length of ccs
59RB_DEBUG_COUNTER(ccs_found) // count for finding corresponding ccs on method lookup
60RB_DEBUG_COUNTER(ccs_not_found) // count for not found corresponding ccs on method lookup
61
62// vm_eval.c
63RB_DEBUG_COUNTER(call0_public)
64RB_DEBUG_COUNTER(call0_other)
65RB_DEBUG_COUNTER(gccct_hit)
66RB_DEBUG_COUNTER(gccct_miss)
67RB_DEBUG_COUNTER(gccct_null)
68
69// iseq
70RB_DEBUG_COUNTER(iseq_num) // number of total created iseq
71RB_DEBUG_COUNTER(iseq_cd_num) // number of total created cd (call_data)
72
73/*
74 * call cache fastpath usage
75 */
76RB_DEBUG_COUNTER(ccf_general)
77RB_DEBUG_COUNTER(ccf_iseq_setup)
78RB_DEBUG_COUNTER(ccf_iseq_setup_0start)
79RB_DEBUG_COUNTER(ccf_iseq_setup_tailcall_0start)
80RB_DEBUG_COUNTER(ccf_iseq_fix) /* several functions created with tool/mk_call_iseq_optimized.rb */
81RB_DEBUG_COUNTER(ccf_iseq_opt) /* has_opt == TRUE (has optional parameters), but other flags are FALSE */
82RB_DEBUG_COUNTER(ccf_iseq_kw1) /* vm_call_iseq_setup_kwparm_kwarg() */
83RB_DEBUG_COUNTER(ccf_iseq_kw2) /* vm_call_iseq_setup_kwparm_nokwarg() */
84RB_DEBUG_COUNTER(ccf_cfunc)
85RB_DEBUG_COUNTER(ccf_cfunc_with_frame)
86RB_DEBUG_COUNTER(ccf_ivar) /* attr_reader */
87RB_DEBUG_COUNTER(ccf_attrset) /* attr_writer */
88RB_DEBUG_COUNTER(ccf_method_missing)
89RB_DEBUG_COUNTER(ccf_zsuper)
90RB_DEBUG_COUNTER(ccf_bmethod)
91RB_DEBUG_COUNTER(ccf_opt_send)
92RB_DEBUG_COUNTER(ccf_opt_call)
93RB_DEBUG_COUNTER(ccf_opt_block_call)
94RB_DEBUG_COUNTER(ccf_super_method)
95
96/*
97 * control frame push counts.
98 *
99 * * frame_push: frame push counts.
100 * * frame_push_*: frame push counts per each type.
101 * * frame_R2R: Ruby frame to Ruby frame
102 * * frame_R2C: Ruby frame to C frame
103 * * frame_C2C: C frame to C frame
104 * * frame_C2R: C frame to Ruby frame
105 */
106RB_DEBUG_COUNTER(frame_push)
107RB_DEBUG_COUNTER(frame_push_method)
108RB_DEBUG_COUNTER(frame_push_block)
109RB_DEBUG_COUNTER(frame_push_class)
110RB_DEBUG_COUNTER(frame_push_top)
111RB_DEBUG_COUNTER(frame_push_cfunc)
112RB_DEBUG_COUNTER(frame_push_ifunc)
113RB_DEBUG_COUNTER(frame_push_eval)
114RB_DEBUG_COUNTER(frame_push_rescue)
115RB_DEBUG_COUNTER(frame_push_dummy)
116
117RB_DEBUG_COUNTER(frame_R2R)
118RB_DEBUG_COUNTER(frame_R2C)
119RB_DEBUG_COUNTER(frame_C2C)
120RB_DEBUG_COUNTER(frame_C2R)
121
122/* instance variable counts
123 *
124 * * ivar_get_ic_hit/miss: ivar_get inline cache (ic) hit/miss counts (VM insn)
125 * * ivar_get_ic_miss_serial: ivar_get ic miss reason by serial (VM insn)
126 * * ivar_get_ic_miss_unset: ... by unset (VM insn)
127 * * ivar_get_ic_miss_noobject: ... by "not T_OBJECT" (VM insn)
128 * * ivar_set_...: same counts with ivar_set (VM insn)
129 * * ivar_get/set_base: call counts of "rb_ivar_get/set()".
130 * because of (1) ic miss.
131 * (2) direct call by C extensions.
132 */
133RB_DEBUG_COUNTER(ivar_get_ic_hit)
134RB_DEBUG_COUNTER(ivar_get_ic_miss)
135RB_DEBUG_COUNTER(ivar_get_ic_miss_serial)
136RB_DEBUG_COUNTER(ivar_get_ic_miss_unset)
137RB_DEBUG_COUNTER(ivar_get_ic_miss_noobject)
138RB_DEBUG_COUNTER(ivar_set_ic_hit)
139RB_DEBUG_COUNTER(ivar_set_ic_miss)
140RB_DEBUG_COUNTER(ivar_set_ic_miss_serial)
141RB_DEBUG_COUNTER(ivar_set_ic_miss_unset)
142RB_DEBUG_COUNTER(ivar_set_ic_miss_iv_hit)
143RB_DEBUG_COUNTER(ivar_set_ic_miss_noobject)
144RB_DEBUG_COUNTER(ivar_get_base)
145RB_DEBUG_COUNTER(ivar_set_base)
146
147/* local variable counts
148 *
149 * * lvar_get: total lvar get counts (VM insn)
150 * * lvar_get_dynamic: lvar get counts if accessing upper env (VM insn)
151 * * lvar_set*: same as "get"
152 * * lvar_set_slowpath: counts using vm_env_write_slowpath()
153 */
154RB_DEBUG_COUNTER(lvar_get)
155RB_DEBUG_COUNTER(lvar_get_dynamic)
156RB_DEBUG_COUNTER(lvar_set)
157RB_DEBUG_COUNTER(lvar_set_dynamic)
158RB_DEBUG_COUNTER(lvar_set_slowpath)
159
160/* GC counts:
161 *
162 * * count: simple count
163 * * _minor: minor gc
164 * * _major: major gc
165 * * other suffix is corresponding to last_gc_info or
166 * gc_profile_record_flag in gc.c.
167 */
168RB_DEBUG_COUNTER(gc_count)
169RB_DEBUG_COUNTER(gc_minor_newobj)
170RB_DEBUG_COUNTER(gc_minor_malloc)
171RB_DEBUG_COUNTER(gc_minor_method)
172RB_DEBUG_COUNTER(gc_minor_capi)
173RB_DEBUG_COUNTER(gc_minor_stress)
174RB_DEBUG_COUNTER(gc_major_nofree)
175RB_DEBUG_COUNTER(gc_major_oldgen)
176RB_DEBUG_COUNTER(gc_major_shady)
177RB_DEBUG_COUNTER(gc_major_force)
178RB_DEBUG_COUNTER(gc_major_oldmalloc)
179
180RB_DEBUG_COUNTER(gc_enter_start)
181RB_DEBUG_COUNTER(gc_enter_mark_continue)
182RB_DEBUG_COUNTER(gc_enter_sweep_continue)
183RB_DEBUG_COUNTER(gc_enter_rest)
184RB_DEBUG_COUNTER(gc_enter_finalizer)
185
186RB_DEBUG_COUNTER(gc_isptr_trial)
187RB_DEBUG_COUNTER(gc_isptr_range)
188RB_DEBUG_COUNTER(gc_isptr_align)
189RB_DEBUG_COUNTER(gc_isptr_maybe)
190
191/* object allocation counts:
192 *
193 * * obj_newobj: newobj counts
194 * * obj_newobj_slowpath: newobj with slowpath counts
195 * * obj_newobj_wb_unprotected: newobj for wb_unprotected.
196 * * obj_free: obj_free() counts
197 * * obj_promote: promoted counts (oldgen)
198 * * obj_wb_unprotect: wb unprotect counts
199 *
200 * * obj_[type]_[attr]: *free'ed counts* for each type.
201 * Note that it is not a allocated counts.
202 * * [type]
203 * * _obj: T_OBJECT
204 * * _str: T_STRING
205 * * _ary: T_ARRAY
206 * * _xxx: T_XXX (hash, struct, ...)
207 *
208 * * [attr]
209 * * _ptr: R?? is not embed.
210 * * _embed: R?? is embed.
211 * * _transient: R?? uses transient heap.
212 * * type specific attr.
213 * * str_shared: str is shared.
214 * * str_nofree: nofree
215 * * str_fstr: fstr
216 * * hash_empty: hash is empty
217 * * hash_1_4: has 1 to 4 entries
218 * * hash_5_8: has 5 to 8 entries
219 * * hash_g8: has n entries (n>8)
220 * * match_under4: has under 4 oniguruma regions allocated
221 * * match_ge4: has n regions allocated (4<=n<8)
222 * * match_ge8: has n regions allocated (8<=n)
223 * * data_empty: T_DATA but no memory free.
224 * * data_xfree: free'ed by xfree().
225 * * data_imm_free: free'ed immediately.
226 * * data_zombie: free'ed with zombie.
227 * * imemo_*: T_IMEMO with each type.
228 */
229RB_DEBUG_COUNTER(obj_newobj)
230RB_DEBUG_COUNTER(obj_newobj_slowpath)
231RB_DEBUG_COUNTER(obj_newobj_wb_unprotected)
232RB_DEBUG_COUNTER(obj_free)
233RB_DEBUG_COUNTER(obj_promote)
234RB_DEBUG_COUNTER(obj_wb_unprotect)
235
236RB_DEBUG_COUNTER(obj_obj_embed)
237RB_DEBUG_COUNTER(obj_obj_transient)
238RB_DEBUG_COUNTER(obj_obj_ptr)
239
240RB_DEBUG_COUNTER(obj_str_ptr)
241RB_DEBUG_COUNTER(obj_str_embed)
242RB_DEBUG_COUNTER(obj_str_shared)
243RB_DEBUG_COUNTER(obj_str_nofree)
244RB_DEBUG_COUNTER(obj_str_fstr)
245
246RB_DEBUG_COUNTER(obj_ary_embed)
247RB_DEBUG_COUNTER(obj_ary_transient)
248RB_DEBUG_COUNTER(obj_ary_ptr)
249RB_DEBUG_COUNTER(obj_ary_extracapa)
250/*
251 ary_shared_create: shared ary by Array#dup and so on.
252 ary_shared: finished in shard.
253 ary_shared_root_occupied: shared_root but has only 1 refcnt.
254 The number (ary_shared - ary_shared_root_occupied) is meaningful.
255 */
256RB_DEBUG_COUNTER(obj_ary_shared_create)
257RB_DEBUG_COUNTER(obj_ary_shared)
258RB_DEBUG_COUNTER(obj_ary_shared_root_occupied)
259
260RB_DEBUG_COUNTER(obj_hash_empty)
261RB_DEBUG_COUNTER(obj_hash_1)
262RB_DEBUG_COUNTER(obj_hash_2)
263RB_DEBUG_COUNTER(obj_hash_3)
264RB_DEBUG_COUNTER(obj_hash_4)
265RB_DEBUG_COUNTER(obj_hash_5_8)
266RB_DEBUG_COUNTER(obj_hash_g8)
267
268RB_DEBUG_COUNTER(obj_hash_null)
269RB_DEBUG_COUNTER(obj_hash_ar)
270RB_DEBUG_COUNTER(obj_hash_st)
271RB_DEBUG_COUNTER(obj_hash_transient)
272RB_DEBUG_COUNTER(obj_hash_force_convert)
273
274RB_DEBUG_COUNTER(obj_struct_embed)
275RB_DEBUG_COUNTER(obj_struct_transient)
276RB_DEBUG_COUNTER(obj_struct_ptr)
277
278RB_DEBUG_COUNTER(obj_data_empty)
279RB_DEBUG_COUNTER(obj_data_xfree)
280RB_DEBUG_COUNTER(obj_data_imm_free)
281RB_DEBUG_COUNTER(obj_data_zombie)
282
283RB_DEBUG_COUNTER(obj_match_under4)
284RB_DEBUG_COUNTER(obj_match_ge4)
285RB_DEBUG_COUNTER(obj_match_ge8)
286RB_DEBUG_COUNTER(obj_match_ptr)
287
288RB_DEBUG_COUNTER(obj_iclass_ptr)
289RB_DEBUG_COUNTER(obj_class_ptr)
290RB_DEBUG_COUNTER(obj_module_ptr)
291
292RB_DEBUG_COUNTER(obj_bignum_ptr)
293RB_DEBUG_COUNTER(obj_bignum_embed)
294RB_DEBUG_COUNTER(obj_float)
295RB_DEBUG_COUNTER(obj_complex)
296RB_DEBUG_COUNTER(obj_rational)
297
298RB_DEBUG_COUNTER(obj_regexp_ptr)
299RB_DEBUG_COUNTER(obj_file_ptr)
300RB_DEBUG_COUNTER(obj_symbol)
301
302RB_DEBUG_COUNTER(obj_imemo_ment)
303RB_DEBUG_COUNTER(obj_imemo_iseq)
304RB_DEBUG_COUNTER(obj_imemo_env)
305RB_DEBUG_COUNTER(obj_imemo_tmpbuf)
306RB_DEBUG_COUNTER(obj_imemo_ast)
307RB_DEBUG_COUNTER(obj_imemo_cref)
308RB_DEBUG_COUNTER(obj_imemo_svar)
309RB_DEBUG_COUNTER(obj_imemo_throw_data)
310RB_DEBUG_COUNTER(obj_imemo_ifunc)
311RB_DEBUG_COUNTER(obj_imemo_memo)
312RB_DEBUG_COUNTER(obj_imemo_parser_strterm)
313RB_DEBUG_COUNTER(obj_imemo_callinfo)
314RB_DEBUG_COUNTER(obj_imemo_callcache)
315RB_DEBUG_COUNTER(obj_imemo_constcache)
316
317/* ar_table */
318RB_DEBUG_COUNTER(artable_hint_hit)
319RB_DEBUG_COUNTER(artable_hint_miss)
320RB_DEBUG_COUNTER(artable_hint_notfound)
321
322/* heap function counts
323 *
324 * * heap_xmalloc/realloc/xfree: call counts
325 */
326RB_DEBUG_COUNTER(heap_xmalloc)
327RB_DEBUG_COUNTER(heap_xrealloc)
328RB_DEBUG_COUNTER(heap_xfree)
329
330/* transient_heap */
331RB_DEBUG_COUNTER(theap_alloc)
332RB_DEBUG_COUNTER(theap_alloc_fail)
333RB_DEBUG_COUNTER(theap_evacuate)
334
335// VM sync
336RB_DEBUG_COUNTER(vm_sync_lock)
337RB_DEBUG_COUNTER(vm_sync_lock_enter)
338RB_DEBUG_COUNTER(vm_sync_lock_enter_nb)
339RB_DEBUG_COUNTER(vm_sync_lock_enter_cr)
340RB_DEBUG_COUNTER(vm_sync_barrier)
341
342/* mjit_exec() counts */
343RB_DEBUG_COUNTER(mjit_exec)
344RB_DEBUG_COUNTER(mjit_exec_not_added)
345RB_DEBUG_COUNTER(mjit_exec_not_ready)
346RB_DEBUG_COUNTER(mjit_exec_not_compiled)
347RB_DEBUG_COUNTER(mjit_exec_call_func)
348
349/* MJIT enqueue / unload */
350RB_DEBUG_COUNTER(mjit_add_iseq_to_process)
351RB_DEBUG_COUNTER(mjit_unload_units)
352
353/* MJIT <-> VM frame push counts */
354RB_DEBUG_COUNTER(mjit_frame_VM2VM)
355RB_DEBUG_COUNTER(mjit_frame_VM2JT)
356RB_DEBUG_COUNTER(mjit_frame_JT2JT)
357RB_DEBUG_COUNTER(mjit_frame_JT2VM)
358
359/* MJIT cancel counters */
360RB_DEBUG_COUNTER(mjit_cancel)
361RB_DEBUG_COUNTER(mjit_cancel_ivar_inline)
362RB_DEBUG_COUNTER(mjit_cancel_exivar_inline)
363RB_DEBUG_COUNTER(mjit_cancel_send_inline)
364RB_DEBUG_COUNTER(mjit_cancel_opt_insn) /* CALL_SIMPLE_METHOD */
365RB_DEBUG_COUNTER(mjit_cancel_invalidate_all)
366RB_DEBUG_COUNTER(mjit_cancel_leave)
367
368/* rb_mjit_unit_list length */
369RB_DEBUG_COUNTER(mjit_length_unit_queue)
370RB_DEBUG_COUNTER(mjit_length_active_units)
371RB_DEBUG_COUNTER(mjit_length_compact_units)
372RB_DEBUG_COUNTER(mjit_length_stale_units)
373
374/* Other MJIT counters */
375RB_DEBUG_COUNTER(mjit_compile_failures)
376
377/* load (not implemented yet) */
378/*
379RB_DEBUG_COUNTER(load_files)
380RB_DEBUG_COUNTER(load_path_is_not_realpath)
381*/
382#endif
383
384#ifndef RUBY_DEBUG_COUNTER_H
385#define RUBY_DEBUG_COUNTER_H 1
386
387#include "ruby/internal/config.h"
388#include <stddef.h> /* for size_t */
389#include "ruby/ruby.h" /* for VALUE */
390
391#if !defined(__GNUC__) && USE_DEBUG_COUNTER
392#error "USE_DEBUG_COUNTER is not supported by other than __GNUC__"
393#endif
394
396#define RB_DEBUG_COUNTER(name) RB_DEBUG_COUNTER_##name,
397#include __FILE__
399#undef RB_DEBUG_COUNTER
401
402#if USE_DEBUG_COUNTER
403extern size_t rb_debug_counter[];
405RUBY_EXTERN void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add);
406
407inline static int
408rb_debug_counter_add(enum rb_debug_counter_type type, int add, int cond)
409{
410 if (cond) {
412 rb_debug_counter[(int)type] += add;
413 }
414 else {
415 rb_debug_counter_add_atomic(type, add);
416 }
417 }
418 return cond;
419}
420
421inline static int
422rb_debug_counter_max(enum rb_debug_counter_type type, unsigned int num)
423{
424 // TODO: sync
425 if (rb_debug_counter[(int)type] < num) {
426 rb_debug_counter[(int)type] = num;
427 return 1;
428 }
429 else {
430 return 0;
431 }
432}
433
434VALUE rb_debug_counter_reset(VALUE klass);
435VALUE rb_debug_counter_show(VALUE klass);
436
437#define RB_DEBUG_COUNTER_INC(type) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, 1)
438#define RB_DEBUG_COUNTER_INC_UNLESS(type, cond) (!rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, !(cond)))
439#define RB_DEBUG_COUNTER_INC_IF(type, cond) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, 1, !!(cond))
440#define RB_DEBUG_COUNTER_ADD(type, num) rb_debug_counter_add(RB_DEBUG_COUNTER_##type, (num), 1)
441#define RB_DEBUG_COUNTER_SETMAX(type, num) rb_debug_counter_max(RB_DEBUG_COUNTER_##type, (unsigned int)(num))
442
443#else
444#define RB_DEBUG_COUNTER_INC(type) ((void)0)
445#define RB_DEBUG_COUNTER_INC_UNLESS(type, cond) (!!(cond))
446#define RB_DEBUG_COUNTER_INC_IF(type, cond) (!!(cond))
447#define RB_DEBUG_COUNTER_ADD(type, num) ((void)0)
448#define RB_DEBUG_COUNTER_SETMAX(type, num) 0
449#endif
450
451void rb_debug_counter_show_results(const char *msg);
452
453RUBY_SYMBOL_EXPORT_BEGIN
454
455size_t ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr);
456void ruby_debug_counter_reset(void);
457void ruby_debug_counter_show_at_exit(int enable);
458
459RUBY_SYMBOL_EXPORT_END
460
461#endif /* RUBY_DEBUG_COUNTER_H */
#define add(x, y)
Definition: date_strftime.c:23
rb_debug_counter_type
@ RB_DEBUG_COUNTER_MAX
void rb_debug_counter_show_results(const char *msg)
#define RB_DEBUG_COUNTER(name)
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)
#define RUBY_EXTERN
Definition: dllexport.h:36
big_t * num
Definition: enough.c:232
Thin wrapper to ruby/config.h.
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
#define NULL
Definition: regenc.h:69
unsigned long VALUE
Definition: value.h:38
rb_ractor_t * ruby_single_main_ractor
Definition: vm.c:381