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 int no_rounding
;
86 static void print_speed(unsigned long speed
)
92 printf("%u.%06u GHz", ((unsigned int) speed
/1000000),
93 ((unsigned int) speed
%1000000));
94 else if (speed
> 100000)
95 printf("%u MHz", (unsigned int) speed
);
96 else if (speed
> 1000)
97 printf("%u.%03u MHz", ((unsigned int) speed
/1000),
98 (unsigned int) (speed
%1000));
100 printf("%lu kHz", speed
);
102 if (speed
> 1000000) {
106 printf("%u.%02u GHz", ((unsigned int) speed
/1000000),
107 ((unsigned int) (speed
%1000000)/10000));
108 } else if (speed
> 100000) {
112 printf("%u MHz", ((unsigned int) speed
/1000));
113 } else if (speed
> 1000) {
117 printf("%u.%01u MHz", ((unsigned int) speed
/1000),
118 ((unsigned int) (speed
%1000)/100));
125 static void print_duration(unsigned long duration
)
130 if (duration
> 1000000)
131 printf("%u.%06u ms", ((unsigned int) duration
/1000000),
132 ((unsigned int) duration
%1000000));
133 else if (duration
> 100000)
134 printf("%u us", ((unsigned int) duration
/1000));
135 else if (duration
> 1000)
136 printf("%u.%03u us", ((unsigned int) duration
/1000),
137 ((unsigned int) duration
%1000));
139 printf("%lu ns", duration
);
141 if (duration
> 1000000) {
142 tmp
= duration
%10000;
145 printf("%u.%02u ms", ((unsigned int) duration
/1000000),
146 ((unsigned int) (duration
%1000000)/10000));
147 } else if (duration
> 100000) {
151 printf("%u us", ((unsigned int) duration
/ 1000));
152 } else if (duration
> 1000) {
156 printf("%u.%01u us", ((unsigned int) duration
/1000),
157 ((unsigned int) (duration
%1000)/100));
159 printf("%lu ns", duration
);
166 static int get_boost_mode(unsigned int cpu
)
168 int support
, active
, b_states
= 0, ret
, pstate_no
, i
;
169 /* ToDo: Make this more global */
170 unsigned long pstates
[MAX_HW_PSTATES
] = {0,};
172 if (cpupower_cpu_info
.vendor
!= X86_VENDOR_AMD
&&
173 cpupower_cpu_info
.vendor
!= X86_VENDOR_INTEL
)
176 ret
= cpufreq_has_boost_support(cpu
, &support
, &active
, &b_states
);
178 printf(_("Error while evaluating Boost Capabilities"
179 " on CPU %d -- are you root?\n"), cpu
);
182 /* P state changes via MSR are identified via cpuid 80000007
183 on Intel and AMD, but we assume boost capable machines can do that
184 if (cpuid_eax(0x80000000) >= 0x80000007
185 && (cpuid_edx(0x80000007) & (1 << 7)))
188 printf(_(" boost state support:\n"));
190 printf(_(" Supported: %s\n"), support
? _("yes") : _("no"));
191 printf(_(" Active: %s\n"), active
? _("yes") : _("no"));
193 if (cpupower_cpu_info
.vendor
== X86_VENDOR_AMD
&&
194 cpupower_cpu_info
.family
>= 0x10) {
195 ret
= decode_pstates(cpu
, cpupower_cpu_info
.family
, b_states
,
196 pstates
, &pstate_no
);
200 printf(_(" Boost States: %d\n"), b_states
);
201 printf(_(" Total States: %d\n"), pstate_no
);
202 for (i
= 0; i
< pstate_no
; i
++) {
204 printf(_(" Pstate-Pb%d: %luMHz (boost state)"
205 "\n"), i
, pstates
[i
]);
207 printf(_(" Pstate-P%d: %luMHz\n"),
208 i
- b_states
, pstates
[i
]);
210 } else if (cpupower_cpu_info
.caps
& CPUPOWER_CAP_HAS_TURBO_RATIO
) {
212 unsigned long long intel_turbo_ratio
= 0;
215 /* Any way to autodetect this ? */
216 if (cpupower_cpu_info
.caps
& CPUPOWER_CAP_IS_SNB
)
220 intel_turbo_ratio
= msr_intel_get_turbo_ratio(cpu
);
221 dprint (" Ratio: 0x%llx - bclk: %f\n",
222 intel_turbo_ratio
, bclk
);
224 ratio
= (intel_turbo_ratio
>> 24) & 0xFF;
226 printf(_(" %.0f MHz max turbo 4 active cores\n"),
229 ratio
= (intel_turbo_ratio
>> 16) & 0xFF;
231 printf(_(" %.0f MHz max turbo 3 active cores\n"),
234 ratio
= (intel_turbo_ratio
>> 8) & 0xFF;
236 printf(_(" %.0f MHz max turbo 2 active cores\n"),
239 ratio
= (intel_turbo_ratio
>> 0) & 0xFF;
241 printf(_(" %.0f MHz max turbo 1 active cores\n"),
247 static void debug_output_one(unsigned int cpu
)
250 struct cpufreq_affected_cpus
*cpus
;
251 struct cpufreq_available_frequencies
*freqs
;
252 unsigned long min
, max
, freq_kernel
, freq_hardware
;
253 unsigned long total_trans
, latency
;
254 unsigned long long total_time
;
255 struct cpufreq_policy
*policy
;
256 struct cpufreq_available_governors
*governors
;
257 struct cpufreq_stats
*stats
;
259 if (cpufreq_cpu_exists(cpu
))
262 freq_kernel
= cpufreq_get_freq_kernel(cpu
);
263 freq_hardware
= cpufreq_get_freq_hardware(cpu
);
265 driver
= cpufreq_get_driver(cpu
);
267 printf(_(" no or unknown cpufreq driver is active on this CPU\n"));
269 printf(_(" driver: %s\n"), driver
);
270 cpufreq_put_driver(driver
);
273 cpus
= cpufreq_get_related_cpus(cpu
);
275 printf(_(" CPUs which run at the same hardware frequency: "));
277 printf("%d ", cpus
->cpu
);
280 printf("%d\n", cpus
->cpu
);
281 cpufreq_put_related_cpus(cpus
);
284 cpus
= cpufreq_get_affected_cpus(cpu
);
286 printf(_(" CPUs which need to have their frequency coordinated by software: "));
288 printf("%d ", cpus
->cpu
);
291 printf("%d\n", cpus
->cpu
);
292 cpufreq_put_affected_cpus(cpus
);
295 latency
= cpufreq_get_transition_latency(cpu
);
297 printf(_(" maximum transition latency: "));
298 print_duration(latency
);
302 if (!(cpufreq_get_hardware_limits(cpu
, &min
, &max
))) {
303 printf(_(" hardware limits: "));
310 freqs
= cpufreq_get_available_frequencies(cpu
);
312 printf(_(" available frequency steps: "));
313 while (freqs
->next
) {
314 print_speed(freqs
->frequency
);
318 print_speed(freqs
->frequency
);
320 cpufreq_put_available_frequencies(freqs
);
323 governors
= cpufreq_get_available_governors(cpu
);
325 printf(_(" available cpufreq governors: "));
326 while (governors
->next
) {
327 printf("%s, ", governors
->governor
);
328 governors
= governors
->next
;
330 printf("%s\n", governors
->governor
);
331 cpufreq_put_available_governors(governors
);
334 policy
= cpufreq_get_policy(cpu
);
336 printf(_(" current policy: frequency should be within "));
337 print_speed(policy
->min
);
339 print_speed(policy
->max
);
342 printf(_("The governor \"%s\" may"
343 " decide which speed to use\n within this range.\n"),
345 cpufreq_put_policy(policy
);
348 if (freq_kernel
|| freq_hardware
) {
349 printf(_(" current CPU frequency is "));
351 print_speed(freq_hardware
);
352 printf(_(" (asserted by call to hardware)"));
354 print_speed(freq_kernel
);
357 stats
= cpufreq_get_stats(cpu
, &total_time
);
359 printf(_(" cpufreq stats: "));
361 print_speed(stats
->frequency
);
362 printf(":%.2f%%", (100.0 * stats
->time_in_state
) / total_time
);
367 cpufreq_put_stats(stats
);
368 total_trans
= cpufreq_get_transitions(cpu
);
370 printf(" (%lu)\n", total_trans
);
380 static int get_freq_kernel(unsigned int cpu
, unsigned int human
)
382 unsigned long freq
= cpufreq_get_freq_kernel(cpu
);
389 printf("%lu\n", freq
);
396 static int get_freq_hardware(unsigned int cpu
, unsigned int human
)
398 unsigned long freq
= cpufreq_get_freq_hardware(cpu
);
405 printf("%lu\n", freq
);
409 /* --hwlimits / -l */
411 static int get_hardware_limits(unsigned int cpu
)
413 unsigned long min
, max
;
414 if (cpufreq_get_hardware_limits(cpu
, &min
, &max
))
416 printf("%lu %lu\n", min
, max
);
422 static int get_driver(unsigned int cpu
)
424 char *driver
= cpufreq_get_driver(cpu
);
427 printf("%s\n", driver
);
428 cpufreq_put_driver(driver
);
434 static int get_policy(unsigned int cpu
)
436 struct cpufreq_policy
*policy
= cpufreq_get_policy(cpu
);
439 printf("%lu %lu %s\n", policy
->min
, policy
->max
, policy
->governor
);
440 cpufreq_put_policy(policy
);
444 /* --governors / -g */
446 static int get_available_governors(unsigned int cpu
)
448 struct cpufreq_available_governors
*governors
=
449 cpufreq_get_available_governors(cpu
);
453 while (governors
->next
) {
454 printf("%s ", governors
->governor
);
455 governors
= governors
->next
;
457 printf("%s\n", governors
->governor
);
458 cpufreq_put_available_governors(governors
);
463 /* --affected-cpus / -a */
465 static int get_affected_cpus(unsigned int cpu
)
467 struct cpufreq_affected_cpus
*cpus
= cpufreq_get_affected_cpus(cpu
);
472 printf("%d ", cpus
->cpu
);
475 printf("%d\n", cpus
->cpu
);
476 cpufreq_put_affected_cpus(cpus
);
480 /* --related-cpus / -r */
482 static int get_related_cpus(unsigned int cpu
)
484 struct cpufreq_affected_cpus
*cpus
= cpufreq_get_related_cpus(cpu
);
489 printf("%d ", cpus
->cpu
);
492 printf("%d\n", cpus
->cpu
);
493 cpufreq_put_related_cpus(cpus
);
499 static int get_freq_stats(unsigned int cpu
, unsigned int human
)
501 unsigned long total_trans
= cpufreq_get_transitions(cpu
);
502 unsigned long long total_time
;
503 struct cpufreq_stats
*stats
= cpufreq_get_stats(cpu
, &total_time
);
506 print_speed(stats
->frequency
);
508 (100.0 * stats
->time_in_state
) / total_time
);
511 stats
->frequency
, stats
->time_in_state
);
516 cpufreq_put_stats(stats
);
518 printf(" (%lu)\n", total_trans
);
524 static int get_latency(unsigned int cpu
, unsigned int human
)
526 unsigned long latency
= cpufreq_get_transition_latency(cpu
);
531 print_duration(latency
);
534 printf("%lu\n", latency
);
538 static struct option info_opts
[] = {
539 {"debug", no_argument
, NULL
, 'e'},
540 {"boost", no_argument
, NULL
, 'b'},
541 {"freq", no_argument
, NULL
, 'f'},
542 {"hwfreq", no_argument
, NULL
, 'w'},
543 {"hwlimits", no_argument
, NULL
, 'l'},
544 {"driver", no_argument
, NULL
, 'd'},
545 {"policy", no_argument
, NULL
, 'p'},
546 {"governors", no_argument
, NULL
, 'g'},
547 {"related-cpus", no_argument
, NULL
, 'r'},
548 {"affected-cpus", no_argument
, NULL
, 'a'},
549 {"stats", no_argument
, NULL
, 's'},
550 {"latency", no_argument
, NULL
, 'y'},
551 {"proc", no_argument
, NULL
, 'o'},
552 {"human", no_argument
, NULL
, 'm'},
553 {"no-rounding", no_argument
, NULL
, 'n'},
557 int cmd_freq_info(int argc
, char **argv
)
560 extern int optind
, opterr
, optopt
;
561 int ret
= 0, cont
= 1;
562 unsigned int cpu
= 0;
563 unsigned int human
= 0;
564 int output_param
= 0;
567 ret
= getopt_long(argc
, argv
, "oefwldpgrasmybn", info_opts
,
609 fprintf(stderr
, "invalid or unknown argument\n");
614 switch (output_param
) {
616 if (!bitmask_isallclear(cpus_chosen
)) {
617 printf(_("The argument passed to this tool can't be "
618 "combined with passing a --cpu argument\n"));
628 /* Default is: show output of CPU 0 only */
629 if (bitmask_isallclear(cpus_chosen
))
630 bitmask_setbit(cpus_chosen
, 0);
632 switch (output_param
) {
634 printf(_("You can't specify more than one --cpu parameter and/or\n"
635 "more than one output-specific argument\n"));
638 printf(_("invalid or unknown argument\n"));
641 proc_cpufreq_output();
645 for (cpu
= bitmask_first(cpus_chosen
);
646 cpu
<= bitmask_last(cpus_chosen
); cpu
++) {
648 if (!bitmask_isbitset(cpus_chosen
, cpu
))
650 if (cpufreq_cpu_exists(cpu
)) {
651 printf(_("couldn't analyze CPU %d as it doesn't seem to be present\n"), cpu
);
654 printf(_("analyzing CPU %d:\n"), cpu
);
656 switch (output_param
) {
661 debug_output_one(cpu
);
664 ret
= get_affected_cpus(cpu
);
667 ret
= get_related_cpus(cpu
);
670 ret
= get_available_governors(cpu
);
673 ret
= get_policy(cpu
);
676 ret
= get_driver(cpu
);
679 ret
= get_hardware_limits(cpu
);
682 ret
= get_freq_hardware(cpu
, human
);
685 ret
= get_freq_kernel(cpu
, human
);
688 ret
= get_freq_stats(cpu
, human
);
691 ret
= get_latency(cpu
, human
);