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

Re: [libvirt] [PATCH] cpu usage in OpenVZ



On Thu, Jul 17, 2008 at 03:04:46PM +0400, Evgeniy Sokolov wrote:
OpenVZ calculate statistics and allow to get them .
Added function for getting cpu usage of container.

Modular some minor comments,  ACK for this patch.

+    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"));

Need to have a leading '_'  in front of the string to be mark
for translation.

+            return -1;
+        }
+    }
+
     /* TODO These need to be calculated differently for OpenVZ */
     //info->cpuTime =
     //info->maxMem = vm->def->maxmem;
@@ -689,6 +700,48 @@ static int openvzListDefinedDomains(virC
     return got;
 }
+static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid) {
+    int fd;
+    char line[PATH_MAX] ;

Best to use something else as the size here - we're not reading
a path & we should try to eliminate uses of PATH_MAX in libvirt,
since  POSIX allows for it to be undefine, or stupidly huge. I
reckon 1024 would do the job for line length in this case.

+    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);

Can we break this expression across multiple lines to avoid going
soo far over 80 chars.

Daniel

Thanks! I completely forgot basic rules.

fixed patch is attached.

Index: src/openvz_driver.c
===================================================================
RCS file: /data/cvs/libvirt/src/openvz_driver.c,v
retrieving revision 1.31
diff -u -p -r1.31 openvz_driver.c
--- src/openvz_driver.c	16 Jul 2008 20:42:38 -0000	1.31
+++ src/openvz_driver.c	18 Jul 2008 09:38:44 -0000
@@ -94,6 +94,8 @@ static int openvzDomainUndefine(virDomai
 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(virDomain
 
     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(virC
     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;
 }

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