Delay checking for want_apcupsd.
[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-2009 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 #include <sys/stat.h>
37
38 /* this is the root of all per disk stats,
39  * also containing the totals. */
40 struct diskio_stat stats = {
41         .next = NULL,
42         .sample = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
43         .sample_read = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
44         .sample_write = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
45         .current = 0,
46         .current_read = 0,
47         .current_write = 0,
48         .last = UINT_MAX,
49         .last_read = UINT_MAX,
50         .last_write = UINT_MAX,
51 };
52
53 void clear_diskio_stats(void)
54 {
55         struct diskio_stat *cur;
56         while (stats.next) {
57                 cur = stats.next;
58                 stats.next = stats.next->next;
59                 free(cur);
60         }
61 }
62
63 struct diskio_stat *prepare_diskio_stat(const char *s)
64 {
65         struct stat sb;
66         char stat_name[text_buffer_size], device_name[text_buffer_size];
67         struct diskio_stat *cur = &stats;
68
69         if (!s)
70                 return &stats;
71
72 #if defined(__FreeBSD__)
73         if (strncmp(s, "/dev/", 5) == 0) {
74                 // supplied a /dev/device arg, so cut off the /dev part
75                 strncpy(device_name, s + 5, text_buffer_size);
76         } else
77 #endif
78         strncpy(device_name, s, text_buffer_size);
79
80         snprintf(stat_name, text_buffer_size, "/dev/%s", device_name);
81
82         if (stat(stat_name, &sb)) {
83                 ERR("diskio device '%s' does not exist", s);
84                 return 0;
85         }
86
87         /* lookup existing */
88         while (cur->next) {
89                 cur = cur->next;
90                 if (!strcmp(cur->dev, device_name)) {
91                         return cur;
92                 }
93         }
94
95         /* no existing found, make a new one */
96         cur->next = calloc(1, sizeof(struct diskio_stat));
97         cur = cur->next;
98         cur->dev = strndup(device_name, text_buffer_size);
99         cur->last = UINT_MAX;
100         cur->last_read = UINT_MAX;
101         cur->last_write = UINT_MAX;
102
103         return cur;
104 }
105
106 void update_diskio_values(struct diskio_stat *ds,
107                 unsigned int reads, unsigned int writes)
108 {
109         int i;
110         double sum=0, sum_r=0, sum_w=0;
111
112         if (reads < ds->last_read || writes < ds->last_write) {
113                 /* counter overflow or reset - rebase to sane values */
114                 ds->last = reads+writes;
115                 ds->last_read = reads;
116                 ds->last_write = writes;
117         }
118         /* since the values in /proc/diskstats are absolute, we have to substract
119          * our last reading. The numbers stand for "sectors read", and we therefore
120          * have to divide by two to get KB */
121         ds->sample_read[0] = (reads - ds->last_read) / 2;
122         ds->sample_write[0] = (writes - ds->last_write) / 2;
123         ds->sample[0] = ds->sample_read[0] + ds->sample_write[0];
124
125         /* compute averages */
126         for (i = 0; i < (signed) info.diskio_avg_samples; i++) {
127                 sum += ds->sample[i];
128                 sum_r += ds->sample_read[i];
129                 sum_w += ds->sample_write[i];
130         }
131         ds->current = sum / (double) info.diskio_avg_samples;
132         ds->current_read = sum_r / (double) info.diskio_avg_samples;
133         ds->current_write = sum_w / (double) info.diskio_avg_samples;
134
135         /* shift sample history */
136         for (i = info.diskio_avg_samples-1; i > 0; i--) {
137                 ds->sample[i] = ds->sample[i-1];
138                 ds->sample_read[i] = ds->sample_read[i-1];
139                 ds->sample_write[i] = ds->sample_write[i-1];
140         }
141
142         /* save last */
143         ds->last_read = reads;
144         ds->last_write = writes;
145         ds->last = ds->last_read + ds->last_write;
146 }
147