1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
15 #include "cpupower_intern.h"
17 unsigned int cpupower_read_sysfs(const char *path
, char *buf
, size_t buflen
)
22 fd
= open(path
, O_RDONLY
);
26 numread
= read(fd
, buf
, buflen
- 1);
35 return (unsigned int) numread
;
38 unsigned int cpupower_write_sysfs(const char *path
, char *buf
, size_t buflen
)
43 fd
= open(path
, O_WRONLY
);
47 numwritten
= write(fd
, buf
, buflen
- 1);
56 return (unsigned int) numwritten
;
60 * Detect whether a CPU is online
63 * 1 -> if CPU is online
64 * 0 -> if CPU is offline
65 * negative errno values in error case
67 int cpupower_is_cpu_online(unsigned int cpu
)
69 char path
[SYSFS_PATH_MAX
];
72 unsigned long long value
;
73 char linebuf
[MAX_LINE_LEN
];
77 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u", cpu
);
79 if (stat(path
, &statbuf
) != 0)
83 * kernel without CONFIG_HOTPLUG_CPU
84 * -> cpuX directory exists, but not cpuX/online file
86 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u/online", cpu
);
87 if (stat(path
, &statbuf
) != 0)
90 fd
= open(path
, O_RDONLY
);
94 numread
= read(fd
, linebuf
, MAX_LINE_LEN
- 1);
99 linebuf
[numread
] = '\0';
102 value
= strtoull(linebuf
, &endp
, 0);
109 /* returns -1 on failure, 0 on success */
110 static int sysfs_topology_read_file(unsigned int cpu
, const char *fname
, int *result
)
112 char linebuf
[MAX_LINE_LEN
];
114 char path
[SYSFS_PATH_MAX
];
116 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u/topology/%s",
118 if (cpupower_read_sysfs(path
, linebuf
, MAX_LINE_LEN
) == 0)
120 *result
= strtol(linebuf
, &endp
, 0);
121 if (endp
== linebuf
|| errno
== ERANGE
)
126 static int __compare(const void *t1
, const void *t2
)
128 struct cpuid_core_info
*top1
= (struct cpuid_core_info
*)t1
;
129 struct cpuid_core_info
*top2
= (struct cpuid_core_info
*)t2
;
130 if (top1
->pkg
< top2
->pkg
)
132 else if (top1
->pkg
> top2
->pkg
)
134 else if (top1
->core
< top2
->core
)
136 else if (top1
->core
> top2
->core
)
138 else if (top1
->cpu
< top2
->cpu
)
140 else if (top1
->cpu
> top2
->cpu
)
147 * Returns amount of cpus, negative on error, cpu_top must be
148 * passed to cpu_topology_release to free resources
150 * Array is sorted after ->pkg, ->core, then ->cpu
152 int get_cpu_topology(struct cpupower_topology
*cpu_top
)
154 int cpu
, last_pkg
, cpus
= sysconf(_SC_NPROCESSORS_CONF
);
156 cpu_top
->core_info
= malloc(sizeof(struct cpuid_core_info
) * cpus
);
157 if (cpu_top
->core_info
== NULL
)
159 cpu_top
->pkgs
= cpu_top
->cores
= 0;
160 for (cpu
= 0; cpu
< cpus
; cpu
++) {
161 cpu_top
->core_info
[cpu
].cpu
= cpu
;
162 cpu_top
->core_info
[cpu
].is_online
= cpupower_is_cpu_online(cpu
);
163 if(sysfs_topology_read_file(
165 "physical_package_id",
166 &(cpu_top
->core_info
[cpu
].pkg
)) < 0) {
167 cpu_top
->core_info
[cpu
].pkg
= -1;
168 cpu_top
->core_info
[cpu
].core
= -1;
171 if(sysfs_topology_read_file(
174 &(cpu_top
->core_info
[cpu
].core
)) < 0) {
175 cpu_top
->core_info
[cpu
].pkg
= -1;
176 cpu_top
->core_info
[cpu
].core
= -1;
181 qsort(cpu_top
->core_info
, cpus
, sizeof(struct cpuid_core_info
),
184 /* Count the number of distinct pkgs values. This works
185 because the primary sort of the core_info struct was just
186 done by pkg value. */
187 last_pkg
= cpu_top
->core_info
[0].pkg
;
188 for(cpu
= 1; cpu
< cpus
; cpu
++) {
189 if (cpu_top
->core_info
[cpu
].pkg
!= last_pkg
&&
190 cpu_top
->core_info
[cpu
].pkg
!= -1) {
192 last_pkg
= cpu_top
->core_info
[cpu
].pkg
;
196 if (!(cpu_top
->core_info
[0].pkg
== -1))
199 /* Intel's cores count is not consecutively numbered, there may
200 * be a core_id of 3, but none of 2. Assume there always is 0
201 * Get amount of cores by counting duplicates in a package
202 for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
203 if (cpu_top->core_info[cpu].core == 0)
209 void cpu_topology_release(struct cpupower_topology cpu_top
)
211 free(cpu_top
.core_info
);