1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
15 #include "cpupower_intern.h"
17 int is_valid_path(const char *path)
19 if (access(path, F_OK) == -1)
24 unsigned int cpupower_read_sysfs(const char *path, char *buf, size_t buflen)
29 fd = open(path, O_RDONLY);
33 numread = read(fd, buf, buflen - 1);
42 return (unsigned int) numread;
45 unsigned int cpupower_write_sysfs(const char *path, char *buf, size_t buflen)
50 fd = open(path, O_WRONLY);
54 numwritten = write(fd, buf, buflen - 1);
63 return (unsigned int) numwritten;
67 * Detect whether a CPU is online
70 * 1 -> if CPU is online
71 * 0 -> if CPU is offline
72 * negative errno values in error case
74 int cpupower_is_cpu_online(unsigned int cpu)
76 char path[SYSFS_PATH_MAX];
79 unsigned long long value;
80 char linebuf[MAX_LINE_LEN];
84 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u", cpu);
86 if (stat(path, &statbuf) != 0)
90 * kernel without CONFIG_HOTPLUG_CPU
91 * -> cpuX directory exists, but not cpuX/online file
93 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/online", cpu);
94 if (stat(path, &statbuf) != 0)
97 fd = open(path, O_RDONLY);
101 numread = read(fd, linebuf, MAX_LINE_LEN - 1);
106 linebuf[numread] = '\0';
109 value = strtoull(linebuf, &endp, 0);
116 /* returns -1 on failure, 0 on success */
117 static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result)
119 char linebuf[MAX_LINE_LEN];
121 char path[SYSFS_PATH_MAX];
123 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s",
125 if (cpupower_read_sysfs(path, linebuf, MAX_LINE_LEN) == 0)
127 *result = strtol(linebuf, &endp, 0);
128 if (endp == linebuf || errno == ERANGE)
133 static int __compare(const void *t1, const void *t2)
135 struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1;
136 struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2;
137 if (top1->pkg < top2->pkg)
139 else if (top1->pkg > top2->pkg)
141 else if (top1->core < top2->core)
143 else if (top1->core > top2->core)
145 else if (top1->cpu < top2->cpu)
147 else if (top1->cpu > top2->cpu)
154 * Returns amount of cpus, negative on error, cpu_top must be
155 * passed to cpu_topology_release to free resources
157 * Array is sorted after ->pkg, ->core, then ->cpu
159 int get_cpu_topology(struct cpupower_topology *cpu_top)
161 int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF);
163 cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus);
164 if (cpu_top->core_info == NULL)
166 cpu_top->pkgs = cpu_top->cores = 0;
167 for (cpu = 0; cpu < cpus; cpu++) {
168 cpu_top->core_info[cpu].cpu = cpu;
169 cpu_top->core_info[cpu].is_online = cpupower_is_cpu_online(cpu);
170 if(sysfs_topology_read_file(
172 "physical_package_id",
173 &(cpu_top->core_info[cpu].pkg)) < 0) {
174 cpu_top->core_info[cpu].pkg = -1;
175 cpu_top->core_info[cpu].core = -1;
178 if(sysfs_topology_read_file(
181 &(cpu_top->core_info[cpu].core)) < 0) {
182 cpu_top->core_info[cpu].pkg = -1;
183 cpu_top->core_info[cpu].core = -1;
188 qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
191 /* Count the number of distinct pkgs values. This works
192 because the primary sort of the core_info struct was just
193 done by pkg value. */
194 last_pkg = cpu_top->core_info[0].pkg;
195 for(cpu = 1; cpu < cpus; cpu++) {
196 if (cpu_top->core_info[cpu].pkg != last_pkg &&
197 cpu_top->core_info[cpu].pkg != -1) {
199 last_pkg = cpu_top->core_info[cpu].pkg;
203 if (!(cpu_top->core_info[0].pkg == -1))
206 /* Intel's cores count is not consecutively numbered, there may
207 * be a core_id of 3, but none of 2. Assume there always is 0
208 * Get amount of cores by counting duplicates in a package
209 for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
210 if (cpu_top->core_info[cpu].core == 0)
216 void cpu_topology_release(struct cpupower_topology cpu_top)
218 free(cpu_top.core_info);