xref: /freebsd/sys/sys/cpuset.h (revision 9274eb16e5da1bd048237cd82366c7db9162aafe)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008,	Jeffrey Roberson <jeff@freebsd.org>
5  * All rights reserved.
6  *
7  * Copyright (c) 2008 Nokia Corporation
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice unmodified, this list of conditions, and the following
15  *    disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _SYS_CPUSET_H_
33 #define	_SYS_CPUSET_H_
34 
35 #include <sys/_cpuset.h>
36 
37 #include <sys/_bitset.h>
38 #include <sys/bitset.h>
39 
40 #define	_NCPUBITS	_BITSET_BITS
41 #define	_NCPUWORDS	__bitset_words(CPU_SETSIZE)
42 
43 #define	CPUSETBUFSIZ	((2 + sizeof(long) * 2) * _NCPUWORDS)
44 
45 #define	CPU_CLR(n, p)			__BIT_CLR(CPU_SETSIZE, n, p)
46 #define	CPU_COPY(f, t)			__BIT_COPY(CPU_SETSIZE, f, t)
47 #define	CPU_ISSET(n, p)			__BIT_ISSET(CPU_SETSIZE, n, p)
48 #define	CPU_SET(n, p)			__BIT_SET(CPU_SETSIZE, n, p)
49 #define	CPU_ZERO(p) 			__BIT_ZERO(CPU_SETSIZE, p)
50 #define	CPU_FILL(p) 			__BIT_FILL(CPU_SETSIZE, p)
51 #define	CPU_SETOF(n, p)			__BIT_SETOF(CPU_SETSIZE, n, p)
52 #define	CPU_EQUAL(p, c)			(__BIT_CMP(CPU_SETSIZE, p, c) == 0)
53 #define	CPU_EMPTY(p)			__BIT_EMPTY(CPU_SETSIZE, p)
54 #define	CPU_ISFULLSET(p)		__BIT_ISFULLSET(CPU_SETSIZE, p)
55 #define	CPU_SUBSET(p, c)		__BIT_SUBSET(CPU_SETSIZE, p, c)
56 #define	CPU_OVERLAP(p, c)		__BIT_OVERLAP(CPU_SETSIZE, p, c)
57 #define	CPU_CMP(p, c)			__BIT_CMP(CPU_SETSIZE, p, c)
58 #define	CPU_OR(d, s1, s2)		__BIT_OR2(CPU_SETSIZE, d, s1, s2)
59 #define	CPU_ORNOT(d, s1, s2)		__BIT_ORNOT2(CPU_SETSIZE, d, s1, s2)
60 #define	CPU_AND(d, s1, s2)		__BIT_AND2(CPU_SETSIZE, d, s1, s2)
61 #define	CPU_ANDNOT(d, s1, s2)		__BIT_ANDNOT2(CPU_SETSIZE, d, s1, s2)
62 #define	CPU_XOR(d, s1, s2)		__BIT_XOR2(CPU_SETSIZE, d, s1, s2)
63 #define	CPU_CLR_ATOMIC(n, p)		__BIT_CLR_ATOMIC(CPU_SETSIZE, n, p)
64 #define	CPU_TEST_CLR_ATOMIC(n, p)	__BIT_TEST_CLR_ATOMIC(CPU_SETSIZE, n, p)
65 #define	CPU_SET_ATOMIC(n, p)		__BIT_SET_ATOMIC(CPU_SETSIZE, n, p)
66 #define	CPU_SET_ATOMIC_ACQ(n, p)	__BIT_SET_ATOMIC_ACQ(CPU_SETSIZE, n, p)
67 #define	CPU_TEST_SET_ATOMIC(n, p)	__BIT_TEST_SET_ATOMIC(CPU_SETSIZE, n, p)
68 #define	CPU_AND_ATOMIC(n, p)		__BIT_AND_ATOMIC(CPU_SETSIZE, n, p)
69 #define	CPU_OR_ATOMIC(d, s)		__BIT_OR_ATOMIC(CPU_SETSIZE, d, s)
70 #define	CPU_COPY_STORE_REL(f, t)	__BIT_COPY_STORE_REL(CPU_SETSIZE, f, t)
71 #define	CPU_FFS(p)			__BIT_FFS(CPU_SETSIZE, p)
72 #define	CPU_FLS(p)			__BIT_FLS(CPU_SETSIZE, p)
73 #define	CPU_FOREACH_ISSET(i, p)		__BIT_FOREACH_ISSET(CPU_SETSIZE, i, p)
74 #define	CPU_FOREACH_ISCLR(i, p)		__BIT_FOREACH_ISCLR(CPU_SETSIZE, i, p)
75 #define	CPU_COUNT(p)			((int)__BIT_COUNT(CPU_SETSIZE, p))
76 #define	CPUSET_FSET			__BITSET_FSET(_NCPUWORDS)
77 #define	CPUSET_T_INITIALIZER(x)		__BITSET_T_INITIALIZER(x)
78 
79 #define CPU_ALLOC_SIZE(_s)		__BITSET_SIZE(_s)
80 #define CPU_ALLOC(_s)			__cpuset_alloc(_s)
81 #define CPU_FREE(p)			__cpuset_free(p)
82 
83 #define CPU_ISSET_S(n, _s, p)		__BIT_ISSET((_s) * 8, n, p)
84 #define CPU_SET_S(n, _s, p)		__BIT_SET((_s) * 8, n, p)
85 #define CPU_CLR_S(n, _s, p)		__BIT_CLR((_s) * 8, n, p)
86 #define CPU_ZERO_S(_s, p)		__BIT_ZERO((_s) * 8, p)
87 
88 #define	CPU_OR_S(_s, d, s1, s2)		__BIT_OR2((_s) * 8, d, s1, s2)
89 #define	CPU_AND_S(_s, d, s1, s2)	__BIT_AND2((_s) * 8, d, s1, s2)
90 #define	CPU_XOR_S(_s, d, s1, s2)	__BIT_XOR2((_s) * 8, d, s1, s2)
91 
92 #define	CPU_COUNT_S(_s, p)		((int)__BIT_COUNT((_s) * 8, p))
93 #define	CPU_EQUAL_S(_s, p, c)		(__BIT_CMP((_s) * 8, p, c) == 0)
94 
95 /*
96  * Valid cpulevel_t values.
97  */
98 #define	CPU_LEVEL_ROOT		1	/* All system cpus. */
99 #define	CPU_LEVEL_CPUSET	2	/* Available cpus for which. */
100 #define	CPU_LEVEL_WHICH		3	/* Actual mask/id for which. */
101 
102 /*
103  * Valid cpuwhich_t values.
104  */
105 #define	CPU_WHICH_TID		1	/* Specifies a thread id. */
106 #define	CPU_WHICH_PID		2	/* Specifies a process id. */
107 #define	CPU_WHICH_CPUSET	3	/* Specifies a set id. */
108 #define	CPU_WHICH_IRQ		4	/* Specifies an irq #. */
109 #define	CPU_WHICH_JAIL		5	/* Specifies a jail id. */
110 #define	CPU_WHICH_DOMAIN	6	/* Specifies a NUMA domain id. */
111 #define	CPU_WHICH_INTRHANDLER	7	/* Specifies an irq # (not ithread). */
112 #define	CPU_WHICH_ITHREAD	8	/* Specifies an irq's ithread. */
113 #define	CPU_WHICH_TIDPID	9	/* Specifies a process or thread id. */
114 
115 /*
116  * Reserved cpuset identifiers.
117  */
118 #define	CPUSET_INVALID	-1
119 #define	CPUSET_DEFAULT	0
120 
121 #ifdef _KERNEL
122 #include <sys/queue.h>
123 
124 LIST_HEAD(setlist, cpuset);
125 extern u_int cpusetsizemin;
126 
127 /*
128  * cpusets encapsulate cpu binding information for one or more threads.
129  *
130  * 	a - Accessed with atomics.
131  *	s - Set at creation, never modified.  Only a ref required to read.
132  *	c - Locked internally by a cpuset lock.
133  *
134  * The bitmask is only modified while holding the cpuset lock.  It may be
135  * read while only a reference is held but the consumer must be prepared
136  * to deal with inconsistent results.
137  */
138 struct cpuset {
139 	volatile u_int		cs_ref;		/* (a) Reference count. */
140 	int			cs_flags;	/* (s) Flags from below. */
141 	LIST_ENTRY(cpuset)	cs_link;	/* (c) All identified sets. */
142 	LIST_ENTRY(cpuset)	cs_siblings;	/* (c) Sibling set link. */
143 	struct setlist		cs_children;	/* (c) List of children. */
144 	struct domainset	*cs_domain;	/* (c) NUMA policy. */
145 	cpusetid_t		cs_id;		/* (s) Id or INVALID. */
146 	struct cpuset		*cs_parent;	/* (s) Pointer to our parent. */
147 	cpuset_t		cs_mask;	/* bitmask of valid cpus. */
148 };
149 
150 #define CPU_SET_ROOT    0x0001  /* Set is a root set. */
151 #define CPU_SET_RDONLY  0x0002  /* No modification allowed. */
152 
153 extern cpuset_t *cpuset_root;
154 struct prison;
155 struct proc;
156 struct thread;
157 
158 /*
159  * Callbacks for copying in/out a cpuset or domainset.  Used for alternate
160  * ABIs, like compat32.
161  */
162 struct cpuset_copy_cb {
163 	int (*cpuset_copyin)(const void *, void *, size_t);
164 	int (*cpuset_copyout)(const void *, void *, size_t);
165 };
166 
167 struct cpuset *cpuset_thread0(void);
168 struct cpuset *cpuset_ref(struct cpuset *);
169 void	cpuset_rel(struct cpuset *);
170 int	cpuset_setthread(lwpid_t id, cpuset_t *);
171 int	cpuset_setithread(lwpid_t id, int cpu);
172 int	cpuset_create_root(struct prison *, struct cpuset **);
173 int	cpuset_setproc_update_set(struct proc *, struct cpuset *);
174 int	cpuset_which(cpuwhich_t, id_t, struct proc **,
175 	    struct thread **, struct cpuset **);
176 void	cpuset_kernthread(struct thread *);
177 
178 char	*cpusetobj_strprint(char *, const cpuset_t *);
179 int	cpusetobj_strscan(cpuset_t *, const char *);
180 #ifdef DDB
181 void	ddb_display_cpuset(const cpuset_t *);
182 #endif
183 
184 #else
185 __BEGIN_DECLS
186 int	cpuset(cpusetid_t *);
187 int	cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
188 int	cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
189 int	cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t *);
190 int	cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const cpuset_t *);
191 __END_DECLS
192 #endif
193 #endif /* !_SYS_CPUSET_H_ */
194