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,240 @@
1
+ #ifndef RA_SINK_H
2
+ #define RA_SINK_H
3
+
4
+ #include <ruby.h>
5
+ #include <pthread.h>
6
+ #include <stdint.h>
7
+ #include <stdio.h>
8
+ #include "names.h"
9
+ #include "os.h"
10
+
11
+ /*
12
+ * The recording sink of runtime_analysis.
13
+ *
14
+ * Ruby interns every string to a small integer and hands the sink a dictionary entry (an id and its
15
+ * bytes), a path definition, or a row of integers. The sink copies those into memory of its own
16
+ * while the caller still holds the GVL, puts them on a ring, and a writer thread takes them off,
17
+ * turns them into text and writes them.
18
+ *
19
+ * The writer thread never touches a Ruby VALUE. It reads only those copies, so the garbage
20
+ * collector cannot disturb it, compaction included. Every change here has to keep that true.
21
+ *
22
+ * There is one sink for each (pid, tid) recording stream, which Ruby opens and holds as a
23
+ * Native::Sink. Inside the extension a sink is known by its handle, its place in the table of
24
+ * sinks, which is what a fiber keeps of the sink it writes to. A sink has its own ring, writer
25
+ * thread and file, so streams do not affect each other. Starting a new file is a row on the ring
26
+ * like any other, which puts the boundary between the files exactly where the roll was asked for,
27
+ * though the call returns before the file is swapped.
28
+ *
29
+ * The files: sink.c is the streams and what Ruby calls on them, ring.c the ring between a
30
+ * thread and its writer, rows.c a row as text, hold.c a stream that writes only the requests it
31
+ * is told to keep, fast.c the calls the wrapper records by itself, and wrapper.c the wrapper in
32
+ * front of each method.
33
+ */
34
+
35
+ struct fast_entry;
36
+ typedef struct ra_sink sink;
37
+ void ra_report_once(sink *s, const char *msg);
38
+
39
+ /* Tier 2 adds the class of each positional argument. Tier 3 adds the receiver's object id and each
40
+ * argument's, which a reader joins on to find what was sent to a value. A row is built on the stack
41
+ * with no allocation, so it stores at most MAX_ARGS of each and writes the true count beside them.
42
+ * A method with more arguments than that has the rest left out, and a reader can see that it was. */
43
+ #define MAX_ARGS 16
44
+
45
+ /* A path definition, one per route from a tree's root to a call, written once per segment:
46
+ * [0]=path_id [1]=parent_path_id (0 at a root)
47
+ * [2]=klass_id [3]=class_method [4]=name_id [5]=file_id [6]=line (the callee)
48
+ * [7]=call_file_id [8]=call_line (where the route was first recorded)
49
+ * A call row then names its callee and every ancestor with one path id:
50
+ * [0]=tree (the call tree's number within this process) [1]=step
51
+ * [2]=path_id [3]=call_file_id [4]=call_line
52
+ */
53
+ #define PATH_FIELDS 9
54
+ #define CALL_FIELDS 5
55
+ #define REC_FIELDS PATH_FIELDS /* the widest fixed part any slot holds */
56
+ #define F_PATH_CLASS_METHOD 3
57
+
58
+ /* A check on something that should be impossible: a ring index out of range, a row kind that does
59
+ * not exist. Each guard comes before the access it protects, so a failure means the code was about
60
+ * to step outside its own buffers and has not yet. In a normal build the sink is disabled and the
61
+ * host process carries on, and Ruby opens a fresh sink at the next call tree. Sanitizer builds
62
+ * define RA_ABORT_ON_INVARIANT and abort, so that a bug fails in CI. */
63
+ #ifdef RA_ABORT_ON_INVARIANT
64
+ # define RA_GUARD_FAIL(s, retval) abort()
65
+ #else
66
+ # define RA_GUARD_FAIL(s, retval) do { (s)->disabled = 1; return retval; } while (0)
67
+ #endif
68
+
69
+ #define RA_GUARD(s, cond, retval) \
70
+ do { \
71
+ if (!(cond)) { \
72
+ char _ra_msg[192]; \
73
+ snprintf(_ra_msg, sizeof(_ra_msg), \
74
+ "runtime_analysis native: invariant failed: %s (%s:%d)\n", \
75
+ #cond, __FILE__, __LINE__); \
76
+ ra_report_once((s), _ra_msg); \
77
+ RA_GUARD_FAIL(s, retval); \
78
+ } \
79
+ } while (0)
80
+
81
+ enum slot_kind {
82
+ SLOT_DICT = 0, SLOT_CALL = 1, SLOT_ROLL = 2,
83
+ SLOT_RETURN = 3, SLOT_KWARGS = 4, SLOT_UNWIND = 5, SLOT_ENTRY = 6, SLOT_PATH = 7, SLOT_ANSWERED = 8,
84
+ SLOT_INNER_ENTRY = 9, SLOT_VALUE = 10, SLOT_IVARS = 11
85
+ };
86
+ #define SLOT_LAST SLOT_IVARS
87
+
88
+ /* Row opcodes (distinct from the slot kinds above), which are what the readers go by: Index::Rows in
89
+ * the gem and the `ra` binary's. Opcode 1 was the call row before paths, which spelled out callee
90
+ * and container; readers still accept it. */
91
+ #define OP_RETURN 2
92
+ #define OP_KWARGS 3
93
+ #define OP_UNWIND 4
94
+ #define OP_ENTRY 5
95
+ #define OP_PATH 6
96
+ #define OP_CALL 7
97
+ #define OP_ANSWERED 8
98
+ #define OP_FORMAT 9
99
+ #define OP_HELD 10
100
+ #define OP_INNER_ENTRY 11
101
+ #define OP_VALUE 12
102
+ #define VALUE_KEYWORD 2 /* the role of a keyword in a value row, whose key is then a string id */
103
+ #define OP_IVARS 13
104
+
105
+ /* The format the files are written in, which a reader checks before it reads a segment. It goes up
106
+ * only when a change would make an older reader misread a file. Version 1 is the format as it was
107
+ * when the mark was added, so a segment with no mark reads as version 1. */
108
+ #define RA_FORMAT_VERSION 1
109
+
110
+ typedef struct {
111
+ char *ptr;
112
+ long len;
113
+ } dstr;
114
+
115
+ typedef struct {
116
+ uint8_t kind;
117
+ int64_t dict_id;
118
+ dstr dict_str; /* owned; the consumer frees it after writing the entry, or (SLOT_ROLL) opening the path */
119
+ int64_t rec[REC_FIELDS];
120
+ int64_t tier; /* 0 = no tail, 2 (classes), or 3 (+ object ids); also the KWARGS tier */
121
+ int64_t argc; /* true positional (or keyword) count; may exceed argn if truncated */
122
+ int argn; /* entries actually stored (<= MAX_ARGS) */
123
+ int64_t args[MAX_ARGS]; /* interned class ids (positional args, or keyword-value classes) */
124
+ int64_t recv_oid; /* tier 3: receiver object id (the join key) */
125
+ int64_t arg_oids[MAX_ARGS];/* tier 3: object ids, paired 1:1 with args[] */
126
+ int64_t kw_names[MAX_ARGS];/* KWARGS: interned keyword name ids, paired 1:1 with args[] */
127
+ } slot;
128
+
129
+ /* A row in the ring: a header word, then only the words the row uses. The header holds the kind in
130
+ * its low byte, whether an argument tail follows in the next bit, and the row's length in words
131
+ * above that. A call with no arguments is 6 words, where a slot is 65. */
132
+ #define RA_ROW_MAX 64 /* the widest row is a keywords row with MAX_ARGS: 55 words */
133
+ #define RA_ROW_TAIL 0x100
134
+ #define RA_ROW_SHIFT 16
135
+ #define RA_WORDS_PER_UNIT 64 /* capacity is asked for in units of 512 bytes */
136
+
137
+ /* A stream that holds (hold.c) keeps its definitions off the ring, since the rows that name them
138
+ * may be written long after or never. Each kind is a list of blocks of records of one width, added
139
+ * to at the end and never moved, so the writer reads the records that were there when it looked
140
+ * while the recorder adds more. */
141
+ struct ra_def_block;
142
+ typedef struct {
143
+ struct ra_def_block *first, *last;
144
+ size_t width; /* words in a record */
145
+ int64_t top; /* the largest id defined, -1 before any */
146
+ } ra_defs;
147
+
148
+ struct ra_keep;
149
+ #define RA_PINNED 8 /* the entry rows remembered after the ring dropped them, of each kind */
150
+ #define RA_PIN_KINDS 2 /* entry rows, then inner entry rows */
151
+
152
+ struct ra_sink {
153
+ uint64_t *words; /* the ring: rows end to end */
154
+ size_t cap; /* in words */
155
+ size_t head, tail, used; /* in words */
156
+ size_t rows;
157
+
158
+ pthread_mutex_t mu;
159
+ pthread_cond_t not_empty;
160
+ pthread_cond_t gather; /* the consumer's short wait for a burst; the producer ends it early */
161
+
162
+ pthread_t consumer;
163
+ int consumer_started;
164
+ int running;
165
+ int disabled; /* an invariant failed: producer drops, consumer stops, host lives on */
166
+ int diag_reported; /* a fault was surfaced to stderr once; the rest are suppressed */
167
+ int write_failed; /* the file being written has had a failed write counted */
168
+ int64_t dropped; /* slots discarded under backpressure or after disable; surfaced to Ruby */
169
+
170
+ FILE *file;
171
+ int64_t bytes_written; /* logical size of the current segment; set by the consumer */
172
+
173
+ struct fast_entry *fast; /* the calls this stream's segment has seen: see the fast path, below */
174
+ size_t fast_cap; /* in entries */
175
+ size_t fast_count;
176
+ ra_name *names; /* the class and keyword names this segment has defined */
177
+ int fast_since_slow; /* rows written the fast way since Ruby last looked at this stream */
178
+ int gathering; /* the consumer is in its short wait for a burst */
179
+ #ifdef RA_TESTING
180
+ long test_stall_ms; /* the consumer sleeps this long before its next batch */
181
+ #endif
182
+
183
+ /* Holding, see hold.c. The held rows are the newest on the ring: everything pushed since the
184
+ * last keep or discard. */
185
+ int hold;
186
+ size_t held_words, held_rows;
187
+ int64_t request_words; /* what the held request has been handed, kept on the ring or not */
188
+ int64_t largest_words; /* the most any request of this stream was handed */
189
+ int64_t held_dropped; /* rows of the held request the ring had no room for */
190
+ /* tree and identity of the entry rows dropped from it, and of the inner entry rows after them */
191
+ int64_t pinned[RA_PIN_KINDS][RA_PINNED][2];
192
+ size_t pinned_count[RA_PIN_KINDS];
193
+ struct ra_keep *keeps, *keeps_last; /* requests to write, oldest first, the first being written */
194
+ ra_defs strings, paths;
195
+ };
196
+
197
+ /* The sinks, by handle, with NULL for a free one. Every method Ruby calls holds the GVL while it
198
+ * changes this table, so the table needs no lock of its own. Each sink's ring has one. */
199
+ extern sink **ra_sinks;
200
+ extern size_t ra_sinks_cap;
201
+ extern unsigned long ra_epoch; /* moved on by anything that could leave a fiber holding a sink that is gone */
202
+ extern int64_t ra_write_errors; /* files a writer could not open, write or name; added to atomically */
203
+
204
+ sink *ra_sink_of(VALUE sink_object); /* the stream a Native::Sink names; raises once it is closed */
205
+
206
+ /* ring.c */
207
+ void ra_push_slot(sink *s, slot *sl);
208
+ void *ra_consumer_main(void *arg);
209
+ void ra_gather_init(pthread_cond_t *gather);
210
+ void ra_ring_read(const sink *s, size_t at, uint64_t *row, size_t n);
211
+ size_t ra_drain_rows(sink *s, size_t at, size_t batch, char *out);
212
+ #ifdef RA_TESTING
213
+ long ra_testing_stall_asked(sink *s);
214
+ void ra_testing_stall(sink *s, long ms);
215
+ void Init_ra_ring_testing(VALUE mTesting);
216
+ #else
217
+ # define ra_testing_stall_asked(s) 0L
218
+ # define ra_testing_stall(s, ms) ((void)(ms))
219
+ #endif
220
+
221
+ /* hold.c */
222
+ int ra_hold_defines(sink *s, slot *sl);
223
+ int ra_hold_room(sink *s, size_t n);
224
+ void ra_hold_main(sink *s, char *out);
225
+ void ra_hold_free(sink *s);
226
+ void Init_ra_hold(VALUE cSink);
227
+
228
+ /* rows.c */
229
+ #define RA_ROW_TEXT_MAX 1280 /* the most text one formatted row takes */
230
+ int ra_formatted(int kind);
231
+ char *ra_format_row(const uint64_t *row, char *at);
232
+ void ra_consume_slot(sink *s, slot *sl);
233
+ int64_t ra_mark_format(FILE *file);
234
+ void ra_write_dict(FILE *file, int64_t id, const char *string, long len);
235
+ void ra_write_path(FILE *file, const int64_t *fields);
236
+ void ra_note_io_error(sink *s);
237
+
238
+ void Init_ra_sink(VALUE mNative);
239
+
240
+ #endif
@@ -0,0 +1,254 @@
1
+ #include "state.h"
2
+ #include "os.h"
3
+ #include <errno.h>
4
+
5
+ static long g_tree_sequence = 0; /* the last tree number handed out in this process */
6
+ static ID id_gem, id_state_key;
7
+
8
+ static void state_mark(void *ptr) {
9
+ rb_gc_mark(((ra_state *)ptr)->entry_point);
10
+ rb_gc_mark(((ra_state *)ptr)->inner_entry);
11
+ }
12
+
13
+ static void state_free(void *ptr) {
14
+ ra_state *st = ptr;
15
+ free(st->hashes);
16
+ free(st->gem_runs);
17
+ xfree(st);
18
+ }
19
+
20
+ static size_t state_memsize(const void *ptr) {
21
+ const ra_state *st = ptr;
22
+ return sizeof(ra_state) + st->cap * (sizeof(uint64_t) + sizeof(long));
23
+ }
24
+
25
+ static const rb_data_type_t state_type = {
26
+ "RuntimeAnalysis::Trace::State",
27
+ { state_mark, state_free, state_memsize, },
28
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
29
+ };
30
+
31
+ static VALUE state_alloc(VALUE klass) {
32
+ ra_state *st;
33
+ VALUE obj = TypedData_Make_Struct(klass, ra_state, &state_type, st);
34
+ st->entry_point = Qnil;
35
+ st->inner_entry = Qnil;
36
+ st->handle = -1;
37
+ return obj;
38
+ }
39
+
40
+ ra_state *ra_state_of(VALUE state) {
41
+ ra_state *st;
42
+ TypedData_Get_Struct(state, ra_state, &state_type, st);
43
+ return st;
44
+ }
45
+
46
+ int ra_state_p(VALUE object) {
47
+ return rb_typeddata_is_kind_of(object, &state_type);
48
+ }
49
+
50
+ /* Trace::State.current keeps each fiber's state under this key in the fiber's own storage. */
51
+ ra_state *ra_state_current(void) {
52
+ VALUE state = ra_state_current_object();
53
+ return NIL_P(state) ? NULL : ra_state_of(state);
54
+ }
55
+
56
+ VALUE ra_state_current_object(void) {
57
+ return rb_thread_local_aref(rb_thread_current(), id_state_key);
58
+ }
59
+
60
+ static void roll(ra_state *st) {
61
+ st->uuid = 0;
62
+ st->stamped = 0;
63
+ st->step = 0;
64
+ st->path_hash = 0;
65
+ st->gem_run = 0;
66
+ }
67
+
68
+ /* Twice the depth. 0 when the system has no memory for it, and the tree is then as deep as it was.
69
+ * The memory is the system's and not Ruby's: Ruby's allocator raises NoMemoryError, which nothing
70
+ * in the recorder catches, into the call being recorded. */
71
+ static int deepen(ra_state *st) {
72
+ size_t cap = st->cap ? st->cap * 2 : 32;
73
+ uint64_t *hashes = ra_realloc(st->hashes, cap * sizeof(uint64_t));
74
+ if (!hashes) return 0;
75
+ st->hashes = hashes;
76
+
77
+ long *gem_runs = ra_realloc(st->gem_runs, cap * sizeof(long));
78
+ if (!gem_runs) return 0;
79
+ st->gem_runs = gem_runs;
80
+ st->cap = cap;
81
+ return 1;
82
+ }
83
+
84
+ /* Whether the tree can take one more call. Asked before anything is changed for the call, so that
85
+ * a call there is no room for is left to Ruby, or refused, with the tree as it was. */
86
+ int ra_state_room(ra_state *st) {
87
+ return st->depth < st->cap || deepen(st);
88
+ }
89
+
90
+ /* The caller has asked ra_state_room. */
91
+ void ra_state_push(ra_state *st, uint64_t key, int gem) {
92
+ st->hashes[st->depth] = st->path_hash;
93
+ st->gem_runs[st->depth] = st->gem_run;
94
+ st->depth++;
95
+ st->path_hash = ra_state_fold(st->path_hash, key);
96
+ st->gem_run = gem ? st->gem_run + 1 : 0;
97
+ }
98
+
99
+ /* Call exit: restore the caller's path hash, and when the tree fully unwinds start afresh. An extra
100
+ * pop on an empty stack leaves a clean state. */
101
+ void ra_state_pop(ra_state *st) {
102
+ if (st->depth > 0) {
103
+ st->depth--;
104
+ st->path_hash = st->hashes[st->depth];
105
+ st->gem_run = st->gem_runs[st->depth];
106
+ } else {
107
+ st->path_hash = 0;
108
+ st->gem_run = 0;
109
+ }
110
+ if (st->depth == 0) roll(st);
111
+ }
112
+
113
+ /* The frame is opaque here: an Integer is the key itself, and anything else is asked for its hash. */
114
+ static uint64_t key_of(VALUE frame) {
115
+ return (uint64_t)(FIXNUM_P(frame) ? FIX2LONG(frame) : NUM2LONG(rb_hash(frame)));
116
+ }
117
+
118
+ /* Call entry: fold this call's frame into the path and return [uuid, step] for the record. A new tree
119
+ * is given the next number. `gem:` says the call is into an included gem. */
120
+ static VALUE state_enter(int argc, VALUE *argv, VALUE self) {
121
+ ra_state *st = ra_state_of(self);
122
+ VALUE frame, options, gem = Qfalse;
123
+ rb_scan_args(argc, argv, "1:", &frame, &options);
124
+ if (!NIL_P(options)) rb_get_kwargs(options, &id_gem, 0, 1, &gem);
125
+
126
+ uint64_t key = key_of(frame);
127
+ if (!ra_state_room(st)) rb_syserr_fail(ENOMEM, "runtime_analysis native: no memory for a deeper tree");
128
+ if (st->uuid == 0) st->uuid = __atomic_add_fetch(&g_tree_sequence, 1, __ATOMIC_RELAXED);
129
+ st->step++;
130
+ ra_state_push(st, key, RTEST(gem));
131
+ return rb_assoc_new(LONG2NUM(st->uuid), LONG2NUM(st->step));
132
+ }
133
+
134
+ static VALUE state_leave(VALUE self) {
135
+ ra_state_pop(ra_state_of(self));
136
+ return Qnil;
137
+ }
138
+
139
+ /* Whether a call into a gem is still within `depth` calls of the app's own code. With nothing in
140
+ * flight there is no app code above, and the answer is no. */
141
+ static VALUE state_within_p(VALUE self, VALUE depth) {
142
+ ra_state *st = ra_state_of(self);
143
+ return (st->depth > 0 && st->gem_run < NUM2LONG(depth)) ? Qtrue : Qfalse;
144
+ }
145
+
146
+ static VALUE state_capturing_p(VALUE self) {
147
+ return ra_state_of(self)->capturing ? Qtrue : Qfalse;
148
+ }
149
+
150
+ static VALUE capture_done(VALUE self) {
151
+ ra_state_of(self)->capturing = 0;
152
+ return Qnil;
153
+ }
154
+
155
+ /* True while the recorder is inside its own capture, so that a wrapped method it calls on the way
156
+ * passes through untraced. */
157
+ static VALUE state_capture(VALUE self) {
158
+ ra_state_of(self)->capturing = 1;
159
+ return rb_ensure(rb_yield, Qnil, capture_done, self);
160
+ }
161
+
162
+ static VALUE state_path_hash(VALUE self) {
163
+ return ULL2NUM(ra_state_of(self)->path_hash);
164
+ }
165
+
166
+ /* The path hash of the caller, or nil with no call in flight. */
167
+ static VALUE state_parent_hash(VALUE self) {
168
+ ra_state *st = ra_state_of(self);
169
+ return st->depth > 0 ? ULL2NUM(st->hashes[st->depth - 1]) : Qnil;
170
+ }
171
+
172
+ /* True with no call in flight: before a tree's first call and after its root has left. */
173
+ static VALUE state_idle_p(VALUE self) {
174
+ return ra_state_of(self)->depth == 0 ? Qtrue : Qfalse;
175
+ }
176
+
177
+ /* How many calls are in flight, which is what the recorder puts the tree back to when it fails
178
+ * part way through a call. */
179
+ static VALUE state_depth(VALUE self) {
180
+ return SIZET2NUM(ra_state_of(self)->depth);
181
+ }
182
+
183
+ static VALUE state_gem_run(VALUE self) {
184
+ return LONG2NUM(ra_state_of(self)->gem_run);
185
+ }
186
+
187
+ /* True the first time it is asked in each tree, for the entry row a tree gets with its first call. */
188
+ static VALUE state_first_record_p(VALUE self) {
189
+ ra_state *st = ra_state_of(self);
190
+ if (st->stamped) return Qfalse;
191
+ st->stamped = 1;
192
+ return Qtrue;
193
+ }
194
+
195
+ static VALUE state_entry_point(VALUE self) {
196
+ return ra_state_of(self)->entry_point;
197
+ }
198
+
199
+ static VALUE state_set_entry_point(VALUE self, VALUE identity) {
200
+ ra_state_of(self)->entry_point = identity;
201
+ return identity;
202
+ }
203
+
204
+ static VALUE state_inner_entry(VALUE self) {
205
+ return ra_state_of(self)->inner_entry;
206
+ }
207
+
208
+ static VALUE state_set_inner_entry(VALUE self, VALUE identity) {
209
+ ra_state_of(self)->inner_entry = identity;
210
+ return identity;
211
+ }
212
+
213
+ /* The number of the tree in flight, nil with none. */
214
+ static VALUE state_tree(VALUE self) {
215
+ long uuid = ra_state_of(self)->uuid;
216
+ return uuid ? LONG2NUM(uuid) : Qnil;
217
+ }
218
+
219
+ #ifdef RA_TESTING
220
+ /* For specs: the tree numbered next follows this one, so that a spec can record where a tree's
221
+ * number stops fitting beside its step in the one Integer a Ruby wrapper is handed (fast.c). */
222
+ static VALUE testing_set_tree_number(VALUE self, VALUE number) {
223
+ __atomic_store_n(&g_tree_sequence, NUM2LONG(number), __ATOMIC_RELAXED);
224
+ return number;
225
+ }
226
+
227
+ void Init_ra_state_testing(VALUE mTesting) {
228
+ rb_define_singleton_method(mTesting, "tree_number=", testing_set_tree_number, 1);
229
+ }
230
+ #endif
231
+
232
+ void Init_ra_state(VALUE mTrace) {
233
+ VALUE cState = rb_define_class_under(mTrace, "State", rb_cObject);
234
+ id_gem = rb_intern("gem");
235
+ id_state_key = rb_intern("runtime_analysis_state");
236
+
237
+ rb_define_alloc_func(cState, state_alloc);
238
+ rb_define_method(cState, "enter", state_enter, -1);
239
+ rb_define_method(cState, "leave", state_leave, 0);
240
+ rb_define_method(cState, "within?", state_within_p, 1);
241
+ rb_define_method(cState, "capturing?", state_capturing_p, 0);
242
+ rb_define_method(cState, "capture", state_capture, 0);
243
+ rb_define_method(cState, "path_hash", state_path_hash, 0);
244
+ rb_define_method(cState, "parent_hash", state_parent_hash, 0);
245
+ rb_define_method(cState, "idle?", state_idle_p, 0);
246
+ rb_define_method(cState, "depth", state_depth, 0);
247
+ rb_define_method(cState, "gem_run", state_gem_run, 0);
248
+ rb_define_method(cState, "first_record?", state_first_record_p, 0);
249
+ rb_define_method(cState, "entry_point", state_entry_point, 0);
250
+ rb_define_method(cState, "entry_point=", state_set_entry_point, 1);
251
+ rb_define_method(cState, "inner_entry", state_inner_entry, 0);
252
+ rb_define_method(cState, "inner_entry=", state_set_inner_entry, 1);
253
+ rb_define_method(cState, "tree", state_tree, 0);
254
+ }
@@ -0,0 +1,57 @@
1
+ #ifndef RA_STATE_H
2
+ #define RA_STATE_H
3
+
4
+ #include <ruby.h>
5
+ #include <stdint.h>
6
+
7
+ /* The call tree in flight on one fiber: the route from the tree's root to the current call as a
8
+ * rolling hash, the tree's number, and a step that orders the calls within it. The wrapper reads and
9
+ * updates this on every recorded call, so it is a C struct, and Ruby sees it as Trace::State. */
10
+ typedef struct {
11
+ long uuid; /* 0 while no tree is in flight */
12
+ long step;
13
+ uint64_t path_hash;
14
+ long gem_run; /* calls into included gems in flight since the last call of the app's own */
15
+ int stamped; /* the tree's entry row has been asked for */
16
+ int capturing; /* the recorder is inside its own capture */
17
+ VALUE entry_point;
18
+ VALUE inner_entry; /* the request or job running inside the entry point, or nil */
19
+ uint64_t *hashes; /* the path hash of each caller, innermost last */
20
+ long *gem_runs;
21
+ size_t depth, cap;
22
+
23
+ /* What the wrapper's fast path goes by. `handle` is the sink this fiber's thread writes to, good
24
+ * while `epoch` is the current one. The `last_` fields are the call row most recently handed to a
25
+ * sink from this fiber, which is how the wrapper learns what the Ruby path wrote for a call. */
26
+ long handle;
27
+ unsigned long epoch;
28
+ long last_handle;
29
+ int64_t last_step, last_path_id, last_file_id;
30
+
31
+ /* Adaptive mode: the wrapper has asked the gate about the call on route `kept_hash` and been told
32
+ * to keep it. Ruby, if the call turns out to be its to record, is given that answer. */
33
+ int kept;
34
+ uint64_t kept_hash;
35
+ } ra_state;
36
+
37
+ /* Keeps the rolling hash a machine integer however deep the stack gets. */
38
+ #define RA_HASH_MASK ((1ULL << 62) - 1)
39
+ #define RA_HASH_MULTIPLIER 1000003ULL
40
+
41
+ static inline uint64_t ra_state_fold(uint64_t path_hash, uint64_t key) {
42
+ return ((path_hash * RA_HASH_MULTIPLIER) ^ key) & RA_HASH_MASK;
43
+ }
44
+
45
+ void Init_ra_state(VALUE mTrace);
46
+ #ifdef RA_TESTING
47
+ void Init_ra_state_testing(VALUE mTesting);
48
+ #endif
49
+ ra_state *ra_state_of(VALUE state);
50
+ int ra_state_p(VALUE object); /* whether it is a Trace::State */
51
+ ra_state *ra_state_current(void); /* this fiber's state, or NULL before it has one */
52
+ VALUE ra_state_current_object(void); /* the same as Ruby holds it, or nil */
53
+ int ra_state_room(ra_state *st);
54
+ void ra_state_push(ra_state *st, uint64_t key, int gem);
55
+ void ra_state_pop(ra_state *st);
56
+
57
+ #endif