2 * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
4 * Licensed under the terms of the GNU GPL License version 2.
17 #include "helpers/helpers.h"
18 #include "helpers/bitmask.h"
22 static unsigned int count_cpus(void)
27 unsigned int cpunr = 0;
29 fp = fopen("/proc/stat", "r");
31 printf(_("Couldn't count the number of CPUs (%s: %s), assuming 1\n"), "/proc/stat", strerror(errno));
36 if (!fgets(value, LINE_LEN, fp))
38 value[LINE_LEN - 1] = '\0';
39 if (strlen(value) < (LINE_LEN - 2))
41 if (strstr(value, "cpu "))
43 if (sscanf(value, "cpu%d ", &cpunr) != 1)
50 /* cpu count starts from 0, on error return 1 (UP) */
55 static void proc_cpufreq_output(void)
57 unsigned int cpu, nr_cpus;
58 struct cpufreq_policy *policy;
59 unsigned int min_pctg = 0;
60 unsigned int max_pctg = 0;
61 unsigned long min, max;
63 printf(_(" minimum CPU frequency - maximum CPU frequency - governor\n"));
65 nr_cpus = count_cpus();
66 for (cpu = 0; cpu < nr_cpus; cpu++) {
67 policy = cpufreq_get_policy(cpu);
71 if (cpufreq_get_hardware_limits(cpu, &min, &max)) {
74 min_pctg = (policy->min * 100) / max;
75 max_pctg = (policy->max * 100) / max;
77 printf("CPU%3d %9lu kHz (%3d %%) - %9lu kHz (%3d %%) - %s\n",
78 cpu , policy->min, max ? min_pctg : 0, policy->max,
79 max ? max_pctg : 0, policy->governor);
81 cpufreq_put_policy(policy);
85 static void print_speed(unsigned long speed)
89 if (speed > 1000000) {
93 printf("%u.%02u GHz", ((unsigned int) speed/1000000),
94 ((unsigned int) (speed%1000000)/10000));
95 } else if (speed > 100000) {
99 printf("%u MHz", ((unsigned int) speed / 1000));
100 } else if (speed > 1000) {
104 printf("%u.%01u MHz", ((unsigned int) speed/1000),
105 ((unsigned int) (speed%1000)/100));
107 printf("%lu kHz", speed);
112 static void print_duration(unsigned long duration)
116 if (duration > 1000000) {
117 tmp = duration % 10000;
120 printf("%u.%02u ms", ((unsigned int) duration/1000000),
121 ((unsigned int) (duration%1000000)/10000));
122 } else if (duration > 100000) {
123 tmp = duration % 1000;
126 printf("%u us", ((unsigned int) duration / 1000));
127 } else if (duration > 1000) {
128 tmp = duration % 100;
131 printf("%u.%01u us", ((unsigned int) duration/1000),
132 ((unsigned int) (duration%1000)/100));
134 printf("%lu ns", duration);
141 static int get_boost_mode(unsigned int cpu)
143 int support, active, b_states = 0, ret, pstate_no, i;
144 /* ToDo: Make this more global */
145 unsigned long pstates[MAX_HW_PSTATES] = {0,};
147 if (cpupower_cpu_info.vendor != X86_VENDOR_AMD &&
148 cpupower_cpu_info.vendor != X86_VENDOR_INTEL)
151 ret = cpufreq_has_boost_support(cpu, &support, &active, &b_states);
153 printf(_("Error while evaluating Boost Capabilities"
154 " on CPU %d -- are you root?\n"), cpu);
157 /* P state changes via MSR are identified via cpuid 80000007
158 on Intel and AMD, but we assume boost capable machines can do that
159 if (cpuid_eax(0x80000000) >= 0x80000007
160 && (cpuid_edx(0x80000007) & (1 << 7)))
163 printf(_(" boost state support:\n"));
165 printf(_(" Supported: %s\n"), support ? _("yes") : _("no"));
166 printf(_(" Active: %s\n"), active ? _("yes") : _("no"));
168 /* ToDo: Only works for AMD for now... */
170 if (cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
171 cpupower_cpu_info.family >= 0x10) {
172 ret = decode_pstates(cpu, cpupower_cpu_info.family, b_states,
173 pstates, &pstate_no);
179 printf(_(" Boost States: %d\n"), b_states);
180 printf(_(" Total States: %d\n"), pstate_no);
181 for (i = 0; i < pstate_no; i++) {
183 printf(_(" Pstate-Pb%d: %luMHz (boost state)\n"),
186 printf(_(" Pstate-P%d: %luMHz\n"),
187 i - b_states, pstates[i]);
192 static void debug_output_one(unsigned int cpu)
195 struct cpufreq_affected_cpus *cpus;
196 struct cpufreq_available_frequencies *freqs;
197 unsigned long min, max, freq_kernel, freq_hardware;
198 unsigned long total_trans, latency;
199 unsigned long long total_time;
200 struct cpufreq_policy *policy;
201 struct cpufreq_available_governors *governors;
202 struct cpufreq_stats *stats;
204 if (cpufreq_cpu_exists(cpu))
207 freq_kernel = cpufreq_get_freq_kernel(cpu);
208 freq_hardware = cpufreq_get_freq_hardware(cpu);
210 driver = cpufreq_get_driver(cpu);
212 printf(_(" no or unknown cpufreq driver is active on this CPU\n"));
214 printf(_(" driver: %s\n"), driver);
215 cpufreq_put_driver(driver);
218 cpus = cpufreq_get_related_cpus(cpu);
220 printf(_(" CPUs which run at the same hardware frequency: "));
222 printf("%d ", cpus->cpu);
225 printf("%d\n", cpus->cpu);
226 cpufreq_put_related_cpus(cpus);
229 cpus = cpufreq_get_affected_cpus(cpu);
231 printf(_(" CPUs which need to have their frequency coordinated by software: "));
233 printf("%d ", cpus->cpu);
236 printf("%d\n", cpus->cpu);
237 cpufreq_put_affected_cpus(cpus);
240 latency = cpufreq_get_transition_latency(cpu);
242 printf(_(" maximum transition latency: "));
243 print_duration(latency);
247 if (!(cpufreq_get_hardware_limits(cpu, &min, &max))) {
248 printf(_(" hardware limits: "));
255 freqs = cpufreq_get_available_frequencies(cpu);
257 printf(_(" available frequency steps: "));
258 while (freqs->next) {
259 print_speed(freqs->frequency);
263 print_speed(freqs->frequency);
265 cpufreq_put_available_frequencies(freqs);
268 governors = cpufreq_get_available_governors(cpu);
270 printf(_(" available cpufreq governors: "));
271 while (governors->next) {
272 printf("%s, ", governors->governor);
273 governors = governors->next;
275 printf("%s\n", governors->governor);
276 cpufreq_put_available_governors(governors);
279 policy = cpufreq_get_policy(cpu);
281 printf(_(" current policy: frequency should be within "));
282 print_speed(policy->min);
284 print_speed(policy->max);
287 printf(_("The governor \"%s\" may"
288 " decide which speed to use\n within this range.\n"),
290 cpufreq_put_policy(policy);
293 if (freq_kernel || freq_hardware) {
294 printf(_(" current CPU frequency is "));
296 print_speed(freq_hardware);
297 printf(_(" (asserted by call to hardware)"));
299 print_speed(freq_kernel);
302 stats = cpufreq_get_stats(cpu, &total_time);
304 printf(_(" cpufreq stats: "));
306 print_speed(stats->frequency);
307 printf(":%.2f%%", (100.0 * stats->time_in_state) / total_time);
312 cpufreq_put_stats(stats);
313 total_trans = cpufreq_get_transitions(cpu);
315 printf(" (%lu)\n", total_trans);
325 static int get_freq_kernel(unsigned int cpu, unsigned int human)
327 unsigned long freq = cpufreq_get_freq_kernel(cpu);
334 printf("%lu\n", freq);
341 static int get_freq_hardware(unsigned int cpu, unsigned int human)
343 unsigned long freq = cpufreq_get_freq_hardware(cpu);
350 printf("%lu\n", freq);
354 /* --hwlimits / -l */
356 static int get_hardware_limits(unsigned int cpu)
358 unsigned long min, max;
359 if (cpufreq_get_hardware_limits(cpu, &min, &max))
361 printf("%lu %lu\n", min, max);
367 static int get_driver(unsigned int cpu)
369 char *driver = cpufreq_get_driver(cpu);
372 printf("%s\n", driver);
373 cpufreq_put_driver(driver);
379 static int get_policy(unsigned int cpu)
381 struct cpufreq_policy *policy = cpufreq_get_policy(cpu);
384 printf("%lu %lu %s\n", policy->min, policy->max, policy->governor);
385 cpufreq_put_policy(policy);
389 /* --governors / -g */
391 static int get_available_governors(unsigned int cpu)
393 struct cpufreq_available_governors *governors =
394 cpufreq_get_available_governors(cpu);
398 while (governors->next) {
399 printf("%s ", governors->governor);
400 governors = governors->next;
402 printf("%s\n", governors->governor);
403 cpufreq_put_available_governors(governors);
408 /* --affected-cpus / -a */
410 static int get_affected_cpus(unsigned int cpu)
412 struct cpufreq_affected_cpus *cpus = cpufreq_get_affected_cpus(cpu);
417 printf("%d ", cpus->cpu);
420 printf("%d\n", cpus->cpu);
421 cpufreq_put_affected_cpus(cpus);
425 /* --related-cpus / -r */
427 static int get_related_cpus(unsigned int cpu)
429 struct cpufreq_affected_cpus *cpus = cpufreq_get_related_cpus(cpu);
434 printf("%d ", cpus->cpu);
437 printf("%d\n", cpus->cpu);
438 cpufreq_put_related_cpus(cpus);
444 static int get_freq_stats(unsigned int cpu, unsigned int human)
446 unsigned long total_trans = cpufreq_get_transitions(cpu);
447 unsigned long long total_time;
448 struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);
451 print_speed(stats->frequency);
453 (100.0 * stats->time_in_state) / total_time);
456 stats->frequency, stats->time_in_state);
461 cpufreq_put_stats(stats);
463 printf(" (%lu)\n", total_trans);
469 static int get_latency(unsigned int cpu, unsigned int human)
471 unsigned long latency = cpufreq_get_transition_latency(cpu);
476 print_duration(latency);
479 printf("%lu\n", latency);
483 void freq_info_help(void)
485 printf(_("Usage: cpupower freqinfo [options]\n"));
486 printf(_("Options:\n"));
487 printf(_(" -e, --debug Prints out debug information [default]\n"));
488 printf(_(" -f, --freq Get frequency the CPU currently runs at, according\n"
489 " to the cpufreq core *\n"));
490 printf(_(" -w, --hwfreq Get frequency the CPU currently runs at, by reading\n"
491 " it from hardware (only available to root) *\n"));
492 printf(_(" -l, --hwlimits Determine the minimum and maximum CPU frequency allowed *\n"));
493 printf(_(" -d, --driver Determines the used cpufreq kernel driver *\n"));
494 printf(_(" -p, --policy Gets the currently used cpufreq policy *\n"));
495 printf(_(" -g, --governors Determines available cpufreq governors *\n"));
496 printf(_(" -r, --related-cpus Determines which CPUs run at the same hardware frequency *\n"));
497 printf(_(" -a, --affected-cpus Determines which CPUs need to have their frequency\n"
498 " coordinated by software *\n"));
499 printf(_(" -s, --stats Shows cpufreq statistics if available\n"));
500 printf(_(" -y, --latency Determines the maximum latency on CPU frequency changes *\n"));
501 printf(_(" -b, --boost Checks for turbo or boost modes *\n"));
502 printf(_(" -o, --proc Prints out information like provided by the /proc/cpufreq\n"
503 " interface in 2.4. and early 2.6. kernels\n"));
504 printf(_(" -m, --human human-readable output for the -f, -w, -s and -y parameters\n"));
505 printf(_(" -h, --help Prints out this screen\n"));
508 printf(_("If no argument is given, full output about\n"
509 "cpufreq is printed which is useful e.g. for reporting bugs.\n\n"));
510 printf(_("By default info of CPU 0 is shown which can be overridden\n"
511 "with the cpupower --cpu main command option.\n"));
514 static struct option info_opts[] = {
515 { .name = "debug", .has_arg = no_argument, .flag = NULL, .val = 'e'},
516 { .name = "boost", .has_arg = no_argument, .flag = NULL, .val = 'b'},
517 { .name = "freq", .has_arg = no_argument, .flag = NULL, .val = 'f'},
518 { .name = "hwfreq", .has_arg = no_argument, .flag = NULL, .val = 'w'},
519 { .name = "hwlimits", .has_arg = no_argument, .flag = NULL, .val = 'l'},
520 { .name = "driver", .has_arg = no_argument, .flag = NULL, .val = 'd'},
521 { .name = "policy", .has_arg = no_argument, .flag = NULL, .val = 'p'},
522 { .name = "governors", .has_arg = no_argument, .flag = NULL, .val = 'g'},
523 { .name = "related-cpus", .has_arg = no_argument, .flag = NULL, .val = 'r'},
524 { .name = "affected-cpus",.has_arg = no_argument, .flag = NULL, .val = 'a'},
525 { .name = "stats", .has_arg = no_argument, .flag = NULL, .val = 's'},
526 { .name = "latency", .has_arg = no_argument, .flag = NULL, .val = 'y'},
527 { .name = "proc", .has_arg = no_argument, .flag = NULL, .val = 'o'},
528 { .name = "human", .has_arg = no_argument, .flag = NULL, .val = 'm'},
529 { .name = "help", .has_arg = no_argument, .flag = NULL, .val = 'h'},
533 int cmd_freq_info(int argc, char **argv)
536 extern int optind, opterr, optopt;
537 int ret = 0, cont = 1;
538 unsigned int cpu = 0;
539 unsigned int human = 0;
540 int output_param = 0;
543 ret = getopt_long(argc, argv, "hoefwldpgrasmyb", info_opts, NULL);
585 fprintf(stderr, "invalid or unknown argument\n");
590 switch (output_param) {
592 if (!bitmask_isallclear(cpus_chosen)) {
593 printf(_("The argument passed to this tool can't be "
594 "combined with passing a --cpu argument\n"));
604 /* Default is: show output of CPU 0 only */
605 if (bitmask_isallclear(cpus_chosen))
606 bitmask_setbit(cpus_chosen, 0);
608 switch (output_param) {
610 printf(_("You can't specify more than one --cpu parameter and/or\n"
611 "more than one output-specific argument\n"));
614 printf(_("invalid or unknown argument\n"));
621 proc_cpufreq_output();
625 for (cpu = bitmask_first(cpus_chosen);
626 cpu <= bitmask_last(cpus_chosen); cpu++) {
628 if (!bitmask_isbitset(cpus_chosen, cpu))
630 if (cpufreq_cpu_exists(cpu)) {
631 printf(_("couldn't analyze CPU %d as it doesn't seem to be present\n"), cpu);
634 printf(_("analyzing CPU %d:\n"), cpu);
636 switch (output_param) {
641 debug_output_one(cpu);
644 ret = get_affected_cpus(cpu);
647 ret = get_related_cpus(cpu);
650 ret = get_available_governors(cpu);
653 ret = get_policy(cpu);
656 ret = get_driver(cpu);
659 ret = get_hardware_limits(cpu);
662 ret = get_freq_hardware(cpu, human);
665 ret = get_freq_kernel(cpu, human);
668 ret = get_freq_stats(cpu, human);
671 ret = get_latency(cpu, human);