]> git.itanic.dy.fi Git - rrdd/commitdiff
Use long long instead of ints when parsing cpu tics
authorTimo Kokkonen <timo.t.kokkonen@nokia.com>
Thu, 1 Oct 2009 11:15:10 +0000 (14:15 +0300)
committerTimo Kokkonen <timo.t.kokkonen@nokia.com>
Thu, 1 Oct 2009 11:15:10 +0000 (14:15 +0300)
This fixes a problem with overwrapping counters on 32bit architectures.

parser.c
string.c
string.h

index 24e8fade8938ba639c5b26cbc20958deccf35bad..d0ef1ba6a929048ed0be4f81ffa99de754d2f36e 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -15,7 +15,7 @@ int cpu_parser(char *data)
        char buf[1024];
        char *str = buf;
        FILE *file = fopen(STATFILE, "r");
-       int user, nice, sys, idle, wait, irq, softirq;
+       long long user, nice, sys, idle, wait, irq, softirq;
 
        if (file == NULL) {
                pr_err("Failed to open file %s\n", STATFILE);
@@ -28,15 +28,15 @@ int cpu_parser(char *data)
                return -1;
        }
 
-       user    = dec_to_int(str, &str);
-       nice    = dec_to_int(str, &str);
-       sys     = dec_to_int(str, &str);
-       idle    = dec_to_int(str, &str);
-       wait    = dec_to_int(str, &str);
-       irq     = dec_to_int(str, &str);
-       softirq = dec_to_int(str, &str);
+       user    = dec_to_longlong(str, &str);
+       nice    = dec_to_longlong(str, &str);
+       sys     = dec_to_longlong(str, &str);
+       idle    = dec_to_longlong(str, &str);
+       wait    = dec_to_longlong(str, &str);
+       irq     = dec_to_longlong(str, &str);
+       softirq = dec_to_longlong(str, &str);
 
-       sprintf(data, "%d:%d:%d:%d:%d:%d:%d",
+       sprintf(data, "%lld:%lld:%lld:%lld:%lld:%lld:%lld",
                user, nice, sys, idle, wait, irq, softirq);
 
        fclose(file);
index 2ae3678d5921afdec7dbaeed76987ff119adfdb4..6dc30d70efb2f3577f87304d53cf8699bf12e9cb 100644 (file)
--- a/string.c
+++ b/string.c
@@ -18,6 +18,24 @@ int dec_to_int(char *src, char **dst)
        return ret;
 }
 
+int dec_to_longlong(char *src, char **dst)
+{
+       int ret;
+
+       while(((*src < '0') || (*src > '9')) && *src)
+               src++;
+
+       ret = atoll(src);
+
+       if (dst) {
+               while(((*src >= '0') && (*src <= '9')) && *src)
+                       src++;
+               *dst = src;
+       }
+
+       return ret;
+}
+
 float dec_to_float(char *src, char **dst)
 {
        float ret;
index d882c0533419aaba27611745893354de755227ba..326c4a6b99a47b445fd718fa4c434ca9125719b3 100644 (file)
--- a/string.h
+++ b/string.h
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 
 int dec_to_int(char *src, char **dst);
+int dec_to_longlong(char *src, char **dst);
 float dec_to_float(char *src, char **dst);
 int get_word(char *src, char **dst, char *word, int size);