Added copyright/GPL header to all .c and .h files in trunk
[collectd.git] / src / cpufreq.c
1 /**
2  * collectd - src/cpufreq.c
3  * Copyright (C) 2005  Peter Holik
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Peter Holik <peter at holik.at>
21  **/
22
23 #include "cpufreq.h"
24
25 /*
26  * Originally written by Peter Holik
27  */
28
29 #if COLLECT_CPUFREQ
30 #define MODULE_NAME "cpufreq"
31
32 #include "plugin.h"
33 #include "common.h"
34
35 static int num_cpu = 0;
36
37 static char *cpufreq_file = "cpufreq-%s.rrd";
38
39 static char *ds_def[] =
40 {
41         "DS:value:GAUGE:25:0:U",
42         NULL
43 };
44 static int ds_num = 1;
45
46 extern time_t curtime;
47
48 #define BUFSIZE 256
49
50 void cpufreq_init (void)
51 {
52         int status;
53         char filename[BUFSIZE];
54
55         num_cpu = 0;
56
57         while (1)
58         {
59                 status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", num_cpu);
60                 if (status < 1 || status >= BUFSIZE)
61                         break;
62
63                 if (access(filename, R_OK))
64                         break;
65
66                 num_cpu++;
67         }
68
69         syslog (LOG_INFO, MODULE_NAME" found %d cpu(s)", num_cpu);
70 }
71
72 void cpufreq_write (char *host, char *inst, char *val)
73 {
74         int status;
75         char file[BUFSIZE];
76
77         status = snprintf (file, BUFSIZE, cpufreq_file, inst);
78         if (status < 1 || status >= BUFSIZE)
79                 return;
80
81         rrd_update_file (host, file, val, ds_def, ds_num);
82 }
83
84 void cpufreq_submit (int cpu_num, unsigned long long val)
85 {
86         char buf[BUFSIZE];
87         char cpu[16];
88
89         if (snprintf (buf, BUFSIZE, "%u:%llu", (unsigned int) curtime, val) >= BUFSIZE)
90                 return;
91         snprintf (cpu, 16, "%i", cpu_num);
92
93         plugin_submit (MODULE_NAME, cpu, buf);
94 }
95
96 void cpufreq_read (void)
97 {
98         int status;
99         unsigned long long val;
100         int i = 0;
101         FILE *fp;
102         char filename[BUFSIZE];
103         char buffer[16];
104
105         for (i = 0; i < num_cpu; i++)
106         {
107                 status = snprintf (filename, BUFSIZE, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", i);
108                 if (status < 1 || status >= BUFSIZE)
109                         return;
110
111                 if ((fp = fopen (filename, "r")) == NULL)
112                 {
113                         syslog (LOG_WARNING, "cpufreq: fopen: %s", strerror (errno));
114                         return;
115                 }
116
117                 if (fgets (buffer, 16, fp) == NULL)
118                 {
119                         syslog (LOG_WARNING, "cpufreq: fgets: %s", strerror (errno));
120                         return;
121                 }
122
123                 if (fclose (fp))
124                         syslog (LOG_WARNING, "cpufreq: fclose: %s", strerror (errno));
125
126                 /* You're seeing correctly: The file is reporting kHz values.. */
127                 val = atoll (buffer) * 1000;
128
129                 cpufreq_submit (i, val);
130         }
131 }
132 #undef BUFSIZE
133
134 void module_register (void)
135 {
136         plugin_register (MODULE_NAME, cpufreq_init, cpufreq_read, cpufreq_write);
137 }
138
139 #undef MODULE_NAME
140 #endif /* COLLECT_CPUFREQ */