1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
33 #include "conky.h" /* text_buffer_size */
41 /* this is the root of all per disk stats,
42 * also containing the totals. */
43 struct diskio_stat stats = {
45 .sample = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
46 .sample_read = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
47 .sample_write = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
52 .last_read = UINT_MAX,
53 .last_write = UINT_MAX,
56 void clear_diskio_stats(void)
58 struct diskio_stat *cur;
61 stats.next = stats.next->next;
68 struct diskio_stat *prepare_diskio_stat(const char *s)
71 char stat_name[text_buffer_size], device_name[text_buffer_size];
72 struct diskio_stat *cur = &stats;
77 #if defined(__FreeBSD__)
78 if (strncmp(s, "/dev/", 5) == 0) {
79 // supplied a /dev/device arg, so cut off the /dev part
80 strncpy(device_name, s + 5, text_buffer_size);
83 strncpy(device_name, s, text_buffer_size);
85 snprintf(stat_name, text_buffer_size, "/dev/%s", device_name);
87 if (stat(stat_name, &sb)) {
88 ERR("diskio device '%s' does not exist", s);
95 if (!strcmp(cur->dev, device_name)) {
100 /* no existing found, make a new one */
101 cur->next = calloc(1, sizeof(struct diskio_stat));
103 cur->dev = strndup(device_name, text_buffer_size);
104 cur->last = UINT_MAX;
105 cur->last_read = UINT_MAX;
106 cur->last_write = UINT_MAX;
111 void update_diskio_values(struct diskio_stat *ds,
112 unsigned int reads, unsigned int writes)
115 double sum=0, sum_r=0, sum_w=0;
117 if (reads < ds->last_read || writes < ds->last_write) {
118 /* counter overflow or reset - rebase to sane values */
119 ds->last = reads+writes;
120 ds->last_read = reads;
121 ds->last_write = writes;
123 /* since the values in /proc/diskstats are absolute, we have to substract
124 * our last reading. The numbers stand for "sectors read", and we therefore
125 * have to divide by two to get KB */
126 ds->sample_read[0] = (reads - ds->last_read) / 2;
127 ds->sample_write[0] = (writes - ds->last_write) / 2;
128 ds->sample[0] = ds->sample_read[0] + ds->sample_write[0];
130 /* compute averages */
131 for (i = 0; i < (signed) info.diskio_avg_samples; i++) {
132 sum += ds->sample[i];
133 sum_r += ds->sample_read[i];
134 sum_w += ds->sample_write[i];
136 ds->current = sum / (double) info.diskio_avg_samples;
137 ds->current_read = sum_r / (double) info.diskio_avg_samples;
138 ds->current_write = sum_w / (double) info.diskio_avg_samples;
140 /* shift sample history */
141 for (i = info.diskio_avg_samples-1; i > 0; i--) {
142 ds->sample[i] = ds->sample[i-1];
143 ds->sample_read[i] = ds->sample_read[i-1];
144 ds->sample_write[i] = ds->sample_write[i-1];
148 ds->last_read = reads;
149 ds->last_write = writes;
150 ds->last = ds->last_read + ds->last_write;