-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cpu affinity and socket partitioning
- Loading branch information
Showing
20 changed files
with
1,137 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* bitset.h -- Dynamic bitset. | ||
* | ||
* Copyright (c) 2001-2020, NLnet Labs. All rights reserved. | ||
* | ||
* See LICENSE for the license. | ||
* | ||
*/ | ||
#include "config.h" | ||
#include "bitset.h" | ||
|
||
#include <assert.h> | ||
#include <limits.h> | ||
#include <string.h> | ||
|
||
size_t nsd_bitset_size(size_t bits) | ||
{ | ||
if(bits == 0) | ||
bits++; | ||
|
||
return (bits / CHAR_BIT) + ((bits % CHAR_BIT) != 0) + sizeof(size_t); | ||
} | ||
|
||
void nsd_bitset_zero(struct nsd_bitset *bset) | ||
{ | ||
size_t sz; | ||
|
||
assert(bset != NULL); | ||
|
||
sz = nsd_bitset_size(bset->size) - sizeof(bset->size); | ||
assert(sz > 0); | ||
memset(bset->bits, 0, sz); | ||
} | ||
|
||
void nsd_bitset_init(struct nsd_bitset *bset, size_t bits) | ||
{ | ||
assert(bset != NULL); | ||
if (bits == 0) | ||
bits++; | ||
|
||
bset->size = bits; | ||
nsd_bitset_zero(bset); | ||
} | ||
|
||
int nsd_bitset_isset(struct nsd_bitset *bset, size_t bit) | ||
{ | ||
assert(bset != NULL); | ||
if(bit >= bset->size) | ||
return 0; | ||
|
||
return (bset->bits[ (bit / CHAR_BIT) ] & (1 << (bit % CHAR_BIT))) != 0; | ||
} | ||
|
||
void nsd_bitset_set(struct nsd_bitset *bset, size_t bit) | ||
{ | ||
assert(bset != NULL); | ||
assert(bset->size > bit); | ||
bset->bits[ (bit / CHAR_BIT) ] |= (1 << (bit % CHAR_BIT)); | ||
} | ||
|
||
void nsd_bitset_unset(struct nsd_bitset *bset, size_t bit) | ||
{ | ||
assert(bset != NULL); | ||
assert(bset->size > bit); | ||
bset->bits[ (bit / CHAR_BIT) ] &= ~(1 << (bit % CHAR_BIT)); | ||
} | ||
|
||
void nsd_bitset_or( | ||
struct nsd_bitset *destset, | ||
struct nsd_bitset *srcset1, | ||
struct nsd_bitset *srcset2) | ||
{ | ||
size_t i, n, size, bytes; | ||
unsigned char bits; | ||
unsigned int mask; | ||
|
||
assert(destset != NULL); | ||
assert(srcset1 != NULL); | ||
assert(srcset2 != NULL); | ||
|
||
size = destset->size; | ||
bytes = (size / CHAR_BIT) + ((size % CHAR_BIT) != 0); | ||
|
||
for(i = 0; i < bytes; i++) { | ||
bits = 0; | ||
|
||
n = (srcset1->size / CHAR_BIT); | ||
if (n > i) { | ||
bits |= srcset1->bits[i]; | ||
} else { | ||
n += ((srcset1->size % CHAR_BIT) != 0); | ||
mask = (1 << ((srcset1->size % CHAR_BIT) + 1)) - 1; | ||
if (n > i) { | ||
bits |= (srcset1->bits[i] & mask); | ||
} | ||
} | ||
n = (srcset2->size / CHAR_BIT); | ||
if (n > i) { | ||
bits |= srcset2->bits[i]; | ||
} else { | ||
n += ((srcset2->size % CHAR_BIT) != 0); | ||
mask = (1 << ((srcset2->size % CHAR_BIT) + 1)) - 1; | ||
if (n > i) { | ||
bits |= (srcset2->bits[i] & mask); | ||
} | ||
} | ||
destset->bits[i] = bits; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* bitset.h -- Dynamic bitset. | ||
* | ||
* Copyright (c) 2001-2020, NLnet Labs. All rights reserved. | ||
* | ||
* See LICENSE for the license. | ||
* | ||
*/ | ||
#ifndef _BITSET_H_ | ||
#define _BITSET_H_ | ||
|
||
#include <assert.h> | ||
#include <limits.h> | ||
#include <string.h> | ||
|
||
typedef struct nsd_bitset nsd_bitset_type; | ||
|
||
struct nsd_bitset { | ||
size_t size; /** Number of available bits in the set */ | ||
unsigned char bits[]; | ||
}; | ||
|
||
size_t nsd_bitset_size(size_t bits); | ||
|
||
void nsd_bitset_zero(struct nsd_bitset *bset); | ||
|
||
void nsd_bitset_init(struct nsd_bitset *bset, size_t bits); | ||
|
||
int nsd_bitset_isset(struct nsd_bitset *bset, size_t bit); | ||
|
||
void nsd_bitset_set(struct nsd_bitset *bset, size_t bit); | ||
|
||
void nsd_bitset_unset(struct nsd_bitset *bset, size_t bit); | ||
|
||
void nsd_bitset_or( | ||
struct nsd_bitset *destset, | ||
struct nsd_bitset *srcset1, | ||
struct nsd_bitset *srcset2); | ||
|
||
#endif /* _BITSET_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* cpuset.c -- CPU affinity. | ||
* | ||
* Copyright (c) 2020, NLnet Labs. All rights reserved. | ||
* | ||
* See LICENSE for the license. | ||
* | ||
*/ | ||
#include "cpuset.h" | ||
|
||
#include <stdlib.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
|
||
#ifndef HAVE_CPUSET_CREATE | ||
#if __linux__ || __FreeBSD__ | ||
cpuset_t *cpuset_create(void) | ||
{ | ||
cpuset_t *set = calloc(1, sizeof(*set)); | ||
return set; | ||
} | ||
#endif | ||
#endif /* !HAVE_CPUSET_CREATE */ | ||
|
||
#ifndef HAVE_CPUSET_DESTROY | ||
#if __linux__ || __FreeBSD__ | ||
void cpuset_destroy(cpuset_t *set) | ||
{ | ||
free(set); | ||
} | ||
#endif | ||
#endif /* !HAVE_CPUSET_DESTROY */ | ||
|
||
#ifndef HAVE_CPUSET_ZERO | ||
#if __linux__ || __FreeBSD__ | ||
void cpuset_zero(cpuset_t *set) | ||
{ | ||
CPU_ZERO(set); | ||
} | ||
#endif | ||
#endif /* !HAVE_CPUSET_ZERO */ | ||
|
||
#ifndef HAVE_CPUSET_SET | ||
#if __linux__ || __FreeBSD__ | ||
int cpuset_set(cpuid_t cpu, cpuset_t *set) | ||
{ | ||
CPU_SET(cpu, set); | ||
return 0; | ||
} | ||
#endif | ||
#endif /* !HAVE_CPUSET_SET */ | ||
|
||
#ifndef HAVE_CPUSET_CLR | ||
#if __linux__ || __FreeBSD__ | ||
int cpuset_clr(cpuid_t cpu, cpuset_t *set) | ||
{ | ||
CPU_CLR(cpu, set); | ||
return 0; | ||
} | ||
#endif | ||
#endif /* !HAVE_CPUSET_CLR */ | ||
|
||
#ifndef HAVE_CPUSET_ISSET | ||
#if __linux__ || __FreeBSD__ | ||
int cpuset_isset(cpuid_t cpu, const cpuset_t *set) | ||
{ | ||
return CPU_ISSET(cpu, set); | ||
} | ||
#endif | ||
#endif /* !HAVE_CPUSET_ISSET */ | ||
|
||
#ifndef HAVE_CPUSET_SIZE | ||
#if __linux__ || __FreeBSD__ | ||
size_t cpuset_size(const cpuset_t *set) | ||
{ | ||
return sizeof(*set); | ||
} | ||
#endif | ||
#endif /* !HAVE_CPUSET_SIZE */ | ||
|
||
#if __linux__ | ||
void cpuset_or(cpuset_t *destset, const cpuset_t *srcset) | ||
{ | ||
CPU_OR(destset, destset, srcset); | ||
} | ||
#endif | ||
#if __FreeBSD__ | ||
void cpuset_or(cpuset_t *destset, const cpuset_t *srcset) | ||
{ | ||
CPU_OR(destset, srcset); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* cpuset.h -- CPU affinity. | ||
* | ||
* Copyright (c) 2020, NLnet Labs. All rights reserved. | ||
* | ||
* See LICENSE for the license. | ||
* | ||
*/ | ||
#ifndef _CPUSET_H_ | ||
#define _CPUSET_H_ | ||
#include "config.h" | ||
|
||
#ifdef HAVE_SCHED_H | ||
# include <sched.h> | ||
#endif | ||
|
||
#ifdef HAVE_SYS_CPUSET_H | ||
# include <sys/cpuset.h> | ||
#endif | ||
|
||
/* | ||
* CPU affinity is currently only supported on Linux and FreeBSD. Other | ||
* operating systems may be supported in the future, but not all operating | ||
* systems offer the same functionality. OpenBSD for example does not support | ||
* any kind of CPU affinity, while Solaris offers specifying a set of | ||
* processors, but a processor can only be part of a single set. | ||
* | ||
* NOTE: On macOS Mojave, processor_set_create returned KERN_FAILURE which | ||
* indicates processor allocation is not supported by the operating | ||
* system. | ||
*/ | ||
|
||
#ifndef HAVE_CPUSET_T | ||
#ifdef HAVE_CPU_SET_T | ||
#define HAVE_CPUSET_T 1 | ||
typedef cpu_set_t cpuset_t; | ||
#endif | ||
#endif | ||
|
||
#ifndef HAVE_CPUID_T | ||
#ifdef __linux__ | ||
typedef int cpuid_t; | ||
#endif | ||
#ifdef __FreeBSD__ | ||
typedef size_t cpuid_t; | ||
#endif | ||
#endif | ||
|
||
#ifndef HAVE_CPUSET_CREATE | ||
cpuset_t *cpuset_create(void); | ||
#endif | ||
|
||
#ifndef HAVE_CPUSET_DESTROY | ||
void cpuset_destroy(cpuset_t *set); | ||
#endif | ||
|
||
#ifndef HAVE_CPUSET_ZERO | ||
void cpuset_zero(cpuset_t *set); | ||
#endif | ||
|
||
#ifndef HAVE_CPUSET_SET | ||
int cpuset_set(cpuid_t cpu, cpuset_t *set); | ||
#endif | ||
|
||
#ifndef HAVE_CPUSET_CLR | ||
int cpuset_clr(cpuid_t cpu, cpuset_t *set); | ||
#endif | ||
|
||
#ifndef HAVE_CPUSET_ISSET | ||
int cpuset_isset(cpuid_t cpu, const cpuset_t *set); | ||
#endif | ||
|
||
#ifndef HAVE_CPUSET_SIZE | ||
size_t cpuset_size(const cpuset_t *set); | ||
#endif | ||
|
||
void cpuset_or(cpuset_t *destset, const cpuset_t *srcset); | ||
|
||
#endif /* _CPUSET_H_ */ |
Oops, something went wrong.