All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Liang, Kan" <kan.liang@linux.intel.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: mingo@redhat.com, acme@kernel.org, namhyung@kernel.org,
	irogers@google.com, linux-kernel@vger.kernel.org,
	linux-perf-users@vger.kernel.org, ak@linux.intel.com,
	eranian@google.com
Subject: Re: [PATCH V5 4/4] perf/x86/intel: Support PEBS counters snapshotting
Date: Wed, 18 Dec 2024 09:52:03 -0500	[thread overview]
Message-ID: <5a4ab06e-8628-4e1d-addb-2af920deffad@linux.intel.com> (raw)
In-Reply-To: <20241218082404.GI11133@noisy.programming.kicks-ass.net>



On 2024-12-18 3:24 a.m., Peter Zijlstra wrote:
> On Tue, Dec 17, 2024 at 12:45:56PM -0500, Liang, Kan wrote:
>>
>>
>> On 2024-12-17 8:37 a.m., Peter Zijlstra wrote:
>>> On Mon, Dec 16, 2024 at 12:45:05PM -0800, kan.liang@linux.intel.com wrote:
>>>
>>>> @@ -2049,6 +2102,40 @@ static void setup_pebs_adaptive_sample_data(struct perf_event *event,
>>>>  		}
>>>>  	}
>>>>  
>>>> +	if (format_group & (PEBS_DATACFG_CNTR | PEBS_DATACFG_METRICS)) {
>>>> +		struct pebs_cntr_header *cntr = next_record;
>>>> +		int bit;
>>>> +
>>>> +		next_record += sizeof(struct pebs_cntr_header);
>>>> +
>>>> +		for_each_set_bit(bit, (unsigned long *)&cntr->cntr, INTEL_PMC_MAX_GENERIC) {
>>>> +			x86_perf_event_update(cpuc->events[bit], (u64 *)next_record);
>>>> +			next_record += sizeof(u64);
>>>> +		}
>>>
>>> I still don't much like any of this -- the next_record value might be in
>>> the past relative to what is already in the event.
>>>
>>
>> I forgot to handle the read case. With the below patch, the next_record
>> value should not contain a previous value, unless there is a HW bug.
>> Because no matter it's read/pmi/context switch, perf always stop the
>> PMU, drains the buffer, and the value is always from the PEBS record.
> 
> Consider this 3 counter case:
> 
>  A is our regular counter
>  B is a PEBS event which reads A
>  C is a PEBS event
> 
> For convencience, let A count the lines in our example (anything
> incrementing at a faster rate will get the same result after all).
> 
> Then, afaict, the following can happen:
> 
>   B-assist			A=1
>   C				A=2
>   B-assist			A=3
>   A-overflow-PMI		A=4
>   C-assist-PMI (DS buffer)	A=5
> 
> So the A-PMI will update A->count to 4.
> Then the C-PMI will process the DS buffer, which will:
> 
>   - adjust A->count to 1
>   - adjust A->count to 3

The patch should have addressed the case. I will update the changelog to
point to the case in the next version.

The below is the code to address the issue in this patch.
Perf will drain the PEBS buffer before handling the non-PEBS overflow.

So the A-PMI will
   - Process the DS. adjust A->count to 3
   - adjust A->count to 4
The C-PMI will no touch the A->count since there is no record from B.

@@ -3115,6 +3115,14 @@ static int handle_pmi_common(struct pt_regs
*regs, u64 status)
 		if (!test_bit(bit, cpuc->active_mask))
 			continue;

+		/*
+		 * There may be unprocessed PEBS records in the PEBS buffer,
+		 * which still stores the previous values.
+		 * Process those records first before handling the latest value.
+		 */
+		if (is_pebs_counter_event(event))
+			x86_pmu.drain_pebs(regs, &data);
+
 		if (!intel_pmu_save_and_restart(event))
 			continue;

Thanks,
Kan

> 
> Leaving us, in the end, with A going backwards.
> 
> How is that not a problem?
> 
> IIRC I've raised this point before, and your Changelog does not mention
> anything about this issue at all :-(
> 


  reply	other threads:[~2024-12-18 14:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-16 20:45 [PATCH V5 1/4] perf/x86/intel/ds: Add PEBS format 6 kan.liang
2024-12-16 20:45 ` [PATCH V5 2/4] perf/x86: Extend event update interface kan.liang
2024-12-16 20:45 ` [PATCH V5 3/4] perf: Extend perf_output_read kan.liang
2024-12-16 20:45 ` [PATCH V5 4/4] perf/x86/intel: Support PEBS counters snapshotting kan.liang
2024-12-17 13:37   ` Peter Zijlstra
2024-12-17 17:45     ` Liang, Kan
2024-12-17 20:29       ` Peter Zijlstra
2024-12-17 20:59         ` Liang, Kan
2024-12-18  8:24       ` Peter Zijlstra
2024-12-18 14:52         ` Liang, Kan [this message]
2024-12-24  9:46 ` [tip: perf/urgent] perf/x86/intel/ds: Add PEBS format 6 tip-bot2 for Kan Liang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5a4ab06e-8628-4e1d-addb-2af920deffad@linux.intel.com \
    --to=kan.liang@linux.intel.com \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.