Skip to content

Commit 6da1540

Browse files
committed
feat: add out dir option
1 parent 87e0224 commit 6da1540

File tree

7 files changed

+69
-29
lines changed

7 files changed

+69
-29
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ A recorder of Ruby programs that produces [CodeTracer](https://github.com/metacr
1111
you can currently use it directly with
1212

1313
```bash
14-
ruby trace.rb <path to ruby file>
15-
# produces several trace json files in the current directory
16-
# or in the folder of `$CODETRACER_DB_TRACE_PATH` if such an env var is defined
14+
ruby trace.rb [--out-dir DIR] <path to ruby file>
15+
# produces several trace json files in DIR,
16+
# or in `$CODETRACER_RUBY_RECORDER_OUT_DIR` if DIR is not provided.
17+
# Defaults to the current directory.
18+
# Pass --help to list all options.
1719
```
1820

1921
You can also invoke a lightweight CLI that loads the native tracer extension
2022
directly:
2123

2224
```bash
23-
ruby src/native_trace.rb <path to ruby file>
25+
ruby src/native_trace.rb [--out-dir DIR] <path to ruby file>
26+
# Uses DIR or `$CODETRACER_RUBY_RECORDER_OUT_DIR` to choose where traces are saved.
2427
```
2528

2629
however you probably want to use it in combination with CodeTracer, which would be released soon.
@@ -34,7 +37,8 @@ The same environment is configured for GitHub Codespaces via devcontainer config
3437
### env variables
3538

3639
* if you pass `CODETRACER_RUBY_TRACER_DEBUG=1`, you enables some additional debug-related logging
37-
* `CODETRACER_DB_TRACE_PATH` can be used to override the path to `trace.json` (it's used internally by codetracer as well)
40+
* `CODETRACER_RUBY_RECORDER_OUT_DIR` can be used to specify the directory for trace files
41+
(used internally by codetracer as well; defaults to the current directory)
3842

3943
## future directions
4044

ext/native_tracer/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ The produced shared library can be required from Ruby:
2020
require_relative 'target/release/libcodetracer_ruby_recorder'
2121
```
2222

23-
Once loaded, the tracer starts writing a trace to `trace.json` or the
24-
path specified via the `CODETRACER_DB_TRACE_PATH` environment variable.
23+
Once loaded, the tracer starts writing a trace to `trace.json` in the
24+
directory specified via the `CODETRACER_RUBY_RECORDER_OUT_DIR` environment
25+
variable (defaults to the current directory).
2526

2627
## Publishing platform-specific gems
2728

src/native_trace.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@
22
# SPDX-License-Identifier: MIT
33
# Simple utility loading the native tracer extension and executing a program.
44

5+
require 'optparse'
6+
7+
options = {}
8+
parser = OptionParser.new do |opts|
9+
opts.banner = "usage: ruby native_trace.rb [options] <program> [args]"
10+
opts.on('-o DIR', '--out-dir DIR', 'Directory to write trace files') do |dir|
11+
options[:out_dir] = dir
12+
end
13+
opts.on('-h', '--help', 'Print this help') do
14+
puts opts
15+
exit
16+
end
17+
end
18+
parser.order!
19+
520
if ARGV.empty?
6-
$stderr.puts("usage: ruby native_trace.rb <program> [args]")
21+
$stderr.puts parser
722
exit 1
823
end
924

25+
out_dir = options[:out_dir] || ENV['CODETRACER_RUBY_RECORDER_OUT_DIR'] || Dir.pwd
26+
ENV['CODETRACER_RUBY_RECORDER_OUT_DIR'] = out_dir
27+
1028
# Path to the compiled native extension
1129
ext_path = File.expand_path('../ext/native_tracer/target/release/libcodetracer_ruby_recorder', __dir__)
1230
require ext_path

src/recorder.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def load_exprs(path)
245245
{}
246246
end
247247

248-
def serialize(program)
248+
def serialize(program, out_dir = nil)
249249
if ENV["CODETRACER_RUBY_TRACER_DEBUG"] == "1"
250250
pp @events
251251
end
@@ -263,11 +263,14 @@ def serialize(program)
263263
metadata_json_output = JSON.pretty_generate(metadata_output)
264264
paths_json_output = JSON.pretty_generate($codetracer_record.paths)
265265

266-
trace_path = ENV["CODETRACER_DB_TRACE_PATH"] || "trace.json"
267-
trace_folder = File.dirname(trace_path)
266+
out_dir = out_dir.nil? || out_dir.empty? ?
267+
(ENV["CODETRACER_RUBY_RECORDER_OUT_DIR"] || ".") : out_dir
268+
269+
trace_folder = out_dir
268270
FileUtils.mkdir_p(trace_folder)
269-
trace_metadata_path = File.join(trace_folder , "trace_metadata.json")
270-
trace_paths_path = File.join(trace_folder , "trace_paths.json")
271+
trace_path = File.join(trace_folder, "trace.json")
272+
trace_metadata_path = File.join(trace_folder, "trace_metadata.json")
273+
trace_paths_path = File.join(trace_folder, "trace_paths.json")
271274

272275
# p trace_path, json_output
273276
File.write(trace_path, json_output)

src/trace.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@
33
# See LICENSE file in the project root for full license information.
44

55
require 'json'
6+
require 'optparse'
67
require_relative 'recorder'
78

8-
if ARGV[0].nil?
9-
$stderr.puts("ruby trace.rb <program> [<args>]")
10-
exit(1)
9+
options = {}
10+
parser = OptionParser.new do |opts|
11+
opts.banner = "usage: ruby trace.rb [options] <program> [args]"
12+
opts.on('-o DIR', '--out-dir DIR', 'Directory to write trace files') do |dir|
13+
options[:out_dir] = dir
14+
end
15+
opts.on('-h', '--help', 'Print this help') do
16+
puts opts
17+
exit
18+
end
1119
end
20+
parser.order!
1221

13-
program = ARGV[0]
22+
program = ARGV.shift
23+
if program.nil?
24+
$stderr.puts parser
25+
exit 1
26+
end
1427

1528
# Warning:
1629
# probably related to our development env:
@@ -265,8 +278,7 @@ def load_variables(binding)
265278
$tracer.ignore('gems/')
266279

267280

268-
trace_args = ARGV
269-
ARGV = ARGV[1..-1]
281+
trace_args = [program] + ARGV
270282
$tracer.activate
271283
begin
272284
Kernel.load(program)
@@ -289,4 +301,5 @@ def load_variables(binding)
289301

290302
$tracer.stop_tracing
291303

292-
$tracer.record.serialize(program)
304+
out_dir = options[:out_dir] || ENV['CODETRACER_RUBY_RECORDER_OUT_DIR'] || Dir.pwd
305+
$tracer.record.serialize(program, out_dir)

test/benchmarks/run_benchmark.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
abort("Unknown benchmark '#{BENCHMARK}'")
1515
end
1616

17-
PROGRAM = File.expand_path("programs/#{BENCHMARK}.rb", __dir__)
17+
PROGRAM = File.join('test', 'benchmarks', 'programs', "#{BENCHMARK}.rb")
1818
FIXTURE = File.expand_path("fixtures/#{BENCHMARK}_trace.json", __dir__)
1919
TMP_DIR = File.expand_path('tmp', __dir__)
20-
OUTPUT = File.join(TMP_DIR, "#{BENCHMARK}_trace.json")
20+
OUTPUT_DIR = File.join(TMP_DIR, BENCHMARK)
2121
EXPECTED_HASH = HASHES[BENCHMARK]
2222

2323
FileUtils.mkdir_p(TMP_DIR)
24+
FileUtils.mkdir_p(OUTPUT_DIR)
2425

2526
unless File.exist?(FIXTURE) && Digest::SHA256.file(FIXTURE).hexdigest == EXPECTED_HASH
2627
warn "Reference trace missing or corrupt. Attempting to fetch via git lfs..."
@@ -31,8 +32,7 @@
3132
raise 'reference trace hash mismatch' unless Digest::SHA256.file(FIXTURE).hexdigest == EXPECTED_HASH
3233

3334
elapsed = Benchmark.realtime do
34-
env = { 'CODETRACER_DB_TRACE_PATH' => OUTPUT }
35-
system(env, 'ruby', File.expand_path('../../src/trace.rb', __dir__), PROGRAM)
35+
system('ruby', File.expand_path('../../src/trace.rb', __dir__), '--out-dir', OUTPUT_DIR, PROGRAM)
3636
raise 'trace failed' unless $?.success?
3737
end
3838
puts "Benchmark runtime: #{(elapsed * 1000).round} ms"
@@ -43,7 +43,8 @@ def files_identical?(a, b)
4343
File.binread(a) == File.binread(b)
4444
end
4545

46-
if files_identical?(FIXTURE, OUTPUT)
46+
OUTPUT_TRACE = File.join(OUTPUT_DIR, 'trace.json')
47+
if files_identical?(FIXTURE, OUTPUT_TRACE)
4748
puts 'Trace matches reference.'
4849
else
4950
warn 'Trace differs from reference!'

test/test_tracer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def run_trace(program_name)
1414
base = File.basename(program_name, '.rb')
1515
Dir.chdir(File.expand_path('..', __dir__)) do
1616
program = File.join('test', 'programs', program_name)
17-
output = File.join('test', 'tmp', "#{base}_trace.json")
18-
env = { 'CODETRACER_DB_TRACE_PATH' => output }
19-
system(env, 'ruby', 'src/trace.rb', program)
17+
out_dir = File.join('test', 'tmp', base)
18+
FileUtils.mkdir_p(out_dir)
19+
system('ruby', 'src/trace.rb', '--out-dir', out_dir, program)
2020
raise "trace failed" unless $?.success?
21+
JSON.parse(File.read(File.join(out_dir, 'trace.json')))
2122
end
22-
JSON.parse(File.read(File.join(TMP_DIR, "#{base}_trace.json")))
2323
end
2424

2525
def expected_trace(program_name)

0 commit comments

Comments
 (0)