runtime_analysis 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +115 -0
- data/LICENSE.txt +96 -0
- data/README.md +164 -0
- data/editor/nvim/README.md +278 -0
- data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
- data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
- data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
- data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
- data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
- data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
- data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
- data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
- data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
- data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
- data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
- data/editor/nvim/plugin/runtime_analysis.lua +71 -0
- data/examples/trackers/PROMPT.md +86 -0
- data/examples/trackers/honeybadger.rb +19 -0
- data/examples/trackers/sentry.rb +16 -0
- data/ext/runtime_analysis/extconf.rb +68 -0
- data/ext/runtime_analysis/fast.c +775 -0
- data/ext/runtime_analysis/fast.h +59 -0
- data/ext/runtime_analysis/hold.c +560 -0
- data/ext/runtime_analysis/init.c +36 -0
- data/ext/runtime_analysis/names.c +78 -0
- data/ext/runtime_analysis/names.h +28 -0
- data/ext/runtime_analysis/novelty.c +139 -0
- data/ext/runtime_analysis/novelty.h +22 -0
- data/ext/runtime_analysis/os.c +73 -0
- data/ext/runtime_analysis/os.h +32 -0
- data/ext/runtime_analysis/ring.c +445 -0
- data/ext/runtime_analysis/rows.c +304 -0
- data/ext/runtime_analysis/sink.c +485 -0
- data/ext/runtime_analysis/sink.h +240 -0
- data/ext/runtime_analysis/state.c +254 -0
- data/ext/runtime_analysis/state.h +57 -0
- data/ext/runtime_analysis/wrapper.c +278 -0
- data/ext/runtime_analysis/wrapper.h +24 -0
- data/lib/runtime_analysis/errors.rb +34 -0
- data/lib/runtime_analysis/failsafe.rb +123 -0
- data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
- data/lib/runtime_analysis/host/declared.rb +152 -0
- data/lib/runtime_analysis/host/inject.rb +90 -0
- data/lib/runtime_analysis/host/job_runners.rb +27 -0
- data/lib/runtime_analysis/host/middleware.rb +23 -0
- data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
- data/lib/runtime_analysis/host/railtie.rb +42 -0
- data/lib/runtime_analysis/host/reports.rb +54 -0
- data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
- data/lib/runtime_analysis/host/web.rb +22 -0
- data/lib/runtime_analysis/host.rb +34 -0
- data/lib/runtime_analysis/inject.rb +5 -0
- data/lib/runtime_analysis/minitest.rb +9 -0
- data/lib/runtime_analysis/native/build.rb +17 -0
- data/lib/runtime_analysis/native/byte_size.rb +27 -0
- data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
- data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
- data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
- data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
- data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
- data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
- data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
- data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
- data/lib/runtime_analysis/native/sink.rb +12 -0
- data/lib/runtime_analysis/native.rb +74 -0
- data/lib/runtime_analysis/platform.rb +30 -0
- data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
- data/lib/runtime_analysis/query/index/graph.rb +107 -0
- data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
- data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
- data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
- data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
- data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
- data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
- data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
- data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
- data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
- data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
- data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
- data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
- data/lib/runtime_analysis/query/index/reading.rb +80 -0
- data/lib/runtime_analysis/query/index/shapes.rb +142 -0
- data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
- data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
- data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
- data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
- data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
- data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
- data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
- data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
- data/lib/runtime_analysis/query/index/signatures.rb +93 -0
- data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
- data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
- data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
- data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
- data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
- data/lib/runtime_analysis/query/index/static/method.rb +19 -0
- data/lib/runtime_analysis/query/index/static/params.rb +52 -0
- data/lib/runtime_analysis/query/index/static/source.rb +69 -0
- data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
- data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
- data/lib/runtime_analysis/query/index/static/value.rb +17 -0
- data/lib/runtime_analysis/query/index/static/values.rb +71 -0
- data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
- data/lib/runtime_analysis/query/index/static.rb +85 -0
- data/lib/runtime_analysis/query/index.rb +32 -0
- data/lib/runtime_analysis/query/nodes.rb +60 -0
- data/lib/runtime_analysis/query/project/config.rb +108 -0
- data/lib/runtime_analysis/query/project.rb +40 -0
- data/lib/runtime_analysis/query/report/document.rb +84 -0
- data/lib/runtime_analysis/query/report/finding.rb +15 -0
- data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
- data/lib/runtime_analysis/query/report/list.rb +15 -0
- data/lib/runtime_analysis/query/report/location.rb +14 -0
- data/lib/runtime_analysis/query/report/one_row.rb +14 -0
- data/lib/runtime_analysis/query/report/outline.rb +15 -0
- data/lib/runtime_analysis/query/report/paint.rb +34 -0
- data/lib/runtime_analysis/query/report/related.rb +11 -0
- data/lib/runtime_analysis/query/report/table.rb +15 -0
- data/lib/runtime_analysis/query/report/text.rb +13 -0
- data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
- data/lib/runtime_analysis/query/report.rb +35 -0
- data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
- data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
- data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
- data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
- data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
- data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
- data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
- data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
- data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
- data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
- data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
- data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
- data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
- data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
- data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
- data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
- data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
- data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
- data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
- data/lib/runtime_analysis/query/surfaces.rb +14 -0
- data/lib/runtime_analysis/query/tools/base.rb +86 -0
- data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
- data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
- data/lib/runtime_analysis/query/tools/origins.rb +47 -0
- data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
- data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
- data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
- data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
- data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
- data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
- data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
- data/lib/runtime_analysis/query/tools.rb +34 -0
- data/lib/runtime_analysis/query.rb +33 -0
- data/lib/runtime_analysis/recording/blob.rb +13 -0
- data/lib/runtime_analysis/recording/format.rb +32 -0
- data/lib/runtime_analysis/recording/opcodes.rb +30 -0
- data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
- data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
- data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
- data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
- data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
- data/lib/runtime_analysis/recording/seen.rb +63 -0
- data/lib/runtime_analysis/recording/sink.rb +19 -0
- data/lib/runtime_analysis/recording/source_blob.rb +20 -0
- data/lib/runtime_analysis/recording.rb +52 -0
- data/lib/runtime_analysis/rspec.rb +8 -0
- data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
- data/lib/runtime_analysis/ruby_parsing.rb +98 -0
- data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
- data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
- data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
- data/lib/runtime_analysis/trace/configuration.rb +115 -0
- data/lib/runtime_analysis/trace/entries.rb +47 -0
- data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
- data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
- data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
- data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
- data/lib/runtime_analysis/trace/hold.rb +142 -0
- data/lib/runtime_analysis/trace/inclusion.rb +46 -0
- data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
- data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
- data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
- data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
- data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
- data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
- data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
- data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
- data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
- data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
- data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
- data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
- data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
- data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
- data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
- data/lib/runtime_analysis/trace/installer.rb +149 -0
- data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
- data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
- data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
- data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
- data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
- data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
- data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
- data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
- data/lib/runtime_analysis/trace/mechanism.rb +28 -0
- data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
- data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
- data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
- data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
- data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
- data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
- data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
- data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
- data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
- data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
- data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
- data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
- data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
- data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
- data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
- data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
- data/lib/runtime_analysis/trace/novelty.rb +17 -0
- data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
- data/lib/runtime_analysis/trace/pure/state.rb +94 -0
- data/lib/runtime_analysis/trace/pure.rb +15 -0
- data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
- data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
- data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
- data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
- data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
- data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
- data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
- data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
- data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
- data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
- data/lib/runtime_analysis/trace/recorder.rb +149 -0
- data/lib/runtime_analysis/trace/state.rb +16 -0
- data/lib/runtime_analysis/trace.rb +142 -0
- data/lib/runtime_analysis/version.rb +6 -0
- data/lib/runtime_analysis/web.rb +6 -0
- data/lib/runtime_analysis.rb +125 -0
- metadata +330 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted through call_site_logger_spec.rb, which rolls segments and caps what a recording keeps.
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Native
|
|
7
|
+
class CallSiteLogger
|
|
8
|
+
module Segments
|
|
9
|
+
UNDER_EVERY_LIMIT = -Platform.unbounded
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def free_segment_path(stream)
|
|
14
|
+
stream.seq = free_seq(stream.tid, stream.seq)
|
|
15
|
+
segment_path(stream.tid, stream.seq)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def free_seq(tid, seq)
|
|
19
|
+
taken_seqs(tid).inject(seq) { |free, taken| taken == free ? free + 1 : free }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def taken_seqs(tid)
|
|
23
|
+
stream = stream_path(tid)
|
|
24
|
+
matching_segments.select { |file| file.rpartition(".").first == stream }.map { |file| seq_of(file) }.sort
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def segment_path(tid, seq)
|
|
28
|
+
"#{stream_path(tid)}.#{seq}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def stream_path(tid)
|
|
32
|
+
"#{@base}.#{Process.pid}.#{tid}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def maybe_rotate(stream)
|
|
36
|
+
return if @hold
|
|
37
|
+
|
|
38
|
+
age = Platform.monotonic_seconds - stream.opened_at
|
|
39
|
+
roll!(stream) if @rotation.roll?(bytes: segment_bytes(stream), age: age, trees: stream.tree_count)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def segment_bytes(stream)
|
|
43
|
+
bytes = stream.sink.bytes_written
|
|
44
|
+
stream.stale = nil if stream.stale && bytes < stream.stale
|
|
45
|
+
stream.stale ? UNDER_EVERY_LIMIT : bytes
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def roll!(stream)
|
|
49
|
+
stream.seq += 1
|
|
50
|
+
stream.sink.roll(segment_path(stream.tid, stream.seq))
|
|
51
|
+
begin_segment(stream).stale = stream.sink.bytes_written
|
|
52
|
+
enforce_retain!
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def begin_segment(stream)
|
|
56
|
+
forget_definitions(stream)
|
|
57
|
+
stream.opened_at = Platform.monotonic_seconds
|
|
58
|
+
stream.tree_count = 0
|
|
59
|
+
stream
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def forget_definitions(stream)
|
|
63
|
+
stream.dict = {}
|
|
64
|
+
stream.paths = []
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def enforce_retain!
|
|
68
|
+
return unless @retain
|
|
69
|
+
|
|
70
|
+
segments = matching_segments
|
|
71
|
+
evict_oldest_closed(closed_segments(segments), segments.sum { |file| File.size(file) })
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def matching_segments
|
|
75
|
+
Dir.glob("#{@base}.*").select { |file| file.match?(/\.\d+\.\d+\.(?:0|[1-9]\d*)\z/) }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def closed_segments(segments)
|
|
79
|
+
segments
|
|
80
|
+
.group_by { |file| file.rpartition(".").first }
|
|
81
|
+
.flat_map { |_stream, files| files - [files.max_by { |file| seq_of(file) }] }
|
|
82
|
+
.sort_by { |file| [File.mtime(file), seq_of(file)] }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def evict_oldest_closed(closed, total)
|
|
86
|
+
closed.each do |f|
|
|
87
|
+
break if total <= @retain
|
|
88
|
+
|
|
89
|
+
total -= File.size(f)
|
|
90
|
+
File.delete(f)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def seq_of(file)
|
|
95
|
+
Integer(file.rpartition(".").last)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted through call_site_logger_spec.rb, which records from many threads and across a fork.
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Native
|
|
7
|
+
class CallSiteLogger
|
|
8
|
+
module Streams
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
def init_streams(units)
|
|
12
|
+
@units = units
|
|
13
|
+
@mutex = Mutex.new
|
|
14
|
+
@next_tid = @largest = 0
|
|
15
|
+
forget_streams
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def stream_for(step)
|
|
19
|
+
stream = ensure_stream
|
|
20
|
+
return stream unless step == 1
|
|
21
|
+
|
|
22
|
+
stream = heal(stream) if stream.sink.disabled?
|
|
23
|
+
stream.tree_count += 1
|
|
24
|
+
stream
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def close_streams
|
|
28
|
+
forked? ? Native.reset_after_fork : @streams.each { |stream| close(stream) }
|
|
29
|
+
forget_streams
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def close(stream)
|
|
33
|
+
@largest = [@largest, stream.sink.largest_request].max
|
|
34
|
+
stream.sink.close
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def ensure_stream
|
|
38
|
+
cached_stream || open_stream_for_current_thread
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def cached_stream
|
|
42
|
+
cache = Thread.current.thread_variable_get(:_ra_stream)
|
|
43
|
+
cache.stream if cache&.epoch.equal?(@epoch) && started_here?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def open_stream_for_current_thread
|
|
47
|
+
@mutex.synchronize do
|
|
48
|
+
start unless started_here?
|
|
49
|
+
remember(add_stream)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def add_stream
|
|
54
|
+
reap_dead_streams
|
|
55
|
+
tid = (@next_tid += 1)
|
|
56
|
+
@streams << begin_segment(Stream.new(open_sink(tid), tid, Thread.current, {}, 0))
|
|
57
|
+
@streams.last
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def open_sink(tid)
|
|
61
|
+
@hold ? Sink.hold(@units) : Sink.open(segment_path(tid, 0), @units)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def reap_dead_streams
|
|
65
|
+
@streams, dead = @streams.partition { |stream| stream.thread.alive? }
|
|
66
|
+
dead.each { |stream| close(stream) }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def heal(dead)
|
|
70
|
+
@mutex.synchronize do
|
|
71
|
+
retire(dead)
|
|
72
|
+
remember(add_stream)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def retire(stream)
|
|
77
|
+
remember(nil)
|
|
78
|
+
close(stream)
|
|
79
|
+
@streams.delete(stream)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def remember(stream)
|
|
83
|
+
Thread.current.thread_variable_set(:_ra_stream, StreamCache.new(@epoch, stream))
|
|
84
|
+
stream
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def start
|
|
88
|
+
forget_streams
|
|
89
|
+
Native.reset_after_fork
|
|
90
|
+
FileUtils.mkdir_p(File.dirname(@base))
|
|
91
|
+
@pid = Process.pid
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def started_here?
|
|
95
|
+
@pid.equal?(Process.pid)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def forked?
|
|
99
|
+
@pid && !started_here?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def forget_streams
|
|
103
|
+
@pid = nil
|
|
104
|
+
@streams = []
|
|
105
|
+
@epoch = Object.new
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "runtime_analysis/recording"
|
|
5
|
+
require_relative "rotation_policy"
|
|
6
|
+
require "runtime_analysis/platform"
|
|
7
|
+
require_relative "call_site_logger/rows"
|
|
8
|
+
require_relative "call_site_logger/held"
|
|
9
|
+
require_relative "call_site_logger/streams"
|
|
10
|
+
require_relative "call_site_logger/segments"
|
|
11
|
+
require_relative "call_site_logger/definitions"
|
|
12
|
+
|
|
13
|
+
module RuntimeAnalysis
|
|
14
|
+
module Native
|
|
15
|
+
class CallSiteLogger
|
|
16
|
+
include Rows
|
|
17
|
+
include Held
|
|
18
|
+
include Streams
|
|
19
|
+
include Segments
|
|
20
|
+
include Definitions
|
|
21
|
+
|
|
22
|
+
DICT = Recording.opcode(:dict)
|
|
23
|
+
RETURN = Recording.opcode(:return)
|
|
24
|
+
KWARGS = Recording.opcode(:kwargs)
|
|
25
|
+
UNWIND = Recording.opcode(:unwind)
|
|
26
|
+
ENTRY = Recording.opcode(:entry)
|
|
27
|
+
INNER_ENTRY = Recording.opcode(:inner_entry)
|
|
28
|
+
VALUE = Recording.opcode(:value)
|
|
29
|
+
KEYWORD_VALUE = 2
|
|
30
|
+
ANSWERED = Recording.opcode(:answered)
|
|
31
|
+
PATH = Recording.opcode(:path)
|
|
32
|
+
CALL = Recording.opcode(:call)
|
|
33
|
+
HELD = Recording.opcode(:held)
|
|
34
|
+
|
|
35
|
+
RAISE_IDS = Recording.raise_ids
|
|
36
|
+
|
|
37
|
+
RING_UNIT_BYTES = 512
|
|
38
|
+
DEFAULT_RING_BYTES = 8 * 1024 * 1024
|
|
39
|
+
|
|
40
|
+
MAX_ARGS = 16
|
|
41
|
+
|
|
42
|
+
Stream = Struct.new(:sink, :tid, :thread, :ids, :seq, :dict, :paths, :opened_at, :tree_count, :stale)
|
|
43
|
+
|
|
44
|
+
StreamCache = Struct.new(:epoch, :stream)
|
|
45
|
+
|
|
46
|
+
def initialize(path = nil, recording_dir: nil, rotation: RotationPolicy.new, hold: false,
|
|
47
|
+
ring: DEFAULT_RING_BYTES)
|
|
48
|
+
@base = path || recording_dir_base(recording_dir) || default_path
|
|
49
|
+
@hold = hold
|
|
50
|
+
@rotation = rotation
|
|
51
|
+
@retain = rotation.retain
|
|
52
|
+
init_streams(ring / RING_UNIT_BYTES)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def finish!
|
|
56
|
+
@mutex.synchronize { close_streams }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def write(stream, kind, row)
|
|
62
|
+
stream.sink.write(kind, row)
|
|
63
|
+
maybe_rotate(stream)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def write_call(stream, dropped_before, row)
|
|
67
|
+
stream.sink.write(CALL, row) if stream.sink.dropped == dropped_before
|
|
68
|
+
maybe_rotate(stream)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def recording_dir_base(dir)
|
|
72
|
+
dir && File.join(dir, "query_parameters.json")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def default_path
|
|
76
|
+
"./tmp/runtime_analysis/native_query_parameters.json"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "byte_size"
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Native
|
|
7
|
+
class RotationPolicy
|
|
8
|
+
MODES = { any: :any?, all: :all? }.freeze
|
|
9
|
+
|
|
10
|
+
attr_reader :retain
|
|
11
|
+
|
|
12
|
+
def initialize(max_bytes: nil, max_age: nil, max_trees: nil, mode: :any, retain: nil)
|
|
13
|
+
@retain = ByteSize.parse(retain)
|
|
14
|
+
@limits = { bytes: ByteSize.parse(max_bytes), age: max_age, trees: max_trees }.compact
|
|
15
|
+
@combined = MODES.fetch(mode) { raise ArgumentError, "unknown rotation mode #{mode.inspect}, use :any or :all" }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def roll?(bytes:, age:, trees:)
|
|
19
|
+
return false if @limits.empty?
|
|
20
|
+
|
|
21
|
+
{ bytes: bytes, age: age, trees: trees }
|
|
22
|
+
.slice(*@limits.keys)
|
|
23
|
+
.map { |dimension, value| value >= @limits.fetch(dimension) }
|
|
24
|
+
.public_send(@combined)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "shellwords"
|
|
4
|
+
require "timeout"
|
|
5
|
+
require "tmpdir"
|
|
6
|
+
require "runtime_analysis/errors"
|
|
7
|
+
require "runtime_analysis/platform"
|
|
8
|
+
|
|
9
|
+
module RuntimeAnalysis
|
|
10
|
+
module Native
|
|
11
|
+
module SanitizerRuntime
|
|
12
|
+
SOURCE = "int main(void) { return 0; }\n"
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def probe(flags, cc:, seconds:)
|
|
17
|
+
Dir.mktmpdir { |dir| started(build(dir, cc, flags), seconds) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def started(binary, seconds)
|
|
21
|
+
return :unbuilt unless binary
|
|
22
|
+
|
|
23
|
+
finishes?(binary, seconds) ? :starts : :hangs
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def build(dir, compiler, flags)
|
|
27
|
+
source = File.join(dir, "probe.c")
|
|
28
|
+
binary = File.join(dir, "probe")
|
|
29
|
+
File.write(source, SOURCE)
|
|
30
|
+
command = [*compiler.shellsplit, *flags.shellsplit, "-o", binary, source]
|
|
31
|
+
binary if system(*command, out: Platform.discard, err: Platform.discard)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def finishes?(program, seconds)
|
|
35
|
+
pid = spawn(kept_from_shell(program), out: Platform.discard, err: Platform.discard)
|
|
36
|
+
Timeout.timeout(seconds) { Process.waitpid(pid) }
|
|
37
|
+
rescue Errors.timed_out
|
|
38
|
+
abandon(pid)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def kept_from_shell(program) = [program, program]
|
|
42
|
+
|
|
43
|
+
def abandon(pid)
|
|
44
|
+
Process.kill("KILL", pid)
|
|
45
|
+
Process.waitpid(pid)
|
|
46
|
+
false
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuntimeAnalysis
|
|
4
|
+
module Native
|
|
5
|
+
class Sink
|
|
6
|
+
def self.rows
|
|
7
|
+
{ call: ROW_CALL, return: ROW_RETURN, kwargs: ROW_KWARGS, unwind: ROW_UNWIND,
|
|
8
|
+
entry: ROW_ENTRY, tail: ROW_TAIL, shift: ROW_SHIFT }
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "native/build"
|
|
4
|
+
require_relative "native/byte_size"
|
|
5
|
+
|
|
6
|
+
module RuntimeAnalysis
|
|
7
|
+
module Native
|
|
8
|
+
INSTALLED_GEM_FEATURE = "runtime_analysis/native_ext"
|
|
9
|
+
FEATURES = [Build.feature, INSTALLED_GEM_FEATURE].freeze
|
|
10
|
+
|
|
11
|
+
class Missing < LoadError; end
|
|
12
|
+
|
|
13
|
+
@problems = []
|
|
14
|
+
LOADED = FEATURES.any? do |feature|
|
|
15
|
+
require feature
|
|
16
|
+
true
|
|
17
|
+
rescue LoadError => e
|
|
18
|
+
@problems << e.message
|
|
19
|
+
false
|
|
20
|
+
end
|
|
21
|
+
require_relative "native/sink" if LOADED
|
|
22
|
+
|
|
23
|
+
def self.problems
|
|
24
|
+
@problems
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.loaded?
|
|
28
|
+
LOADED
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.require!
|
|
32
|
+
raise Missing, missing unless loaded?
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.wrapper = (Wrapper if loaded?)
|
|
36
|
+
|
|
37
|
+
def self.sink = (Sink if loaded?)
|
|
38
|
+
|
|
39
|
+
def self.testing = (Testing if const_defined?(:Testing, false))
|
|
40
|
+
|
|
41
|
+
def self.rows = (loaded? ? Sink.rows : {})
|
|
42
|
+
|
|
43
|
+
def self.logger(recording_dir:, rotate: {}, retain: nil, **shape)
|
|
44
|
+
require!
|
|
45
|
+
rotation = RotationPolicy.new(**rotate, retain: retain)
|
|
46
|
+
CallSiteLogger.new(nil, recording_dir: recording_dir, rotation: rotation, **shape)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.bytes(size) = ByteSize.parse(size)
|
|
50
|
+
|
|
51
|
+
def self.root = File.expand_path("../..", __dir__)
|
|
52
|
+
|
|
53
|
+
def self.installed?
|
|
54
|
+
Gem.path.any? { |home| root.start_with?(File.join(home, "gems", "")) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.missing
|
|
58
|
+
<<~MESSAGE
|
|
59
|
+
runtime_analysis records through its native extension, which is not built for Ruby #{RUBY_VERSION} on #{RUBY_PLATFORM}.
|
|
60
|
+
#{fix}
|
|
61
|
+
Ruby said: #{problems.join(" / ")}
|
|
62
|
+
MESSAGE
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.fix
|
|
66
|
+
return "It is built on CRuby alone. Record without it: config.mechanism = :pure" unless RUBY_ENGINE == "ruby"
|
|
67
|
+
return "Build it with: gem pristine runtime_analysis" if installed?
|
|
68
|
+
|
|
69
|
+
"Build it by running: cd #{root} && bundle exec rake compile"
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
require "runtime_analysis/native/call_site_logger"
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuntimeAnalysis
|
|
4
|
+
module Platform
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def monotonic_seconds = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
8
|
+
|
|
9
|
+
def discard = File::NULL
|
|
10
|
+
|
|
11
|
+
def search_path_separator = File::PATH_SEPARATOR
|
|
12
|
+
|
|
13
|
+
def without_waiting = Process::WNOHANG
|
|
14
|
+
|
|
15
|
+
def exit_status = Process::Status
|
|
16
|
+
|
|
17
|
+
def unbounded = Float::INFINITY
|
|
18
|
+
|
|
19
|
+
def reading_without_waiting = File::RDONLY | File::NONBLOCK
|
|
20
|
+
|
|
21
|
+
def c_compiler
|
|
22
|
+
require "rbconfig"
|
|
23
|
+
RbConfig::CONFIG.fetch("CC")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def lib = File.expand_path("..", __dir__)
|
|
27
|
+
|
|
28
|
+
def gem_root = File.dirname(lib)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
require "runtime_analysis/query/index"
|
|
5
|
+
|
|
6
|
+
module RuntimeAnalysis
|
|
7
|
+
module Query
|
|
8
|
+
module Index
|
|
9
|
+
module Anonymise
|
|
10
|
+
Meth = Struct.new(:identity, :klass, :name, :class_method, keyword_init: true)
|
|
11
|
+
Link = Struct.new(:caller, :callee, :calls, keyword_init: true)
|
|
12
|
+
|
|
13
|
+
Model = Data.define(:methods, :links)
|
|
14
|
+
Mapping = Data.define(:methods, :klasses)
|
|
15
|
+
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
def build(graph)
|
|
19
|
+
nodes = graph.trees.flat_map(&:nodes)
|
|
20
|
+
Model.new(methods: methods_in(nodes), links: links_in(nodes))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def methods_in(nodes)
|
|
24
|
+
frames = nodes.flat_map { |node| [node.container, node.callee] }
|
|
25
|
+
frames.uniq(&:identity).map { |frame| meth_from(frame) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def meth_from(frame)
|
|
29
|
+
Meth.new(identity: frame.identity, klass: frame.klass, name: frame.name,
|
|
30
|
+
class_method: frame.class_method)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def links_in(nodes)
|
|
34
|
+
nodes.group_by { |node| [node.container.identity, node.callee.identity] }
|
|
35
|
+
.map { |(from, to), group| Link.new(caller: from, callee: to, calls: group.size) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def tokenise(model, secret:)
|
|
39
|
+
tokens = Tokens.new(secret)
|
|
40
|
+
rewritten = model.methods.to_h { |meth| [meth.identity, tokens.rewrite(meth)] }
|
|
41
|
+
[Model.new(methods: rewritten.values.sort_by(&:identity), links: relink(model.links, rewritten)),
|
|
42
|
+
tokens.mapping(rewritten)]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def relink(links, rewritten)
|
|
46
|
+
links.map { |link| token_link(link, rewritten) }.sort_by { |link| [link.caller, link.callee] }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def token_link(link, rewritten)
|
|
50
|
+
Link.new(caller: rewritten.fetch(link.caller).identity,
|
|
51
|
+
callee: rewritten.fetch(link.callee).identity, calls: link.calls)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
class Tokens
|
|
55
|
+
TOKEN_BITS = 64
|
|
56
|
+
BITS_PER_HEX_DIGIT = 4
|
|
57
|
+
HEX_DIGITS = TOKEN_BITS / BITS_PER_HEX_DIGIT
|
|
58
|
+
|
|
59
|
+
def initialize(secret)
|
|
60
|
+
raise ArgumentError, "a secret is needed to tokenise" if secret.to_s.empty?
|
|
61
|
+
|
|
62
|
+
@secret = secret
|
|
63
|
+
@klasses = {}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def rewrite(meth)
|
|
67
|
+
Meth.new(identity: identity(meth), klass: klass(meth.klass), name: name(meth.name),
|
|
68
|
+
class_method: meth.class_method)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def identity(meth)
|
|
72
|
+
"#{klass(meth.klass)}#{meth.class_method ? "." : "#"}#{name(meth.name)}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def klass(value)
|
|
76
|
+
return MAIN if value == MAIN
|
|
77
|
+
|
|
78
|
+
@klasses[value] ||= "C#{digest("class", value)}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def name(value)
|
|
82
|
+
"m#{digest("method", value)}"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def mapping(rewritten)
|
|
86
|
+
Mapping.new(methods: rewritten.transform_values(&:identity), klasses: @klasses)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def digest(namespace, value)
|
|
92
|
+
OpenSSL::HMAC.hexdigest("SHA256", @secret, "#{namespace}\0#{value}")[0, HEX_DIGITS]
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "reading"
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Query
|
|
7
|
+
module Index
|
|
8
|
+
class Graph
|
|
9
|
+
Method = Struct.new(:identity, :klass, :name, :class_method, :file, :line, keyword_init: true)
|
|
10
|
+
|
|
11
|
+
class Method
|
|
12
|
+
def location = line ? "#{file}:#{line}" : file
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
Edge = Struct.new(:caller, :callee, :nodes, keyword_init: true)
|
|
16
|
+
|
|
17
|
+
class Edge
|
|
18
|
+
def count = nodes.size
|
|
19
|
+
def call_sites = nodes.group_by { |node| "#{node.call.call_file}:#{node.call.call_line}" }
|
|
20
|
+
def spec_count = nodes.count { |node| node.tree.spec? }
|
|
21
|
+
def run_count = nodes.count { |node| node.tree.run? }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
module Neighbours
|
|
25
|
+
def callers_of(identity)
|
|
26
|
+
edges_from(nodes_of(identity).group_by { |node| node.container.identity }, callee: identity)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def callees_of(identity)
|
|
30
|
+
children = nodes_of(identity).flat_map(&:children)
|
|
31
|
+
edges_from(children.group_by(&:method_identity), caller: identity)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def edges_by(identity, &label)
|
|
35
|
+
edges_from(nodes_of(identity).group_by(&label), callee: identity).values
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def edges_from(grouped, caller: nil, callee: nil)
|
|
41
|
+
grouped.to_h do |identity, nodes|
|
|
42
|
+
[identity, Edge.new(caller: caller || identity, callee: callee || identity, nodes: nodes)]
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
include Neighbours
|
|
48
|
+
|
|
49
|
+
attr_reader :recording, :trees
|
|
50
|
+
|
|
51
|
+
def self.load(dir)
|
|
52
|
+
new(Reading.load(dir))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.neighbours = Neighbours
|
|
56
|
+
def self.method_entry(**) = Method.new(**)
|
|
57
|
+
|
|
58
|
+
def initialize(recording)
|
|
59
|
+
@recording = recording
|
|
60
|
+
@trees = recording.trees
|
|
61
|
+
index!
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def methods
|
|
65
|
+
@methods.values
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def method(identity)
|
|
69
|
+
@methods[identity]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def nodes_of(identity)
|
|
73
|
+
@nodes_by_method.fetch(identity, [])
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def edges
|
|
77
|
+
@edges ||= trees.flat_map(&:nodes).group_by { |node| [node.container.identity, node.method_identity] }
|
|
78
|
+
.map { |(from, to), nodes| Edge.new(caller: from, callee: to, nodes: nodes) }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def spec_trees = trees.select(&:spec?)
|
|
82
|
+
def run_trees = trees.select(&:run?)
|
|
83
|
+
|
|
84
|
+
def inspect = "#<#{self.class} #{methods.size} methods, #{edges.size} edges>"
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def index!
|
|
89
|
+
@methods = {}
|
|
90
|
+
@nodes_by_method = Hash.new { |hash, key| hash[key] = [] }
|
|
91
|
+
trees.each { |tree| tree.nodes.each { |node| index_node(tree, node) } }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def index_node(tree, node)
|
|
95
|
+
node.tree = tree
|
|
96
|
+
@methods[node.method_identity] ||= method_from(node.callee)
|
|
97
|
+
@nodes_by_method[node.method_identity] << node
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def method_from(frame)
|
|
101
|
+
Method.new(identity: frame.identity, klass: frame.klass, name: frame.name,
|
|
102
|
+
class_method: frame.class_method, file: frame.file, line: frame.line)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|