xref: /freebsd/sys/sys/cpuset.h (revision 19261079)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
32  */
33 
34 #ifndef _SYS_CPUSET_H_
35 #define	_SYS_CPUSET_H_
36 
37 #include <sys/_cpuset.h>
38 
39 #include <sys/bitset.h>
40 
41 #define	_NCPUBITS	_BITSET_BITS
42 #define	_NCPUWORDS	__bitset_words(CPU_SETSIZE)
43 
44 #define	CPUSETBUFSIZ	((2 + sizeof(long) * 2) * _NCPUWORDS)
45 
46 #define	CPU_CLR(n, p)			BIT_CLR(CPU_SETSIZE, n, p)
47 #define	CPU_COPY(f, t)			BIT_COPY(CPU_SETSIZE, f, t)
48 #define	CPU_ISSET(n, p)			BIT_ISSET(CPU_SETSIZE, n, p)
49 #define	CPU_SET(n, p)			BIT_SET(CPU_SETSIZE, n, p)
50 #define	CPU_ZERO(p) 			BIT_ZERO(CPU_SETSIZE, p)
51 #define	CPU_FILL(p) 			BIT_FILL(CPU_SETSIZE, p)
52 #define	CPU_SETOF(n, p)			BIT_SETOF(CPU_SETSIZE, n, p)
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, s)			BIT_OR(CPU_SETSIZE, d, s)
59 #define	CPU_AND(d, s)			BIT_AND(CPU_SETSIZE, d, s)
60 #define	CPU_ANDNOT(d, s)		BIT_ANDNOT(CPU_SETSIZE, d, s)
61 #define	CPU_CLR_ATOMIC(n, p)		BIT_CLR_ATOMIC(CPU_SETSIZE, n, p)
62 #define	CPU_SET_ATOMIC(n, p)		BIT_SET_ATOMIC(CPU_SETSIZE, n, p)
63 #define	CPU_SET_ATOMIC_ACQ(n, p)	BIT_SET_ATOMIC_ACQ(CPU_SETSIZE, n, p)
64 #define	CPU_AND_ATOMIC(n, p)		BIT_AND_ATOMIC(CPU_SETSIZE, n, p)
65 #define	CPU_OR_ATOMIC(d, s)		BIT_OR_ATOMIC(CPU_SETSIZE, d, s)
66 #define	CPU_COPY_STORE_REL(f, t)	BIT_COPY_STORE_REL(CPU_SETSIZE, f, t)
67 #define	CPU_FFS(p)			BIT_FFS(CPU_SETSIZE, p)
68 #define	CPU_FLS(p)			BIT_FLS(CPU_SETSIZE, p)
69 #define	CPU_COUNT(p)			((int)BIT_COUNT(CPU_SETSIZE, p))
70 #define	CPUSET_FSET			BITSET_FSET(_NCPUWORDS)
71 #define	CPUSET_T_INITIALIZER		BITSET_T_INITIALIZER
72 
73 /*
74  * Valid cpulevel_t values.
75  */
76 #define	CPU_LEVEL_ROOT		1	/* All system cpus. */
77 #define	CPU_LEVEL_CPUSET	2	/* Available cpus for which. */
78 #define	CPU_LEVEL_WHICH		3	/* Actual mask/id for which. */
79 
80 /*
81  * Valid cpuwhich_t values.
82  */
83 #define	CPU_WHICH_TID		1	/* Specifies a thread id. */
84 #define	CPU_WHICH_PID		2	/* Specifies a process id. */
85 #define	CPU_WHICH_CPUSET	3	/* Specifies a set id. */
86 #define	CPU_WHICH_IRQ		4	/* Specifies an irq #. */
87 #define	CPU_WHICH_JAIL		5	/* Specifies a jail id. */
88 #define	CPU_WHICH_DOMAIN	6	/* Specifies a NUMA domain id. */
89 #define	CPU_WHICH_INTRHANDLER	7	/* Specifies an irq # (not ithread). */
90 #define	CPU_WHICH_ITHREAD	8	/* Specifies an irq's ithread. */
91 
92 /*
93  * Reserved cpuset identifiers.
94  */
95 #define	CPUSET_INVALID	-1
96 #define	CPUSET_DEFAULT	0
97 
98 #ifdef _KERNEL
99 #include <sys/queue.h>
100 
101 LIST_HEAD(setlist, cpuset);
102 
103 /*
104  * cpusets encapsulate cpu binding information for one or more threads.
105  *
106  * 	a - Accessed with atomics.
107  *	s - Set at creation, never modified.  Only a ref required to read.
108  *	c - Locked internally by a cpuset lock.
109  *
110  * The bitmask is only modified while holding the cpuset lock.  It may be
111  * read while only a reference is held but the consumer must be prepared
112  * to deal with inconsistent results.
113  */
114 struct cpuset {
115 	volatile u_int		cs_ref;		/* (a) Reference count. */
116 	int			cs_flags;	/* (s) Flags from below. */
117 	LIST_ENTRY(cpuset)	cs_link;	/* (c) All identified sets. */
118 	LIST_ENTRY(cpuset)	cs_siblings;	/* (c) Sibling set link. */
119 	struct setlist		cs_children;	/* (c) List of children. */
120 	struct domainset	*cs_domain;	/* (c) NUMA policy. */
121 	cpusetid_t		cs_id;		/* (s) Id or INVALID. */
122 	struct cpuset		*cs_parent;	/* (s) Pointer to our parent. */
123 	cpuset_t		cs_mask;	/* bitmask of valid cpus. */
124 };
125 
126 #define CPU_SET_ROOT    0x0001  /* Set is a root set. */
127 #define CPU_SET_RDONLY  0x0002  /* No modification allowed. */
128 
129 extern cpuset_t *cpuset_root;
130 struct prison;
131 struct proc;
132 struct thread;
133 
134 struct cpuset *cpuset_thread0(void);
135 struct cpuset *cpuset_ref(struct cpuset *);
136 void	cpuset_rel(struct cpuset *);
137 int	cpuset_setthread(lwpid_t id, cpuset_t *);
138 int	cpuset_setithread(lwpid_t id, int cpu);
139 int	cpuset_create_root(struct prison *, struct cpuset **);
140 int	cpuset_setproc_update_set(struct proc *, struct cpuset *);
141 int	cpuset_which(cpuwhich_t, id_t, struct proc **,
142 	    struct thread **, struct cpuset **);
143 void	cpuset_kernthread(struct thread *);
144 
145 char	*cpusetobj_strprint(char *, const cpuset_t *);
146 int	cpusetobj_strscan(cpuset_t *, const char *);
147 #ifdef DDB
148 void	ddb_display_cpuset(const cpuset_t *);
149 #endif
150 
151 #else
152 __BEGIN_DECLS
153 int	cpuset(cpusetid_t *);
154 int	cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
155 int	cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
156 int	cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t *);
157 int	cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const cpuset_t *);
158 __END_DECLS
159 #endif
160 #endif /* !_SYS_CPUSET_H_ */
161