Hardware Locality (hwloc)  PR-737-20250925.0822.gite8f69c77f
glibc-sched.h
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright © 2009 CNRS
4  * Copyright © 2009-2023 Inria. All rights reserved.
5  * Copyright © 2009-2011 Université Bordeaux
6  * Copyright © 2011 Cisco Systems, Inc. All rights reserved.
7  * See COPYING in top-level directory.
8  */
9 
18 #ifndef HWLOC_GLIBC_SCHED_H
19 #define HWLOC_GLIBC_SCHED_H
20 
21 #include "hwloc.h"
22 #include "hwloc/helper.h"
23 
24 #include <assert.h>
25 
26 #if !defined _GNU_SOURCE || (!defined _SCHED_H && !defined _SCHED_H_) || (!defined CPU_SETSIZE && !defined sched_priority)
27 #error Please make sure to include sched.h before including glibc-sched.h, and define _GNU_SOURCE before any inclusion of sched.h
28 #endif
29 
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 
36 #ifdef HWLOC_HAVE_CPU_SET
37 
38 
59 static __hwloc_inline int
61  cpu_set_t *schedset, size_t schedsetsize)
62 {
63 #ifdef CPU_ZERO_S
64  unsigned cpu;
65  CPU_ZERO_S(schedsetsize, schedset);
66  hwloc_bitmap_foreach_begin(cpu, hwlocset)
67  CPU_SET_S(cpu, schedsetsize, schedset);
69 #else /* !CPU_ZERO_S */
70  unsigned cpu;
71  CPU_ZERO(schedset);
72  assert(schedsetsize == sizeof(cpu_set_t));
73  hwloc_bitmap_foreach_begin(cpu, hwlocset)
74  CPU_SET(cpu, schedset);
76 #endif /* !CPU_ZERO_S */
77  return 0;
78 }
79 
90 static __hwloc_inline int
91 hwloc_cpuset_from_glibc_sched_affinity(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_cpuset_t hwlocset,
92  const cpu_set_t *schedset, size_t schedsetsize)
93 {
94  int cpu;
95 #ifdef CPU_ZERO_S
96  int count;
97 #endif
98  hwloc_bitmap_zero(hwlocset);
99 #ifdef CPU_ZERO_S
100  count = CPU_COUNT_S(schedsetsize, schedset);
101  cpu = 0;
102  while (count) {
103  if (CPU_ISSET_S(cpu, schedsetsize, schedset)) {
104  if (hwloc_bitmap_set(hwlocset, cpu) < 0)
105  return -1;
106  count--;
107  }
108  cpu++;
109  }
110 #else /* !CPU_ZERO_S */
111  /* sched.h does not support dynamic cpu_set_t (introduced in glibc 2.7),
112  * assume we have a very old interface without CPU_COUNT (added in 2.6)
113  */
114  assert(schedsetsize == sizeof(cpu_set_t));
115  for(cpu=0; cpu<CPU_SETSIZE; cpu++)
116  if (CPU_ISSET(cpu, schedset))
117  if (hwloc_bitmap_set(hwlocset, cpu) < 0)
118  return -1;
119 #endif /* !CPU_ZERO_S */
120  return 0;
121 }
122 
126 #endif /* CPU_SET */
127 
128 
129 #ifdef __cplusplus
130 } /* extern "C" */
131 #endif
132 
133 
134 #endif /* HWLOC_GLIBC_SCHED_H */
hwloc_const_cpuset_t
hwloc_const_bitmap_t hwloc_const_cpuset_t
A non-modifiable hwloc_cpuset_t.
Definition: hwloc.h:164
hwloc_topology_t
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:778
hwloc_cpuset_from_glibc_sched_affinity
int hwloc_cpuset_from_glibc_sched_affinity(hwloc_topology_t topology, hwloc_cpuset_t hwlocset, const cpu_set_t *schedset, size_t schedsetsize)
Convert glibc sched affinity CPU set schedset into hwloc CPU set.
Definition: glibc-sched.h:91
hwloc_bitmap_foreach_end
#define hwloc_bitmap_foreach_end()
End of loop macro iterating on a bitmap.
Definition: bitmap.h:457
hwloc_bitmap_foreach_begin
#define hwloc_bitmap_foreach_begin(id, bitmap)
Loop macro iterating on bitmap bitmap.
Definition: bitmap.h:443
hwloc_cpuset_t
hwloc_bitmap_t hwloc_cpuset_t
A CPU set is a bitmap whose bits are set according to CPU physical OS indexes.
Definition: hwloc.h:162
hwloc_bitmap_zero
void hwloc_bitmap_zero(hwloc_bitmap_t bitmap)
Empty the bitmap bitmap.
hwloc_bitmap_set
int hwloc_bitmap_set(hwloc_bitmap_t bitmap, unsigned id)
Add index id in bitmap bitmap.
hwloc_cpuset_to_glibc_sched_affinity
int hwloc_cpuset_to_glibc_sched_affinity(hwloc_topology_t topology, hwloc_const_cpuset_t hwlocset, cpu_set_t *schedset, size_t schedsetsize)
Convert hwloc CPU set toposet into glibc sched affinity CPU set schedset.
Definition: glibc-sched.h:60