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.
Files changed (267) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +115 -0
  3. data/LICENSE.txt +96 -0
  4. data/README.md +164 -0
  5. data/editor/nvim/README.md +278 -0
  6. data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
  7. data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
  8. data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
  9. data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
  10. data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
  11. data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
  12. data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
  13. data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
  14. data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
  15. data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
  16. data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
  17. data/editor/nvim/plugin/runtime_analysis.lua +71 -0
  18. data/examples/trackers/PROMPT.md +86 -0
  19. data/examples/trackers/honeybadger.rb +19 -0
  20. data/examples/trackers/sentry.rb +16 -0
  21. data/ext/runtime_analysis/extconf.rb +68 -0
  22. data/ext/runtime_analysis/fast.c +775 -0
  23. data/ext/runtime_analysis/fast.h +59 -0
  24. data/ext/runtime_analysis/hold.c +560 -0
  25. data/ext/runtime_analysis/init.c +36 -0
  26. data/ext/runtime_analysis/names.c +78 -0
  27. data/ext/runtime_analysis/names.h +28 -0
  28. data/ext/runtime_analysis/novelty.c +139 -0
  29. data/ext/runtime_analysis/novelty.h +22 -0
  30. data/ext/runtime_analysis/os.c +73 -0
  31. data/ext/runtime_analysis/os.h +32 -0
  32. data/ext/runtime_analysis/ring.c +445 -0
  33. data/ext/runtime_analysis/rows.c +304 -0
  34. data/ext/runtime_analysis/sink.c +485 -0
  35. data/ext/runtime_analysis/sink.h +240 -0
  36. data/ext/runtime_analysis/state.c +254 -0
  37. data/ext/runtime_analysis/state.h +57 -0
  38. data/ext/runtime_analysis/wrapper.c +278 -0
  39. data/ext/runtime_analysis/wrapper.h +24 -0
  40. data/lib/runtime_analysis/errors.rb +34 -0
  41. data/lib/runtime_analysis/failsafe.rb +123 -0
  42. data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
  43. data/lib/runtime_analysis/host/declared.rb +152 -0
  44. data/lib/runtime_analysis/host/inject.rb +90 -0
  45. data/lib/runtime_analysis/host/job_runners.rb +27 -0
  46. data/lib/runtime_analysis/host/middleware.rb +23 -0
  47. data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
  48. data/lib/runtime_analysis/host/railtie.rb +42 -0
  49. data/lib/runtime_analysis/host/reports.rb +54 -0
  50. data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
  51. data/lib/runtime_analysis/host/web.rb +22 -0
  52. data/lib/runtime_analysis/host.rb +34 -0
  53. data/lib/runtime_analysis/inject.rb +5 -0
  54. data/lib/runtime_analysis/minitest.rb +9 -0
  55. data/lib/runtime_analysis/native/build.rb +17 -0
  56. data/lib/runtime_analysis/native/byte_size.rb +27 -0
  57. data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
  58. data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
  59. data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
  60. data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
  61. data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
  62. data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
  63. data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
  64. data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
  65. data/lib/runtime_analysis/native/sink.rb +12 -0
  66. data/lib/runtime_analysis/native.rb +74 -0
  67. data/lib/runtime_analysis/platform.rb +30 -0
  68. data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
  69. data/lib/runtime_analysis/query/index/graph.rb +107 -0
  70. data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
  71. data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
  72. data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
  73. data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
  74. data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
  75. data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
  76. data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
  77. data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
  78. data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
  79. data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
  80. data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
  81. data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
  82. data/lib/runtime_analysis/query/index/reading.rb +80 -0
  83. data/lib/runtime_analysis/query/index/shapes.rb +142 -0
  84. data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
  85. data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
  86. data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
  87. data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
  88. data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
  89. data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
  90. data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
  91. data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
  92. data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
  93. data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
  94. data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
  95. data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
  96. data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
  97. data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
  98. data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
  99. data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
  100. data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
  101. data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
  102. data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
  103. data/lib/runtime_analysis/query/index/signatures.rb +93 -0
  104. data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
  105. data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
  106. data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
  107. data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
  108. data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
  109. data/lib/runtime_analysis/query/index/static/method.rb +19 -0
  110. data/lib/runtime_analysis/query/index/static/params.rb +52 -0
  111. data/lib/runtime_analysis/query/index/static/source.rb +69 -0
  112. data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
  113. data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
  114. data/lib/runtime_analysis/query/index/static/value.rb +17 -0
  115. data/lib/runtime_analysis/query/index/static/values.rb +71 -0
  116. data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
  117. data/lib/runtime_analysis/query/index/static.rb +85 -0
  118. data/lib/runtime_analysis/query/index.rb +32 -0
  119. data/lib/runtime_analysis/query/nodes.rb +60 -0
  120. data/lib/runtime_analysis/query/project/config.rb +108 -0
  121. data/lib/runtime_analysis/query/project.rb +40 -0
  122. data/lib/runtime_analysis/query/report/document.rb +84 -0
  123. data/lib/runtime_analysis/query/report/finding.rb +15 -0
  124. data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
  125. data/lib/runtime_analysis/query/report/list.rb +15 -0
  126. data/lib/runtime_analysis/query/report/location.rb +14 -0
  127. data/lib/runtime_analysis/query/report/one_row.rb +14 -0
  128. data/lib/runtime_analysis/query/report/outline.rb +15 -0
  129. data/lib/runtime_analysis/query/report/paint.rb +34 -0
  130. data/lib/runtime_analysis/query/report/related.rb +11 -0
  131. data/lib/runtime_analysis/query/report/table.rb +15 -0
  132. data/lib/runtime_analysis/query/report/text.rb +13 -0
  133. data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
  134. data/lib/runtime_analysis/query/report.rb +35 -0
  135. data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
  136. data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
  137. data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
  138. data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
  139. data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
  140. data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
  141. data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
  142. data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
  143. data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
  144. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
  145. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
  146. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
  147. data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
  148. data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
  149. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
  150. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
  151. data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
  152. data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
  153. data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
  154. data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
  155. data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
  156. data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
  157. data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
  158. data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
  159. data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
  160. data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
  161. data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
  162. data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
  163. data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
  164. data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
  165. data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
  166. data/lib/runtime_analysis/query/surfaces.rb +14 -0
  167. data/lib/runtime_analysis/query/tools/base.rb +86 -0
  168. data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
  169. data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
  170. data/lib/runtime_analysis/query/tools/origins.rb +47 -0
  171. data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
  172. data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
  173. data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
  174. data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
  175. data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
  176. data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
  177. data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
  178. data/lib/runtime_analysis/query/tools.rb +34 -0
  179. data/lib/runtime_analysis/query.rb +33 -0
  180. data/lib/runtime_analysis/recording/blob.rb +13 -0
  181. data/lib/runtime_analysis/recording/format.rb +32 -0
  182. data/lib/runtime_analysis/recording/opcodes.rb +30 -0
  183. data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
  184. data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
  185. data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
  186. data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
  187. data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
  188. data/lib/runtime_analysis/recording/seen.rb +63 -0
  189. data/lib/runtime_analysis/recording/sink.rb +19 -0
  190. data/lib/runtime_analysis/recording/source_blob.rb +20 -0
  191. data/lib/runtime_analysis/recording.rb +52 -0
  192. data/lib/runtime_analysis/rspec.rb +8 -0
  193. data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
  194. data/lib/runtime_analysis/ruby_parsing.rb +98 -0
  195. data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
  196. data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
  197. data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
  198. data/lib/runtime_analysis/trace/configuration.rb +115 -0
  199. data/lib/runtime_analysis/trace/entries.rb +47 -0
  200. data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
  201. data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
  202. data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
  203. data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
  204. data/lib/runtime_analysis/trace/hold.rb +142 -0
  205. data/lib/runtime_analysis/trace/inclusion.rb +46 -0
  206. data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
  207. data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
  208. data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
  209. data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
  210. data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
  211. data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
  212. data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
  213. data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
  214. data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
  215. data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
  216. data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
  217. data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
  218. data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
  219. data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
  220. data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
  221. data/lib/runtime_analysis/trace/installer.rb +149 -0
  222. data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
  223. data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
  224. data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
  225. data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
  226. data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
  227. data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
  228. data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
  229. data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
  230. data/lib/runtime_analysis/trace/mechanism.rb +28 -0
  231. data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
  232. data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
  233. data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
  234. data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
  235. data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
  236. data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
  237. data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
  238. data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
  239. data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
  240. data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
  241. data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
  242. data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
  243. data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
  244. data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
  245. data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
  246. data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
  247. data/lib/runtime_analysis/trace/novelty.rb +17 -0
  248. data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
  249. data/lib/runtime_analysis/trace/pure/state.rb +94 -0
  250. data/lib/runtime_analysis/trace/pure.rb +15 -0
  251. data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
  252. data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
  253. data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
  254. data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
  255. data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
  256. data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
  257. data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
  258. data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
  259. data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
  260. data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
  261. data/lib/runtime_analysis/trace/recorder.rb +149 -0
  262. data/lib/runtime_analysis/trace/state.rb +16 -0
  263. data/lib/runtime_analysis/trace.rb +142 -0
  264. data/lib/runtime_analysis/version.rb +6 -0
  265. data/lib/runtime_analysis/web.rb +6 -0
  266. data/lib/runtime_analysis.rb +125 -0
  267. metadata +330 -0
