cpuset.c revision 1.21 1 1.21 maxv /* $NetBSD: cpuset.c,v 1.21 2019/05/11 11:53:55 maxv Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.7 christos #ifndef _STANDALONE
33 1.1 christos #include <sys/cdefs.h>
34 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
35 1.21 maxv __RCSID("$NetBSD: cpuset.c,v 1.21 2019/05/11 11:53:55 maxv Exp $");
36 1.1 christos #endif /* LIBC_SCCS and not lint */
37 1.1 christos
38 1.19 kamil #ifdef _LIBC
39 1.19 kamil #include "namespace.h"
40 1.19 kamil #endif
41 1.19 kamil
42 1.1 christos #include <sys/param.h>
43 1.1 christos #include <sys/sched.h>
44 1.1 christos #ifdef _KERNEL
45 1.1 christos #include <sys/kmem.h>
46 1.1 christos #include <lib/libkern/libkern.h>
47 1.1 christos #include <sys/atomic.h>
48 1.1 christos #else
49 1.8 christos #include <errno.h>
50 1.1 christos #include <string.h>
51 1.1 christos #include <stdlib.h>
52 1.1 christos #include <sys/sysctl.h>
53 1.1 christos #endif
54 1.1 christos
55 1.1 christos #define CPUSET_SHIFT 5
56 1.1 christos #define CPUSET_MASK 31
57 1.21 maxv #define CPUSET_NENTRIES(nc) (((nc) >> CPUSET_SHIFT) + 1)
58 1.8 christos #ifndef __lint__
59 1.13 christos #define CPUSET_SIZE(n) (sizeof( \
60 1.8 christos struct { \
61 1.13 christos uint32_t bits[0]; \
62 1.13 christos }) + sizeof(uint32_t) * (n))
63 1.8 christos #else
64 1.13 christos #define CPUSET_SIZE(n) 0
65 1.8 christos #endif
66 1.1 christos
67 1.1 christos struct _cpuset {
68 1.8 christos uint32_t bits[0];
69 1.8 christos };
70 1.8 christos
71 1.17 rmind #ifndef _KERNEL
72 1.1 christos
73 1.8 christos static size_t cpuset_size = 0;
74 1.8 christos static size_t cpuset_nentries = 0;
75 1.8 christos
76 1.1 christos size_t
77 1.5 christos /*ARGSUSED*/
78 1.1 christos _cpuset_size(const cpuset_t *c)
79 1.1 christos {
80 1.14 rmind
81 1.8 christos return cpuset_size;
82 1.1 christos }
83 1.1 christos
84 1.1 christos void
85 1.1 christos _cpuset_zero(cpuset_t *c)
86 1.1 christos {
87 1.14 rmind
88 1.14 rmind memset(c->bits, 0, cpuset_nentries * sizeof(c->bits[0]));
89 1.1 christos }
90 1.1 christos
91 1.1 christos int
92 1.8 christos _cpuset_isset(cpuid_t i, const cpuset_t *c)
93 1.1 christos {
94 1.12 dholland const unsigned long j = i >> CPUSET_SHIFT;
95 1.2 rmind
96 1.12 dholland if (j >= cpuset_nentries) {
97 1.8 christos errno = EINVAL;
98 1.1 christos return -1;
99 1.8 christos }
100 1.20 kamil return ((1U << (unsigned int)(i & CPUSET_MASK)) & c->bits[j]) != 0;
101 1.1 christos }
102 1.1 christos
103 1.1 christos int
104 1.8 christos _cpuset_set(cpuid_t i, cpuset_t *c)
105 1.1 christos {
106 1.12 dholland const unsigned long j = i >> CPUSET_SHIFT;
107 1.2 rmind
108 1.12 dholland if (j >= cpuset_nentries) {
109 1.8 christos errno = EINVAL;
110 1.1 christos return -1;
111 1.8 christos }
112 1.20 kamil c->bits[j] |= 1U << (unsigned int)(i & CPUSET_MASK);
113 1.1 christos return 0;
114 1.1 christos }
115 1.1 christos
116 1.1 christos int
117 1.8 christos _cpuset_clr(cpuid_t i, cpuset_t *c)
118 1.1 christos {
119 1.12 dholland const unsigned long j = i >> CPUSET_SHIFT;
120 1.2 rmind
121 1.12 dholland if (j >= cpuset_nentries) {
122 1.8 christos errno = EINVAL;
123 1.1 christos return -1;
124 1.8 christos }
125 1.20 kamil c->bits[j] &= ~(1U << (unsigned int)(i & CPUSET_MASK));
126 1.1 christos return 0;
127 1.1 christos }
128 1.1 christos
129 1.1 christos cpuset_t *
130 1.1 christos _cpuset_create(void)
131 1.1 christos {
132 1.14 rmind
133 1.8 christos if (cpuset_size == 0) {
134 1.8 christos static int mib[2] = { CTL_HW, HW_NCPU };
135 1.8 christos size_t len;
136 1.10 he u_int nc;
137 1.8 christos
138 1.16 rmind len = sizeof(nc);
139 1.18 christos if (sysctl(mib, (unsigned int)__arraycount(mib), &nc, &len,
140 1.18 christos NULL, 0) == -1)
141 1.8 christos return NULL;
142 1.8 christos
143 1.10 he cpuset_nentries = CPUSET_NENTRIES(nc);
144 1.13 christos cpuset_size = CPUSET_SIZE(cpuset_nentries);
145 1.3 rmind }
146 1.8 christos return calloc(1, cpuset_size);
147 1.1 christos }
148 1.1 christos
149 1.1 christos void
150 1.1 christos _cpuset_destroy(cpuset_t *c)
151 1.1 christos {
152 1.14 rmind
153 1.1 christos free(c);
154 1.1 christos }
155 1.1 christos
156 1.1 christos #endif
157 1.7 christos #endif
158