2009-07-28 21:44:22 +00:00
|
|
|
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
2009-09-12 10:50:51 +00:00
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
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
|
2009-03-30 04:55:30 +00:00
|
|
|
* Copyright (c) 2005-2009 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>
|
2009-05-14 22:52:18 +00:00
|
|
|
#include <sys/stat.h>
|
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. */
|
2009-05-14 22:52:18 +00:00
|
|
|
struct diskio_stat stats = {
|
2008-12-25 15:36:29 +00:00
|
|
|
.next = NULL,
|
2009-05-01 23:03:59 +00:00
|
|
|
.sample = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
|
|
|
|
.sample_read = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
|
|
|
|
.sample_write = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
|
2008-12-25 15:36:29 +00:00
|
|
|
.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;
|
2009-05-28 13:42:27 +00:00
|
|
|
if (cur->dev)
|
|
|
|
free(cur->dev);
|
2008-12-25 15:36:29 +00:00
|
|
|
free(cur);
|
2008-02-09 02:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct diskio_stat *prepare_diskio_stat(const char *s)
|
|
|
|
{
|
2009-05-14 22:52:18 +00:00
|
|
|
struct stat sb;
|
|
|
|
char stat_name[text_buffer_size], device_name[text_buffer_size];
|
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
|
|
|
|
2009-05-14 22:52:18 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
if (strncmp(s, "/dev/", 5) == 0) {
|
|
|
|
// supplied a /dev/device arg, so cut off the /dev part
|
|
|
|
strncpy(device_name, s + 5, text_buffer_size);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
strncpy(device_name, s, text_buffer_size);
|
|
|
|
|
|
|
|
snprintf(stat_name, text_buffer_size, "/dev/%s", device_name);
|
|
|
|
|
|
|
|
if (stat(stat_name, &sb)) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("diskio device '%s' does not exist", s);
|
2009-05-14 22:52:18 +00:00
|
|
|
}
|
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
/* lookup existing */
|
|
|
|
while (cur->next) {
|
|
|
|
cur = cur->next;
|
2009-05-14 22:52:18 +00:00
|
|
|
if (!strcmp(cur->dev, device_name)) {
|
2008-12-25 15:36:29 +00:00
|
|
|
return cur;
|
2009-05-14 22:52:18 +00:00
|
|
|
}
|
2008-02-09 02:21:06 +00:00
|
|
|
}
|
2008-12-25 15:36:29 +00:00
|
|
|
|
|
|
|
/* no existing found, make a new one */
|
2009-05-14 22:52:18 +00:00
|
|
|
cur->next = calloc(1, sizeof(struct diskio_stat));
|
2008-12-25 15:36:29 +00:00
|
|
|
cur = cur->next;
|
2009-05-14 22:52:18 +00:00
|
|
|
cur->dev = strndup(device_name, text_buffer_size);
|
2008-12-25 15:36:29 +00:00
|
|
|
cur->last = UINT_MAX;
|
|
|
|
cur->last_read = UINT_MAX;
|
|
|
|
cur->last_write = UINT_MAX;
|
2009-05-14 22:52:18 +00:00
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2009-05-14 22:52:18 +00:00
|
|
|
void update_diskio_values(struct diskio_stat *ds,
|
2008-12-25 15:36:29 +00:00
|
|
|
unsigned int reads, unsigned int writes)
|
|
|
|
{
|
2009-05-01 23:03:59 +00:00
|
|
|
int i;
|
|
|
|
double sum=0, sum_r=0, sum_w=0;
|
|
|
|
|
2008-12-25 15:36:29 +00:00
|
|
|
if (reads < ds->last_read || writes < ds->last_write) {
|
|
|
|
/* counter overflow or reset - rebase to sane values */
|
2009-05-01 23:03:59 +00:00
|
|
|
ds->last = reads+writes;
|
|
|
|
ds->last_read = reads;
|
|
|
|
ds->last_write = writes;
|
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 */
|
2009-05-01 23:03:59 +00:00
|
|
|
ds->sample_read[0] = (reads - ds->last_read) / 2;
|
|
|
|
ds->sample_write[0] = (writes - ds->last_write) / 2;
|
|
|
|
ds->sample[0] = ds->sample_read[0] + ds->sample_write[0];
|
|
|
|
|
|
|
|
/* compute averages */
|
|
|
|
for (i = 0; i < (signed) info.diskio_avg_samples; i++) {
|
|
|
|
sum += ds->sample[i];
|
|
|
|
sum_r += ds->sample_read[i];
|
|
|
|
sum_w += ds->sample_write[i];
|
|
|
|
}
|
|
|
|
ds->current = sum / (double) info.diskio_avg_samples;
|
|
|
|
ds->current_read = sum_r / (double) info.diskio_avg_samples;
|
|
|
|
ds->current_write = sum_w / (double) info.diskio_avg_samples;
|
|
|
|
|
|
|
|
/* shift sample history */
|
|
|
|
for (i = info.diskio_avg_samples-1; i > 0; i--) {
|
|
|
|
ds->sample[i] = ds->sample[i-1];
|
|
|
|
ds->sample_read[i] = ds->sample_read[i-1];
|
|
|
|
ds->sample_write[i] = ds->sample_write[i-1];
|
|
|
|
}
|
2008-12-25 15:36:29 +00:00
|
|
|
|
2009-05-01 23:03:59 +00:00
|
|
|
/* save last */
|
2008-12-25 15:36:29 +00:00
|
|
|
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
|
|
|
|