2 * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
4 * Licensed under the terms of the GNU GPL License version 2.
18 #include "helpers/sysfs.h"
19 #include "helpers/helpers.h"
20 #include "helpers/bitmask.h"
24 static unsigned int count_cpus(void)
29 unsigned int cpunr = 0;
31 fp = fopen("/proc/stat", "r");
33 printf(_("Couldn't count the number of CPUs (%s: %s), assuming 1\n"), "/proc/stat", strerror(errno));
38 if (!fgets(value, LINE_LEN, fp))
40 value[LINE_LEN - 1] = '\0';
41 if (strlen(value) < (LINE_LEN - 2))
43 if (strstr(value, "cpu "))
45 if (sscanf(value, "cpu%d ", &cpunr) != 1)
52 /* cpu count starts from 0, on error return 1 (UP) */
57 static void proc_cpufreq_output(void)
59 unsigned int cpu, nr_cpus;
60 struct cpufreq_policy *policy;
61 unsigned int min_pctg = 0;
62 unsigned int max_pctg = 0;
63 unsigned long min, max;
65 printf(_(" minimum CPU frequency - maximum CPU frequency - governor\n"));
67 nr_cpus = count_cpus();
68 for (cpu = 0; cpu < nr_cpus; cpu++) {
69 policy = cpufreq_get_policy(cpu);
73 if (cpufreq_get_hardware_limits(cpu, &min, &max)) {
76 min_pctg = (policy->min * 100) / max;
77 max_pctg = (policy->max * 100) / max;
79 printf("CPU%3d %9lu kHz (%3d %%) - %9lu kHz (%3d %%) - %s\n",
80 cpu , policy->min, max ? min_pctg : 0, policy->max,
81 max ? max_pctg : 0, policy->governor);
83 cpufreq_put_policy(policy);
87 static int no_rounding;
88 static void print_speed(unsigned long speed)
94 printf("%u.%06u GHz", ((unsigned int) speed/1000000),
95 ((unsigned int) speed%1000000));
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);
164 static int get_boost_mode_x86(unsigned int cpu)
166 int support, active, b_states = 0, ret, pstate_no, i;
167 /* ToDo: Make this more global */
168 unsigned long pstates[MAX_HW_PSTATES] = {0,};
170 ret = cpufreq_has_boost_support(cpu, &support, &active, &b_states);
172 printf(_("Error while evaluating Boost Capabilities"
173 " on CPU %d -- are you root?\n"), cpu);
176 /* P state changes via MSR are identified via cpuid 80000007
177 on Intel and AMD, but we assume boost capable machines can do that
178 if (cpuid_eax(0x80000000) >= 0x80000007
179 && (cpuid_edx(0x80000007) & (1 << 7)))
182 printf(_(" boost state support:\n"));
184 printf(_(" Supported: %s\n"), support ? _("yes") : _("no"));
185 printf(_(" Active: %s\n"), active ? _("yes") : _("no"));
187 if ((cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
188 cpupower_cpu_info.family >= 0x10) ||
189 cpupower_cpu_info.vendor == X86_VENDOR_HYGON) {
190 ret = decode_pstates(cpu, cpupower_cpu_info.family, b_states,
191 pstates, &pstate_no);
195 printf(_(" Boost States: %d\n"), b_states);
196 printf(_(" Total States: %d\n"), pstate_no);
197 for (i = 0; i < pstate_no; i++) {
201 printf(_(" Pstate-Pb%d: %luMHz (boost state)"
202 "\n"), i, pstates[i]);
204 printf(_(" Pstate-P%d: %luMHz\n"),
205 i - b_states, pstates[i]);
207 } else if (cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO) {
209 unsigned long long intel_turbo_ratio = 0;
212 /* Any way to autodetect this ? */
213 if (cpupower_cpu_info.caps & CPUPOWER_CAP_IS_SNB)
217 intel_turbo_ratio = msr_intel_get_turbo_ratio(cpu);
218 dprint (" Ratio: 0x%llx - bclk: %f\n",
219 intel_turbo_ratio, bclk);
221 ratio = (intel_turbo_ratio >> 24) & 0xFF;
223 printf(_(" %.0f MHz max turbo 4 active cores\n"),
226 ratio = (intel_turbo_ratio >> 16) & 0xFF;
228 printf(_(" %.0f MHz max turbo 3 active cores\n"),
231 ratio = (intel_turbo_ratio >> 8) & 0xFF;
233 printf(_(" %.0f MHz max turbo 2 active cores\n"),
236 ratio = (intel_turbo_ratio >> 0) & 0xFF;
238 printf(_(" %.0f MHz max turbo 1 active cores\n"),
246 static int get_boost_mode(unsigned int cpu)
248 struct cpufreq_frequencies *freqs;
250 if (cpupower_cpu_info.vendor == X86_VENDOR_AMD ||
251 cpupower_cpu_info.vendor == X86_VENDOR_HYGON ||
252 cpupower_cpu_info.vendor == X86_VENDOR_INTEL)
253 return get_boost_mode_x86(cpu);
255 freqs = cpufreq_get_frequencies("boost", cpu);
257 printf(_(" boost frequency steps: "));
258 while (freqs->next) {
259 print_speed(freqs->frequency);
263 print_speed(freqs->frequency);
265 cpufreq_put_frequencies(freqs);
273 static int get_freq_kernel(unsigned int cpu, unsigned int human)
275 unsigned long freq = cpufreq_get_freq_kernel(cpu);
276 printf(_(" current CPU frequency: "));
278 printf(_(" Unable to call to kernel\n"));
285 printf(_(" (asserted by call to kernel)\n"));
292 static int get_freq_hardware(unsigned int cpu, unsigned int human)
294 unsigned long freq = cpufreq_get_freq_hardware(cpu);
295 printf(_(" current CPU frequency: "));
297 printf("Unable to call hardware\n");
304 printf(_(" (asserted by call to hardware)\n"));
308 /* --hwlimits / -l */
310 static int get_hardware_limits(unsigned int cpu, unsigned int human)
312 unsigned long min, max;
314 if (cpufreq_get_hardware_limits(cpu, &min, &max)) {
315 printf(_("Not Available\n"));
320 printf(_(" hardware limits: "));
326 printf("%lu %lu\n", min, max);
333 static int get_driver(unsigned int cpu)
335 char *driver = cpufreq_get_driver(cpu);
337 printf(_(" no or unknown cpufreq driver is active on this CPU\n"));
340 printf(" driver: %s\n", driver);
341 cpufreq_put_driver(driver);
347 static int get_policy(unsigned int cpu)
349 struct cpufreq_policy *policy = cpufreq_get_policy(cpu);
351 printf(_(" Unable to determine current policy\n"));
354 printf(_(" current policy: frequency should be within "));
355 print_speed(policy->min);
357 print_speed(policy->max);
360 printf(_("The governor \"%s\" may decide which speed to use\n"
361 " within this range.\n"),
363 cpufreq_put_policy(policy);
367 /* --governors / -g */
369 static int get_available_governors(unsigned int cpu)
371 struct cpufreq_available_governors *governors =
372 cpufreq_get_available_governors(cpu);
374 printf(_(" available cpufreq governors: "));
376 printf(_("Not Available\n"));
380 while (governors->next) {
381 printf("%s ", governors->governor);
382 governors = governors->next;
384 printf("%s\n", governors->governor);
385 cpufreq_put_available_governors(governors);
390 /* --affected-cpus / -a */
392 static int get_affected_cpus(unsigned int cpu)
394 struct cpufreq_affected_cpus *cpus = cpufreq_get_affected_cpus(cpu);
396 printf(_(" CPUs which need to have their frequency coordinated by software: "));
398 printf(_("Not Available\n"));
403 printf("%d ", cpus->cpu);
406 printf("%d\n", cpus->cpu);
407 cpufreq_put_affected_cpus(cpus);
411 /* --related-cpus / -r */
413 static int get_related_cpus(unsigned int cpu)
415 struct cpufreq_affected_cpus *cpus = cpufreq_get_related_cpus(cpu);
417 printf(_(" CPUs which run at the same hardware frequency: "));
419 printf(_("Not Available\n"));
424 printf("%d ", cpus->cpu);
427 printf("%d\n", cpus->cpu);
428 cpufreq_put_related_cpus(cpus);
434 static int get_freq_stats(unsigned int cpu, unsigned int human)
436 unsigned long total_trans = cpufreq_get_transitions(cpu);
437 unsigned long long total_time;
438 struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);
441 print_speed(stats->frequency);
443 (100.0 * stats->time_in_state) / total_time);
446 stats->frequency, stats->time_in_state);
451 cpufreq_put_stats(stats);
453 printf(" (%lu)\n", total_trans);
459 static int get_latency(unsigned int cpu, unsigned int human)
461 unsigned long latency = cpufreq_get_transition_latency(cpu);
463 printf(_(" maximum transition latency: "));
464 if (!latency || latency == UINT_MAX) {
465 printf(_(" Cannot determine or is not supported.\n"));
470 print_duration(latency);
473 printf("%lu\n", latency);
477 static void debug_output_one(unsigned int cpu)
479 struct cpufreq_frequencies *freqs;
482 get_related_cpus(cpu);
483 get_affected_cpus(cpu);
485 get_hardware_limits(cpu, 1);
487 freqs = cpufreq_get_frequencies("available", cpu);
489 printf(_(" available frequency steps: "));
490 while (freqs->next) {
491 print_speed(freqs->frequency);
495 print_speed(freqs->frequency);
497 cpufreq_put_frequencies(freqs);
500 get_available_governors(cpu);
502 if (get_freq_hardware(cpu, 1) < 0)
503 get_freq_kernel(cpu, 1);
507 static struct option info_opts[] = {
508 {"debug", no_argument, NULL, 'e'},
509 {"boost", no_argument, NULL, 'b'},
510 {"freq", no_argument, NULL, 'f'},
511 {"hwfreq", no_argument, NULL, 'w'},
512 {"hwlimits", no_argument, NULL, 'l'},
513 {"driver", no_argument, NULL, 'd'},
514 {"policy", no_argument, NULL, 'p'},
515 {"governors", no_argument, NULL, 'g'},
516 {"related-cpus", no_argument, NULL, 'r'},
517 {"affected-cpus", no_argument, NULL, 'a'},
518 {"stats", no_argument, NULL, 's'},
519 {"latency", no_argument, NULL, 'y'},
520 {"proc", no_argument, NULL, 'o'},
521 {"human", no_argument, NULL, 'm'},
522 {"no-rounding", no_argument, NULL, 'n'},
526 int cmd_freq_info(int argc, char **argv)
529 extern int optind, opterr, optopt;
530 int ret = 0, cont = 1;
531 unsigned int cpu = 0;
532 unsigned int human = 0;
533 int output_param = 0;
536 ret = getopt_long(argc, argv, "oefwldpgrasmybn", info_opts,
578 fprintf(stderr, "invalid or unknown argument\n");
583 switch (output_param) {
585 if (!bitmask_isallclear(cpus_chosen)) {
586 printf(_("The argument passed to this tool can't be "
587 "combined with passing a --cpu argument\n"));
597 /* Default is: show output of CPU 0 only */
598 if (bitmask_isallclear(cpus_chosen))
599 bitmask_setbit(cpus_chosen, 0);
601 switch (output_param) {
603 printf(_("You can't specify more than one --cpu parameter and/or\n"
604 "more than one output-specific argument\n"));
607 printf(_("invalid or unknown argument\n"));
610 proc_cpufreq_output();
614 for (cpu = bitmask_first(cpus_chosen);
615 cpu <= bitmask_last(cpus_chosen); cpu++) {
617 if (!bitmask_isbitset(cpus_chosen, cpu))
620 printf(_("analyzing CPU %d:\n"), cpu);
622 if (sysfs_is_cpu_online(cpu) != 1) {
623 printf(_(" *is offline\n"));
628 switch (output_param) {
633 debug_output_one(cpu);
636 ret = get_affected_cpus(cpu);
639 ret = get_related_cpus(cpu);
642 ret = get_available_governors(cpu);
645 ret = get_policy(cpu);
648 ret = get_driver(cpu);
651 ret = get_hardware_limits(cpu, human);
654 ret = get_freq_hardware(cpu, human);
657 ret = get_freq_kernel(cpu, human);
660 ret = get_freq_stats(cpu, human);
663 ret = get_latency(cpu, human);