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
;
39 * Detect whether a CPU is online
42 * 1 -> if CPU is online
43 * 0 -> if CPU is offline
44 * negative errno values in error case
46 int cpupower_is_cpu_online(unsigned int cpu
)
48 char path
[SYSFS_PATH_MAX
];
51 unsigned long long value
;
52 char linebuf
[MAX_LINE_LEN
];
56 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u", cpu
);
58 if (stat(path
, &statbuf
) != 0)
62 * kernel without CONFIG_HOTPLUG_CPU
63 * -> cpuX directory exists, but not cpuX/online file
65 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u/online", cpu
);
66 if (stat(path
, &statbuf
) != 0)
69 fd
= open(path
, O_RDONLY
);
73 numread
= read(fd
, linebuf
, MAX_LINE_LEN
- 1);
78 linebuf
[numread
] = '\0';
81 value
= strtoull(linebuf
, &endp
, 0);
88 /* returns -1 on failure, 0 on success */
89 static int sysfs_topology_read_file(unsigned int cpu
, const char *fname
, int *result
)
91 char linebuf
[MAX_LINE_LEN
];
93 char path
[SYSFS_PATH_MAX
];
95 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u/topology/%s",
97 if (cpupower_read_sysfs(path
, linebuf
, MAX_LINE_LEN
) == 0)
99 *result
= strtol(linebuf
, &endp
, 0);
100 if (endp
== linebuf
|| errno
== ERANGE
)
105 static int __compare(const void *t1
, const void *t2
)
107 struct cpuid_core_info
*top1
= (struct cpuid_core_info
*)t1
;
108 struct cpuid_core_info
*top2
= (struct cpuid_core_info
*)t2
;
109 if (top1
->pkg
< top2
->pkg
)
111 else if (top1
->pkg
> top2
->pkg
)
113 else if (top1
->core
< top2
->core
)
115 else if (top1
->core
> top2
->core
)
117 else if (top1
->cpu
< top2
->cpu
)
119 else if (top1
->cpu
> top2
->cpu
)
126 * Returns amount of cpus, negative on error, cpu_top must be
127 * passed to cpu_topology_release to free resources
129 * Array is sorted after ->pkg, ->core, then ->cpu
131 int get_cpu_topology(struct cpupower_topology
*cpu_top
)
133 int cpu
, last_pkg
, cpus
= sysconf(_SC_NPROCESSORS_CONF
);
135 cpu_top
->core_info
= malloc(sizeof(struct cpuid_core_info
) * cpus
);
136 if (cpu_top
->core_info
== NULL
)
138 cpu_top
->pkgs
= cpu_top
->cores
= 0;
139 for (cpu
= 0; cpu
< cpus
; cpu
++) {
140 cpu_top
->core_info
[cpu
].cpu
= cpu
;
141 cpu_top
->core_info
[cpu
].is_online
= cpupower_is_cpu_online(cpu
);
142 if(sysfs_topology_read_file(
144 "physical_package_id",
145 &(cpu_top
->core_info
[cpu
].pkg
)) < 0) {
146 cpu_top
->core_info
[cpu
].pkg
= -1;
147 cpu_top
->core_info
[cpu
].core
= -1;
150 if(sysfs_topology_read_file(
153 &(cpu_top
->core_info
[cpu
].core
)) < 0) {
154 cpu_top
->core_info
[cpu
].pkg
= -1;
155 cpu_top
->core_info
[cpu
].core
= -1;
160 qsort(cpu_top
->core_info
, cpus
, sizeof(struct cpuid_core_info
),
163 /* Count the number of distinct pkgs values. This works
164 because the primary sort of the core_info struct was just
165 done by pkg value. */
166 last_pkg
= cpu_top
->core_info
[0].pkg
;
167 for(cpu
= 1; cpu
< cpus
; cpu
++) {
168 if (cpu_top
->core_info
[cpu
].pkg
!= last_pkg
&&
169 cpu_top
->core_info
[cpu
].pkg
!= -1) {
171 last_pkg
= cpu_top
->core_info
[cpu
].pkg
;
175 if (!(cpu_top
->core_info
[0].pkg
== -1))
178 /* Intel's cores count is not consecutively numbered, there may
179 * be a core_id of 3, but none of 2. Assume there always is 0
180 * Get amount of cores by counting duplicates in a package
181 for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
182 if (cpu_top->core_info[cpu].core == 0)
188 void cpu_topology_release(struct cpupower_topology cpu_top
)
190 free(cpu_top
.core_info
);