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