[libvirt] [PATCH 3/7] tests: Inline a sysconf call for linuxCPUStatsToBuf

John Ferlan jferlan at redhat.com
Fri Sep 28 15:28:06 UTC 2018


While unlikely, sysconf(_SC_CLK_TCK) could fail leading to
indeterminate results for the subsequent division. So let's
just remove the # define and inline the same change.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 tests/virhostcputest.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/tests/virhostcputest.c b/tests/virhostcputest.c
index 8cbc302cae..1df4bb8ee6 100644
--- a/tests/virhostcputest.c
+++ b/tests/virhostcputest.c
@@ -72,7 +72,6 @@ linuxTestCompareFiles(const char *cpuinfofile,
     return ret;
 }
 
-# define TICK_TO_NSEC (1000ull * 1000ull * 1000ull / sysconf(_SC_CLK_TCK))
 
 static int
 linuxCPUStatsToBuf(virBufferPtr buf,
@@ -81,6 +80,15 @@ linuxCPUStatsToBuf(virBufferPtr buf,
                    size_t nparams)
 {
     size_t i = 0;
+    unsigned long long tick_to_nsec;
+    long long sc_clk_tck;
+
+    if ((sc_clk_tck = sysconf(_SC_CLK_TCK)) < 0) {
+        fprintf(stderr, "sysconf(_SC_CLK_TCK) fails : %s\n",
+                strerror(errno));
+        return -1;
+    }
+    tick_to_nsec = (1000ull * 1000ull * 1000ull) / sc_clk_tck;
 
     if (cpu < 0)
         virBufferAddLit(buf, "cpu:\n");
@@ -89,7 +97,7 @@ linuxCPUStatsToBuf(virBufferPtr buf,
 
     for (i = 0; i < nparams; i++)
         virBufferAsprintf(buf, "%s: %llu\n", param[i].field,
-                          param[i].value / TICK_TO_NSEC);
+                          param[i].value / tick_to_nsec);
 
     virBufferAddChar(buf, '\n');
     return 0;
-- 
2.17.1




More information about the libvir-list mailing list