Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion bin/vernier
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

load "#{__dir__}/../exe/vernier"
#
# This file was generated by Bundler.
#
# The application 'vernier' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("vernier", "vernier")
2 changes: 1 addition & 1 deletion examples/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def show
# warm up
make_request.call

Vernier.trace(out: "rails.json") do |collector|
Vernier.trace(out: "rails.json", mode: :jit_side_exit) do |collector|
ActiveSupport::Notifications.monotonic_subscribe do |name, start, finish, id, payload|
collector.add_marker(
name:,
Expand Down
2 changes: 1 addition & 1 deletion examples/threaded_http_requests.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "vernier"

Vernier.trace(out: "http_requests.json") do
Vernier.trace(out: "http_requests.json", mode: :jit_side_exit) do

require "net/http"
require "uri"
Expand Down
3 changes: 3 additions & 0 deletions exe/vernier
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ parser = OptionParser.new(banner) do |o|
o.on('--start-paused', "don't automatically start the profiler") do
options[:start_paused] = true
end
o.on('--mode [MODE]', String) do |s|
options[:mode] = s
end
end

parser.parse!
Expand Down
61 changes: 61 additions & 0 deletions ext/vernier/vernier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,65 @@ class CustomCollector : public BaseCollector {
}
};

class SideExitCollector : public BaseCollector {
SampleList samples;

void sample() {
RawSample sample;
sample.sample();
int stack_index = frame_list.stack_index(sample);

native_thread_id_t thread_id = 0;
samples.record_sample(stack_index, TimeStamp::Now(), thread_id, CATEGORY_NORMAL);
}

static void side_exit_cb(rb_event_flag_t evflag, VALUE data, VALUE self, ID mid, VALUE klass) {
SideExitCollector *collector = static_cast<SideExitCollector *>((void *)NUM2ULL(data));
collector->sample();
}

bool start() {
if (!BaseCollector::start()) {
return false;
}

rb_add_event_hook(side_exit_cb, RUBY_INTERNAL_EVENT_JIT_SIDE_EXIT, PTR2NUM(this));

return true;
}

VALUE stop() {
BaseCollector::stop();

frame_list.finalize();

VALUE result = build_collector_result();

reset();

return result;
}

VALUE build_collector_result() {
VALUE result = BaseCollector::build_collector_result();

VALUE threads = rb_hash_new();
rb_ivar_set(result, rb_intern("@threads"), threads);

VALUE thread_hash = rb_hash_new();
samples.write_result(thread_hash);

rb_hash_aset(threads, ULL2NUM(0), thread_hash);
rb_hash_aset(thread_hash, sym("tid"), ULL2NUM(0));
rb_hash_aset(thread_hash, sym("name"), rb_str_new_cstr("side exits"));
rb_hash_aset(thread_hash, sym("started_at"), ULL2NUM(started_at.nanoseconds()));

frame_list.write_result(result);

return result;
}
};

class RetainedCollector : public BaseCollector {
void reset() {
object_frames.clear();
Expand Down Expand Up @@ -1403,6 +1462,8 @@ static VALUE collector_new(VALUE self, VALUE mode, VALUE options) {
collector = new RetainedCollector();
} else if (mode == sym("custom")) {
collector = new CustomCollector();
} else if (mode == sym("jit_side_exit")) {
collector = new SideExitCollector();
} else if (mode == sym("wall")) {
VALUE intervalv = rb_hash_aref(options, sym("interval"));
TimeStamp interval;
Expand Down