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 if (cpupower_cpu_info
.vendor
== X86_VENDOR_AMD
&&
169 cpupower_cpu_info
.family
>= 0x10) {
170 ret
= decode_pstates(cpu
, cpupower_cpu_info
.family
, b_states
,
171 pstates
, &pstate_no
);
175 printf(_(" Boost States: %d\n"), b_states
);
176 printf(_(" Total States: %d\n"), pstate_no
);
177 for (i
= 0; i
< pstate_no
; i
++) {
179 printf(_(" Pstate-Pb%d: %luMHz (boost state)"
180 "\n"), i
, pstates
[i
]);
182 printf(_(" Pstate-P%d: %luMHz\n"),
183 i
- b_states
, pstates
[i
]);
185 } else if (cpupower_cpu_info
.caps
& CPUPOWER_CAP_HAS_TURBO_RATIO
) {
187 unsigned long long intel_turbo_ratio
= 0;
190 /* Any way to autodetect this ? */
191 if (cpupower_cpu_info
.caps
& CPUPOWER_CAP_IS_SNB
)
195 intel_turbo_ratio
= msr_intel_get_turbo_ratio(cpu
);
196 dprint (" Ratio: 0x%llx - bclk: %f\n",
197 intel_turbo_ratio
, bclk
);
199 ratio
= (intel_turbo_ratio
>> 24) & 0xFF;
201 printf(_(" %.0f MHz max turbo 4 active cores\n"),
204 ratio
= (intel_turbo_ratio
>> 16) & 0xFF;
206 printf(_(" %.0f MHz max turbo 3 active cores\n"),
209 ratio
= (intel_turbo_ratio
>> 8) & 0xFF;
211 printf(_(" %.0f MHz max turbo 2 active cores\n"),
214 ratio
= (intel_turbo_ratio
>> 0) & 0xFF;
216 printf(_(" %.0f MHz max turbo 1 active cores\n"),
222 static void debug_output_one(unsigned int cpu
)
225 struct cpufreq_affected_cpus
*cpus
;
226 struct cpufreq_available_frequencies
*freqs
;
227 unsigned long min
, max
, freq_kernel
, freq_hardware
;
228 unsigned long total_trans
, latency
;
229 unsigned long long total_time
;
230 struct cpufreq_policy
*policy
;
231 struct cpufreq_available_governors
*governors
;
232 struct cpufreq_stats
*stats
;
234 if (cpufreq_cpu_exists(cpu
))
237 freq_kernel
= cpufreq_get_freq_kernel(cpu
);
238 freq_hardware
= cpufreq_get_freq_hardware(cpu
);
240 driver
= cpufreq_get_driver(cpu
);
242 printf(_(" no or unknown cpufreq driver is active on this CPU\n"));
244 printf(_(" driver: %s\n"), driver
);
245 cpufreq_put_driver(driver
);
248 cpus
= cpufreq_get_related_cpus(cpu
);
250 printf(_(" CPUs which run at the same hardware frequency: "));
252 printf("%d ", cpus
->cpu
);
255 printf("%d\n", cpus
->cpu
);
256 cpufreq_put_related_cpus(cpus
);
259 cpus
= cpufreq_get_affected_cpus(cpu
);
261 printf(_(" CPUs which need to have their frequency coordinated by software: "));
263 printf("%d ", cpus
->cpu
);
266 printf("%d\n", cpus
->cpu
);
267 cpufreq_put_affected_cpus(cpus
);
270 latency
= cpufreq_get_transition_latency(cpu
);
272 printf(_(" maximum transition latency: "));
273 print_duration(latency
);
277 if (!(cpufreq_get_hardware_limits(cpu
, &min
, &max
))) {
278 printf(_(" hardware limits: "));
285 freqs
= cpufreq_get_available_frequencies(cpu
);
287 printf(_(" available frequency steps: "));
288 while (freqs
->next
) {
289 print_speed(freqs
->frequency
);
293 print_speed(freqs
->frequency
);
295 cpufreq_put_available_frequencies(freqs
);
298 governors
= cpufreq_get_available_governors(cpu
);
300 printf(_(" available cpufreq governors: "));
301 while (governors
->next
) {
302 printf("%s, ", governors
->governor
);
303 governors
= governors
->next
;
305 printf("%s\n", governors
->governor
);
306 cpufreq_put_available_governors(governors
);
309 policy
= cpufreq_get_policy(cpu
);
311 printf(_(" current policy: frequency should be within "));
312 print_speed(policy
->min
);
314 print_speed(policy
->max
);
317 printf(_("The governor \"%s\" may"
318 " decide which speed to use\n within this range.\n"),
320 cpufreq_put_policy(policy
);
323 if (freq_kernel
|| freq_hardware
) {
324 printf(_(" current CPU frequency is "));
326 print_speed(freq_hardware
);
327 printf(_(" (asserted by call to hardware)"));
329 print_speed(freq_kernel
);
332 stats
= cpufreq_get_stats(cpu
, &total_time
);
334 printf(_(" cpufreq stats: "));
336 print_speed(stats
->frequency
);
337 printf(":%.2f%%", (100.0 * stats
->time_in_state
) / total_time
);
342 cpufreq_put_stats(stats
);
343 total_trans
= cpufreq_get_transitions(cpu
);
345 printf(" (%lu)\n", total_trans
);
355 static int get_freq_kernel(unsigned int cpu
, unsigned int human
)
357 unsigned long freq
= cpufreq_get_freq_kernel(cpu
);
364 printf("%lu\n", freq
);
371 static int get_freq_hardware(unsigned int cpu
, unsigned int human
)
373 unsigned long freq
= cpufreq_get_freq_hardware(cpu
);
380 printf("%lu\n", freq
);
384 /* --hwlimits / -l */
386 static int get_hardware_limits(unsigned int cpu
)
388 unsigned long min
, max
;
389 if (cpufreq_get_hardware_limits(cpu
, &min
, &max
))
391 printf("%lu %lu\n", min
, max
);
397 static int get_driver(unsigned int cpu
)
399 char *driver
= cpufreq_get_driver(cpu
);
402 printf("%s\n", driver
);
403 cpufreq_put_driver(driver
);
409 static int get_policy(unsigned int cpu
)
411 struct cpufreq_policy
*policy
= cpufreq_get_policy(cpu
);
414 printf("%lu %lu %s\n", policy
->min
, policy
->max
, policy
->governor
);
415 cpufreq_put_policy(policy
);
419 /* --governors / -g */
421 static int get_available_governors(unsigned int cpu
)
423 struct cpufreq_available_governors
*governors
=
424 cpufreq_get_available_governors(cpu
);
428 while (governors
->next
) {
429 printf("%s ", governors
->governor
);
430 governors
= governors
->next
;
432 printf("%s\n", governors
->governor
);
433 cpufreq_put_available_governors(governors
);
438 /* --affected-cpus / -a */
440 static int get_affected_cpus(unsigned int cpu
)
442 struct cpufreq_affected_cpus
*cpus
= cpufreq_get_affected_cpus(cpu
);
447 printf("%d ", cpus
->cpu
);
450 printf("%d\n", cpus
->cpu
);
451 cpufreq_put_affected_cpus(cpus
);
455 /* --related-cpus / -r */
457 static int get_related_cpus(unsigned int cpu
)
459 struct cpufreq_affected_cpus
*cpus
= cpufreq_get_related_cpus(cpu
);
464 printf("%d ", cpus
->cpu
);
467 printf("%d\n", cpus
->cpu
);
468 cpufreq_put_related_cpus(cpus
);
474 static int get_freq_stats(unsigned int cpu
, unsigned int human
)
476 unsigned long total_trans
= cpufreq_get_transitions(cpu
);
477 unsigned long long total_time
;
478 struct cpufreq_stats
*stats
= cpufreq_get_stats(cpu
, &total_time
);
481 print_speed(stats
->frequency
);
483 (100.0 * stats
->time_in_state
) / total_time
);
486 stats
->frequency
, stats
->time_in_state
);
491 cpufreq_put_stats(stats
);
493 printf(" (%lu)\n", total_trans
);
499 static int get_latency(unsigned int cpu
, unsigned int human
)
501 unsigned long latency
= cpufreq_get_transition_latency(cpu
);
506 print_duration(latency
);
509 printf("%lu\n", latency
);
513 static struct option info_opts
[] = {
514 { .name
= "debug", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'e'},
515 { .name
= "boost", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'b'},
516 { .name
= "freq", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'f'},
517 { .name
= "hwfreq", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'w'},
518 { .name
= "hwlimits", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'l'},
519 { .name
= "driver", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'd'},
520 { .name
= "policy", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'p'},
521 { .name
= "governors", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'g'},
522 { .name
= "related-cpus", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'r'},
523 { .name
= "affected-cpus",.has_arg
= no_argument
, .flag
= NULL
, .val
= 'a'},
524 { .name
= "stats", .has_arg
= no_argument
, .flag
= NULL
, .val
= 's'},
525 { .name
= "latency", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'y'},
526 { .name
= "proc", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'o'},
527 { .name
= "human", .has_arg
= no_argument
, .flag
= NULL
, .val
= 'm'},
531 int cmd_freq_info(int argc
, char **argv
)
534 extern int optind
, opterr
, optopt
;
535 int ret
= 0, cont
= 1;
536 unsigned int cpu
= 0;
537 unsigned int human
= 0;
538 int output_param
= 0;
541 ret
= getopt_long(argc
, argv
, "oefwldpgrasmyb", info_opts
, NULL
);
579 fprintf(stderr
, "invalid or unknown argument\n");
584 switch (output_param
) {
586 if (!bitmask_isallclear(cpus_chosen
)) {
587 printf(_("The argument passed to this tool can't be "
588 "combined with passing a --cpu argument\n"));
598 /* Default is: show output of CPU 0 only */
599 if (bitmask_isallclear(cpus_chosen
))
600 bitmask_setbit(cpus_chosen
, 0);
602 switch (output_param
) {
604 printf(_("You can't specify more than one --cpu parameter and/or\n"
605 "more than one output-specific argument\n"));
608 printf(_("invalid or unknown argument\n"));
611 proc_cpufreq_output();
615 for (cpu
= bitmask_first(cpus_chosen
);
616 cpu
<= bitmask_last(cpus_chosen
); cpu
++) {
618 if (!bitmask_isbitset(cpus_chosen
, cpu
))
620 if (cpufreq_cpu_exists(cpu
)) {
621 printf(_("couldn't analyze CPU %d as it doesn't seem to be present\n"), cpu
);
624 printf(_("analyzing CPU %d:\n"), cpu
);
626 switch (output_param
) {
631 debug_output_one(cpu
);
634 ret
= get_affected_cpus(cpu
);
637 ret
= get_related_cpus(cpu
);
640 ret
= get_available_governors(cpu
);
643 ret
= get_policy(cpu
);
646 ret
= get_driver(cpu
);
649 ret
= get_hardware_limits(cpu
);
652 ret
= get_freq_hardware(cpu
, human
);
655 ret
= get_freq_kernel(cpu
, human
);
658 ret
= get_freq_stats(cpu
, human
);
661 ret
= get_latency(cpu
, human
);