aboutsummaryrefslogtreecommitdiffstats
path: root/tools/power/cpupower/utils/helpers/topology.c
diff options
context:
space:
mode:
authorThomas Renninger <trenn@suse.com>2016-04-28 15:24:40 +0200
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>2016-04-28 16:02:29 +0200
commitac5a181d065d74fb6b213d538f743392f27bcdbd (patch)
treece5eab193a80e4514c1ebe931c391af57741a99d /tools/power/cpupower/utils/helpers/topology.c
parentcpupowerutils: bench: trivial fix of spelling mistake on "average" (diff)
downloadlinux-dev-ac5a181d065d74fb6b213d538f743392f27bcdbd.tar.xz
linux-dev-ac5a181d065d74fb6b213d538f743392f27bcdbd.zip
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 <trenn@suse.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Diffstat (limited to 'tools/power/cpupower/utils/helpers/topology.c')
-rw-r--r--tools/power/cpupower/utils/helpers/topology.c107
1 files changed, 2 insertions, 105 deletions
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 <errno.h>
#include <fcntl.h>
-#include <helpers/helpers.h>
-#include <helpers/sysfs.h>
+#include <cpuidle.h>
-/* 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);
-}