Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option --dont-start and make current profile available as RubyProf::Profile.current #330

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
26 changes: 19 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 All @@ -668,6 +676,8 @@ static VALUE prof_start(VALUE self)
}
}

rb_iv_set(cProfile, "@current", self);

prof_install_hook(self);
return self;
}
Expand Down Expand Up @@ -749,6 +759,8 @@ static VALUE prof_stop(VALUE self)
profile->running = profile->paused = Qfalse;
profile->last_thread_data = NULL;

rb_iv_set(cProfile, "@current", Qnil);

return self;
}

Expand Down Expand Up @@ -805,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 @@ -815,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 @@ -841,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 @@ -912,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
4 changes: 4 additions & 0 deletions lib/ruby-prof/profile.rb
Expand Up @@ -4,6 +4,10 @@

module RubyProf
class Profile
class << self
attr_accessor :current
end

def measure_mode_string
case self.measure_mode
when WALL_TIME
Expand Down
18 changes: 18 additions & 0 deletions test/profile_test.rb
Expand Up @@ -5,6 +5,24 @@
require_relative './call_tree_builder'

class ProfileTest < TestCase
def test_current
profile = RubyProf::Profile.new
refute_same(profile, RubyProf::Profile.current)

yielded = false
profile.profile do
yielded = true
assert_same(profile, RubyProf::Profile.current)
end
assert(yielded)
assert_nil(RubyProf::Profile.current)

profile.start
assert_same(profile, RubyProf::Profile.current)
profile.stop
assert_nil(RubyProf::Profile.current)
end

def test_measure_mode
profile = RubyProf::Profile.new(:measure_mode => RubyProf::PROCESS_TIME)
assert_equal(RubyProf::PROCESS_TIME, profile.measure_mode)
Expand Down