From 8b1baed6028ab85099ee9823ec2bc0b1d848b84d Mon Sep 17 00:00:00 2001 From: markj Date: Fri, 3 Mar 2017 20:57:40 +0000 Subject: [PATCH] Fix a ticks comparison in sched_pctcpu_update(). We may fail to reset the %CPU tracking window if a thread does not run for over half of the ticks rollover period, resulting in a bogus %CPU value for the thread until ticks fully rolls over. Handle this by comparing the unsigned difference ticks - ts_ltick with SCHED_TICK_TARG instead. Reviewed by: cem, jeff MFC after: 1 week Sponsored by: Dell EMC Isilon --- sys/kern/sched_ule.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sys/kern/sched_ule.c b/sys/kern/sched_ule.c index b650f24db9e5..a2d76ddcd81e 100644 --- a/sys/kern/sched_ule.c +++ b/sys/kern/sched_ule.c @@ -1662,7 +1662,11 @@ sched_pctcpu_update(struct td_sched *ts, int run) { int t = ticks; - if (t - ts->ts_ltick >= SCHED_TICK_TARG) { + /* + * The signed difference may be negative if the thread hasn't run for + * over half of the ticks rollover period. + */ + if ((u_int)(t - ts->ts_ltick) >= SCHED_TICK_TARG) { ts->ts_ticks = 0; ts->ts_ftick = t - SCHED_TICK_TARG; } else if (t - ts->ts_ftick >= SCHED_TICK_MAX) {