simplify diskio stats
authorPhil Sutter <phil@nwl.cc>
Tue, 16 Dec 2008 01:32:30 +0000 (02:32 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 16 Dec 2008 01:32:30 +0000 (02:32 +0100)
Instead of splitting information, use diskio_stats[0] in diskio.c for
the totals. This saves a few branches, and frees some data from struct
information.

src/conky.c
src/conky.h
src/diskio.c

index d9c82d6..513fd9e 100644 (file)
@@ -1557,11 +1557,14 @@ void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
  *         which gets overwritten in consecutive calls. I.e.:
  *         this function is NOT reentrant.
  */
-const char *dev_name(const char *path)
+static const char *dev_name(const char *path)
 {
        static char buf[255];   /* should be enough for pathnames */
        ssize_t buflen;
 
+       if (!path)
+               return NULL;
+
 #define DEV_NAME(x) \
   x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
        if ((buflen = readlink(path, buf, 254)) == -1)
@@ -1889,53 +1892,32 @@ static struct text_object *construct_text_object(const char *s,
                }
 #if defined(__linux__)
        END OBJ(diskio, INFO_DISKIO)
-               if (arg) {
-                       obj->data.diskio = prepare_diskio_stat(dev_name(arg));
-               } else {
-                       obj->data.diskio = NULL;
-               }
+               obj->data.diskio = prepare_diskio_stat(dev_name(arg));
        END OBJ(diskio_read, INFO_DISKIO)
-               if (arg) {
-                       obj->data.diskio = prepare_diskio_stat(dev_name(arg));
-               } else {
-                       obj->data.diskio = NULL;
-               }
+               obj->data.diskio = prepare_diskio_stat(dev_name(arg));
        END OBJ(diskio_write, INFO_DISKIO)
-               if (arg) {
-                       obj->data.diskio = prepare_diskio_stat(dev_name(arg));
-               } else {
-                       obj->data.diskio = NULL;
-               }
+               obj->data.diskio = prepare_diskio_stat(dev_name(arg));
        END OBJ(diskiograph, INFO_DISKIO)
                char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
                        &obj->e, &obj->showaslog);
 
-               if (buf) {
-                       obj->data.diskio = prepare_diskio_stat(buf);
+               obj->data.diskio = prepare_diskio_stat(buf);
+               if (buf)
                        free(buf);
-               } else {
-                       obj->data.diskio = NULL;
-               }
        END OBJ(diskiograph_read, INFO_DISKIO)
                char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
                        &obj->e, &obj->showaslog);
 
-               if (buf) {
-                       obj->data.diskio = prepare_diskio_stat(buf);
+               obj->data.diskio = prepare_diskio_stat(buf);
+               if (buf)
                        free(buf);
-               } else {
-                       obj->data.diskio = NULL;
-               }
        END OBJ(diskiograph_write, INFO_DISKIO)
                char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
                        &obj->e, &obj->showaslog);
 
-               if (buf) {
-                       obj->data.diskio = prepare_diskio_stat(buf);
+               obj->data.diskio = prepare_diskio_stat(buf);
+               if (buf)
                        free(buf);
-               } else {
-                       obj->data.diskio = NULL;
-               }
 #endif
        END OBJ(color, 0)
 #ifdef X11
