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