Merge branch 'pr/1792'
[collectd.git] / src / cpufreq.c
1 /**
2  * collectd - src/cpufreq.c
3  * Copyright (C) 2005-2007  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 "collectd.h"
24
25 #include "common.h"
26 #include "plugin.h"
27
28 static int num_cpu = 0;
29
30 static int cpufreq_init (void)
31 {
32         int status;
33         char filename[256];
34
35         num_cpu = 0;
36
37         while (1)
38         {
39                 status = ssnprintf (filename, sizeof (filename),
40                                 "/sys/devices/system/cpu/cpu%d/cpufreq/"
41                                 "scaling_cur_freq", num_cpu);
42                 if ((status < 1) || ((unsigned int)status >= sizeof (filename)))
43                         break;
44
45                 if (access (filename, R_OK))
46                         break;
47
48                 num_cpu++;
49         }
50
51         INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
52                         (num_cpu == 1) ? "" : "s");
53
54         if (num_cpu == 0)
55                 plugin_unregister_read ("cpufreq");
56
57         return (0);
58 } /* int cpufreq_init */
59
60 static void cpufreq_submit (int cpu_num, value_t value)
61 {
62         value_list_t vl = VALUE_LIST_INIT;
63
64         vl.values = &value;
65         vl.values_len = 1;
66         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
67         sstrncpy (vl.plugin, "cpufreq", sizeof (vl.plugin));
68         sstrncpy (vl.type, "cpufreq", sizeof (vl.type));
69         ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%i", cpu_num);
70
71         plugin_dispatch_values (&vl);
72 }
73
74 static int cpufreq_read (void)
75 {
76         for (int i = 0; i < num_cpu; i++)
77         {
78                 char filename[PATH_MAX];
79                 ssnprintf (filename, sizeof (filename),
80                                 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
81
82                 value_t v;
83                 if (parse_value_file (filename, &v, DS_TYPE_GAUGE) != 0)
84                 {
85                         WARNING ("cpufreq plugin: Reading \"%s\" failed.", filename);
86                         continue;
87                 }
88
89                 /* convert kHz to Hz */
90                 v.gauge *= 1000.0;
91
92                 cpufreq_submit (i, v);
93         }
94
95         return (0);
96 } /* int cpufreq_read */
97
98 void module_register (void)
99 {
100         plugin_register_init ("cpufreq", cpufreq_init);
101         plugin_register_read ("cpufreq", cpufreq_read);
102 }