Skip to content

Commit

Permalink
Support installing the gem on TruffleRuby
Browse files Browse the repository at this point in the history
* Fixes #159
  • Loading branch information
eregon committed Aug 13, 2022
1 parent 36a4711 commit 8245c33
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [ ruby-head, '3.1', '3.0', '2.7', '2.6', '2.5', '2.4', '2.3', '2.2' ]
ruby: [ ruby-head, '3.1', '3.0', '2.7', '2.6', '2.5', '2.4', '2.3', '2.2', truffleruby ]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
18 changes: 14 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ Rake::TestTask.new(:test) do |t|
t.test_files = FileList["test/**/test_*.rb"]
end

require "rake/extensiontask"
if RUBY_ENGINE == "truffleruby"
task :compile do
# noop
end

Rake::ExtensionTask.new("stackprof") do |ext|
ext.ext_dir = "ext/stackprof"
ext.lib_dir = "lib/stackprof"
task :clean do
# noop
end
else
require "rake/extensiontask"

Rake::ExtensionTask.new("stackprof") do |ext|
ext.ext_dir = "ext/stackprof"
ext.lib_dir = "lib/stackprof"
end
end

task default: %i(compile test)
6 changes: 6 additions & 0 deletions ext/stackprof/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require 'mkmf'

if RUBY_ENGINE == 'truffleruby'
File.write('Makefile', dummy_makefile($srcdir).join(""))
return
end

if have_func('rb_postponed_job_register_one') &&
have_func('rb_profile_frames') &&
have_func('rb_tracepoint_new') &&
Expand Down
6 changes: 5 additions & 1 deletion lib/stackprof.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require "stackprof/stackprof"
if RUBY_ENGINE == 'truffleruby'
require "stackprof/truffleruby"
else
require "stackprof/stackprof"
end

if defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?
StackProf.use_postponed_job!
Expand Down
37 changes: 37 additions & 0 deletions lib/stackprof/truffleruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module StackProf
# Define the same methods as stackprof.c
class << self
def running?
false
end

def run(*args)
unimplemented
end

def start(*args)
unimplemented
end

def stop
unimplemented
end

def results(*args)
unimplemented
end

def sample
unimplemented
end

def use_postponed_job!
# noop
end

private def unimplemented
raise "Use --cpusampler=flamegraph or --cpusampler instead of StackProf on TruffleRuby.\n" \
"See https://www.graalvm.org/tools/profiling/ and `ruby --help:cpusampler` for more details."
end
end
end
2 changes: 1 addition & 1 deletion test/test_stackprof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,4 @@ def idle
r.close
w.close
end
end
end unless RUBY_ENGINE == 'truffleruby'
18 changes: 18 additions & 0 deletions test/test_truffleruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$:.unshift File.expand_path('../../lib', __FILE__)
require 'stackprof'
require 'minitest/autorun'

if RUBY_ENGINE == 'truffleruby'
class StackProfTruffleRubyTest < MiniTest::Test
def test_error
error = assert_raises RuntimeError do
StackProf.run(mode: :cpu) do
unreacheable
end
end

assert_match(/TruffleRuby/, error.message)
assert_match(/--cpusampler/, error.message)
end
end
end

0 comments on commit 8245c33

Please sign in to comment.