From ac5a181d065d74fb6b213d538f743392f27bcdbd Mon Sep 17 00:00:00 2001 From: Thomas Renninger Date: Thu, 28 Apr 2016 15:24:40 +0200 Subject: cpupower: Add cpuidle parts into library This more or less is a renaming and moving of functions and should not introduce any functional change. cpupower was built from cpufrequtils (which had a C library providing easy access to cpu frequency platform info). In the meantime it got enhanced by quite some neat cpuidle userspace tools. Now the cpu idle functions have been separated and added to the cpupower.so library. So beside an already existing public header file: cpufreq.h cpupower now also exports these cpu idle functions in: cpuidle.h Here again pasted for better review of the interfaces: ====================================== int cpuidle_is_state_disabled(unsigned int cpu, unsigned int idlestate); int cpuidle_state_disable(unsigned int cpu, unsigned int idlestate, unsigned int disable); unsigned long cpuidle_state_latency(unsigned int cpu, unsigned int idlestate); unsigned long cpuidle_state_usage(unsigned int cpu, unsigned int idlestate); unsigned long long cpuidle_state_time(unsigned int cpu, unsigned int idlestate); char *cpuidle_state_name(unsigned int cpu, unsigned int idlestate); char *cpuidle_state_desc(unsigned int cpu, unsigned int idlestate); unsigned int cpuidle_state_count(unsigned int cpu); char *cpuidle_get_governor(void); char *cpuidle_get_driver(void); ====================================== Signed-off-by: Thomas Renninger Signed-off-by: Rafael J. Wysocki --- tools/power/cpupower/utils/cpufreq-set.c | 8 +- tools/power/cpupower/utils/cpuidle-info.c | 32 +++--- tools/power/cpupower/utils/cpuidle-set.c | 26 ++--- tools/power/cpupower/utils/helpers/helpers.h | 26 +---- tools/power/cpupower/utils/helpers/topology.c | 107 +-------------------- .../cpupower/utils/idle_monitor/cpuidle_sysfs.c | 12 +-- 6 files changed, 43 insertions(+), 168 deletions(-) (limited to 'tools/power/cpupower/utils') diff --git a/tools/power/cpupower/utils/cpufreq-set.c b/tools/power/cpupower/utils/cpufreq-set.c index 0fbd1a22c0a9..b4bf76971dc9 100644 --- a/tools/power/cpupower/utils/cpufreq-set.c +++ b/tools/power/cpupower/utils/cpufreq-set.c @@ -16,8 +16,8 @@ #include #include "cpufreq.h" +#include "cpuidle.h" #include "helpers/helpers.h" -#include "helpers/sysfs.h" #define NORM_FREQ_LEN 32 @@ -296,7 +296,7 @@ int cmd_freq_set(int argc, char **argv) struct cpufreq_affected_cpus *cpus; if (!bitmask_isbitset(cpus_chosen, cpu) || - cpufreq_cpu_exists(cpu)) + cpupower_is_cpu_online(cpu)) continue; cpus = cpufreq_get_related_cpus(cpu); @@ -316,10 +316,10 @@ int cmd_freq_set(int argc, char **argv) cpu <= bitmask_last(cpus_chosen); cpu++) { if (!bitmask_isbitset(cpus_chosen, cpu) || - cpufreq_cpu_exists(cpu)) + cpupower_is_cpu_online(cpu)) continue; - if (sysfs_is_cpu_online(cpu) != 1) + if (cpupower_is_cpu_online(cpu) != 1) continue; printf(_("Setting cpu: %d\n"), cpu); diff --git a/tools/power/cpupower/utils/cpuidle-info.c b/tools/power/cpupower/utils/cpuidle-info.c index 8bf8ab5ffa25..b59c85defa05 100644 --- a/tools/power/cpupower/utils/cpuidle-info.c +++ b/tools/power/cpupower/utils/cpuidle-info.c @@ -13,8 +13,10 @@ #include #include -#include "helpers/helpers.h" +#include + #include "helpers/sysfs.h" +#include "helpers/helpers.h" #include "helpers/bitmask.h" #define LINE_LEN 10 @@ -24,7 +26,7 @@ static void cpuidle_cpu_output(unsigned int cpu, int verbose) unsigned int idlestates, idlestate; char *tmp; - idlestates = sysfs_get_idlestate_count(cpu); + idlestates = cpuidle_state_count(cpu); if (idlestates == 0) { printf(_("CPU %u: No idle states\n"), cpu); return; @@ -33,7 +35,7 @@ static void cpuidle_cpu_output(unsigned int cpu, int verbose) printf(_("Number of idle states: %d\n"), idlestates); printf(_("Available idle states:")); for (idlestate = 0; idlestate < idlestates; idlestate++) { - tmp = sysfs_get_idlestate_name(cpu, idlestate); + tmp = cpuidle_state_name(cpu, idlestate); if (!tmp) continue; printf(" %s", tmp); @@ -45,28 +47,28 @@ static void cpuidle_cpu_output(unsigned int cpu, int verbose) return; for (idlestate = 0; idlestate < idlestates; idlestate++) { - int disabled = sysfs_is_idlestate_disabled(cpu, idlestate); + int disabled = cpuidle_is_state_disabled(cpu, idlestate); /* Disabled interface not supported on older kernels */ if (disabled < 0) disabled = 0; - tmp = sysfs_get_idlestate_name(cpu, idlestate); + tmp = cpuidle_state_name(cpu, idlestate); if (!tmp) continue; printf("%s%s:\n", tmp, (disabled) ? " (DISABLED) " : ""); free(tmp); - tmp = sysfs_get_idlestate_desc(cpu, idlestate); + tmp = cpuidle_state_desc(cpu, idlestate); if (!tmp) continue; printf(_("Flags/Description: %s\n"), tmp); free(tmp); printf(_("Latency: %lu\n"), - sysfs_get_idlestate_latency(cpu, idlestate)); + cpuidle_state_latency(cpu, idlestate)); printf(_("Usage: %lu\n"), - sysfs_get_idlestate_usage(cpu, idlestate)); + cpuidle_state_usage(cpu, idlestate)); printf(_("Duration: %llu\n"), - sysfs_get_idlestate_time(cpu, idlestate)); + cpuidle_state_time(cpu, idlestate)); } } @@ -74,7 +76,7 @@ static void cpuidle_general_output(void) { char *tmp; - tmp = sysfs_get_cpuidle_driver(); + tmp = cpuidle_get_driver(); if (!tmp) { printf(_("Could not determine cpuidle driver\n")); return; @@ -83,7 +85,7 @@ static void cpuidle_general_output(void) printf(_("CPUidle driver: %s\n"), tmp); free(tmp); - tmp = sysfs_get_cpuidle_governor(); + tmp = cpuidle_get_governor(); if (!tmp) { printf(_("Could not determine cpuidle governor\n")); return; @@ -98,7 +100,7 @@ static void proc_cpuidle_cpu_output(unsigned int cpu) long max_allowed_cstate = 2000000000; unsigned int cstate, cstates; - cstates = sysfs_get_idlestate_count(cpu); + cstates = cpuidle_state_count(cpu); if (cstates == 0) { printf(_("CPU %u: No C-states info\n"), cpu); return; @@ -113,11 +115,11 @@ static void proc_cpuidle_cpu_output(unsigned int cpu) "type[C%d] "), cstate, cstate); printf(_("promotion[--] demotion[--] ")); printf(_("latency[%03lu] "), - sysfs_get_idlestate_latency(cpu, cstate)); + cpuidle_state_latency(cpu, cstate)); printf(_("usage[%08lu] "), - sysfs_get_idlestate_usage(cpu, cstate)); + cpuidle_state_usage(cpu, cstate)); printf(_("duration[%020Lu] \n"), - sysfs_get_idlestate_time(cpu, cstate)); + cpuidle_state_time(cpu, cstate)); } } diff --git a/tools/power/cpupower/utils/cpuidle-set.c b/tools/power/cpupower/utils/cpuidle-set.c index d6b6ae44b8c2..691c24d50ef4 100644 --- a/tools/power/cpupower/utils/cpuidle-set.c +++ b/tools/power/cpupower/utils/cpuidle-set.c @@ -5,12 +5,12 @@ #include #include #include - #include -#include "cpufreq.h" +#include +#include + #include "helpers/helpers.h" -#include "helpers/sysfs.h" static struct option info_opts[] = { {"disable", required_argument, NULL, 'd'}, @@ -104,16 +104,16 @@ int cmd_idle_set(int argc, char **argv) if (!bitmask_isbitset(cpus_chosen, cpu)) continue; - if (sysfs_is_cpu_online(cpu) != 1) + if (cpupower_is_cpu_online(cpu) != 1) continue; - idlestates = sysfs_get_idlestate_count(cpu); + idlestates = cpuidle_state_count(cpu); if (idlestates <= 0) continue; switch (param) { case 'd': - ret = sysfs_idlestate_disable(cpu, idlestate, 1); + ret = cpuidle_state_disable(cpu, idlestate, 1); if (ret == 0) printf(_("Idlestate %u disabled on CPU %u\n"), idlestate, cpu); else if (ret == -1) @@ -126,7 +126,7 @@ int cmd_idle_set(int argc, char **argv) idlestate, cpu); break; case 'e': - ret = sysfs_idlestate_disable(cpu, idlestate, 0); + ret = cpuidle_state_disable(cpu, idlestate, 0); if (ret == 0) printf(_("Idlestate %u enabled on CPU %u\n"), idlestate, cpu); else if (ret == -1) @@ -140,13 +140,13 @@ int cmd_idle_set(int argc, char **argv) break; case 'D': for (idlestate = 0; idlestate < idlestates; idlestate++) { - disabled = sysfs_is_idlestate_disabled + disabled = cpuidle_is_state_disabled (cpu, idlestate); - state_latency = sysfs_get_idlestate_latency + state_latency = cpuidle_state_latency (cpu, idlestate); if (disabled == 1) { if (latency > state_latency){ - ret = sysfs_idlestate_disable + ret = cpuidle_state_disable (cpu, idlestate, 0); if (ret == 0) printf(_("Idlestate %u enabled on CPU %u\n"), idlestate, cpu); @@ -154,7 +154,7 @@ int cmd_idle_set(int argc, char **argv) continue; } if (latency <= state_latency){ - ret = sysfs_idlestate_disable + ret = cpuidle_state_disable (cpu, idlestate, 1); if (ret == 0) printf(_("Idlestate %u disabled on CPU %u\n"), idlestate, cpu); @@ -163,10 +163,10 @@ int cmd_idle_set(int argc, char **argv) break; case 'E': for (idlestate = 0; idlestate < idlestates; idlestate++) { - disabled = sysfs_is_idlestate_disabled + disabled = cpuidle_is_state_disabled (cpu, idlestate); if (disabled == 1) { - ret = sysfs_idlestate_disable + ret = cpuidle_state_disable (cpu, idlestate, 0); if (ret == 0) printf(_("Idlestate %u enabled on CPU %u\n"), idlestate, cpu); diff --git a/tools/power/cpupower/utils/helpers/helpers.h b/tools/power/cpupower/utils/helpers/helpers.h index aa9e95486a2d..afb66f80554e 100644 --- a/tools/power/cpupower/utils/helpers/helpers.h +++ b/tools/power/cpupower/utils/helpers/helpers.h @@ -14,6 +14,7 @@ #include #include "helpers/bitmask.h" +#include /* Internationalization ****************************/ #ifdef NLS @@ -92,31 +93,6 @@ extern int get_cpu_info(unsigned int cpu, struct cpupower_cpu_info *cpu_info); extern struct cpupower_cpu_info cpupower_cpu_info; /* cpuid and cpuinfo helpers **************************/ -struct cpuid_core_info { - int pkg; - int core; - int cpu; - - /* flags */ - unsigned int is_online:1; -}; - -/* CPU topology/hierarchy parsing ******************/ -struct cpupower_topology { - /* Amount of CPU cores, packages and threads per core in the system */ - unsigned int cores; - unsigned int pkgs; - unsigned int threads; /* per core */ - - /* Array gets mallocated with cores entries, holding per core info */ - struct cpuid_core_info *core_info; -}; - -extern int get_cpu_topology(struct cpupower_topology *cpu_top); -extern void cpu_topology_release(struct cpupower_topology cpu_top); - -/* CPU topology/hierarchy parsing ******************/ - /* X86 ONLY ****************************************/ #if defined(__i386__) || defined(__x86_64__) diff --git a/tools/power/cpupower/utils/helpers/topology.c b/tools/power/cpupower/utils/helpers/topology.c index 5f9c908f4557..a1a6c6041a1e 100644 --- a/tools/power/cpupower/utils/helpers/topology.c +++ b/tools/power/cpupower/utils/helpers/topology.c @@ -16,110 +16,7 @@ #include #include -#include -#include +#include -/* returns -1 on failure, 0 on success */ -static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result) -{ - char linebuf[MAX_LINE_LEN]; - char *endp; - char path[SYSFS_PATH_MAX]; +/* CPU topology/hierarchy parsing ******************/ - snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s", - cpu, fname); - if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0) - return -1; - *result = strtol(linebuf, &endp, 0); - if (endp == linebuf || errno == ERANGE) - return -1; - return 0; -} - -static int __compare(const void *t1, const void *t2) -{ - struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1; - struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2; - if (top1->pkg < top2->pkg) - return -1; - else if (top1->pkg > top2->pkg) - return 1; - else if (top1->core < top2->core) - return -1; - else if (top1->core > top2->core) - return 1; - else if (top1->cpu < top2->cpu) - return -1; - else if (top1->cpu > top2->cpu) - return 1; - else - return 0; -} - -/* - * Returns amount of cpus, negative on error, cpu_top must be - * passed to cpu_topology_release to free resources - * - * Array is sorted after ->pkg, ->core, then ->cpu - */ -int get_cpu_topology(struct cpupower_topology *cpu_top) -{ - int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF); - - cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus); - if (cpu_top->core_info == NULL) - return -ENOMEM; - cpu_top->pkgs = cpu_top->cores = 0; - for (cpu = 0; cpu < cpus; cpu++) { - cpu_top->core_info[cpu].cpu = cpu; - cpu_top->core_info[cpu].is_online = sysfs_is_cpu_online(cpu); - if(sysfs_topology_read_file( - cpu, - "physical_package_id", - &(cpu_top->core_info[cpu].pkg)) < 0) { - cpu_top->core_info[cpu].pkg = -1; - cpu_top->core_info[cpu].core = -1; - continue; - } - if(sysfs_topology_read_file( - cpu, - "core_id", - &(cpu_top->core_info[cpu].core)) < 0) { - cpu_top->core_info[cpu].pkg = -1; - cpu_top->core_info[cpu].core = -1; - continue; - } - } - - qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info), - __compare); - - /* Count the number of distinct pkgs values. This works - because the primary sort of the core_info struct was just - done by pkg value. */ - last_pkg = cpu_top->core_info[0].pkg; - for(cpu = 1; cpu < cpus; cpu++) { - if (cpu_top->core_info[cpu].pkg != last_pkg && - cpu_top->core_info[cpu].pkg != -1) { - - last_pkg = cpu_top->core_info[cpu].pkg; - cpu_top->pkgs++; - } - } - if (!(cpu_top->core_info[0].pkg == -1)) - cpu_top->pkgs++; - - /* Intel's cores count is not consecutively numbered, there may - * be a core_id of 3, but none of 2. Assume there always is 0 - * Get amount of cores by counting duplicates in a package - for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) { - if (cpu_top->core_info[cpu].core == 0) - cpu_top->cores++; - */ - return cpus; -} - -void cpu_topology_release(struct cpupower_topology cpu_top) -{ - free(cpu_top.core_info); -} diff --git a/tools/power/cpupower/utils/idle_monitor/cpuidle_sysfs.c b/tools/power/cpupower/utils/idle_monitor/cpuidle_sysfs.c index bcd22a1a3970..1b5da0066ebf 100644 --- a/tools/power/cpupower/utils/idle_monitor/cpuidle_sysfs.c +++ b/tools/power/cpupower/utils/idle_monitor/cpuidle_sysfs.c @@ -10,8 +10,8 @@ #include #include #include +#include -#include "helpers/sysfs.h" #include "helpers/helpers.h" #include "idle_monitor/cpupower-monitor.h" @@ -51,7 +51,7 @@ static int cpuidle_start(void) for (state = 0; state < cpuidle_sysfs_monitor.hw_states_num; state++) { previous_count[cpu][state] = - sysfs_get_idlestate_time(cpu, state); + cpuidle_state_time(cpu, state); dprint("CPU %d - State: %d - Val: %llu\n", cpu, state, previous_count[cpu][state]); } @@ -70,7 +70,7 @@ static int cpuidle_stop(void) for (state = 0; state < cpuidle_sysfs_monitor.hw_states_num; state++) { current_count[cpu][state] = - sysfs_get_idlestate_time(cpu, state); + cpuidle_state_time(cpu, state); dprint("CPU %d - State: %d - Val: %llu\n", cpu, state, previous_count[cpu][state]); } @@ -132,13 +132,13 @@ static struct cpuidle_monitor *cpuidle_register(void) char *tmp; /* Assume idle state count is the same for all CPUs */ - cpuidle_sysfs_monitor.hw_states_num = sysfs_get_idlestate_count(0); + cpuidle_sysfs_monitor.hw_states_num = cpuidle_state_count(0); if (cpuidle_sysfs_monitor.hw_states_num <= 0) return NULL; for (num = 0; num < cpuidle_sysfs_monitor.hw_states_num; num++) { - tmp = sysfs_get_idlestate_name(0, num); + tmp = cpuidle_state_name(0, num); if (tmp == NULL) continue; @@ -146,7 +146,7 @@ static struct cpuidle_monitor *cpuidle_register(void) strncpy(cpuidle_cstates[num].name, tmp, CSTATE_NAME_LEN - 1); free(tmp); - tmp = sysfs_get_idlestate_desc(0, num); + tmp = cpuidle_state_desc(0, num); if (tmp == NULL) continue; strncpy(cpuidle_cstates[num].desc, tmp, CSTATE_DESC_LEN - 1); -- cgit v1.2.3-59-g8ed1b