Path: blob/master/tools/power/cpupower/utils/cpufreq-info.c
26292 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* (C) 2004-2009 Dominik Brodowski <[email protected]>3*/456#include <unistd.h>7#include <stdio.h>8#include <errno.h>9#include <stdlib.h>10#include <string.h>11#include <limits.h>1213#include <getopt.h>1415#include "cpufreq.h"16#include "helpers/sysfs.h"17#include "helpers/helpers.h"18#include "helpers/bitmask.h"1920#define LINE_LEN 102122static unsigned int count_cpus(void)23{24FILE *fp;25char value[LINE_LEN];26unsigned int ret = 0;27unsigned int cpunr = 0;2829fp = fopen("/proc/stat", "r");30if (!fp) {31printf(_("Couldn't count the number of CPUs (%s: %s), assuming 1\n"), "/proc/stat", strerror(errno));32return 1;33}3435while (!feof(fp)) {36if (!fgets(value, LINE_LEN, fp))37continue;38value[LINE_LEN - 1] = '\0';39if (strlen(value) < (LINE_LEN - 2))40continue;41if (strstr(value, "cpu "))42continue;43if (sscanf(value, "cpu%d ", &cpunr) != 1)44continue;45if (cpunr > ret)46ret = cpunr;47}48fclose(fp);4950/* cpu count starts from 0, on error return 1 (UP) */51return ret + 1;52}535455static void proc_cpufreq_output(void)56{57unsigned int cpu, nr_cpus;58struct cpufreq_policy *policy;59unsigned int min_pctg = 0;60unsigned int max_pctg = 0;61unsigned long min, max;6263printf(_(" minimum CPU frequency - maximum CPU frequency - governor\n"));6465nr_cpus = count_cpus();66for (cpu = 0; cpu < nr_cpus; cpu++) {67policy = cpufreq_get_policy(cpu);68if (!policy)69continue;7071if (cpufreq_get_hardware_limits(cpu, &min, &max)) {72max = 0;73} else {74min_pctg = (policy->min * 100) / max;75max_pctg = (policy->max * 100) / max;76}77printf("CPU%3d %9lu kHz (%3d %%) - %9lu kHz (%3d %%) - %s\n",78cpu , policy->min, max ? min_pctg : 0, policy->max,79max ? max_pctg : 0, policy->governor);8081cpufreq_put_policy(policy);82}83}8485static int no_rounding;86static void print_duration(unsigned long duration)87{88unsigned long tmp;8990if (no_rounding) {91if (duration > 1000000)92printf("%u.%06u ms", ((unsigned int) duration/1000000),93((unsigned int) duration%1000000));94else if (duration > 100000)95printf("%u us", ((unsigned int) duration/1000));96else if (duration > 1000)97printf("%u.%03u us", ((unsigned int) duration/1000),98((unsigned int) duration%1000));99else100printf("%lu ns", duration);101} else {102if (duration > 1000000) {103tmp = duration%10000;104if (tmp >= 5000)105duration += 10000;106printf("%u.%02u ms", ((unsigned int) duration/1000000),107((unsigned int) (duration%1000000)/10000));108} else if (duration > 100000) {109tmp = duration%1000;110if (tmp >= 500)111duration += 1000;112printf("%u us", ((unsigned int) duration / 1000));113} else if (duration > 1000) {114tmp = duration%100;115if (tmp >= 50)116duration += 100;117printf("%u.%01u us", ((unsigned int) duration/1000),118((unsigned int) (duration%1000)/100));119} else120printf("%lu ns", duration);121}122}123124static int get_boost_mode_x86(unsigned int cpu)125{126int support, active, b_states = 0, ret, pstate_no, i;127/* ToDo: Make this more global */128unsigned long pstates[MAX_HW_PSTATES] = {0,};129130ret = cpufreq_has_x86_boost_support(cpu, &support, &active, &b_states);131if (ret) {132printf(_("Error while evaluating Boost Capabilities"133" on CPU %d -- are you root?\n"), cpu);134return ret;135}136/* P state changes via MSR are identified via cpuid 80000007137on Intel and AMD, but we assume boost capable machines can do that138if (cpuid_eax(0x80000000) >= 0x80000007139&& (cpuid_edx(0x80000007) & (1 << 7)))140*/141142printf(_(" boost state support:\n"));143144printf(_(" Supported: %s\n"), support ? _("yes") : _("no"));145printf(_(" Active: %s\n"), active ? _("yes") : _("no"));146147if (cpupower_cpu_info.vendor == X86_VENDOR_AMD &&148cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATE) {149return 0;150} else if ((cpupower_cpu_info.vendor == X86_VENDOR_AMD &&151cpupower_cpu_info.family >= 0x10) ||152cpupower_cpu_info.vendor == X86_VENDOR_HYGON) {153ret = decode_pstates(cpu, b_states, pstates, &pstate_no);154if (ret)155return ret;156157printf(_(" Boost States: %d\n"), b_states);158printf(_(" Total States: %d\n"), pstate_no);159for (i = 0; i < pstate_no; i++) {160if (!pstates[i])161continue;162if (i < b_states)163printf(_(" Pstate-Pb%d: %luMHz (boost state)"164"\n"), i, pstates[i]);165else166printf(_(" Pstate-P%d: %luMHz\n"),167i - b_states, pstates[i]);168}169} else if (cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO) {170double bclk;171unsigned long long intel_turbo_ratio = 0;172unsigned int ratio;173174/* Any way to autodetect this ? */175if (cpupower_cpu_info.caps & CPUPOWER_CAP_IS_SNB)176bclk = 100.00;177else178bclk = 133.33;179intel_turbo_ratio = msr_intel_get_turbo_ratio(cpu);180dprint (" Ratio: 0x%llx - bclk: %f\n",181intel_turbo_ratio, bclk);182183ratio = (intel_turbo_ratio >> 24) & 0xFF;184if (ratio)185printf(_(" %.0f MHz max turbo 4 active cores\n"),186ratio * bclk);187188ratio = (intel_turbo_ratio >> 16) & 0xFF;189if (ratio)190printf(_(" %.0f MHz max turbo 3 active cores\n"),191ratio * bclk);192193ratio = (intel_turbo_ratio >> 8) & 0xFF;194if (ratio)195printf(_(" %.0f MHz max turbo 2 active cores\n"),196ratio * bclk);197198ratio = (intel_turbo_ratio >> 0) & 0xFF;199if (ratio)200printf(_(" %.0f MHz max turbo 1 active cores\n"),201ratio * bclk);202}203return 0;204}205206static int get_boost_mode_generic(unsigned int cpu)207{208bool active;209210if (!cpufreq_has_generic_boost_support(&active)) {211printf(_(" boost state support:\n"));212printf(_(" Active: %s\n"), active ? _("yes") : _("no"));213}214215return 0;216}217218/* --boost / -b */219220static int get_boost_mode(unsigned int cpu)221{222struct cpufreq_available_frequencies *freqs;223224if (cpupower_cpu_info.vendor == X86_VENDOR_AMD ||225cpupower_cpu_info.vendor == X86_VENDOR_HYGON ||226cpupower_cpu_info.vendor == X86_VENDOR_INTEL)227return get_boost_mode_x86(cpu);228else229get_boost_mode_generic(cpu);230231freqs = cpufreq_get_boost_frequencies(cpu);232if (freqs) {233printf(_(" boost frequency steps: "));234while (freqs->next) {235print_speed(freqs->frequency, no_rounding);236printf(", ");237freqs = freqs->next;238}239print_speed(freqs->frequency, no_rounding);240printf("\n");241cpufreq_put_available_frequencies(freqs);242}243244return 0;245}246247/* --freq / -f */248249static int get_freq_kernel(unsigned int cpu, unsigned int human)250{251unsigned long freq = cpufreq_get_freq_kernel(cpu);252printf(_(" current CPU frequency: "));253if (!freq) {254printf(_(" Unable to call to kernel\n"));255return -EINVAL;256}257if (human) {258print_speed(freq, no_rounding);259} else260printf("%lu", freq);261printf(_(" (asserted by call to kernel)\n"));262return 0;263}264265266/* --hwfreq / -w */267268static int get_freq_hardware(unsigned int cpu, unsigned int human)269{270unsigned long freq;271272if (cpupower_cpu_info.caps & CPUPOWER_CAP_APERF)273return -EINVAL;274275freq = cpufreq_get_freq_hardware(cpu);276printf(_(" current CPU frequency: "));277if (!freq) {278printf("Unable to call hardware\n");279return -EINVAL;280}281if (human) {282print_speed(freq, no_rounding);283} else284printf("%lu", freq);285printf(_(" (asserted by call to hardware)\n"));286return 0;287}288289/* --hwlimits / -l */290291static int get_hardware_limits(unsigned int cpu, unsigned int human)292{293unsigned long min, max;294295if (cpufreq_get_hardware_limits(cpu, &min, &max)) {296printf(_("Not Available\n"));297return -EINVAL;298}299300if (human) {301printf(_(" hardware limits: "));302print_speed(min, no_rounding);303printf(" - ");304print_speed(max, no_rounding);305printf("\n");306} else {307printf("%lu %lu\n", min, max);308}309return 0;310}311312/* --driver / -d */313314static int get_driver(unsigned int cpu)315{316char *driver = cpufreq_get_driver(cpu);317if (!driver) {318printf(_(" no or unknown cpufreq driver is active on this CPU\n"));319return -EINVAL;320}321printf(" driver: %s\n", driver);322cpufreq_put_driver(driver);323return 0;324}325326/* --policy / -p */327328static int get_policy(unsigned int cpu)329{330struct cpufreq_policy *policy = cpufreq_get_policy(cpu);331if (!policy) {332printf(_(" Unable to determine current policy\n"));333return -EINVAL;334}335printf(_(" current policy: frequency should be within "));336print_speed(policy->min, no_rounding);337printf(_(" and "));338print_speed(policy->max, no_rounding);339340printf(".\n ");341printf(_("The governor \"%s\" may decide which speed to use\n"342" within this range.\n"),343policy->governor);344cpufreq_put_policy(policy);345return 0;346}347348/* --governors / -g */349350static int get_available_governors(unsigned int cpu)351{352struct cpufreq_available_governors *governors =353cpufreq_get_available_governors(cpu);354355printf(_(" available cpufreq governors: "));356if (!governors) {357printf(_("Not Available\n"));358return -EINVAL;359}360361while (governors->next) {362printf("%s ", governors->governor);363governors = governors->next;364}365printf("%s\n", governors->governor);366cpufreq_put_available_governors(governors);367return 0;368}369370371/* --affected-cpus / -a */372373static int get_affected_cpus(unsigned int cpu)374{375struct cpufreq_affected_cpus *cpus = cpufreq_get_affected_cpus(cpu);376377printf(_(" CPUs which need to have their frequency coordinated by software: "));378if (!cpus) {379printf(_("Not Available\n"));380return -EINVAL;381}382383while (cpus->next) {384printf("%d ", cpus->cpu);385cpus = cpus->next;386}387printf("%d\n", cpus->cpu);388cpufreq_put_affected_cpus(cpus);389return 0;390}391392/* --related-cpus / -r */393394static int get_related_cpus(unsigned int cpu)395{396struct cpufreq_affected_cpus *cpus = cpufreq_get_related_cpus(cpu);397398printf(_(" CPUs which run at the same hardware frequency: "));399if (!cpus) {400printf(_("Not Available\n"));401return -EINVAL;402}403404while (cpus->next) {405printf("%d ", cpus->cpu);406cpus = cpus->next;407}408printf("%d\n", cpus->cpu);409cpufreq_put_related_cpus(cpus);410return 0;411}412413/* --stats / -s */414415static int get_freq_stats(unsigned int cpu, unsigned int human)416{417unsigned long total_trans = cpufreq_get_transitions(cpu);418unsigned long long total_time;419struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);420while (stats) {421if (human) {422print_speed(stats->frequency, no_rounding);423printf(":%.2f%%",424(100.0 * stats->time_in_state) / total_time);425} else426printf("%lu:%llu",427stats->frequency, stats->time_in_state);428stats = stats->next;429if (stats)430printf(", ");431}432cpufreq_put_stats(stats);433if (total_trans)434printf(" (%lu)\n", total_trans);435return 0;436}437438/* --epp / -z */439440static int get_epp(unsigned int cpu, bool interactive)441{442char *epp;443444epp = cpufreq_get_energy_performance_preference(cpu);445if (!epp)446return -EINVAL;447if (interactive)448printf(_(" energy performance preference: %s\n"), epp);449450cpufreq_put_energy_performance_preference(epp);451452return 0;453}454455/* --latency / -y */456457static int get_latency(unsigned int cpu, unsigned int human)458{459unsigned long latency = cpufreq_get_transition_latency(cpu);460461if (!get_epp(cpu, false))462return -EINVAL;463464printf(_(" maximum transition latency: "));465if (!latency || latency == UINT_MAX) {466printf(_(" Cannot determine or is not supported.\n"));467return -EINVAL;468}469470if (human) {471print_duration(latency);472printf("\n");473} else474printf("%lu\n", latency);475return 0;476}477478/* --performance / -c */479480static int get_perf_cap(unsigned int cpu)481{482if (cpupower_cpu_info.vendor == X86_VENDOR_AMD &&483cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATE)484amd_pstate_show_perf_and_freq(cpu, no_rounding);485486return 0;487}488489static void debug_output_one(unsigned int cpu)490{491struct cpufreq_available_frequencies *freqs;492493get_driver(cpu);494get_related_cpus(cpu);495get_affected_cpus(cpu);496get_latency(cpu, 1);497get_epp(cpu, true);498get_hardware_limits(cpu, 1);499500freqs = cpufreq_get_available_frequencies(cpu);501if (freqs) {502printf(_(" available frequency steps: "));503while (freqs->next) {504print_speed(freqs->frequency, no_rounding);505printf(", ");506freqs = freqs->next;507}508print_speed(freqs->frequency, no_rounding);509printf("\n");510cpufreq_put_available_frequencies(freqs);511}512513get_available_governors(cpu);514get_policy(cpu);515if (get_freq_hardware(cpu, 1) < 0)516get_freq_kernel(cpu, 1);517get_boost_mode(cpu);518get_perf_cap(cpu);519}520521static struct option info_opts[] = {522{"debug", no_argument, NULL, 'e'},523{"boost", no_argument, NULL, 'b'},524{"freq", no_argument, NULL, 'f'},525{"hwfreq", no_argument, NULL, 'w'},526{"hwlimits", no_argument, NULL, 'l'},527{"driver", no_argument, NULL, 'd'},528{"policy", no_argument, NULL, 'p'},529{"governors", no_argument, NULL, 'g'},530{"related-cpus", no_argument, NULL, 'r'},531{"affected-cpus", no_argument, NULL, 'a'},532{"stats", no_argument, NULL, 's'},533{"latency", no_argument, NULL, 'y'},534{"proc", no_argument, NULL, 'o'},535{"human", no_argument, NULL, 'm'},536{"no-rounding", no_argument, NULL, 'n'},537{"performance", no_argument, NULL, 'c'},538{"epp", no_argument, NULL, 'z'},539{ },540};541542int cmd_freq_info(int argc, char **argv)543{544extern char *optarg;545extern int optind, opterr, optopt;546int ret = 0, cont = 1;547unsigned int cpu = 0;548unsigned int human = 0;549int output_param = 0;550551do {552ret = getopt_long(argc, argv, "oefwldpgrasmybncz", info_opts,553NULL);554switch (ret) {555case '?':556output_param = '?';557cont = 0;558break;559case -1:560cont = 0;561break;562case 'b':563case 'o':564case 'a':565case 'r':566case 'g':567case 'p':568case 'd':569case 'l':570case 'w':571case 'f':572case 'e':573case 's':574case 'y':575case 'c':576case 'z':577if (output_param) {578output_param = -1;579cont = 0;580break;581}582output_param = ret;583break;584case 'm':585if (human) {586output_param = -1;587cont = 0;588break;589}590human = 1;591break;592case 'n':593no_rounding = 1;594break;595default:596fprintf(stderr, "invalid or unknown argument\n");597return EXIT_FAILURE;598}599} while (cont);600601switch (output_param) {602case 'o':603if (!bitmask_isallclear(cpus_chosen)) {604printf(_("The argument passed to this tool can't be "605"combined with passing a --cpu argument\n"));606return -EINVAL;607}608break;609case 0:610output_param = 'e';611}612613ret = 0;614615/* Default is: show output of base_cpu only */616if (bitmask_isallclear(cpus_chosen))617bitmask_setbit(cpus_chosen, base_cpu);618619switch (output_param) {620case -1:621printf(_("You can't specify more than one --cpu parameter and/or\n"622"more than one output-specific argument\n"));623return -EINVAL;624case '?':625printf(_("invalid or unknown argument\n"));626return -EINVAL;627case 'o':628proc_cpufreq_output();629return EXIT_SUCCESS;630}631632for (cpu = bitmask_first(cpus_chosen);633cpu <= bitmask_last(cpus_chosen); cpu++) {634635if (!bitmask_isbitset(cpus_chosen, cpu))636continue;637638printf(_("analyzing CPU %d:\n"), cpu);639640if (sysfs_is_cpu_online(cpu) != 1) {641printf(_(" *is offline\n"));642printf("\n");643continue;644}645646switch (output_param) {647case 'b':648get_boost_mode(cpu);649break;650case 'e':651debug_output_one(cpu);652break;653case 'a':654ret = get_affected_cpus(cpu);655break;656case 'r':657ret = get_related_cpus(cpu);658break;659case 'g':660ret = get_available_governors(cpu);661break;662case 'p':663ret = get_policy(cpu);664break;665case 'd':666ret = get_driver(cpu);667break;668case 'l':669ret = get_hardware_limits(cpu, human);670break;671case 'w':672ret = get_freq_hardware(cpu, human);673break;674case 'f':675ret = get_freq_kernel(cpu, human);676break;677case 's':678ret = get_freq_stats(cpu, human);679break;680case 'y':681ret = get_latency(cpu, human);682break;683case 'c':684ret = get_perf_cap(cpu);685break;686case 'z':687ret = get_epp(cpu, true);688break;689}690if (ret)691return ret;692}693return ret;694}695696697