cpuset.h revision 176730
1/*-
2 * Copyright (c) 2008,	Jeffrey Roberson <jeff@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/sys/cpuset.h 176730 2008-03-02 07:39:22Z jeff $
27 */
28
29#ifndef _SYS_CPUSET_H_
30#define	_SYS_CPUSET_H_
31
32#ifdef _KERNEL
33#define	CPU_SETSIZE	MAXCPU
34#endif
35
36#define	CPU_MAXSIZE	128
37
38#ifndef	CPU_SETSIZE
39#define	CPU_SETSIZE	CPU_MAXSIZE
40#endif
41
42#define	_NCPUBITS	(sizeof(long) * NBBY)	/* bits per mask */
43#define	_NCPUWORDS	howmany(CPU_SETSIZE, _NCPUBITS)
44
45typedef	struct _cpuset {
46	long	__bits[howmany(CPU_SETSIZE, _NCPUBITS)];
47} cpuset_t;
48
49#define	__cpuset_mask(n)	((long)1 << ((n) % _NCPUBITS))
50#define	CPU_CLR(n, p)	((p)->__bits[(n)/_NCPUBITS] &= ~__cpuset_mask(n))
51#define	CPU_COPY(f, t)	(void)(*(t) = *(f))
52#define	CPU_ISSET(n, p)	(((p)->__bits[(n)/_NCPUBITS] & __cpuset_mask(n)) != 0)
53#define	CPU_SET(n, p)	((p)->__bits[(n)/_NCPUBITS] |= __cpuset_mask(n))
54#define	CPU_ZERO(p) do {				\
55	__size_t __i;					\
56	for (__i = 0; __i < _NCPUWORDS; __i++)		\
57		(p)->__bits[__i] = 0;			\
58} while (0)
59
60#define	CPU_EMPTY(p) __extension__ ({			\
61	__size_t __i;					\
62	for (__i = 0; __i < _NCPUWORDS; __i++)		\
63		if ((p)->__bits[__i])			\
64			break;				\
65	__i == _NCPUWORDS;				\
66})
67
68#define	CPU_OR(d, s) do {				\
69	__size_t __i;					\
70	for (__i = 0; __i < _NCPUWORDS; __i++)		\
71		(d)->__bits[__i] |= (s)->__bits[__i];	\
72} while (0)
73
74#define	CPU_AND(d, s) do {				\
75	__size_t __i;					\
76	for (__i = 0; __i < _NCPUWORDS; __i++)		\
77		(d)->__bits[__i] &= (s)->__bits[__i];	\
78} while (0)
79
80#define	CPU_NAND(d, s) do {				\
81	__size_t __i;					\
82	for (__i = 0; __i < _NCPUWORDS; __i++)		\
83		(d)->__bits[__i] &= ~(s)->__bits[__i];	\
84} while (0)
85
86/*
87 * Valid cpulevel_t values.
88 */
89#define	CPU_LEVEL_ROOT		1	/* All system cpus. */
90#define	CPU_LEVEL_CPUSET	2	/* Available cpus for which. */
91#define	CPU_LEVEL_WHICH		3	/* Actual mask/id for which. */
92
93/*
94 * Valid cpuwhich_t values.
95 */
96#define	CPU_WHICH_TID		1	/* Specifies a thread id. */
97#define	CPU_WHICH_PID		2	/* Specifies a process id. */
98#define	CPU_WHICH_CPUSET	3	/* Specifies a set id. */
99
100/*
101 * Reserved cpuset identifiers.
102 */
103#define	CPUSET_INVALID	-1
104#define	CPUSET_DEFAULT	0
105
106#ifdef _KERNEL
107LIST_HEAD(setlist, cpuset);
108
109/*
110 * cpusets encapsulate cpu binding information for one or more threads.
111 *
112 * 	a - Accessed with atomics.
113 *	s - Set at creation, never modified.  Only a ref required to read.
114 *	c - Locked internally by a cpuset lock.
115 *
116 * The bitmask is only modified while holding the cpuset lock.  It may be
117 * read while only a reference is held but the consumer must be prepared
118 * to deal with inconsistent results.
119 */
120struct cpuset {
121	cpuset_t		cs_mask;	/* bitmask of valid cpus. */
122	volatile u_int		cs_ref;		/* (a) Reference count. */
123	int			cs_flags;	/* (s) Flags from below. */
124	cpusetid_t		cs_id;		/* (s) Id or INVALID. */
125	struct cpuset		*cs_parent;	/* (s) Pointer to our parent. */
126	LIST_ENTRY(cpuset)	cs_link;	/* (c) All identified sets. */
127	LIST_ENTRY(cpuset)	cs_siblings;	/* (c) Sibling set link. */
128	struct setlist		cs_children;	/* (c) List of children. */
129};
130
131#define CPU_SET_ROOT    0x0001  /* Set is a root set. */
132#define CPU_SET_RDONLY  0x0002  /* No modification allowed. */
133
134struct cpuset *cpuset_thread0(void);
135struct cpuset *cpuset_ref(struct cpuset *);
136void cpuset_rel(struct cpuset *);
137#else
138__BEGIN_DECLS
139int	cpuset(cpusetid_t *);
140int	cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
141int	cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
142int	cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, int, cpuset_t *);
143int	cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, int, cpuset_t *);
144__END_DECLS
145#endif
146#endif /* !_SYS_CPUSET_H_ */
147