Commit | Line | Data |
---|---|---|
a1957954 BVA |
1 | #include "os/os.h" |
2 | ||
3 | #include <windows.h> | |
4 | ||
a1957954 BVA |
5 | /* Return all processors regardless of processor group */ |
6 | unsigned int cpus_online(void) | |
7 | { | |
8 | return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); | |
9 | } | |
10 | ||
11 | static void print_mask(os_cpu_mask_t *cpumask) | |
12 | { | |
13 | for (int i = 0; i < FIO_CPU_MASK_ROWS; i++) | |
f5515176 | 14 | dprint(FD_PROCESS, "cpumask[%d]=%" PRIu64 "\n", i, cpumask->row[i]); |
a1957954 BVA |
15 | } |
16 | ||
17 | /* Return the index of the least significant set CPU in cpumask or -1 if no | |
18 | * CPUs are set */ | |
19 | int first_set_cpu(os_cpu_mask_t *cpumask) | |
20 | { | |
21 | int cpus_offset, mask_first_cpu, row; | |
22 | ||
23 | cpus_offset = 0; | |
24 | row = 0; | |
25 | mask_first_cpu = -1; | |
26 | while (mask_first_cpu < 0 && row < FIO_CPU_MASK_ROWS) { | |
27 | int row_first_cpu; | |
28 | ||
29 | row_first_cpu = __builtin_ffsll(cpumask->row[row]) - 1; | |
f5515176 | 30 | dprint(FD_PROCESS, "row_first_cpu=%d cpumask->row[%d]=%" PRIu64 "\n", |
a1957954 BVA |
31 | row_first_cpu, row, cpumask->row[row]); |
32 | if (row_first_cpu > -1) { | |
33 | mask_first_cpu = cpus_offset + row_first_cpu; | |
34 | dprint(FD_PROCESS, "first set cpu in mask is at index %d\n", | |
35 | mask_first_cpu); | |
36 | } else { | |
37 | cpus_offset += FIO_CPU_MASK_STRIDE; | |
38 | row++; | |
39 | } | |
40 | } | |
41 | ||
42 | return mask_first_cpu; | |
43 | } | |
44 | ||
45 | /* Return the index of the most significant set CPU in cpumask or -1 if no | |
46 | * CPUs are set */ | |
47 | static int last_set_cpu(os_cpu_mask_t *cpumask) | |
48 | { | |
49 | int cpus_offset, mask_last_cpu, row; | |
50 | ||
51 | cpus_offset = (FIO_CPU_MASK_ROWS - 1) * FIO_CPU_MASK_STRIDE; | |
52 | row = FIO_CPU_MASK_ROWS - 1; | |
53 | mask_last_cpu = -1; | |
54 | while (mask_last_cpu < 0 && row >= 0) { | |
55 | int row_last_cpu; | |
56 | ||
57 | if (cpumask->row[row] == 0) | |
58 | row_last_cpu = -1; | |
59 | else { | |
60 | uint64_t tmp = cpumask->row[row]; | |
61 | ||
62 | row_last_cpu = 0; | |
63 | while (tmp >>= 1) | |
64 | row_last_cpu++; | |
65 | } | |
66 | ||
f5515176 | 67 | dprint(FD_PROCESS, "row_last_cpu=%d cpumask->row[%d]=%" PRIu64 "\n", |
a1957954 BVA |
68 | row_last_cpu, row, cpumask->row[row]); |
69 | if (row_last_cpu > -1) { | |
70 | mask_last_cpu = cpus_offset + row_last_cpu; | |
71 | dprint(FD_PROCESS, "last set cpu in mask is at index %d\n", | |
72 | mask_last_cpu); | |
73 | } else { | |
74 | cpus_offset -= FIO_CPU_MASK_STRIDE; | |
75 | row--; | |
76 | } | |
77 | } | |
78 | ||
79 | return mask_last_cpu; | |
80 | } | |
81 | ||
82 | static int mask_to_group_mask(os_cpu_mask_t *cpumask, int *processor_group, uint64_t *affinity_mask) | |
83 | { | |
84 | WORD online_groups, group, group_size; | |
85 | bool found; | |
86 | int cpus_offset, search_cpu, last_cpu, bit_offset, row, end; | |
87 | uint64_t group_cpumask; | |
88 | ||
89 | search_cpu = first_set_cpu(cpumask); | |
90 | if (search_cpu < 0) { | |
91 | log_info("CPU mask doesn't set any CPUs\n"); | |
92 | return 1; | |
93 | } | |
94 | ||
95 | /* Find processor group first set CPU applies to */ | |
96 | online_groups = GetActiveProcessorGroupCount(); | |
97 | group = 0; | |
98 | found = false; | |
99 | cpus_offset = 0; | |
100 | group_size = 0; | |
101 | while (!found && group < online_groups) { | |
102 | group_size = GetActiveProcessorCount(group); | |
103 | dprint(FD_PROCESS, "group=%d group_start=%d group_size=%u search_cpu=%d\n", | |
104 | group, cpus_offset, group_size, search_cpu); | |
105 | if (cpus_offset + group_size > search_cpu) | |
106 | found = true; | |
107 | else { | |
108 | cpus_offset += group_size; | |
109 | group++; | |
110 | } | |
111 | } | |
112 | ||
113 | if (!found) { | |
114 | log_err("CPU mask contains processor beyond last active processor index (%d)\n", | |
115 | cpus_offset - 1); | |
116 | print_mask(cpumask); | |
117 | return 1; | |
118 | } | |
119 | ||
120 | /* Check all the CPUs in the mask apply to ONLY that processor group */ | |
121 | last_cpu = last_set_cpu(cpumask); | |
122 | if (last_cpu > (cpus_offset + group_size - 1)) { | |
123 | log_info("CPU mask cannot bind CPUs (e.g. %d, %d) that are " | |
124 | "in different processor groups\n", search_cpu, | |
125 | last_cpu); | |
126 | print_mask(cpumask); | |
127 | return 1; | |
128 | } | |
129 | ||
130 | /* Extract the current processor group mask from the cpumask */ | |
131 | row = cpus_offset / FIO_CPU_MASK_STRIDE; | |
132 | bit_offset = cpus_offset % FIO_CPU_MASK_STRIDE; | |
133 | group_cpumask = cpumask->row[row] >> bit_offset; | |
134 | end = bit_offset + group_size; | |
135 | if (end > FIO_CPU_MASK_STRIDE && (row + 1 < FIO_CPU_MASK_ROWS)) { | |
136 | /* Some of the next row needs to be part of the mask */ | |
137 | int needed, needed_shift, needed_mask_shift; | |
138 | uint64_t needed_mask; | |
139 | ||
140 | needed = end - FIO_CPU_MASK_STRIDE; | |
141 | needed_shift = FIO_CPU_MASK_STRIDE - bit_offset; | |
142 | needed_mask_shift = FIO_CPU_MASK_STRIDE - needed; | |
143 | needed_mask = (uint64_t)-1 >> needed_mask_shift; | |
f5515176 SW |
144 | dprint(FD_PROCESS, |
145 | "bit_offset=%d end=%d needed=%d needed_shift=%d needed_mask=%" PRIu64 "needed_mask_shift=%d\n", | |
146 | bit_offset, end, needed, needed_shift, needed_mask, | |
147 | needed_mask_shift); | |
a1957954 BVA |
148 | group_cpumask |= (cpumask->row[row + 1] & needed_mask) << needed_shift; |
149 | } | |
150 | group_cpumask &= (uint64_t)-1 >> (FIO_CPU_MASK_STRIDE - group_size); | |
151 | ||
152 | /* Return group and mask */ | |
f5515176 SW |
153 | dprint(FD_PROCESS, "Returning group=%d group_mask=%" PRIu64 "\n", |
154 | group, group_cpumask); | |
a1957954 BVA |
155 | *processor_group = group; |
156 | *affinity_mask = group_cpumask; | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
161 | int fio_setaffinity(int pid, os_cpu_mask_t cpumask) | |
162 | { | |
163 | HANDLE handle = NULL; | |
164 | int group, ret; | |
165 | uint64_t group_mask = 0; | |
166 | GROUP_AFFINITY new_group_affinity; | |
167 | ||
168 | ret = -1; | |
169 | ||
170 | if (mask_to_group_mask(&cpumask, &group, &group_mask) != 0) | |
171 | goto err; | |
172 | ||
173 | handle = OpenThread(THREAD_QUERY_INFORMATION | THREAD_SET_INFORMATION, | |
174 | TRUE, pid); | |
175 | if (handle == NULL) { | |
176 | log_err("fio_setaffinity: failed to get handle for pid %d\n", pid); | |
177 | goto err; | |
178 | } | |
179 | ||
180 | /* Set group and mask. | |
181 | * Note: if the GROUP_AFFINITY struct's Reserved members are not | |
182 | * initialised to 0 then SetThreadGroupAffinity will fail with | |
183 | * GetLastError() set to ERROR_INVALID_PARAMETER */ | |
184 | new_group_affinity.Mask = (KAFFINITY) group_mask; | |
185 | new_group_affinity.Group = group; | |
186 | new_group_affinity.Reserved[0] = 0; | |
187 | new_group_affinity.Reserved[1] = 0; | |
188 | new_group_affinity.Reserved[2] = 0; | |
189 | if (SetThreadGroupAffinity(handle, &new_group_affinity, NULL) != 0) | |
190 | ret = 0; | |
191 | else { | |
4937100f SW |
192 | log_err("fio_setaffinity: failed to set thread affinity (pid %d, group %d, mask %" PRIx64 ", GetLastError=%lu)\n", |
193 | pid, group, group_mask, GetLastError()); | |
a1957954 BVA |
194 | goto err; |
195 | } | |
196 | ||
197 | err: | |
198 | if (handle) | |
199 | CloseHandle(handle); | |
200 | return ret; | |
201 | } | |
202 | ||
203 | static void cpu_to_row_offset(int cpu, int *row, int *offset) | |
204 | { | |
205 | *row = cpu / FIO_CPU_MASK_STRIDE; | |
206 | *offset = cpu << FIO_CPU_MASK_STRIDE * *row; | |
207 | } | |
208 | ||
209 | int fio_cpuset_init(os_cpu_mask_t *mask) | |
210 | { | |
211 | for (int i = 0; i < FIO_CPU_MASK_ROWS; i++) | |
212 | mask->row[i] = 0; | |
213 | return 0; | |
214 | } | |
215 | ||
216 | /* | |
217 | * fio_getaffinity() should not be called once a fio_setaffinity() call has | |
218 | * been made because fio_setaffinity() may put the process into multiple | |
219 | * processor groups | |
220 | */ | |
221 | int fio_getaffinity(int pid, os_cpu_mask_t *mask) | |
222 | { | |
223 | int ret; | |
224 | int row, offset, end, group, group_size, group_start_cpu; | |
225 | DWORD_PTR process_mask, system_mask; | |
226 | HANDLE handle; | |
227 | PUSHORT current_groups; | |
228 | USHORT group_count; | |
229 | WORD online_groups; | |
230 | ||
231 | ret = -1; | |
232 | current_groups = NULL; | |
233 | handle = OpenProcess(PROCESS_QUERY_INFORMATION, TRUE, pid); | |
234 | if (handle == NULL) { | |
235 | log_err("fio_getaffinity: failed to get handle for pid %d\n", | |
236 | pid); | |
237 | goto err; | |
238 | } | |
239 | ||
2f56f0a0 | 240 | group_count = 16; |
a1957954 BVA |
241 | /* |
242 | * GetProcessGroupAffinity() seems to expect more than the natural | |
243 | * alignment for a USHORT from the area pointed to by current_groups so | |
244 | * arrange for maximum alignment by allocating via malloc() | |
245 | */ | |
2f56f0a0 | 246 | current_groups = malloc(group_count * sizeof(USHORT)); |
a1957954 BVA |
247 | if (!current_groups) { |
248 | log_err("fio_getaffinity: malloc failed\n"); | |
249 | goto err; | |
250 | } | |
2f56f0a0 | 251 | if (!GetProcessGroupAffinity(handle, &group_count, current_groups)) { |
4937100f | 252 | log_err("%s: failed to get single group affinity for pid %d (%lu)\n", |
2f56f0a0 BVA |
253 | __func__, pid, GetLastError()); |
254 | goto err; | |
255 | } | |
256 | if (group_count > 1) { | |
257 | log_err("%s: pid %d is associated with %d process groups\n", | |
258 | __func__, pid, group_count); | |
259 | goto err; | |
260 | } | |
261 | if (!GetProcessAffinityMask(handle, &process_mask, &system_mask)) { | |
f5515176 | 262 | log_err("%s: GetProcessAffinityMask() failed for pid %d\n", |
2f56f0a0 | 263 | __func__, pid); |
a1957954 BVA |
264 | goto err; |
265 | } | |
a1957954 BVA |
266 | |
267 | /* Convert group and group relative mask to full CPU mask */ | |
268 | online_groups = GetActiveProcessorGroupCount(); | |
269 | if (online_groups == 0) { | |
270 | log_err("fio_getaffinity: error retrieving total processor groups\n"); | |
271 | goto err; | |
272 | } | |
273 | ||
274 | group = 0; | |
275 | group_start_cpu = 0; | |
276 | group_size = 0; | |
277 | dprint(FD_PROCESS, "current_groups=%d group_count=%d\n", | |
278 | current_groups[0], group_count); | |
279 | while (true) { | |
280 | group_size = GetActiveProcessorCount(group); | |
281 | if (group_size == 0) { | |
282 | log_err("fio_getaffinity: error retrieving size of " | |
283 | "processor group %d\n", group); | |
284 | goto err; | |
285 | } else if (group >= current_groups[0] || group >= online_groups) | |
286 | break; | |
287 | else { | |
288 | group_start_cpu += group_size; | |
289 | group++; | |
290 | } | |
291 | } | |
292 | ||
293 | if (group != current_groups[0]) { | |
294 | log_err("fio_getaffinity: could not find processor group %d\n", | |
295 | current_groups[0]); | |
296 | goto err; | |
297 | } | |
298 | ||
299 | dprint(FD_PROCESS, "group_start_cpu=%d, group size=%u\n", | |
300 | group_start_cpu, group_size); | |
301 | if ((group_start_cpu + group_size) >= FIO_MAX_CPUS) { | |
302 | log_err("fio_getaffinity failed: current CPU affinity (group " | |
303 | "%d, group_start_cpu %d, group_size %d) extends " | |
304 | "beyond mask's highest CPU (%d)\n", group, | |
305 | group_start_cpu, group_size, FIO_MAX_CPUS); | |
306 | goto err; | |
307 | } | |
308 | ||
309 | fio_cpuset_init(mask); | |
310 | cpu_to_row_offset(group_start_cpu, &row, &offset); | |
311 | mask->row[row] = process_mask; | |
312 | mask->row[row] <<= offset; | |
313 | end = offset + group_size; | |
314 | if (end > FIO_CPU_MASK_STRIDE) { | |
315 | int needed; | |
316 | uint64_t needed_mask; | |
317 | ||
318 | needed = FIO_CPU_MASK_STRIDE - end; | |
319 | needed_mask = (uint64_t)-1 >> (FIO_CPU_MASK_STRIDE - needed); | |
320 | row++; | |
321 | mask->row[row] = process_mask; | |
322 | mask->row[row] >>= needed; | |
323 | mask->row[row] &= needed_mask; | |
324 | } | |
325 | ret = 0; | |
326 | ||
327 | err: | |
328 | if (handle) | |
329 | CloseHandle(handle); | |
330 | if (current_groups) | |
331 | free(current_groups); | |
332 | ||
333 | return ret; | |
334 | } | |
335 | ||
336 | void fio_cpu_clear(os_cpu_mask_t *mask, int cpu) | |
337 | { | |
338 | int row, offset; | |
339 | cpu_to_row_offset(cpu, &row, &offset); | |
340 | ||
341 | mask->row[row] &= ~(1ULL << offset); | |
342 | } | |
343 | ||
344 | void fio_cpu_set(os_cpu_mask_t *mask, int cpu) | |
345 | { | |
346 | int row, offset; | |
347 | cpu_to_row_offset(cpu, &row, &offset); | |
348 | ||
349 | mask->row[row] |= 1ULL << offset; | |
350 | } | |
351 | ||
352 | int fio_cpu_isset(os_cpu_mask_t *mask, int cpu) | |
353 | { | |
354 | int row, offset; | |
355 | cpu_to_row_offset(cpu, &row, &offset); | |
356 | ||
357 | return (mask->row[row] & (1ULL << offset)) != 0; | |
358 | } | |
359 | ||
360 | int fio_cpu_count(os_cpu_mask_t *mask) | |
361 | { | |
362 | int count = 0; | |
363 | ||
364 | for (int i = 0; i < FIO_CPU_MASK_ROWS; i++) | |
365 | count += hweight64(mask->row[i]); | |
366 | ||
367 | return count; | |
368 | } | |
369 | ||
370 | int fio_cpuset_exit(os_cpu_mask_t *mask) | |
371 | { | |
372 | return 0; | |
373 | } |