e9d682
From b7a08f453fc448415ce320532907e61fa34f95b7 Mon Sep 17 00:00:00 2001
e9d682
Message-Id: <b7a08f453fc448415ce320532907e61fa34f95b7@dist-git>
e9d682
From: Michal Privoznik <mprivozn@redhat.com>
e9d682
Date: Tue, 9 Aug 2022 16:15:55 +0200
e9d682
Subject: [PATCH] util: Extend virProcessGetStatInfo() for sysTime and userTime
e9d682
MIME-Version: 1.0
e9d682
Content-Type: text/plain; charset=UTF-8
e9d682
Content-Transfer-Encoding: 8bit
e9d682
e9d682
The virProcessGetStatInfo() helper parses /proc stat file for
e9d682
given PID and/or TID and reports cumulative cpuTime which is just
e9d682
a sum of user and sys times. But in near future, we'll need those
e9d682
times separately, so make the function return them too (if caller
e9d682
desires).
e9d682
e9d682
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
e9d682
Reviewed-by: Ján Tomko <jtomko@redhat.com>
e9d682
(cherry picked from commit cdc22d9a21e472a02dae8157e3cca5832f161feb)
e9d682
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2157094
e9d682
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
e9d682
---
e9d682
 src/ch/ch_driver.c     |  1 +
e9d682
 src/qemu/qemu_driver.c |  4 +++-
e9d682
 src/util/virprocess.c  | 33 ++++++++++++++++++++++-----------
e9d682
 src/util/virprocess.h  |  2 ++
e9d682
 4 files changed, 28 insertions(+), 12 deletions(-)
