5 #ifdef CONFIG_WINDOWS_XP
6 int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
11 h = OpenThread(THREAD_QUERY_INFORMATION | THREAD_SET_INFORMATION, TRUE,
14 bSuccess = SetThreadAffinityMask(h, cpumask);
16 log_err("fio_setaffinity failed: failed to set thread affinity (pid %d, mask %.16llx)\n",
17 pid, (long long unsigned) cpumask);
21 log_err("fio_setaffinity failed: failed to get handle for pid %d\n",
25 return bSuccess ? 0 : -1;
28 int fio_getaffinity(int pid, os_cpu_mask_t *mask)
30 os_cpu_mask_t systemMask;
32 HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, TRUE, pid);
35 GetProcessAffinityMask(h, mask, &systemMask);
38 log_err("fio_getaffinity failed: failed to get handle for pid %d\n",
46 void fio_cpu_clear(os_cpu_mask_t *mask, int cpu)
48 *mask &= ~(1ULL << cpu);
51 void fio_cpu_set(os_cpu_mask_t *mask, int cpu)
56 int fio_cpu_isset(os_cpu_mask_t *mask, int cpu)
58 return (*mask & (1ULL << cpu)) != 0;
61 int fio_cpu_count(os_cpu_mask_t *mask)
63 return hweight64(*mask);
66 int fio_cpuset_init(os_cpu_mask_t *mask)
72 int fio_cpuset_exit(os_cpu_mask_t *mask)
76 #else /* CONFIG_WINDOWS_XP */
77 /* Return all processors regardless of processor group */
78 unsigned int cpus_online(void)
80 return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
83 static void print_mask(os_cpu_mask_t *cpumask)
85 for (int i = 0; i < FIO_CPU_MASK_ROWS; i++)
86 dprint(FD_PROCESS, "cpumask[%d]=%" PRIu64 "\n", i, cpumask->row[i]);
89 /* Return the index of the least significant set CPU in cpumask or -1 if no
91 int first_set_cpu(os_cpu_mask_t *cpumask)
93 int cpus_offset, mask_first_cpu, row;
98 while (mask_first_cpu < 0 && row < FIO_CPU_MASK_ROWS) {
101 row_first_cpu = __builtin_ffsll(cpumask->row[row]) - 1;
102 dprint(FD_PROCESS, "row_first_cpu=%d cpumask->row[%d]=%" PRIu64 "\n",
103 row_first_cpu, row, cpumask->row[row]);
104 if (row_first_cpu > -1) {
105 mask_first_cpu = cpus_offset + row_first_cpu;
106 dprint(FD_PROCESS, "first set cpu in mask is at index %d\n",
109 cpus_offset += FIO_CPU_MASK_STRIDE;
114 return mask_first_cpu;
117 /* Return the index of the most significant set CPU in cpumask or -1 if no
119 static int last_set_cpu(os_cpu_mask_t *cpumask)
121 int cpus_offset, mask_last_cpu, row;
123 cpus_offset = (FIO_CPU_MASK_ROWS - 1) * FIO_CPU_MASK_STRIDE;
124 row = FIO_CPU_MASK_ROWS - 1;
126 while (mask_last_cpu < 0 && row >= 0) {
129 if (cpumask->row[row] == 0)
132 uint64_t tmp = cpumask->row[row];
139 dprint(FD_PROCESS, "row_last_cpu=%d cpumask->row[%d]=%" PRIu64 "\n",
140 row_last_cpu, row, cpumask->row[row]);
141 if (row_last_cpu > -1) {
142 mask_last_cpu = cpus_offset + row_last_cpu;
143 dprint(FD_PROCESS, "last set cpu in mask is at index %d\n",
146 cpus_offset -= FIO_CPU_MASK_STRIDE;
151 return mask_last_cpu;
154 static int mask_to_group_mask(os_cpu_mask_t *cpumask, int *processor_group, uint64_t *affinity_mask)
156 WORD online_groups, group, group_size;
158 int cpus_offset, search_cpu, last_cpu, bit_offset, row, end;
159 uint64_t group_cpumask;
161 search_cpu = first_set_cpu(cpumask);
162 if (search_cpu < 0) {
163 log_info("CPU mask doesn't set any CPUs\n");
167 /* Find processor group first set CPU applies to */
168 online_groups = GetActiveProcessorGroupCount();
173 while (!found && group < online_groups) {
174 group_size = GetActiveProcessorCount(group);
175 dprint(FD_PROCESS, "group=%d group_start=%d group_size=%u search_cpu=%d\n",
176 group, cpus_offset, group_size, search_cpu);
177 if (cpus_offset + group_size > search_cpu)
180 cpus_offset += group_size;
186 log_err("CPU mask contains processor beyond last active processor index (%d)\n",
192 /* Check all the CPUs in the mask apply to ONLY that processor group */
193 last_cpu = last_set_cpu(cpumask);
194 if (last_cpu > (cpus_offset + group_size - 1)) {
195 log_info("CPU mask cannot bind CPUs (e.g. %d, %d) that are "
196 "in different processor groups\n", search_cpu,
202 /* Extract the current processor group mask from the cpumask */
203 row = cpus_offset / FIO_CPU_MASK_STRIDE;
204 bit_offset = cpus_offset % FIO_CPU_MASK_STRIDE;
205 group_cpumask = cpumask->row[row] >> bit_offset;
206 end = bit_offset + group_size;
207 if (end > FIO_CPU_MASK_STRIDE && (row + 1 < FIO_CPU_MASK_ROWS)) {
208 /* Some of the next row needs to be part of the mask */
209 int needed, needed_shift, needed_mask_shift;
210 uint64_t needed_mask;
212 needed = end - FIO_CPU_MASK_STRIDE;
213 needed_shift = FIO_CPU_MASK_STRIDE - bit_offset;
214 needed_mask_shift = FIO_CPU_MASK_STRIDE - needed;
215 needed_mask = (uint64_t)-1 >> needed_mask_shift;
217 "bit_offset=%d end=%d needed=%d needed_shift=%d needed_mask=%" PRIu64 "needed_mask_shift=%d\n",
218 bit_offset, end, needed, needed_shift, needed_mask,
220 group_cpumask |= (cpumask->row[row + 1] & needed_mask) << needed_shift;
222 group_cpumask &= (uint64_t)-1 >> (FIO_CPU_MASK_STRIDE - group_size);
224 /* Return group and mask */
225 dprint(FD_PROCESS, "Returning group=%d group_mask=%" PRIu64 "\n",
226 group, group_cpumask);
227 *processor_group = group;
228 *affinity_mask = group_cpumask;
233 int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
235 HANDLE handle = NULL;
237 uint64_t group_mask = 0;
238 GROUP_AFFINITY new_group_affinity;
242 if (mask_to_group_mask(&cpumask, &group, &group_mask) != 0)
245 handle = OpenThread(THREAD_QUERY_INFORMATION | THREAD_SET_INFORMATION,
247 if (handle == NULL) {
248 log_err("fio_setaffinity: failed to get handle for pid %d\n", pid);
252 /* Set group and mask.
253 * Note: if the GROUP_AFFINITY struct's Reserved members are not
254 * initialised to 0 then SetThreadGroupAffinity will fail with
255 * GetLastError() set to ERROR_INVALID_PARAMETER */
256 new_group_affinity.Mask = (KAFFINITY) group_mask;
257 new_group_affinity.Group = group;
258 new_group_affinity.Reserved[0] = 0;
259 new_group_affinity.Reserved[1] = 0;
260 new_group_affinity.Reserved[2] = 0;
261 if (SetThreadGroupAffinity(handle, &new_group_affinity, NULL) != 0)
264 log_err("fio_setaffinity: failed to set thread affinity (pid %d, group %d, mask %" PRIx64 ", GetLastError=%lu)\n",
265 pid, group, group_mask, GetLastError());
275 static void cpu_to_row_offset(int cpu, int *row, int *offset)
277 *row = cpu / FIO_CPU_MASK_STRIDE;
278 *offset = cpu << FIO_CPU_MASK_STRIDE * *row;
281 int fio_cpuset_init(os_cpu_mask_t *mask)
283 for (int i = 0; i < FIO_CPU_MASK_ROWS; i++)
289 * fio_getaffinity() should not be called once a fio_setaffinity() call has
290 * been made because fio_setaffinity() may put the process into multiple
293 int fio_getaffinity(int pid, os_cpu_mask_t *mask)
296 int row, offset, end, group, group_size, group_start_cpu;
297 DWORD_PTR process_mask, system_mask;
299 PUSHORT current_groups;
304 current_groups = NULL;
305 handle = OpenProcess(PROCESS_QUERY_INFORMATION, TRUE, pid);
306 if (handle == NULL) {
307 log_err("fio_getaffinity: failed to get handle for pid %d\n",
314 * GetProcessGroupAffinity() seems to expect more than the natural
315 * alignment for a USHORT from the area pointed to by current_groups so
316 * arrange for maximum alignment by allocating via malloc()
318 current_groups = malloc(group_count * sizeof(USHORT));
319 if (!current_groups) {
320 log_err("fio_getaffinity: malloc failed\n");
323 if (!GetProcessGroupAffinity(handle, &group_count, current_groups)) {
324 log_err("%s: failed to get single group affinity for pid %d (%lu)\n",
325 __func__, pid, GetLastError());
328 if (group_count > 1) {
329 log_err("%s: pid %d is associated with %d process groups\n",
330 __func__, pid, group_count);
333 if (!GetProcessAffinityMask(handle, &process_mask, &system_mask)) {
334 log_err("%s: GetProcessAffinityMask() failed for pid %d\n",
339 /* Convert group and group relative mask to full CPU mask */
340 online_groups = GetActiveProcessorGroupCount();
341 if (online_groups == 0) {
342 log_err("fio_getaffinity: error retrieving total processor groups\n");
349 dprint(FD_PROCESS, "current_groups=%d group_count=%d\n",
350 current_groups[0], group_count);
352 group_size = GetActiveProcessorCount(group);
353 if (group_size == 0) {
354 log_err("fio_getaffinity: error retrieving size of "
355 "processor group %d\n", group);
357 } else if (group >= current_groups[0] || group >= online_groups)
360 group_start_cpu += group_size;
365 if (group != current_groups[0]) {
366 log_err("fio_getaffinity: could not find processor group %d\n",
371 dprint(FD_PROCESS, "group_start_cpu=%d, group size=%u\n",
372 group_start_cpu, group_size);
373 if ((group_start_cpu + group_size) >= FIO_MAX_CPUS) {
374 log_err("fio_getaffinity failed: current CPU affinity (group "
375 "%d, group_start_cpu %d, group_size %d) extends "
376 "beyond mask's highest CPU (%d)\n", group,
377 group_start_cpu, group_size, FIO_MAX_CPUS);
381 fio_cpuset_init(mask);
382 cpu_to_row_offset(group_start_cpu, &row, &offset);
383 mask->row[row] = process_mask;
384 mask->row[row] <<= offset;
385 end = offset + group_size;
386 if (end > FIO_CPU_MASK_STRIDE) {
388 uint64_t needed_mask;
390 needed = FIO_CPU_MASK_STRIDE - end;
391 needed_mask = (uint64_t)-1 >> (FIO_CPU_MASK_STRIDE - needed);
393 mask->row[row] = process_mask;
394 mask->row[row] >>= needed;
395 mask->row[row] &= needed_mask;
403 free(current_groups);
408 void fio_cpu_clear(os_cpu_mask_t *mask, int cpu)
411 cpu_to_row_offset(cpu, &row, &offset);
413 mask->row[row] &= ~(1ULL << offset);
416 void fio_cpu_set(os_cpu_mask_t *mask, int cpu)
419 cpu_to_row_offset(cpu, &row, &offset);
421 mask->row[row] |= 1ULL << offset;
424 int fio_cpu_isset(os_cpu_mask_t *mask, int cpu)
427 cpu_to_row_offset(cpu, &row, &offset);
429 return (mask->row[row] & (1ULL << offset)) != 0;
432 int fio_cpu_count(os_cpu_mask_t *mask)
436 for (int i = 0; i < FIO_CPU_MASK_ROWS; i++)
437 count += hweight64(mask->row[i]);
442 int fio_cpuset_exit(os_cpu_mask_t *mask)
446 #endif /* CONFIG_WINDOWS_XP */