1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
17 #include "helpers/sysfs.h"
18 #include "helpers/helpers.h"
19 #include "helpers/bitmask.h"
23 static unsigned int count_cpus(void)
28 unsigned int cpunr
= 0;
30 fp
= fopen("/proc/stat", "r");
32 printf(_("Couldn't count the number of CPUs (%s: %s), assuming 1\n"), "/proc/stat", strerror(errno
));
37 if (!fgets(value
, LINE_LEN
, fp
))
39 value
[LINE_LEN
- 1] = '\0';
40 if (strlen(value
) < (LINE_LEN
- 2))
42 if (strstr(value
, "cpu "))
44 if (sscanf(value
, "cpu%d ", &cpunr
) != 1)
51 /* cpu count starts from 0, on error return 1 (UP) */
56 static void proc_cpufreq_output(void)
58 unsigned int cpu
, nr_cpus
;
59 struct cpufreq_policy
*policy
;
60 unsigned int min_pctg
= 0;
61 unsigned int max_pctg
= 0;
62 unsigned long min
, max
;
64 printf(_(" minimum CPU frequency - maximum CPU frequency - governor\n"));
66 nr_cpus
= count_cpus();
67 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
68 policy
= cpufreq_get_policy(cpu
);
72 if (cpufreq_get_hardware_limits(cpu
, &min
, &max
)) {
75 min_pctg
= (policy
->min
* 100) / max
;
76 max_pctg
= (policy
->max
* 100) / max
;
78 printf("CPU%3d %9lu kHz (%3d %%) - %9lu kHz (%3d %%) - %s\n",
79 cpu
, policy
->min
, max
? min_pctg
: 0, policy
->max
,
80 max
? max_pctg
: 0, policy
->governor
);
82 cpufreq_put_policy(policy
);
86 static int no_rounding
;
87 static void print_duration(unsigned long duration
)
92 if (duration
> 1000000)
93 printf("%u.%06u ms", ((unsigned int) duration
/1000000),
94 ((unsigned int) duration
%1000000));
95 else if (duration
> 100000)
96 printf("%u us", ((unsigned int) duration
/1000));
97 else if (duration
> 1000)
98 printf("%u.%03u us", ((unsigned int) duration
/1000),
99 ((unsigned int) duration
%1000));
101 printf("%lu ns", duration
);
103 if (duration
> 1000000) {
104 tmp
= duration
%10000;
107 printf("%u.%02u ms", ((unsigned int) duration
/1000000),
108 ((unsigned int) (duration
%1000000)/10000));
109 } else if (duration
> 100000) {
113 printf("%u us", ((unsigned int) duration
/ 1000));
114 } else if (duration
> 1000) {
118 printf("%u.%01u us", ((unsigned int) duration
/1000),
119 ((unsigned int) (duration
%1000)/100));
121 printf("%lu ns", duration
);
125 static int get_boost_mode_x86(unsigned int cpu
)
127 int support
, active
, b_states
= 0, ret
, pstate_no
, i
;
128 /* ToDo: Make this more global */
129 unsigned long pstates
[MAX_HW_PSTATES
] = {0,};
131 ret
= cpufreq_has_boost_support(cpu
, &support
, &active
, &b_states
);
133 printf(_("Error while evaluating Boost Capabilities"
134 " on CPU %d -- are you root?\n"), cpu
);
137 /* P state changes via MSR are identified via cpuid 80000007
138 on Intel and AMD, but we assume boost capable machines can do that
139 if (cpuid_eax(0x80000000) >= 0x80000007
140 && (cpuid_edx(0x80000007) & (1 << 7)))
143 printf(_(" boost state support:\n"));
145 printf(_(" Supported: %s\n"), support
? _("yes") : _("no"));
146 printf(_(" Active: %s\n"), active
? _("yes") : _("no"));
148 if (cpupower_cpu_info
.vendor
== X86_VENDOR_AMD
&&
149 cpupower_cpu_info
.caps
& CPUPOWER_CAP_AMD_PSTATE
) {
151 } else if ((cpupower_cpu_info
.vendor
== X86_VENDOR_AMD
&&
152 cpupower_cpu_info
.family
>= 0x10) ||
153 cpupower_cpu_info
.vendor
== X86_VENDOR_HYGON
) {
154 ret
= decode_pstates(cpu
, b_states
, pstates
, &pstate_no
);
158 printf(_(" Boost States: %d\n"), b_states
);
159 printf(_(" Total States: %d\n"), pstate_no
);
160 for (i
= 0; i
< pstate_no
; i
++) {
164 printf(_(" Pstate-Pb%d: %luMHz (boost state)"
165 "\n"), i
, pstates
[i
]);
167 printf(_(" Pstate-P%d: %luMHz\n"),
168 i
- b_states
, pstates
[i
]);
170 } else if (cpupower_cpu_info
.caps
& CPUPOWER_CAP_HAS_TURBO_RATIO
) {
172 unsigned long long intel_turbo_ratio
= 0;
175 /* Any way to autodetect this ? */
176 if (cpupower_cpu_info
.caps
& CPUPOWER_CAP_IS_SNB
)
180 intel_turbo_ratio
= msr_intel_get_turbo_ratio(cpu
);
181 dprint (" Ratio: 0x%llx - bclk: %f\n",
182 intel_turbo_ratio
, bclk
);
184 ratio
= (intel_turbo_ratio
>> 24) & 0xFF;
186 printf(_(" %.0f MHz max turbo 4 active cores\n"),
189 ratio
= (intel_turbo_ratio
>> 16) & 0xFF;
191 printf(_(" %.0f MHz max turbo 3 active cores\n"),
194 ratio
= (intel_turbo_ratio
>> 8) & 0xFF;
196 printf(_(" %.0f MHz max turbo 2 active cores\n"),
199 ratio
= (intel_turbo_ratio
>> 0) & 0xFF;
201 printf(_(" %.0f MHz max turbo 1 active cores\n"),
209 static int get_boost_mode(unsigned int cpu
)
211 struct cpufreq_available_frequencies
*freqs
;
213 if (cpupower_cpu_info
.vendor
== X86_VENDOR_AMD
||
214 cpupower_cpu_info
.vendor
== X86_VENDOR_HYGON
||
215 cpupower_cpu_info
.vendor
== X86_VENDOR_INTEL
)
216 return get_boost_mode_x86(cpu
);
218 freqs
= cpufreq_get_boost_frequencies(cpu
);
220 printf(_(" boost frequency steps: "));
221 while (freqs
->next
) {
222 print_speed(freqs
->frequency
, no_rounding
);
226 print_speed(freqs
->frequency
, no_rounding
);
228 cpufreq_put_available_frequencies(freqs
);
236 static int get_freq_kernel(unsigned int cpu
, unsigned int human
)
238 unsigned long freq
= cpufreq_get_freq_kernel(cpu
);
239 printf(_(" current CPU frequency: "));
241 printf(_(" Unable to call to kernel\n"));
245 print_speed(freq
, no_rounding
);
248 printf(_(" (asserted by call to kernel)\n"));
255 static int get_freq_hardware(unsigned int cpu
, unsigned int human
)
259 if (cpupower_cpu_info
.caps
& CPUPOWER_CAP_APERF
)
262 freq
= cpufreq_get_freq_hardware(cpu
);
263 printf(_(" current CPU frequency: "));
265 printf("Unable to call hardware\n");
269 print_speed(freq
, no_rounding
);
272 printf(_(" (asserted by call to hardware)\n"));
276 /* --hwlimits / -l */
278 static int get_hardware_limits(unsigned int cpu
, unsigned int human
)
280 unsigned long min
, max
;
282 if (cpufreq_get_hardware_limits(cpu
, &min
, &max
)) {
283 printf(_("Not Available\n"));
288 printf(_(" hardware limits: "));
289 print_speed(min
, no_rounding
);
291 print_speed(max
, no_rounding
);
294 printf("%lu %lu\n", min
, max
);
301 static int get_driver(unsigned int cpu
)
303 char *driver
= cpufreq_get_driver(cpu
);
305 printf(_(" no or unknown cpufreq driver is active on this CPU\n"));
308 printf(" driver: %s\n", driver
);
309 cpufreq_put_driver(driver
);
315 static int get_policy(unsigned int cpu
)
317 struct cpufreq_policy
*policy
= cpufreq_get_policy(cpu
);
319 printf(_(" Unable to determine current policy\n"));
322 printf(_(" current policy: frequency should be within "));
323 print_speed(policy
->min
, no_rounding
);
325 print_speed(policy
->max
, no_rounding
);
328 printf(_("The governor \"%s\" may decide which speed to use\n"
329 " within this range.\n"),
331 cpufreq_put_policy(policy
);
335 /* --governors / -g */
337 static int get_available_governors(unsigned int cpu
)
339 struct cpufreq_available_governors
*governors
=
340 cpufreq_get_available_governors(cpu
);
342 printf(_(" available cpufreq governors: "));
344 printf(_("Not Available\n"));
348 while (governors
->next
) {
349 printf("%s ", governors
->governor
);
350 governors
= governors
->next
;
352 printf("%s\n", governors
->governor
);
353 cpufreq_put_available_governors(governors
);
358 /* --affected-cpus / -a */
360 static int get_affected_cpus(unsigned int cpu
)
362 struct cpufreq_affected_cpus
*cpus
= cpufreq_get_affected_cpus(cpu
);
364 printf(_(" CPUs which need to have their frequency coordinated by software: "));
366 printf(_("Not Available\n"));
371 printf("%d ", cpus
->cpu
);
374 printf("%d\n", cpus
->cpu
);
375 cpufreq_put_affected_cpus(cpus
);
379 /* --related-cpus / -r */
381 static int get_related_cpus(unsigned int cpu
)
383 struct cpufreq_affected_cpus
*cpus
= cpufreq_get_related_cpus(cpu
);
385 printf(_(" CPUs which run at the same hardware frequency: "));
387 printf(_("Not Available\n"));
392 printf("%d ", cpus
->cpu
);
395 printf("%d\n", cpus
->cpu
);
396 cpufreq_put_related_cpus(cpus
);
402 static int get_freq_stats(unsigned int cpu
, unsigned int human
)
404 unsigned long total_trans
= cpufreq_get_transitions(cpu
);
405 unsigned long long total_time
;
406 struct cpufreq_stats
*stats
= cpufreq_get_stats(cpu
, &total_time
);
409 print_speed(stats
->frequency
, no_rounding
);
411 (100.0 * stats
->time_in_state
) / total_time
);
414 stats
->frequency
, stats
->time_in_state
);
419 cpufreq_put_stats(stats
);
421 printf(" (%lu)\n", total_trans
);
427 static int get_epp(unsigned int cpu
, bool interactive
)
431 epp
= cpufreq_get_energy_performance_preference(cpu
);
435 printf(_(" energy performance preference: %s\n"), epp
);
437 cpufreq_put_energy_performance_preference(epp
);
444 static int get_latency(unsigned int cpu
, unsigned int human
)
446 unsigned long latency
= cpufreq_get_transition_latency(cpu
);
448 if (!get_epp(cpu
, false))
451 printf(_(" maximum transition latency: "));
452 if (!latency
|| latency
== UINT_MAX
) {
453 printf(_(" Cannot determine or is not supported.\n"));
458 print_duration(latency
);
461 printf("%lu\n", latency
);
465 /* --performance / -c */
467 static int get_perf_cap(unsigned int cpu
)
469 if (cpupower_cpu_info
.vendor
== X86_VENDOR_AMD
&&
470 cpupower_cpu_info
.caps
& CPUPOWER_CAP_AMD_PSTATE
)
471 amd_pstate_show_perf_and_freq(cpu
, no_rounding
);
476 static void debug_output_one(unsigned int cpu
)
478 struct cpufreq_available_frequencies
*freqs
;
481 get_related_cpus(cpu
);
482 get_affected_cpus(cpu
);
485 get_hardware_limits(cpu
, 1);
487 freqs
= cpufreq_get_available_frequencies(cpu
);
489 printf(_(" available frequency steps: "));
490 while (freqs
->next
) {
491 print_speed(freqs
->frequency
, no_rounding
);
495 print_speed(freqs
->frequency
, no_rounding
);
497 cpufreq_put_available_frequencies(freqs
);
500 get_available_governors(cpu
);
502 if (get_freq_hardware(cpu
, 1) < 0)
503 get_freq_kernel(cpu
, 1);
508 static struct option info_opts
[] = {
509 {"debug", no_argument
, NULL
, 'e'},
510 {"boost", no_argument
, NULL
, 'b'},
511 {"freq", no_argument
, NULL
, 'f'},
512 {"hwfreq", no_argument
, NULL
, 'w'},
513 {"hwlimits", no_argument
, NULL
, 'l'},
514 {"driver", no_argument
, NULL
, 'd'},
515 {"policy", no_argument
, NULL
, 'p'},
516 {"governors", no_argument
, NULL
, 'g'},
517 {"related-cpus", no_argument
, NULL
, 'r'},
518 {"affected-cpus", no_argument
, NULL
, 'a'},
519 {"stats", no_argument
, NULL
, 's'},
520 {"latency", no_argument
, NULL
, 'y'},
521 {"proc", no_argument
, NULL
, 'o'},
522 {"human", no_argument
, NULL
, 'm'},
523 {"no-rounding", no_argument
, NULL
, 'n'},
524 {"performance", no_argument
, NULL
, 'c'},
525 {"epp", no_argument
, NULL
, 'z'},
529 int cmd_freq_info(int argc
, char **argv
)
532 extern int optind
, opterr
, optopt
;
533 int ret
= 0, cont
= 1;
534 unsigned int cpu
= 0;
535 unsigned int human
= 0;
536 int output_param
= 0;
539 ret
= getopt_long(argc
, argv
, "oefwldpgrasmybncz", info_opts
,
583 fprintf(stderr
, "invalid or unknown argument\n");
588 switch (output_param
) {
590 if (!bitmask_isallclear(cpus_chosen
)) {
591 printf(_("The argument passed to this tool can't be "
592 "combined with passing a --cpu argument\n"));
602 /* Default is: show output of base_cpu only */
603 if (bitmask_isallclear(cpus_chosen
))
604 bitmask_setbit(cpus_chosen
, base_cpu
);
606 switch (output_param
) {
608 printf(_("You can't specify more than one --cpu parameter and/or\n"
609 "more than one output-specific argument\n"));
612 printf(_("invalid or unknown argument\n"));
615 proc_cpufreq_output();
619 for (cpu
= bitmask_first(cpus_chosen
);
620 cpu
<= bitmask_last(cpus_chosen
); cpu
++) {
622 if (!bitmask_isbitset(cpus_chosen
, cpu
))
625 printf(_("analyzing CPU %d:\n"), cpu
);
627 if (sysfs_is_cpu_online(cpu
) != 1) {
628 printf(_(" *is offline\n"));
633 switch (output_param
) {
638 debug_output_one(cpu
);
641 ret
= get_affected_cpus(cpu
);
644 ret
= get_related_cpus(cpu
);
647 ret
= get_available_governors(cpu
);
650 ret
= get_policy(cpu
);
653 ret
= get_driver(cpu
);
656 ret
= get_hardware_limits(cpu
, human
);
659 ret
= get_freq_hardware(cpu
, human
);
662 ret
= get_freq_kernel(cpu
, human
);
665 ret
= get_freq_stats(cpu
, human
);
668 ret
= get_latency(cpu
, human
);
671 ret
= get_perf_cap(cpu
);
674 ret
= get_epp(cpu
, true);