2008-02-09 02:21:06 +00:00
|
|
|
/*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2008-03-31 04:56:39 +00:00
|
|
|
* Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-09 02:21:06 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "conky.h" /* text_buffer_size */
|
|
|
|
#include "logging.h"
|
|
|
|
#include "diskio.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include <stdlib.h>
|
2008-03-29 12:44:29 +00:00
|
|
|
#include <limits.h>
|
2008-04-10 22:45:45 +00:00
|
|
|
/* The following ifdefs were adapted from gkrellm */
|
|
|
|
#include <linux/major.h>
|
|
|
|
|
|
|
|
#if !defined(MD_MAJOR)
|
|
|
|
#define MD_MAJOR 9
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(LVM_BLK_MAJOR)
|
|
|
|
#define LVM_BLK_MAJOR 58
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(NBD_MAJOR)
|
|
|
|
#define NBD_MAJOR 43
|
|
|
|
#endif
|
2008-02-09 02:21:06 +00:00
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
/* this is the root of all per disk stats,
|
|
|
|
* also containing the totals. */
|
|
|
|
static struct diskio_stat stats = {
|
|
|
|
.next = NULL,
|
|
|
|
.current = 0,
|
|
|
|
.current_read = 0,
|
|
|
|
.current_write = 0,
|
|
|
|
.last = UINT_MAX,
|
|
|
|
.last_read = UINT_MAX,
|
|
|
|
.last_write = UINT_MAX,
|
|
|
|
};
|
2008-02-09 02:21:06 +00:00
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
void clear_diskio_stats(void)
|
2008-02-09 02:21:06 +00:00
|
|
|
{
|
2008-12-25 15:36:29 +00:00
|
|
|
struct diskio_stat *cur;
|
|
|
|
while (stats.next) {
|
|
|
|
cur = stats.next;
|
|
|
|
stats.next = stats.next->next;
|
|
|
|
free(cur);
|
2008-02-09 02:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct diskio_stat *prepare_diskio_stat(const char *s)
|
|
|
|
{
|
2008-12-25 15:36:29 +00:00
|
|
|
struct diskio_stat *cur = &stats;
|
2008-12-16 01:32:30 +00:00
|
|
|
|
|
|
|
if (!s)
|
2008-12-25 15:36:29 +00:00
|
|
|
return &stats;
|
2008-12-16 01:32:30 +00:00
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
/* lookup existing */
|
|
|
|
while (cur->next) {
|
|
|
|
cur = cur->next;
|
|
|
|
if (!strcmp(cur->dev, s))
|
|
|
|
return cur;
|
2008-02-09 02:21:06 +00:00
|
|
|
}
|
2008-12-25 15:36:29 +00:00
|
|
|
|
|
|
|
/* no existing found, make a new one */
|
|
|
|
cur->next = malloc(sizeof(struct diskio_stat));
|
|
|
|
cur = cur->next;
|
|
|
|
memset(cur, 0, sizeof(struct diskio_stat));
|
|
|
|
cur->dev = strndup(s, text_buffer_size);
|
|
|
|
cur->last = UINT_MAX;
|
|
|
|
cur->last_read = UINT_MAX;
|
|
|
|
cur->last_write = UINT_MAX;
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_diskio_values(struct diskio_stat *ds,
|
|
|
|
unsigned int reads, unsigned int writes)
|
|
|
|
{
|
|
|
|
if (reads < ds->last_read || writes < ds->last_write) {
|
|
|
|
/* counter overflow or reset - rebase to sane values */
|
|
|
|
ds->last = 0;
|
|
|
|
ds->last_read = 0;
|
|
|
|
ds->last_write = 0;
|
2008-03-31 03:56:03 +00:00
|
|
|
}
|
2008-12-25 15:36:29 +00:00
|
|
|
/* since the values in /proc/diskstats are absolute, we have to substract
|
|
|
|
* our last reading. The numbers stand for "sectors read", and we therefore
|
|
|
|
* have to divide by two to get KB */
|
|
|
|
ds->current_read = (reads - ds->last_read) / 2;
|
|
|
|
ds->current_write = (writes - ds->last_write) / 2;
|
|
|
|
ds->current = ds->current_read + ds->current_write;
|
|
|
|
|
|
|
|
ds->last_read = reads;
|
|
|
|
ds->last_write = writes;
|
|
|
|
ds->last = ds->last_read + ds->last_write;
|
2008-02-09 02:21:06 +00:00
|
|
|
}
|
2008-04-10 22:45:45 +00:00
|
|
|
|
|
|
|
void update_diskio(void)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
static int rep = 0;
|
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
struct diskio_stat *cur;
|
2008-04-10 22:45:45 +00:00
|
|
|
char buf[512], devbuf[64];
|
|
|
|
unsigned int major, minor;
|
2008-12-25 15:36:29 +00:00
|
|
|
unsigned int reads, writes;
|
|
|
|
unsigned int total_reads, total_writes;
|
2008-04-10 22:45:45 +00:00
|
|
|
int col_count = 0;
|
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
stats.current = 0;
|
|
|
|
stats.current_read = 0;
|
|
|
|
stats.current_write = 0;
|
2008-12-16 01:32:30 +00:00
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
if (!(fp = open_file("/proc/diskstats", &rep))) {
|
2008-04-10 22:45:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read reads and writes from all disks (minor = 0), including cd-roms
|
|
|
|
* and floppies, and sum them up */
|
2008-12-25 14:06:38 +00:00
|
|
|
while (fgets(buf, 512, fp)) {
|
2008-04-10 22:45:45 +00:00
|
|
|
col_count = sscanf(buf, "%u %u %s %*u %*u %u %*u %*u %*u %u", &major,
|
|
|
|
&minor, devbuf, &reads, &writes);
|
|
|
|
/* ignore subdevices (they have only 3 matching entries in their line)
|
|
|
|
* and virtual devices (LVM, network block devices, RAM disks, Loopback)
|
|
|
|
*
|
|
|
|
* XXX: ignore devices which are part of a SW RAID (MD_MAJOR) */
|
|
|
|
if (col_count == 5 && major != LVM_BLK_MAJOR && major != NBD_MAJOR
|
|
|
|
&& major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
|
2008-12-25 15:36:29 +00:00
|
|
|
total_reads += reads;
|
|
|
|
total_writes += writes;
|
2008-04-10 22:45:45 +00:00
|
|
|
} else {
|
|
|
|
col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
|
|
|
|
&major, &minor, devbuf, &reads, &writes);
|
|
|
|
if (col_count != 5) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-12-25 15:36:29 +00:00
|
|
|
cur = stats.next;
|
|
|
|
while (cur && strcmp(devbuf, cur->dev))
|
|
|
|
cur = cur->next;
|
2008-04-10 22:45:45 +00:00
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
if (cur)
|
|
|
|
update_diskio_values(cur, reads, writes);
|
2008-04-10 22:45:45 +00:00
|
|
|
}
|
2008-12-25 15:36:29 +00:00
|
|
|
update_diskio_values(&stats, total_reads, total_writes);
|
2008-04-10 22:45:45 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|