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 > 100000)
97 printf("%u MHz", (unsigned int) speed);
98 else if (speed > 1000)
99 printf("%u.%03u MHz", ((unsigned int) speed/1000),
100 (unsigned int) (speed%1000));
102 printf("%lu kHz", speed);
104 if (speed > 1000000) {
108 printf("%u.%02u GHz", ((unsigned int) speed/1000000),
109 ((unsigned int) (speed%1000000)/10000));
110 } else if (speed > 100000) {
114 printf("%u MHz", ((unsigned int) speed/1000));
115 } else if (speed > 1000) {
119 printf("%u.%01u MHz", ((unsigned int) speed/1000),
120 ((unsigned int) (speed%1000)/100));
127 static void print_duration(unsigned long duration)
132 if (duration > 1000000)
133 printf("%u.%06u ms", ((unsigned int) duration/1000000),
134 ((unsigned int) duration%1000000));
135 else if (duration > 100000)
136 printf("%u us", ((unsigned int) duration/1000));
137 else if (duration > 1000)
138 printf("%u.%03u us", ((unsigned int) duration/1000),
139 ((unsigned int) duration%1000));
141 printf("%lu ns", duration);
143 if (duration > 1000000) {
144 tmp = duration%10000;
147 printf("%u.%02u ms", ((unsigned int) duration/1000000),
148 ((unsigned int) (duration%1000000)/10000));
149 } else if (duration > 100000) {
153 printf("%u us", ((unsigned int) duration / 1000));
154 } else if (duration > 1000) {
158 printf("%u.%01u us", ((unsigned int) duration/1000),
159 ((unsigned int) (duration%1000)/100));
161 printf("%lu ns", duration);
168 static int get_boost_mode(unsigned int cpu)
170 int support, active, b_states = 0, ret, pstate_no, i;
171 /* ToDo: Make this more global */
172 unsigned long pstates[MAX_HW_PSTATES] = {0,};
174 if (cpupower_cpu_info.vendor != X86_VENDOR_AMD &&
175 cpupower_cpu_info.vendor != X86_VENDOR_INTEL)
178 ret = cpufreq_has_boost_support(cpu, &support, &active, &b_states);
180 printf(_("Error while evaluating Boost Capabilities"
181 " on CPU %d -- are you root?\n"), cpu);
184 /* P state changes via MSR are identified via cpuid 80000007
185 on Intel and AMD, but we assume boost capable machines can do that
186 if (cpuid_eax(0x80000000) >= 0x80000007
187 && (cpuid_edx(0x80000007) & (1 << 7)))
190 printf(_(" boost state support:\n"));
192 printf(_(" Supported: %s\n"), support ? _("yes") : _("no"));
193 printf(_(" Active: %s\n"), active ? _("yes") : _("no"));
195 if (cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
196 cpupower_cpu_info.family >= 0x10) {
197 ret = decode_pstates(cpu, cpupower_cpu_info.family, b_states,
198 pstates, &pstate_no);
202 printf(_(" Boost States: %d\n"), b_states);
203 printf(_(" Total States: %d\n"), pstate_no);
204 for (i = 0; i < pstate_no; i++) {
208 printf(_(" Pstate-Pb%d: %luMHz (boost state)"
209 "\n"), i, pstates[i]);
211 printf(_(" Pstate-P%d: %luMHz\n"),
212 i - b_states, pstates[i]);
214 } else if (cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO) {
216 unsigned long long intel_turbo_ratio = 0;
219 /* Any way to autodetect this ? */
220 if (cpupower_cpu_info.caps & CPUPOWER_CAP_IS_SNB)
224 intel_turbo_ratio = msr_intel_get_turbo_ratio(cpu);
225 dprint (" Ratio: 0x%llx - bclk: %f\n",
226 intel_turbo_ratio, bclk);
228 ratio = (intel_turbo_ratio >> 24) & 0xFF;
230 printf(_(" %.0f MHz max turbo 4 active cores\n"),
233 ratio = (intel_turbo_ratio >> 16) & 0xFF;
235 printf(_(" %.0f MHz max turbo 3 active cores\n"),
238 ratio = (intel_turbo_ratio >> 8) & 0xFF;
240 printf(_(" %.0f MHz max turbo 2 active cores\n"),
243 ratio = (intel_turbo_ratio >> 0) & 0xFF;
245 printf(_(" %.0f MHz max turbo 1 active cores\n"),
253 static int get_freq_kernel(unsigned int cpu, unsigned int human)
255 unsigned long freq = cpufreq_get_freq_kernel(cpu);
256 printf(_(" current CPU frequency: "));
258 printf(_(" Unable to call to kernel\n"));
265 printf(_(" (asserted by call to kernel)\n"));
272 static int get_freq_hardware(unsigned int cpu, unsigned int human)
274 unsigned long freq = cpufreq_get_freq_hardware(cpu);
275 printf(_(" current CPU frequency: "));
277 printf("Unable to call hardware\n");
284 printf(_(" (asserted by call to hardware)\n"));
288 /* --hwlimits / -l */
290 static int get_hardware_limits(unsigned int cpu, unsigned int human)
292 unsigned long min, max;
294 if (cpufreq_get_hardware_limits(cpu, &min, &max)) {
295 printf(_("Not Available\n"));
300 printf(_(" hardware limits: "));
306 printf("%lu %lu\n", min, max);
313 static int get_driver(unsigned int cpu)
315 char *driver = cpufreq_get_driver(cpu);
317 printf(_(" no or unknown cpufreq driver is active on this CPU\n"));
320 printf(" driver: %s\n", driver);
321 cpufreq_put_driver(driver);
327 static int get_policy(unsigned int cpu)
329 struct cpufreq_policy *policy = cpufreq_get_policy(cpu);
331 printf(_(" Unable to determine current policy\n"));
334 printf(_(" current policy: frequency should be within "));
335 print_speed(policy->min);
337 print_speed(policy->max);
340 printf(_("The governor \"%s\" may decide which speed to use\n"
341 " within this range.\n"),
343 cpufreq_put_policy(policy);
347 /* --governors / -g */
349 static int get_available_governors(unsigned int cpu)
351 struct cpufreq_available_governors *governors =
352 cpufreq_get_available_governors(cpu);
354 printf(_(" available cpufreq governors: "));
356 printf(_("Not Available\n"));
360 while (governors->next) {
361 printf("%s ", governors->governor);
362 governors = governors->next;
364 printf("%s\n", governors->governor);
365 cpufreq_put_available_governors(governors);
370 /* --affected-cpus / -a */
372 static int get_affected_cpus(unsigned int cpu)
374 struct cpufreq_affected_cpus *cpus = cpufreq_get_affected_cpus(cpu);
376 printf(_(" CPUs which need to have their frequency coordinated by software: "));
378 printf(_("Not Available\n"));
383 printf("%d ", cpus->cpu);
386 printf("%d\n", cpus->cpu);
387 cpufreq_put_affected_cpus(cpus);
391 /* --related-cpus / -r */
393 static int get_related_cpus(unsigned int cpu)
395 struct cpufreq_affected_cpus *cpus = cpufreq_get_related_cpus(cpu);
397 printf(_(" CPUs which run at the same hardware frequency: "));
399 printf(_("Not Available\n"));
404 printf("%d ", cpus->cpu);
407 printf("%d\n", cpus->cpu);
408 cpufreq_put_related_cpus(cpus);
414 static int get_freq_stats(unsigned int cpu, unsigned int human)
416 unsigned long total_trans = cpufreq_get_transitions(cpu);
417 unsigned long long total_time;
418 struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);
421 print_speed(stats->frequency);
423 (100.0 * stats->time_in_state) / total_time);
426 stats->frequency, stats->time_in_state);
431 cpufreq_put_stats(stats);
433 printf(" (%lu)\n", total_trans);
439 static int get_latency(unsigned int cpu, unsigned int human)
441 unsigned long latency = cpufreq_get_transition_latency(cpu);
443 printf(_(" maximum transition latency: "));
444 if (!latency || latency == UINT_MAX) {
445 printf(_(" Cannot determine or is not supported.\n"));
450 print_duration(latency);
453 printf("%lu\n", latency);
457 static void debug_output_one(unsigned int cpu)
459 struct cpufreq_available_frequencies *freqs;
462 get_related_cpus(cpu);
463 get_affected_cpus(cpu);
465 get_hardware_limits(cpu, 1);
467 freqs = cpufreq_get_available_frequencies(cpu);
469 printf(_(" available frequency steps: "));
470 while (freqs->next) {
471 print_speed(freqs->frequency);
475 print_speed(freqs->frequency);
477 cpufreq_put_available_frequencies(freqs);
480 get_available_governors(cpu);
482 if (get_freq_hardware(cpu, 1) < 0)
483 get_freq_kernel(cpu, 1);
487 static struct option info_opts[] = {
488 {"debug", no_argument, NULL, 'e'},
489 {"boost", no_argument, NULL, 'b'},
490 {"freq", no_argument, NULL, 'f'},
491 {"hwfreq", no_argument, NULL, 'w'},
492 {"hwlimits", no_argument, NULL, 'l'},
493 {"driver", no_argument, NULL, 'd'},
494 {"policy", no_argument, NULL, 'p'},
495 {"governors", no_argument, NULL, 'g'},
496 {"related-cpus", no_argument, NULL, 'r'},
497 {"affected-cpus", no_argument, NULL, 'a'},
498 {"stats", no_argument, NULL, 's'},
499 {"latency", no_argument, NULL, 'y'},
500 {"proc", no_argument, NULL, 'o'},
501 {"human", no_argument, NULL, 'm'},
502 {"no-rounding", no_argument, NULL, 'n'},
506 int cmd_freq_info(int argc, char **argv)
509 extern int optind, opterr, optopt;
510 int ret = 0, cont = 1;
511 unsigned int cpu = 0;
512 unsigned int human = 0;
513 int output_param = 0;
516 ret = getopt_long(argc, argv, "oefwldpgrasmybn", info_opts,
558 fprintf(stderr, "invalid or unknown argument\n");
563 switch (output_param) {
565 if (!bitmask_isallclear(cpus_chosen)) {
566 printf(_("The argument passed to this tool can't be "
567 "combined with passing a --cpu argument\n"));
577 /* Default is: show output of CPU 0 only */
578 if (bitmask_isallclear(cpus_chosen))
579 bitmask_setbit(cpus_chosen, 0);
581 switch (output_param) {
583 printf(_("You can't specify more than one --cpu parameter and/or\n"
584 "more than one output-specific argument\n"));
587 printf(_("invalid or unknown argument\n"));
590 proc_cpufreq_output();
594 for (cpu = bitmask_first(cpus_chosen);
595 cpu <= bitmask_last(cpus_chosen); cpu++) {
597 if (!bitmask_isbitset(cpus_chosen, cpu))
600 printf(_("analyzing CPU %d:\n"), cpu);
602 if (sysfs_is_cpu_online(cpu) != 1) {
603 printf(_(" *is offline\n"));
608 switch (output_param) {
613 debug_output_one(cpu);
616 ret = get_affected_cpus(cpu);
619 ret = get_related_cpus(cpu);
622 ret = get_available_governors(cpu);
625 ret = get_policy(cpu);
628 ret = get_driver(cpu);
631 ret = get_hardware_limits(cpu, human);
634 ret = get_freq_hardware(cpu, human);
637 ret = get_freq_kernel(cpu, human);
640 ret = get_freq_stats(cpu, human);
643 ret = get_latency(cpu, human);