xref: /netbsd-src/common/lib/libc/sys/cpuset.c (revision dedb2f3a2d55167815a66e691182d66e3f28692b)
1*dedb2f3aSmaxv /*	$NetBSD: cpuset.c,v 1.21 2019/05/11 11:53:55 maxv Exp $	*/
2849fbad5Schristos 
3849fbad5Schristos /*-
4849fbad5Schristos  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5849fbad5Schristos  * All rights reserved.
6849fbad5Schristos  *
7849fbad5Schristos  * This code is derived from software contributed to The NetBSD Foundation
8849fbad5Schristos  * by Christos Zoulas.
9849fbad5Schristos  *
10849fbad5Schristos  * Redistribution and use in source and binary forms, with or without
11849fbad5Schristos  * modification, are permitted provided that the following conditions
12849fbad5Schristos  * are met:
13849fbad5Schristos  * 1. Redistributions of source code must retain the above copyright
14849fbad5Schristos  *    notice, this list of conditions and the following disclaimer.
15849fbad5Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16849fbad5Schristos  *    notice, this list of conditions and the following disclaimer in the
17849fbad5Schristos  *    documentation and/or other materials provided with the distribution.
18849fbad5Schristos  *
19849fbad5Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20849fbad5Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21849fbad5Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22849fbad5Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23849fbad5Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24849fbad5Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25849fbad5Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26849fbad5Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27849fbad5Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28849fbad5Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29849fbad5Schristos  * POSSIBILITY OF SUCH DAMAGE.
30849fbad5Schristos  */
31849fbad5Schristos 
32ddbb9c58Schristos #ifndef _STANDALONE
33849fbad5Schristos #include <sys/cdefs.h>
34849fbad5Schristos #if defined(LIBC_SCCS) && !defined(lint)
35*dedb2f3aSmaxv __RCSID("$NetBSD: cpuset.c,v 1.21 2019/05/11 11:53:55 maxv Exp $");
36849fbad5Schristos #endif /* LIBC_SCCS and not lint */
37849fbad5Schristos 
38c5b83981Skamil #ifdef _LIBC
39c5b83981Skamil #include "namespace.h"
40c5b83981Skamil #endif
41c5b83981Skamil 
42849fbad5Schristos #include <sys/param.h>
43849fbad5Schristos #include <sys/sched.h>
44849fbad5Schristos #ifdef _KERNEL
45849fbad5Schristos #include <sys/kmem.h>
46849fbad5Schristos #include <lib/libkern/libkern.h>
47849fbad5Schristos #include <sys/atomic.h>
48849fbad5Schristos #else
49dfeefcdcSchristos #include <errno.h>
50849fbad5Schristos #include <string.h>
51849fbad5Schristos #include <stdlib.h>
52849fbad5Schristos #include <sys/sysctl.h>
53849fbad5Schristos #endif
54849fbad5Schristos 
55849fbad5Schristos #define	CPUSET_SHIFT	5
56849fbad5Schristos #define	CPUSET_MASK	31
57*dedb2f3aSmaxv #define	CPUSET_NENTRIES(nc)	(((nc) >> CPUSET_SHIFT) + 1)
58dfeefcdcSchristos #ifndef __lint__
59512ce498Schristos #define CPUSET_SIZE(n)	(sizeof( \
60dfeefcdcSchristos 	struct {  \
61512ce498Schristos 		uint32_t bits[0]; \
62512ce498Schristos 	}) + sizeof(uint32_t) * (n))
63dfeefcdcSchristos #else
64512ce498Schristos #define CPUSET_SIZE(n)	0
65dfeefcdcSchristos #endif
66849fbad5Schristos 
67849fbad5Schristos struct _cpuset {
68849fbad5Schristos 	uint32_t	bits[0];
69849fbad5Schristos };
70849fbad5Schristos 
7152b220e9Srmind #ifndef _KERNEL
72dfeefcdcSchristos 
73dfeefcdcSchristos static size_t cpuset_size = 0;
74dfeefcdcSchristos static size_t cpuset_nentries = 0;
75dfeefcdcSchristos 
76849fbad5Schristos size_t
77834ef620Schristos /*ARGSUSED*/
_cpuset_size(const cpuset_t * c)78849fbad5Schristos _cpuset_size(const cpuset_t *c)
79849fbad5Schristos {
809c30a053Srmind 
81dfeefcdcSchristos 	return cpuset_size;
82849fbad5Schristos }
83849fbad5Schristos 
84849fbad5Schristos void
_cpuset_zero(cpuset_t * c)85849fbad5Schristos _cpuset_zero(cpuset_t *c)
86849fbad5Schristos {
879c30a053Srmind 
889c30a053Srmind 	memset(c->bits, 0, cpuset_nentries * sizeof(c->bits[0]));
89849fbad5Schristos }
90849fbad5Schristos 
91849fbad5Schristos int
_cpuset_isset(cpuid_t i,const cpuset_t * c)92dfeefcdcSchristos _cpuset_isset(cpuid_t i, const cpuset_t *c)
93849fbad5Schristos {
94154fe9b1Sdholland 	const unsigned long j = i >> CPUSET_SHIFT;
958f807354Srmind 
96154fe9b1Sdholland 	if (j >= cpuset_nentries) {
97dfeefcdcSchristos 		errno = EINVAL;
98849fbad5Schristos 		return -1;
99dfeefcdcSchristos 	}
1004581cca7Skamil 	return ((1U << (unsigned int)(i & CPUSET_MASK)) & c->bits[j]) != 0;
101849fbad5Schristos }
102849fbad5Schristos 
103849fbad5Schristos int
_cpuset_set(cpuid_t i,cpuset_t * c)104dfeefcdcSchristos _cpuset_set(cpuid_t i, cpuset_t *c)
105849fbad5Schristos {
106154fe9b1Sdholland 	const unsigned long j = i >> CPUSET_SHIFT;
1078f807354Srmind 
108154fe9b1Sdholland 	if (j >= cpuset_nentries) {
109dfeefcdcSchristos 		errno = EINVAL;
110849fbad5Schristos 		return -1;
111dfeefcdcSchristos 	}
1124581cca7Skamil 	c->bits[j] |= 1U << (unsigned int)(i & CPUSET_MASK);
113849fbad5Schristos 	return 0;
114849fbad5Schristos }
115849fbad5Schristos 
116849fbad5Schristos int
_cpuset_clr(cpuid_t i,cpuset_t * c)117dfeefcdcSchristos _cpuset_clr(cpuid_t i, cpuset_t *c)
118849fbad5Schristos {
119154fe9b1Sdholland 	const unsigned long j = i >> CPUSET_SHIFT;
1208f807354Srmind 
121154fe9b1Sdholland 	if (j >= cpuset_nentries) {
122dfeefcdcSchristos 		errno = EINVAL;
123849fbad5Schristos 		return -1;
124dfeefcdcSchristos 	}
1254581cca7Skamil 	c->bits[j] &= ~(1U << (unsigned int)(i & CPUSET_MASK));
126849fbad5Schristos 	return 0;
127849fbad5Schristos }
128849fbad5Schristos 
129849fbad5Schristos cpuset_t *
_cpuset_create(void)130849fbad5Schristos _cpuset_create(void)
131849fbad5Schristos {
1329c30a053Srmind 
133dfeefcdcSchristos 	if (cpuset_size == 0) {
134849fbad5Schristos 		static int mib[2] = { CTL_HW, HW_NCPU };
1358f807354Srmind 		size_t len;
136d6e5879dShe 		u_int nc;
137dfeefcdcSchristos 
138380cb143Srmind 		len = sizeof(nc);
139a5fd370aSchristos 		if (sysctl(mib, (unsigned int)__arraycount(mib), &nc, &len,
140a5fd370aSchristos 		    NULL, 0) == -1)
141849fbad5Schristos 			return NULL;
142dfeefcdcSchristos 
143d6e5879dShe 		cpuset_nentries = CPUSET_NENTRIES(nc);
144512ce498Schristos 		cpuset_size = CPUSET_SIZE(cpuset_nentries);
1457d902495Srmind 	}
146dfeefcdcSchristos 	return calloc(1, cpuset_size);
147849fbad5Schristos }
148849fbad5Schristos 
149849fbad5Schristos void
_cpuset_destroy(cpuset_t * c)150849fbad5Schristos _cpuset_destroy(cpuset_t *c)
151849fbad5Schristos {
1529c30a053Srmind 
153dfeefcdcSchristos 	free(c);
154dfeefcdcSchristos }
155dfeefcdcSchristos 
156849fbad5Schristos #endif
157ddbb9c58Schristos #endif
158