From e1df029e6c8bb166a6cec05dc12f699d84631bc9 Mon Sep 17 00:00:00 2001
From: Jim Warner <james.warner@comcast.net>
Date: Sat, 22 Jun 2024 00:00:00 -0500
Subject: [PATCH 1/3] library: implement guest/total tics change, <stat> api

This patch removes guest tics from the total lest they
be counted twice. Such tics have already been included
in user/nice values. For proof, just follow this path:

   fs/proc/stat.c: show_stat() --->
      kernel/sched/cputime.c: kcpustat_cpu_fetch()

Reference(s):
https://gitlab.com/procps-ng/procps/-/issues/339

. Sep, 2020 - where guest distortion occurred
commit ec21588be108d092804eb72edbba2ba214bc6190

Signed-off-by: Jim Warner <james.warner@comcast.net>
---
 library/include/stat.h |  6 +++---
 library/stat.c         | 12 ++++++++----
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/library/include/stat.h b/library/include/stat.h
index ff7c5b17..d8ca2add 100644
--- a/library/include/stat.h
+++ b/library/include/stat.h
@@ -45,8 +45,8 @@ enum stat_item {
     STAT_TIC_IRQ,                 //  ull_int         "
     STAT_TIC_SOFTIRQ,             //  ull_int         "
     STAT_TIC_STOLEN,              //  ull_int         "
-    STAT_TIC_GUEST,               //  ull_int         "
-    STAT_TIC_GUEST_NICE,          //  ull_int         "
+    STAT_TIC_GUEST,               //  ull_int         " (note: also included in USER)
+    STAT_TIC_GUEST_NICE,          //  ull_int         " (note: also included in NICE)
 
     STAT_TIC_DELTA_USER,          //   sl_int        derived from above
     STAT_TIC_DELTA_NICE,          //   sl_int         "
@@ -63,7 +63,7 @@ enum stat_item {
     STAT_TIC_SUM_SYSTEM,          //  ull_int        derived from SYSTEM + IRQ + SOFTIRQ tics
     STAT_TIC_SUM_IDLE,            //  ull_int        derived from IDLE + IOWAIT tics
     STAT_TIC_SUM_BUSY,            //  ull_int        derived from SUM_TOTAL - SUM_IDLE tics
-    STAT_TIC_SUM_TOTAL,           //  ull_int        derived from sum of all 10 tics
+    STAT_TIC_SUM_TOTAL,           //  ull_int        derived from sum of all tics, minus 2 GUEST tics
 
     STAT_TIC_SUM_DELTA_USER,      //   sl_int        derived from above
     STAT_TIC_SUM_DELTA_SYSTEM,    //   sl_int         "
diff --git a/library/stat.c b/library/stat.c
index 202dfa52..96672431 100644
--- a/library/stat.c
+++ b/library/stat.c
@@ -572,11 +572,15 @@ static inline void stat_derive_unique (
     this->new.xidl
         = this->new.idle
         + this->new.iowait;
+    /* note: we exclude guest tics from xtot since ...
+             'user' already includes 'guest'
+             'nice' already includes 'gnice'
+       ( see linux sources: ./kernel/sched/cputime.c, kcpustat_cpu_fetch ) */
     this->new.xtot
-        = this->new.xusr + this->new.xsys + this->new.xidl
-        + this->new.stolen
-        + this->new.guest
-        + this->new.gnice;
+        = this->new.xusr
+        + this->new.xsys
+        + this->new.xidl
+        + this->new.stolen;
     this->new.xbsy
         = this->new.xtot - this->new.xidl;
 
-- 
2.33.0

