| 1 | # Trace experiments |
| 2 | |
| 3 | Execution traces allow for trialing new events on an experimental basis via |
| 4 | trace experiments. |
| 5 | This document is a guide that explains how you can define your own trace |
| 6 | experiments. |
| 7 | |
| 8 | Note that if you're just trying to do some debugging or perform some light |
| 9 | instrumentation, then a trace experiment is way overkill. |
| 10 | Use `runtime/trace.Log` instead. |
| 11 | Even if you're just trying to create a proof-of-concept for a low-frequency |
| 12 | event, `runtime/trace.Log` will probably be easier overall if you can make |
| 13 | it work. |
| 14 | |
| 15 | Consider a trace experiment if: |
| 16 | - The volume of new trace events will be relatively high, and so the events |
| 17 | would benefit from a more compact representation (creating new tables to |
| 18 | deduplicate data, taking advantage of the varint representation, etc.). |
| 19 | - It's not safe to call `runtime/trace.Log` (or its runtime equivalent) in |
| 20 | the contexts you want to generate an event (for example, for events about |
| 21 | timers). |
| 22 | |
| 23 | ## Defining a new experiment |
| 24 | |
| 25 | To define a new experiment, modify `internal/trace/tracev2` to define a |
| 26 | new `Experiment` enum value. |
| 27 | |
| 28 | An experiment consists of two parts: timed events and experimental batches. |
| 29 | Timed events are events like any other and follow the same format. |
| 30 | They are easier to order and require less work to make use of. |
| 31 | Experimental batches are essentially bags of bytes that correspond to |
| 32 | an entire trace generation. |
| 33 | What they contain and how they're interpreted is totally up to you, but |
| 34 | they're most often useful for tables that your other events can refer into. |
| 35 | For example, the AllocFree experiment uses them to store type information |
| 36 | that allocation events can refer to. |
| 37 | |
| 38 | ### Defining new events |
| 39 | |
| 40 | 1. Define your new experiment event types (by convention, experimental events |
| 41 | types start at ID 127, so look for the `const` block defining events |
| 42 | starting there). |
| 43 | 2. Describe your new events in `specs`. |
| 44 | Use the documentation for `Spec` to write your new specs, and check your |
| 45 | work by running the tests in the `internal/trace/tracev2` package. |
| 46 | If you wish for your event argument to be interpreted in a particular |
| 47 | way, follow the naming convention in |
| 48 | `src/internal/trace/tracev2/spec.go`. |
| 49 | For example, if you intend to emit a string argument, make sure the |
| 50 | argument name has the suffix `string`. |
| 51 | 3. Add ordering and validation logic for your new events to |
| 52 | `src/internal/trace/order.go` by listing handlers for those events in |
| 53 | the `orderingDispatch` table. |
| 54 | If your events are always emitted in a regular user goroutine context, |
| 55 | then the handler should be trivial and just validate the scheduling |
| 56 | context to match userGoReqs. |
| 57 | If it's more complicated, see `(*ordering).advanceAllocFree` for a |
| 58 | slightly more complicated example that handles events from a larger |
| 59 | variety of execution environments. |
| 60 | If you need to encode a partial ordering, look toward the scheduler |
| 61 | events (names beginning with `Go`) or just ask someone for help. |
| 62 | 4. Add your new events to the `tracev2Type2Kind` table in |
| 63 | `src/internal/trace/event.go`. |
| 64 | |
| 65 | ## Emitting data |
| 66 | |
| 67 | ### Emitting your new events |
| 68 | |
| 69 | 1. Define helper methods on `runtime.traceEventWriter` for emitting your |
| 70 | events. |
| 71 | 2. Instrument the runtime with calls to these helper methods. |
| 72 | Make sure to call `traceAcquire` and `traceRelease` around the operation |
| 73 | your event represents, otherwise it will not be emitted atomically with |
| 74 | that operation completing, resulting in a potentially misleading trace. |
| 75 | |
| 76 | ### Emitting experimental batches |
| 77 | |
| 78 | To emit experimental batches, use the `runtime.unsafeTraceExpWriter` to |
| 79 | write experimental batches associated with your experiment. |
| 80 | Heed the warnings and make sure that while you write them, the trace |
| 81 | generation cannot advance. |
| 82 | Note that each experiment can only have one distinguishable set of |
| 83 | batches. |
| 84 | |
| 85 | ## Recovering experimental data |
| 86 | |
| 87 | ### Recovering experimental events from the trace |
| 88 | |
| 89 | Experimental events will appear in the event stream as an event with the |
| 90 | `EventExperimental` `Kind`. |
| 91 | Use the `Experimental` method to collect the raw data inserted into the |
| 92 | trace. |
| 93 | It's essentially up to you to interpret the event from here. |
| 94 | I recommend writing a thin wrapper API to present a cleaner interface if you |
| 95 | so desire. |
| 96 | |
| 97 | ### Recovering experimental batches |
| 98 | |
| 99 | Parse out all the experimental batches from `Sync` events as they come. |
| 100 | These experimental batches are all for the same generation as all the |
| 101 | experimental events up until the next `Sync` event. |