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