This source file includes following definitions.
- hwloc_freebsd_bsd2hwloc
- hwloc_freebsd_hwloc2bsd
- hwloc_freebsd_set_sth_affinity
- hwloc_freebsd_get_sth_affinity
- hwloc_freebsd_set_thisproc_cpubind
- hwloc_freebsd_get_thisproc_cpubind
- hwloc_freebsd_set_thisthread_cpubind
- hwloc_freebsd_get_thisthread_cpubind
- hwloc_freebsd_set_proc_cpubind
- hwloc_freebsd_get_proc_cpubind
- hwloc_freebsd_set_thread_cpubind
- hwloc_freebsd_get_thread_cpubind
- hwloc_freebsd_node_meminfo_info
- hwloc_look_freebsd
- hwloc_set_freebsd_hooks
- hwloc_freebsd_component_instantiate
1
2
3
4
5
6
7
8
9 #include <private/autogen/config.h>
10
11 #include <sys/types.h>
12 #include <stdlib.h>
13 #include <inttypes.h>
14 #include <sys/param.h>
15 #include <pthread.h>
16 #ifdef HAVE_PTHREAD_NP_H
17 #include <pthread_np.h>
18 #endif
19 #ifdef HAVE_SYS_CPUSET_H
20 #include <sys/cpuset.h>
21 #endif
22 #ifdef HAVE_SYS_SYSCTL_H
23 #include <sys/sysctl.h>
24 #endif
25
26 #include <hwloc.h>
27 #include <private/private.h>
28 #include <private/debug.h>
29
30 #if defined(HAVE_SYS_CPUSET_H) && defined(HAVE_CPUSET_SETAFFINITY)
31 static void
32 hwloc_freebsd_bsd2hwloc(hwloc_bitmap_t hwloc_cpuset, const cpuset_t *cset)
33 {
34 unsigned cpu;
35 hwloc_bitmap_zero(hwloc_cpuset);
36 for (cpu = 0; cpu < CPU_SETSIZE; cpu++)
37 if (CPU_ISSET(cpu, cset))
38 hwloc_bitmap_set(hwloc_cpuset, cpu);
39 }
40
41 static void
42 hwloc_freebsd_hwloc2bsd(hwloc_const_bitmap_t hwloc_cpuset, cpuset_t *cset)
43 {
44 unsigned cpu;
45 CPU_ZERO(cset);
46 for (cpu = 0; cpu < CPU_SETSIZE; cpu++)
47 if (hwloc_bitmap_isset(hwloc_cpuset, cpu))
48 CPU_SET(cpu, cset);
49 }
50
51 static int
52 hwloc_freebsd_set_sth_affinity(hwloc_topology_t topology __hwloc_attribute_unused, cpulevel_t level, cpuwhich_t which, id_t id, hwloc_const_bitmap_t hwloc_cpuset, int flags __hwloc_attribute_unused)
53 {
54 cpuset_t cset;
55
56 hwloc_freebsd_hwloc2bsd(hwloc_cpuset, &cset);
57
58 if (cpuset_setaffinity(level, which, id, sizeof(cset), &cset))
59 return -1;
60
61 return 0;
62 }
63
64 static int
65 hwloc_freebsd_get_sth_affinity(hwloc_topology_t topology __hwloc_attribute_unused, cpulevel_t level, cpuwhich_t which, id_t id, hwloc_bitmap_t hwloc_cpuset, int flags __hwloc_attribute_unused)
66 {
67 cpuset_t cset;
68
69 if (cpuset_getaffinity(level, which, id, sizeof(cset), &cset))
70 return -1;
71
72 hwloc_freebsd_bsd2hwloc(hwloc_cpuset, &cset);
73 return 0;
74 }
75
76 static int
77 hwloc_freebsd_set_thisproc_cpubind(hwloc_topology_t topology, hwloc_const_bitmap_t hwloc_cpuset, int flags)
78 {
79 return hwloc_freebsd_set_sth_affinity(topology, CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, hwloc_cpuset, flags);
80 }
81
82 static int
83 hwloc_freebsd_get_thisproc_cpubind(hwloc_topology_t topology, hwloc_bitmap_t hwloc_cpuset, int flags)
84 {
85 return hwloc_freebsd_get_sth_affinity(topology, CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, hwloc_cpuset, flags);
86 }
87
88 static int
89 hwloc_freebsd_set_thisthread_cpubind(hwloc_topology_t topology, hwloc_const_bitmap_t hwloc_cpuset, int flags)
90 {
91 return hwloc_freebsd_set_sth_affinity(topology, CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, hwloc_cpuset, flags);
92 }
93
94 static int
95 hwloc_freebsd_get_thisthread_cpubind(hwloc_topology_t topology, hwloc_bitmap_t hwloc_cpuset, int flags)
96 {
97 return hwloc_freebsd_get_sth_affinity(topology, CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, hwloc_cpuset, flags);
98 }
99
100 static int
101 hwloc_freebsd_set_proc_cpubind(hwloc_topology_t topology, hwloc_pid_t pid, hwloc_const_bitmap_t hwloc_cpuset, int flags)
102 {
103 return hwloc_freebsd_set_sth_affinity(topology, CPU_LEVEL_WHICH, CPU_WHICH_PID, pid, hwloc_cpuset, flags);
104 }
105
106 static int
107 hwloc_freebsd_get_proc_cpubind(hwloc_topology_t topology, hwloc_pid_t pid, hwloc_bitmap_t hwloc_cpuset, int flags)
108 {
109 return hwloc_freebsd_get_sth_affinity(topology, CPU_LEVEL_WHICH, CPU_WHICH_PID, pid, hwloc_cpuset, flags);
110 }
111
112 #ifdef hwloc_thread_t
113
114 #if HAVE_DECL_PTHREAD_SETAFFINITY_NP
115 #pragma weak pthread_setaffinity_np
116 static int
117 hwloc_freebsd_set_thread_cpubind(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_thread_t tid, hwloc_const_bitmap_t hwloc_cpuset, int flags __hwloc_attribute_unused)
118 {
119 int err;
120 cpuset_t cset;
121
122 if (!pthread_setaffinity_np) {
123 errno = ENOSYS;
124 return -1;
125 }
126
127 hwloc_freebsd_hwloc2bsd(hwloc_cpuset, &cset);
128
129 err = pthread_setaffinity_np(tid, sizeof(cset), &cset);
130
131 if (err) {
132 errno = err;
133 return -1;
134 }
135
136 return 0;
137 }
138 #endif
139
140 #if HAVE_DECL_PTHREAD_GETAFFINITY_NP
141 #pragma weak pthread_getaffinity_np
142 static int
143 hwloc_freebsd_get_thread_cpubind(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_thread_t tid, hwloc_bitmap_t hwloc_cpuset, int flags __hwloc_attribute_unused)
144 {
145 int err;
146 cpuset_t cset;
147
148 if (!pthread_getaffinity_np) {
149 errno = ENOSYS;
150 return -1;
151 }
152
153 err = pthread_getaffinity_np(tid, sizeof(cset), &cset);
154
155 if (err) {
156 errno = err;
157 return -1;
158 }
159
160 hwloc_freebsd_bsd2hwloc(hwloc_cpuset, &cset);
161 return 0;
162 }
163 #endif
164 #endif
165 #endif
166
167 #if (defined HAVE_SYSCTL) && (defined HAVE_SYS_SYSCTL_H)
168 static void
169 hwloc_freebsd_node_meminfo_info(struct hwloc_topology *topology)
170 {
171 int mib[2] = { CTL_HW, HW_PHYSMEM };
172 unsigned long physmem;
173 size_t len = sizeof(physmem);
174 sysctl(mib, 2, &physmem, &len, NULL, 0);
175 topology->machine_memory.local_memory = physmem;
176
177
178 }
179 #endif
180
181 static int
182 hwloc_look_freebsd(struct hwloc_backend *backend)
183 {
184 struct hwloc_topology *topology = backend->topology;
185
186 if (!topology->levels[0][0]->cpuset) {
187
188 int nbprocs = hwloc_fallback_nbprocessors(topology);
189 if (nbprocs >= 1)
190 topology->support.discovery->pu = 1;
191 else
192 nbprocs = 1;
193 hwloc_alloc_root_sets(topology->levels[0][0]);
194 hwloc_setup_pu_level(topology, nbprocs);
195 }
196
197
198 #if (defined HAVE_SYSCTL) && (defined HAVE_SYS_SYSCTL_H)
199 hwloc_freebsd_node_meminfo_info(topology);
200 #endif
201 hwloc_obj_add_info(topology->levels[0][0], "Backend", "FreeBSD");
202 hwloc_add_uname_info(topology, NULL);
203 return 0;
204 }
205
206 void
207 hwloc_set_freebsd_hooks(struct hwloc_binding_hooks *hooks __hwloc_attribute_unused,
208 struct hwloc_topology_support *support __hwloc_attribute_unused)
209 {
210 #if defined(HAVE_SYS_CPUSET_H) && defined(HAVE_CPUSET_SETAFFINITY)
211 hooks->set_thisproc_cpubind = hwloc_freebsd_set_thisproc_cpubind;
212 hooks->get_thisproc_cpubind = hwloc_freebsd_get_thisproc_cpubind;
213 hooks->set_thisthread_cpubind = hwloc_freebsd_set_thisthread_cpubind;
214 hooks->get_thisthread_cpubind = hwloc_freebsd_get_thisthread_cpubind;
215 hooks->set_proc_cpubind = hwloc_freebsd_set_proc_cpubind;
216 hooks->get_proc_cpubind = hwloc_freebsd_get_proc_cpubind;
217 #ifdef hwloc_thread_t
218 #if HAVE_DECL_PTHREAD_SETAFFINITY_NP
219 hooks->set_thread_cpubind = hwloc_freebsd_set_thread_cpubind;
220 #endif
221 #if HAVE_DECL_PTHREAD_GETAFFINITY_NP
222 hooks->get_thread_cpubind = hwloc_freebsd_get_thread_cpubind;
223 #endif
224 #endif
225 #endif
226
227 }
228
229 static struct hwloc_backend *
230 hwloc_freebsd_component_instantiate(struct hwloc_disc_component *component,
231 const void *_data1 __hwloc_attribute_unused,
232 const void *_data2 __hwloc_attribute_unused,
233 const void *_data3 __hwloc_attribute_unused)
234 {
235 struct hwloc_backend *backend;
236 backend = hwloc_backend_alloc(component);
237 if (!backend)
238 return NULL;
239 backend->discover = hwloc_look_freebsd;
240 return backend;
241 }
242
243 static struct hwloc_disc_component hwloc_freebsd_disc_component = {
244 HWLOC_DISC_COMPONENT_TYPE_CPU,
245 "freebsd",
246 HWLOC_DISC_COMPONENT_TYPE_GLOBAL,
247 hwloc_freebsd_component_instantiate,
248 50,
249 1,
250 NULL
251 };
252
253 const struct hwloc_component hwloc_freebsd_component = {
254 HWLOC_COMPONENT_ABI,
255 NULL, NULL,
256 HWLOC_COMPONENT_TYPE_DISC,
257 0,
258 &hwloc_freebsd_disc_component
259 };