cpuset.c revision 1.5 1 /* $NetBSD: cpuset.c,v 1.5 2008/06/16 02:30:03 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: cpuset.c,v 1.5 2008/06/16 02:30:03 christos Exp $");
35 #endif /* LIBC_SCCS and not lint */
36
37 #include <sys/param.h>
38 #include <sys/sched.h>
39 #ifdef _KERNEL
40 #include <sys/kmem.h>
41 #include <lib/libkern/libkern.h>
42 #include <sys/atomic.h>
43 #else
44 #include <string.h>
45 #include <stdlib.h>
46 #include <sys/sysctl.h>
47 #endif
48
49 #define CPUSET_SHIFT 5
50 #define CPUSET_MASK 31
51 #define CPUSET_SIZE(nc) ((nc) > 32 ? ((nc) >> CPUSET_SHIFT) : 1)
52
53 struct _cpuset {
54 size_t size;
55 unsigned int nused;
56 struct _cpuset *next;
57 uint32_t bits[0];
58 };
59
60 size_t
61 /*ARGSUSED*/
62 _cpuset_size(const cpuset_t *c)
63 {
64 return sizeof(struct {
65 size_t size;
66 unsigned int nused;
67 struct _cpuset *next;
68 uint32_t bits[c->size];
69 });
70 }
71
72 void
73 _cpuset_zero(cpuset_t *c)
74 {
75 #ifdef _KERNEL
76 KASSERT(c->nused == 1);
77 #endif
78 (void)memset(c->bits, 0, c->size * sizeof(c->bits[0]));
79 }
80
81 int
82 _cpuset_isset(const cpuset_t *c, cpuid_t i)
83 {
84 const int j = i >> CPUSET_SHIFT;
85
86 if (j >= c->size || j < 0)
87 return -1;
88 return ((1 << (i & CPUSET_MASK)) & c->bits[j]) != 0;
89 }
90
91 int
92 _cpuset_set(cpuset_t *c, cpuid_t i)
93 {
94 const int j = i >> CPUSET_SHIFT;
95
96 if (j >= c->size || j < 0)
97 return -1;
98 c->bits[j] |= 1 << (i & CPUSET_MASK);
99 return 0;
100 }
101
102 int
103 _cpuset_clr(cpuset_t *c, cpuid_t i)
104 {
105 const int j = i >> CPUSET_SHIFT;
106
107 if (j >= c->size || j < 0)
108 return -1;
109 c->bits[j] &= ~(1 << (i & CPUSET_MASK));
110 return 0;
111 }
112
113 cpuset_t *
114 _cpuset_create(void)
115 {
116 cpuset_t s, *c;
117 #ifdef _KERNEL
118 s.size = CPUSET_SIZE(MAXCPUS);
119 c = kmem_zalloc(_cpuset_size(&s), KM_SLEEP);
120 #else
121 static int mib[2] = { CTL_HW, HW_NCPU };
122 size_t len;
123 int nc;
124 if (sysctl(mib, __arraycount(mib), &nc, &len, NULL, 0) == -1)
125 return NULL;
126 s.size = CPUSET_SIZE(nc);
127 c = calloc(1, _cpuset_size(&s));
128 #endif
129 if (c != NULL) {
130 c->next = NULL;
131 c->nused = 1;
132 c->size = s.size;
133 }
134 return c;
135 }
136
137 void
138 _cpuset_destroy(cpuset_t *c)
139 {
140 #ifdef _KERNEL
141 while (c) {
142 KASSERT(c->nused == 0);
143 kmem_free(c, _cpuset_size(c));
144 c = c->next;
145 }
146 #else
147 free(c);
148 #endif
149 }
150
151 #ifdef _KERNEL
152 size_t
153 kcpuset_nused(const cpuset_t *c)
154 {
155 return c->nused;
156 }
157
158 void
159 kcpuset_copy(cpuset_t *d, const cpuset_t *s)
160 {
161
162 KASSERT(d->size == s->size);
163 KASSERT(d->nused == 1);
164 (void)memcpy(d->bits, s->bits, d->size * sizeof(d->bits[0]));
165 }
166
167 void
168 kcpuset_use(cpuset_t *c)
169 {
170
171 atomic_inc_uint(&c->nused);
172 }
173
174 void
175 kcpuset_unuse(cpuset_t *c, cpuset_t **lst)
176 {
177
178 if (atomic_dec_uint_nv(&c->nused) != 0)
179 return;
180 KASSERT(c->nused > 0);
181 KASSERT(c->next == NULL);
182 if (lst == NULL) {
183 _cpuset_destroy(c);
184 return;
185 }
186 c->next = *lst;
187 *lst = c;
188 }
189 #endif
190