[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 1/2] Get cpuMhz of virNodeGetInfo() from cpufreq/cpuinfo_max_freq, if exist



virNodeGetInfo() gets from
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq, first.

Signed-off-by: Minoru Usui <usui mxm nes nec co jp>
---
 src/nodeinfo.c |   29 ++++++++++++++++++++++-------
 1 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/nodeinfo.c b/src/nodeinfo.c
index 22d53e5..f4e6e1e 100644
--- a/src/nodeinfo.c
+++ b/src/nodeinfo.c
@@ -60,7 +60,7 @@
 # define CPU_SYS_PATH "/sys/devices/system/cpu"
 
 /* NB, this is not static as we need to call it from the testsuite */
-int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
+int linuxNodeInfoCPUPopulate(FILE *cpuinfo, int cpu_mhz,
                              virNodeInfoPtr nodeinfo,
                              bool need_hyperthreads);
 
@@ -167,7 +167,7 @@ static int parse_socket(unsigned int cpu)
     return get_cpu_value(cpu, "topology/physical_package_id", false);
 }
 
-int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
+int linuxNodeInfoCPUPopulate(FILE *cpuinfo, int cpu_mhz,
                              virNodeInfoPtr nodeinfo,
                              bool need_hyperthreads)
 {
@@ -182,7 +182,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
     int online;
 
     nodeinfo->cpus = 0;
-    nodeinfo->mhz = 0;
+    nodeinfo->mhz = cpu_mhz;
     nodeinfo->cores = 1;
 
     nodeinfo->nodes = 1;
@@ -207,7 +207,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
                 return -1;
             }
             nodeinfo->cpus++;
-        } else if (STRPREFIX(buf, "cpu MHz")) {
+        } else if (!cpu_mhz && (STRPREFIX(buf, "cpu MHz"))) {
             char *p;
             unsigned int ui;
             buf += 9;
@@ -336,14 +336,29 @@ int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo) {
 
 #ifdef __linux__
     {
-    int ret;
-    FILE *cpuinfo = fopen(CPUINFO_PATH, "r");
+    int ret, cpu_mhz = 0;
+    FILE *cpuinfo;
+
+    /*
+     * nodeinfo->mhz should return maximum frequency,
+     * but "cpu MHz" of /proc/cpuinfo is scaled by power saving feature.
+     * So it gets cpufreq/cpuinfo_max_freq, if possible.
+     */
+    ret = get_cpu_value(0, "cpufreq/cpuinfo_max_freq", true);
+    if (ret < 0)
+	return -1;
+    else if (ret != 1) {
+	/* convert unit */
+	cpu_mhz = ret / 1000;
+    }
+
+    cpuinfo = fopen(CPUINFO_PATH, "r");
     if (!cpuinfo) {
         virReportSystemError(errno,
                              _("cannot open %s"), CPUINFO_PATH);
         return -1;
     }
-    ret = linuxNodeInfoCPUPopulate(cpuinfo, nodeinfo, true);
+    ret = linuxNodeInfoCPUPopulate(cpuinfo, cpu_mhz, nodeinfo, true);
     VIR_FORCE_FCLOSE(cpuinfo);
     if (ret < 0)
         return -1;
-- 
1.7.1

-- 
Minoru Usui <usui mxm nes nec co jp>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]