add emacs indentation variables to source files in line with current vim settings
[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                 ERR("diskio device '%s' does not exist", s);
89                 return 0;
90         }
91
92         /* lookup existing */
93         while (cur->next) {
94                 cur = cur->next;
95                 if (!strcmp(cur->dev, device_name)) {
96                         return cur;
97                 }
98         }
99
100         /* no existing found, make a new one */
101         cur->next = calloc(1, sizeof(struct diskio_stat));
102         cur = cur->next;
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;
107
108         return cur;
109 }
110
111 void update_diskio_values(struct diskio_stat *ds,
112                 unsigned int reads, unsigned int writes)
113 {
114         int i;
115         double sum=0, sum_r=0, sum_w=0;
116
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;
122         }
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];
129
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];
135         }
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;
139
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];
145         }
146
147         /* save last */
148         ds->last_read = reads;
149         ds->last_write = writes;
150         ds->last = ds->last_read + ds->last_write;
151 }
152