@@ -4102,59 +4084,28 @@ static void generate_text_internal(char *p, int p_max_size,
                        /* TODO: move this correction from kB to kB/s elsewhere
                         * (or get rid of it??) */
                        OBJ(diskio) {
-                               if (obj->data.diskio) {
-                                       human_readable(
-                                                       (obj->data.diskio->current / update_interval) * 1024LL,
-                                                       p, p_max_size, "diskio");
-                               } else {
-                                       human_readable(info.diskio_value * 1024LL, p, p_max_size,
-                                                       "diskio");
-                               }
+                               human_readable((obj->data.diskio->current / update_interval) * 1024LL,
+                                               p, p_max_size, "diskio");
                        }
                        OBJ(diskio_write) {
-                               if (obj->data.diskio) {
-                                       human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size,
-                                                       "diskio_write");
-                               } else {
-                                       human_readable(info.diskio_write_value * 1024LL, p, p_max_size,
-                                                       "diskio_write");
-                               }
+                               human_readable((obj->data.diskio->current / update_interval) * 1024LL,
+                                               p, p_max_size, "diskio_write");
                        }
                        OBJ(diskio_read) {
-                               if (obj->data.diskio) {
-                                       human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size,
-                                                       "diskio_read");
-                               } else {
-                                       human_readable(info.diskio_read_value * 1024LL, p, p_max_size,
-                                                       "diskio_read");
-                               }
+                               human_readable((obj->data.diskio->current / update_interval) * 1024LL,
+                                               p, p_max_size, "diskio_read");
                        }
                        OBJ(diskiograph) {
-                               if (obj->data.diskio) {
-                                       new_graph(p, obj->a, obj->b, obj->c, obj->d,
-                                                       obj->data.diskio->current, obj->e, 1, obj->showaslog);
-                               } else {
-                                       new_graph(p, obj->a, obj->b, obj->c, obj->d, info.diskio_value,
-                                                       obj->e, 1, obj->showaslog);
-                               }
+                               new_graph(p, obj->a, obj->b, obj->c, obj->d,
+                                         obj->data.diskio->current, obj->e, 1, obj->showaslog);
                        }
                        OBJ(diskiograph_read) {
-                               if (obj->data.diskio) {
-                                       new_graph(p, obj->a, obj->b, obj->c, obj->d,
-                                                       obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
-                               } else {
-                                       new_graph(p, obj->a, obj->b, obj->c, obj->d,
-                                                       info.diskio_read_value, obj->e, 1, obj->showaslog);
-                               }
+                               new_graph(p, obj->a, obj->b, obj->c, obj->d,
+                                         obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
                        }
                        OBJ(diskiograph_write) {
-                               if (obj->data.diskio) {
-                                       new_graph(p, obj->a, obj->b, obj->c, obj->d,
-                                                       obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
-                               } else {
-                                       new_graph(p, obj->a, obj->b, obj->c, obj->d,
-                                                       info.diskio_write_value, obj->e, 1, obj->showaslog);
-                               }
+                               new_graph(p, obj->a, obj->b, obj->c, obj->d,
+                                         obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
                        }
                        OBJ(downspeed) {
                                spaced_print(p, p_max_size, "%d", 6, "downspeed",
@@ -5677,10 +5628,6 @@ static void generate_text(void)
        current_update_time = get_time();
 
        update_stuff();
-       /* fix diskio rates to b/s (use update_interval */
-       info.diskio_read_value /= update_interval;
-       info.diskio_write_value /= update_interval;
-       info.diskio_value /= update_interval;
 
        /* add things to the buffer */
 
index d87ab3c..96bc93e 100644 (file)
@@ -266,10 +266,6 @@ struct information {
 #endif
 
        short kflags;   /* kernel settings, see enum KFLAG */
-
-       unsigned int diskio_value;
-       unsigned int diskio_read_value;
-       unsigned int diskio_write_value;
 };
 
 /* needed by linux.c and top.c -> outsource somewhere */
index 60b6352..af9c35a 100644 (file)
@@ -54,7 +54,7 @@ struct diskio_stat *diskio_stats = diskio_stats_;
 void clear_diskio_stats(void)
 {
        unsigned i;
-       for(i = 0; i < MAX_DISKIO_STATS; i++) {
+       for(i = 1; i < MAX_DISKIO_STATS; i++) {
                if (diskio_stats[i].dev) {
                        free(diskio_stats[i].dev);
                        diskio_stats[i].dev = 0;
@@ -70,8 +70,12 @@ struct diskio_stat *prepare_diskio_stat(const char *s)
        int found = 0;
        char device[text_buffer_size], fbuf[text_buffer_size];
        static int rep = 0;
+
+       if (!s)
+               return &diskio_stats[0];
+
        /* lookup existing or get new */
-       for (i = 0; i < MAX_DISKIO_STATS; i++) {
+       for (i = 1; i < MAX_DISKIO_STATS; i++) {
                if (diskio_stats[i].dev) {
                        if (strcmp(diskio_stats[i].dev, s) == 0) {
                                return &diskio_stats[i];
@@ -145,7 +149,8 @@ void update_diskio(void)
        int tot, tot_read, tot_write;
 
        if (!(fp = open_file("/proc/diskstats", &rep))) {
-               info.diskio_value = 0;
+
+               diskio_stats[0].current = 0;
                return;
        }
 
@@ -171,7 +176,7 @@ void update_diskio(void)
                                continue;
                        }
                }
-               for (i = 0; i < MAX_DISKIO_STATS; i++) {
+               for (i = 1; i < MAX_DISKIO_STATS; i++) {
                        if (diskio_stats[i].dev &&
                                        strncmp(devbuf, diskio_stats[i].dev, text_buffer_size) == 0) {
                                diskio_stats[i].current =
@@ -222,9 +227,9 @@ void update_diskio(void)
        last_read = current_read;
        last_write = current_write;
 
-       info.diskio_value = tot;
-       info.diskio_read_value = tot_read;
-       info.diskio_write_value = tot_write;
+       diskio_stats[0].current = tot;
+       diskio_stats[0].current_read = tot_read;
+       diskio_stats[0].current_write = tot_write;
 
        fclose(fp);
 }