Home | History | Annotate | Line # | Download | only in sys
cpuset.c revision 1.7
      1 /*	$NetBSD: cpuset.c,v 1.7 2008/06/16 13:02:08 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 #ifndef _STANDALONE
     33 #include <sys/cdefs.h>
     34 #if defined(LIBC_SCCS) && !defined(lint)
     35 __RCSID("$NetBSD: cpuset.c,v 1.7 2008/06/16 13:02:08 christos Exp $");
     36 #endif /* LIBC_SCCS and not lint */
     37 
     38 #include <sys/param.h>
     39 #include <sys/sched.h>
     40 #ifdef _KERNEL
     41 #include <sys/kmem.h>
     42 #include <lib/libkern/libkern.h>
     43 #include <sys/atomic.h>
     44 #else
     45 #include <string.h>
     46 #include <stdlib.h>
     47 #include <sys/sysctl.h>
     48 #endif
     49 
     50 #define	CPUSET_SHIFT	5
     51 #define	CPUSET_MASK	31
     52 #define CPUSET_SIZE(nc)	((nc) > 32 ? ((nc) >> CPUSET_SHIFT) : 1)
     53 
     54 struct _cpuset {
     55 	size_t		size;
     56 	unsigned int	nused;
     57 	struct _cpuset *next;
     58 	uint32_t	bits[0];
     59 };
     60 
     61 size_t
     62 /*ARGSUSED*/
     63 _cpuset_size(const cpuset_t *c)
     64 {
     65 	return sizeof(struct {
     66 	    size_t size;
     67 	    unsigned int nused;
     68 	    struct _cpuset *next;
     69 	    uint32_t bits[c->size];
     70 	});
     71 }
     72 
     73 void
     74 _cpuset_zero(cpuset_t *c)
     75 {
     76 #ifdef _KERNEL
     77 	KASSERT(c->nused == 1);
     78 #endif
     79 	(void)memset(c->bits, 0, c->size * sizeof(c->bits[0]));
     80 }
     81 
     82 int
     83 _cpuset_isset(const cpuset_t *c, cpuid_t i)
     84 {
     85 	const int j = i >> CPUSET_SHIFT;
     86 
     87 	if (j >= c->size || j < 0)
     88 		return -1;
     89 	return ((1 << (i & CPUSET_MASK)) & c->bits[j]) != 0;
     90 }
     91 
     92 int
     93 _cpuset_set(cpuset_t *c, cpuid_t i)
     94 {
     95 	const int j = i >> CPUSET_SHIFT;
     96 
     97 	if (j >= c->size || j < 0)
     98 		return -1;
     99 	c->bits[j] |= 1 << (i & CPUSET_MASK);
    100 	return 0;
    101 }
    102 
    103 int
    104 _cpuset_clr(cpuset_t *c, cpuid_t i)
    105 {
    106 	const int j = i >> CPUSET_SHIFT;
    107 
    108 	if (j >= c->size || j < 0)
    109 		return -1;
    110 	c->bits[j] &= ~(1 << (i & CPUSET_MASK));
    111 	return 0;
    112 }
    113 
    114 cpuset_t *
    115 _cpuset_create(void)
    116 {
    117 	cpuset_t s, *c;
    118 #ifdef _KERNEL
    119 	s.size = CPUSET_SIZE(MAXCPUS);
    120 	c = kmem_zalloc(_cpuset_size(&s), KM_SLEEP);
    121 #else
    122 	static int mib[2] = { CTL_HW, HW_NCPU };
    123 	size_t len;
    124 	int nc;
    125 	if (sysctl(mib, __arraycount(mib), &nc, &len, NULL, 0) == -1)
    126 		return NULL;
    127 	s.size = CPUSET_SIZE((unsigned int)nc);
    128 	c = calloc(1, _cpuset_size(&s));
    129 #endif
    130 	if (c != NULL) {
    131 		c->next = NULL;
    132 		c->nused = 1;
    133 		c->size = s.size;
    134 	}
    135 	return c;
    136 }
    137 
    138 void
    139 _cpuset_destroy(cpuset_t *c)
    140 {
    141 #ifdef _KERNEL
    142 	while (c) {
    143 		KASSERT(c->nused == 0);
    144 		kmem_free(c, _cpuset_size(c));
    145 		c = c->next;
    146 	}
    147 #else
    148 	free(c);
    149 #endif
    150 }
    151 
    152 #ifdef _KERNEL
    153 size_t
    154 kcpuset_nused(const cpuset_t *c)
    155 {
    156 	return c->nused;
    157 }
    158 
    159 void
    160 kcpuset_copy(cpuset_t *d, const cpuset_t *s)
    161 {
    162 
    163 	KASSERT(d->size == s->size);
    164 	KASSERT(d->nused == 1);
    165 	(void)memcpy(d->bits, s->bits, d->size * sizeof(d->bits[0]));
    166 }
    167 
    168 void
    169 kcpuset_use(cpuset_t *c)
    170 {
    171 
    172 	atomic_inc_uint(&c->nused);
    173 }
    174 
    175 void
    176 kcpuset_unuse(cpuset_t *c, cpuset_t **lst)
    177 {
    178 
    179 	if (atomic_dec_uint_nv(&c->nused) != 0)
    180 		return;
    181 	KASSERT(c->nused > 0);
    182 	KASSERT(c->next == NULL);
    183 	if (lst == NULL) {
    184 		_cpuset_destroy(c);
    185 		return;
    186 	}
    187 	c->next = *lst;
    188 	*lst = c;
    189 }
    190 #endif
    191 #endif
    192