Separate cpuset and kcpuset, and only use the bits in userland. Requested

by yamt
This commit is contained in:
christos 2008-06-22 00:05:09 +00:00
parent b35d3ac323
commit dfeefcdcb8
1 changed files with 128 additions and 63 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: cpuset.c,v 1.7 2008/06/16 13:02:08 christos Exp $ */
/* $NetBSD: cpuset.c,v 1.8 2008/06/22 00:05:09 christos Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -32,7 +32,7 @@
#ifndef _STANDALONE
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: cpuset.c,v 1.7 2008/06/16 13:02:08 christos Exp $");
__RCSID("$NetBSD: cpuset.c,v 1.8 2008/06/22 00:05:09 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@ -42,6 +42,7 @@ __RCSID("$NetBSD: cpuset.c,v 1.7 2008/06/16 13:02:08 christos Exp $");
#include <lib/libkern/libkern.h>
#include <sys/atomic.h>
#else
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/sysctl.h>
@ -49,64 +50,85 @@ __RCSID("$NetBSD: cpuset.c,v 1.7 2008/06/16 13:02:08 christos Exp $");
#define CPUSET_SHIFT 5
#define CPUSET_MASK 31
#define CPUSET_SIZE(nc) ((nc) > 32 ? ((nc) >> CPUSET_SHIFT) : 1)
#define CPUSET_NENTRIES(nc) ((nc) > 32 ? ((nc) >> CPUSET_SHIFT) : 1)
#ifndef __lint__
#define CPUSET_SIZE() sizeof( \
struct { \
uint32_t bits[cpuset_nentries]; \
})
#else
#define CPUSET_SIZE() 0
#endif
struct _cpuset {
size_t size;
unsigned int nused;
struct _cpuset *next;
uint32_t bits[0];
};
#ifdef _KERNEL
struct _kcpuset {
unsigned int nused;
struct _kcpuset *next;
uint32_t bits[0];
};
#define KCPUSET_SIZE() sizeof( \
struct { \
unsigned int nused; \
struct _kcpuset *next; \
uint32_t bits[cpuset_nentries]; \
})
#endif
static size_t cpuset_size = 0;
static size_t cpuset_nentries = 0;
#ifndef _KERNEL
size_t
/*ARGSUSED*/
_cpuset_size(const cpuset_t *c)
{
return sizeof(struct {
size_t size;
unsigned int nused;
struct _cpuset *next;
uint32_t bits[c->size];
});
return cpuset_size;
}
void
_cpuset_zero(cpuset_t *c)
{
#ifdef _KERNEL
KASSERT(c->nused == 1);
#endif
(void)memset(c->bits, 0, c->size * sizeof(c->bits[0]));
(void)memset(c->bits, 0, cpuset_nentries * sizeof(c->bits[0]));
}
int
_cpuset_isset(const cpuset_t *c, cpuid_t i)
_cpuset_isset(cpuid_t i, const cpuset_t *c)
{
const int j = i >> CPUSET_SHIFT;
if (j >= c->size || j < 0)
if (j >= cpuset_nentries || j < 0) {
errno = EINVAL;
return -1;
}
return ((1 << (i & CPUSET_MASK)) & c->bits[j]) != 0;
}
int
_cpuset_set(cpuset_t *c, cpuid_t i)
_cpuset_set(cpuid_t i, cpuset_t *c)
{
const int j = i >> CPUSET_SHIFT;
if (j >= c->size || j < 0)
if (j >= cpuset_nentries || j < 0) {
errno = EINVAL;
return -1;
}
c->bits[j] |= 1 << (i & CPUSET_MASK);
return 0;
}
int
_cpuset_clr(cpuset_t *c, cpuid_t i)
_cpuset_clr(cpuid_t i, cpuset_t *c)
{
const int j = i >> CPUSET_SHIFT;
if (j >= c->size || j < 0)
if (j >= cpuset_nentries || j < 0) {
errno = EINVAL;
return -1;
}
c->bits[j] &= ~(1 << (i & CPUSET_MASK));
return 0;
}
@ -114,78 +136,121 @@ _cpuset_clr(cpuset_t *c, cpuid_t i)
cpuset_t *
_cpuset_create(void)
{
cpuset_t s, *c;
#ifdef _KERNEL
s.size = CPUSET_SIZE(MAXCPUS);
c = kmem_zalloc(_cpuset_size(&s), KM_SLEEP);
#else
static int mib[2] = { CTL_HW, HW_NCPU };
size_t len;
int nc;
if (sysctl(mib, __arraycount(mib), &nc, &len, NULL, 0) == -1)
return NULL;
s.size = CPUSET_SIZE((unsigned int)nc);
c = calloc(1, _cpuset_size(&s));
#endif
if (c != NULL) {
c->next = NULL;
c->nused = 1;
c->size = s.size;
if (cpuset_size == 0) {
static int mib[2] = { CTL_HW, HW_NCPU };
size_t len;
int nc;
if (sysctl(mib, __arraycount(mib), &nc, &len, NULL, 0) == -1)
return NULL;
cpuset_nentries = nc;
cpuset_size = CPUSET_SIZE();
}
return c;
return calloc(1, cpuset_size);
}
void
_cpuset_destroy(cpuset_t *c)
{
#ifdef _KERNEL
free(c);
}
#else
kcpuset_t *
kcpuset_create(void)
{
kcpuset_t *c;
if (cpuset_size == 0) {
cpuset_nentries = CPUSET_NENTRIES(MAXCPUS);
cpuset_size = KCPUSET_SIZE();
}
c = kmem_zalloc(cpuset_size, KM_SLEEP);
c->next = NULL;
c->nused = 1;
return c;
}
void
kcpuset_destroy(kcpuset_t *c)
{
kcpuset_t *nc;
while (c) {
KASSERT(c->nused == 0);
kmem_free(c, _cpuset_size(c));
c = c->next;
nc = c->next;
kmem_free(c, cpuset_size);
c = nc;
}
#else
free(c);
#endif
}
#ifdef _KERNEL
size_t
kcpuset_nused(const cpuset_t *c)
{
return c->nused;
}
void
kcpuset_copy(cpuset_t *d, const cpuset_t *s)
kcpuset_copy(kcpuset_t *d, const kcpuset_t *s)
{
KASSERT(d->size == s->size);
KASSERT(d->nused == 1);
(void)memcpy(d->bits, s->bits, d->size * sizeof(d->bits[0]));
(void)memcpy(d->bits, s->bits, cpuset_nentries * sizeof(s->bits[0]));
}
void
kcpuset_use(cpuset_t *c)
kcpuset_use(kcpuset_t *c)
{
atomic_inc_uint(&c->nused);
}
void
kcpuset_unuse(cpuset_t *c, cpuset_t **lst)
kcpuset_unuse(kcpuset_t *c, kcpuset_t **lst)
{
if (atomic_dec_uint_nv(&c->nused) != 0)
return;
KASSERT(c->nused > 0);
KASSERT(c->nused == 0);
KASSERT(c->next == NULL);
if (lst == NULL) {
_cpuset_destroy(c);
kcpuset_destroy(c);
return;
}
c->next = *lst;
*lst = c;
}
int
kcpuset_copyin(const cpuset_t *u, kcpuset_t *k, size_t len)
{
KASSERT(k->nused > 0);
KASSERT(k->next == NULL);
if (len != CPUSET_SIZE())
return EINVAL;
return copyin(u->bits, k->bits, cpuset_nentries * sizeof(k->bits[0]));
}
int
kcpuset_copyout(const kcpuset_t *k, cpuset_t *u, size_t len)
{
KASSERT(k->nused > 0);
KASSERT(k->next == NULL);
if (len != CPUSET_SIZE())
return EINVAL;
return copyout(k->bits, u->bits, cpuset_nentries * sizeof(u->bits[0]));
}
void
kcpuset_zero(kcpuset_t *c)
{
KASSERT(c->nused > 0);
KASSERT(c->next == NULL);
(void)memset(c->bits, 0, cpuset_nentries * sizeof(c->bits[0]));
}
int
kcpuset_isset(cpuid_t i, const kcpuset_t *c)
{
const int j = i >> CPUSET_SHIFT;
KASSERT(c->nused > 0);
KASSERT(c->next == NULL);
if (j >= cpuset_nentries || j < 0)
return -1;
return ((1 << (i & CPUSET_MASK)) & c->bits[j]) != 0;
}
#endif
#endif