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