]> xenbits.xensource.com Git - libvirt.git/commitdiff
bhyve: report cpuTime in bhyveDomainGetInfo
authorRoman Bogorodskiy <bogorodskiy@gmail.com>
Mon, 21 Apr 2014 10:59:58 +0000 (14:59 +0400)
committerRoman Bogorodskiy <bogorodskiy@gmail.com>
Mon, 5 May 2014 14:03:15 +0000 (18:03 +0400)
Add a helper function virBhyveGetDomainTotalCpuStats() to
obtain process CPU time using kvm (kernel memory interface)
and use it to set cpuTime field of the virDomainInfo struct in
bhyveDomainGetInfo().

configure.ac
src/bhyve/bhyve_driver.c
src/bhyve/bhyve_process.c
src/bhyve/bhyve_process.h

index fef99af9c237b0ecdd7170366769c96576cf17e5..7ab4ce7c605e7c363d01927ca4078ec0744932c5 100644 (file)
@@ -2684,6 +2684,13 @@ AC_CHECK_DECLS([cpuset_getaffinity],
                 #include <sys/cpuset.h>
                ])
 
+# Check for BSD kvm (kernel memory interface)
+if test $with_freebsd = yes; then
+     AC_CHECK_LIB([kvm], [kvm_getprocs], [],
+                  [AC_MSG_ERROR([BSD kernel memory interface library is required to build on FreeBSD])]
+                 )
+fi
+
 # Check if we need to look for ifconfig
 if test "$want_ifconfig" = "yes"; then
      AC_PATH_PROG([IFCONFIG_PATH], [ifconfig])
index fe178337674ecede4cd7e0476fb71f5cf4cc0be0..67571d2a406e58d6850bbf531a484e74745fa898 100644 (file)
@@ -269,6 +269,13 @@ bhyveDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
     if (virDomainGetInfoEnsureACL(domain->conn, vm->def) < 0)
         goto cleanup;
 
+    if (virDomainObjIsActive(vm)) {
+        if (virBhyveGetDomainTotalCpuStats(vm, &(info->cpuTime)) < 0)
+            goto cleanup;
+    } else {
+        info->cpuTime = 0;
+    }
+
     info->state = virDomainObjGetState(vm, NULL);
     info->maxMem = vm->def->mem.max_balloon;
     info->nrVirtCpu = vm->def->vcpus;
index 855d175284567c57b3dc3dbd06e5f0054328d5a4..9f02a4910aa886e4ed69ed3cfed2482245395129 100644 (file)
 #include <config.h>
 
 #include <fcntl.h>
+#include <kvm.h>
+#include <sys/param.h>
 #include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
 #include <sys/ioctl.h>
 #include <net/if.h>
 #include <net/if_tap.h>
@@ -248,3 +252,39 @@ virBhyveProcessStop(bhyveConnPtr driver,
     virCommandFree(cmd);
     return ret;
 }
+
+int
+virBhyveGetDomainTotalCpuStats(virDomainObjPtr vm,
+                               unsigned long long *cpustats)
+{
+    struct kinfo_proc *kp;
+    kvm_t *kd;
+    char errbuf[_POSIX2_LINE_MAX];
+    int nprocs;
+    int ret = -1;
+
+    if ((kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) {
+        virReportError(VIR_ERR_SYSTEM_ERROR,
+                       _("Unable to get kvm descriptor: %s"),
+                       errbuf);
+        return -1;
+
+    }
+
+    kp = kvm_getprocs(kd, KERN_PROC_PID, vm->pid, &nprocs);
+    if (kp == NULL || nprocs != 1) {
+        virReportError(VIR_ERR_SYSTEM_ERROR,
+                       _("Unable to obtain information about pid: %d"),
+                       (int)vm->pid);
+        goto cleanup;
+    }
+
+    *cpustats = kp->ki_runtime * 1000ull;
+
+    ret = 0;
+
+ cleanup:
+    kvm_close(kd);
+
+    return ret;
+}
index f91504ee266909d253e3b0384d4bb92ed9884149..3049ad0cb4f3b4c0e6e5926ac6eed7de546afd3e 100644 (file)
@@ -34,6 +34,9 @@ int virBhyveProcessStop(bhyveConnPtr driver,
                         virDomainObjPtr vm,
                         virDomainShutoffReason reason);
 
+int virBhyveGetDomainTotalCpuStats(virDomainObjPtr vm,
+                                   unsigned long long *cpustats);
+
 typedef enum {
     VIR_BHYVE_PROCESS_START_AUTODESTROY = 1 << 0,
 } bhyveProcessStartFlags;