Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1532031

Re: [PATCH] perf/x86: fix event counter update issue

From Peter Zijlstra <peterz@infradead.org>
Newsgroups linux.kernel
Subject Re: [PATCH] perf/x86: fix event counter update issue
Date 2016-11-29 10:30 +0100
Message-ID <sIMTD-6Co-23@gated-at.bofh.it> (permalink)
References <sIzMJ-6iD-5@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


So caveat that I'm ill and cannot think much..

On Mon, Nov 28, 2016 at 11:26:46AM -0800, kan.liang@intel.com wrote:

> Here, all the possible failure cases are listed.
> Terms:
>     - new: current PMU counter value which read from rdpmcl.
>     - prev: previous counter value which is stored in &hwc->prev_count.
>     - in PMI/not in PMI: the event update happens in PMI handler or not.

> Current code to calculate delta:
>     delta = (new << shift) - (prev << shift);
>     delta >>= shift;
> 
> Case A: Not in PMI.  new > prev. But delta is negative.
>    That's the failure case of Test 2.
>    delta is s64 type. new and prev are u64 type. If the new is big
>    enough, after doing left shift and sub, the bit 64 of the result may
>    still be 1.
>    After converting to s64, the sign flag will be set. Since delta is
>    s64 type, arithmetic right shift is applied, which copy the sign flag
>    into empty bit positions on the upper end of delta.
>    It can be fixed by adding the max count value.
> 
>    Here is the real data for test2 on KNL.
>    new = aea96e1927
>    prev = ffffff0000000001
>    delta = aea96e1927000000 - 1000000 = aea96e1926000000
>    aea96e1926000000 >> 24 = ffffffaea96e1926   <<  negative delta

How can this happen? IIRC the thing increments, we program a negative
value, and when it passes 0 we generate a PMI.

And note that we _ALWAYS_ set the IN bits, even for !sampling events.
Also note we set max_period to (1<<31) - 1, so we should never exceed 31
bits.


> Case B: In PMI. new > prev. delta is positive.
>    That's the failure case of Test 3.
>    The PMI is triggered by overflow. But there is latency between
>    overflow and PMI handler. So new has small amount.
>    Current calculation lose the max count value.

That doesn't make sense, per the 31bit limit.


> Case C: In PMI. new < prev. delta is negative.
>    The PMU counter may be start from a big value. E.g. the fixed period
>    is small.
>    It can be fixed by adding the max count value.

Doesn't make sense, how can this happen?

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH] perf/x86: fix event counter update issue kan.liang@intel.com - 2016-11-28 20:30 +0100
  Re: [PATCH] perf/x86: fix event counter update issue Stephane Eranian <eranian@google.com> - 2016-11-28 20:50 +0100
    RE: [PATCH] perf/x86: fix event counter update issue "Liang, Kan" <kan.liang@intel.com> - 2016-11-28 21:10 +0100
      Re: [PATCH] perf/x86: fix event counter update issue Stephane Eranian <eranian@google.com> - 2016-11-28 21:20 +0100
        RE: [PATCH] perf/x86: fix event counter update issue "Liang, Kan" <kan.liang@intel.com> - 2016-11-28 21:30 +0100
  Re: [PATCH] perf/x86: fix event counter update issue Peter Zijlstra <peterz@infradead.org> - 2016-11-29 10:30 +0100
    RE: [PATCH] perf/x86: fix event counter update issue "Liang, Kan" <kan.liang@intel.com> - 2016-11-29 15:50 +0100
      Re: [PATCH] perf/x86: fix event counter update issue Peter Zijlstra <peterz@infradead.org> - 2016-11-29 18:00 +0100
        RE: [PATCH] perf/x86: fix event counter update issue "Liang, Kan" <kan.liang@intel.com> - 2016-11-29 18:10 +0100
          Re: [PATCH] perf/x86: fix event counter update issue Peter Zijlstra <peterz@infradead.org> - 2016-11-29 18:20 +0100
    Re: [PATCH] perf/x86: fix event counter update issue Stephane Eranian <eranian@google.com> - 2016-11-29 18:30 +0100
      Re: [PATCH] perf/x86: fix event counter update issue Peter Zijlstra <peterz@infradead.org> - 2016-11-29 18:40 +0100
        Re: [PATCH] perf/x86: fix event counter update issue Stephane Eranian <eranian@google.com> - 2016-11-29 19:20 +0100
        Re: [PATCH] perf/x86: fix event counter update issue Andi Kleen <ak@linux.intel.com> - 2016-11-29 19:40 +0100
        RE: [PATCH] perf/x86: fix event counter update issue "Liang, Kan" <kan.liang@intel.com> - 2016-11-29 20:10 +0100
          Re: [PATCH] perf/x86: fix event counter update issue Peter Zijlstra <peterz@infradead.org> - 2016-11-29 20:40 +0100
            Re: [PATCH] perf/x86: fix event counter update issue Stephane Eranian <eranian@google.com> - 2016-11-29 21:40 +0100
            RE: [PATCH] perf/x86: fix event counter update issue "Liang, Kan" <kan.liang@intel.com> - 2016-11-29 21:40 +0100
              RE: [PATCH] perf/x86: fix event counter update issue "Odzioba, Lukasz" <lukasz.odzioba@intel.com> - 2016-12-02 14:00 +0100
                Re: [PATCH] perf/x86: fix event counter update issue Peter Zijlstra <peterz@infradead.org> - 2016-12-05 11:30 +0100
                RE: [PATCH] perf/x86: fix event counter update issue "Odzioba, Lukasz" <lukasz.odzioba@intel.com> - 2016-12-05 12:30 +0100
      RE: [PATCH] perf/x86: fix event counter update issue "Odzioba, Lukasz" <lukasz.odzioba@intel.com> - 2016-11-29 20:10 +0100

csiph-web