e9d682
e9d682
diff --git a/src/ch/ch_driver.c b/src/ch/ch_driver.c
e9d682
index e7c172c894..bde148075d 100644
e9d682
--- a/src/ch/ch_driver.c
e9d682
+++ b/src/ch/ch_driver.c
e9d682
@@ -1075,6 +1075,7 @@ chDomainHelperGetVcpus(virDomainObj *vm,
e9d682
             vcpuinfo->number = i;
e9d682
             vcpuinfo->state = VIR_VCPU_RUNNING;
e9d682
             if (virProcessGetStatInfo(&vcpuinfo->cpuTime,
e9d682
+                                      NULL, NULL,
e9d682
                                       &vcpuinfo->cpu, NULL,
e9d682
                                       vm->pid, vcpupid) < 0) {
e9d682
                 virReportSystemError(errno, "%s",
e9d682
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
e9d682
index ebd6365f52..84cf2c6a4f 100644
e9d682
--- a/src/qemu/qemu_driver.c
e9d682
+++ b/src/qemu/qemu_driver.c
e9d682
@@ -1359,6 +1359,7 @@ qemuDomainHelperGetVcpus(virDomainObj *vm,
e9d682
             vcpuinfo->state = VIR_VCPU_RUNNING;
e9d682
 
e9d682
             if (virProcessGetStatInfo(&vcpuinfo->cpuTime,
e9d682
+                                      NULL, NULL,
e9d682
                                       &vcpuinfo->cpu, NULL,
e9d682
                                       vm->pid, vcpupid) < 0) {
e9d682
                 virReportSystemError(errno, "%s",
e9d682
@@ -2528,6 +2529,7 @@ qemuDomainGetInfo(virDomainPtr dom,
e9d682
 
e9d682
     if (virDomainObjIsActive(vm)) {
e9d682
         if (virProcessGetStatInfo(&(info->cpuTime), NULL, NULL,
e9d682
+                                  NULL, NULL,
e9d682
                                   vm->pid, 0) < 0) {
e9d682
             virReportError(VIR_ERR_OPERATION_FAILED, "%s",
e9d682
                            _("cannot read cputime for domain"));
e9d682
@@ -10770,7 +10772,7 @@ qemuDomainMemoryStatsInternal(virQEMUDriver *driver,
e9d682
         ret = 0;
e9d682
     }
e9d682
 
e9d682
-    if (virProcessGetStatInfo(NULL, NULL, &rss, vm->pid, 0) < 0) {
e9d682
+    if (virProcessGetStatInfo(NULL, NULL, NULL, NULL, &rss, vm->pid, 0) < 0) {
e9d682
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
e9d682
                        _("cannot get RSS for domain"));
e9d682
     } else {
e9d682
diff --git a/src/util/virprocess.c b/src/util/virprocess.c
e9d682
index 013afd91b4..11f36e00a8 100644
e9d682
--- a/src/util/virprocess.c
e9d682
+++ b/src/util/virprocess.c
e9d682
@@ -1737,32 +1737,37 @@ virProcessGetStat(pid_t pid,
e9d682
 #ifdef __linux__
e9d682
 int
e9d682
 virProcessGetStatInfo(unsigned long long *cpuTime,
e9d682
+                      unsigned long long *userTime,
e9d682
+                      unsigned long long *sysTime,
e9d682
                       int *lastCpu,
e9d682
                       long *vm_rss,
e9d682
                       pid_t pid,
e9d682
                       pid_t tid)
e9d682
 {
e9d682
     g_auto(GStrv) proc_stat = virProcessGetStat(pid, tid);
e9d682
-    unsigned long long usertime = 0, systime = 0;
e9d682
+    unsigned long long utime = 0;
e9d682
+    unsigned long long stime = 0;
e9d682
+    const unsigned long long jiff2nsec = 1000ull * 1000ull * 1000ull /
e9d682
+                                         (unsigned long long) sysconf(_SC_CLK_TCK);
e9d682
     long rss = 0;
e9d682
     int cpu = 0;
e9d682
 
e9d682
     if (!proc_stat ||
e9d682
-        virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_UTIME], NULL, 10, &usertime) < 0 ||
e9d682
-        virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_STIME], NULL, 10, &systime) < 0 ||
e9d682
+        virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_UTIME], NULL, 10, &utime) < 0 ||
e9d682
+        virStrToLong_ullp(proc_stat[VIR_PROCESS_STAT_STIME], NULL, 10, &stime) < 0 ||
e9d682
         virStrToLong_l(proc_stat[VIR_PROCESS_STAT_RSS], NULL, 10, &rss) < 0 ||
e9d682
         virStrToLong_i(proc_stat[VIR_PROCESS_STAT_PROCESSOR], NULL, 10, &cpu) < 0) {
e9d682
         VIR_WARN("cannot parse process status data");
e9d682
     }
e9d682
 
e9d682
-    /* We got jiffies
e9d682
-     * We want nanoseconds
e9d682
-     * _SC_CLK_TCK is jiffies per second
e9d682
-     * So calculate thus....
e9d682
-     */
e9d682
+    utime *= jiff2nsec;
e9d682
+    stime *= jiff2nsec;
e9d682
     if (cpuTime)
e9d682
-        *cpuTime = 1000ull * 1000ull * 1000ull * (usertime + systime)
e9d682
-            / (unsigned long long) sysconf(_SC_CLK_TCK);
e9d682
+        *cpuTime = utime + stime;
e9d682
+    if (userTime)
e9d682
+        *userTime = utime;
e9d682
+    if (sysTime)
e9d682
+        *sysTime = stime;
e9d682
     if (lastCpu)
e9d682
         *lastCpu = cpu;
e9d682
 
e9d682
@@ -1771,7 +1776,7 @@ virProcessGetStatInfo(unsigned long long *cpuTime,
e9d682
 
e9d682
 
e9d682
     VIR_DEBUG("Got status for %d/%d user=%llu sys=%llu cpu=%d rss=%ld",
e9d682
-              (int) pid, tid, usertime, systime, cpu, rss);
e9d682
+              (int) pid, tid, utime, stime, cpu, rss);
e9d682
 
e9d682
     return 0;
e9d682
 }
e9d682
@@ -1844,6 +1849,8 @@ virProcessGetSchedInfo(unsigned long long *cpuWait,
e9d682
 #else
e9d682
 int
e9d682
 virProcessGetStatInfo(unsigned long long *cpuTime,
e9d682
+                      unsigned long long *userTime,
e9d682
+                      unsigned long long *sysTime,
e9d682
                       int *lastCpu,
e9d682
                       long *vm_rss,
e9d682
                       pid_t pid G_GNUC_UNUSED,
e9d682
@@ -1853,6 +1860,10 @@ virProcessGetStatInfo(unsigned long long *cpuTime,
e9d682
      * platforms, so just report neutral values */
e9d682
     if (cpuTime)
e9d682
         *cpuTime = 0;
e9d682
+    if (userTime)
e9d682
+        *userTime = 0;
e9d682
+    if (sysTime)
e9d682
+        *sysTime = 0;
e9d682
     if (lastCpu)
e9d682
         *lastCpu = 0;
e9d682
     if (vm_rss)
e9d682
diff --git a/src/util/virprocess.h b/src/util/virprocess.h
e9d682
index 086fbe0e4d..f5a4a4e508 100644
e9d682
--- a/src/util/virprocess.h
e9d682
+++ b/src/util/virprocess.h
e9d682
@@ -195,6 +195,8 @@ typedef enum {
e9d682
 int virProcessNamespaceAvailable(unsigned int ns);
e9d682
 
e9d682
 int virProcessGetStatInfo(unsigned long long *cpuTime,
e9d682
+                          unsigned long long *userTime,
e9d682
+                          unsigned long long *sysTime,
e9d682
                           int *lastCpu,
e9d682
                           long *vm_rss,
e9d682
                           pid_t pid,
e9d682
-- 
e9d682
2.39.0
e9d682