perf: Fix 0 count issue of cpu-clock
PATCH 1/1 perf: Fix 0 count issue of cpu-clock
Currently cpu-clock event always returns 0 count, e.g.,
perf stat -e cpu-clock -- sleep 1
Performance counter stats for 'sleep 1':
0 cpu-clock # 0.000 CPUs utilized
1.002308394 seconds time elapsed
The root cause is the commit 'bc4394e5e79c ("perf: Fix the throttle
error of some clock events")' adds PERF_EF_UPDATE flag check before
calling cpu_clock_event_update() to update the count, however the PERF_EF_UPDATE flag is never set when the cpu-clock event is stopped in counting mode (pmu->dev() -> cpu_clock_event_del() -> cpu_clock_event_stop()). This leads to the cpu-clock event count is never updated.
To fix this issue, force to set PERF_EF_UPDATE flag for cpu-clock event just like what task-clock does. Besides, or flags with PERF_EF_UPDATE for task-clock although currently the flags argument would always be 0.
---
With this change, both cpu-clock and task-clock can do counting and samping correctly.
1. perf stat -e cpu-clock,task-clock -- true
Performance counter stats for 'true':
240,636 cpu-clock # 0.358 CPUs utilized
243,319 task-clock # 0.362 CPUs utilized2. perf record -e cpu-clock -c 10000 -Iax,bx -- sleep 1 [ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.028 MB perf.data (36 samples) ]
3. perf record -e task-clock -c 10000 -Iax,bx -- sleep 1 [ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.029 MB perf.data (41 samples) ]
kernel/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/events/core.c +2 -2
@@ -11964,7 +11964,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)
@@ -12043,7 +12043,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)static void task_clock_event_del (struct perf_event *event,int flags) { -task_clock_event_stop (event,PERF_EF_UPDATE ); +task_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void task_clock_event_read (struct perf_event *event)
base-commit: 2093d8cf80fa5552d1025a78a8f3a10bf3b6466e
prerequisite-patch-id: a15bcd62a8dcd219d17489eef88b66ea5488a2a0
prerequisite-patch-id: 2a0eefce67b21d1f30c272fd8115b0dc1aca3897
Signature · 1 line
2.34.1
Quoted 17 lines · Dapeng Mi
Currently cpu-clock event always returns 0 count, e.g.,
perf stat -e cpu-clock -- sleep 1
Performance counter stats for 'sleep 1':
0 cpu-clock # 0.000 CPUs utilized
1.002308394 seconds time elapsedThe root cause is the commit '
bc4394e5e79c("perf: Fix the throttle
error of some clock events")' addsPERF_EF_UPDATEflag check before
callingcpu_clock_event_update()to update the count, however thePERF_EF_UPDATEflag is never set when the cpu-clock event is stopped in counting mode (pmu->dev()->cpu_clock_event_del()->cpu_clock_event_stop()). This leads to the cpu-clock event count is never updated.
Quoted 6 lines
To fix this issue, force to set
PERF_EF_UPDATEflag for cpu-clock event just like what task-clock does. Besides, or flags withPERF_EF_UPDATEfor task-clock although currently the flags argument would always be 0.Fixes bc4394e5e79c ("perf: Fix the throttle error of some clock events")Signed-off-by Dapeng Mi <[email protected]>
Thanks Dapeng! This is a fairly major regression and I'm surprised my kernel picked it up so quickly. For those interested the relevant part of the breaking change is requiring PERF_EF_UPDATE:
```
@@ -11829,7 +11834,8 @@ static void cpu_clock_event_start(struct
perf_event *event, int flags)
static void cpu_clock_event_stop(struct perf_event *event, int flags)
{
perf_swevent_cancel_hrtimer(event);
- cpu_clock_event_update(event);
+ if (flags & PERF_EF_UPDATE)
+ cpu_clock_event_update(event);
}
```Hopefully a maintainer can pick this up quickly. Thanks, Ian
Quoted 51 lines
---
With this change, both cpu-clock and task-clock can do counting and samping correctly.
1. perf stat -e cpu-clock,task-clock -- true
Performance counter stats for 'true':
240,636 cpu-clock # 0.358 CPUs utilized 243,319 task-clock # 0.362 CPUs utilized2. perf record -e cpu-clock -c 10000 -Iax,bx -- sleep 1 [ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.028 MB perf.data (36 samples) ]3. perf record -e task-clock -c 10000 -Iax,bx -- sleep 1 [ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.029 MB perf.data (41 samples) ]kernel/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/events/core.c +2
-2@@ -11964,7 +11964,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)@@ -12043,7 +12043,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)static void task_clock_event_del (struct perf_event *event,int flags) { -task_clock_event_stop (event,PERF_EF_UPDATE ); +task_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void task_clock_event_read (struct perf_event *event)base-commit:
2093d8cf80fa5552d1025a78a8f3a10bf3b6466e
prerequisite-patch-id:a15bcd62a8dcd219d17489eef88b66ea5488a2a0
prerequisite-patch-id:2a0eefce67b21d1f30c272fd8115b0dc1aca3897--
2.34.1
Ian Rogers
reviewerQuoted 42 lines · Ian Rogers
Quoted 17 lines · Dapeng Mi
Currently cpu-clock event always returns 0 count, e.g.,
perf stat -e cpu-clock -- sleep 1
Performance counter stats for 'sleep 1':
0 cpu-clock # 0.000 CPUs utilized
1.002308394 seconds time elapsedThe root cause is the commit '
bc4394e5e79c("perf: Fix the throttle
error of some clock events")' addsPERF_EF_UPDATEflag check before
callingcpu_clock_event_update()to update the count, however thePERF_EF_UPDATEflag is never set when the cpu-clock event is stopped in counting mode (pmu->dev()->cpu_clock_event_del()->cpu_clock_event_stop()). This leads to the cpu-clock event count is never updated.Quoted 6 lines
To fix this issue, force to set
PERF_EF_UPDATEflag for cpu-clock event just like what task-clock does. Besides, or flags withPERF_EF_UPDATEfor task-clock although currently the flags argument would always be 0.Fixes bc4394e5e79c ("perf: Fix the throttle error of some clock events")Signed-off-by Dapeng Mi <[email protected]>Thanks Dapeng! This is a fairly major regression and I'm surprised my kernel picked it up so quickly. For those interested the relevant part of the breaking change is requiring
PERF_EF_UPDATE:```
(quoted diff) +2
-1@@ -11829,7 +11834,8 @@ static void cpu_clock_event_start(struct perf_event *event, int flags) static void cpu_clock_event_stop(struct perf_event *event, int flags) { perf_swevent_cancel_hrtimer(event); - cpu_clock_event_update(event); + if (flags & PERF_EF_UPDATE) + cpu_clock_event_update(event); } ```
Quoted 4 lines
Reviewed-by Ian Rogers <[email protected]>Hopefully a maintainer can pick this up quickly. Thanks, Ian
Hi Peter and Ingo,
any update on picking this up? Having timer based software events (task-clock, cpu-clock) broken is fairly major regression especially in environments like hypervisors where PMU access is blocked.
Thanks,
Ian
Quoted 51 lines
Quoted 51 lines
---
With this change, both cpu-clock and task-clock can do counting and samping correctly.
1. perf stat -e cpu-clock,task-clock -- true
Performance counter stats for 'true':
240,636 cpu-clock # 0.358 CPUs utilized 243,319 task-clock # 0.362 CPUs utilized2. perf record -e cpu-clock -c 10000 -Iax,bx -- sleep 1 [ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.028 MB perf.data (36 samples) ]3. perf record -e task-clock -c 10000 -Iax,bx -- sleep 1 [ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.029 MB perf.data (41 samples) ]kernel/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/events/core.c +2
-2@@ -11964,7 +11964,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)@@ -12043,7 +12043,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)static void task_clock_event_del (struct perf_event *event,int flags) { -task_clock_event_stop (event,PERF_EF_UPDATE ); +task_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void task_clock_event_read (struct perf_event *event)base-commit:
2093d8cf80fa5552d1025a78a8f3a10bf3b6466e
prerequisite-patch-id:a15bcd62a8dcd219d17489eef88b66ea5488a2a0
prerequisite-patch-id:2a0eefce67b21d1f30c272fd8115b0dc1aca3897--
2.34.1
Hello,
Quoted 16 lines · Dapeng Mi
Currently cpu-clock event always returns 0 count, e.g.,
perf stat -e cpu-clock -- sleep 1
Performance counter stats for 'sleep 1':
0 cpu-clock # 0.000 CPUs utilized
1.002308394 seconds time elapsedThe root cause is the commit '
bc4394e5e79c("perf: Fix the throttle
error of some clock events")' addsPERF_EF_UPDATEflag check before
callingcpu_clock_event_update()to update the count, however thePERF_EF_UPDATEflag is never set when the cpu-clock event is stopped in counting mode (pmu->dev()->cpu_clock_event_del()->cpu_clock_event_stop()). This leads to the cpu-clock event count is never updated.
Quoted 6 lines
To fix this issue, force to set
PERF_EF_UPDATEflag for cpu-clock event just like what task-clock does. Besides, or flags withPERF_EF_UPDATEfor task-clock although currently the flags argument would always be 0.Fixes bc4394e5e79c ("perf: Fix the throttle error of some clock events")Signed-off-by Dapeng Mi <[email protected]>
Thanks,
Namhyung
Quoted 51 lines
---
With this change, both cpu-clock and task-clock can do counting and samping correctly.
1. perf stat -e cpu-clock,task-clock -- true
Performance counter stats for 'true':
240,636 cpu-clock # 0.358 CPUs utilized 243,319 task-clock # 0.362 CPUs utilized2. perf record -e cpu-clock -c 10000 -Iax,bx -- sleep 1 [ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.028 MB perf.data (36 samples) ]3. perf record -e task-clock -c 10000 -Iax,bx -- sleep 1 [ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.029 MB perf.data (41 samples) ]kernel/events/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/events/core.c +2
-2@@ -11964,7 +11964,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)@@ -12043,7 +12043,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)static void task_clock_event_del (struct perf_event *event,int flags) { -task_clock_event_stop (event,PERF_EF_UPDATE ); +task_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void task_clock_event_read (struct perf_event *event)base-commit:
2093d8cf80fa5552d1025a78a8f3a10bf3b6466e
prerequisite-patch-id:a15bcd62a8dcd219d17489eef88b66ea5488a2a0
prerequisite-patch-id:2a0eefce67b21d1f30c272fd8115b0dc1aca3897Signature · 1 line
2.34.1
Peter Zijlstra
maintainerQuoted 16 lines · Dapeng Mi
Currently cpu-clock event always returns 0 count, e.g.,
perf stat -e cpu-clock -- sleep 1
Performance counter stats for 'sleep 1':
0 cpu-clock # 0.000 CPUs utilized
1.002308394 seconds time elapsedThe root cause is the commit '
bc4394e5e79c("perf: Fix the throttle
error of some clock events")' addsPERF_EF_UPDATEflag check before
callingcpu_clock_event_update()to update the count, however thePERF_EF_UPDATEflag is never set when the cpu-clock event is stopped in counting mode (pmu->dev()->cpu_clock_event_del()->cpu_clock_event_stop()). This leads to the cpu-clock event count is never updated.
Quoted 7 lines
To fix this issue, force to set
PERF_EF_UPDATEflag for cpu-clock event just like what task-clock does. Besides, or flags withPERF_EF_UPDATEfor task-clock although currently the flags argument would always be 0.Fixes bc4394e5e79c ("perf: Fix the throttle error of some clock events")Signed-off-by Dapeng Mi <[email protected]>---
So I'm forever struggling to find what tree a particular commit is in, but afaict the above fingered commit is already in Linus' tree and so this should go to perf/urgent, right?
Peter Zijlstra
maintainerQuoted 14 lines · Dapeng Mi
kernel/events/core.c +1
-1@@ -11964,7 +11964,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)
kernel/events/core.c static int task_clock_event_add(struct perf_event *event, int flags) · line 12047
Quoted 6 lines
kernel/events/core.c +1
-1@@ -12043,7 +12043,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)static void task_clock_event_del (struct perf_event *event,int flags) { -task_clock_event_stop (event,PERF_EF_UPDATE ); +task_clock_event_stop (event, flags |PERF_EF_UPDATE ); }
I think it can both just be: PERF_EF_UPDATE. The only pmu::del() caller hands in flags=0, but if there were to be flags added, we'd have to audit all del methods anyway.
Also, the few comments we do have already note that ->del() must do ->stop(EF_UPDATE).
Peter Zijlstra
maintainerQuoted 27 lines · Peter Zijlstra
Quoted 14 lines · Dapeng Mi
kernel/events/core.c +1
-1@@ -11964,7 +11964,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)kernel/events/core.c static int task_clock_event_add(struct perf_event *event, int flags) · line 12047
Quoted 6 lines
kernel/events/core.c +1
-1@@ -12043,7 +12043,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)static void task_clock_event_del (struct perf_event *event,int flags) { -task_clock_event_stop (event,PERF_EF_UPDATE ); +task_clock_event_stop (event, flags |PERF_EF_UPDATE ); }I think it can both just be:
PERF_EF_UPDATE. The onlypmu::del()caller hands in flags=0, but if there were to be flags added, we'd have to audit all del methods anyway.
Quoted 2 lines
Also, the few comments we do have already note that ->del() must do ->stop(EF_UPDATE).
Updated patch now sits in queue/perf/urgent.
Quoted 27 lines · Peter Zijlstra
Quoted 26 lines · Peter Zijlstra
Quoted 14 lines · Dapeng Mi
kernel/events/core.c +1
-1@@ -11964,7 +11964,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)kernel/events/core.c static int task_clock_event_add(struct perf_event *event, int flags) · line 12047
Quoted 6 lines
kernel/events/core.c +1
-1@@ -12043,7 +12043,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)static void task_clock_event_del (struct perf_event *event,int flags) { -task_clock_event_stop (event,PERF_EF_UPDATE ); +task_clock_event_stop (event, flags |PERF_EF_UPDATE ); }I think it can both just be:
PERF_EF_UPDATE. The onlypmu::del()caller hands in flags=0, but if there were to be flags added, we'd have to audit all del methods anyway.
Quoted 3 lines
Quoted 2 lines
Also, the few comments we do have already note that ->del() must do ->stop(EF_UPDATE).
Updated patch now sits in queue/perf/urgent.
Hi Peter,
Thanks for merging. If we decide not to or the "flags", then the last sentence "Besides, or flags with PERF_EF_UPDATE for task-clock although currently the flags argument would always be 0." in the commit message should be dropped as well. :)
Thanks,
Dapeng Mi
Peter Zijlstra
maintainerQuoted 31 lines · Dapeng
Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · Peter Zijlstra
Quoted 14 lines · Dapeng Mi
kernel/events/core.c +1
-1@@ -11964,7 +11964,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event, flags |PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)kernel/events/core.c static int task_clock_event_add(struct perf_event *event, int flags) · line 12047
Quoted 6 lines
kernel/events/core.c +1
-1@@ -12043,7 +12043,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)static void task_clock_event_del (struct perf_event *event,int flags) { -task_clock_event_stop (event,PERF_EF_UPDATE ); +task_clock_event_stop (event, flags |PERF_EF_UPDATE ); }I think it can both just be:
PERF_EF_UPDATE. The onlypmu::del()caller hands in flags=0, but if there were to be flags added, we'd have to audit all del methods anyway.Also, the few comments we do have already note that ->del() must do ->stop(EF_UPDATE).
Quoted 8 lines
Quoted 1 line
Updated patch now sits in queue/perf/urgent.
Hi Peter,
Thanks for merging. If we decide not to or the "flags", then the last sentence "Besides, or flags with
PERF_EF_UPDATEfor task-clock although currently the flags argument would always be 0." in the commit message should be dropped as well. :)
Should be fixed now. Thanks!
Ian Rogers
reviewerQuoted 7 lines · Peter Zijlstra
Quoted 5 lines · Dapeng
Quoted 3 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra
Also, the few comments we do have already note that ->del() must do ->stop(EF_UPDATE).
Hi Peter,
I was trying to do a quick sanity check that other callers of stop were passing in EF_UPDATE. I wondered if the case of perf_event_throttle_group with leader sampling has an issue? The throttle should only happen on an event or group leader that is in sampling mode, but the group members could be non-sampling with leader sampling. Sorry for the noise if I'm wrong on this. Fwiw, the other uses either pass in EF_UPDATE or I think for __perf_event_aux_pause and __perf_event_overflow the problem isn't relevant.
Thanks,
Ian
Quoted 8 lines · Ian Rogers
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Dapeng
Quoted 3 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra
Also, the few comments we do have already note that ->del() must do ->stop(EF_UPDATE).
Hi Peter,
Quoted 8 lines
I was trying to do a quick sanity check that other callers of stop were passing in
EF_UPDATE. I wondered if the case ofperf_event_throttle_groupwith leader sampling has an issue? The throttle should only happen on an event or group leader that is in sampling mode, but the group members could be non-sampling with leader sampling. Sorry for the noise if I'm wrong on this. Fwiw, the other uses either pass inEF_UPDATEor I think for__perf_event_aux_pauseand__perf_event_overflowthe problem isn't relevant.
Base on the logic, it looks we need to add the flag EF_UPDATE in perf_event_throttle_group(), but need Peter's confirm.
Quoted 4 lines
Thanks,
Ian
The following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: f1f96511b1c4c33e53f05909dd267878e0643a9a Gitweb: https://git.kernel.org/tip/f1f96511b1c4c33e53f05909dd267878e0643a9a Author: Dapeng Mi <[email protected]> AuthorDate: Wed, 12 Nov 2025 16:05:26 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Thu, 20 Nov 2025 10:42:12 +01:00
perf: Fix 0 count issue of cpu-clock
Currently cpu-clock event always returns 0 count, e.g.,
perf stat -e cpu-clock -- sleep 1
Performance counter stats for 'sleep 1':
0 cpu-clock # 0.000 CPUs utilized
1.002308394 seconds time elapsed
The root cause is the commit 'bc4394e5e79c ("perf: Fix the throttle
error of some clock events")' adds PERF_EF_UPDATE flag check before
calling cpu_clock_event_update() to update the count, however the PERF_EF_UPDATE flag is never set when the cpu-clock event is stopped in counting mode (pmu->dev() -> cpu_clock_event_del() -> cpu_clock_event_stop()). This leads to the cpu-clock event count is never updated.
To fix this issue, force to set PERF_EF_UPDATE flag for cpu-clock event just like what task-clock does.
--- kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/core.c +1 -1
@@ -11901,7 +11901,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)static void cpu_clock_event_del (struct perf_event *event,int flags) { -cpu_clock_event_stop (event, flags); +cpu_clock_event_stop (event,PERF_EF_UPDATE ); }static void cpu_clock_event_read (struct perf_event *event)