rewrite linux diskio code
[monky] / src / diskio.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * Any original torsmo code is licensed under the BSD license
5  *
6  * All code written since the fork of torsmo is licensed under the GPL
7  *
8  * Please see COPYING for details
9  *
10  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
11  * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
12  * (see AUTHORS)
13  * All rights reserved.
14  *
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.
19  *
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/>.
26  *
27  */
28
29 #include "config.h"
30 #include "conky.h"      /* text_buffer_size */
31 #include "logging.h"
32 #include "diskio.h"
33 #include "common.h"
34 #include <stdlib.h>
35 #include <limits.h>
36 /* The following ifdefs were adapted from gkrellm */
37 #include <linux/major.h>
38
39 #if !defined(MD_MAJOR)
40 #define MD_MAJOR 9
41 #endif
42
43 #if !defined(LVM_BLK_MAJOR)
44 #define LVM_BLK_MAJOR 58
45 #endif
46
47 #if !defined(NBD_MAJOR)
48 #define NBD_MAJOR 43
49 #endif
50
51 /* this is the root of all per disk stats,
52  * also containing the totals. */
53 static struct diskio_stat stats = {
54         .next = NULL,
55         .current = 0,
56         .current_read = 0,
57         .current_write = 0,
58         .last = UINT_MAX,
59         .last_read = UINT_MAX,
60         .last_write = UINT_MAX,
61 };
62
63 void clear_diskio_stats(void)
64 {
65         struct diskio_stat *cur;
66         while (stats.next) {
67                 cur = stats.next;
68                 stats.next = stats.next->next;
69                 free(cur);
70         }
71 }
72
73 struct diskio_stat *prepare_diskio_stat(const char *s)
74 {
75         struct diskio_stat *cur = &stats;
76
77         if (!s)
78                 return &stats;
79
80         /* lookup existing */
81         while (cur->next) {
82                 cur = cur->next;
83                 if (!strcmp(cur->dev, s))
84                         return cur;
85         }
86
87         /* no existing found, make a new one */
88         cur->next = malloc(sizeof(struct diskio_stat));
89         cur = cur->next;
90         memset(cur, 0, sizeof(struct diskio_stat));
91         cur->dev = strndup(s, text_buffer_size);
92         cur->last = UINT_MAX;
93         cur->last_read = UINT_MAX;
94         cur->last_write = UINT_MAX;
95         return cur;
96 }
97
98 static void update_diskio_values(struct diskio_stat *ds,
99                 unsigned int reads, unsigned int writes)
100 {
101         if (reads < ds->last_read || writes < ds->last_write) {
102                 /* counter overflow or reset - rebase to sane values */
103                 ds->last = 0;
104                 ds->last_read = 0;
105                 ds->last_write = 0;
106         }
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;
113
114         ds->last_read = reads;
115         ds->last_write = writes;
116         ds->last = ds->last_read + ds->last_write;
117 }
118
119 void update_diskio(void)
120 {
121         FILE *fp;
122         static int rep = 0;
123
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;
129         int col_count = 0;
130
131         stats.current = 0;
132         stats.current_read = 0;
133         stats.current_write = 0;
134
135         if (!(fp = open_file("/proc/diskstats", &rep))) {
136                 return;
137         }
138
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)
146                  *
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;
152                 } else {
153                         col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
154                                 &major, &minor, devbuf, &reads, &writes);
155                         if (col_count != 5) {
156                                 continue;
157                         }
158                 }
159                 cur = stats.next;
160                 while (cur && strcmp(devbuf, cur->dev))
161                         cur = cur->next;
162
163                 if (cur)
164                         update_diskio_values(cur, reads, writes);
165         }
166         update_diskio_values(&stats, total_reads, total_writes);
167         fclose(fp);
168 }
169