[PATCH v6 3/8] perf cs-etm: Use thread-stack for last branch entries

Leo Yan leo.yan at arm.com
Mon Jun 8 05:38:40 PDT 2026


On Thu, Jun 04, 2026 at 03:09:26PM +0100, James Clark wrote:

[...]

> > @@ -1798,14 +1746,7 @@ static int cs_etm__sample(struct cs_etm_queue *etmq,
> >   	tidq->period_instructions += tidq->packet->instr_count;
> > -	/*
> > -	 * Record a branch when the last instruction in
> > -	 * PREV_PACKET is a branch.
> > -	 */
> > -	if (etm->synth_opts.last_branch &&
> > -	    tidq->prev_packet->sample_type == CS_ETM_RANGE &&
> > -	    tidq->prev_packet->last_instr_taken_branch)
> > -		cs_etm__update_last_branch_rb(etmq, tidq);
> > +	cs_etm__add_stack_event(etmq, tidq);
> 
> Would it be cleaner to call this whenever a branch sample is generated?

We should not couple stack event and generating branch samples.

The reason is the stack event can be used separately by instruction
samples, e.g., the option "--itrace=i100il64g16". And the branch stack
and call chain must get ready before synthesing samples.

> Seems like the conditions for calling thread_stack__event() and
> cs_etm__synth_branch_sample() are slightly different (ignoring the fact that
> branches are only generated when the user asks for them).
> 
> Maybe the conditions should be different, but maybe a comment why or if
> they're the same, a shared function for the conditions would help.

I can add a helper to check if a packet is taken branch and it can be
used by cs_etm__add_stack_event() and generating samples.

  bool cs_etm__packet_is_taken_branch(struct cs_etm_packet *packet)
  {
      if (packet->sample_type == CS_ETM_RANGE &&
          packet->last_instr_taken_branch)
          return true;

      return false;
  }

Thanks,
Leo



More information about the linux-arm-kernel mailing list