2018-05-12 16:03:00 +00:00
|
|
|
/*
|
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
|
2021-02-27 15:14:19 +00:00
|
|
|
* Copyright (c) 2005-2021 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "diskio.h"
|
|
|
|
#include <sys/stat.h>
|
2018-05-12 23:26:31 +00:00
|
|
|
#include <cstdlib>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <vector>
|
|
|
|
#include "common.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "config.h"
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "conky.h" /* text_buffer_size */
|
2009-10-07 20:44:17 +00:00
|
|
|
#include "core.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "logging.h"
|
2009-10-07 20:44:17 +00:00
|
|
|
#include "specials.h"
|
|
|
|
#include "text_object.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. */
|
2010-01-04 17:06:14 +00:00
|
|
|
struct diskio_stat stats;
|
2008-02-09 02:21:06 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
void clear_diskio_stats() {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct diskio_stat *cur;
|
2018-05-12 23:26:31 +00:00
|
|
|
while (stats.next != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
cur = stats.next;
|
|
|
|
stats.next = stats.next->next;
|
|
|
|
free_and_zero(cur->dev);
|
|
|
|
free(cur);
|
|
|
|
}
|
2008-02-09 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
struct diskio_stat *prepare_diskio_stat(const char *s) {
|
2018-05-12 23:26:31 +00:00
|
|
|
struct stat sb {};
|
2018-05-12 16:03:00 +00:00
|
|
|
std::vector<char> stat_name(text_buffer_size.get(*state)),
|
|
|
|
device_name(text_buffer_size.get(*state)),
|
|
|
|
device_s(text_buffer_size.get(*state));
|
|
|
|
struct diskio_stat *cur = &stats;
|
|
|
|
char *rpbuf;
|
2018-08-10 14:47:19 +00:00
|
|
|
char rpbuf2[256];
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2019-02-23 19:40:34 +00:00
|
|
|
if (s == nullptr) { return &stats; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
if (strncmp(s, "label:", 6) == 0) {
|
|
|
|
snprintf(&(device_name[0]), text_buffer_size.get(*state),
|
|
|
|
"/dev/disk/by-label/%s", s + 6);
|
2018-08-10 14:47:19 +00:00
|
|
|
rpbuf = realpath(&(device_name[0]), nullptr);
|
|
|
|
} else if (0 == (strncmp(s, "partuuid:", 9))) {
|
|
|
|
snprintf(&(device_name[0]), text_buffer_size.get(*state),
|
|
|
|
"/dev/disk/by-partuuid/%s", s + 9);
|
2018-05-12 23:26:31 +00:00
|
|
|
rpbuf = realpath(&device_name[0], nullptr);
|
2018-08-10 14:47:19 +00:00
|
|
|
snprintf(rpbuf2, 255, "%s", rpbuf);
|
2018-05-12 16:03:00 +00:00
|
|
|
} else {
|
2018-05-12 23:26:31 +00:00
|
|
|
rpbuf = realpath(s, nullptr);
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (rpbuf != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
strncpy(&device_s[0], rpbuf, text_buffer_size.get(*state));
|
|
|
|
free(rpbuf);
|
|
|
|
} else {
|
|
|
|
strncpy(&device_s[0], s, text_buffer_size.get(*state));
|
|
|
|
}
|
2015-02-19 19:42:50 +00:00
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__linux__)
|
2018-05-12 16:03:00 +00:00
|
|
|
if (strncmp(&device_s[0], "/dev/", 5) == 0) {
|
|
|
|
device_s.erase(device_s.begin(), device_s.begin() + 5);
|
|
|
|
}
|
2009-05-14 22:52:18 +00:00
|
|
|
#endif
|
2018-05-12 16:03:00 +00:00
|
|
|
strncpy(&(device_name[0]), &device_s[0], text_buffer_size.get(*state));
|
2009-05-14 22:52:18 +00:00
|
|
|
|
2018-01-19 14:00:42 +00:00
|
|
|
#if !defined(__sun)
|
2018-05-12 16:03:00 +00:00
|
|
|
/*
|
|
|
|
* On Solaris we currently don't use the name of disk's special file so
|
|
|
|
* this test is useless.
|
|
|
|
*/
|
|
|
|
|
2018-08-10 14:47:19 +00:00
|
|
|
if (strncmp(s, "label:", 6) == 0) {
|
|
|
|
snprintf(&(stat_name[0]), text_buffer_size.get(*state), "/dev/%s",
|
2019-02-23 19:40:34 +00:00
|
|
|
&(device_name[0]));
|
2018-08-10 14:47:19 +00:00
|
|
|
if ((stat(&(stat_name[0]), &sb) != 0) || !S_ISBLK(sb.st_mode)) {
|
|
|
|
NORM_ERR("diskio device '%s' does not exist", &device_s[0]);
|
|
|
|
}
|
|
|
|
} else if ((0 == (strncmp(s, "partuuid:", 9))) &&
|
2019-02-23 19:40:34 +00:00
|
|
|
((stat(rpbuf2, &sb) != 0) || !S_ISBLK(sb.st_mode))) {
|
|
|
|
NORM_ERR("diskio device '%s' does not exist", &device_s[0]);
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2018-08-10 14:47:19 +00:00
|
|
|
|
2018-01-19 14:00:42 +00:00
|
|
|
#endif
|
2009-05-14 22:52:18 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
/* lookup existing */
|
2018-05-12 23:26:31 +00:00
|
|
|
while (cur->next != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
cur = cur->next;
|
2019-02-23 19:40:34 +00:00
|
|
|
if (strcmp(cur->dev, &(device_name[0])) == 0) { return cur; }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* no existing found, make a new one */
|
|
|
|
cur->next = new diskio_stat;
|
|
|
|
cur = cur->next;
|
|
|
|
cur->dev = strndup(&(device_s[0]), text_buffer_size.get(*state));
|
|
|
|
cur->last = UINT_MAX;
|
|
|
|
cur->last_read = UINT_MAX;
|
|
|
|
cur->last_write = UINT_MAX;
|
|
|
|
|
|
|
|
return cur;
|
2008-12-25 15:36:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_diskio_arg(struct text_object *obj, const char *arg) {
|
|
|
|
obj->data.opaque = prepare_diskio_stat(arg);
|
2009-10-07 20:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* dir indicates the direction:
|
|
|
|
* -1: read
|
|
|
|
* 0: read + write
|
|
|
|
* 1: write
|
|
|
|
*/
|
2018-05-12 16:03:00 +00:00
|
|
|
static void print_diskio_dir(struct text_object *obj, int dir, char *p,
|
2018-08-07 23:22:23 +00:00
|
|
|
unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *diskio = static_cast<struct diskio_stat *>(obj->data.opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
double val;
|
|
|
|
|
2019-02-23 19:40:34 +00:00
|
|
|
if (diskio == nullptr) { return; }
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (dir < 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
val = diskio->current_read;
|
2018-05-12 23:26:31 +00:00
|
|
|
} else if (dir == 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
val = diskio->current;
|
2018-05-12 23:26:31 +00:00
|
|
|
} else {
|
2018-05-12 16:03:00 +00:00
|
|
|
val = diskio->current_write;
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
/* TODO: move this correction from kB to kB/s elsewhere
|
|
|
|
* (or get rid of it??) */
|
|
|
|
human_readable((val / active_update_interval()) * 1024LL, p, p_max_size);
|
2009-10-07 20:44:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_diskio(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
print_diskio_dir(obj, 0, p, p_max_size);
|
2009-11-08 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 19:40:34 +00:00
|
|
|
void print_diskio_read(struct text_object *obj, char *p,
|
|
|
|
unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
print_diskio_dir(obj, -1, p, p_max_size);
|
2009-11-08 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 19:40:34 +00:00
|
|
|
void print_diskio_write(struct text_object *obj, char *p,
|
|
|
|
unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
print_diskio_dir(obj, 1, p, p_max_size);
|
2009-11-08 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_X11
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_diskiograph_arg(struct text_object *obj, const char *arg) {
|
2018-05-12 23:26:31 +00:00
|
|
|
char *buf = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
buf = scan_graph(obj, arg, 0);
|
2009-10-07 20:44:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
obj->data.opaque = prepare_diskio_stat(dev_name(buf));
|
|
|
|
free_and_zero(buf);
|
2009-10-07 20:44:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
double diskiographval(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *diskio = static_cast<struct diskio_stat *>(obj->data.opaque);
|
2009-10-07 20:44:17 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
return (diskio != nullptr ? diskio->current : 0);
|
2009-10-07 20:44:17 +00:00
|
|
|
}
|
2009-11-08 12:54:42 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
double diskiographval_read(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *diskio = static_cast<struct diskio_stat *>(obj->data.opaque);
|
2009-11-08 12:54:42 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
return (diskio != nullptr ? diskio->current_read : 0);
|
2009-11-08 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
double diskiographval_write(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *diskio = static_cast<struct diskio_stat *>(obj->data.opaque);
|
2009-11-23 23:17:52 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
return (diskio != nullptr ? diskio->current_write : 0);
|
2009-11-08 12:54:42 +00:00
|
|
|
}
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_X11 */
|
2009-10-07 20:44:17 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void update_diskio_values(struct diskio_stat *ds, unsigned int reads,
|
|
|
|
unsigned int writes) {
|
|
|
|
int i;
|
|
|
|
double sum = 0, sum_r = 0, sum_w = 0;
|
|
|
|
|
|
|
|
if (reads < ds->last_read || writes < ds->last_write) {
|
|
|
|
/* counter overflow or reset - rebase to sane values */
|
|
|
|
ds->last = reads + writes;
|
|
|
|
ds->last_read = reads;
|
|
|
|
ds->last_write = writes;
|
|
|
|
}
|
|
|
|
/* since the values in /proc/diskstats are absolute, we have to subtract
|
|
|
|
* our last reading. The numbers stand for "sectors read", and we therefore
|
|
|
|
* have to divide by two to get KB */
|
|
|
|
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 */
|
|
|
|
int samples = diskio_avg_samples.get(*state);
|
|
|
|
for (i = 0; i < samples; i++) {
|
|
|
|
sum += ds->sample[i];
|
|
|
|
sum_r += ds->sample_read[i];
|
|
|
|
sum_w += ds->sample_write[i];
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
ds->current = sum / static_cast<double>(samples);
|
|
|
|
ds->current_read = sum_r / static_cast<double>(samples);
|
|
|
|
ds->current_write = sum_w / static_cast<double>(samples);
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
/* shift sample history */
|
|
|
|
for (i = 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save last */
|
|
|
|
ds->last_read = reads;
|
|
|
|
ds->last_write = writes;
|
|
|
|
ds->last = ds->last_read + ds->last_write;
|
2008-02-09 02:21:06 +00:00
|
|
|
}
|