changeset 3:2cb8a6cbe468

cpuacct: convert per cpu usage from nanoseconds to jiffies
author Louis Opter <louis@dotcloud.com>
date Fri, 31 Dec 2010 20:04:25 +0100
parents ba9d8601d5f9
children 50215911acb3
files _lxcstats.h container.c lxcstats.h probes/cpuacct.c probes/tests/cpuacct.c
diffstat 5 files changed, 16 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/_lxcstats.h	Thu Dec 30 19:01:47 2010 +0100
+++ b/_lxcstats.h	Fri Dec 31 20:04:25 2010 +0100
@@ -4,6 +4,8 @@
 # include "compat/compat.h"
 # include "probes/probes.h"
 
+# define NSEC_PER_SEC 1000000000L
+
 struct      dirent;
 
 /**
--- a/container.c	Thu Dec 30 19:01:47 2010 +0100
+++ b/container.c	Fri Dec 31 20:04:25 2010 +0100
@@ -5,7 +5,6 @@
 #include <dirent.h>
 #include <err.h>
 #include <errno.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -27,8 +26,6 @@
     assert(ct);
     assert(name);
 
-    printf("new container %s\n", name);
-
     c = calloc(1, sizeof(*c));
     if (!c)
         return (NULL);
--- a/lxcstats.h	Thu Dec 30 19:01:47 2010 +0100
+++ b/lxcstats.h	Fri Dec 31 20:04:25 2010 +0100
@@ -17,8 +17,8 @@
     struct  {
         uint32_t    user;       /*< CPU time spent in userland                          */
         uint32_t    system;     /*< CPU time spent in kernelland                        */
-        uint64_t    *percpu;    /*< CPU time per CPU (zero-terminated array or NULL)    */
-    }               cpuacct;
+        uint32_t    *percpu;    /*< CPU time per CPU (zero-terminated array or NULL)    */
+    }               cpuacct;    /*< CPU accounting in USER_HZ                           */
 };
 
 /**
--- a/probes/cpuacct.c	Thu Dec 30 19:01:47 2010 +0100
+++ b/probes/cpuacct.c	Fri Dec 31 20:04:25 2010 +0100
@@ -6,10 +6,20 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "lxcstats.h"
 #include "_lxcstats.h"
 
+static inline uint32_t
+nsecs_to_user_hz(uint64_t counter)
+{
+    /*
+     * XXX I don't know if it will work for custom values of HZ in the kernel.
+     */
+    return (counter / (NSEC_PER_SEC / sysconf(_SC_CLK_TCK)));
+}
+
 static int
 read_stat(struct lxcst *c)
 {
@@ -61,7 +71,7 @@
             errno = 0;
             p = values;
             for (i = 0; i != ncpus && *p; ++i) {
-                c->cpuacct.percpu[i] = strtoll(p, &p, 10);
+                c->cpuacct.percpu[i] = nsecs_to_user_hz(strtoll(p, &p, 10));
                 if (errno)
                     goto err;
                 p += strcspn(p, "0123456789");
--- a/probes/tests/cpuacct.c	Thu Dec 30 19:01:47 2010 +0100
+++ b/probes/tests/cpuacct.c	Fri Dec 31 20:04:25 2010 +0100
@@ -28,7 +28,7 @@
 
     printf("user %d\nsystem %d\n", c->cpuacct.user, c->cpuacct.system);
     for (i = 0; c->cpuacct.percpu[i]; ++i)
-        printf("cpu[%d] %lu\n", i, c->cpuacct.percpu[i]);
+        printf("cpu[%d] %u\n", i, c->cpuacct.percpu[i]);
 
     _lxcst_container_delete(c);