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,59 @@
|
|
|
1
|
+
#ifndef RA_FAST_H
|
|
2
|
+
#define RA_FAST_H
|
|
3
|
+
|
|
4
|
+
#include "sink.h"
|
|
5
|
+
#include "state.h"
|
|
6
|
+
|
|
7
|
+
/* What the wrapper knows of each method it stands in front of, by registry id. `fast` says the
|
|
8
|
+
* wrapper may record the method's calls itself. `hash_kw` marks a method with no keyword
|
|
9
|
+
* parameters, which is handed any keywords as a Hash. `probe` marks a method the probe names, whose
|
|
10
|
+
* values Ruby writes. `ivarc` is how many ivars the method's source writes that the wrapper reads
|
|
11
|
+
* back when a call returns, their names in ra_ivars. Both sit in the padding, so a method that
|
|
12
|
+
* writes none costs one byte test. Touched only under the GVL. */
|
|
13
|
+
#define RA_IVARS_MAX 8
|
|
14
|
+
typedef struct { uint8_t fast; uint8_t hash_kw; uint8_t probe; uint8_t ivarc; uint64_t salt; } ra_method;
|
|
15
|
+
typedef struct { ID names[RA_IVARS_MAX]; } ra_ivars;
|
|
16
|
+
|
|
17
|
+
extern int ra_fast; /* the wrapper may write a repeated call's rows itself */
|
|
18
|
+
extern ra_method *ra_methods;
|
|
19
|
+
extern size_t ra_methods_cap;
|
|
20
|
+
extern ra_ivars *ra_ivar_names; /* by registry id, beside ra_methods and as long */
|
|
21
|
+
extern int64_t ra_fast_rows; /* calls written without Ruby, for Native.stats */
|
|
22
|
+
extern int64_t ra_slow_calls; /* calls handed to the Ruby recorder, for Native.stats */
|
|
23
|
+
|
|
24
|
+
static inline int ra_fast_method(long id) {
|
|
25
|
+
return ra_fast && (size_t)id < ra_methods_cap && ra_methods[id].fast;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/* A method named by the probe, whose arguments Ruby reads at every tier. */
|
|
29
|
+
static inline int ra_probed_method(long id) {
|
|
30
|
+
return (size_t)id < ra_methods_cap && ra_methods[id].probe;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* Where a call was made from. Finding it walks the caller's frame, so it is read the first time
|
|
34
|
+
* it is asked for, and a call that adaptive mode skips never asks. For the C wrapper it is the Ruby
|
|
35
|
+
* frame running, and for a wrapper written in Ruby, `above` set, the frame above the wrapper's. */
|
|
36
|
+
typedef struct { const char *file; int line; int read; int above; } ra_site;
|
|
37
|
+
|
|
38
|
+
int ra_site_above(ra_site *site);
|
|
39
|
+
|
|
40
|
+
static inline int ra_site_known(ra_site *site) {
|
|
41
|
+
if (!site->read) {
|
|
42
|
+
site->read = 1;
|
|
43
|
+
if (site->above) return ra_site_above(site);
|
|
44
|
+
if ((site->file = rb_sourcefile())) site->line = rb_sourceline();
|
|
45
|
+
}
|
|
46
|
+
return site->file != NULL;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
uint64_t ra_frame_key(long id, VALUE receiver);
|
|
50
|
+
VALUE ra_fast_call(ra_state *st, long id, ra_site *from, int argc, VALUE *argv, VALUE self);
|
|
51
|
+
void ra_fast_remember(ra_state *st, const char *file, int line);
|
|
52
|
+
void ra_fast_forget(sink *s);
|
|
53
|
+
void ra_fast_start(VALUE gate, int fast);
|
|
54
|
+
void Init_ra_fast(VALUE mWrapper);
|
|
55
|
+
#ifdef RA_TESTING
|
|
56
|
+
void Init_ra_fast_testing(VALUE mTesting);
|
|
57
|
+
#endif
|
|
58
|
+
|
|
59
|
+
#endif
|
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
#include "sink.h"
|
|
2
|
+
#include <errno.h>
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <time.h>
|
|
6
|
+
#include <unistd.h>
|
|
7
|
+
|
|
8
|
+
/* no-unit-test: covered by lib/runtime_analysis/native/held_sink_spec.rb.
|
|
9
|
+
*
|
|
10
|
+
* A stream that holds. Its rows wait on the ring and its writer sleeps. Ruby ends each request by
|
|
11
|
+
* discarding what the ring holds of it, which moves the head back and writes nothing, or by keeping
|
|
12
|
+
* it, and the writer then writes those rows as a segment of their own.
|
|
13
|
+
*
|
|
14
|
+
* A kept segment has to read by itself, and the strings and paths its rows name were mostly defined
|
|
15
|
+
* during earlier requests that were discarded. So definitions never go on the ring. They are kept
|
|
16
|
+
* for the life of the stream, and the writer puts the ones a kept request names at the top of its
|
|
17
|
+
* segment. The segment is written beside its name and renamed when it is whole, so a file a reader
|
|
18
|
+
* finds under a segment's name is a finished one.
|
|
19
|
+
*
|
|
20
|
+
* A request larger than the ring loses its oldest rows, since the end of a failed request is what
|
|
21
|
+
* says why it failed. While a kept request waits to be written its rows cannot be given up, and the
|
|
22
|
+
* request after it then loses its newest rows, as a stream that does not hold does. */
|
|
23
|
+
|
|
24
|
+
#define RA_DEF_BLOCK 512 /* records */
|
|
25
|
+
|
|
26
|
+
struct ra_def_block {
|
|
27
|
+
struct ra_def_block *prev, *next;
|
|
28
|
+
size_t count;
|
|
29
|
+
uint64_t words[];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/* A request to write: the rows it has on the ring, counted from the tail once every request before
|
|
33
|
+
* it is written. */
|
|
34
|
+
struct ra_keep {
|
|
35
|
+
struct ra_keep *next;
|
|
36
|
+
char *path, *part; /* the segment's name, and the name it is written under */
|
|
37
|
+
size_t words, rows;
|
|
38
|
+
int64_t dropped, at;
|
|
39
|
+
int pinned_count[RA_PIN_KINDS];
|
|
40
|
+
int64_t pinned[RA_PIN_KINDS][RA_PINNED][2];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/* ------------------------------------------------------------ definitions */
|
|
44
|
+
|
|
45
|
+
/* Called with the lock held. The record is whole before the count that admits it moves. */
|
|
46
|
+
static int defs_add(ra_defs *defs, const uint64_t *record, int64_t id) {
|
|
47
|
+
struct ra_def_block *block = defs->last;
|
|
48
|
+
if (!block || block->count == RA_DEF_BLOCK) {
|
|
49
|
+
block = ra_calloc(1, sizeof(*block) + RA_DEF_BLOCK * defs->width * sizeof(uint64_t));
|
|
50
|
+
if (!block) return 0;
|
|
51
|
+
block->prev = defs->last;
|
|
52
|
+
if (defs->last) defs->last->next = block; else defs->first = block;
|
|
53
|
+
defs->last = block;
|
|
54
|
+
}
|
|
55
|
+
memcpy(block->words + block->count * defs->width, record, defs->width * sizeof(uint64_t));
|
|
56
|
+
block->count++;
|
|
57
|
+
if (id > defs->top) defs->top = id;
|
|
58
|
+
return 1;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* A dictionary entry or a path, on a stream that holds, is kept here and the caller is done with
|
|
62
|
+
* it. The lock is waited for: a definition lost would leave every later row that names it unread,
|
|
63
|
+
* for as long as the stream lives, and the writer holds the lock for a few instructions at a time. */
|
|
64
|
+
int ra_hold_defines(sink *s, slot *sl) {
|
|
65
|
+
if (sl->kind != SLOT_DICT && sl->kind != SLOT_PATH) return 0;
|
|
66
|
+
|
|
67
|
+
uint64_t record[PATH_FIELDS];
|
|
68
|
+
int added;
|
|
69
|
+
pthread_mutex_lock(&s->mu);
|
|
70
|
+
if (sl->kind == SLOT_DICT) {
|
|
71
|
+
record[0] = (uint64_t)sl->dict_id;
|
|
72
|
+
record[1] = (uint64_t)(uintptr_t)sl->dict_str.ptr;
|
|
73
|
+
record[2] = (uint64_t)sl->dict_str.len;
|
|
74
|
+
added = sl->dict_id >= 0 && defs_add(&s->strings, record, sl->dict_id);
|
|
75
|
+
} else {
|
|
76
|
+
for (int i = 0; i < PATH_FIELDS; i++) record[i] = (uint64_t)sl->rec[i];
|
|
77
|
+
added = defs_add(&s->paths, record, sl->rec[0]);
|
|
78
|
+
}
|
|
79
|
+
if (!added) __atomic_fetch_add(&s->dropped, 1, __ATOMIC_RELAXED);
|
|
80
|
+
pthread_mutex_unlock(&s->mu);
|
|
81
|
+
|
|
82
|
+
if (!added) free(sl->dict_str.ptr);
|
|
83
|
+
return 1;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static void defs_free(ra_defs *defs, int strings) {
|
|
87
|
+
struct ra_def_block *block = defs->first;
|
|
88
|
+
while (block) {
|
|
89
|
+
struct ra_def_block *next = block->next;
|
|
90
|
+
if (strings) {
|
|
91
|
+
for (size_t i = 0; i < block->count; i++) free((char *)(uintptr_t)block->words[i * 3 + 1]);
|
|
92
|
+
}
|
|
93
|
+
free(block);
|
|
94
|
+
block = next;
|
|
95
|
+
}
|
|
96
|
+
defs->first = defs->last = NULL;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
static void free_keep(struct ra_keep *keep) {
|
|
100
|
+
free(keep->path);
|
|
101
|
+
free(keep->part);
|
|
102
|
+
free(keep);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
void ra_hold_free(sink *s) {
|
|
106
|
+
defs_free(&s->strings, 1);
|
|
107
|
+
defs_free(&s->paths, 0);
|
|
108
|
+
while (s->keeps) {
|
|
109
|
+
struct ra_keep *keep = s->keeps;
|
|
110
|
+
s->keeps = keep->next;
|
|
111
|
+
free_keep(keep);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* ------------------------------------------------------------ the recorder's side */
|
|
116
|
+
|
|
117
|
+
/* Which kind of pinned row a row's first word says it is: 0 an entry row, 1 an inner entry row, -1
|
|
118
|
+
* a row that is not pinned. */
|
|
119
|
+
static int pin_kind(uint64_t head) {
|
|
120
|
+
switch (head & 0xff) {
|
|
121
|
+
case SLOT_ENTRY: return 0;
|
|
122
|
+
case SLOT_INNER_ENTRY: return 1;
|
|
123
|
+
default: return -1;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static const int PIN_OPS[RA_PIN_KINDS] = { OP_ENTRY, OP_INNER_ENTRY };
|
|
128
|
+
|
|
129
|
+
/* Gives up the oldest row. Called with the lock held, and only while no kept request waits. A
|
|
130
|
+
* word there that is not the start of a row should be impossible, and is answered as every broken
|
|
131
|
+
* invariant is: the stream is disabled and Ruby opens another. Nothing is reported from here, since
|
|
132
|
+
* this is the recording thread and the report may not be waited for. */
|
|
133
|
+
static int drop_oldest(sink *s) {
|
|
134
|
+
uint64_t row[3];
|
|
135
|
+
size_t n = (size_t)(s->words[s->tail] >> RA_ROW_SHIFT);
|
|
136
|
+
if (n < 3 || n > RA_ROW_MAX || n > s->held_words) {
|
|
137
|
+
#ifdef RA_ABORT_ON_INVARIANT
|
|
138
|
+
abort();
|
|
139
|
+
#endif
|
|
140
|
+
/* coverage:off -- invariant guard only a bug in the extension reaches */
|
|
141
|
+
s->disabled = 1;
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
/* coverage:on */
|
|
145
|
+
int pinned = pin_kind(s->words[s->tail]);
|
|
146
|
+
if (pinned >= 0) {
|
|
147
|
+
ra_ring_read(s, s->tail, row, 3);
|
|
148
|
+
int64_t *pin = s->pinned[pinned][s->pinned_count[pinned]++ % RA_PINNED];
|
|
149
|
+
pin[0] = (int64_t)row[1];
|
|
150
|
+
pin[1] = (int64_t)row[2];
|
|
151
|
+
}
|
|
152
|
+
s->tail = (s->tail + n) % s->cap;
|
|
153
|
+
s->used -= n;
|
|
154
|
+
s->rows--;
|
|
155
|
+
s->held_words -= n;
|
|
156
|
+
s->held_rows--;
|
|
157
|
+
s->held_dropped++;
|
|
158
|
+
return 1;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/* Makes room for a row of `n` words by giving up the oldest rows of the request being held. Returns
|
|
162
|
+
* 0 when it may not or cannot, and the row is then the one dropped. Called with the lock held. */
|
|
163
|
+
int ra_hold_room(sink *s, size_t n) {
|
|
164
|
+
if (!s->hold || s->keeps || n > s->cap) return 0;
|
|
165
|
+
while (s->cap - s->used < n && s->held_rows > 0) {
|
|
166
|
+
if (!drop_oldest(s)) return 0;
|
|
167
|
+
}
|
|
168
|
+
return s->cap - s->used >= n;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static void forget_held(sink *s) {
|
|
172
|
+
s->held_words = s->held_rows = 0;
|
|
173
|
+
s->request_words = 0;
|
|
174
|
+
s->held_dropped = 0;
|
|
175
|
+
memset(s->pinned_count, 0, sizeof(s->pinned_count));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* discard: the rows held since the last keep or discard were never here. Waits for the lock, as
|
|
179
|
+
* keep does: a request ends once, and its rows must not run on into the next one's. */
|
|
180
|
+
static VALUE sink_discard(VALUE self) {
|
|
181
|
+
sink *s = ra_sink_of(self);
|
|
182
|
+
if (!s->hold) return Qnil;
|
|
183
|
+
|
|
184
|
+
pthread_mutex_lock(&s->mu);
|
|
185
|
+
s->head = (s->head + s->cap - s->held_words) % s->cap;
|
|
186
|
+
s->used -= s->held_words;
|
|
187
|
+
s->rows -= s->held_rows;
|
|
188
|
+
forget_held(s);
|
|
189
|
+
if (s->used == 0) s->head = s->tail = 0;
|
|
190
|
+
pthread_mutex_unlock(&s->mu);
|
|
191
|
+
return Qnil;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#define RA_PART ".part"
|
|
195
|
+
|
|
196
|
+
static struct ra_keep *new_keep(VALUE path) {
|
|
197
|
+
struct ra_keep *keep = ra_calloc(1, sizeof(*keep));
|
|
198
|
+
size_t len = (size_t)RSTRING_LEN(path);
|
|
199
|
+
if (!keep) return NULL;
|
|
200
|
+
|
|
201
|
+
keep->path = ra_malloc(len + 1);
|
|
202
|
+
keep->part = ra_malloc(len + sizeof(RA_PART));
|
|
203
|
+
if (!keep->path || !keep->part) {
|
|
204
|
+
free_keep(keep);
|
|
205
|
+
return NULL;
|
|
206
|
+
}
|
|
207
|
+
memcpy(keep->path, RSTRING_PTR(path), len);
|
|
208
|
+
keep->path[len] = '\0';
|
|
209
|
+
memcpy(keep->part, keep->path, len);
|
|
210
|
+
memcpy(keep->part + len, RA_PART, sizeof(RA_PART));
|
|
211
|
+
return keep;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* Called with the lock held. */
|
|
215
|
+
static void queue_keep(sink *s, struct ra_keep *keep) {
|
|
216
|
+
keep->words = s->held_words;
|
|
217
|
+
keep->rows = s->held_rows;
|
|
218
|
+
keep->dropped = s->held_dropped;
|
|
219
|
+
keep->at = (int64_t)time(NULL);
|
|
220
|
+
for (int kind = 0; kind < RA_PIN_KINDS; kind++) {
|
|
221
|
+
keep->pinned_count[kind] = s->pinned_count[kind] < RA_PINNED ? (int)s->pinned_count[kind] : RA_PINNED;
|
|
222
|
+
}
|
|
223
|
+
memcpy(keep->pinned, s->pinned, sizeof(keep->pinned));
|
|
224
|
+
|
|
225
|
+
if (s->keeps_last) s->keeps_last->next = keep; else s->keeps = keep;
|
|
226
|
+
s->keeps_last = keep;
|
|
227
|
+
__atomic_fetch_add(&s->dropped, keep->dropped, __ATOMIC_RELAXED);
|
|
228
|
+
forget_held(s);
|
|
229
|
+
pthread_cond_signal(&s->not_empty);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/* keep(path): the rows held since the last keep or discard are written to `path` as a segment of
|
|
233
|
+
* their own. False when nothing was held, and no file is made. */
|
|
234
|
+
static VALUE sink_keep(VALUE self, VALUE path) {
|
|
235
|
+
sink *s = ra_sink_of(self);
|
|
236
|
+
StringValue(path);
|
|
237
|
+
if (!s->hold) return Qfalse;
|
|
238
|
+
|
|
239
|
+
struct ra_keep *keep = new_keep(path);
|
|
240
|
+
if (!keep) { /* no memory to say what to write: the request goes the way a healthy one does */
|
|
241
|
+
sink_discard(self);
|
|
242
|
+
return Qfalse;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
pthread_mutex_lock(&s->mu);
|
|
246
|
+
int kept = !s->disabled && s->held_rows > 0;
|
|
247
|
+
if (kept) queue_keep(s, keep);
|
|
248
|
+
pthread_mutex_unlock(&s->mu);
|
|
249
|
+
|
|
250
|
+
if (kept) return Qtrue;
|
|
251
|
+
free_keep(keep);
|
|
252
|
+
return Qfalse;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/* largest_request: the bytes the largest request this stream has held took, or would have taken
|
|
256
|
+
* in a ring with room for it. Only the stream's own thread adds to it, with the GVL held. */
|
|
257
|
+
static VALUE sink_largest_request(VALUE self) {
|
|
258
|
+
return LL2NUM(ra_sink_of(self)->largest_words * (int64_t)sizeof(uint64_t));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/* held(most): the newest whole rows of the request being held, at most `most` words of them, as
|
|
262
|
+
* [words, rows left out]. The words are the rows as the ring has them, in a String of 64-bit
|
|
263
|
+
* integers, for Trace::NilOrigin to read while the request is still open. nil for a stream that
|
|
264
|
+
* does not hold.
|
|
265
|
+
*
|
|
266
|
+
* Only the thread that owns the stream adds to what is held, and it is the one asking, so the size
|
|
267
|
+
* is read before the lock is taken and the String is made with no lock held. */
|
|
268
|
+
static VALUE sink_held(VALUE self, VALUE most) {
|
|
269
|
+
sink *s = ra_sink_of(self);
|
|
270
|
+
size_t limit = NUM2SIZET(most);
|
|
271
|
+
if (!s->hold) return Qnil;
|
|
272
|
+
|
|
273
|
+
size_t room = s->held_words < limit ? s->held_words : limit;
|
|
274
|
+
VALUE words = rb_str_buf_new((long)(room * sizeof(uint64_t)));
|
|
275
|
+
size_t left = 0, skipped = 0;
|
|
276
|
+
|
|
277
|
+
pthread_mutex_lock(&s->mu);
|
|
278
|
+
size_t at = (s->head + s->cap - s->held_words) % s->cap;
|
|
279
|
+
left = s->held_words;
|
|
280
|
+
while (left > room) {
|
|
281
|
+
size_t n = (size_t)(s->words[at] >> RA_ROW_SHIFT);
|
|
282
|
+
if (n < 1 || n > left) { left = 0; break; } /* not the start of a row: hand over nothing */
|
|
283
|
+
at = (at + n) % s->cap;
|
|
284
|
+
left -= n;
|
|
285
|
+
skipped++;
|
|
286
|
+
}
|
|
287
|
+
ra_ring_read(s, at, (uint64_t *)RSTRING_PTR(words), left);
|
|
288
|
+
pthread_mutex_unlock(&s->mu);
|
|
289
|
+
|
|
290
|
+
rb_str_set_len(words, (long)(left * sizeof(uint64_t)));
|
|
291
|
+
return rb_assoc_new(words, SIZET2NUM(skipped));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/* ------------------------------------------------------------ the writer's side */
|
|
295
|
+
|
|
296
|
+
/* The definitions that were there when the writer looked. */
|
|
297
|
+
struct defs_seen { struct ra_def_block *first, *last; size_t last_count; };
|
|
298
|
+
|
|
299
|
+
static struct defs_seen defs_look(const ra_defs *defs) {
|
|
300
|
+
struct defs_seen seen = { defs->first, defs->last, defs->last ? defs->last->count : 0 };
|
|
301
|
+
return seen;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
static size_t seen_in(const struct defs_seen *seen, const struct ra_def_block *block) {
|
|
305
|
+
return block == seen->last ? seen->last_count : RA_DEF_BLOCK;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/* The ids a kept request names, a bit for each. Without the memory for them every definition is
|
|
309
|
+
* written, which reads the same and is only larger. */
|
|
310
|
+
struct named { uint8_t *bits; int64_t top; };
|
|
311
|
+
|
|
312
|
+
#define RA_NAMED_MOST (1LL << 32) /* ids past this many are not worth a table: write them all */
|
|
313
|
+
|
|
314
|
+
static struct named named_upto(int64_t top) {
|
|
315
|
+
struct named ids = { NULL, top };
|
|
316
|
+
if (top >= 0 && top < RA_NAMED_MOST) ids.bits = ra_calloc((size_t)(top / 8) + 1, 1);
|
|
317
|
+
return ids;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
static void name(struct named *ids, int64_t id) {
|
|
321
|
+
if (ids->bits && id >= 0 && id <= ids->top) ids->bits[id >> 3] |= (uint8_t)(1 << (id & 7));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
static int named(const struct named *ids, int64_t id) {
|
|
325
|
+
if (!ids->bits) return 1;
|
|
326
|
+
return id >= 0 && id <= ids->top && (ids->bits[id >> 3] >> (id & 7)) & 1;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
struct kept {
|
|
330
|
+
struct named strings, paths;
|
|
331
|
+
int pinned_live; /* a bit for each pinned row whose tree still has a row, RA_PINNED to a kind */
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/* A row's values follow its fixed fields: the tier, the true count, the count stored, the
|
|
335
|
+
* receiver's id, then each stored value's class, their object ids and, in a keywords row, their
|
|
336
|
+
* names. Notes the classes and the names. Returns 0 for a tail that does not fit its row. */
|
|
337
|
+
static int name_values(struct kept *k, const uint64_t *row, size_t n, size_t fixed, int keywords) {
|
|
338
|
+
if (!(row[0] & RA_ROW_TAIL)) return 1;
|
|
339
|
+
if (n < 1 + fixed + 4) return 0;
|
|
340
|
+
|
|
341
|
+
const uint64_t *tail = row + 1 + fixed;
|
|
342
|
+
int64_t stored = (int64_t)tail[2];
|
|
343
|
+
if (stored < 0 || stored > MAX_ARGS || 1 + fixed + 4 + (size_t)stored * (keywords ? 3 : 1) > n) return 0;
|
|
344
|
+
for (int64_t i = 0; i < stored; i++) name(&k->strings, (int64_t)tail[4 + i]);
|
|
345
|
+
for (int64_t i = 0; keywords && i < stored; i++) name(&k->strings, (int64_t)tail[4 + 2 * stored + i]);
|
|
346
|
+
return 1;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/* How many fixed fields each kind of row a stream holds has, 0 for a kind it never holds. */
|
|
350
|
+
static const size_t HELD_FIELDS[] = { 0, CALL_FIELDS, 0, 4, 2, 7, 2, 0, 3, 2, 6, 2 };
|
|
351
|
+
|
|
352
|
+
/* Notes every string and path one row names. Returns 0 for a row that makes no sense. */
|
|
353
|
+
static int name_row(struct kept *k, const uint64_t *row, size_t n) {
|
|
354
|
+
int kind = (int)(row[0] & 0xff);
|
|
355
|
+
if (kind > SLOT_LAST || HELD_FIELDS[kind] == 0 || n < 1 + HELD_FIELDS[kind]) return 0;
|
|
356
|
+
|
|
357
|
+
switch (kind) {
|
|
358
|
+
case SLOT_CALL:
|
|
359
|
+
name(&k->paths, (int64_t)row[3]);
|
|
360
|
+
name(&k->strings, (int64_t)row[4]);
|
|
361
|
+
break;
|
|
362
|
+
case SLOT_RETURN:
|
|
363
|
+
name(&k->strings, (int64_t)row[3]);
|
|
364
|
+
break;
|
|
365
|
+
case SLOT_UNWIND:
|
|
366
|
+
name(&k->strings, (int64_t)row[3]);
|
|
367
|
+
name(&k->strings, (int64_t)row[4]);
|
|
368
|
+
name(&k->strings, (int64_t)row[6]);
|
|
369
|
+
name(&k->strings, (int64_t)row[7]);
|
|
370
|
+
break;
|
|
371
|
+
case SLOT_ENTRY:
|
|
372
|
+
case SLOT_INNER_ENTRY:
|
|
373
|
+
name(&k->strings, (int64_t)row[2]);
|
|
374
|
+
break;
|
|
375
|
+
case SLOT_ANSWERED:
|
|
376
|
+
name(&k->strings, (int64_t)row[3]);
|
|
377
|
+
break;
|
|
378
|
+
case SLOT_VALUE:
|
|
379
|
+
if (row[3] == VALUE_KEYWORD) name(&k->strings, (int64_t)row[4]);
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
return name_values(k, row, n, HELD_FIELDS[kind], kind == SLOT_KWARGS || kind == SLOT_IVARS);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
static void note_tree(struct kept *k, const struct ra_keep *keep, const uint64_t *row) {
|
|
386
|
+
if (pin_kind(row[0]) >= 0) return;
|
|
387
|
+
for (int kind = 0; kind < RA_PIN_KINDS; kind++) {
|
|
388
|
+
for (int i = 0; i < keep->pinned_count[kind]; i++) {
|
|
389
|
+
if (keep->pinned[kind][i][0] == (int64_t)row[1]) k->pinned_live |= 1 << (kind * RA_PINNED + i);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/* Reads the request's rows where they lie, from word `at`. The recorder writes only past the head
|
|
395
|
+
* and gives up no row while a kept request waits, so these are the writer's until it gives them
|
|
396
|
+
* back. */
|
|
397
|
+
static int name_rows(sink *s, const struct ra_keep *keep, size_t at, struct kept *k) {
|
|
398
|
+
uint64_t row[RA_ROW_MAX];
|
|
399
|
+
size_t taken = 0;
|
|
400
|
+
for (size_t i = 0; i < keep->rows; i++) {
|
|
401
|
+
size_t start = (at + taken) % s->cap;
|
|
402
|
+
size_t n = (size_t)(s->words[start] >> RA_ROW_SHIFT);
|
|
403
|
+
RA_GUARD(s, n >= 1 && n <= RA_ROW_MAX && taken + n <= keep->words, 0);
|
|
404
|
+
ra_ring_read(s, start, row, n);
|
|
405
|
+
RA_GUARD(s, name_row(k, row, n), 0);
|
|
406
|
+
note_tree(k, keep, row);
|
|
407
|
+
taken += n;
|
|
408
|
+
}
|
|
409
|
+
return 1;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/* A path that is named names its caller's path and four strings. A caller is defined before the
|
|
413
|
+
* paths that go through it, so going from the newest back reaches every ancestor. */
|
|
414
|
+
static void name_ancestors(const struct defs_seen *paths, struct kept *k) {
|
|
415
|
+
for (struct ra_def_block *block = paths->last; block; block = block->prev) {
|
|
416
|
+
for (size_t i = seen_in(paths, block); i-- > 0;) {
|
|
417
|
+
const uint64_t *path = block->words + i * PATH_FIELDS;
|
|
418
|
+
if (!named(&k->paths, (int64_t)path[0])) continue;
|
|
419
|
+
name(&k->paths, (int64_t)path[1]);
|
|
420
|
+
name(&k->strings, (int64_t)path[2]);
|
|
421
|
+
name(&k->strings, (int64_t)path[4]);
|
|
422
|
+
name(&k->strings, (int64_t)path[5]);
|
|
423
|
+
name(&k->strings, (int64_t)path[7]);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
static void write_strings(FILE *file, const struct defs_seen *strings, const struct kept *k) {
|
|
429
|
+
for (struct ra_def_block *block = strings->first; block; block = block->next) {
|
|
430
|
+
for (size_t i = 0; i < seen_in(strings, block); i++) {
|
|
431
|
+
const uint64_t *entry = block->words + i * 3;
|
|
432
|
+
if (!named(&k->strings, (int64_t)entry[0])) continue;
|
|
433
|
+
ra_write_dict(file, (int64_t)entry[0], (const char *)(uintptr_t)entry[1], (long)entry[2]);
|
|
434
|
+
}
|
|
435
|
+
if (block == strings->last) break;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
static void write_paths(FILE *file, const struct defs_seen *paths, const struct kept *k) {
|
|
440
|
+
for (struct ra_def_block *block = paths->first; block; block = block->next) {
|
|
441
|
+
for (size_t i = 0; i < seen_in(paths, block); i++) {
|
|
442
|
+
const int64_t *path = (const int64_t *)(block->words + i * PATH_FIELDS);
|
|
443
|
+
if (named(&k->paths, path[0])) ra_write_path(file, path);
|
|
444
|
+
}
|
|
445
|
+
if (block == paths->last) break;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/* The segment says first that it is one request that was kept, how many of its rows the ring had
|
|
450
|
+
* no room for, and when it was kept, in seconds since 1970. */
|
|
451
|
+
static void write_head(FILE *file, const struct ra_keep *keep) {
|
|
452
|
+
ra_mark_format(file);
|
|
453
|
+
fprintf(file, "[%d,%lld,%lld]\n", OP_HELD, (long long)keep->dropped, (long long)keep->at);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
static void write_pinned(FILE *file, const struct ra_keep *keep, const struct kept *k) {
|
|
457
|
+
for (int kind = 0; kind < RA_PIN_KINDS; kind++) {
|
|
458
|
+
for (int i = 0; i < keep->pinned_count[kind]; i++) {
|
|
459
|
+
if (!(k->pinned_live & (1 << (kind * RA_PINNED + i)))) continue;
|
|
460
|
+
const int64_t *pin = keep->pinned[kind][i];
|
|
461
|
+
fprintf(file, "[%d,%lld,%lld]\n", PIN_OPS[kind], (long long)pin[0], (long long)pin[1]);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
struct looked { size_t at; struct defs_seen strings, paths; int64_t strings_top, paths_top; };
|
|
467
|
+
|
|
468
|
+
static int write_kept(sink *s, const struct ra_keep *keep, const struct looked *seen, char *out) {
|
|
469
|
+
struct kept k = { named_upto(seen->strings_top), named_upto(seen->paths_top), 0 };
|
|
470
|
+
int whole = name_rows(s, keep, seen->at, &k);
|
|
471
|
+
if (whole) {
|
|
472
|
+
for (int kind = 0; kind < RA_PIN_KINDS; kind++) {
|
|
473
|
+
for (int i = 0; i < keep->pinned_count[kind]; i++) {
|
|
474
|
+
if (k.pinned_live & (1 << (kind * RA_PINNED + i))) name(&k.strings, keep->pinned[kind][i][1]);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
name_ancestors(&seen->paths, &k);
|
|
478
|
+
|
|
479
|
+
write_head(s->file, keep);
|
|
480
|
+
write_strings(s->file, &seen->strings, &k);
|
|
481
|
+
write_paths(s->file, &seen->paths, &k);
|
|
482
|
+
write_pinned(s->file, keep, &k);
|
|
483
|
+
ra_note_io_error(s);
|
|
484
|
+
whole = ra_drain_rows(s, seen->at, keep->rows, out) == keep->words;
|
|
485
|
+
}
|
|
486
|
+
free(k.strings.bits);
|
|
487
|
+
free(k.paths.bits);
|
|
488
|
+
return whole;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
static void report_segment(sink *s, const char *what, const char *path) {
|
|
492
|
+
char msg[256];
|
|
493
|
+
__atomic_fetch_add(&ra_write_errors, 1, __ATOMIC_RELAXED);
|
|
494
|
+
snprintf(msg, sizeof(msg), "runtime_analysis native: could not %s segment %s: %s\n", what, path, strerror(errno));
|
|
495
|
+
ra_report_once(s, msg);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/* A segment that could not be written whole, because a row made no sense, is not left behind. */
|
|
499
|
+
static void write_segment(sink *s, const struct ra_keep *keep, const struct looked *seen, char *out) {
|
|
500
|
+
s->file = fopen(keep->part, "w");
|
|
501
|
+
if (!s->file) {
|
|
502
|
+
report_segment(s, "open", keep->part);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
s->write_failed = 0;
|
|
506
|
+
int whole = write_kept(s, keep, seen, out);
|
|
507
|
+
fclose(s->file);
|
|
508
|
+
s->file = NULL;
|
|
509
|
+
if (!whole) unlink(keep->part);
|
|
510
|
+
else if (rename(keep->part, keep->path) != 0) report_segment(s, "name", keep->path);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/* The writer of a stream that holds: asleep until a request is kept, and at the stream's close it
|
|
514
|
+
* writes the ones still waiting and leaves what was only held. A request stays first in the list
|
|
515
|
+
* while it is written, which is what tells the recorder its rows are not to be given up. */
|
|
516
|
+
void ra_hold_main(sink *s, char *out) {
|
|
517
|
+
for (;;) {
|
|
518
|
+
pthread_mutex_lock(&s->mu);
|
|
519
|
+
while (!s->keeps && s->running && !s->disabled) pthread_cond_wait(&s->not_empty, &s->mu);
|
|
520
|
+
struct ra_keep *keep = s->keeps;
|
|
521
|
+
if (s->disabled || !keep) {
|
|
522
|
+
pthread_mutex_unlock(&s->mu);
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
struct looked seen = { s->tail, defs_look(&s->strings), defs_look(&s->paths), s->strings.top, s->paths.top };
|
|
526
|
+
long stall = ra_testing_stall_asked(s);
|
|
527
|
+
pthread_mutex_unlock(&s->mu);
|
|
528
|
+
|
|
529
|
+
ra_testing_stall(s, stall);
|
|
530
|
+
write_segment(s, keep, &seen, out);
|
|
531
|
+
|
|
532
|
+
pthread_mutex_lock(&s->mu);
|
|
533
|
+
s->tail = (seen.at + keep->words) % s->cap;
|
|
534
|
+
s->used -= keep->words;
|
|
535
|
+
s->rows -= keep->rows;
|
|
536
|
+
s->keeps = keep->next;
|
|
537
|
+
if (!s->keeps) s->keeps_last = NULL;
|
|
538
|
+
if (s->used == 0) s->head = s->tail = 0;
|
|
539
|
+
pthread_mutex_unlock(&s->mu);
|
|
540
|
+
|
|
541
|
+
free_keep(keep);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
void Init_ra_hold(VALUE cSink) {
|
|
546
|
+
rb_define_method(cSink, "keep", sink_keep, 1);
|
|
547
|
+
rb_define_method(cSink, "discard", sink_discard, 0);
|
|
548
|
+
rb_define_method(cSink, "held", sink_held, 1);
|
|
549
|
+
rb_define_method(cSink, "largest_request", sink_largest_request, 0);
|
|
550
|
+
|
|
551
|
+
/* What a held row's first word says, for the reader of #held: its kind in the low byte, whether
|
|
552
|
+
* values follow its fixed fields, and its length in words above RA_ROW_SHIFT bits. */
|
|
553
|
+
rb_define_const(cSink, "ROW_CALL", INT2FIX(SLOT_CALL));
|
|
554
|
+
rb_define_const(cSink, "ROW_RETURN", INT2FIX(SLOT_RETURN));
|
|
555
|
+
rb_define_const(cSink, "ROW_KWARGS", INT2FIX(SLOT_KWARGS));
|
|
556
|
+
rb_define_const(cSink, "ROW_UNWIND", INT2FIX(SLOT_UNWIND));
|
|
557
|
+
rb_define_const(cSink, "ROW_ENTRY", INT2FIX(SLOT_ENTRY));
|
|
558
|
+
rb_define_const(cSink, "ROW_TAIL", INT2FIX(RA_ROW_TAIL));
|
|
559
|
+
rb_define_const(cSink, "ROW_SHIFT", INT2FIX(RA_ROW_SHIFT));
|
|
560
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#include <ruby.h>
|
|
2
|
+
#include "state.h"
|
|
3
|
+
#include "names.h"
|
|
4
|
+
#include "novelty.h"
|
|
5
|
+
#include "sink.h"
|
|
6
|
+
#include "fast.h"
|
|
7
|
+
#include "wrapper.h"
|
|
8
|
+
#include "os.h"
|
|
9
|
+
|
|
10
|
+
/* The extension is RuntimeAnalysis::Native and the two classes of RuntimeAnalysis::Trace that the
|
|
11
|
+
* wrapper reads on every call. sink.h says which file holds what. */
|
|
12
|
+
RUBY_FUNC_EXPORTED void Init_native_ext(void) {
|
|
13
|
+
VALUE mRoot = rb_define_module("RuntimeAnalysis");
|
|
14
|
+
VALUE mNative = rb_define_module_under(mRoot, "Native");
|
|
15
|
+
VALUE mTrace = rb_define_module_under(mRoot, "Trace");
|
|
16
|
+
|
|
17
|
+
/* Native::Sink is a stream. Native::Wrapper is what the recorder tells the wrappers. */
|
|
18
|
+
VALUE mWrapper = rb_define_module_under(mNative, "Wrapper");
|
|
19
|
+
|
|
20
|
+
Init_ra_state(mTrace);
|
|
21
|
+
Init_ra_novelty(mTrace);
|
|
22
|
+
Init_ra_names();
|
|
23
|
+
Init_ra_sink(mNative);
|
|
24
|
+
Init_ra_fast(mWrapper);
|
|
25
|
+
Init_ra_wrapper(mWrapper);
|
|
26
|
+
|
|
27
|
+
/* Native::Testing is what the gem's own specs steer the extension by. Only the build made for
|
|
28
|
+
* them has it: see os.h. */
|
|
29
|
+
#ifdef RA_TESTING
|
|
30
|
+
VALUE mTesting = rb_define_module_under(mNative, "Testing");
|
|
31
|
+
Init_ra_state_testing(mTesting);
|
|
32
|
+
Init_ra_ring_testing(mTesting);
|
|
33
|
+
Init_ra_fast_testing(mTesting);
|
|
34
|
+
Init_ra_os(mTesting);
|
|
35
|
+
#endif
|
|
36
|
+
}
|