janestreet / magic-trace

magic-trace collects and displays high-resolution traces of what a process is doing
https://magic-trace.org
MIT License
4.58k stars 87 forks source link

Filter trace to range #222

Closed pbaumbacher closed 2 years ago

pbaumbacher commented 2 years ago

Adds a new flag -filter "(<start> <stop>)" to magic trace. With this flag, the trace is trimmed to only include events that occur between start (inclusive) and stop (exclusive) (#81). If either symbol doesn't appear in the trace, no output is produced. In multi-snapshot mode, the trace only includes regions between consecutive start and stop calls. The -filter flag is compatible with the same modes as the -trigger flag.

When producing a .sexp output, only events that occur in the filtered region are emitted. When producing a fuchsia .fxt output, the full available callstack around the filtered region is reconstructed. Active stack frames at the beginning of the filtered region have the time start was called as their inferred start time.

pbaumbacher commented 2 years ago

Otherwise LGTM. I think if you rebased/squashed we could be good to merge.

I had to update the range logic a bit to handle Sample events. The most straightforward way to do this was just to treat any stack containing a range symbol as a Symbol_hit, but a consequence is that in sampling mode the filtered region begins at the end of the range start function, instead of at the beginning in tracing mode.

Making the behavior identical would require a bit more work: the control flow would have to be fairly different depending on if we're receiving sampling events or perf events. I think using sampling with the filter isn't likely to be a super common use-case, but let me know what you think.