0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2025-01-03 07:31:54 +00:00

Memory pages allocator is now a global simple lockless structure

This commit is contained in:
Maria Matejka 2022-09-09 20:57:59 +02:00
parent 3d627d09d4
commit b80823fe82
4 changed files with 66 additions and 65 deletions

View File

@ -279,8 +279,8 @@ rlookup(unsigned long a)
void void
resource_init(void) resource_init(void)
{ {
resource_sys_init();
rcu_init(); rcu_init();
resource_sys_init();
root_pool.r.class = &pool_class; root_pool.r.class = &pool_class;
root_pool.name = "Root"; root_pool.name = "Root";

View File

@ -122,6 +122,7 @@ void buffer_realloc(void **buf, unsigned *size, unsigned need, unsigned item_siz
/* Allocator of whole pages; for use in slabs and other high-level allocators. */ /* Allocator of whole pages; for use in slabs and other high-level allocators. */
#define PAGE_HEAD(x) ((void *) (((uintptr_t) (x)) & ~(page_size-1))) #define PAGE_HEAD(x) ((void *) (((uintptr_t) (x)) & ~(page_size-1)))
extern long page_size; extern long page_size;
extern _Atomic int pages_kept;
void *alloc_page(void); void *alloc_page(void);
void free_page(void *); void free_page(void *);

View File

@ -109,7 +109,6 @@ print_size(char *dsc, struct resmem vals)
extern pool *rt_table_pool; extern pool *rt_table_pool;
extern pool *rta_pool; extern pool *rta_pool;
extern uint *pages_kept;
void void
cmd_show_memory(void) cmd_show_memory(void)
@ -121,8 +120,9 @@ cmd_show_memory(void)
print_size("Protocols:", rmemsize(proto_pool)); print_size("Protocols:", rmemsize(proto_pool));
struct resmem total = rmemsize(&root_pool); struct resmem total = rmemsize(&root_pool);
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
print_size("Standby memory:", (struct resmem) { .overhead = page_size * *pages_kept }); int pages_kept = atomic_load_explicit(&pages_kept, memory_order_relaxed);
total.overhead += page_size * *pages_kept; print_size("Standby memory:", (struct resmem) { .overhead = page_size * pages_kept });
total.overhead += page_size * pages_kept;
#endif #endif
print_size("Total:", total); print_size("Total:", total);
cli_msg(0, ""); cli_msg(0, "");

View File

@ -10,6 +10,7 @@
#include "lib/resource.h" #include "lib/resource.h"
#include "lib/lists.h" #include "lib/lists.h"
#include "lib/event.h" #include "lib/event.h"
#include "lib/rcu.h"
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
@ -22,41 +23,32 @@
long page_size = 0; long page_size = 0;
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
#define KEEP_PAGES_MAIN_MAX 256 #define KEEP_PAGES_MAX 256
#define KEEP_PAGES_MAIN_MIN 8 #define KEEP_PAGES_MIN 8
#define CLEANUP_PAGES_BULK 256
STATIC_ASSERT(KEEP_PAGES_MAIN_MIN * 4 < KEEP_PAGES_MAIN_MAX); STATIC_ASSERT(KEEP_PAGES_MIN * 4 < KEEP_PAGES_MAX);
static _Bool use_fake = 0; static _Bool use_fake = 0;
static _Bool initialized = 0;
#if DEBUGGING #if DEBUGGING
struct free_page { struct free_page {
node unused[42]; node unused[42];
node n; struct free_page * _Atomic next;
}; };
#else #else
struct free_page { struct free_page {
node n; struct free_page * _Atomic next;
}; };
#endif #endif
struct free_pages { static struct free_page * _Atomic page_stack = NULL;
list pages;
u16 min, max; /* Minimal and maximal number of free pages kept */
uint cnt; /* Number of empty pages */
event cleanup;
};
static void global_free_pages_cleanup_event(void *); static void page_cleanup(void *);
static event page_cleanup_event = { .hook = page_cleanup, };
#define SCHEDULE_CLEANUP do if (initialized && !shutting_down) ev_send(&global_event_list, &page_cleanup_event); while (0)
static struct free_pages global_free_pages = { _Atomic int pages_kept = 0;
.min = KEEP_PAGES_MAIN_MIN,
.max = KEEP_PAGES_MAIN_MAX,
.cleanup = { .hook = global_free_pages_cleanup_event },
};
uint *pages_kept = &global_free_pages.cnt;
static void * static void *
alloc_sys_page(void) alloc_sys_page(void)
@ -90,20 +82,21 @@ alloc_page(void)
} }
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
struct free_pages *fps = &global_free_pages; rcu_read_lock();
struct free_page *fp = atomic_load_explicit(&page_stack, memory_order_acquire);
while (fp && !atomic_compare_exchange_strong_explicit(
&page_stack, &fp, atomic_load_explicit(&fp->next, memory_order_acquire),
memory_order_acq_rel, memory_order_acquire))
;
rcu_read_unlock();
if (fps->cnt) if (!fp)
{ return alloc_sys_page();
struct free_page *fp = SKIP_BACK(struct free_page, n, HEAD(fps->pages));
rem_node(&fp->n);
if ((--fps->cnt < fps->min) && !shutting_down)
ev_send(&global_work_list, &fps->cleanup);
bzero(fp, page_size); if (atomic_fetch_sub_explicit(&pages_kept, 1, memory_order_relaxed) <= KEEP_PAGES_MIN)
return fp; SCHEDULE_CLEANUP;
}
return alloc_sys_page(); return fp;
#endif #endif
} }
@ -117,45 +110,51 @@ free_page(void *ptr)
} }
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
struct free_pages *fps = &global_free_pages; rcu_read_lock();
struct free_page *fp = ptr; struct free_page *fp = ptr;
struct free_page *next = atomic_load_explicit(&page_stack, memory_order_acquire);
fp->n = (node) {}; do atomic_store_explicit(&fp->next, next, memory_order_release);
add_tail(&fps->pages, &fp->n); while (!atomic_compare_exchange_strong_explicit(
&page_stack, &next, fp,
memory_order_acq_rel, memory_order_acquire));
rcu_read_unlock();
if ((++fps->cnt > fps->max) && !shutting_down) if (atomic_fetch_add_explicit(&pages_kept, 1, memory_order_relaxed) >= KEEP_PAGES_MAX)
ev_send(&global_work_list, &fps->cleanup); SCHEDULE_CLEANUP;
#endif #endif
} }
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
static void static void
global_free_pages_cleanup_event(void *data UNUSED) page_cleanup(void *_ UNUSED)
{ {
if (shutting_down) struct free_page *stack = atomic_exchange_explicit(&page_stack, NULL, memory_order_acq_rel);
if (!stack)
return; return;
struct free_pages *fps = &global_free_pages; synchronize_rcu();
while (fps->cnt / 2 < fps->min) do {
{ struct free_page *f = stack;
struct free_page *fp = alloc_sys_page(); stack = atomic_load_explicit(&f->next, memory_order_acquire);
fp->n = (node) {};
add_tail(&fps->pages, &fp->n);
fps->cnt++;
}
for (uint seen = 0; (seen < CLEANUP_PAGES_BULK) && (fps->cnt > fps->max / 2); seen++) if (munmap(f, page_size) == 0)
{ continue;
struct free_page *fp = SKIP_BACK(struct free_page, n, TAIL(fps->pages)); else if (errno != ENOMEM)
rem_node(&fp->n); bug("munmap(%p) failed: %m", f);
if (munmap(fp, page_size) == 0)
fps->cnt--;
else if (errno == ENOMEM)
add_head(&fps->pages, &fp->n);
else else
bug("munmap(%p) failed: %m", fp); free_page(f);
}
while (stack && (atomic_fetch_sub_explicit(&pages_kept, 1, memory_order_relaxed) >= KEEP_PAGES_MAX / 2));
while (stack)
{
atomic_fetch_sub_explicit(&pages_kept, 1, memory_order_relaxed);
struct free_page *f = stack;
stack = atomic_load_explicit(&f->next, memory_order_acquire);
free_page(f);
} }
} }
#endif #endif
@ -164,17 +163,17 @@ void
resource_sys_init(void) resource_sys_init(void)
{ {
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
ASSERT_DIE(global_free_pages.cnt == 0);
if (!(page_size = sysconf(_SC_PAGESIZE))) if (!(page_size = sysconf(_SC_PAGESIZE)))
die("System page size must be non-zero"); die("System page size must be non-zero");
if (u64_popcount(page_size) == 1) if (u64_popcount(page_size) == 1)
{ {
struct free_pages *fps = &global_free_pages;
init_list(&fps->pages); for (int i = 0; i < (KEEP_PAGES_MIN * 2); i++)
global_free_pages_cleanup_event(NULL); free_page(alloc_page());
page_cleanup(NULL);
initialized = 1;
return; return;
} }
@ -184,4 +183,5 @@ resource_sys_init(void)
#endif #endif
page_size = 4096; page_size = 4096;
initialized = 1;
} }