1
2
3
4
5
6
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <stdio.h>
12#include <errno.h>
13#include <stdlib.h>
14
15#include "cpupower.h"
16#include "cpupower_intern.h"
17
18unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
19{
20 int fd;
21 ssize_t numread;
22
23 fd = open(path, O_RDONLY);
24 if (fd == -1)
25 return 0;
26
27 numread = read(fd, buf, buflen - 1);
28 if (numread < 1) {
29 close(fd);
30 return 0;
31 }
32
33 buf[numread] = '\0';
34 close(fd);
35
36 return (unsigned int) numread;
37}
38
39
40
41
42
43
44
45
46
47int cpupower_is_cpu_online(unsigned int cpu)
48{
49 char path[SYSFS_PATH_MAX];
50 int fd;
51 ssize_t numread;
52 unsigned long long value;
53 char linebuf[MAX_LINE_LEN];
54 char *endp;
55 struct stat statbuf;
56
57 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u", cpu);
58
59 if (stat(path, &statbuf) != 0)
60 return 0;
61
62
63
64
65
66 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/online", cpu);
67 if (stat(path, &statbuf) != 0)
68 return 1;
69
70 fd = open(path, O_RDONLY);
71 if (fd == -1)
72 return -errno;
73
74 numread = read(fd, linebuf, MAX_LINE_LEN - 1);
75 if (numread < 1) {
76 close(fd);
77 return -EIO;
78 }
79 linebuf[numread] = '\0';
80 close(fd);
81
82 value = strtoull(linebuf, &endp, 0);
83 if (value > 1)
84 return -EINVAL;
85
86 return value;
87}
88
89
90static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result)
91{
92 char linebuf[MAX_LINE_LEN];
93 char *endp;
94 char path[SYSFS_PATH_MAX];
95
96 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s",
97 cpu, fname);
98 if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0)
99 return -1;
100 *result = strtol(linebuf, &endp, 0);
101 if (endp == linebuf || errno == ERANGE)
102 return -1;
103 return 0;
104}
105
106static int __compare(const void *t1, const void *t2)
107{
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)
111 return -1;
112 else if (top1->pkg > top2->pkg)
113 return 1;
114 else if (top1->core < top2->core)
115 return -1;
116 else if (top1->core > top2->core)
117 return 1;
118 else if (top1->cpu < top2->cpu)
119 return -1;
120 else if (top1->cpu > top2->cpu)
121 return 1;
122 else
123 return 0;
124}
125
126
127
128
129
130
131
132int get_cpu_topology(struct cpupower_topology *cpu_top)
133{
134 int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF);
135
136 cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus);
137 if (cpu_top->core_info == NULL)
138 return -ENOMEM;
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(
144 cpu,
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;
149 continue;
150 }
151 if(sysfs_topology_read_file(
152 cpu,
153 "core_id",
154 &(cpu_top->core_info[cpu].core)) < 0) {
155 cpu_top->core_info[cpu].pkg = -1;
156 cpu_top->core_info[cpu].core = -1;
157 continue;
158 }
159 }
160
161 qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
162 __compare);
163
164
165
166
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) {
171
172 last_pkg = cpu_top->core_info[cpu].pkg;
173 cpu_top->pkgs++;
174 }
175 }
176 if (!(cpu_top->core_info[0].pkg == -1))
177 cpu_top->pkgs++;
178
179
180
181
182
183
184
185
186 return cpus;
187}
188
189void cpu_topology_release(struct cpupower_topology cpu_top)
190{
191 free(cpu_top.core_info);
192}
193