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_speed(unsigned long speed)
93 printf("%u.%06u GHz", ((unsigned int) speed/1000000),
94 ((unsigned int) speed%1000000));
95 else if (speed > 1000)
96 printf("%u.%03u MHz", ((unsigned int) speed/1000),
97 (unsigned int) (speed%1000));
99 printf("%lu kHz", speed);
101 if (speed > 1000000) {
105 printf("%u.%02u GHz", ((unsigned int) speed/1000000),
106 ((unsigned int) (speed%1000000)/10000));
107 } else if (speed > 100000) {
111 printf("%u MHz", ((unsigned int) speed/1000));
112 } else if (speed > 1000) {
116 printf("%u.%01u MHz", ((unsigned int) speed/1000),
117 ((unsigned int) (speed%1000)/100));
124 static void print_duration(unsigned long duration)
129 if (duration > 1000000)
130 printf("%u.%06u ms", ((unsigned int) duration/1000000),
131 ((unsigned int) duration%1000000));
132 else if (duration > 100000)
133 printf("%u us", ((unsigned int) duration/1000));
134 else if (duration > 1000)
135 printf("%u.%03u us", ((unsigned int) duration/1000),
136 ((unsigned int) duration%1000));
138 printf("%lu ns", duration);
140 if (duration > 1000000) {
141 tmp = duration%10000;
144 printf("%u.%02u ms", ((unsigned int) duration/1000000),
145 ((unsigned int) (duration%1000000)/10000));
146 } else if (duration > 100000) {
150 printf("%u us", ((unsigned int) duration / 1000));
151 } else if (duration > 1000) {
155 printf("%u.%01u us", ((unsigned int) duration/1000),
156 ((unsigned int) (duration%1000)/100));
158 printf("%lu ns", duration);
163 static int get_boost_mode_x86(unsigned int cpu)
165 int support, active, b_states = 0, ret, pstate_no, i;
166 /* ToDo: Make this more global */
167 unsigned long pstates[MAX_HW_PSTATES] = {0,};
169 ret = cpufreq_has_boost_support(cpu, &support, &active, &b_states);
171 printf(_("Error while evaluating Boost Capabilities"
172 " on CPU %d -- are you root?\n"), cpu);
175 /* P state changes via MSR are identified via cpuid 80000007
176 on Intel and AMD, but we assume boost capable machines can do that
177 if (cpuid_eax(0x80000000) >= 0x80000007
178 && (cpuid_edx(0x80000007) & (1 << 7)))
181 printf(_(" boost state support:\n"));
183 printf(_(" Supported: %s\n"), support ? _("yes") : _("no"));
184 printf(_(" Active: %s\n"), active ? _("yes") : _("no"));
186 if ((cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
187 cpupower_cpu_info.family >= 0x10) ||
188 cpupower_cpu_info.vendor == X86_VENDOR_HYGON) {
189 ret = decode_pstates(cpu, cpupower_cpu_info.family, b_states,
190 pstates, &pstate_no);
194 printf(_(" Boost States: %d\n"), b_states);
195 printf(_(" Total States: %d\n"), pstate_no);
196 for (i = 0; i < pstate_no; i++) {
200 printf(_(" Pstate-Pb%d: %luMHz (boost state)"
201 "\n"), i, pstates[i]);
203 printf(_(" Pstate-P%d: %luMHz\n"),
204 i - b_states, pstates[i]);
206 } else if (cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO) {
208 unsigned long long intel_turbo_ratio = 0;
211 /* Any way to autodetect this ? */
212 if (cpupower_cpu_info.caps & CPUPOWER_CAP_IS_SNB)
216 intel_turbo_ratio = msr_intel_get_turbo_ratio(cpu);
217 dprint (" Ratio: 0x%llx - bclk: %f\n",
218 intel_turbo_ratio, bclk);
220 ratio = (intel_turbo_ratio >> 24) & 0xFF;
222 printf(_(" %.0f MHz max turbo 4 active cores\n"),
225 ratio = (intel_turbo_ratio >> 16) & 0xFF;
227 printf(_(" %.0f MHz max turbo 3 active cores\n"),
230 ratio = (intel_turbo_ratio >> 8) & 0xFF;
232 printf(_(" %.0f MHz max turbo 2 active cores\n"),
235 ratio = (intel_turbo_ratio >> 0) & 0xFF;
237 printf(_(" %.0f MHz max turbo 1 active cores\n"),
245 static int get_boost_mode(unsigned int cpu)
247 struct cpufreq_available_frequencies *freqs;
249 if (cpupower_cpu_info.vendor == X86_VENDOR_AMD ||
250 cpupower_cpu_info.vendor == X86_VENDOR_HYGON ||
251 cpupower_cpu_info.vendor == X86_VENDOR_INTEL)
252 return get_boost_mode_x86(cpu);
254 freqs = cpufreq_get_boost_frequencies(cpu);
256 printf(_(" boost frequency steps: "));
257 while (freqs->next) {
258 print_speed(freqs->frequency);
262 print_speed(freqs->frequency);
264 cpufreq_put_available_frequencies(freqs);
272 static int get_freq_kernel(unsigned int cpu, unsigned int human)
274 unsigned long freq = cpufreq_get_freq_kernel(cpu);
275 printf(_(" current CPU frequency: "));
277 printf(_(" Unable to call to kernel\n"));
284 printf(_(" (asserted by call to kernel)\n"));
291 static int get_freq_hardware(unsigned int cpu, unsigned int human)
293 unsigned long freq = cpufreq_get_freq_hardware(cpu);
294 printf(_(" current CPU frequency: "));
296 printf("Unable to call hardware\n");
303 printf(_(" (asserted by call to hardware)\n"));
307 /* --hwlimits / -l */
309 static int get_hardware_limits(unsigned int cpu, unsigned int human)
311 unsigned long min, max;
313 if (cpufreq_get_hardware_limits(cpu, &min, &max)) {
314 printf(_("Not Available\n"));
319 printf(_(" hardware limits: "));
325 printf("%lu %lu\n", min, max);
332 static int get_driver(unsigned int cpu)
334 char *driver = cpufreq_get_driver(cpu);
336 printf(_(" no or unknown cpufreq driver is active on this CPU\n"));
339 printf(" driver: %s\n", driver);
340 cpufreq_put_driver(driver);
346 static int get_policy(unsigned int cpu)
348 struct cpufreq_policy *policy = cpufreq_get_policy(cpu);
350 printf(_(" Unable to determine current policy\n"));
353 printf(_(" current policy: frequency should be within "));
354 print_speed(policy->min);
356 print_speed(policy->max);
359 printf(_("The governor \"%s\" may decide which speed to use\n"
360 " within this range.\n"),
362 cpufreq_put_policy(policy);
366 /* --governors / -g */
368 static int get_available_governors(unsigned int cpu)
370 struct cpufreq_available_governors *governors =
371 cpufreq_get_available_governors(cpu);
373 printf(_(" available cpufreq governors: "));
375 printf(_("Not Available\n"));
379 while (governors->next) {
380 printf("%s ", governors->governor);
381 governors = governors->next;
383 printf("%s\n", governors->governor);
384 cpufreq_put_available_governors(governors);
389 /* --affected-cpus / -a */
391 static int get_affected_cpus(unsigned int cpu)
393 struct cpufreq_affected_cpus *cpus = cpufreq_get_affected_cpus(cpu);
395 printf(_(" CPUs which need to have their frequency coordinated by software: "));
397 printf(_("Not Available\n"));
402 printf("%d ", cpus->cpu);
405 printf("%d\n", cpus->cpu);
406 cpufreq_put_affected_cpus(cpus);
410 /* --related-cpus / -r */
412 static int get_related_cpus(unsigned int cpu)
414 struct cpufreq_affected_cpus *cpus = cpufreq_get_related_cpus(cpu);
416 printf(_(" CPUs which run at the same hardware frequency: "));
418 printf(_("Not Available\n"));
423 printf("%d ", cpus->cpu);
426 printf("%d\n", cpus->cpu);
427 cpufreq_put_related_cpus(cpus);
433 static int get_freq_stats(unsigned int cpu, unsigned int human)
435 unsigned long total_trans = cpufreq_get_transitions(cpu);
436 unsigned long long total_time;
437 struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);
440 print_speed(stats->frequency);
442 (100.0 * stats->time_in_state) / total_time);
445 stats->frequency, stats->time_in_state);
450 cpufreq_put_stats(stats);
452 printf(" (%lu)\n", total_trans);
458 static int get_latency(unsigned int cpu, unsigned int human)
460 unsigned long latency = cpufreq_get_transition_latency(cpu);
462 printf(_(" maximum transition latency: "));
463 if (!latency || latency == UINT_MAX) {
464 printf(_(" Cannot determine or is not supported.\n"));
469 print_duration(latency);
472 printf("%lu\n", latency);
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);
484 get_hardware_limits(cpu, 1);
486 freqs = cpufreq_get_available_frequencies(cpu);
488 printf(_(" available frequency steps: "));
489 while (freqs->next) {
490 print_speed(freqs->frequency);
494 print_speed(freqs->frequency);
496 cpufreq_put_available_frequencies(freqs);
499 get_available_governors(cpu);
501 if (get_freq_hardware(cpu, 1) < 0)
502 get_freq_kernel(cpu, 1);
506 static struct option info_opts[] = {
507 {"debug", no_argument, NULL, 'e'},
508 {"boost", no_argument, NULL, 'b'},
509 {"freq", no_argument, NULL, 'f'},
510 {"hwfreq", no_argument, NULL, 'w'},
511 {"hwlimits", no_argument, NULL, 'l'},
512 {"driver", no_argument, NULL, 'd'},
513 {"policy", no_argument, NULL, 'p'},
514 {"governors", no_argument, NULL, 'g'},
515 {"related-cpus", no_argument, NULL, 'r'},
516 {"affected-cpus", no_argument, NULL, 'a'},
517 {"stats", no_argument, NULL, 's'},
518 {"latency", no_argument, NULL, 'y'},
519 {"proc", no_argument, NULL, 'o'},
520 {"human", no_argument, NULL, 'm'},
521 {"no-rounding", no_argument, NULL, 'n'},
525 int cmd_freq_info(int argc, char **argv)
528 extern int optind, opterr, optopt;
529 int ret = 0, cont = 1;
530 unsigned int cpu = 0;
531 unsigned int human = 0;
532 int output_param = 0;
535 ret = getopt_long(argc, argv, "oefwldpgrasmybn", info_opts,
577 fprintf(stderr, "invalid or unknown argument\n");
582 switch (output_param) {
584 if (!bitmask_isallclear(cpus_chosen)) {
585 printf(_("The argument passed to this tool can't be "
586 "combined with passing a --cpu argument\n"));
596 /* Default is: show output of CPU 0 only */
597 if (bitmask_isallclear(cpus_chosen))
598 bitmask_setbit(cpus_chosen, 0);
600 switch (output_param) {
602 printf(_("You can't specify more than one --cpu parameter and/or\n"
603 "more than one output-specific argument\n"));
606 printf(_("invalid or unknown argument\n"));
609 proc_cpufreq_output();
613 for (cpu = bitmask_first(cpus_chosen);
614 cpu <= bitmask_last(cpus_chosen); cpu++) {
616 if (!bitmask_isbitset(cpus_chosen, cpu))
619 printf(_("analyzing CPU %d:\n"), cpu);
621 if (sysfs_is_cpu_online(cpu) != 1) {
622 printf(_(" *is offline\n"));
627 switch (output_param) {
632 debug_output_one(cpu);
635 ret = get_affected_cpus(cpu);
638 ret = get_related_cpus(cpu);
641 ret = get_available_governors(cpu);
644 ret = get_policy(cpu);
647 ret = get_driver(cpu);
650 ret = get_hardware_limits(cpu, human);
653 ret = get_freq_hardware(cpu, human);
656 ret = get_freq_kernel(cpu, human);
659 ret = get_freq_stats(cpu, human);
662 ret = get_latency(cpu, human);