2 * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
5 * Copyright (c) 2008 Nokia Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _SYS_CPUSET_H_
33 #define _SYS_CPUSET_H_
36 #define CPU_SETSIZE MAXCPU
39 #define CPU_MAXSIZE 128
42 #define CPU_SETSIZE CPU_MAXSIZE
45 #define _NCPUBITS (sizeof(long) * NBBY) /* bits per mask */
46 #define _NCPUWORDS howmany(CPU_SETSIZE, _NCPUBITS)
48 typedef struct _cpuset {
49 long __bits[howmany(CPU_SETSIZE, _NCPUBITS)];
52 #define __cpuset_mask(n) ((long)1 << ((n) % _NCPUBITS))
53 #define CPU_CLR(n, p) ((p)->__bits[(n)/_NCPUBITS] &= ~__cpuset_mask(n))
54 #define CPU_COPY(f, t) (void)(*(t) = *(f))
55 #define CPU_ISSET(n, p) (((p)->__bits[(n)/_NCPUBITS] & __cpuset_mask(n)) != 0)
56 #define CPU_SET(n, p) ((p)->__bits[(n)/_NCPUBITS] |= __cpuset_mask(n))
57 #define CPU_ZERO(p) do { \
59 for (__i = 0; __i < _NCPUWORDS; __i++) \
60 (p)->__bits[__i] = 0; \
64 #define CPU_EMPTY(p) __extension__ ({ \
66 for (__i = 0; __i < _NCPUWORDS; __i++) \
67 if ((p)->__bits[__i]) \
72 /* Is c a subset of p. */
73 #define CPU_SUBSET(p, c) __extension__ ({ \
75 for (__i = 0; __i < _NCPUWORDS; __i++) \
76 if (((c)->__bits[__i] & \
77 (p)->__bits[__i]) != \
83 /* Are there any common bits between b & c? */
84 #define CPU_OVERLAP(p, c) __extension__ ({ \
86 for (__i = 0; __i < _NCPUWORDS; __i++) \
87 if (((c)->__bits[__i] & \
88 (p)->__bits[__i]) != 0) \
93 /* Compare two sets, returns 0 if equal 1 otherwise. */
94 #define CPU_CMP(p, c) __extension__ ({ \
96 for (__i = 0; __i < _NCPUWORDS; __i++) \
97 if (((c)->__bits[__i] != \
103 #define CPU_OR(d, s) do { \
105 for (__i = 0; __i < _NCPUWORDS; __i++) \
106 (d)->__bits[__i] |= (s)->__bits[__i]; \
109 #define CPU_AND(d, s) do { \
111 for (__i = 0; __i < _NCPUWORDS; __i++) \
112 (d)->__bits[__i] &= (s)->__bits[__i]; \
115 #define CPU_NAND(d, s) do { \
117 for (__i = 0; __i < _NCPUWORDS; __i++) \
118 (d)->__bits[__i] &= ~(s)->__bits[__i]; \
122 * Valid cpulevel_t values.
124 #define CPU_LEVEL_ROOT 1 /* All system cpus. */
125 #define CPU_LEVEL_CPUSET 2 /* Available cpus for which. */
126 #define CPU_LEVEL_WHICH 3 /* Actual mask/id for which. */
129 * Valid cpuwhich_t values.
131 #define CPU_WHICH_TID 1 /* Specifies a thread id. */
132 #define CPU_WHICH_PID 2 /* Specifies a process id. */
133 #define CPU_WHICH_CPUSET 3 /* Specifies a set id. */
135 #define CPU_WHICH_JAIL 5 /* Specifies a jail id. */
138 * Reserved cpuset identifiers.
140 #define CPUSET_INVALID -1
141 #define CPUSET_DEFAULT 0
144 LIST_HEAD(setlist, cpuset);
147 * cpusets encapsulate cpu binding information for one or more threads.
149 * a - Accessed with atomics.
150 * s - Set at creation, never modified. Only a ref required to read.
151 * c - Locked internally by a cpuset lock.
153 * The bitmask is only modified while holding the cpuset lock. It may be
154 * read while only a reference is held but the consumer must be prepared
155 * to deal with inconsistent results.
158 cpuset_t cs_mask; /* bitmask of valid cpus. */
159 volatile u_int cs_ref; /* (a) Reference count. */
160 int cs_flags; /* (s) Flags from below. */
161 cpusetid_t cs_id; /* (s) Id or INVALID. */
162 struct cpuset *cs_parent; /* (s) Pointer to our parent. */
163 LIST_ENTRY(cpuset) cs_link; /* (c) All identified sets. */
164 LIST_ENTRY(cpuset) cs_siblings; /* (c) Sibling set link. */
165 struct setlist cs_children; /* (c) List of children. */
168 #define CPU_SET_ROOT 0x0001 /* Set is a root set. */
169 #define CPU_SET_RDONLY 0x0002 /* No modification allowed. */
171 extern cpuset_t *cpuset_root;
175 struct cpuset *cpuset_thread0(void);
176 struct cpuset *cpuset_ref(struct cpuset *);
177 void cpuset_rel(struct cpuset *);
178 int cpuset_setthread(lwpid_t id, cpuset_t *);
179 int cpuset_create_root(struct thread *, struct cpuset **);
180 int cpuset_setproc_update_set(struct proc *, struct cpuset *);
184 int cpuset(cpusetid_t *);
185 int cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
186 int cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
187 int cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t *);
188 int cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const cpuset_t *);
191 #endif /* !_SYS_CPUSET_H_ */