-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #828 from no92/cpu-memes
- Loading branch information
Showing
17 changed files
with
336 additions
and
170 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,13 @@ | ||
#ifndef _MLIBC_INTERNAL_CPU_SET_H | ||
#define _MLIBC_INTERNAL_CPU_SET_H | ||
|
||
typedef unsigned long __cpu_mask; | ||
|
||
#define CPU_SETSIZE 1024 | ||
#define __NCPUBITS (8 * sizeof(__cpu_mask)) | ||
|
||
typedef struct { | ||
__cpu_mask __bits[CPU_SETSIZE / __NCPUBITS]; | ||
} cpu_set_t; | ||
|
||
#endif /* _MLIBC_INTERNAL_CPU_SET_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,71 @@ | ||
#include <limits.h> | ||
#include <sched.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
cpu_set_t *__mlibc_cpu_alloc(int num_cpus) { | ||
return reinterpret_cast<cpu_set_t *>(calloc(1, CPU_ALLOC_SIZE(num_cpus))); | ||
} | ||
|
||
#define CPU_MASK_BITS (CHAR_BIT * sizeof(__cpu_mask)) | ||
|
||
size_t __mlibc_cpu_alloc_size(int num_cpus) { | ||
/* calculate the (unaligned) remainder that doesn't neatly fit in one __cpu_mask; 0 or 1 */ | ||
size_t remainder = ((num_cpus % CPU_MASK_BITS) + CPU_MASK_BITS - 1) / CPU_MASK_BITS; | ||
return sizeof(__cpu_mask) * (num_cpus / CPU_MASK_BITS + remainder); | ||
} | ||
|
||
void __mlibc_cpu_zero(const size_t setsize, cpu_set_t *set) { | ||
memset(set, 0, CPU_ALLOC_SIZE(setsize)); | ||
} | ||
|
||
void __mlibc_cpu_set(const int cpu, const size_t setsize, cpu_set_t *set) { | ||
if(cpu >= static_cast<int>(setsize * CHAR_BIT)) { | ||
return; | ||
} | ||
|
||
unsigned char *ptr = reinterpret_cast<unsigned char *>(set); | ||
size_t off = cpu / CHAR_BIT; | ||
size_t mask = 1 << (cpu % CHAR_BIT); | ||
|
||
ptr[off] |= mask; | ||
} | ||
|
||
void __mlibc_cpu_clear(const int cpu, const size_t setsize, cpu_set_t *set) { | ||
if(cpu >= static_cast<int>(setsize * CHAR_BIT)) { | ||
return; | ||
} | ||
|
||
unsigned char *ptr = reinterpret_cast<unsigned char *>(set); | ||
size_t off = cpu / CHAR_BIT; | ||
size_t mask = 1 << (cpu % CHAR_BIT); | ||
|
||
ptr[off] &= ~mask; | ||
} | ||
|
||
|
||
int __mlibc_cpu_isset(const int cpu, const size_t setsize, const cpu_set_t *set) { | ||
if(cpu >= static_cast<int>(setsize * CHAR_BIT)) { | ||
return false; | ||
} | ||
|
||
const unsigned char *ptr = reinterpret_cast<const unsigned char *>(set); | ||
size_t off = cpu / CHAR_BIT; | ||
size_t mask = 1 << (cpu % CHAR_BIT); | ||
|
||
return (ptr[off] & mask); | ||
} | ||
|
||
int __mlibc_cpu_count(const size_t setsize, const cpu_set_t *set) { | ||
size_t count = 0; | ||
const unsigned char *ptr = reinterpret_cast<const unsigned char *>(set); | ||
|
||
for(size_t i = 0; i < setsize; i++) { | ||
for(size_t bit = 0; bit < CHAR_BIT; bit++) { | ||
if((1 << bit) & ptr[i]) | ||
count++; | ||
} | ||
} | ||
|
||
return count; | ||
} |
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,45 @@ | ||
#ifndef _LINUX_CPU_SET_H | ||
#define _LINUX_CPU_SET_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <bits/cpu_set.h> | ||
#include <bits/size_t.h> | ||
#include <limits.h> | ||
#include <stdlib.h> | ||
|
||
cpu_set_t *__mlibc_cpu_alloc(int num_cpus); | ||
size_t __mlibc_cpu_alloc_size(int num_cpus); | ||
|
||
void __mlibc_cpu_zero(const size_t setsize, cpu_set_t *set); | ||
void __mlibc_cpu_set(const int cpu, const size_t setsize, cpu_set_t *set); | ||
void __mlibc_cpu_clear(const int cpu, const size_t setsize, cpu_set_t *set); | ||
int __mlibc_cpu_isset(const int cpu, const size_t setsize, const cpu_set_t *set); | ||
int __mlibc_cpu_count(const size_t setsize, const cpu_set_t *set); | ||
|
||
#define CPU_ALLOC_SIZE(n) __mlibc_cpu_alloc_size((n)) | ||
#define CPU_ALLOC(n) __mlibc_cpu_alloc((n)) | ||
#define CPU_FREE(set) free((set)) | ||
|
||
#define CPU_ZERO_S(setsize, set) __mlibc_cpu_zero((setsize), (set)) | ||
#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set) | ||
|
||
#define CPU_SET_S(cpu, setsize, set) __mlibc_cpu_set((cpu), (setsize), (set)) | ||
#define CPU_SET(cpu, set) CPU_ZERO_S(cpu, sizeof(cpu_set_t), set) | ||
|
||
#define CPU_CLR_S(cpu, setsize, set) __mlibc_cpu_clear((cpu), (setsize), (set)) | ||
#define CPU_CLR(cpu, set) CPU_CLR_S(cpu, sizeof(cpu_set_t), set) | ||
|
||
#define CPU_ISSET_S(cpu, setsize, set) __mlibc_cpu_isset((cpu), (setsize), (set)) | ||
#define CPU_ISSET(cpu, set) CPU_ISSET_S(cpu, sizeof(cpu_set_t), set) | ||
|
||
#define CPU_COUNT_S(setsize, set) __mlibc_cpu_count((setsize), (set)) | ||
#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set) | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* _LINUX_CPU_SET_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
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
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
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
Oops, something went wrong.