[lxc-devel] [PATCH] /proc/stat in lxcfs

Christian Brauner christianvanbrauner at gmail.com
Mon Feb 16 18:11:40 UTC 2015


> > +        else if (sscanf(cpu_char, "%d", &cpu) != 1) {
> 
> This is an unorthodox (for lxc) coding style.  Could you explain
> exactly what you're doing here?  I would expect just:
> 
>     if (sscanf(cpu_char, "%d", &cpu) != 1))
>         continue;
>     if (!cpu_in_cpuset(cpu, cpuset))
>         continue;

Your way is the better way to do it. I adjusted and squased the PR and
post the patch again here as well:

commit 21c3c7ace34eaf5f19074e332222d050896b5cc3
Author: Christian Brauner <christianvanbrauner at gmail.com>
Date:   Sun Feb 15 11:31:31 2015 +0100

    Show cpu-average in /proc/stat and start cup numbering at 0

diff --git a/lxcfs.c b/lxcfs.c
index d6fb101..d358829 100644
--- a/lxcfs.c
+++ b/lxcfs.c
@@ -1642,7 +1642,7 @@ static int proc_stat_read(char *buf, size_t size, off_t offset,
    nih_local char *cpuset = NULL;
    char *line = NULL;
    size_t linelen = 0, total_len = 0;
-   int curcpu = 0;
+   int curcpu = -1; /* cpu numbering starts at 0 */
    FILE *f;
 
    if (offset)
@@ -1662,16 +1662,21 @@ static int proc_stat_read(char *buf, size_t size, off_t offset,
    while (getline(&line, &linelen, f) != -1) {
        size_t l;
        int cpu;
+        char cpu_char[10]; /* That's a lot of cores */
        char *c;
 
-       if (sscanf(line, "cpu%d", &cpu) != 1) {
-           /* not a ^cpu line, just print it */
+       if (sscanf(line, "cpu%9[^ ]", cpu_char) != 1) {
+           /* not a ^cpuN line containing a number N, just print it */
            l = snprintf(buf, size, "%s", line);
            buf += l;
            size -= l;
            total_len += l;
            continue;
        }
+
+        if (sscanf(cpu_char, "%d", &cpu) != 1)
+                continue;
+
        if (!cpu_in_cpuset(cpu, cpuset))
            continue;
        curcpu ++;

Christian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20150216/0487a340/attachment-0001.sig>


More information about the lxc-devel mailing list