d1bb74b29543496aaefab382326688266df428c1
[monky] / src / diskio.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  * (see AUTHORS)
14  * All rights reserved.
15  *
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.
20  *
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/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31
32 #include "config.h"
33 #include "conky.h"      /* text_buffer_size */
34 #include "logging.h"
35 #include "diskio.h"
36 #include "common.h"
37 #include <stdlib.h>
38 #include <limits.h>
39 #include <sys/stat.h>
40
41 /* this is the root of all per disk stats,
42  * also containing the totals. */
43 struct diskio_stat stats = {
44         .next = NULL,
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},
48         .current = 0,
49         .current_read = 0,
50         .current_write = 0,
51         .last = UINT_MAX,
52         .last_read = UINT_MAX,
53         .last_write = UINT_MAX,
54 };
55
56 void clear_diskio_stats(void)
57 {
58         struct diskio_stat *cur;
59         while (stats.next) {
60                 cur = stats.next;
61                 stats.next = stats.next->next;
62                 if (cur->dev)
63                         free(cur->dev);
64                 free(cur);
65         }
66 }
67
68 struct diskio_stat *prepare_diskio_stat(const char *s)
69 {
70         struct stat sb;
71         char stat_name[text_buffer_size], device_name[text_buffer_size];
72         struct diskio_stat *cur = &stats;
73
74         if (!s)
75                 return &stats;
76
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);
81         } else
82 #endif
83         strncpy(device_name, s, text_buffer_size);
84
85         snprintf(stat_name, text_buffer_size, "/dev/%s", device_name);
86
87         if (stat(stat_name, &sb)) {
88                 NORM_ERR("diskio device '%s' does not exist", s);
89         }
90
91         /* lookup existing */
92         while (cur->next) {
93                 cur = cur->next;
94                 if (!strcmp(cur->dev, device_name)) {
95                         return cur;
96                 }
97         }
98
99         /* no existing found, make a new one */
100         cur->next = calloc(1, sizeof(struct diskio_stat));
101         cur = cur->next;
102         cur->dev = strndup(device_name, text_buffer_size);
103         cur->last = UINT_MAX;
104         cur->last_read = UINT_MAX;
105         cur->last_write = UINT_MAX;
106
107         return cur;
108 }
109
110 void update_diskio_values(struct diskio_stat *ds,
111                 unsigned int reads, unsigned int writes)
112 {
113         int i;
114         double sum=0, sum_r=0, sum_w=0;
115
116         if (reads < ds->last_read || writes < ds->last_write) {
117                 /* counter overflow or reset - rebase to sane values */
118                 ds->last = reads+writes;
119                 ds->last_read = reads;
120                 ds->last_write = writes;
121         }
122         /* since the values in /proc/diskstats are absolute, we have to substract
123          * our last reading. The numbers stand for "sectors read", and we therefore
124          * have to divide by two to get KB */
125         ds->sample_read[0] = (reads - ds->last_read) / 2;
126         ds->sample_write[0] = (writes - ds->last_write) / 2;
127         ds->sample[0] = ds->sample_read[0] + ds->sample_write[0];
128
129         /* compute averages */
130         for (i = 0; i < (signed) info.diskio_avg_samples; i++) {
131                 sum += ds->sample[i];
132                 sum_r += ds->sample_read[i];
133                 sum_w += ds->sample_write[i];
134         }
135         ds->current = sum / (double) info.diskio_avg_samples;
136         ds->current_read = sum_r / (double) info.diskio_avg_samples;
137         ds->current_write = sum_w / (double) info.diskio_avg_samples;
138
139         /* shift sample history */
140         for (i = info.diskio_avg_samples-1; i > 0; i--) {
141                 ds->sample[i] = ds->sample[i-1];
142                 ds->sample_read[i] = ds->sample_read[i-1];
143                 ds->sample_write[i] = ds->sample_write[i-1];
144         }
145
146         /* save last */
147         ds->last_read = reads;
148         ds->last_write = writes;
149         ds->last = ds->last_read + ds->last_write;
150 }
151