2 * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
4 * Licensed under the terms of the GNU GPL License version 2.
16 #include "cpupower_intern.h"
18 unsigned int sysfs_read_file(const char *path
, char *buf
, size_t buflen
)
23 fd
= open(path
, O_RDONLY
);
27 numread
= read(fd
, buf
, buflen
- 1);
36 return (unsigned int) numread
;
40 * Detect whether a CPU is online
43 * 1 -> if CPU is online
44 * 0 -> if CPU is offline
45 * negative errno values in error case
47 int cpupower_is_cpu_online(unsigned int cpu
)
49 char path
[SYSFS_PATH_MAX
];
52 unsigned long long value
;
53 char linebuf
[MAX_LINE_LEN
];
57 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u", cpu
);
59 if (stat(path
, &statbuf
) != 0)
63 * kernel without CONFIG_HOTPLUG_CPU
64 * -> cpuX directory exists, but not cpuX/online file
66 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u/online", cpu
);
67 if (stat(path
, &statbuf
) != 0)
70 fd
= open(path
, O_RDONLY
);
74 numread
= read(fd
, linebuf
, MAX_LINE_LEN
- 1);
79 linebuf
[numread
] = '\0';
82 value
= strtoull(linebuf
, &endp
, 0);
89 /* returns -1 on failure, 0 on success */
90 static int sysfs_topology_read_file(unsigned int cpu
, const char *fname
, int *result
)
92 char linebuf
[MAX_LINE_LEN
];
94 char path
[SYSFS_PATH_MAX
];
96 snprintf(path
, sizeof(path
), PATH_TO_CPU
"cpu%u/topology/%s",
98 if (sysfs_read_file(path
, linebuf
, MAX_LINE_LEN
) == 0)
100 *result
= strtol(linebuf
, &endp
, 0);
101 if (endp
== linebuf
|| errno
== ERANGE
)
106 static int __compare(const void *t1
, const void *t2
)
108 struct cpuid_core_info
*top1
= (struct cpuid_core_info
*)t1
;
109 struct cpuid_core_info
*top2
= (struct cpuid_core_info
*)t2
;
110 if (top1
->pkg
< top2
->pkg
)
112 else if (top1
->pkg
> top2
->pkg
)
114 else if (top1
->core
< top2
->core
)
116 else if (top1
->core
> top2
->core
)
118 else if (top1
->cpu
< top2
->cpu
)
120 else if (top1
->cpu
> top2
->cpu
)
127 * Returns amount of cpus, negative on error, cpu_top must be
128 * passed to cpu_topology_release to free resources
130 * Array is sorted after ->pkg, ->core, then ->cpu
132 int get_cpu_topology(struct cpupower_topology
*cpu_top
)
134 int cpu
, last_pkg
, cpus
= sysconf(_SC_NPROCESSORS_CONF
);
136 cpu_top
->core_info
= malloc(sizeof(struct cpuid_core_info
) * cpus
);
137 if (cpu_top
->core_info
== NULL
)
139 cpu_top
->pkgs
= cpu_top
->cores
= 0;
140 for (cpu
= 0; cpu
< cpus
; cpu
++) {
141 cpu_top
->core_info
[cpu
].cpu
= cpu
;
142 cpu_top
->core_info
[cpu
].is_online
= cpupower_is_cpu_online(cpu
);
143 if(sysfs_topology_read_file(
145 "physical_package_id",
146 &(cpu_top
->core_info
[cpu
].pkg
)) < 0) {
147 cpu_top
->core_info
[cpu
].pkg
= -1;
148 cpu_top
->core_info
[cpu
].core
= -1;
151 if(sysfs_topology_read_file(
154 &(cpu_top
->core_info
[cpu
].core
)) < 0) {
155 cpu_top
->core_info
[cpu
].pkg
= -1;
156 cpu_top
->core_info
[cpu
].core
= -1;
161 qsort(cpu_top
->core_info
, cpus
, sizeof(struct cpuid_core_info
),
164 /* Count the number of distinct pkgs values. This works
165 because the primary sort of the core_info struct was just
166 done by pkg value. */
167 last_pkg
= cpu_top
->core_info
[0].pkg
;
168 for(cpu
= 1; cpu
< cpus
; cpu
++) {
169 if (cpu_top
->core_info
[cpu
].pkg
!= last_pkg
&&
170 cpu_top
->core_info
[cpu
].pkg
!= -1) {
172 last_pkg
= cpu_top
->core_info
[cpu
].pkg
;
176 if (!(cpu_top
->core_info
[0].pkg
== -1))
179 /* Intel's cores count is not consecutively numbered, there may
180 * be a core_id of 3, but none of 2. Assume there always is 0
181 * Get amount of cores by counting duplicates in a package
182 for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
183 if (cpu_top->core_info[cpu].core == 0)
189 void cpu_topology_release(struct cpupower_topology cpu_top
)
191 free(cpu_top
.core_info
);