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,445 @@
1
+ #include "sink.h"
2
+ #include <errno.h>
3
+ #include <poll.h>
4
+ #include <sched.h>
5
+ #include <stddef.h>
6
+ #include <stdlib.h>
7
+ #include <string.h>
8
+ #include <time.h>
9
+ #include <unistd.h>
10
+
11
+ /* The ring between a recording thread and its writer. The thread packs each row into the words it
12
+ * uses and pushes it without ever waiting, and the writer thread takes rows a batch at a time,
13
+ * turns them into text and writes them. */
14
+
15
+ /* How many of a slot's fixed fields each kind of row uses. */
16
+ static const int REC_USED[] = { 0, CALL_FIELDS, 0, 4, 2, 7, 2, PATH_FIELDS, 3, 2, 6, 2 };
17
+
18
+ static int named_values(int kind) { return kind == SLOT_KWARGS || kind == SLOT_IVARS; }
19
+ static int has_string(int kind) { return kind == SLOT_DICT || kind == SLOT_ROLL; }
20
+ static int has_tail(const slot *sl) {
21
+ return sl->kind == SLOT_RETURN || named_values(sl->kind) || (sl->kind == SLOT_CALL && sl->tier != 0);
22
+ }
23
+ static int has_oids(const slot *sl) { return !(sl->kind == SLOT_CALL && sl->tier == 2); }
24
+
25
+ /* Writes the row for a slot into `row`, which has room for RA_ROW_MAX words. Returns its length. */
26
+ static size_t pack_row(const slot *sl, uint64_t *row) {
27
+ size_t n = 1;
28
+ uint64_t header = sl->kind;
29
+ for (int i = 0; i < REC_USED[sl->kind]; i++) row[n++] = (uint64_t)sl->rec[i];
30
+ if (has_string(sl->kind)) {
31
+ row[n++] = (uint64_t)sl->dict_id;
32
+ row[n++] = (uint64_t)(uintptr_t)sl->dict_str.ptr;
33
+ row[n++] = (uint64_t)sl->dict_str.len;
34
+ }
35
+ if (has_tail(sl)) {
36
+ header |= RA_ROW_TAIL;
37
+ row[n++] = (uint64_t)sl->tier;
38
+ row[n++] = (uint64_t)sl->argc;
39
+ row[n++] = (uint64_t)sl->argn;
40
+ row[n++] = (uint64_t)sl->recv_oid;
41
+ for (int i = 0; i < sl->argn; i++) row[n++] = (uint64_t)sl->args[i];
42
+ if (has_oids(sl)) for (int i = 0; i < sl->argn; i++) row[n++] = (uint64_t)sl->arg_oids[i];
43
+ if (named_values(sl->kind)) for (int i = 0; i < sl->argn; i++) row[n++] = (uint64_t)sl->kw_names[i];
44
+ }
45
+ row[0] = header | ((uint64_t)n << RA_ROW_SHIFT);
46
+ return n;
47
+ }
48
+
49
+ /* The slot a row was packed from, with the fields the row's kind uses filled in. Only the kinds
50
+ * that are not formatted from their words come here, and none of those has a tail. */
51
+ static void unpack_row(const uint64_t *row, slot *sl) {
52
+ size_t n = 1;
53
+ memset(sl, 0, offsetof(slot, args));
54
+ sl->kind = (uint8_t)(row[0] & 0xff);
55
+ for (int i = 0; i < REC_USED[sl->kind]; i++) sl->rec[i] = (int64_t)row[n++];
56
+ if (has_string(sl->kind)) {
57
+ sl->dict_id = (int64_t)row[n++];
58
+ sl->dict_str.ptr = (char *)(uintptr_t)row[n++];
59
+ sl->dict_str.len = (long)row[n++];
60
+ }
61
+ }
62
+
63
+ /* A row may run off the end of the ring and carry on from its start. */
64
+ static void ring_write(sink *s, const uint64_t *row, size_t n) {
65
+ size_t first = s->cap - s->head < n ? s->cap - s->head : n;
66
+ memcpy(s->words + s->head, row, first * sizeof(uint64_t));
67
+ memcpy(s->words, row + first, (n - first) * sizeof(uint64_t));
68
+ }
69
+
70
+ void ra_ring_read(const sink *s, size_t at, uint64_t *row, size_t n) {
71
+ size_t first = s->cap - at < n ? s->cap - at : n;
72
+ memcpy(row, s->words + at, first * sizeof(uint64_t));
73
+ memcpy(row + first, s->words, (n - first) * sizeof(uint64_t));
74
+ }
75
+
76
+ /* The rows the drain thread takes for each time it takes the lock. The recorder takes the lock for
77
+ * every row, and the drain thread waits its turn behind it, so a small batch spends its time
78
+ * waiting: at 256 rows a tier 3 call and its return took 240 ns to write, and at 8,192 they take 120. */
79
+ #define RA_DRAIN_BATCH 8192
80
+ #define RA_GATHER_NS 2000000L /* 2ms: long enough to gather a burst, short enough to read as now */
81
+
82
+ /* The gather is a timed wait on a condition, so that a producer filling the ring can end it. It is
83
+ * measured on the monotonic clock, which a step in wall-clock time cannot stretch. macOS has no way
84
+ * to put a condition on that clock and has a relative wait instead. Called with the lock held. */
85
+ void ra_gather_init(pthread_cond_t *gather) {
86
+ #ifdef __APPLE__
87
+ pthread_cond_init(gather, NULL);
88
+ #else
89
+ pthread_condattr_t monotonic;
90
+ pthread_condattr_init(&monotonic);
91
+ pthread_condattr_setclock(&monotonic, CLOCK_MONOTONIC);
92
+ pthread_cond_init(gather, &monotonic);
93
+ pthread_condattr_destroy(&monotonic);
94
+ #endif
95
+ }
96
+
97
+ static void gather_wait(sink *s) {
98
+ #ifdef __APPLE__
99
+ struct timespec wait = { 0, RA_GATHER_NS };
100
+ pthread_cond_timedwait_relative_np(&s->gather, &s->mu, &wait);
101
+ #else
102
+ struct timespec until;
103
+ clock_gettime(CLOCK_MONOTONIC, &until);
104
+ until.tv_nsec += RA_GATHER_NS;
105
+ if (until.tv_nsec >= 1000000000L) { until.tv_sec++; until.tv_nsec -= 1000000000L; }
106
+ pthread_cond_timedwait(&s->gather, &s->mu, &until);
107
+ #endif
108
+ }
109
+
110
+ /* The text of a batch's calls, returns and keywords gathers here and is written in one go: when the
111
+ * batch ends, when the buffer is nearly full, and before any other kind of row, so the file keeps
112
+ * the rows in order. */
113
+ #define RA_OUT_BYTES (64 * 1024)
114
+
115
+ static size_t flush_rows(sink *s, const char *out, size_t fill) {
116
+ if (fill == 0 || !s->file) return 0;
117
+ s->bytes_written += (int64_t)fwrite(out, 1, fill, s->file);
118
+ ra_note_io_error(s);
119
+ return 0;
120
+ }
121
+
122
+ /* The rows of a batch, as far as the first that makes no sense. `fill` is left at the text that is
123
+ * ready in `out` and not yet written. */
124
+ static size_t drain_batch(sink *s, size_t at, size_t batch, char *out, size_t *fill) {
125
+ size_t taken = 0;
126
+ uint64_t row[RA_ROW_MAX];
127
+ slot sl;
128
+ for (size_t i = 0; i < batch; i++) {
129
+ size_t start = (at + taken) % s->cap;
130
+ size_t n = (size_t)(s->words[start] >> RA_ROW_SHIFT);
131
+ RA_GUARD(s, n >= 1 && n <= RA_ROW_MAX && (s->words[start] & 0xff) <= SLOT_LAST, 0);
132
+ ra_ring_read(s, start, row, n);
133
+ taken += n;
134
+
135
+ if (ra_formatted((int)(row[0] & 0xff))) {
136
+ if (*fill + RA_ROW_TEXT_MAX > RA_OUT_BYTES) *fill = flush_rows(s, out, *fill);
137
+ char *end = ra_format_row(row, out + *fill);
138
+ RA_GUARD(s, end != NULL, 0);
139
+ *fill = (size_t)(end - out);
140
+ continue;
141
+ }
142
+ *fill = flush_rows(s, out, *fill);
143
+ unpack_row(row, &sl);
144
+ ra_consume_slot(s, &sl); /* may set s->disabled via a guard */
145
+ }
146
+ return taken;
147
+ }
148
+
149
+ /* Writes out `batch` rows starting at word `at`, with the lock released. The producer only writes
150
+ * into the free words after head, and it cannot reach these while they are still counted as used,
151
+ * so they are ours until the count gives them back. Returns the words the rows took, or 0 when a
152
+ * row makes no sense and the sink has been disabled: the rows that came before that one in the
153
+ * batch are sound, and are written all the same. */
154
+ size_t ra_drain_rows(sink *s, size_t at, size_t batch, char *out) {
155
+ size_t fill = 0;
156
+ size_t taken = drain_batch(s, at, batch, out, &fill);
157
+ flush_rows(s, out, fill);
158
+ return taken;
159
+ }
160
+
161
+ void *ra_consumer_main(void *arg) {
162
+ sink *s = (sink *)arg;
163
+ char *out = ra_malloc(RA_OUT_BYTES);
164
+ if (!out) { /* no buffer to write calls from: disable the sink and leave the host running */
165
+ pthread_mutex_lock(&s->mu);
166
+ s->disabled = 1;
167
+ pthread_mutex_unlock(&s->mu);
168
+ return NULL;
169
+ }
170
+ if (s->hold) {
171
+ ra_hold_main(s, out);
172
+ free(out);
173
+ return NULL;
174
+ }
175
+ for (;;) {
176
+ pthread_mutex_lock(&s->mu);
177
+ while (s->rows == 0 && s->running && !s->disabled) {
178
+ pthread_cond_wait(&s->not_empty, &s->mu);
179
+ }
180
+ if (s->disabled || (s->rows == 0 && !s->running)) {
181
+ pthread_mutex_unlock(&s->mu);
182
+ break;
183
+ }
184
+
185
+ /* A recorder that makes a row every few microseconds would otherwise find us caught up after each
186
+ * one: woken, one row written, flushed, asleep again, which is a trip into the kernel on both
187
+ * threads for every row. So with only a little waiting, give the burst a moment to gather. The
188
+ * ring holds far more than arrives in that time, and at close there is nothing to wait for. */
189
+ if (s->running && s->rows < RA_DRAIN_BATCH) {
190
+ s->gathering = 1;
191
+ gather_wait(s);
192
+ s->gathering = 0;
193
+ }
194
+
195
+ /* One lock per batch instead of one per row keeps the producer's trylock from colliding with us.
196
+ * The batch is capped so that a full ring gets room back before it has all been written. */
197
+ size_t at = s->tail;
198
+ size_t batch = s->rows < RA_DRAIN_BATCH ? s->rows : RA_DRAIN_BATCH;
199
+ long stall = ra_testing_stall_asked(s);
200
+ pthread_mutex_unlock(&s->mu);
201
+
202
+ ra_testing_stall(s, stall);
203
+ size_t taken = ra_drain_rows(s, at, batch, out);
204
+
205
+ pthread_mutex_lock(&s->mu);
206
+ s->tail = (at + taken) % s->cap;
207
+ s->used -= taken;
208
+ s->rows -= batch;
209
+ int caught_up = (s->rows == 0);
210
+ /* An empty ring starts again from its first word. A stream that keeps up then only ever
211
+ * touches the start of its ring, and the rest stays memory the system has not had to find. */
212
+ if (s->used == 0) s->head = s->tail = 0;
213
+ pthread_mutex_unlock(&s->mu);
214
+
215
+ /* Once the queue is drained, push buffered writes to disk so a long-running host's recent records
216
+ * are visible now, not only at buffer-full/rotate/close. Flushed after the unlock (never under
217
+ * s->mu, which the GVL-holding producer also takes) and only by this single consumer thread, so
218
+ * reading s->file/s->disabled here is race-free and cannot collide with a roll. During a burst
219
+ * rows stay above 0, so this costs nothing until the app pauses. */
220
+ if (caught_up && s->file && !s->disabled) {
221
+ fflush(s->file);
222
+ ra_note_io_error(s);
223
+ }
224
+ }
225
+ if (s->file) fflush(s->file);
226
+ free(out);
227
+ return NULL;
228
+ }
229
+
230
+
231
+ /* The producer holds the GVL, and a producer that waited would stop every Ruby thread. So a row
232
+ * that cannot go onto the ring is dropped and counted. A dropped call is a gap in its tree's steps,
233
+ * which a reader can see. A dropped dictionary entry leaves later rows naming an id that reads as
234
+ * an empty label. Ruby is given the count. A disabled sink drops everything the same way.
235
+ *
236
+ * For the same reason the producer does not wait for the ring's lock. It tries the lock, yields
237
+ * and tries again for RA_LOCK_BUDGET_NS, and then drops the row. The writer holds the lock twice
238
+ * for each batch and for a few instructions each time, so the budget is only reached when the
239
+ * writer has been descheduled while holding it. The time is read from the monotonic clock, which a
240
+ * step in wall-clock time cannot stretch. macOS has no pthread_mutex_timedlock, hence the loop. */
241
+ #define RA_LOCK_BUDGET_NS 50000L /* 50 microseconds */
242
+
243
+ #ifdef RA_TESTING
244
+ /* For specs that compare two recordings row for row: the producer waits for the lock, so that
245
+ * which rows are written does not depend on when the machine ran the writer. */
246
+ static int ra_patient;
247
+ #endif
248
+
249
+ static int ra_timedlock(pthread_mutex_t *m) {
250
+ if (pthread_mutex_trylock(m) == 0) return 0; /* fast path: uncontended */
251
+ #ifdef RA_TESTING
252
+ if (ra_patient) return pthread_mutex_lock(m);
253
+ #endif
254
+
255
+ struct timespec now, deadline;
256
+ clock_gettime(CLOCK_MONOTONIC, &deadline);
257
+ deadline.tv_nsec += RA_LOCK_BUDGET_NS;
258
+ if (deadline.tv_nsec >= 1000000000L) { deadline.tv_sec++; deadline.tv_nsec -= 1000000000L; }
259
+
260
+ for (;;) {
261
+ sched_yield();
262
+ if (pthread_mutex_trylock(m) == 0) return 0;
263
+ clock_gettime(CLOCK_MONOTONIC, &now);
264
+ if (now.tv_sec > deadline.tv_sec ||
265
+ (now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec)) {
266
+ return ETIMEDOUT;
267
+ }
268
+ }
269
+ }
270
+
271
+ void ra_push_slot(sink *s, slot *sl) {
272
+ if (s->hold && ra_hold_defines(s, sl)) return;
273
+
274
+ uint64_t row[RA_ROW_MAX];
275
+ size_t n = pack_row(sl, row);
276
+
277
+ /* Counted whether or not the ring has room, so that the largest says what a ring would need. */
278
+ if (s->hold && (s->request_words += (int64_t)n) > s->largest_words) s->largest_words = s->request_words;
279
+
280
+ if (ra_timedlock(&s->mu) != 0) { /* couldn't take the ring within budget: drop, never block the GVL */
281
+ __atomic_fetch_add(&s->dropped, 1, __ATOMIC_RELAXED);
282
+ free(sl->dict_str.ptr);
283
+ return;
284
+ }
285
+ int full = s->cap - s->used < n && !ra_hold_room(s, n);
286
+ if (s->disabled || full) {
287
+ /* A held request's drops are counted with it, and reach the total only if it is kept. */
288
+ if (s->hold && !s->disabled) s->held_dropped++;
289
+ else __atomic_fetch_add(&s->dropped, 1, __ATOMIC_RELAXED);
290
+ pthread_mutex_unlock(&s->mu);
291
+ free(sl->dict_str.ptr); /* NULL for records; the owned copy for dict/roll, so a drop can't leak */
292
+ return;
293
+ }
294
+ /* The consumer sleeps only on an empty ring, so that is the only time it needs waking. One that
295
+ * holds sleeps until it is told to keep a request. */
296
+ int wake = !s->hold && s->rows == 0;
297
+ ring_write(s, row, n);
298
+ s->head = (s->head + n) % s->cap;
299
+ s->used += n;
300
+ s->rows++;
301
+ if (s->hold) {
302
+ s->held_words += n;
303
+ s->held_rows++;
304
+ }
305
+ if (wake) pthread_cond_signal(&s->not_empty);
306
+ /* Filling fast: end the consumer's wait. It is asked once, by clearing the flag, and the flag is
307
+ * only ever read and written under the lock, so the request cannot fall between its check and
308
+ * its wait. */
309
+ if (s->gathering && s->used >= s->cap / 2) {
310
+ s->gathering = 0;
311
+ pthread_cond_signal(&s->gather);
312
+ }
313
+ pthread_mutex_unlock(&s->mu);
314
+ }
315
+
316
+
317
+ /* ------------------------------------------------------------- test hooks */
318
+
319
+ #ifdef RA_TESTING
320
+ /* What a spec asked the consumer to wait before its next batch, asked once. Called with the lock
321
+ * held; the wait itself is made with the lock released. */
322
+ long ra_testing_stall_asked(sink *s) {
323
+ long ms = s->test_stall_ms;
324
+ s->test_stall_ms = 0;
325
+ return ms;
326
+ }
327
+
328
+ /* The wait ends when the stream is closed, so that a spec can ask for one longer than any machine
329
+ * takes over its rows and not pay for it. */
330
+ void ra_testing_stall(sink *s, long ms) {
331
+ struct timespec tick = { 0, 1000000L };
332
+ for (long waited = 0; waited < ms; waited++) {
333
+ pthread_mutex_lock(&s->mu);
334
+ int closing = !s->running;
335
+ pthread_mutex_unlock(&s->mu);
336
+ if (closing) return;
337
+ nanosleep(&tick, NULL);
338
+ }
339
+ }
340
+
341
+ /* For specs: takes a sink's lock on another thread and returns once that thread has it, so that a
342
+ * spec can show the producer drops a row and does not wait. The thread lets go when it is told to.
343
+ * It also lets go after `ms`, so that a producer which did wait fails its spec and does not freeze
344
+ * it: the producer holds the GVL, and nothing in Ruby could time it out. */
345
+ static pthread_mutex_t ra_hold_mu = PTHREAD_MUTEX_INITIALIZER;
346
+ static pthread_cond_t ra_hold_taken = PTHREAD_COND_INITIALIZER;
347
+ static pthread_t ra_hold_holder;
348
+ static int ra_hold_held, ra_hold_release;
349
+ static int ra_hold_started; /* a holder was started and has not been joined: only then is there one to join */
350
+
351
+ struct ra_hold_arg { sink *s; long ms; };
352
+ static void *ra_hold_thread(void *p) {
353
+ struct ra_hold_arg *a = (struct ra_hold_arg *)p;
354
+ struct timespec tick = { 0, 1000000L };
355
+ pthread_mutex_lock(&a->s->mu);
356
+ pthread_mutex_lock(&ra_hold_mu);
357
+ ra_hold_held = 1;
358
+ pthread_cond_signal(&ra_hold_taken);
359
+ for (long waited = 0; !ra_hold_release && waited < a->ms; waited++) {
360
+ pthread_mutex_unlock(&ra_hold_mu);
361
+ nanosleep(&tick, NULL);
362
+ pthread_mutex_lock(&ra_hold_mu);
363
+ }
364
+ ra_hold_held = 0;
365
+ pthread_mutex_unlock(&ra_hold_mu);
366
+ pthread_mutex_unlock(&a->s->mu);
367
+ free(a);
368
+ return NULL;
369
+ }
370
+
371
+ /* Has the consumer wait `ms`, or until the stream is closed, before it writes its next batch, with
372
+ * the ring's lock released, so a spec can fill the ring and count exactly what fits. */
373
+ static VALUE testing_stall_writer(VALUE self, VALUE stream, VALUE ms) {
374
+ sink *s = ra_sink_of(stream);
375
+ pthread_mutex_lock(&s->mu);
376
+ s->test_stall_ms = NUM2LONG(ms);
377
+ pthread_mutex_unlock(&s->mu);
378
+ return Qnil;
379
+ }
380
+
381
+ static VALUE testing_ring_head(VALUE self, VALUE stream) {
382
+ sink *s = ra_sink_of(stream);
383
+ pthread_mutex_lock(&s->mu);
384
+ size_t head = s->head;
385
+ pthread_mutex_unlock(&s->mu);
386
+ return SIZET2NUM(head);
387
+ }
388
+
389
+ static VALUE testing_hold_lock(VALUE self, VALUE stream, VALUE ms) {
390
+ sink *s = ra_sink_of(stream);
391
+ struct ra_hold_arg *a = ra_malloc(sizeof(*a));
392
+ if (!a) return Qnil;
393
+ a->s = s;
394
+ a->ms = NUM2LONG(ms);
395
+ ra_hold_release = 0;
396
+ if (ra_thread_create(&ra_hold_holder, NULL, ra_hold_thread, a) != 0) { free(a); return Qnil; }
397
+ ra_hold_started = 1;
398
+ pthread_mutex_lock(&ra_hold_mu);
399
+ while (!ra_hold_held) pthread_cond_wait(&ra_hold_taken, &ra_hold_mu);
400
+ pthread_mutex_unlock(&ra_hold_mu);
401
+ return Qnil;
402
+ }
403
+
404
+ static VALUE testing_lock_held(VALUE self) {
405
+ pthread_mutex_lock(&ra_hold_mu);
406
+ int held = ra_hold_held;
407
+ pthread_mutex_unlock(&ra_hold_mu);
408
+ return held ? Qtrue : Qfalse;
409
+ }
410
+
411
+ static VALUE testing_release_lock(VALUE self) {
412
+ if (!ra_hold_started) return Qnil;
413
+ ra_hold_started = 0;
414
+ pthread_mutex_lock(&ra_hold_mu);
415
+ ra_hold_release = 1;
416
+ pthread_mutex_unlock(&ra_hold_mu);
417
+ pthread_join(ra_hold_holder, NULL);
418
+ return Qnil;
419
+ }
420
+
421
+ static VALUE testing_set_patient(VALUE self, VALUE on) {
422
+ ra_patient = RTEST(on) ? 1 : 0;
423
+ return on;
424
+ }
425
+
426
+ /* Whether this build aborts on a broken invariant, where the one a program is recorded with
427
+ * disables the stream: the examples of the second cannot run on the first. */
428
+ static VALUE testing_aborts_on_invariant_p(VALUE self) {
429
+ #ifdef RA_ABORT_ON_INVARIANT
430
+ return Qtrue;
431
+ #else
432
+ return Qfalse;
433
+ #endif
434
+ }
435
+
436
+ void Init_ra_ring_testing(VALUE mTesting) {
437
+ rb_define_singleton_method(mTesting, "aborts_on_invariant?", testing_aborts_on_invariant_p, 0);
438
+ rb_define_singleton_method(mTesting, "patient=", testing_set_patient, 1);
439
+ rb_define_singleton_method(mTesting, "hold_lock", testing_hold_lock, 2);
440
+ rb_define_singleton_method(mTesting, "lock_held?", testing_lock_held, 0);
441
+ rb_define_singleton_method(mTesting, "release_lock", testing_release_lock, 0);
442
+ rb_define_singleton_method(mTesting, "stall_writer", testing_stall_writer, 2);
443
+ rb_define_singleton_method(mTesting, "ring_head", testing_ring_head, 1);
444
+ }
445
+ #endif