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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #define MODULE_NAME "cpufreq"
29 static int num_cpu = 0;
31 static int cpufreq_init (void)
32 {
33 int status;
34 char filename[256];
36 num_cpu = 0;
38 while (1)
39 {
40 status = ssnprintf (filename, sizeof (filename),
41 "/sys/devices/system/cpu/cpu%d/cpufreq/"
42 "scaling_cur_freq", num_cpu);
43 if ((status < 1) || ((unsigned int)status >= sizeof (filename)))
44 break;
46 if (access (filename, R_OK))
47 break;
49 num_cpu++;
50 }
52 INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
53 (num_cpu == 1) ? "" : "s");
55 if (num_cpu == 0)
56 plugin_unregister_read ("cpufreq");
58 return (0);
59 } /* int cpufreq_init */
61 static void cpufreq_submit (int cpu_num, double value)
62 {
63 value_t values[1];
64 value_list_t vl = VALUE_LIST_INIT;
66 values[0].gauge = value;
68 vl.values = values;
69 vl.values_len = 1;
70 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
71 sstrncpy (vl.plugin, "cpufreq", sizeof (vl.plugin));
72 sstrncpy (vl.type, "cpufreq", sizeof (vl.type));
73 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
74 "%i", cpu_num);
76 plugin_dispatch_values (&vl);
77 }
79 static int cpufreq_read (void)
80 {
81 int status;
82 unsigned long long val;
83 int i = 0;
84 FILE *fp;
85 char filename[256];
86 char buffer[16];
88 for (i = 0; i < num_cpu; i++)
89 {
90 status = ssnprintf (filename, sizeof (filename),
91 "/sys/devices/system/cpu/cpu%d/cpufreq/"
92 "scaling_cur_freq", i);
93 if ((status < 1) || ((unsigned int)status >= sizeof (filename)))
94 return (-1);
96 if ((fp = fopen (filename, "r")) == NULL)
97 {
98 char errbuf[1024];
99 WARNING ("cpufreq: fopen (%s): %s", filename,
100 sstrerror (errno, errbuf,
101 sizeof (errbuf)));
102 return (-1);
103 }
105 if (fgets (buffer, 16, fp) == NULL)
106 {
107 char errbuf[1024];
108 WARNING ("cpufreq: fgets: %s",
109 sstrerror (errno, errbuf,
110 sizeof (errbuf)));
111 fclose (fp);
112 return (-1);
113 }
115 if (fclose (fp))
116 {
117 char errbuf[1024];
118 WARNING ("cpufreq: fclose: %s",
119 sstrerror (errno, errbuf,
120 sizeof (errbuf)));
121 }
124 /* You're seeing correctly: The file is reporting kHz values.. */
125 val = atoll (buffer) * 1000;
127 cpufreq_submit (i, val);
128 }
130 return (0);
131 } /* int cpufreq_read */
133 void module_register (void)
134 {
135 plugin_register_init ("cpufreq", cpufreq_init);
136 plugin_register_read ("cpufreq", cpufreq_read);
137 }