1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "conky.h" /* text_buffer_size */
38 #include "text_object.h"
43 /* this is the root of all per disk stats,
44 * also containing the totals. */
45 struct diskio_stat stats = {
47 .sample = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
48 .sample_read = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
49 .sample_write = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
54 .last_read = UINT_MAX,
55 .last_write = UINT_MAX,
58 void clear_diskio_stats(void)
60 struct diskio_stat *cur;
63 stats.next = stats.next->next;
70 struct diskio_stat *prepare_diskio_stat(const char *s)
73 char stat_name[text_buffer_size], device_name[text_buffer_size];
74 struct diskio_stat *cur = &stats;
79 strncpy(device_name, s, text_buffer_size);
81 snprintf(stat_name, text_buffer_size, "/dev/%s", device_name);
83 if (stat(stat_name, &sb)) {
84 NORM_ERR("diskio device '%s' does not exist", s);
90 if (!strcmp(cur->dev, device_name)) {
95 /* no existing found, make a new one */
96 cur->next = calloc(1, sizeof(struct diskio_stat));
98 cur->dev = strndup(device_name, text_buffer_size);
100 cur->last_read = UINT_MAX;
101 cur->last_write = UINT_MAX;
106 void parse_diskio_arg(struct text_object *obj, const char *arg)
108 obj->data.opaque = prepare_diskio_stat(arg);
111 /* dir indicates the direction:
116 static void print_diskio_dir(struct text_object *obj, int dir, char *p, int p_max_size)
118 struct diskio_stat *diskio = obj->data.opaque;
125 val = diskio->current_read;
127 val = diskio->current;
129 val = diskio->current_write;
131 /* TODO: move this correction from kB to kB/s elsewhere
132 * (or get rid of it??) */
133 human_readable((val / update_interval) * 1024LL, p, p_max_size);
136 void print_diskio(struct text_object *obj, char *p, int p_max_size)
138 print_diskio_dir(obj, 0, p, p_max_size);
141 void print_diskio_read(struct text_object *obj, char *p, int p_max_size)
143 print_diskio_dir(obj, -1, p, p_max_size);
146 void print_diskio_write(struct text_object *obj, char *p, int p_max_size)
148 print_diskio_dir(obj, 1, p, p_max_size);
152 void parse_diskiograph_arg(struct text_object *obj, const char *arg)
155 buf = scan_graph(obj, arg, 0);
157 obj->data.opaque = prepare_diskio_stat(dev_name(buf));
162 static void print_diskiograph_dir(struct text_object *obj, int dir, char *p, int p_max_size)
164 struct diskio_stat *diskio = obj->data.opaque;
174 val = diskio->current_read;
176 val = diskio->current;
178 val = diskio->current_write;
180 new_graph(obj, p, p_max_size, val);
183 void print_diskiograph(struct text_object *obj, char *p, int p_max_size)
185 print_diskiograph_dir(obj, 0, p, p_max_size);
188 void print_diskiograph_read(struct text_object *obj, char *p, int p_max_size)
190 print_diskiograph_dir(obj, -1, p, p_max_size);
193 void print_diskiograph_write(struct text_object *obj, char *p, int p_max_size)
195 print_diskiograph_dir(obj, 1, p, p_max_size);
199 void update_diskio_values(struct diskio_stat *ds,
200 unsigned int reads, unsigned int writes)
203 double sum=0, sum_r=0, sum_w=0;
205 if (reads < ds->last_read || writes < ds->last_write) {
206 /* counter overflow or reset - rebase to sane values */
207 ds->last = reads+writes;
208 ds->last_read = reads;
209 ds->last_write = writes;
211 /* since the values in /proc/diskstats are absolute, we have to subtract
212 * our last reading. The numbers stand for "sectors read", and we therefore
213 * have to divide by two to get KB */
214 ds->sample_read[0] = (reads - ds->last_read) / 2;
215 ds->sample_write[0] = (writes - ds->last_write) / 2;
216 ds->sample[0] = ds->sample_read[0] + ds->sample_write[0];
218 /* compute averages */
219 for (i = 0; i < (signed) info.diskio_avg_samples; i++) {
220 sum += ds->sample[i];
221 sum_r += ds->sample_read[i];
222 sum_w += ds->sample_write[i];
224 ds->current = sum / (double) info.diskio_avg_samples;
225 ds->current_read = sum_r / (double) info.diskio_avg_samples;
226 ds->current_write = sum_w / (double) info.diskio_avg_samples;
228 /* shift sample history */
229 for (i = info.diskio_avg_samples-1; i > 0; i--) {
230 ds->sample[i] = ds->sample[i-1];
231 ds->sample_read[i] = ds->sample_read[i-1];
232 ds->sample_write[i] = ds->sample_write[i-1];
236 ds->last_read = reads;
237 ds->last_write = writes;
238 ds->last = ds->last_read + ds->last_write;