Module: check_mk
Branch: master
Commit: 79e551186b02cfbdbc1a9f681a6c07ac231984a8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=79e551186b02cf…
Author: Simon Betz <si(a)mathias-kettner.de>
Date: Thu Sep 27 16:47:59 2018 +0200
6735 FIX cpu_util_unix: Fixed calculation of CPU usage on UNIX
Thanks to Juergen Raschke for his patch!
The result of the cpu_usage is wrong. The cpu_time_guest is included in
cpu_time_user: See
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/kerne…
for more information:
<code>
/*
* Account guest CPU time to a process.
* @p: the process that the CPU time gets accounted to
* @cputime: the CPU time spent in virtual machine since the last update
*/
void account_guest_time(struct task_struct *p, u64 cputime)
{
u64 *cpustat = kcpustat_this_cpu->cpustat;
/* Add guest time to process. */
p->utime += cputime;
account_group_user_time(p, cputime);
p->gtime += cputime;
/* Add guest time to cpustat. */
if (task_nice(p) > 0) {
cpustat[CPUTIME_NICE] += cputime;
cpustat[CPUTIME_GUEST_NICE] += cputime;
} else {
cpustat[CPUTIME_USER] += cputime;
cpustat[CPUTIME_GUEST] += cputime;
}
}
</code>
FEED-935
Change-Id: Ic26ae1d06da1284b61649831e4967776f0a89684
---
.werks/6735 | 40 ++++++++++++++++++++++++++++++++++++++++
checks/cpu_util.include | 16 +++++++++++++---
2 files changed, 53 insertions(+), 3 deletions(-)
diff --git a/.werks/6735 b/.werks/6735
new file mode 100644
index 0000000..4b5ed95
--- /dev/null
+++ b/.werks/6735
@@ -0,0 +1,40 @@
+Title: cpu_util_unix: Fixed calculation of CPU usage on UNIX
+Level: 1
+Component: checks
+Class: fix
+Compatible: compat
+Edition: cre
+State: unknown
+Version: 1.6.0i1
+Date: 1538059527
+
+The result of the cpu_usage is wrong. The cpu_time_guest is included in
+cpu_time_user: See
+https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/kernel/sched/cputime.c
+for more information:
+
+<code>
+/*
+ * Account guest CPU time to a process.
+ * @p: the process that the CPU time gets accounted to
+ * @cputime: the CPU time spent in virtual machine since the last update
+ */
+void account_guest_time(struct task_struct *p, u64 cputime)
+{
+ u64 *cpustat = kcpustat_this_cpu->cpustat;
+
+ /* Add guest time to process. */
+ p->utime += cputime;
+ account_group_user_time(p, cputime);
+ p->gtime += cputime;
+
+ /* Add guest time to cpustat. */
+ if (task_nice(p) > 0) {
+ cpustat[CPUTIME_NICE] += cputime;
+ cpustat[CPUTIME_GUEST_NICE] += cputime;
+ } else {
+ cpustat[CPUTIME_USER] += cputime;
+ cpustat[CPUTIME_GUEST] += cputime;
+ }
+}
+</code>
diff --git a/checks/cpu_util.include b/checks/cpu_util.include
index c5557cc..eb429af 100644
--- a/checks/cpu_util.include
+++ b/checks/cpu_util.include
@@ -132,8 +132,8 @@ def check_cpu_util(util, params, this_time=None, cores=None):
# - 5 - irq: servicing interrupts
# - 6 - softirq: servicing softirqs
# - 7 - steal: involuntary wait
-# - 8 - guest: time spent in guest OK
-# - 9 - guest_nice: time spent in niced guest OK
+# - 8 - guest: time spent in guest OK, also counted in 0 (user)
+# - 9 - guest_nice: time spent in niced guest OK, also counted in 1 (nice)
def check_cpu_util_unix(values, params, cores = None):
this_time = time.time()
# Compute jiffi-differences of all relevant counters
@@ -150,7 +150,17 @@ def check_cpu_util_unix(values, params, cores = None):
if sum_jiffies == 0:
raise MKCounterWrapped("Too short time difference since last check")
- user = diff_values[0] + diff_values[1] # add user + nice
+ user = diff_values[0] + diff_values[1] # add user + nice
+ if len(values) >= 10: # and (values[8] or values[9]):
+ #
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/kerne…
+ # see 'account_guest_time'
+ # if task_nice(p) <= 0:
+ # cpustat[CPUTIME_USER] += cputime;
+ # cpustat[CPUTIME_GUEST] += cputime;
+ subtrahend = diff_values[8] + diff_values[9]
+ user -= subtrahend
+ sum_jiffies -= subtrahend
+
system = diff_values[2] + diff_values[5] + diff_values[6]
wait = diff_values[4]
user_perc = 100.0 * float(user) / float(sum_jiffies)