@@ -0,0 +1,485 @@
1
+ #include "sink.h"
2
+ #include "state.h"
3
+ #include "fast.h"
4
+ #include <errno.h>
5
+ #include <poll.h>
6
+ #include <sched.h>
7
+ #include <stddef.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+ #include <time.h>
11
+ #include <unistd.h>
12
+
13
+ /* The streams, and what Ruby calls on one: open it, hand it rows, roll it to a new file, close it.
14
+ * Each row Ruby hands over is copied into a slot on the stack and pushed onto the stream's ring. */
15
+
16
+ sink **ra_sinks = NULL;
17
+ size_t ra_sinks_cap = 0;
18
+ unsigned long ra_epoch = 1;
19
+ int64_t ra_write_errors = 0;
20
+
21
+ /* Process-wide drop total from sinks that have already been closed, so a short-lived thread's drops are
22
+ * not lost when its sink is torn down. Touched only under the GVL (teardown and stats both hold it), so
23
+ * it needs no atomic of its own; live sinks' drops are summed lock-free from their per-sink counters. */
24
+ static int64_t g_closed_dropped = 0;
25
+
26
+ /* Reports the first fault on a sink to stderr, once, without ever waiting. If stderr cannot take the
27
+ * line this instant, as when a terminal or a process manager has stalled the pipe, the line is
28
+ * dropped, and Ruby can still see the fault through #dropped and #disabled?. Called only on the
29
+ * writer thread and never with the sink's lock held, so the producer cannot be held up behind it. */
30
+ void ra_report_once(sink *s, const char *msg) {
31
+ if (s->diag_reported) return;
32
+ s->diag_reported = 1;
33
+
34
+ struct pollfd pfd = { .fd = STDERR_FILENO, .events = POLLOUT, .revents = 0 };
35
+ if (poll(&pfd, 1, 0) == 1 && (pfd.revents & POLLOUT)) {
36
+ ssize_t written = write(STDERR_FILENO, msg, strlen(msg));
37
+ (void)written; /* best-effort: a short or failed write must still not disturb the host */
38
+ }
39
+ }
40
+
41
+ static long registry_insert(sink *s) {
42
+ for (size_t i = 0; i < ra_sinks_cap; i++) {
43
+ if (!ra_sinks[i]) { ra_sinks[i] = s; return (long)i; }
44
+ }
45
+ size_t old = ra_sinks_cap;
46
+ size_t want = old ? old * 2 : 8;
47
+ sink **grown = ra_realloc(ra_sinks, want * sizeof(sink *));
48
+ if (!grown) return -1; /* keep the existing table intact; caller raises */
49
+ ra_sinks = grown;
50
+ for (size_t i = old; i < want; i++) ra_sinks[i] = NULL;
51
+ ra_sinks_cap = want;
52
+ ra_sinks[old] = s;
53
+ return (long)old;
54
+ }
55
+
56
+ /* Native::Sink, which is what Ruby holds of a stream: its place in the table, and the stream that
57
+ * was there when it was opened. A close, or a fork, leaves the place empty or holding another
58
+ * stream, and the object then names nothing. */
59
+ typedef struct { long handle; sink *opened; } sink_ref;
60
+
61
+ static const rb_data_type_t sink_type = {
62
+ "RuntimeAnalysis::Native::Sink",
63
+ { NULL, RUBY_DEFAULT_FREE, NULL, },
64
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
65
+ };
66
+
67
+ static sink_ref *ref_of(VALUE self) {
68
+ sink_ref *ref;
69
+ TypedData_Get_Struct(self, sink_ref, &sink_type, ref);
70
+ return ref;
71
+ }
72
+
73
+ static sink *open_sink(const sink_ref *ref) {
74
+ long h = ref->handle;
75
+ return (h >= 0 && (size_t)h < ra_sinks_cap && ra_sinks[h] == ref->opened) ? ref->opened : NULL;
76
+ }
77
+
78
+ sink *ra_sink_of(VALUE self) {
79
+ sink *s = open_sink(ref_of(self));
80
+ if (!s) rb_raise(rb_eRuntimeError, "runtime_analysis native: this sink is closed");
81
+ return s;
82
+ }
83
+
84
+ static void teardown(sink *s);
85
+
86
+ /* The system had no memory for a stream. The error is one the recorder catches and counts, so the
87
+ * program that was being recorded carries on. NoMemoryError would have passed through it. */
88
+ NORETURN(static void no_memory(void));
89
+ static void no_memory(void) {
90
+ rb_syserr_fail(ENOMEM, "runtime_analysis native: no memory for a stream");
91
+ }
92
+
93
+ /* A stream that could not be opened leaves no segment behind: an empty one would read as a
94
+ * thread that recorded nothing. `fresh` says the file was not there before this stream, and one
95
+ * an earlier recording wrote is left as it was. */
96
+ static void give_up_file(FILE *f, const char *path, int fresh) {
97
+ if (f) fclose(f);
98
+ if (fresh) unlink(path);
99
+ }
100
+
101
+ /* A stream with no path holds: it has no file until it is told to keep a request, see hold.c.
102
+ * The memory is asked for before the file is opened, so that a stream there is no memory for
103
+ * creates nothing. */
104
+ static sink *new_sink(const char *path, size_t cap, int fresh) {
105
+ sink *s = ra_calloc(1, sizeof(sink));
106
+ uint64_t *words = s ? ra_calloc(cap, sizeof(uint64_t)) : NULL;
107
+ if (!s || !words) { free(s); free(words); no_memory(); }
108
+
109
+ FILE *f = path ? fopen(path, "a") : NULL;
110
+ if (path && !f) {
111
+ int failed = errno;
112
+ free(s);
113
+ free(words);
114
+ rb_syserr_fail(failed, "could not open runtime_analysis native output file");
115
+ }
116
+ s->words = words;
117
+ s->cap = cap;
118
+ s->file = f;
119
+ s->hold = !path;
120
+ s->strings.width = 3;
121
+ s->paths.width = PATH_FIELDS;
122
+ s->strings.top = s->paths.top = -1;
123
+ if (f) s->bytes_written = ra_mark_format(f);
124
+ s->running = 1;
125
+ pthread_mutex_init(&s->mu, NULL);
126
+ pthread_cond_init(&s->not_empty, NULL);
127
+ ra_gather_init(&s->gather);
128
+
129
+ if (ra_thread_create(&s->consumer, NULL, ra_consumer_main, s) != 0) {
130
+ pthread_mutex_destroy(&s->mu);
131
+ pthread_cond_destroy(&s->not_empty);
132
+ pthread_cond_destroy(&s->gather);
133
+ give_up_file(f, path, fresh);
134
+ free(s->words);
135
+ free(s);
136
+ rb_raise(rb_eRuntimeError, "could not start runtime_analysis native consumer thread");
137
+ }
138
+ s->consumer_started = 1;
139
+ return s;
140
+ }
141
+
142
+ static VALUE open_stream(VALUE klass, const char *path, VALUE capacity) {
143
+ long requested = NIL_P(capacity) ? (1 << 16) : NUM2LONG(capacity);
144
+ if (requested < 0) rb_raise(rb_eArgError, "runtime_analysis native capacity must be non-negative");
145
+ size_t cap = (size_t)(requested < 2 ? 2 : requested) * RA_WORDS_PER_UNIT;
146
+
147
+ sink_ref *ref;
148
+ VALUE self = TypedData_Make_Struct(klass, sink_ref, &sink_type, ref);
149
+ ref->handle = -1;
150
+ int fresh = path && access(path, F_OK) != 0;
151
+ sink *s = new_sink(path, cap, fresh);
152
+ ref->handle = registry_insert(s);
153
+ if (ref->handle < 0) { /* no room in the table: unwind the sink, then raise */
154
+ teardown(s);
155
+ give_up_file(NULL, path, fresh);
156
+ no_memory();
157
+ }
158
+ ref->opened = s;
159
+ return self;
160
+ }
161
+
162
+ /* Sink.open(path, capacity): a stream writing to `path`, with a ring of `capacity` units of 512
163
+ * bytes. */
164
+ static VALUE sink_open(int argc, VALUE *argv, VALUE klass) {
165
+ VALUE path, capacity;
166
+ rb_scan_args(argc, argv, "11", &path, &capacity);
167
+ return open_stream(klass, StringValueCStr(path), capacity);
168
+ }
169
+
170
+ /* Sink.hold(capacity): a stream that writes nothing until it is told to keep what it holds. */
171
+ static VALUE sink_hold(int argc, VALUE *argv, VALUE klass) {
172
+ VALUE capacity;
173
+ rb_scan_args(argc, argv, "01", &capacity);
174
+ return open_stream(klass, NULL, capacity);
175
+ }
176
+
177
+ /* The stream's number, which the wrapper names when it asks Ruby for a class's id. */
178
+ static VALUE sink_handle(VALUE self) {
179
+ return LONG2NUM(ref_of(self)->handle);
180
+ }
181
+
182
+ /* define(id, string): a dictionary entry. The bytes are copied, and the writer frees the copy. */
183
+ static VALUE sink_define(VALUE self, VALUE id, VALUE str) {
184
+ sink *s = ra_sink_of(self);
185
+
186
+ slot sl;
187
+ memset(&sl, 0, sizeof(sl));
188
+ sl.kind = SLOT_DICT;
189
+ sl.dict_id = NUM2LL(id);
190
+
191
+ if (NIL_P(str)) {
192
+ sl.dict_str.ptr = NULL;
193
+ sl.dict_str.len = -1; /* interned nil -> JSON null on the way out */
194
+ } else {
195
+ StringValue(str);
196
+ long len = RSTRING_LEN(str);
197
+ char *copy = ra_malloc(len ? len : 1);
198
+ if (!copy) { /* OOM copying the string: drop it rather than raise into the traced program */
199
+ pthread_mutex_lock(&s->mu);
200
+ __atomic_fetch_add(&s->dropped, 1, __ATOMIC_RELAXED);
201
+ pthread_mutex_unlock(&s->mu);
202
+ return Qnil;
203
+ }
204
+ memcpy(copy, RSTRING_PTR(str), len);
205
+ sl.dict_str.ptr = copy;
206
+ sl.dict_str.len = len;
207
+ }
208
+
209
+ ra_push_slot(s, &sl);
210
+ return Qnil;
211
+ }
212
+
213
+ /* Each kind of row arrives as the array of integers it is written as, strings already interned. */
214
+
215
+ static long at_least(VALUE fields, long count, const char *what) {
216
+ long len = RARRAY_LEN(fields);
217
+ if (len < count) rb_raise(rb_eArgError, "runtime_analysis native %s must have at least %ld fields", what, count);
218
+ return len;
219
+ }
220
+
221
+ static int capped(long entries) {
222
+ return entries > MAX_ARGS ? MAX_ARGS : (int)entries;
223
+ }
224
+
225
+ /* An unwind, an entry or an answered row: `count` integers and nothing after them. */
226
+ static void plain_row(slot *sl, VALUE fields, int count, const char *what) {
227
+ at_least(fields, count, what);
228
+ for (int i = 0; i < count; i++) sl->rec[i] = NUM2LL(RARRAY_AREF(fields, i));
229
+ }
230
+
231
+ /* A path: PATH_FIELDS integers, of which whether the callee is a class method may also be given as
232
+ * Ruby's own true or false. */
233
+ static void path_row(slot *sl, VALUE fields) {
234
+ at_least(fields, PATH_FIELDS, "path");
235
+ for (int i = 0; i < PATH_FIELDS; i++) {
236
+ VALUE field = RARRAY_AREF(fields, i);
237
+ sl->rec[i] = (i == F_PATH_CLASS_METHOD && !FIXNUM_P(field)) ? RTEST(field) : NUM2LL(field);
238
+ }
239
+ }
240
+
241
+ /* A call: the CALL_FIELDS integers, then nothing, or [2, argc, class...], or
242
+ * [3, argc, receiver id, (class, object id)...]. Any other tier is caught by the writer's guard. */
243
+ static void call_row(slot *sl, VALUE fields) {
244
+ long len = at_least(fields, CALL_FIELDS, "call");
245
+ for (int i = 0; i < CALL_FIELDS; i++) sl->rec[i] = NUM2LL(RARRAY_AREF(fields, i));
246
+ if (len == CALL_FIELDS) return;
247
+
248
+ const long tail = CALL_FIELDS + 2;
249
+ at_least(fields, tail, "call with arguments");
250
+ sl->tier = NUM2LL(RARRAY_AREF(fields, CALL_FIELDS));
251
+ sl->argc = NUM2LL(RARRAY_AREF(fields, CALL_FIELDS + 1));
252
+ if (sl->tier == 2) {
253
+ sl->argn = capped(len - tail);
254
+ for (int j = 0; j < sl->argn; j++) sl->args[j] = NUM2LL(RARRAY_AREF(fields, tail + j));
255
+ } else if (sl->tier == 3) {
256
+ at_least(fields, tail + 1, "tier 3 call");
257
+ sl->recv_oid = NUM2LL(RARRAY_AREF(fields, tail));
258
+ sl->argn = capped((len - tail - 1) / 2);
259
+ for (int j = 0; j < sl->argn; j++) {
260
+ sl->args[j] = NUM2LL(RARRAY_AREF(fields, tail + 1 + 2 * j));
261
+ sl->arg_oids[j] = NUM2LL(RARRAY_AREF(fields, tail + 2 + 2 * j));
262
+ }
263
+ }
264
+ }
265
+
266
+ /* A return: [tree, step, class, object id], and for an Array its true size and the class and id of
267
+ * each element stored. A tier of 1 tells the writer the elements are there. */
268
+ static void return_row(slot *sl, VALUE fields) {
269
+ long len = at_least(fields, 4, "return");
270
+ for (int i = 0; i < 4; i++) sl->rec[i] = NUM2LL(RARRAY_AREF(fields, i));
271
+ if (len == 4) return;
272
+
273
+ sl->tier = 1;
274
+ sl->argc = NUM2LL(RARRAY_AREF(fields, 4));
275
+ sl->argn = capped((len - 5) / 2);
276
+ for (int j = 0; j < sl->argn; j++) {
277
+ sl->args[j] = NUM2LL(RARRAY_AREF(fields, 5 + 2 * j));
278
+ sl->arg_oids[j] = NUM2LL(RARRAY_AREF(fields, 6 + 2 * j));
279
+ }
280
+ }
281
+
282
+ /* Keywords: [tree, step, tier, count], then each keyword's name and class and, at tier 3, its
283
+ * value's object id. */
284
+ #define KW_HEADER 4
285
+ static void keywords_row(slot *sl, VALUE fields) {
286
+ long len = at_least(fields, KW_HEADER, "kwargs row");
287
+ sl->rec[0] = NUM2LL(RARRAY_AREF(fields, 0));
288
+ sl->rec[1] = NUM2LL(RARRAY_AREF(fields, 1));
289
+ sl->tier = NUM2LL(RARRAY_AREF(fields, 2));
290
+ sl->argc = NUM2LL(RARRAY_AREF(fields, 3));
291
+
292
+ int width = sl->tier == 3 ? 3 : 2;
293
+ sl->argn = capped((len - KW_HEADER) / width);
294
+ for (int j = 0; j < sl->argn; j++) {
295
+ long base = KW_HEADER + (long)j * width;
296
+ sl->kw_names[j] = NUM2LL(RARRAY_AREF(fields, base));
297
+ sl->args[j] = NUM2LL(RARRAY_AREF(fields, base + 1));
298
+ if (width == 3) sl->arg_oids[j] = NUM2LL(RARRAY_AREF(fields, base + 2));
299
+ }
300
+ }
301
+
302
+ /* The fiber's state is told the call row Ruby has just handed over, so that the wrapper, when Ruby
303
+ * returns to it, can remember the row for the next call like this one. */
304
+ static void note_call(VALUE self, const slot *sl) {
305
+ ra_state *st = ra_state_current();
306
+ if (!st) return;
307
+ st->last_handle = ref_of(self)->handle;
308
+ st->last_step = sl->rec[1];
309
+ st->last_path_id = sl->rec[2];
310
+ st->last_file_id = sl->rec[3];
311
+ }
312
+
313
+ static uint8_t fill_row(slot *sl, int op, VALUE fields) {
314
+ switch (op) {
315
+ case OP_CALL: call_row(sl, fields); return SLOT_CALL;
316
+ case OP_RETURN: return_row(sl, fields); return SLOT_RETURN;
317
+ case OP_KWARGS: keywords_row(sl, fields); return SLOT_KWARGS;
318
+ case OP_PATH: path_row(sl, fields); return SLOT_PATH;
319
+ case OP_UNWIND: plain_row(sl, fields, 7, "unwind"); return SLOT_UNWIND;
320
+ case OP_ENTRY: plain_row(sl, fields, 2, "entry"); return SLOT_ENTRY;
321
+ case OP_ANSWERED: plain_row(sl, fields, 3, "answered row"); return SLOT_ANSWERED;
322
+ case OP_INNER_ENTRY: plain_row(sl, fields, 2, "inner entry"); return SLOT_INNER_ENTRY;
323
+ case OP_VALUE: plain_row(sl, fields, 6, "value row"); return SLOT_VALUE;
324
+ default: rb_raise(rb_eArgError, "runtime_analysis native: no row of kind %d", op);
325
+ }
326
+ }
327
+
328
+ /* write(kind, fields): one row, of the kind its opcode names. */
329
+ static VALUE sink_write(VALUE self, VALUE kind, VALUE fields) {
330
+ sink *s = ra_sink_of(self);
331
+ Check_Type(fields, T_ARRAY);
332
+
333
+ slot sl;
334
+ memset(&sl, 0, sizeof(sl));
335
+ sl.kind = fill_row(&sl, NUM2INT(kind), fields);
336
+ if (sl.kind == SLOT_CALL) note_call(self, &sl);
337
+ ra_push_slot(s, &sl);
338
+ return Qnil;
339
+ }
340
+
341
+ /* roll(path): the rows after this one go to a new file. */
342
+ static VALUE sink_roll(VALUE self, VALUE path) {
343
+ sink *s = ra_sink_of(self);
344
+ if (s->hold) return Qnil; /* it has no file to roll from */
345
+
346
+ StringValue(path);
347
+ long len = RSTRING_LEN(path);
348
+ char *copy = ra_malloc(len + 1);
349
+ if (!copy) { /* OOM copying the path: drop the roll rather than raise into the traced program */
350
+ pthread_mutex_lock(&s->mu);
351
+ __atomic_fetch_add(&s->dropped, 1, __ATOMIC_RELAXED);
352
+ pthread_mutex_unlock(&s->mu);
353
+ return Qnil;
354
+ }
355
+ memcpy(copy, RSTRING_PTR(path), len);
356
+ copy[len] = '\0';
357
+
358
+ slot sl;
359
+ memset(&sl, 0, sizeof(sl));
360
+ sl.kind = SLOT_ROLL;
361
+ sl.dict_str.ptr = copy;
362
+ sl.dict_str.len = len;
363
+
364
+ ra_fast_forget(s); /* the next segment spells its paths and strings out again */
365
+ ra_push_slot(s, &sl);
366
+ return Qnil;
367
+ }
368
+
369
+ /* Read without the ring's lock. Ruby compares it with the size to rotate at, and a value that is a
370
+ * few rows old does that as well. */
371
+ static VALUE sink_bytes_written(VALUE self) {
372
+ return LL2NUM(ra_sink_of(self)->bytes_written);
373
+ }
374
+
375
+ /* The rows this sink has dropped, for want of room or after it was disabled. */
376
+ static VALUE sink_dropped(VALUE self) {
377
+ return LL2NUM(__atomic_load_n(&ra_sink_of(self)->dropped, __ATOMIC_RELAXED));
378
+ }
379
+
380
+ /* True once an invariant guard has disabled this sink. Ruby asks at the start of each call tree and
381
+ * opens a fresh sink when it has. */
382
+ static VALUE sink_disabled_p(VALUE self) {
383
+ return ra_sink_of(self)->disabled ? Qtrue : Qfalse;
384
+ }
385
+
386
+ static void teardown(sink *s) {
387
+ pthread_mutex_lock(&s->mu);
388
+ s->running = 0;
389
+ pthread_cond_broadcast(&s->not_empty);
390
+ pthread_cond_broadcast(&s->gather);
391
+ pthread_mutex_unlock(&s->mu);
392
+
393
+ pthread_join(s->consumer, NULL);
394
+
395
+ /* The sink's drops go into the process total before it is freed. Ruby holds the GVL here, as it
396
+ * does when it reads the total. */
397
+ g_closed_dropped += __atomic_load_n(&s->dropped, __ATOMIC_RELAXED);
398
+
399
+ if (s->file) {
400
+ fflush(s->file);
401
+ fclose(s->file);
402
+ }
403
+ ra_hold_free(s);
404
+ free(s->words);
405
+ free(s->fast);
406
+ free(s->names);
407
+ free(s);
408
+ ra_epoch++; /* a fiber may still hold this sink's handle */
409
+ }
410
+
411
+ /* close: the rows still on the ring are written and the file is closed. False for a sink that was
412
+ * closed already. */
413
+ static VALUE sink_close(VALUE self) {
414
+ sink_ref *ref = ref_of(self);
415
+ sink *s = open_sink(ref);
416
+ if (!s) return Qfalse;
417
+
418
+ ra_sinks[ref->handle] = NULL;
419
+ ref->opened = NULL;
420
+ teardown(s);
421
+ return Qtrue;
422
+ }
423
+
424
+ /* The counters for the whole process. Each sink counts its own drops, so a recording thread only
425
+ * ever writes to its own counter, and the sum over the sinks is made here, when Ruby asks. Ruby
426
+ * holds the GVL as it asks, which is what keeps the table of sinks still while it is read. Drops
427
+ * from sinks already closed are in g_closed_dropped. */
428
+ static VALUE native_stats(VALUE self) {
429
+ int64_t dropped = g_closed_dropped;
430
+ long streams = 0, disabled = 0;
431
+ for (size_t i = 0; i < ra_sinks_cap; i++) {
432
+ sink *s = ra_sinks[i];
433
+ if (!s) continue;
434
+ streams++;
435
+ dropped += __atomic_load_n(&s->dropped, __ATOMIC_RELAXED);
436
+ if (s->disabled) disabled++;
437
+ }
438
+
439
+ VALUE h = rb_hash_new();
440
+ rb_hash_aset(h, ID2SYM(rb_intern("dropped")), LL2NUM(dropped));
441
+ rb_hash_aset(h, ID2SYM(rb_intern("streams")), LONG2NUM(streams));
442
+ rb_hash_aset(h, ID2SYM(rb_intern("disabled")), LONG2NUM(disabled));
443
+ rb_hash_aset(h, ID2SYM(rb_intern("write_errors")), LL2NUM(__atomic_load_n(&ra_write_errors, __ATOMIC_RELAXED)));
444
+ rb_hash_aset(h, ID2SYM(rb_intern("fast")), LL2NUM(ra_fast_rows));
445
+ rb_hash_aset(h, ID2SYM(rb_intern("slow")), LL2NUM(ra_slow_calls));
446
+ return h;
447
+ }
448
+
449
+ /* Called in the child after a fork. No writer thread came across, and a ring's lock may have been
450
+ * held when the fork happened, so the child leaves every sink it inherited alone and opens its own.
451
+ * It does not flush or close an inherited FILE: the buffer holds bytes the parent has yet to write,
452
+ * and flushing the child's copy would write them twice. The inherited sinks' memory is not freed
453
+ * in the child. */
454
+ static VALUE native_reset_after_fork(VALUE self) {
455
+ for (size_t i = 0; i < ra_sinks_cap; i++) ra_sinks[i] = NULL;
456
+ ra_epoch++;
457
+ return Qnil;
458
+ }
459
+
460
+ /* In a forked child the sinks are the parent's, with no thread draining them. The Ruby side opens
461
+ * new ones on its next call, and until then no fiber may write to an old one the fast way. */
462
+ static void forked_child(void) {
463
+ ra_epoch++;
464
+ }
465
+
466
+ void Init_ra_sink(VALUE mNative) {
467
+ pthread_atfork(NULL, NULL, forked_child);
468
+ rb_define_const(mNative, "FORMAT_VERSION", INT2FIX(RA_FORMAT_VERSION));
469
+ rb_define_singleton_method(mNative, "stats", native_stats, 0);
470
+ rb_define_singleton_method(mNative, "reset_after_fork", native_reset_after_fork, 0);
471
+
472
+ VALUE cSink = rb_define_class_under(mNative, "Sink", rb_cObject);
473
+ rb_undef_alloc_func(cSink);
474
+ rb_define_singleton_method(cSink, "open", sink_open, -1);
475
+ rb_define_singleton_method(cSink, "hold", sink_hold, -1);
476
+ Init_ra_hold(cSink);
477
+ rb_define_method(cSink, "handle", sink_handle, 0);
478
+ rb_define_method(cSink, "define", sink_define, 2);
479
+ rb_define_method(cSink, "write", sink_write, 2);
480
+ rb_define_method(cSink, "roll", sink_roll, 1);
481
+ rb_define_method(cSink, "bytes_written", sink_bytes_written, 0);
482
+ rb_define_method(cSink, "dropped", sink_dropped, 0);
483
+ rb_define_method(cSink, "disabled?", sink_disabled_p, 0);
484
+ rb_define_method(cSink, "close", sink_close, 0);
485
+ }