]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/sys/cpuset.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / sys / cpuset.h
1 /*-
2  * Copyright (c) 2008,  Jeffrey Roberson <jeff@freebsd.org>
3  * All rights reserved.
4  *
5  * Copyright (c) 2008 Nokia Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
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.
17  *
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.
28  *
29  * $FreeBSD$
30  */
31
32 #ifndef _SYS_CPUSET_H_
33 #define _SYS_CPUSET_H_
34
35 #include <sys/_cpuset.h>
36
37 #define CPUSETBUFSIZ    ((2 + sizeof(long) * 2) * _NCPUWORDS)
38
39 /*
40  * Macros addressing word and bit within it, tuned to make compiler
41  * optimize cases when CPU_SETSIZE fits into single machine word.
42  */
43 #define __cpuset_mask(n)                                \
44         ((long)1 << ((_NCPUWORDS == 1) ? (__size_t)(n) : ((n) % _NCPUBITS)))
45 #define __cpuset_word(n)        ((_NCPUWORDS == 1) ? 0 : ((n) / _NCPUBITS))
46
47 #define CPU_CLR(n, p)   ((p)->__bits[__cpuset_word(n)] &= ~__cpuset_mask(n))
48 #define CPU_COPY(f, t)  (void)(*(t) = *(f))
49 #define CPU_ISSET(n, p) (((p)->__bits[__cpuset_word(n)] & __cpuset_mask(n)) != 0)
50 #define CPU_SET(n, p)   ((p)->__bits[__cpuset_word(n)] |= __cpuset_mask(n))
51 #define CPU_ZERO(p) do {                                \
52         __size_t __i;                                   \
53         for (__i = 0; __i < _NCPUWORDS; __i++)          \
54                 (p)->__bits[__i] = 0;                   \
55 } while (0)
56
57 #define CPU_FILL(p) do {                                \
58         __size_t __i;                                   \
59         for (__i = 0; __i < _NCPUWORDS; __i++)          \
60                 (p)->__bits[__i] = -1;                  \
61 } while (0)
62
63 #define CPU_SETOF(n, p) do {                                    \
64         CPU_ZERO(p);                                            \
65         ((p)->__bits[__cpuset_word(n)] = __cpuset_mask(n));     \
66 } while (0)
67
68 /* Is p empty. */
69 #define CPU_EMPTY(p) __extension__ ({                   \
70         __size_t __i;                                   \
71         for (__i = 0; __i < _NCPUWORDS; __i++)          \
72                 if ((p)->__bits[__i])                   \
73                         break;                          \
74         __i == _NCPUWORDS;                              \
75 })
76
77 /* Is p full set. */
78 #define CPU_ISFULLSET(p) __extension__ ({               \
79         __size_t __i;                                   \
80         for (__i = 0; __i < _NCPUWORDS; __i++)          \
81                 if ((p)->__bits[__i] != (long)-1)       \
82                         break;                          \
83         __i == _NCPUWORDS;                              \
84 })
85
86 /* Is c a subset of p. */
87 #define CPU_SUBSET(p, c) __extension__ ({               \
88         __size_t __i;                                   \
89         for (__i = 0; __i < _NCPUWORDS; __i++)          \
90                 if (((c)->__bits[__i] &                 \
91                     (p)->__bits[__i]) !=                \
92                     (c)->__bits[__i])                   \
93                         break;                          \
94         __i == _NCPUWORDS;                              \
95 })
96
97 /* Are there any common bits between b & c? */
98 #define CPU_OVERLAP(p, c) __extension__ ({              \
99         __size_t __i;                                   \
100         for (__i = 0; __i < _NCPUWORDS; __i++)          \
101                 if (((c)->__bits[__i] &                 \
102                     (p)->__bits[__i]) != 0)             \
103                         break;                          \
104         __i != _NCPUWORDS;                              \
105 })
106
107 /* Compare two sets, returns 0 if equal 1 otherwise. */
108 #define CPU_CMP(p, c) __extension__ ({                  \
109         __size_t __i;                                   \
110         for (__i = 0; __i < _NCPUWORDS; __i++)          \
111                 if (((c)->__bits[__i] !=                \
112                     (p)->__bits[__i]))                  \
113                         break;                          \
114         __i != _NCPUWORDS;                              \
115 })
116
117 #define CPU_OR(d, s) do {                               \
118         __size_t __i;                                   \
119         for (__i = 0; __i < _NCPUWORDS; __i++)          \
120                 (d)->__bits[__i] |= (s)->__bits[__i];   \
121 } while (0)
122
123 #define CPU_AND(d, s) do {                              \
124         __size_t __i;                                   \
125         for (__i = 0; __i < _NCPUWORDS; __i++)          \
126                 (d)->__bits[__i] &= (s)->__bits[__i];   \
127 } while (0)
128
129 #define CPU_NAND(d, s) do {                             \
130         __size_t __i;                                   \
131         for (__i = 0; __i < _NCPUWORDS; __i++)          \
132                 (d)->__bits[__i] &= ~(s)->__bits[__i];  \
133 } while (0)
134
135 #define CPU_CLR_ATOMIC(n, p)                                            \
136         atomic_clear_long(&(p)->__bits[__cpuset_word(n)], __cpuset_mask(n))
137
138 #define CPU_SET_ATOMIC(n, p)                                            \
139         atomic_set_long(&(p)->__bits[__cpuset_word(n)], __cpuset_mask(n))
140
141 /* Convenience functions catering special cases. */ 
142 #define CPU_OR_ATOMIC(d, s) do {                        \
143         __size_t __i;                                   \
144         for (__i = 0; __i < _NCPUWORDS; __i++)          \
145                 atomic_set_long(&(d)->__bits[__i],      \
146                     (s)->__bits[__i]);                  \
147 } while (0)
148
149 #define CPU_COPY_STORE_REL(f, t) do {                           \
150         __size_t __i;                                           \
151         for (__i = 0; __i < _NCPUWORDS; __i++)                  \
152                 atomic_store_rel_long(&(t)->__bits[__i],        \
153                     (f)->__bits[__i]);                          \
154 } while (0)
155
156 /*
157  * Valid cpulevel_t values.
158  */
159 #define CPU_LEVEL_ROOT          1       /* All system cpus. */
160 #define CPU_LEVEL_CPUSET        2       /* Available cpus for which. */
161 #define CPU_LEVEL_WHICH         3       /* Actual mask/id for which. */
162
163 /*
164  * Valid cpuwhich_t values.
165  */
166 #define CPU_WHICH_TID           1       /* Specifies a thread id. */
167 #define CPU_WHICH_PID           2       /* Specifies a process id. */
168 #define CPU_WHICH_CPUSET        3       /* Specifies a set id. */
169 #define CPU_WHICH_IRQ           4       /* Specifies an irq #. */
170 #define CPU_WHICH_JAIL          5       /* Specifies a jail id. */
171
172 /*
173  * Reserved cpuset identifiers.
174  */
175 #define CPUSET_INVALID  -1
176 #define CPUSET_DEFAULT  0
177
178 #ifdef _KERNEL
179 LIST_HEAD(setlist, cpuset);
180
181 /*
182  * cpusets encapsulate cpu binding information for one or more threads.
183  *
184  *      a - Accessed with atomics.
185  *      s - Set at creation, never modified.  Only a ref required to read.
186  *      c - Locked internally by a cpuset lock.
187  *
188  * The bitmask is only modified while holding the cpuset lock.  It may be
189  * read while only a reference is held but the consumer must be prepared
190  * to deal with inconsistent results.
191  */
192 struct cpuset {
193         cpuset_t                cs_mask;        /* bitmask of valid cpus. */
194         volatile u_int          cs_ref;         /* (a) Reference count. */
195         int                     cs_flags;       /* (s) Flags from below. */
196         cpusetid_t              cs_id;          /* (s) Id or INVALID. */
197         struct cpuset           *cs_parent;     /* (s) Pointer to our parent. */
198         LIST_ENTRY(cpuset)      cs_link;        /* (c) All identified sets. */
199         LIST_ENTRY(cpuset)      cs_siblings;    /* (c) Sibling set link. */
200         struct setlist          cs_children;    /* (c) List of children. */
201 };
202
203 #define CPU_SET_ROOT    0x0001  /* Set is a root set. */
204 #define CPU_SET_RDONLY  0x0002  /* No modification allowed. */
205
206 extern cpuset_t *cpuset_root;
207 struct prison;
208 struct proc;
209
210 struct cpuset *cpuset_thread0(void);
211 struct cpuset *cpuset_ref(struct cpuset *);
212 void    cpuset_rel(struct cpuset *);
213 int     cpuset_setthread(lwpid_t id, cpuset_t *);
214 int     cpuset_create_root(struct prison *, struct cpuset **);
215 int     cpuset_setproc_update_set(struct proc *, struct cpuset *);
216 int     cpusetobj_ffs(const cpuset_t *);
217 char    *cpusetobj_strprint(char *, const cpuset_t *);
218 int     cpusetobj_strscan(cpuset_t *, const char *);
219
220 #else
221 __BEGIN_DECLS
222 int     cpuset(cpusetid_t *);
223 int     cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
224 int     cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
225 int     cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t *);
226 int     cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const cpuset_t *);
227 __END_DECLS
228 #endif
229 #endif /* !_SYS_CPUSET_H_ */