2 * Conky, a system monitor, based on torsmo
4 * Any original torsmo code is licensed under the BSD license
6 * All code written since the fork of torsmo is licensed under the GPL
8 * Please see COPYING for details
10 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
11 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
13 * All rights reserved.
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "conky.h" /* text_buffer_size */
36 /* The following ifdefs were adapted from gkrellm */
37 #include <linux/major.h>
39 #if !defined(MD_MAJOR)
43 #if !defined(LVM_BLK_MAJOR)
44 #define LVM_BLK_MAJOR 58
47 #if !defined(NBD_MAJOR)
51 /* this is the root of all per disk stats,
52 * also containing the totals. */
53 static struct diskio_stat stats = {
59 .last_read = UINT_MAX,
60 .last_write = UINT_MAX,
63 void clear_diskio_stats(void)
65 struct diskio_stat *cur;
68 stats.next = stats.next->next;
73 struct diskio_stat *prepare_diskio_stat(const char *s)
75 struct diskio_stat *cur = &stats;
83 if (!strcmp(cur->dev, s))
87 /* no existing found, make a new one */
88 cur->next = malloc(sizeof(struct diskio_stat));
90 memset(cur, 0, sizeof(struct diskio_stat));
91 cur->dev = strndup(s, text_buffer_size);
93 cur->last_read = UINT_MAX;
94 cur->last_write = UINT_MAX;
98 static void update_diskio_values(struct diskio_stat *ds,
99 unsigned int reads, unsigned int writes)
101 if (reads < ds->last_read || writes < ds->last_write) {
102 /* counter overflow or reset - rebase to sane values */
107 /* since the values in /proc/diskstats are absolute, we have to substract
108 * our last reading. The numbers stand for "sectors read", and we therefore
109 * have to divide by two to get KB */
110 ds->current_read = (reads - ds->last_read) / 2;
111 ds->current_write = (writes - ds->last_write) / 2;
112 ds->current = ds->current_read + ds->current_write;
114 ds->last_read = reads;
115 ds->last_write = writes;
116 ds->last = ds->last_read + ds->last_write;
119 void update_diskio(void)
124 struct diskio_stat *cur;
125 char buf[512], devbuf[64];
126 unsigned int major, minor;
127 unsigned int reads, writes;
128 unsigned int total_reads, total_writes;
132 stats.current_read = 0;
133 stats.current_write = 0;
135 if (!(fp = open_file("/proc/diskstats", &rep))) {
139 /* read reads and writes from all disks (minor = 0), including cd-roms
140 * and floppies, and sum them up */
141 while (fgets(buf, 512, fp)) {
142 col_count = sscanf(buf, "%u %u %s %*u %*u %u %*u %*u %*u %u", &major,
143 &minor, devbuf, &reads, &writes);
144 /* ignore subdevices (they have only 3 matching entries in their line)
145 * and virtual devices (LVM, network block devices, RAM disks, Loopback)
147 * XXX: ignore devices which are part of a SW RAID (MD_MAJOR) */
148 if (col_count == 5 && major != LVM_BLK_MAJOR && major != NBD_MAJOR
149 && major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
150 total_reads += reads;
151 total_writes += writes;
153 col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
154 &major, &minor, devbuf, &reads, &writes);
155 if (col_count != 5) {
160 while (cur && strcmp(devbuf, cur->dev))
164 update_diskio_values(cur, reads, writes);
166 update_diskio_values(&stats, total_reads, total_writes);