Skip to content

Commit

Permalink
add option --start-paused
Browse files Browse the repository at this point in the history
  • Loading branch information
paddor committed Jan 17, 2024
1 parent d7d6c8d commit da949ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
11 changes: 9 additions & 2 deletions bin/ruby-prof
Expand Up @@ -45,6 +45,7 @@ module RubyProf
# self - Self time
# wait - Wait time
# child - Child time
# --start-paused Pause the profile immediately (resume manually with RubyProf::Profile.current.resume)
# --track_allocations Track allocations while profiling
# -v, --version version Show version (1.1.0)
# -h, --help Show help message
Expand Down Expand Up @@ -73,6 +74,7 @@ module RubyProf
options.pre_libs = Array.new
options.pre_execs = Array.new
options.printer = RubyProf::FlatPrinter
options.start_paused = false
options.track_allocations = false
end

Expand Down Expand Up @@ -232,6 +234,10 @@ module RubyProf
end
end

opts.on('--start-paused', "Pause the profile immediately (resume manually with RubyProf::Profile.current.resume)") do
options.start_paused = true
end

opts.on('--track_allocations', 'Track allocations while profiling') do
options.track_allocations = true
end
Expand Down Expand Up @@ -299,8 +305,9 @@ module RubyProf
end

script = ARGV.shift
profile.profile do
load script

profile.profile paused: options.start_paused do
load script
end
end
end
Expand Down
22 changes: 15 additions & 7 deletions ext/ruby_prof/rp_profile.c
Expand Up @@ -634,8 +634,16 @@ static VALUE prof_profile_track_allocations(VALUE self)
start -> self
Starts recording profile data.*/
static VALUE prof_start(VALUE self)
static VALUE prof_start(int argc, VALUE* argv, VALUE self)
{
VALUE keywords;
rb_scan_args_kw(RB_SCAN_ARGS_KEYWORDS, argc, argv, ":", &keywords);

ID table[] = {rb_intern("paused")};
VALUE values[1];
rb_get_kwargs(keywords, table, 0, 1, values);
VALUE paused = values[0] == Qundef ? Qfalse : values[0];

char* trace_file_name;

prof_profile_t* profile = prof_get_profile(self);
Expand All @@ -646,7 +654,7 @@ static VALUE prof_start(VALUE self)
}

profile->running = Qtrue;
profile->paused = Qfalse;
profile->paused = paused;
profile->last_thread_data = threads_table_insert(profile, rb_fiber_current());

/* open trace file if environment wants it */
Expand Down Expand Up @@ -809,7 +817,7 @@ static VALUE prof_remove_thread(VALUE self, VALUE thread)
..
end
*/
static VALUE prof_profile_instance(VALUE self)
static VALUE prof_profile_instance(int argc, VALUE* argv, VALUE self)
{
int result;
prof_profile_t* profile = prof_get_profile(self);
Expand All @@ -819,7 +827,7 @@ static VALUE prof_profile_instance(VALUE self)
rb_raise(rb_eArgError, "A block must be provided to the profile method.");
}

prof_start(self);
prof_start(argc, argv, self);
rb_protect(rb_yield, self, &result);
self = prof_stop(self);

Expand All @@ -845,7 +853,7 @@ static VALUE prof_profile_instance(VALUE self)
*/
static VALUE prof_profile_class(int argc, VALUE* argv, VALUE klass)
{
return prof_profile_instance(rb_class_new_instance(argc, argv, cProfile));
return prof_profile_instance(0, NULL, rb_class_new_instance(argc, argv, cProfile));
}

/* call-seq:
Expand Down Expand Up @@ -916,8 +924,8 @@ void rp_init_profile(void)

rb_define_singleton_method(cProfile, "profile", prof_profile_class, -1);
rb_define_method(cProfile, "initialize", prof_initialize, -1);
rb_define_method(cProfile, "profile", prof_profile_instance, 0);
rb_define_method(cProfile, "start", prof_start, 0);
rb_define_method(cProfile, "profile", prof_profile_instance, -1);
rb_define_method(cProfile, "start", prof_start, -1);
rb_define_method(cProfile, "stop", prof_stop, 0);
rb_define_method(cProfile, "resume", prof_resume, 0);
rb_define_method(cProfile, "pause", prof_pause, 0);
Expand Down

0 comments on commit da949ca

Please sign in to comment.