]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
OpenVZ domain cpu time
authorDaniel Veillard <veillard@redhat.com>
Mon, 21 Jul 2008 08:08:25 +0000 (08:08 +0000)
committerDaniel Veillard <veillard@redhat.com>
Mon, 21 Jul 2008 08:08:25 +0000 (08:08 +0000)
* src/openvz_driver.c: patch from Evgeniy Sokolov to get OpenVZ
  domain cpu time consumption.
Daniel

ChangeLog
src/openvz_driver.c

index f99b03fa27d2a2070808cbf07775504fcd541809..f98ff74c1845e762d9a01921b9ef76e31238ddde 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Mon Jul 21 10:07:08 CEST 2008 Daniel Veillard <veillard@redhat.com>
+
+       * src/openvz_driver.c: patch from Evgeniy Sokolov to get OpenVZ
+         domain cpu time consumption.
+
 Mon Jul 21 10:01:36 CEST 2008 Daniel Veillard <veillard@redhat.com>
 
        * configure.in: patch from David Lively to try to detect the xen
index c15d9ea108753bacf50934f5f2bcfa8aded908d6..2960354e01087448cfa497380f4c190cceee9292 100644 (file)
@@ -94,6 +94,8 @@ static int openvzDomainUndefine(virDomainPtr dom);
 static int convCmdbufExec(char cmdbuf[], char *cmdExec[]);
 static void cmdExecFree(char *cmdExec[]);
 
+static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid);
+
 struct openvz_driver ovz_driver;
 
 static int convCmdbufExec(char cmdbuf[], char *cmdExec[])
@@ -279,6 +281,16 @@ static int openvzDomainGetInfo(virDomainPtr dom,
 
     info->state = vm->status;
 
+    if (!openvzIsActiveVM(vm)) {
+        info->cpuTime = 0;
+    } else {
+        if (openvzGetProcessInfo(&(info->cpuTime), dom->id) < 0) {
+            openvzError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                            _("cannot read cputime for domain %d"), dom->id);
+            return -1;
+        }
+    }
+
     /* TODO These need to be calculated differently for OpenVZ */
     //info->cpuTime =
     //info->maxMem = vm->def->maxmem;
@@ -689,6 +701,51 @@ static int openvzListDefinedDomains(virConnectPtr conn,
     return got;
 }
 
+static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid) {
+    int fd;
+    char line[1024] ;
+    unsigned long long usertime, systime, nicetime;
+    int readvps = 0, ret;
+
+/* read statistic from /proc/vz/vestat.
+sample:
+Version: 2.2
+      VEID     user      nice     system     uptime                 idle   other..
+        33       78         0       1330   59454597      142650441835148   other..
+        55      178         0       5340   59424597      542650441835148   other..
+*/
+
+    if ((fd = open("/proc/vz/vestat", O_RDONLY)) == -1)
+        return -1;
+
+    /*search line with VEID=vpsid*/
+    while(1) {
+        ret = openvz_readline(fd, line, sizeof(line));
+        if(ret <= 0)
+            break;
+
+        if (sscanf(line, "%d %llu %llu %llu",
+                          &readvps, &usertime, &nicetime, &systime) != 4)
+            continue;
+
+        if (readvps == vpsid)
+            break; /*found vpsid*/
+    }
+
+    close(fd);
+    if (ret < 0)
+        return -1;
+
+    if (readvps != vpsid) /*not found*/
+        return -1;
+
+    /* convert jiffies to nanoseconds */
+    *cpuTime = 1000ull * 1000ull * 1000ull * (usertime + nicetime  + systime)
+                                     / (unsigned long long)sysconf(_SC_CLK_TCK);
+
+    return 0;
+}
+
 static int openvzNumDefinedDomains(virConnectPtr conn ATTRIBUTE_UNUSED) {
     return ovz_driver.num_inactive;
 }