xref: /f-stack/tools/compat/include/sys/cpuset.h (revision 1eaf0ac3)
1*1eaf0ac3Slogwang /*-
2*1eaf0ac3Slogwang  * Copyright (c) 2008,	Jeffrey Roberson <[email protected]>
3*1eaf0ac3Slogwang  * All rights reserved.
4*1eaf0ac3Slogwang  *
5*1eaf0ac3Slogwang  * Copyright (c) 2008 Nokia Corporation
6*1eaf0ac3Slogwang  * All rights reserved.
7*1eaf0ac3Slogwang  *
8*1eaf0ac3Slogwang  * Redistribution and use in source and binary forms, with or without
9*1eaf0ac3Slogwang  * modification, are permitted provided that the following conditions
10*1eaf0ac3Slogwang  * are met:
11*1eaf0ac3Slogwang  * 1. Redistributions of source code must retain the above copyright
12*1eaf0ac3Slogwang  *    notice unmodified, this list of conditions, and the following
13*1eaf0ac3Slogwang  *    disclaimer.
14*1eaf0ac3Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
15*1eaf0ac3Slogwang  *    notice, this list of conditions and the following disclaimer in the
16*1eaf0ac3Slogwang  *    documentation and/or other materials provided with the distribution.
17*1eaf0ac3Slogwang  *
18*1eaf0ac3Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*1eaf0ac3Slogwang  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*1eaf0ac3Slogwang  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*1eaf0ac3Slogwang  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*1eaf0ac3Slogwang  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*1eaf0ac3Slogwang  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*1eaf0ac3Slogwang  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*1eaf0ac3Slogwang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*1eaf0ac3Slogwang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*1eaf0ac3Slogwang  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*1eaf0ac3Slogwang  *
29*1eaf0ac3Slogwang  * $FreeBSD$
30*1eaf0ac3Slogwang  */
31*1eaf0ac3Slogwang 
32*1eaf0ac3Slogwang #ifndef _SYS_CPUSET_H_
33*1eaf0ac3Slogwang #define	_SYS_CPUSET_H_
34*1eaf0ac3Slogwang 
35*1eaf0ac3Slogwang #include <sys/_cpuset.h>
36*1eaf0ac3Slogwang 
37*1eaf0ac3Slogwang #include <sys/bitset.h>
38*1eaf0ac3Slogwang 
39*1eaf0ac3Slogwang #define	_NCPUBITS	_BITSET_BITS
40*1eaf0ac3Slogwang #define	_NCPUWORDS	__bitset_words(CPU_SETSIZE)
41*1eaf0ac3Slogwang 
42*1eaf0ac3Slogwang #define	CPUSETBUFSIZ	((2 + sizeof(long) * 2) * _NCPUWORDS)
43*1eaf0ac3Slogwang 
44*1eaf0ac3Slogwang #define	CPU_CLR(n, p)			BIT_CLR(CPU_SETSIZE, n, p)
45*1eaf0ac3Slogwang #define	CPU_COPY(f, t)			BIT_COPY(CPU_SETSIZE, f, t)
46*1eaf0ac3Slogwang #define	CPU_ISSET(n, p)			BIT_ISSET(CPU_SETSIZE, n, p)
47*1eaf0ac3Slogwang #define	CPU_SET(n, p)			BIT_SET(CPU_SETSIZE, n, p)
48*1eaf0ac3Slogwang #define	CPU_ZERO(p) 			BIT_ZERO(CPU_SETSIZE, p)
49*1eaf0ac3Slogwang #define	CPU_FILL(p) 			BIT_FILL(CPU_SETSIZE, p)
50*1eaf0ac3Slogwang #define	CPU_SETOF(n, p)			BIT_SETOF(CPU_SETSIZE, n, p)
51*1eaf0ac3Slogwang #define	CPU_EMPTY(p)			BIT_EMPTY(CPU_SETSIZE, p)
52*1eaf0ac3Slogwang #define	CPU_ISFULLSET(p)		BIT_ISFULLSET(CPU_SETSIZE, p)
53*1eaf0ac3Slogwang #define	CPU_SUBSET(p, c)		BIT_SUBSET(CPU_SETSIZE, p, c)
54*1eaf0ac3Slogwang #define	CPU_OVERLAP(p, c)		BIT_OVERLAP(CPU_SETSIZE, p, c)
55*1eaf0ac3Slogwang #define	CPU_CMP(p, c)			BIT_CMP(CPU_SETSIZE, p, c)
56*1eaf0ac3Slogwang #define	CPU_OR(d, s)			BIT_OR(CPU_SETSIZE, d, s)
57*1eaf0ac3Slogwang #define	CPU_AND(d, s)			BIT_AND(CPU_SETSIZE, d, s)
58*1eaf0ac3Slogwang #define	CPU_NAND(d, s)			BIT_NAND(CPU_SETSIZE, d, s)
59*1eaf0ac3Slogwang #define	CPU_CLR_ATOMIC(n, p)		BIT_CLR_ATOMIC(CPU_SETSIZE, n, p)
60*1eaf0ac3Slogwang #define	CPU_SET_ATOMIC(n, p)		BIT_SET_ATOMIC(CPU_SETSIZE, n, p)
61*1eaf0ac3Slogwang #define	CPU_SET_ATOMIC_ACQ(n, p)	BIT_SET_ATOMIC_ACQ(CPU_SETSIZE, n, p)
62*1eaf0ac3Slogwang #define	CPU_AND_ATOMIC(n, p)		BIT_AND_ATOMIC(CPU_SETSIZE, n, p)
63*1eaf0ac3Slogwang #define	CPU_OR_ATOMIC(d, s)		BIT_OR_ATOMIC(CPU_SETSIZE, d, s)
64*1eaf0ac3Slogwang #define	CPU_COPY_STORE_REL(f, t)	BIT_COPY_STORE_REL(CPU_SETSIZE, f, t)
65*1eaf0ac3Slogwang #define	CPU_FFS(p)			BIT_FFS(CPU_SETSIZE, p)
66*1eaf0ac3Slogwang #define	CPU_COUNT(p)			BIT_COUNT(CPU_SETSIZE, p)
67*1eaf0ac3Slogwang #define	CPUSET_FSET			BITSET_FSET(_NCPUWORDS)
68*1eaf0ac3Slogwang #define	CPUSET_T_INITIALIZER		BITSET_T_INITIALIZER
69*1eaf0ac3Slogwang 
70*1eaf0ac3Slogwang /*
71*1eaf0ac3Slogwang  * Valid cpulevel_t values.
72*1eaf0ac3Slogwang  */
73*1eaf0ac3Slogwang #define	CPU_LEVEL_ROOT		1	/* All system cpus. */
74*1eaf0ac3Slogwang #define	CPU_LEVEL_CPUSET	2	/* Available cpus for which. */
75*1eaf0ac3Slogwang #define	CPU_LEVEL_WHICH		3	/* Actual mask/id for which. */
76*1eaf0ac3Slogwang 
77*1eaf0ac3Slogwang /*
78*1eaf0ac3Slogwang  * Valid cpuwhich_t values.
79*1eaf0ac3Slogwang  */
80*1eaf0ac3Slogwang #define	CPU_WHICH_TID		1	/* Specifies a thread id. */
81*1eaf0ac3Slogwang #define	CPU_WHICH_PID		2	/* Specifies a process id. */
82*1eaf0ac3Slogwang #define	CPU_WHICH_CPUSET	3	/* Specifies a set id. */
83*1eaf0ac3Slogwang #define	CPU_WHICH_IRQ		4	/* Specifies an irq #. */
84*1eaf0ac3Slogwang #define	CPU_WHICH_JAIL		5	/* Specifies a jail id. */
85*1eaf0ac3Slogwang #define	CPU_WHICH_DOMAIN	6	/* Specifies a NUMA domain id. */
86*1eaf0ac3Slogwang 
87*1eaf0ac3Slogwang /*
88*1eaf0ac3Slogwang  * Reserved cpuset identifiers.
89*1eaf0ac3Slogwang  */
90*1eaf0ac3Slogwang #define	CPUSET_INVALID	-1
91*1eaf0ac3Slogwang #define	CPUSET_DEFAULT	0
92*1eaf0ac3Slogwang 
93*1eaf0ac3Slogwang #ifdef _KERNEL
94*1eaf0ac3Slogwang #include <sys/queue.h>
95*1eaf0ac3Slogwang 
96*1eaf0ac3Slogwang LIST_HEAD(setlist, cpuset);
97*1eaf0ac3Slogwang 
98*1eaf0ac3Slogwang /*
99*1eaf0ac3Slogwang  * cpusets encapsulate cpu binding information for one or more threads.
100*1eaf0ac3Slogwang  *
101*1eaf0ac3Slogwang  * 	a - Accessed with atomics.
102*1eaf0ac3Slogwang  *	s - Set at creation, never modified.  Only a ref required to read.
103*1eaf0ac3Slogwang  *	c - Locked internally by a cpuset lock.
104*1eaf0ac3Slogwang  *
105*1eaf0ac3Slogwang  * The bitmask is only modified while holding the cpuset lock.  It may be
106*1eaf0ac3Slogwang  * read while only a reference is held but the consumer must be prepared
107*1eaf0ac3Slogwang  * to deal with inconsistent results.
108*1eaf0ac3Slogwang  */
109*1eaf0ac3Slogwang struct cpuset {
110*1eaf0ac3Slogwang 	cpuset_t		cs_mask;	/* bitmask of valid cpus. */
111*1eaf0ac3Slogwang 	volatile u_int		cs_ref;		/* (a) Reference count. */
112*1eaf0ac3Slogwang 	int			cs_flags;	/* (s) Flags from below. */
113*1eaf0ac3Slogwang 	cpusetid_t		cs_id;		/* (s) Id or INVALID. */
114*1eaf0ac3Slogwang 	struct cpuset		*cs_parent;	/* (s) Pointer to our parent. */
115*1eaf0ac3Slogwang 	LIST_ENTRY(cpuset)	cs_link;	/* (c) All identified sets. */
116*1eaf0ac3Slogwang 	LIST_ENTRY(cpuset)	cs_siblings;	/* (c) Sibling set link. */
117*1eaf0ac3Slogwang 	struct setlist		cs_children;	/* (c) List of children. */
118*1eaf0ac3Slogwang };
119*1eaf0ac3Slogwang 
120*1eaf0ac3Slogwang #define CPU_SET_ROOT    0x0001  /* Set is a root set. */
121*1eaf0ac3Slogwang #define CPU_SET_RDONLY  0x0002  /* No modification allowed. */
122*1eaf0ac3Slogwang 
123*1eaf0ac3Slogwang extern cpuset_t *cpuset_root;
124*1eaf0ac3Slogwang struct prison;
125*1eaf0ac3Slogwang struct proc;
126*1eaf0ac3Slogwang struct thread;
127*1eaf0ac3Slogwang 
128*1eaf0ac3Slogwang struct cpuset *cpuset_thread0(void);
129*1eaf0ac3Slogwang struct cpuset *cpuset_ref(struct cpuset *);
130*1eaf0ac3Slogwang void	cpuset_rel(struct cpuset *);
131*1eaf0ac3Slogwang int	cpuset_setthread(lwpid_t id, cpuset_t *);
132*1eaf0ac3Slogwang int	cpuset_setithread(lwpid_t id, int cpu);
133*1eaf0ac3Slogwang int	cpuset_create_root(struct prison *, struct cpuset **);
134*1eaf0ac3Slogwang int	cpuset_setproc_update_set(struct proc *, struct cpuset *);
135*1eaf0ac3Slogwang int	cpuset_which(cpuwhich_t, id_t, struct proc **,
136*1eaf0ac3Slogwang 	    struct thread **, struct cpuset **);
137*1eaf0ac3Slogwang 
138*1eaf0ac3Slogwang char	*cpusetobj_strprint(char *, const cpuset_t *);
139*1eaf0ac3Slogwang int	cpusetobj_strscan(cpuset_t *, const char *);
140*1eaf0ac3Slogwang #ifdef DDB
141*1eaf0ac3Slogwang void	ddb_display_cpuset(const cpuset_t *);
142*1eaf0ac3Slogwang #endif
143*1eaf0ac3Slogwang 
144*1eaf0ac3Slogwang #else
145*1eaf0ac3Slogwang __BEGIN_DECLS
146*1eaf0ac3Slogwang int	cpuset(cpusetid_t *);
147*1eaf0ac3Slogwang int	cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
148*1eaf0ac3Slogwang int	cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
149*1eaf0ac3Slogwang int	cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t *);
150*1eaf0ac3Slogwang int	cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const cpuset_t *);
151*1eaf0ac3Slogwang __END_DECLS
152*1eaf0ac3Slogwang #endif
153*1eaf0ac3Slogwang #endif /* !_SYS_CPUSET_H_ */
154