2 * collectd - src/cpufreq.c
3 * Copyright (C) 2005-2007 Peter Holik
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.
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.
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
20 * Peter Holik <peter at holik.at>
27 #define MODULE_NAME "cpufreq"
29 static int num_cpu = 0;
31 static int cpufreq_init (void)
40 status = snprintf (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)))
46 if (access (filename, R_OK))
52 INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
53 (num_cpu == 1) ? "" : "s");
56 plugin_unregister_read ("cpufreq");
59 } /* int cpufreq_init */
61 static void cpufreq_submit (int cpu_num, double value)
64 value_list_t vl = VALUE_LIST_INIT;
66 values[0].gauge = value;
70 vl.time = time (NULL);
71 strcpy (vl.host, hostname_g);
72 strcpy (vl.plugin, "cpufreq");
73 snprintf (vl.type_instance, sizeof (vl.type_instance),
76 plugin_dispatch_values ("cpufreq", &vl);
79 static int cpufreq_read (void)
82 unsigned long long val;
88 for (i = 0; i < num_cpu; i++)
90 status = snprintf (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)))
96 if ((fp = fopen (filename, "r")) == NULL)
99 WARNING ("cpufreq: fopen (%s): %s", filename,
100 sstrerror (errno, errbuf,
105 if (fgets (buffer, 16, fp) == NULL)
108 WARNING ("cpufreq: fgets: %s",
109 sstrerror (errno, errbuf,
118 WARNING ("cpufreq: fclose: %s",
119 sstrerror (errno, errbuf,
124 /* You're seeing correctly: The file is reporting kHz values.. */
125 val = atoll (buffer) * 1000;
127 cpufreq_submit (i, val);
131 } /* int cpufreq_read */
133 void module_register (void)
135 plugin_register_init ("cpufreq", cpufreq_init);
136 plugin_register_read ("cpufreq", cpufreq_read);