14f19048fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ac5a181dSThomas Renninger /*
3ac5a181dSThomas Renninger * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
4ac5a181dSThomas Renninger */
5ac5a181dSThomas Renninger
6ac5a181dSThomas Renninger #include <sys/types.h>
7ac5a181dSThomas Renninger #include <sys/stat.h>
8ac5a181dSThomas Renninger #include <fcntl.h>
9ac5a181dSThomas Renninger #include <unistd.h>
10ac5a181dSThomas Renninger #include <stdio.h>
11ac5a181dSThomas Renninger #include <errno.h>
12ac5a181dSThomas Renninger #include <stdlib.h>
13ac5a181dSThomas Renninger
14ac5a181dSThomas Renninger #include "cpupower.h"
15ac5a181dSThomas Renninger #include "cpupower_intern.h"
16ac5a181dSThomas Renninger
is_valid_path(const char * path)17*1ce5ab7cSWyes Karny int is_valid_path(const char *path)
18*1ce5ab7cSWyes Karny {
19*1ce5ab7cSWyes Karny if (access(path, F_OK) == -1)
20*1ce5ab7cSWyes Karny return 0;
21*1ce5ab7cSWyes Karny return 1;
22*1ce5ab7cSWyes Karny }
23*1ce5ab7cSWyes Karny
cpupower_read_sysfs(const char * path,char * buf,size_t buflen)249de9aa45SKonstantin Khlebnikov unsigned int cpupower_read_sysfs(const char *path, char *buf, size_t buflen)
25ac5a181dSThomas Renninger {
26ac5a181dSThomas Renninger ssize_t numread;
278113ab20SBorislav Petkov int fd;
28ac5a181dSThomas Renninger
29ac5a181dSThomas Renninger fd = open(path, O_RDONLY);
30ac5a181dSThomas Renninger if (fd == -1)
31ac5a181dSThomas Renninger return 0;
32ac5a181dSThomas Renninger
33ac5a181dSThomas Renninger numread = read(fd, buf, buflen - 1);
34ac5a181dSThomas Renninger if (numread < 1) {
35ac5a181dSThomas Renninger close(fd);
36ac5a181dSThomas Renninger return 0;
37ac5a181dSThomas Renninger }
38ac5a181dSThomas Renninger
39ac5a181dSThomas Renninger buf[numread] = '\0';
40ac5a181dSThomas Renninger close(fd);
41ac5a181dSThomas Renninger
42ac5a181dSThomas Renninger return (unsigned int) numread;
43ac5a181dSThomas Renninger }
44ac5a181dSThomas Renninger
cpupower_write_sysfs(const char * path,char * buf,size_t buflen)458113ab20SBorislav Petkov unsigned int cpupower_write_sysfs(const char *path, char *buf, size_t buflen)
468113ab20SBorislav Petkov {
478113ab20SBorislav Petkov ssize_t numwritten;
488113ab20SBorislav Petkov int fd;
498113ab20SBorislav Petkov
508113ab20SBorislav Petkov fd = open(path, O_WRONLY);
518113ab20SBorislav Petkov if (fd == -1)
528113ab20SBorislav Petkov return 0;
538113ab20SBorislav Petkov
548113ab20SBorislav Petkov numwritten = write(fd, buf, buflen - 1);
558113ab20SBorislav Petkov if (numwritten < 1) {
568113ab20SBorislav Petkov perror(path);
578113ab20SBorislav Petkov close(fd);
588113ab20SBorislav Petkov return -1;
598113ab20SBorislav Petkov }
608113ab20SBorislav Petkov
618113ab20SBorislav Petkov close(fd);
628113ab20SBorislav Petkov
638113ab20SBorislav Petkov return (unsigned int) numwritten;
648113ab20SBorislav Petkov }
658113ab20SBorislav Petkov
66ac5a181dSThomas Renninger /*
67ac5a181dSThomas Renninger * Detect whether a CPU is online
68ac5a181dSThomas Renninger *
69ac5a181dSThomas Renninger * Returns:
70ac5a181dSThomas Renninger * 1 -> if CPU is online
71ac5a181dSThomas Renninger * 0 -> if CPU is offline
72ac5a181dSThomas Renninger * negative errno values in error case
73ac5a181dSThomas Renninger */
cpupower_is_cpu_online(unsigned int cpu)74ac5a181dSThomas Renninger int cpupower_is_cpu_online(unsigned int cpu)
75ac5a181dSThomas Renninger {
76ac5a181dSThomas Renninger char path[SYSFS_PATH_MAX];
77ac5a181dSThomas Renninger int fd;
78ac5a181dSThomas Renninger ssize_t numread;
79ac5a181dSThomas Renninger unsigned long long value;
80ac5a181dSThomas Renninger char linebuf[MAX_LINE_LEN];
81ac5a181dSThomas Renninger char *endp;
82ac5a181dSThomas Renninger struct stat statbuf;
83ac5a181dSThomas Renninger
84ac5a181dSThomas Renninger snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u", cpu);
85ac5a181dSThomas Renninger
86ac5a181dSThomas Renninger if (stat(path, &statbuf) != 0)
87ac5a181dSThomas Renninger return 0;
88ac5a181dSThomas Renninger
89ac5a181dSThomas Renninger /*
90ac5a181dSThomas Renninger * kernel without CONFIG_HOTPLUG_CPU
91ac5a181dSThomas Renninger * -> cpuX directory exists, but not cpuX/online file
92ac5a181dSThomas Renninger */
93ac5a181dSThomas Renninger snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/online", cpu);
94ac5a181dSThomas Renninger if (stat(path, &statbuf) != 0)
95ac5a181dSThomas Renninger return 1;
96ac5a181dSThomas Renninger
97ac5a181dSThomas Renninger fd = open(path, O_RDONLY);
98ac5a181dSThomas Renninger if (fd == -1)
99ac5a181dSThomas Renninger return -errno;
100ac5a181dSThomas Renninger
101ac5a181dSThomas Renninger numread = read(fd, linebuf, MAX_LINE_LEN - 1);
102ac5a181dSThomas Renninger if (numread < 1) {
103ac5a181dSThomas Renninger close(fd);
104ac5a181dSThomas Renninger return -EIO;
105ac5a181dSThomas Renninger }
106ac5a181dSThomas Renninger linebuf[numread] = '\0';
107ac5a181dSThomas Renninger close(fd);
108ac5a181dSThomas Renninger
109ac5a181dSThomas Renninger value = strtoull(linebuf, &endp, 0);
110ac5a181dSThomas Renninger if (value > 1)
111ac5a181dSThomas Renninger return -EINVAL;
112ac5a181dSThomas Renninger
113ac5a181dSThomas Renninger return value;
114ac5a181dSThomas Renninger }
115ac5a181dSThomas Renninger
116ac5a181dSThomas Renninger /* returns -1 on failure, 0 on success */
sysfs_topology_read_file(unsigned int cpu,const char * fname,int * result)117ac5a181dSThomas Renninger static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result)
118ac5a181dSThomas Renninger {
119ac5a181dSThomas Renninger char linebuf[MAX_LINE_LEN];
120ac5a181dSThomas Renninger char *endp;
121ac5a181dSThomas Renninger char path[SYSFS_PATH_MAX];
122ac5a181dSThomas Renninger
123ac5a181dSThomas Renninger snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s",
124ac5a181dSThomas Renninger cpu, fname);
1259de9aa45SKonstantin Khlebnikov if (cpupower_read_sysfs(path, linebuf, MAX_LINE_LEN) == 0)
126ac5a181dSThomas Renninger return -1;
127ac5a181dSThomas Renninger *result = strtol(linebuf, &endp, 0);
128ac5a181dSThomas Renninger if (endp == linebuf || errno == ERANGE)
129ac5a181dSThomas Renninger return -1;
130ac5a181dSThomas Renninger return 0;
131ac5a181dSThomas Renninger }
132ac5a181dSThomas Renninger
__compare(const void * t1,const void * t2)133ac5a181dSThomas Renninger static int __compare(const void *t1, const void *t2)
134ac5a181dSThomas Renninger {
135ac5a181dSThomas Renninger struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1;
136ac5a181dSThomas Renninger struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2;
137ac5a181dSThomas Renninger if (top1->pkg < top2->pkg)
138ac5a181dSThomas Renninger return -1;
139ac5a181dSThomas Renninger else if (top1->pkg > top2->pkg)
140ac5a181dSThomas Renninger return 1;
141ac5a181dSThomas Renninger else if (top1->core < top2->core)
142ac5a181dSThomas Renninger return -1;
143ac5a181dSThomas Renninger else if (top1->core > top2->core)
144ac5a181dSThomas Renninger return 1;
145ac5a181dSThomas Renninger else if (top1->cpu < top2->cpu)
146ac5a181dSThomas Renninger return -1;
147ac5a181dSThomas Renninger else if (top1->cpu > top2->cpu)
148ac5a181dSThomas Renninger return 1;
149ac5a181dSThomas Renninger else
150ac5a181dSThomas Renninger return 0;
151ac5a181dSThomas Renninger }
152ac5a181dSThomas Renninger
153ac5a181dSThomas Renninger /*
154ac5a181dSThomas Renninger * Returns amount of cpus, negative on error, cpu_top must be
155ac5a181dSThomas Renninger * passed to cpu_topology_release to free resources
156ac5a181dSThomas Renninger *
157ac5a181dSThomas Renninger * Array is sorted after ->pkg, ->core, then ->cpu
158ac5a181dSThomas Renninger */
get_cpu_topology(struct cpupower_topology * cpu_top)159ac5a181dSThomas Renninger int get_cpu_topology(struct cpupower_topology *cpu_top)
160ac5a181dSThomas Renninger {
161ac5a181dSThomas Renninger int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF);
162ac5a181dSThomas Renninger
163ac5a181dSThomas Renninger cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus);
164ac5a181dSThomas Renninger if (cpu_top->core_info == NULL)
165ac5a181dSThomas Renninger return -ENOMEM;
166ac5a181dSThomas Renninger cpu_top->pkgs = cpu_top->cores = 0;
167ac5a181dSThomas Renninger for (cpu = 0; cpu < cpus; cpu++) {
168ac5a181dSThomas Renninger cpu_top->core_info[cpu].cpu = cpu;
169ac5a181dSThomas Renninger cpu_top->core_info[cpu].is_online = cpupower_is_cpu_online(cpu);
170ac5a181dSThomas Renninger if(sysfs_topology_read_file(
171ac5a181dSThomas Renninger cpu,
172ac5a181dSThomas Renninger "physical_package_id",
173ac5a181dSThomas Renninger &(cpu_top->core_info[cpu].pkg)) < 0) {
174ac5a181dSThomas Renninger cpu_top->core_info[cpu].pkg = -1;
175ac5a181dSThomas Renninger cpu_top->core_info[cpu].core = -1;
176ac5a181dSThomas Renninger continue;
177ac5a181dSThomas Renninger }
178ac5a181dSThomas Renninger if(sysfs_topology_read_file(
179ac5a181dSThomas Renninger cpu,
180ac5a181dSThomas Renninger "core_id",
181ac5a181dSThomas Renninger &(cpu_top->core_info[cpu].core)) < 0) {
182ac5a181dSThomas Renninger cpu_top->core_info[cpu].pkg = -1;
183ac5a181dSThomas Renninger cpu_top->core_info[cpu].core = -1;
184ac5a181dSThomas Renninger continue;
185ac5a181dSThomas Renninger }
186ac5a181dSThomas Renninger }
187ac5a181dSThomas Renninger
188ac5a181dSThomas Renninger qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
189ac5a181dSThomas Renninger __compare);
190ac5a181dSThomas Renninger
191ac5a181dSThomas Renninger /* Count the number of distinct pkgs values. This works
192ac5a181dSThomas Renninger because the primary sort of the core_info struct was just
193ac5a181dSThomas Renninger done by pkg value. */
194ac5a181dSThomas Renninger last_pkg = cpu_top->core_info[0].pkg;
195ac5a181dSThomas Renninger for(cpu = 1; cpu < cpus; cpu++) {
196ac5a181dSThomas Renninger if (cpu_top->core_info[cpu].pkg != last_pkg &&
197ac5a181dSThomas Renninger cpu_top->core_info[cpu].pkg != -1) {
198ac5a181dSThomas Renninger
199ac5a181dSThomas Renninger last_pkg = cpu_top->core_info[cpu].pkg;
200ac5a181dSThomas Renninger cpu_top->pkgs++;
201ac5a181dSThomas Renninger }
202ac5a181dSThomas Renninger }
203ac5a181dSThomas Renninger if (!(cpu_top->core_info[0].pkg == -1))
204ac5a181dSThomas Renninger cpu_top->pkgs++;
205ac5a181dSThomas Renninger
206ac5a181dSThomas Renninger /* Intel's cores count is not consecutively numbered, there may
207ac5a181dSThomas Renninger * be a core_id of 3, but none of 2. Assume there always is 0
208ac5a181dSThomas Renninger * Get amount of cores by counting duplicates in a package
209ac5a181dSThomas Renninger for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
210ac5a181dSThomas Renninger if (cpu_top->core_info[cpu].core == 0)
211ac5a181dSThomas Renninger cpu_top->cores++;
212ac5a181dSThomas Renninger */
213ac5a181dSThomas Renninger return cpus;
214ac5a181dSThomas Renninger }
215ac5a181dSThomas Renninger
cpu_topology_release(struct cpupower_topology cpu_top)216ac5a181dSThomas Renninger void cpu_topology_release(struct cpupower_topology cpu_top)
217ac5a181dSThomas Renninger {
218ac5a181dSThomas Renninger free(cpu_top.core_info);
219ac5a181dSThomas Renninger }
220