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 #ifdef _KERNEL 36 #define CPU_SETSIZE MAXCPU 37 #endif 38 39 #define CPU_MAXSIZE 128 40 41 #ifndef CPU_SETSIZE 42 #define CPU_SETSIZE CPU_MAXSIZE 43 #endif 44 45 #define _NCPUBITS (sizeof(long) * NBBY) /* bits per mask */ 46 #define _NCPUWORDS howmany(CPU_SETSIZE, _NCPUBITS) 47 48 typedef struct _cpuset { 49 long __bits[howmany(CPU_SETSIZE, _NCPUBITS)]; 50 } cpuset_t; 51 52 #define __cpuset_mask(n) ((long)1 << ((n) % _NCPUBITS)) 53 #define CPU_CLR(n, p) ((p)->__bits[(n)/_NCPUBITS] &= ~__cpuset_mask(n)) 54 #define CPU_COPY(f, t) (void)(*(t) = *(f)) 55 #define CPU_ISSET(n, p) (((p)->__bits[(n)/_NCPUBITS] & __cpuset_mask(n)) != 0) 56 #define CPU_SET(n, p) ((p)->__bits[(n)/_NCPUBITS] |= __cpuset_mask(n)) 57 #define CPU_ZERO(p) do { \ 58 __size_t __i; \ 59 for (__i = 0; __i < _NCPUWORDS; __i++) \ 60 (p)->__bits[__i] = 0; \ 61 } while (0) 62 63 #define CPU_FILL(p) do { \ 64 __size_t __i; \ 65 for (__i = 0; __i < _NCPUWORDS; __i++) \ 66 (p)->__bits[__i] = -1; \ 67 } while (0) 68 69 /* Is p empty. */ 70 #define CPU_EMPTY(p) __extension__ ({ \ 71 __size_t __i; \ 72 for (__i = 0; __i < _NCPUWORDS; __i++) \ 73 if ((p)->__bits[__i]) \ 74 break; \ 75 __i == _NCPUWORDS; \ 76 }) 77 78 /* Is c a subset of p. */ 79 #define CPU_SUBSET(p, c) __extension__ ({ \ 80 __size_t __i; \ 81 for (__i = 0; __i < _NCPUWORDS; __i++) \ 82 if (((c)->__bits[__i] & \ 83 (p)->__bits[__i]) != \ 84 (c)->__bits[__i]) \ 85 break; \ 86 __i == _NCPUWORDS; \ 87 }) 88 89 /* Are there any common bits between b & c? */ 90 #define CPU_OVERLAP(p, c) __extension__ ({ \ 91 __size_t __i; \ 92 for (__i = 0; __i < _NCPUWORDS; __i++) \ 93 if (((c)->__bits[__i] & \ 94 (p)->__bits[__i]) != 0) \ 95 break; \ 96 __i != _NCPUWORDS; \ 97 }) 98 99 /* Compare two sets, returns 0 if equal 1 otherwise. */ 100 #define CPU_CMP(p, c) __extension__ ({ \ 101 __size_t __i; \ 102 for (__i = 0; __i < _NCPUWORDS; __i++) \ 103 if (((c)->__bits[__i] != \ 104 (p)->__bits[__i])) \ 105 break; \ 106 __i != _NCPUWORDS; \ 107 }) 108 109 #define CPU_OR(d, s) do { \ 110 __size_t __i; \ 111 for (__i = 0; __i < _NCPUWORDS; __i++) \ 112 (d)->__bits[__i] |= (s)->__bits[__i]; \ 113 } while (0) 114 115 #define CPU_AND(d, s) do { \ 116 __size_t __i; \ 117 for (__i = 0; __i < _NCPUWORDS; __i++) \ 118 (d)->__bits[__i] &= (s)->__bits[__i]; \ 119 } while (0) 120 121 #define CPU_NAND(d, s) do { \ 122 __size_t __i; \ 123 for (__i = 0; __i < _NCPUWORDS; __i++) \ 124 (d)->__bits[__i] &= ~(s)->__bits[__i]; \ 125 } while (0) 126 127 /* 128 * Valid cpulevel_t values. 129 */ 130 #define CPU_LEVEL_ROOT 1 /* All system cpus. */ 131 #define CPU_LEVEL_CPUSET 2 /* Available cpus for which. */ 132 #define CPU_LEVEL_WHICH 3 /* Actual mask/id for which. */ 133 134 /* 135 * Valid cpuwhich_t values. 136 */ 137 #define CPU_WHICH_TID 1 /* Specifies a thread id. */ 138 #define CPU_WHICH_PID 2 /* Specifies a process id. */ 139 #define CPU_WHICH_CPUSET 3 /* Specifies a set id. */ 140 #define CPU_WHICH_IRQ 4 /* Specifies an irq #. */ 141 #define CPU_WHICH_JAIL 5 /* Specifies a jail id. */ 142 143 /* 144 * Reserved cpuset identifiers. 145 */ 146 #define CPUSET_INVALID -1 147 #define CPUSET_DEFAULT 0 148 149 #ifdef _KERNEL 150 LIST_HEAD(setlist, cpuset); 151 152 /* 153 * cpusets encapsulate cpu binding information for one or more threads. 154 * 155 * a - Accessed with atomics. 156 * s - Set at creation, never modified. Only a ref required to read. 157 * c - Locked internally by a cpuset lock. 158 * 159 * The bitmask is only modified while holding the cpuset lock. It may be 160 * read while only a reference is held but the consumer must be prepared 161 * to deal with inconsistent results. 162 */ 163 struct cpuset { 164 cpuset_t cs_mask; /* bitmask of valid cpus. */ 165 volatile u_int cs_ref; /* (a) Reference count. */ 166 int cs_flags; /* (s) Flags from below. */ 167 cpusetid_t cs_id; /* (s) Id or INVALID. */ 168 struct cpuset *cs_parent; /* (s) Pointer to our parent. */ 169 LIST_ENTRY(cpuset) cs_link; /* (c) All identified sets. */ 170 LIST_ENTRY(cpuset) cs_siblings; /* (c) Sibling set link. */ 171 struct setlist cs_children; /* (c) List of children. */ 172 }; 173 174 #define CPU_SET_ROOT 0x0001 /* Set is a root set. */ 175 #define CPU_SET_RDONLY 0x0002 /* No modification allowed. */ 176 177 extern cpuset_t *cpuset_root; 178 struct prison; 179 struct proc; 180 181 struct cpuset *cpuset_thread0(void); 182 struct cpuset *cpuset_ref(struct cpuset *); 183 void cpuset_rel(struct cpuset *); 184 int cpuset_setthread(lwpid_t id, cpuset_t *); 185 int cpuset_create_root(struct prison *, struct cpuset **); 186 int cpuset_setproc_update_set(struct proc *, struct cpuset *); 187 188 #else 189 __BEGIN_DECLS 190 int cpuset(cpusetid_t *); 191 int cpuset_setid(cpuwhich_t, id_t, cpusetid_t); 192 int cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *); 193 int cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t *); 194 int cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const cpuset_t *); 195 __END_DECLS 196 #endif 197 #endif /* !_SYS_CPUSET_H_ */ 198