1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-28 17:48:39 +00:00

simplify diskio stats

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.
This commit is contained in:
Phil Sutter 2008-12-16 02:32:30 +01:00
parent 9a85812e06
commit 0720a4fbc9
3 changed files with 37 additions and 89 deletions

View 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.: * which gets overwritten in consecutive calls. I.e.:
* this function is NOT reentrant. * 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 */ static char buf[255]; /* should be enough for pathnames */
ssize_t buflen; ssize_t buflen;
if (!path)
return NULL;
#define DEV_NAME(x) \ #define DEV_NAME(x) \
x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
if ((buflen = readlink(path, buf, 254)) == -1) if ((buflen = readlink(path, buf, 254)) == -1)
@ -1889,53 +1892,32 @@ static struct text_object *construct_text_object(const char *s,
} }
#if defined(__linux__) #if defined(__linux__)
END OBJ(diskio, INFO_DISKIO) END OBJ(diskio, INFO_DISKIO)
if (arg) { obj->data.diskio = prepare_diskio_stat(dev_name(arg));
obj->data.diskio = prepare_diskio_stat(dev_name(arg));
} else {
obj->data.diskio = NULL;
}
END OBJ(diskio_read, INFO_DISKIO) END OBJ(diskio_read, INFO_DISKIO)
if (arg) { obj->data.diskio = prepare_diskio_stat(dev_name(arg));
obj->data.diskio = prepare_diskio_stat(dev_name(arg));
} else {
obj->data.diskio = NULL;
}
END OBJ(diskio_write, INFO_DISKIO) END OBJ(diskio_write, INFO_DISKIO)
if (arg) { obj->data.diskio = prepare_diskio_stat(dev_name(arg));
obj->data.diskio = prepare_diskio_stat(dev_name(arg));
} else {
obj->data.diskio = NULL;
}
END OBJ(diskiograph, INFO_DISKIO) END OBJ(diskiograph, INFO_DISKIO)
char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d, char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->showaslog); &obj->e, &obj->showaslog);
if (buf) { obj->data.diskio = prepare_diskio_stat(buf);
obj->data.diskio = prepare_diskio_stat(buf); if (buf)
free(buf); free(buf);
} else {
obj->data.diskio = NULL;
}
END OBJ(diskiograph_read, INFO_DISKIO) END OBJ(diskiograph_read, INFO_DISKIO)
char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d, char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->showaslog); &obj->e, &obj->showaslog);
if (buf) { obj->data.diskio = prepare_diskio_stat(buf);
obj->data.diskio = prepare_diskio_stat(buf); if (buf)
free(buf); free(buf);
} else {
obj->data.diskio = NULL;
}
END OBJ(diskiograph_write, INFO_DISKIO) END OBJ(diskiograph_write, INFO_DISKIO)
char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d, char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->showaslog); &obj->e, &obj->showaslog);
if (buf) { obj->data.diskio = prepare_diskio_stat(buf);
obj->data.diskio = prepare_diskio_stat(buf); if (buf)
free(buf); free(buf);
} else {
obj->data.diskio = NULL;
}
#endif #endif
END OBJ(color, 0) END OBJ(color, 0)
#ifdef X11 #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 /* TODO: move this correction from kB to kB/s elsewhere
* (or get rid of it??) */ * (or get rid of it??) */
OBJ(diskio) { OBJ(diskio) {
if (obj->data.diskio) { human_readable((obj->data.diskio->current / update_interval) * 1024LL,
human_readable( p, p_max_size, "diskio");
(obj->data.diskio->current / update_interval) * 1024LL,
p, p_max_size, "diskio");
} else {
human_readable(info.diskio_value * 1024LL, p, p_max_size,
"diskio");
}
} }
OBJ(diskio_write) { OBJ(diskio_write) {
if (obj->data.diskio) { human_readable((obj->data.diskio->current / update_interval) * 1024LL,
human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size, p, p_max_size, "diskio_write");
"diskio_write");
} else {
human_readable(info.diskio_write_value * 1024LL, p, p_max_size,
"diskio_write");
}
} }
OBJ(diskio_read) { OBJ(diskio_read) {
if (obj->data.diskio) { human_readable((obj->data.diskio->current / update_interval) * 1024LL,
human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size, p, p_max_size, "diskio_read");
"diskio_read");
} else {
human_readable(info.diskio_read_value * 1024LL, p, p_max_size,
"diskio_read");
}
} }
OBJ(diskiograph) { OBJ(diskiograph) {
if (obj->data.diskio) { new_graph(p, obj->a, obj->b, obj->c, obj->d,
new_graph(p, obj->a, obj->b, obj->c, obj->d, obj->data.diskio->current, obj->e, 1, obj->showaslog);
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);
}
} }
OBJ(diskiograph_read) { OBJ(diskiograph_read) {
if (obj->data.diskio) { new_graph(p, obj->a, obj->b, obj->c, obj->d,
new_graph(p, obj->a, obj->b, obj->c, obj->d, obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
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);
}
} }
OBJ(diskiograph_write) { OBJ(diskiograph_write) {
if (obj->data.diskio) { new_graph(p, obj->a, obj->b, obj->c, obj->d,
new_graph(p, obj->a, obj->b, obj->c, obj->d, obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
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);
}
} }
OBJ(downspeed) { OBJ(downspeed) {
spaced_print(p, p_max_size, "%d", 6, "downspeed", spaced_print(p, p_max_size, "%d", 6, "downspeed",
@ -5677,10 +5628,6 @@ static void generate_text(void)
current_update_time = get_time(); current_update_time = get_time();
update_stuff(); 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 */ /* add things to the buffer */

View File

@ -266,10 +266,6 @@ struct information {
#endif #endif
short kflags; /* kernel settings, see enum KFLAG */ 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 */ /* needed by linux.c and top.c -> outsource somewhere */

View File

@ -54,7 +54,7 @@ struct diskio_stat *diskio_stats = diskio_stats_;
void clear_diskio_stats(void) void clear_diskio_stats(void)
{ {
unsigned i; unsigned i;
for(i = 0; i < MAX_DISKIO_STATS; i++) { for(i = 1; i < MAX_DISKIO_STATS; i++) {
if (diskio_stats[i].dev) { if (diskio_stats[i].dev) {
free(diskio_stats[i].dev); free(diskio_stats[i].dev);
diskio_stats[i].dev = 0; diskio_stats[i].dev = 0;
@ -70,8 +70,12 @@ struct diskio_stat *prepare_diskio_stat(const char *s)
int found = 0; int found = 0;
char device[text_buffer_size], fbuf[text_buffer_size]; char device[text_buffer_size], fbuf[text_buffer_size];
static int rep = 0; static int rep = 0;
if (!s)
return &diskio_stats[0];
/* lookup existing or get new */ /* 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 (diskio_stats[i].dev) {
if (strcmp(diskio_stats[i].dev, s) == 0) { if (strcmp(diskio_stats[i].dev, s) == 0) {
return &diskio_stats[i]; return &diskio_stats[i];
@ -145,7 +149,8 @@ void update_diskio(void)
int tot, tot_read, tot_write; int tot, tot_read, tot_write;
if (!(fp = open_file("/proc/diskstats", &rep))) { if (!(fp = open_file("/proc/diskstats", &rep))) {
info.diskio_value = 0;
diskio_stats[0].current = 0;
return; return;
} }
@ -171,7 +176,7 @@ void update_diskio(void)
continue; continue;
} }
} }
for (i = 0; i < MAX_DISKIO_STATS; i++) { for (i = 1; i < MAX_DISKIO_STATS; i++) {
if (diskio_stats[i].dev && if (diskio_stats[i].dev &&
strncmp(devbuf, diskio_stats[i].dev, text_buffer_size) == 0) { strncmp(devbuf, diskio_stats[i].dev, text_buffer_size) == 0) {
diskio_stats[i].current = diskio_stats[i].current =
@ -222,9 +227,9 @@ void update_diskio(void)
last_read = current_read; last_read = current_read;
last_write = current_write; last_write = current_write;
info.diskio_value = tot; diskio_stats[0].current = tot;
info.diskio_read_value = tot_read; diskio_stats[0].current_read = tot_read;
info.diskio_write_value = tot_write; diskio_stats[0].current_write = tot_write;
fclose(fp); fclose(fp);
} }