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 #if defined(KERNEL_LINUX)
30 # define CPUFREQ_HAVE_READ 1
32 # define CPUFREQ_HAVE_READ 0
37 static int num_cpu = 0;
40 static int cpufreq_init (void)
50 status = snprintf (filename, sizeof (filename),
51 "/sys/devices/system/cpu/cpu%d/cpufreq/"
52 "scaling_cur_freq", num_cpu);
53 if (status < 1 || status >= sizeof (filename))
56 if (access (filename, R_OK))
62 INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
63 (num_cpu == 1) ? "" : "s");
66 plugin_unregister_read ("cpufreq");
67 #endif /* defined(KERNEL_LINUX) */
70 } /* int cpufreq_init */
72 static void cpufreq_submit (int cpu_num, double value)
75 value_list_t vl = VALUE_LIST_INIT;
77 values[0].gauge = value;
81 vl.time = time (NULL);
82 strcpy (vl.host, hostname_g);
83 strcpy (vl.plugin, "cpufreq");
84 snprintf (vl.type_instance, sizeof (vl.type_instance),
87 plugin_dispatch_values ("cpufreq", &vl);
90 static int cpufreq_read (void)
94 unsigned long long val;
100 for (i = 0; i < num_cpu; i++)
102 status = snprintf (filename, sizeof (filename),
103 "/sys/devices/system/cpu/cpu%d/cpufreq/"
104 "scaling_cur_freq", i);
105 if (status < 1 || status >= sizeof (filename))
108 if ((fp = fopen (filename, "r")) == NULL)
111 WARNING ("cpufreq: fopen (%s): %s", filename,
112 sstrerror (errno, errbuf,
117 if (fgets (buffer, 16, fp) == NULL)
120 WARNING ("cpufreq: fgets: %s",
121 sstrerror (errno, errbuf,
130 WARNING ("cpufreq: fclose: %s",
131 sstrerror (errno, errbuf,
136 /* You're seeing correctly: The file is reporting kHz values.. */
137 val = atoll (buffer) * 1000;
139 cpufreq_submit (i, val);
141 #endif /* defined(KERNEL_LINUX) */
144 } /* int cpufreq_read */
145 #endif /* CPUFREQ_HAVE_READ */
148 void module_register (void)
150 #if CPUFREQ_HAVE_READ
151 plugin_register_init ("cpufreq", cpufreq_init);
152 plugin_register_read ("cpufreq", cpufreq_read);
153 #endif /* CPUFREQ_HAVE_READ */