perfetto icon indicating copy to clipboard operation
perfetto copied to clipboard

traced_perf - support setting period/frequency for follower events.

Open abhmen01 opened this issue 7 months ago • 5 comments

This change adds support for configuring the sampling period or frequency for follower events.

Previously, only the group leader event could be configured to trigger samples. With this change, follower events can also independently trigger samples by specifying the 'period' or 'frequency' field in the config.

This aligns Perfetto's behavior more closely with perf semantics, e.g., perf record -e '{context-switches,cpu-clock/period=<SAMPLE_PERIOD>}', and allows more flexible PMU event sampling.

abhmen01 avatar Apr 21 '26 18:04 abhmen01

The current behavior is indeed leader sampling, and I agree that this is how perfetto works today. The intent of this patch is to extend that behavior, not to make events independent. In particular, this is not trying to model perf record -e A,B, where each event is recorded separately. The goal is to keep the events in a group and read them together, but allow a follower event to trigger sampling when its own period overflows.

So today the behavior is effectively equivalent to: perf record -e '{A,B}':S where only the leader can trigger samples, I presume.

With this patch, if a period is specified for follower B, the intended behavior becomes: perf record -e '{A,B/period=100/}'

That is, the group is still read as a unit regardless of which event triggered the sample, but sampling is no longer restricted to the leader only. So I think the key difference is:

current behavior: grouped read, leader-only sample trigger intended behavior with this patch: grouped read, leader or follower can trigger if that follower has its own sampling period configured.

abhmen01 avatar Apr 28 '26 11:04 abhmen01

@rsavitski A concrete use case is periodic IPC measurement for a process. For example:

perf record -e '{sched:sched_switch/period=1/,cpu-cycles/period=10000/,inst_retired}'

This keeps the given events in one group so they are read together as a consistent snapshot. sched:sched_switch/period=1/ can trigger a sample on every context switch, while cpu-cycles/period=10000/ can also trigger a sample every 10,000 cycles. At each sample point, both the cycle count and retired-instruction count are captured together, which allows IPC to be computed periodically.

The equivalent perfetto configuration will be:

data_sources: {
    config {
        name: "linux.perf"
        perf_event_config {
            timebase {
                period: 1
                tracepoint {
                    name: "sched/sched_switch"
                }
                name: "sched_switch"
            }
            followers: {
                period: 10000
                raw_event {
                    type: 8
                    config: 17
                }
                name: "CPU_CYCLES"
            }
            followers: {
                raw_event {
                    type: 8
                    config: 8
                }
                name: "INST_RETIRED"
            }
            ignore_open_failure: true

        }
    }
}

abhmen01 avatar Apr 29 '26 19:04 abhmen01

I think this is one of the things which could be nice but seems like quite a bit of subtle code to reason about. Are we sure there's no way to do this with just specifying multiple linux.perf data sources in a single config?

LalitMaganti avatar May 18 '26 14:05 LalitMaganti

@LalitMaganti Multiple linux.perf data sources would create independent groups, not one grouped read. That is fine for recording counters separately, but not equivalent to what this patch is trying to do. The intent here is to keep a single perf group, read all events together as one snapshot, and allow a follower overflow to trigger that group read. Multiple data sources do not preserve that behavior.

abhmen01 avatar May 19 '26 17:05 abhmen01

Sure. I think the thing we have to weigh is whether the cost of adding something which we would not use in the forseeable and is so niche is worth it. Right now, the team is already under a bit of short-staffing pressure give some engineers who have moved on from the team so I'm not sure if a subtle and complex patch like this is really something we want to take on right now especially if there are teething issues for this going forward as well.

We might need to park this for now and revisit at some point in the future.

LalitMaganti avatar May 19 '26 17:05 LalitMaganti