HelenOS sources
This source file includes following definitions.
- slab_space_alloc
- slab_space_free
- obj2slab
- slab_obj_destroy
- slab_obj_create
- get_mag_from_cache
- put_mag_to_cache
- magazine_destroy
- get_full_current_mag
- magazine_obj_get
- make_empty_current_mag
- magazine_obj_put
- comp_objects
- badness
- make_magcache
- _slab_cache_create
- slab_cache_create
- _slab_reclaim
- _slab_free
- slab_cache_destroy
- slab_alloc
- slab_free
- slab_reclaim
- slab_print_list
- slab_cache_init
- slab_enable_cpucache
#include <assert.h>
#include <errno.h>
#include <synch/spinlock.h>
#include <mm/slab.h>
#include <adt/list.h>
#include <memw.h>
#include <align.h>
#include <mm/frame.h>
#include <config.h>
#include <stdio.h>
#include <arch.h>
#include <panic.h>
#include <bitops.h>
#include <macros.h>
#include <cpu.h>
#include <stdlib.h>
IRQ_SPINLOCK_STATIC_INITIALIZE(slab_cache_lock);
static LIST_INITIALIZE(slab_cache_list);
static slab_cache_t mag_cache;
static slab_cache_t slab_cache_cache;
static slab_cache_t slab_mag_cache;
static slab_cache_t *slab_extern_cache;
typedef struct {
slab_cache_t *cache;
link_t link;
void *start;
size_t available;
size_t nextavail;
} slab_t;
#ifdef CONFIG_DEBUG
static unsigned int _slab_initialized = 0;
#endif
_NO_TRACE static slab_t *slab_space_alloc(slab_cache_t *cache,
unsigned int flags)
{
size_t zone = 0;
uintptr_t data_phys =
frame_alloc_generic(cache->frames, FRAME_LOWMEM | flags, 0, &zone);
if (!data_phys)
return NULL;
void *data = (void *) PA2KA(data_phys);
slab_t *slab;
size_t fsize;
if (!(cache->flags & SLAB_CACHE_SLINSIDE)) {
slab = slab_alloc(slab_extern_cache, flags);
if (!slab) {
frame_free(KA2PA(data), cache->frames);
return NULL;
}
} else {
fsize = FRAMES2SIZE(cache->frames);
slab = data + fsize - sizeof(*slab);
}
size_t i;
for (i = 0; i < cache->frames; i++)
frame_set_parent(ADDR2PFN(KA2PA(data)) + i, slab, zone);
slab->start = data;
slab->available = cache->objects;
slab->nextavail = 0;
slab->cache = cache;
for (i = 0; i < cache->objects; i++)
*((size_t *) (slab->start + i * cache->size)) = i + 1;
atomic_inc(&cache->allocated_slabs);
return slab;
}
_NO_TRACE static size_t slab_space_free(slab_cache_t *cache, slab_t *slab)
{
frame_free(KA2PA(slab->start), slab->cache->frames);
if (!(cache->flags & SLAB_CACHE_SLINSIDE))
slab_free(slab_extern_cache, slab);
atomic_dec(&cache->allocated_slabs);
return cache->frames;
}
_NO_TRACE static slab_t *obj2slab(void *obj)
{
return (slab_t *) frame_get_parent(ADDR2PFN(KA2PA(obj)), 0);
}
_NO_TRACE static size_t slab_obj_destroy(slab_cache_t *cache, void *obj,
slab_t *slab)
{
if (!slab)
slab = obj2slab(obj);
assert(slab->cache == cache);
size_t freed = 0;
if (cache->destructor)
freed = cache->destructor(obj);
irq_spinlock_lock(&cache->slablock, true);
assert(slab->available < cache->objects);
*((size_t *) obj) = slab->nextavail;
slab->nextavail = (obj - slab->start) / cache->size;
slab->available++;
if (slab->available == cache->objects) {
list_remove(&slab->link);
irq_spinlock_unlock(&cache->slablock, true);
return freed + slab_space_free(cache, slab);
} else if (slab->available == 1) {
list_remove(&slab->link);
list_prepend(&slab->link, &cache->partial_slabs);
}
irq_spinlock_unlock(&cache->slablock, true);
return freed;
}
_NO_TRACE static void *slab_obj_create(slab_cache_t *cache, unsigned int flags)
{
irq_spinlock_lock(&cache->slablock, true);
slab_t *slab;
if (list_empty(&cache->partial_slabs)) {
irq_spinlock_unlock(&cache->slablock, true);
slab = slab_space_alloc(cache, flags);
if (!slab)
return NULL;
irq_spinlock_lock(&cache->slablock, true);
} else {
slab = list_get_instance(list_first(&cache->partial_slabs),
slab_t, link);
list_remove(&slab->link);
}
void *obj = slab->start + slab->nextavail * cache->size;
slab->nextavail = *((size_t *) obj);
slab->available--;
if (!slab->available)
list_prepend(&slab->link, &cache->full_slabs);
else
list_prepend(&slab->link, &cache->partial_slabs);
irq_spinlock_unlock(&cache->slablock, true);
if ((cache->constructor) && (cache->constructor(obj, flags) != EOK)) {
slab_obj_destroy(cache, obj, slab);
return NULL;
}
return obj;
}
_NO_TRACE static slab_magazine_t *get_mag_from_cache(slab_cache_t *cache,
bool first)
{
slab_magazine_t *mag = NULL;
link_t *cur;
irq_spinlock_lock(&cache->maglock, true);
if (!list_empty(&cache->magazines)) {
if (first)
cur = list_first(&cache->magazines);
else
cur = list_last(&cache->magazines);
mag = list_get_instance(cur, slab_magazine_t, link);
list_remove(&mag->link);
atomic_dec(&cache->magazine_counter);
}
irq_spinlock_unlock(&cache->maglock, true);
return mag;
}
_NO_TRACE static void put_mag_to_cache(slab_cache_t *cache,
slab_magazine_t *mag)
{
irq_spinlock_lock(&cache->maglock, true);
list_prepend(&mag->link, &cache->magazines);
atomic_inc(&cache->magazine_counter);
irq_spinlock_unlock(&cache->maglock, true);
}
_NO_TRACE static size_t magazine_destroy(slab_cache_t *cache,
slab_magazine_t *mag)
{
size_t i;
size_t frames = 0;
for (i = 0; i < mag->busy; i++) {
frames += slab_obj_destroy(cache, mag->objs[i], NULL);
atomic_dec(&cache->cached_objs);
}
slab_free(&mag_cache, mag);
return frames;
}
_NO_TRACE static slab_magazine_t *get_full_current_mag(slab_cache_t *cache)
{
slab_magazine_t *cmag = cache->mag_cache[CPU->id].current;
slab_magazine_t *lastmag = cache->mag_cache[CPU->id].last;
assert(irq_spinlock_locked(&cache->mag_cache[CPU->id].lock));
if (cmag) {
if (cmag->busy)
return cmag;
if ((lastmag) && (lastmag->busy)) {
cache->mag_cache[CPU->id].current = lastmag;
cache->mag_cache[CPU->id].last = cmag;
return lastmag;
}
}
slab_magazine_t *newmag = get_mag_from_cache(cache, 1);
if (!newmag)
return NULL;
if (lastmag)
magazine_destroy(cache, lastmag);
cache->mag_cache[CPU->id].last = cmag;
cache->mag_cache[CPU->id].current = newmag;
return newmag;
}
_NO_TRACE static void *magazine_obj_get(slab_cache_t *cache)
{
if (!CPU)
return NULL;
irq_spinlock_lock(&cache->mag_cache[CPU->id].lock, true);
slab_magazine_t *mag = get_full_current_mag(cache);
if (!mag) {
irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
return NULL;
}
void *obj = mag->objs[--mag->busy];
irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
atomic_dec(&cache->cached_objs);
return obj;
}
_NO_TRACE static slab_magazine_t *make_empty_current_mag(slab_cache_t *cache)
{
slab_magazine_t *cmag = cache->mag_cache[CPU->id].current;
slab_magazine_t *lastmag = cache->mag_cache[CPU->id].last;
assert(irq_spinlock_locked(&cache->mag_cache[CPU->id].lock));
if (cmag) {
if (cmag->busy < cmag->size)
return cmag;
if ((lastmag) && (lastmag->busy < lastmag->size)) {
cache->mag_cache[CPU->id].last = cmag;
cache->mag_cache[CPU->id].current = lastmag;
return lastmag;
}
}
slab_magazine_t *newmag = slab_alloc(&mag_cache,
FRAME_ATOMIC | FRAME_NO_RECLAIM);
if (!newmag)
return NULL;
newmag->size = SLAB_MAG_SIZE;
newmag->busy = 0;
if (lastmag)
put_mag_to_cache(cache, lastmag);
cache->mag_cache[CPU->id].last = cmag;
cache->mag_cache[CPU->id].current = newmag;
return newmag;
}
_NO_TRACE static int magazine_obj_put(slab_cache_t *cache, void *obj)
{
if (!CPU)
return -1;
irq_spinlock_lock(&cache->mag_cache[CPU->id].lock, true);
slab_magazine_t *mag = make_empty_current_mag(cache);
if (!mag) {
irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
return -1;
}
mag->objs[mag->busy++] = obj;
irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
atomic_inc(&cache->cached_objs);
return 0;
}
_NO_TRACE static size_t comp_objects(slab_cache_t *cache)
{
if (cache->flags & SLAB_CACHE_SLINSIDE)
return (FRAMES2SIZE(cache->frames) - sizeof(slab_t)) /
cache->size;
else
return FRAMES2SIZE(cache->frames) / cache->size;
}
_NO_TRACE static size_t badness(slab_cache_t *cache)
{
size_t objects = comp_objects(cache);
size_t ssize = FRAMES2SIZE(cache->frames);
if (cache->flags & SLAB_CACHE_SLINSIDE)
ssize -= sizeof(slab_t);
return ssize - objects * cache->size;
}
_NO_TRACE static bool make_magcache(slab_cache_t *cache)
{
assert(_slab_initialized >= 2);
cache->mag_cache = slab_alloc(&slab_mag_cache, FRAME_ATOMIC);
if (!cache->mag_cache)
return false;
size_t i;
for (i = 0; i < config.cpu_count; i++) {
memsetb(&cache->mag_cache[i], sizeof(cache->mag_cache[i]), 0);
irq_spinlock_initialize(&cache->mag_cache[i].lock,
"slab.cache.mag_cache[].lock");
}
return true;
}
_NO_TRACE static void _slab_cache_create(slab_cache_t *cache, const char *name,
size_t size, size_t align, errno_t (*constructor)(void *obj,
unsigned int kmflag), size_t (*destructor)(void *obj), unsigned int flags)
{
assert(size > 0);
memsetb(cache, sizeof(*cache), 0);
cache->name = name;
if (align < sizeof(sysarg_t))
align = sizeof(sysarg_t);
size = ALIGN_UP(size, align);
cache->size = size;
cache->constructor = constructor;
cache->destructor = destructor;
cache->flags = flags;
list_initialize(&cache->full_slabs);
list_initialize(&cache->partial_slabs);
list_initialize(&cache->magazines);
irq_spinlock_initialize(&cache->slablock, "slab.cache.slablock");
irq_spinlock_initialize(&cache->maglock, "slab.cache.maglock");
if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
(void) make_magcache(cache);
if (cache->size < SLAB_INSIDE_SIZE)
cache->flags |= SLAB_CACHE_SLINSIDE;
cache->frames = SIZE2FRAMES(cache->size);
while (badness(cache) > SLAB_MAX_BADNESS(cache))
cache->frames <<= 1;
cache->objects = comp_objects(cache);
if (badness(cache) > sizeof(slab_t))
cache->flags |= SLAB_CACHE_SLINSIDE;
irq_spinlock_lock(&slab_cache_lock, true);
list_append(&cache->link, &slab_cache_list);
irq_spinlock_unlock(&slab_cache_lock, true);
}
slab_cache_t *slab_cache_create(const char *name, size_t size, size_t align,
errno_t (*constructor)(void *obj, unsigned int kmflag),
size_t (*destructor)(void *obj), unsigned int flags)
{
slab_cache_t *cache = slab_alloc(&slab_cache_cache, FRAME_ATOMIC);
if (!cache)
panic("Not enough memory to allocate slab cache %s.", name);
_slab_cache_create(cache, name, size, align, constructor, destructor,
flags);
return cache;
}
_NO_TRACE static size_t _slab_reclaim(slab_cache_t *cache, unsigned int flags)
{
if (cache->flags & SLAB_CACHE_NOMAGAZINE)
return 0;
size_t magcount = atomic_load(&cache->magazine_counter);
slab_magazine_t *mag;
size_t frames = 0;
while ((magcount--) && (mag = get_mag_from_cache(cache, 0))) {
frames += magazine_destroy(cache, mag);
if ((!(flags & SLAB_RECLAIM_ALL)) && (frames))
break;
}
if (flags & SLAB_RECLAIM_ALL) {
size_t i;
for (i = 0; i < config.cpu_count; i++) {
irq_spinlock_lock(&cache->mag_cache[i].lock, true);
mag = cache->mag_cache[i].current;
if (mag)
frames += magazine_destroy(cache, mag);
cache->mag_cache[i].current = NULL;
mag = cache->mag_cache[i].last;
if (mag)
frames += magazine_destroy(cache, mag);
cache->mag_cache[i].last = NULL;
irq_spinlock_unlock(&cache->mag_cache[i].lock, true);
}
}
return frames;
}
_NO_TRACE static void _slab_free(slab_cache_t *cache, void *obj, slab_t *slab)
{
if (!obj)
return;
ipl_t ipl = interrupts_disable();
if ((cache->flags & SLAB_CACHE_NOMAGAZINE) ||
(magazine_obj_put(cache, obj)))
slab_obj_destroy(cache, obj, slab);
interrupts_restore(ipl);
atomic_dec(&cache->allocated_objs);
}
void slab_cache_destroy(slab_cache_t *cache)
{
irq_spinlock_lock(&slab_cache_lock, true);
list_remove(&cache->link);
irq_spinlock_unlock(&slab_cache_lock, true);
_slab_reclaim(cache, SLAB_RECLAIM_ALL);
if ((!list_empty(&cache->full_slabs)) ||
(!list_empty(&cache->partial_slabs)))
panic("Destroying cache that is not empty.");
if (!(cache->flags & SLAB_CACHE_NOMAGAZINE) && cache->mag_cache) {
slab_free(&slab_mag_cache, cache->mag_cache);
}
slab_free(&slab_cache_cache, cache);
}
void *slab_alloc(slab_cache_t *cache, unsigned int flags)
{
ipl_t ipl = interrupts_disable();
void *result = NULL;
if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
result = magazine_obj_get(cache);
if (!result)
result = slab_obj_create(cache, flags);
interrupts_restore(ipl);
if (result)
atomic_inc(&cache->allocated_objs);
return result;
}
void slab_free(slab_cache_t *cache, void *obj)
{
_slab_free(cache, obj, NULL);
}
size_t slab_reclaim(unsigned int flags)
{
irq_spinlock_lock(&slab_cache_lock, true);
size_t frames = 0;
list_foreach(slab_cache_list, link, slab_cache_t, cache) {
frames += _slab_reclaim(cache, flags);
}
irq_spinlock_unlock(&slab_cache_lock, true);
return frames;
}
void slab_print_list(void)
{
printf("[cache name ] [size ] [pages ] [obj/pg] [slabs ]"
" [cached] [alloc ] [ctl]\n");
size_t skip = 0;
while (true) {
irq_spinlock_lock(&slab_cache_lock, true);
link_t *cur = list_first(&slab_cache_list);
size_t i = 0;
while (i < skip && cur != NULL) {
i++;
cur = list_next(cur, &slab_cache_list);
}
if (cur == NULL) {
irq_spinlock_unlock(&slab_cache_lock, true);
break;
}
skip++;
slab_cache_t *cache = list_get_instance(cur, slab_cache_t, link);
const char *name = cache->name;
size_t frames = cache->frames;
size_t size = cache->size;
size_t objects = cache->objects;
long allocated_slabs = atomic_load(&cache->allocated_slabs);
long cached_objs = atomic_load(&cache->cached_objs);
long allocated_objs = atomic_load(&cache->allocated_objs);
unsigned int flags = cache->flags;
irq_spinlock_unlock(&slab_cache_lock, true);
printf("%-18s %8zu %8zu %8zu %8ld %8ld %8ld %-5s\n",
name, size, frames, objects, allocated_slabs,
cached_objs, allocated_objs,
flags & SLAB_CACHE_SLINSIDE ? "in" : "out");
}
}
void slab_cache_init(void)
{
_slab_cache_create(&mag_cache, "slab_magazine_t",
sizeof(slab_magazine_t) + SLAB_MAG_SIZE * sizeof(void *),
sizeof(uintptr_t), NULL, NULL, SLAB_CACHE_NOMAGAZINE |
SLAB_CACHE_SLINSIDE);
_slab_cache_create(&slab_cache_cache, "slab_cache_cache",
sizeof(slab_cache_cache), sizeof(uintptr_t), NULL, NULL,
SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
slab_extern_cache = slab_cache_create("slab_t", sizeof(slab_t), 0,
NULL, NULL, SLAB_CACHE_SLINSIDE | SLAB_CACHE_MAGDEFERRED);
#ifdef CONFIG_DEBUG
_slab_initialized = 1;
#endif
}
void slab_enable_cpucache(void)
{
#ifdef CONFIG_DEBUG
_slab_initialized = 2;
#endif
_slab_cache_create(&slab_mag_cache, "slab_mag_cache",
sizeof(slab_mag_cache_t) * config.cpu_count, sizeof(uintptr_t),
NULL, NULL, SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
irq_spinlock_lock(&slab_cache_lock, false);
list_foreach(slab_cache_list, link, slab_cache_t, slab) {
if ((slab->flags & SLAB_CACHE_MAGDEFERRED) !=
SLAB_CACHE_MAGDEFERRED)
continue;
(void) make_magcache(slab);
slab->flags &= ~SLAB_CACHE_MAGDEFERRED;
}
irq_spinlock_unlock(&slab_cache_lock, false);
}
HelenOS homepage, sources at GitHub