From 1a960576262223e6ea1a2ead214cfaf90fc304e6 Mon Sep 17 00:00:00 2001 From: ko1 Date: Fri, 17 Nov 2017 06:24:55 +0000 Subject: [PATCH] remove `trace_` prefix insns lazily. * vm_trace.c (update_global_event_hook): set only when tracing is added. If tracing was off (event flags are decreased), then ignore them. Next `trace_` prefix instruction will trace off itself (lazy tracing off). * vm_insnhelper.c (vm_trace): trace-off for when trace is not needed. * iseq.c (rb_iseq_trace_set): fix trace-off process (it was never off tracing). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60817 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- iseq.c | 19 +++++++++---------- iseq.h | 1 + vm_insnhelper.c | 7 +++++++ vm_trace.c | 10 +++++++--- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/iseq.c b/iseq.c index b2938588f2..3dda5159ec 100644 --- a/iseq.c +++ b/iseq.c @@ -336,8 +336,6 @@ prepare_iseq_build(rb_iseq_t *iseq, return Qtrue; } -static void rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events); - static VALUE finish_iseq_build(rb_iseq_t *iseq) { @@ -2311,15 +2309,16 @@ rb_iseq_defined_string(enum defined_type type) return str; } -#define TRACE_INSN_P(insn) ((insn) >= VM_INSTRUCTION_SIZE/2) #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE #define INSN_CODE(insn) ((VALUE)table[insn]) +#define TRACE_INSN_P(insn, insn_encoded) ((VALUE)table[insn] != insn_encoded) #else #define INSN_CODE(insn) (insn) +#define TRACE_INSN_P(insn, insn_encoded) ((insn_encoded) >= VM_INSTRUCTION_SIZE/2) #endif -static void +void rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events) { unsigned int i; @@ -2335,17 +2334,17 @@ rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events) int insn = (int)code[i]; rb_event_flag_t events = rb_iseq_event_flags(iseq, i); + /* code represents before transformation */ + VM_ASSERT(insn < VM_INSTRUCTION_SIZE/2); + if (events & turnon_events) { - if (!TRACE_INSN_P(insn)) { + if (!TRACE_INSN_P(insn, iseq_encoded[i])) { iseq_encoded[i] = INSN_CODE(insn + VM_INSTRUCTION_SIZE/2); } - else { - /* OK */ - } } - else if (TRACE_INSN_P(insn)) { + else if (TRACE_INSN_P(insn, iseq_encoded[i])) { VM_ASSERT(insn - VM_INSTRUCTION_SIZE/2 >= 0); - iseq_encoded[i] = INSN_CODE(insn - VM_INSTRUCTION_SIZE/2); + iseq_encoded[i] = INSN_CODE(insn); } i += insn_len(insn); } diff --git a/iseq.h b/iseq.h index 78272c07ae..8c8def5e0a 100644 --- a/iseq.h +++ b/iseq.h @@ -116,6 +116,7 @@ VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt); VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc); struct st_table *ruby_insn_make_insn_table(void); unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos); +void rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events); void rb_iseq_trace_set_all(rb_event_flag_t turnon_events); void rb_iseq_trace_on_all(void); diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 357a85d589..b172c2d20f 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -3729,9 +3729,16 @@ vm_trace(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, const VALUE *p { const rb_iseq_t *iseq = reg_cfp->iseq; size_t pos = pc - iseq->body->iseq_encoded; + rb_event_flag_t cur_event_flags = ruby_vm_event_flags; rb_event_flag_t events = rb_iseq_event_flags(iseq, pos); rb_event_flag_t event; + if ((events & cur_event_flags) == 0) { + /* disable trace */ + rb_iseq_trace_set(iseq, cur_event_flags); + return; + } + if (ec->trace_arg != NULL) return; if (0) { diff --git a/vm_trace.c b/vm_trace.c index efa2ee1bfb..d01a966a70 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -72,10 +72,14 @@ rb_vm_trace_mark_event_hooks(rb_hook_list_t *hooks) static void update_global_event_hook(rb_event_flag_t vm_events) { - if ((vm_events & RUBY_EVENTS_TRACE_BY_ISEQ) != - (ruby_vm_event_flags & RUBY_EVENTS_TRACE_BY_ISEQ)) { - rb_iseq_trace_set_all(vm_events); + rb_event_flag_t new_iseq_events = vm_events & RUBY_EVENTS_TRACE_BY_ISEQ; + rb_event_flag_t cur_iseq_events = ruby_vm_event_flags & RUBY_EVENTS_TRACE_BY_ISEQ; + + if (new_iseq_events > cur_iseq_events) { + /* write all ISeqs iff new events are added */ + rb_iseq_trace_set_all(vm_events); } + ruby_vm_event_flags = vm_events; rb_objspace_set_event_hook(vm_events); }