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);
126 static int get_boost_mode_x86(unsigned int cpu)
128 int support, active, b_states = 0, ret, pstate_no, i;
129 /* ToDo: Make this more global */
130 unsigned long pstates[MAX_HW_PSTATES] = {0,};
132 ret = cpufreq_has_boost_support(cpu, &support, &active, &b_states);
134 printf(_("Error while evaluating Boost Capabilities"
135 " on CPU %d -- are you root?\n"), cpu);
138 /* P state changes via MSR are identified via cpuid 80000007
139 on Intel and AMD, but we assume boost capable machines can do that
140 if (cpuid_eax(0x80000000) >= 0x80000007
141 && (cpuid_edx(0x80000007) & (1 << 7)))
144 printf(_(" boost state support:\n"));
146 printf(_(" Supported: %s\n"), support ? _("yes") : _("no"));
147 printf(_(" Active: %s\n"), active ? _("yes") : _("no"));
149 if (cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
150 cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATE) {
152 } else if ((cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
153 cpupower_cpu_info.family >= 0x10) ||
154 cpupower_cpu_info.vendor == X86_VENDOR_HYGON) {
155 ret = decode_pstates(cpu, b_states, pstates, &pstate_no);
159 printf(_(" Boost States: %d\n"), b_states);
160 printf(_(" Total States: %d\n"), pstate_no);
161 for (i = 0; i < pstate_no; i++) {
165 printf(_(" Pstate-Pb%d: %luMHz (boost state)"
166 "\n"), i, pstates[i]);
168 printf(_(" Pstate-P%d: %luMHz\n"),
169 i - b_states, pstates[i]);
171 } else if (cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO) {
173 unsigned long long intel_turbo_ratio = 0;
176 /* Any way to autodetect this ? */
177 if (cpupower_cpu_info.caps & CPUPOWER_CAP_IS_SNB)
181 intel_turbo_ratio = msr_intel_get_turbo_ratio(cpu);
182 dprint (" Ratio: 0x%llx - bclk: %f\n",
183 intel_turbo_ratio, bclk);
185 ratio = (intel_turbo_ratio >> 24) & 0xFF;
187 printf(_(" %.0f MHz max turbo 4 active cores\n"),
190 ratio = (intel_turbo_ratio >> 16) & 0xFF;
192 printf(_(" %.0f MHz max turbo 3 active cores\n"),
195 ratio = (intel_turbo_ratio >> 8) & 0xFF;
197 printf(_(" %.0f MHz max turbo 2 active cores\n"),
200 ratio = (intel_turbo_ratio >> 0) & 0xFF;
202 printf(_(" %.0f MHz max turbo 1 active cores\n"),
210 static int get_boost_mode(unsigned int cpu)
212 struct cpufreq_available_frequencies *freqs;
214 if (cpupower_cpu_info.vendor == X86_VENDOR_AMD ||
215 cpupower_cpu_info.vendor == X86_VENDOR_HYGON ||
216 cpupower_cpu_info.vendor == X86_VENDOR_INTEL)
217 return get_boost_mode_x86(cpu);
219 freqs = cpufreq_get_boost_frequencies(cpu);
221 printf(_(" boost frequency steps: "));
222 while (freqs->next) {
223 print_speed(freqs->frequency, no_rounding);
227 print_speed(freqs->frequency, no_rounding);
229 cpufreq_put_available_frequencies(freqs);
237 static int get_freq_kernel(unsigned int cpu, unsigned int human)
239 unsigned long freq = cpufreq_get_freq_kernel(cpu);
240 printf(_(" current CPU frequency: "));
242 printf(_(" Unable to call to kernel\n"));
246 print_speed(freq, no_rounding);
249 printf(_(" (asserted by call to kernel)\n"));
256 static int get_freq_hardware(unsigned int cpu, unsigned int human)
258 unsigned long freq = cpufreq_get_freq_hardware(cpu);
259 printf(_(" current CPU frequency: "));
261 printf("Unable to call hardware\n");
265 print_speed(freq, no_rounding);
268 printf(_(" (asserted by call to hardware)\n"));
272 /* --hwlimits / -l */
274 static int get_hardware_limits(unsigned int cpu, unsigned int human)
276 unsigned long min, max;
278 if (cpufreq_get_hardware_limits(cpu, &min, &max)) {
279 printf(_("Not Available\n"));
284 printf(_(" hardware limits: "));
285 print_speed(min, no_rounding);
287 print_speed(max, no_rounding);
290 printf("%lu %lu\n", min, max);
297 static int get_driver(unsigned int cpu)
299 char *driver = cpufreq_get_driver(cpu);
301 printf(_(" no or unknown cpufreq driver is active on this CPU\n"));
304 printf(" driver: %s\n", driver);
305 cpufreq_put_driver(driver);
311 static int get_policy(unsigned int cpu)
313 struct cpufreq_policy *policy = cpufreq_get_policy(cpu);
315 printf(_(" Unable to determine current policy\n"));
318 printf(_(" current policy: frequency should be within "));
319 print_speed(policy->min, no_rounding);
321 print_speed(policy->max, no_rounding);
324 printf(_("The governor \"%s\" may decide which speed to use\n"
325 " within this range.\n"),
327 cpufreq_put_policy(policy);
331 /* --governors / -g */
333 static int get_available_governors(unsigned int cpu)
335 struct cpufreq_available_governors *governors =
336 cpufreq_get_available_governors(cpu);
338 printf(_(" available cpufreq governors: "));
340 printf(_("Not Available\n"));
344 while (governors->next) {
345 printf("%s ", governors->governor);
346 governors = governors->next;
348 printf("%s\n", governors->governor);
349 cpufreq_put_available_governors(governors);
354 /* --affected-cpus / -a */
356 static int get_affected_cpus(unsigned int cpu)
358 struct cpufreq_affected_cpus *cpus = cpufreq_get_affected_cpus(cpu);
360 printf(_(" CPUs which need to have their frequency coordinated by software: "));
362 printf(_("Not Available\n"));
367 printf("%d ", cpus->cpu);
370 printf("%d\n", cpus->cpu);
371 cpufreq_put_affected_cpus(cpus);
375 /* --related-cpus / -r */
377 static int get_related_cpus(unsigned int cpu)
379 struct cpufreq_affected_cpus *cpus = cpufreq_get_related_cpus(cpu);
381 printf(_(" CPUs which run at the same hardware frequency: "));
383 printf(_("Not Available\n"));
388 printf("%d ", cpus->cpu);
391 printf("%d\n", cpus->cpu);
392 cpufreq_put_related_cpus(cpus);
398 static int get_freq_stats(unsigned int cpu, unsigned int human)
400 unsigned long total_trans = cpufreq_get_transitions(cpu);
401 unsigned long long total_time;
402 struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);
405 print_speed(stats->frequency, no_rounding);
407 (100.0 * stats->time_in_state) / total_time);
410 stats->frequency, stats->time_in_state);
415 cpufreq_put_stats(stats);
417 printf(" (%lu)\n", total_trans);
423 static int get_latency(unsigned int cpu, unsigned int human)
425 unsigned long latency = cpufreq_get_transition_latency(cpu);
427 printf(_(" maximum transition latency: "));
428 if (!latency || latency == UINT_MAX) {
429 printf(_(" Cannot determine or is not supported.\n"));
434 print_duration(latency);
437 printf("%lu\n", latency);
441 /* --performance / -c */
443 static int get_perf_cap(unsigned int cpu)
445 if (cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
446 cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATE)
447 amd_pstate_show_perf_and_freq(cpu, no_rounding);
452 static void debug_output_one(unsigned int cpu)
454 struct cpufreq_available_frequencies *freqs;
457 get_related_cpus(cpu);
458 get_affected_cpus(cpu);
460 get_hardware_limits(cpu, 1);
462 freqs = cpufreq_get_available_frequencies(cpu);
464 printf(_(" available frequency steps: "));
465 while (freqs->next) {
466 print_speed(freqs->frequency, no_rounding);
470 print_speed(freqs->frequency, no_rounding);
472 cpufreq_put_available_frequencies(freqs);
475 get_available_governors(cpu);
477 if (get_freq_hardware(cpu, 1) < 0)
478 get_freq_kernel(cpu, 1);
483 static struct option info_opts[] = {
484 {"debug", no_argument, NULL, 'e'},
485 {"boost", no_argument, NULL, 'b'},
486 {"freq", no_argument, NULL, 'f'},
487 {"hwfreq", no_argument, NULL, 'w'},
488 {"hwlimits", no_argument, NULL, 'l'},
489 {"driver", no_argument, NULL, 'd'},
490 {"policy", no_argument, NULL, 'p'},
491 {"governors", no_argument, NULL, 'g'},
492 {"related-cpus", no_argument, NULL, 'r'},
493 {"affected-cpus", no_argument, NULL, 'a'},
494 {"stats", no_argument, NULL, 's'},
495 {"latency", no_argument, NULL, 'y'},
496 {"proc", no_argument, NULL, 'o'},
497 {"human", no_argument, NULL, 'm'},
498 {"no-rounding", no_argument, NULL, 'n'},
499 {"performance", no_argument, NULL, 'c'},
503 int cmd_freq_info(int argc, char **argv)
506 extern int optind, opterr, optopt;
507 int ret = 0, cont = 1;
508 unsigned int cpu = 0;
509 unsigned int human = 0;
510 int output_param = 0;
513 ret = getopt_long(argc, argv, "oefwldpgrasmybnc", info_opts,
556 fprintf(stderr, "invalid or unknown argument\n");
561 switch (output_param) {
563 if (!bitmask_isallclear(cpus_chosen)) {
564 printf(_("The argument passed to this tool can't be "
565 "combined with passing a --cpu argument\n"));
575 /* Default is: show output of base_cpu only */
576 if (bitmask_isallclear(cpus_chosen))
577 bitmask_setbit(cpus_chosen, base_cpu);
579 switch (output_param) {
581 printf(_("You can't specify more than one --cpu parameter and/or\n"
582 "more than one output-specific argument\n"));
585 printf(_("invalid or unknown argument\n"));
588 proc_cpufreq_output();
592 for (cpu = bitmask_first(cpus_chosen);
593 cpu <= bitmask_last(cpus_chosen); cpu++) {
595 if (!bitmask_isbitset(cpus_chosen, cpu))
598 printf(_("analyzing CPU %d:\n"), cpu);
600 if (sysfs_is_cpu_online(cpu) != 1) {
601 printf(_(" *is offline\n"));
606 switch (output_param) {
611 debug_output_one(cpu);
614 ret = get_affected_cpus(cpu);
617 ret = get_related_cpus(cpu);
620 ret = get_available_governors(cpu);
623 ret = get_policy(cpu);
626 ret = get_driver(cpu);
629 ret = get_hardware_limits(cpu, human);
632 ret = get_freq_hardware(cpu, human);
635 ret = get_freq_kernel(cpu, human);
638 ret = get_freq_stats(cpu, human);
641 ret = get_latency(cpu, human);
644 ret = get_perf_cap(cpu);