5ceaacc8601ed92c20d3cc3f5501752d33caba83
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 #if defined(KERNEL_LINUX)
30 # define CPUFREQ_HAVE_READ 1
31 #else
32 # define CPUFREQ_HAVE_READ 0
33 #endif
35 static data_source_t data_source[1] =
36 {
37 {"value", DS_TYPE_GAUGE, 0, NAN}
38 };
40 static data_set_t data_set =
41 {
42 "cpufreq", 1, data_source
43 };
45 #if CPUFREQ_HAVE_READ
46 #ifdef KERNEL_LINUX
47 static int num_cpu = 0;
48 #endif
50 static int cpufreq_init (void)
51 {
52 #ifdef KERNEL_LINUX
53 int status;
54 char filename[256];
56 num_cpu = 0;
58 while (1)
59 {
60 status = snprintf (filename, sizeof (filename),
61 "/sys/devices/system/cpu/cpu%d/cpufreq/"
62 "scaling_cur_freq", num_cpu);
63 if (status < 1 || status >= sizeof (filename))
64 break;
66 if (access (filename, R_OK))
67 break;
69 num_cpu++;
70 }
72 INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
73 (num_cpu == 1) ? "" : "s");
75 if (num_cpu == 0)
76 plugin_unregister_read ("cpufreq");
77 #endif /* defined(KERNEL_LINUX) */
79 return (0);
80 } /* int cpufreq_init */
82 static void cpufreq_submit (int cpu_num, double value)
83 {
84 value_t values[1];
85 value_list_t vl = VALUE_LIST_INIT;
87 values[0].gauge = value;
89 vl.values = values;
90 vl.values_len = 1;
91 vl.time = time (NULL);
92 strcpy (vl.host, hostname_g);
93 strcpy (vl.plugin, "cpufreq");
94 snprintf (vl.type_instance, sizeof (vl.type_instance),
95 "%i", cpu_num);
97 plugin_dispatch_values ("cpufreq", &vl);
98 }
100 static int cpufreq_read (void)
101 {
102 #ifdef KERNEL_LINUX
103 int status;
104 unsigned long long val;
105 int i = 0;
106 FILE *fp;
107 char filename[256];
108 char buffer[16];
110 for (i = 0; i < num_cpu; i++)
111 {
112 status = snprintf (filename, sizeof (filename),
113 "/sys/devices/system/cpu/cpu%d/cpufreq/"
114 "scaling_cur_freq", i);
115 if (status < 1 || status >= sizeof (filename))
116 return (-1);
118 if ((fp = fopen (filename, "r")) == NULL)
119 {
120 char errbuf[1024];
121 WARNING ("cpufreq: fopen (%s): %s", filename,
122 sstrerror (errno, errbuf,
123 sizeof (errbuf)));
124 return (-1);
125 }
127 if (fgets (buffer, 16, fp) == NULL)
128 {
129 char errbuf[1024];
130 WARNING ("cpufreq: fgets: %s",
131 sstrerror (errno, errbuf,
132 sizeof (errbuf)));
133 fclose (fp);
134 return (-1);
135 }
137 if (fclose (fp))
138 {
139 char errbuf[1024];
140 WARNING ("cpufreq: fclose: %s",
141 sstrerror (errno, errbuf,
142 sizeof (errbuf)));
143 }
146 /* You're seeing correctly: The file is reporting kHz values.. */
147 val = atoll (buffer) * 1000;
149 cpufreq_submit (i, val);
150 }
151 #endif /* defined(KERNEL_LINUX) */
153 return (0);
154 } /* int cpufreq_read */
155 #endif /* CPUFREQ_HAVE_READ */
156 #undef BUFSIZE
158 void module_register (void)
159 {
160 plugin_register_data_set (&data_set);
162 #if CPUFREQ_HAVE_READ
163 plugin_register_init ("cpufreq", cpufreq_init);
164 plugin_register_read ("cpufreq", cpufreq_read);
165 #endif /* CPUFREQ_HAVE_READ */
166 }