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,78 @@
|
|
|
1
|
+
#include "names.h"
|
|
2
|
+
#include <string.h>
|
|
3
|
+
|
|
4
|
+
static size_t name_home(VALUE key) {
|
|
5
|
+
return (size_t)(((uint64_t)key * 0x9E3779B97F4A7C15ULL) >> 32) & (RA_NAME_SLOTS - 1);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/* Entries are only ever cleared all together, so the first empty slot ends the search. */
|
|
9
|
+
int64_t ra_names_find(const ra_name *names, VALUE key) {
|
|
10
|
+
size_t home = name_home(key);
|
|
11
|
+
for (size_t i = 0; i < RA_NAME_PROBES; i++) {
|
|
12
|
+
const ra_name *e = &names[(home + i) & (RA_NAME_SLOTS - 1)];
|
|
13
|
+
if (e->key == key) return e->id;
|
|
14
|
+
if (e->key == 0) return -1;
|
|
15
|
+
}
|
|
16
|
+
return -1;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/* Into the first empty slot in reach, else over the entry in the key's own slot, which is then asked
|
|
20
|
+
* of Ruby again when it is next met. */
|
|
21
|
+
void ra_names_store(ra_name *names, VALUE key, int64_t id) {
|
|
22
|
+
size_t home = name_home(key);
|
|
23
|
+
ra_name *e = &names[home];
|
|
24
|
+
for (size_t i = 0; i < RA_NAME_PROBES; i++) {
|
|
25
|
+
ra_name *probe = &names[(home + i) & (RA_NAME_SLOTS - 1)];
|
|
26
|
+
if (probe->key == 0 || probe->key == key) { e = probe; break; }
|
|
27
|
+
}
|
|
28
|
+
e->key = key;
|
|
29
|
+
e->id = id;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* The objects whose addresses are keys. Marking them, not as movable, keeps each alive and in place,
|
|
33
|
+
* so an address cannot come to mean another object. The count is capped, and a program that makes
|
|
34
|
+
* classes without end has the rest of them named by Ruby each time. */
|
|
35
|
+
#define RA_KEPT_SLOTS 32768
|
|
36
|
+
#define RA_KEPT_MOST (RA_KEPT_SLOTS / 2)
|
|
37
|
+
|
|
38
|
+
static VALUE g_kept[RA_KEPT_SLOTS];
|
|
39
|
+
static size_t g_kept_count = 0;
|
|
40
|
+
static VALUE g_keeper = Qnil;
|
|
41
|
+
|
|
42
|
+
static void keeper_mark(void *unused) {
|
|
43
|
+
(void)unused;
|
|
44
|
+
if (g_kept_count == 0) return;
|
|
45
|
+
for (size_t i = 0; i < RA_KEPT_SLOTS; i++) {
|
|
46
|
+
if (g_kept[i]) rb_gc_mark(g_kept[i]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static const rb_data_type_t keeper_type = {
|
|
51
|
+
"RuntimeAnalysis::Native::Kept",
|
|
52
|
+
{ keeper_mark, NULL, NULL, },
|
|
53
|
+
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
int ra_keep(VALUE object) {
|
|
57
|
+
if (SPECIAL_CONST_P(object)) return 1;
|
|
58
|
+
|
|
59
|
+
size_t at = (size_t)(((uint64_t)object * 0x9E3779B97F4A7C15ULL) >> 32) & (RA_KEPT_SLOTS - 1);
|
|
60
|
+
while (g_kept[at]) {
|
|
61
|
+
if (g_kept[at] == object) return 1;
|
|
62
|
+
at = (at + 1) & (RA_KEPT_SLOTS - 1);
|
|
63
|
+
}
|
|
64
|
+
if (g_kept_count >= RA_KEPT_MOST) return 0;
|
|
65
|
+
g_kept[at] = object;
|
|
66
|
+
g_kept_count++;
|
|
67
|
+
return 1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
void ra_keep_none(void) {
|
|
71
|
+
memset(g_kept, 0, sizeof(g_kept));
|
|
72
|
+
g_kept_count = 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
void Init_ra_names(void) {
|
|
76
|
+
g_keeper = TypedData_Wrap_Struct(rb_cObject, &keeper_type, g_kept);
|
|
77
|
+
rb_gc_register_address(&g_keeper);
|
|
78
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#ifndef RA_NAMES_H
|
|
2
|
+
#define RA_NAMES_H
|
|
3
|
+
|
|
4
|
+
#include <ruby.h>
|
|
5
|
+
#include <stdint.h>
|
|
6
|
+
|
|
7
|
+
/* The names a stream's segment has defined, by the object each is the name of: a class for its
|
|
8
|
+
* instances, a class or module passed as a value, a keyword's symbol. The wrapper reads an argument's
|
|
9
|
+
* class id from here, where the Ruby path builds the name and looks it up as a string.
|
|
10
|
+
*
|
|
11
|
+
* A key is an object's address, so the object must stay where it is and stay alive for as long as
|
|
12
|
+
* any table holds it. ra_keep sees to both, and a table only stores what it kept. */
|
|
13
|
+
#define RA_NAME_SLOTS 2048 /* a power of two: the table is indexed by masking */
|
|
14
|
+
#define RA_NAME_PROBES 8
|
|
15
|
+
|
|
16
|
+
typedef struct {
|
|
17
|
+
VALUE key; /* 0 marks an empty entry */
|
|
18
|
+
int64_t id;
|
|
19
|
+
} ra_name;
|
|
20
|
+
|
|
21
|
+
int64_t ra_names_find(const ra_name *names, VALUE key); /* -1 when the key is not there */
|
|
22
|
+
void ra_names_store(ra_name *names, VALUE key, int64_t id);
|
|
23
|
+
|
|
24
|
+
int ra_keep(VALUE object); /* 0 when no more can be kept, and the name is then not stored */
|
|
25
|
+
void ra_keep_none(void); /* every table must be emptied with it */
|
|
26
|
+
void Init_ra_names(void);
|
|
27
|
+
|
|
28
|
+
#endif
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
#include "novelty.h"
|
|
2
|
+
#include "state.h"
|
|
3
|
+
#include "os.h"
|
|
4
|
+
|
|
5
|
+
#define RA_SEEN_START 1024 /* a power of two, as every size the table grows to is */
|
|
6
|
+
|
|
7
|
+
static void novelty_free(void *ptr) {
|
|
8
|
+
ra_novelty *gate = ptr;
|
|
9
|
+
free(gate->seen);
|
|
10
|
+
xfree(gate);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static size_t novelty_memsize(const void *ptr) {
|
|
14
|
+
return sizeof(ra_novelty) + ((const ra_novelty *)ptr)->cap * sizeof(uint64_t);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static const rb_data_type_t novelty_type = {
|
|
18
|
+
"RuntimeAnalysis::Trace::Novelty",
|
|
19
|
+
{ NULL, novelty_free, novelty_memsize, },
|
|
20
|
+
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
static VALUE novelty_alloc(VALUE klass) {
|
|
24
|
+
ra_novelty *gate;
|
|
25
|
+
VALUE obj = TypedData_Make_Struct(klass, ra_novelty, &novelty_type, gate);
|
|
26
|
+
gate->resample = 64;
|
|
27
|
+
return obj;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
ra_novelty *ra_novelty_of(VALUE novelty) {
|
|
31
|
+
ra_novelty *gate;
|
|
32
|
+
TypedData_Get_Struct(novelty, ra_novelty, &novelty_type, gate);
|
|
33
|
+
return gate;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* A route is stored as its hash, and no stored hash may be 0, the empty slot. */
|
|
37
|
+
static uint64_t stored(uint64_t path_hash) {
|
|
38
|
+
return path_hash ? path_hash : 0x9E3779B97F4A7C15ULL;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static size_t home(uint64_t key, size_t cap) {
|
|
42
|
+
return (size_t)((key * 0x9E3779B97F4A7C15ULL) >> 20) & (cap - 1);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static void place(uint64_t *seen, size_t cap, uint64_t key) {
|
|
46
|
+
size_t at = home(key, cap);
|
|
47
|
+
while (seen[at]) at = (at + 1) & (cap - 1);
|
|
48
|
+
seen[at] = key;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* Twice the room. 0 when the system has no memory for it, and the table is then as it was. */
|
|
52
|
+
static int grow(ra_novelty *gate) {
|
|
53
|
+
size_t cap = gate->cap ? gate->cap * 2 : RA_SEEN_START;
|
|
54
|
+
uint64_t *seen = ra_calloc(cap, sizeof(uint64_t));
|
|
55
|
+
if (!seen) return 0;
|
|
56
|
+
|
|
57
|
+
for (size_t i = 0; i < gate->cap; i++) {
|
|
58
|
+
if (gate->seen[i]) place(seen, cap, gate->seen[i]);
|
|
59
|
+
}
|
|
60
|
+
free(gate->seen);
|
|
61
|
+
gate->seen = seen;
|
|
62
|
+
gate->cap = cap;
|
|
63
|
+
return 1;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static int met(const ra_novelty *gate, uint64_t key) {
|
|
67
|
+
if (!gate->cap) return 0;
|
|
68
|
+
for (size_t at = home(key, gate->cap); gate->seen[at]; at = (at + 1) & (gate->cap - 1)) {
|
|
69
|
+
if (gate->seen[at] == key) return 1;
|
|
70
|
+
}
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* True the first time a route is met. The table is kept under half full, and is asked to grow only
|
|
75
|
+
* for a route it does not hold, so the routes it remembers are skipped as before while there is no
|
|
76
|
+
* memory for another. A route there is no memory to remember is new every time it is met: its calls
|
|
77
|
+
* are all kept, which is more than was asked for and never less, and nothing is raised into the
|
|
78
|
+
* program being recorded. */
|
|
79
|
+
static int novel(ra_novelty *gate, uint64_t key) {
|
|
80
|
+
if (met(gate, key)) return 0;
|
|
81
|
+
if (gate->count * 2 >= gate->cap && !grow(gate)) return 1;
|
|
82
|
+
|
|
83
|
+
place(gate->seen, gate->cap, key);
|
|
84
|
+
gate->count++;
|
|
85
|
+
return 1;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
int ra_novelty_keep(ra_novelty *gate, uint64_t path_hash) {
|
|
89
|
+
int fresh = novel(gate, stored(path_hash));
|
|
90
|
+
gate->since = fresh ? 0 : gate->since + 1;
|
|
91
|
+
if (fresh || gate->since >= gate->resample) {
|
|
92
|
+
gate->since = 0;
|
|
93
|
+
gate->recorded++;
|
|
94
|
+
return 1;
|
|
95
|
+
}
|
|
96
|
+
gate->skipped++;
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* A route is an Integer, the path hash. Anything else is asked for its hash. */
|
|
101
|
+
static uint64_t route_of(VALUE route) {
|
|
102
|
+
return (uint64_t)(FIXNUM_P(route) ? FIX2LONG(route) : NUM2LONG(rb_hash(route)));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/* The wrapper asks about a call before it knows whether it can record the call itself. When it
|
|
106
|
+
* cannot, Ruby records it and asks again here, and is given the answer the wrapper had: the call is
|
|
107
|
+
* counted once. */
|
|
108
|
+
static VALUE novelty_record_p(VALUE self, VALUE route) {
|
|
109
|
+
uint64_t path_hash = route_of(route);
|
|
110
|
+
ra_state *st = ra_state_current();
|
|
111
|
+
if (st && st->kept && st->kept_hash == path_hash) {
|
|
112
|
+
st->kept = 0;
|
|
113
|
+
return Qtrue;
|
|
114
|
+
}
|
|
115
|
+
return ra_novelty_keep(ra_novelty_of(self), path_hash) ? Qtrue : Qfalse;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static VALUE novelty_set_resample(VALUE self, VALUE resample) {
|
|
119
|
+
ra_novelty_of(self)->resample = NUM2LONG(resample);
|
|
120
|
+
return resample;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static VALUE novelty_recorded(VALUE self) {
|
|
124
|
+
return LONG2NUM(ra_novelty_of(self)->recorded);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static VALUE novelty_skipped(VALUE self) {
|
|
128
|
+
return LONG2NUM(ra_novelty_of(self)->skipped);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
void Init_ra_novelty(VALUE mTrace) {
|
|
132
|
+
VALUE cNovelty = rb_define_class_under(mTrace, "Novelty", rb_cObject);
|
|
133
|
+
|
|
134
|
+
rb_define_alloc_func(cNovelty, novelty_alloc);
|
|
135
|
+
rb_define_method(cNovelty, "record?", novelty_record_p, 1);
|
|
136
|
+
rb_define_method(cNovelty, "resample=", novelty_set_resample, 1);
|
|
137
|
+
rb_define_method(cNovelty, "recorded", novelty_recorded, 0);
|
|
138
|
+
rb_define_method(cNovelty, "skipped", novelty_skipped, 0);
|
|
139
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#ifndef RA_NOVELTY_H
|
|
2
|
+
#define RA_NOVELTY_H
|
|
3
|
+
|
|
4
|
+
#include <ruby.h>
|
|
5
|
+
#include <stdint.h>
|
|
6
|
+
|
|
7
|
+
/* Adaptive mode's gate: which calls are worth recording. A route seen for the first time is kept,
|
|
8
|
+
* and a repeat is kept once in every `resample` repeats. Ruby sees it as Trace::Novelty. It is in C
|
|
9
|
+
* because the wrapper asks it on every call, and most of its answers are no. */
|
|
10
|
+
typedef struct {
|
|
11
|
+
uint64_t *seen; /* the routes met, by path hash; 0 marks an empty slot */
|
|
12
|
+
size_t cap, count;
|
|
13
|
+
long resample;
|
|
14
|
+
long since; /* repeats since a call was last kept */
|
|
15
|
+
long recorded, skipped;
|
|
16
|
+
} ra_novelty;
|
|
17
|
+
|
|
18
|
+
void Init_ra_novelty(VALUE mTrace);
|
|
19
|
+
ra_novelty *ra_novelty_of(VALUE novelty); /* raises TypeError for anything else */
|
|
20
|
+
int ra_novelty_keep(ra_novelty *gate, uint64_t path_hash);
|
|
21
|
+
|
|
22
|
+
#endif
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#include "os.h"
|
|
2
|
+
|
|
3
|
+
#ifdef RA_TESTING
|
|
4
|
+
#include <errno.h>
|
|
5
|
+
|
|
6
|
+
/* For each kind of request, how many more are to be answered before one is refused: 1 refuses the
|
|
7
|
+
* next. 0 refuses none, which is how it starts and how it is left once one has been refused. */
|
|
8
|
+
enum { RA_OS_MALLOC, RA_OS_CALLOC, RA_OS_REALLOC, RA_OS_THREAD, RA_OS_KINDS };
|
|
9
|
+
static long ra_refuse_in[RA_OS_KINDS];
|
|
10
|
+
|
|
11
|
+
/* How many requests after that one are refused too, one after the other. */
|
|
12
|
+
static long ra_refuse_more[RA_OS_KINDS];
|
|
13
|
+
|
|
14
|
+
static int refused(int kind) {
|
|
15
|
+
if (__atomic_load_n(&ra_refuse_in[kind], __ATOMIC_RELAXED) == 0) return 0;
|
|
16
|
+
if (__atomic_sub_fetch(&ra_refuse_in[kind], 1, __ATOMIC_RELAXED) != 0) return 0;
|
|
17
|
+
if (__atomic_load_n(&ra_refuse_more[kind], __ATOMIC_RELAXED) > 0) {
|
|
18
|
+
__atomic_sub_fetch(&ra_refuse_more[kind], 1, __ATOMIC_RELAXED);
|
|
19
|
+
__atomic_store_n(&ra_refuse_in[kind], 1, __ATOMIC_RELAXED);
|
|
20
|
+
}
|
|
21
|
+
return 1;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
void *ra_malloc(size_t size) {
|
|
25
|
+
return refused(RA_OS_MALLOC) ? NULL : malloc(size);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
void *ra_calloc(size_t count, size_t size) {
|
|
29
|
+
return refused(RA_OS_CALLOC) ? NULL : calloc(count, size);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
void *ra_realloc(void *was, size_t size) {
|
|
33
|
+
return refused(RA_OS_REALLOC) ? NULL : realloc(was, size);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
int ra_thread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*run)(void *), void *arg) {
|
|
37
|
+
return refused(RA_OS_THREAD) ? EAGAIN : pthread_create(thread, attr, run, arg);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static int kind_of(VALUE what) {
|
|
41
|
+
static const char *const names[RA_OS_KINDS] = { "malloc", "calloc", "realloc", "thread" };
|
|
42
|
+
const char *name = rb_id2name(rb_to_id(what));
|
|
43
|
+
for (int kind = 0; kind < RA_OS_KINDS; kind++) {
|
|
44
|
+
if (strcmp(name, names[kind]) == 0) return kind;
|
|
45
|
+
}
|
|
46
|
+
rb_raise(rb_eArgError, "runtime_analysis native: the system is not asked for %s", name);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/* refuse(what, nth, times = 1): the nth request from now for :malloc, :calloc, :realloc or a
|
|
50
|
+
* :thread is refused, as the system refuses one it has no room for, and with it as many of those
|
|
51
|
+
* that follow as make `times`. An nth of 0 refuses none. */
|
|
52
|
+
static VALUE testing_refuse(int argc, VALUE *argv, VALUE self) {
|
|
53
|
+
VALUE what, nth, times;
|
|
54
|
+
rb_scan_args(argc, argv, "21", &what, &nth, ×);
|
|
55
|
+
int kind = kind_of(what);
|
|
56
|
+
__atomic_store_n(&ra_refuse_more[kind], NIL_P(times) ? 0 : NUM2LONG(times) - 1, __ATOMIC_RELAXED);
|
|
57
|
+
__atomic_store_n(&ra_refuse_in[kind], NUM2LONG(nth), __ATOMIC_RELAXED);
|
|
58
|
+
return Qnil;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* refusing?(what): whether a request of that kind is still to be refused. */
|
|
62
|
+
static VALUE testing_refusing_p(VALUE self, VALUE what) {
|
|
63
|
+
return __atomic_load_n(&ra_refuse_in[kind_of(what)], __ATOMIC_RELAXED) ? Qtrue : Qfalse;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
void Init_ra_os(VALUE mTesting) {
|
|
67
|
+
rb_define_singleton_method(mTesting, "refuse", testing_refuse, -1);
|
|
68
|
+
rb_define_singleton_method(mTesting, "refusing?", testing_refusing_p, 1);
|
|
69
|
+
}
|
|
70
|
+
#else
|
|
71
|
+
/* The build that is shipped has nothing here: os.h names the system's own calls. */
|
|
72
|
+
typedef int ra_os_has_no_shim;
|
|
73
|
+
#endif
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#ifndef RA_OS_H
|
|
2
|
+
#define RA_OS_H
|
|
3
|
+
|
|
4
|
+
/* What the extension asks the system for: memory and threads. In the build that is shipped these
|
|
5
|
+
* four are the libc and pthread calls themselves, by name, and nothing stands between.
|
|
6
|
+
*
|
|
7
|
+
* RA_TESTING marks the build the gem's own specs run on, which extconf.rb makes only when it is
|
|
8
|
+
* asked to by name. That build alone has Native::Testing, and everything a spec steers the
|
|
9
|
+
* extension by is compiled under this one flag: a program is never recorded with any of it. There
|
|
10
|
+
* the four go through os.c, where a spec can have the system refuse the next request of a kind:
|
|
11
|
+
* see Native::Testing.refuse. That is how each path taken when the system refuses is run, and what
|
|
12
|
+
* it leaves the program with. */
|
|
13
|
+
|
|
14
|
+
#include <stdlib.h>
|
|
15
|
+
#include <pthread.h>
|
|
16
|
+
|
|
17
|
+
#ifdef RA_TESTING
|
|
18
|
+
#include <ruby.h>
|
|
19
|
+
|
|
20
|
+
void *ra_malloc(size_t size);
|
|
21
|
+
void *ra_calloc(size_t count, size_t size);
|
|
22
|
+
void *ra_realloc(void *was, size_t size);
|
|
23
|
+
int ra_thread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*run)(void *), void *arg);
|
|
24
|
+
void Init_ra_os(VALUE mTesting);
|
|
25
|
+
#else
|
|
26
|
+
#define ra_malloc malloc
|
|
27
|
+
#define ra_calloc calloc
|
|
28
|
+
#define ra_realloc realloc
|
|
29
|
+
#define ra_thread_create pthread_create
|
|
30
|
+
#endif
|
|
31
|
+
|
|
32
|
+
#endif
|