← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
ckernel_alloc.c File Reference
#include "ckernel_alloc.h"
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 
#define HUGE_PAGE_SIZE   (2UL * 1024UL * 1024UL)
 

Functions

static size_t align_up_bytes (size_t n, size_t align)
 
void ck_bump_alloc_free (ck_bump_alloc_t *alloc)
 
int ck_bump_alloc_init (ck_bump_alloc_t *alloc, const char *weights_path, size_t total_size, size_t weights_base, size_t activations_base)
 
int ck_bump_alloc_needs_weight_materialization (const ck_bump_alloc_t *alloc)
 
static void ck_bump_alloc_reset (ck_bump_alloc_t *alloc)
 
void * ck_huge_alloc (size_t bytes)
 
void ck_huge_free (void *ptr, size_t bytes)
 
static ck_huge_alloc_entry_t * detach_allocation (void *ptr)
 
static int env_flag_enabled (const char *name)
 
static int record_allocation (void *ptr, size_t len, int was_mmap)
 

Variables

static ck_huge_alloc_entry_t * g_alloc_list = NULL
 
static pthread_mutex_t g_alloc_mutex = PTHREAD_MUTEX_INITIALIZER
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 1 of file ckernel_alloc.c.

◆ HUGE_PAGE_SIZE

#define HUGE_PAGE_SIZE   (2UL * 1024UL * 1024UL)

Definition at line 18 of file ckernel_alloc.c.

Function Documentation

◆ align_up_bytes()

static size_t align_up_bytes ( size_t  n,
size_t  align 
)
static

Definition at line 31 of file ckernel_alloc.c.

32{
33 if (align == 0) return n;
34 return (n + align - 1) & ~(align - 1);
35}

Referenced by ck_bump_alloc_init(), and ck_huge_alloc().

◆ ck_bump_alloc_free()

void ck_bump_alloc_free ( ck_bump_alloc_t *  alloc)

Definition at line 343 of file ckernel_alloc.c.

344{
345 if (!alloc || !alloc->base) {
346 ck_bump_alloc_reset(alloc);
347 return;
348 }
349
350 if (alloc->mode == CK_BUMP_MODE_MIXED_FILE_BACKED) {
351 if (alloc->mapped_len > 0) {
352 munmap(alloc->base, alloc->mapped_len);
353 }
354 } else if (alloc->mode == CK_BUMP_MODE_ANON) {
355 ck_huge_free(alloc->base, alloc->total_size);
356 }
357
358 ck_bump_alloc_reset(alloc);
359}
static void ck_bump_alloc_reset(ck_bump_alloc_t *alloc)
void ck_huge_free(void *ptr, size_t bytes)
@ CK_BUMP_MODE_ANON
@ CK_BUMP_MODE_MIXED_FILE_BACKED
ck_bump_mode_t mode

References ck_bump_alloc_t::base, ck_bump_alloc_reset(), CK_BUMP_MODE_ANON, CK_BUMP_MODE_MIXED_FILE_BACKED, ck_huge_free(), ck_bump_alloc_t::mapped_len, ck_bump_alloc_t::mode, and ck_bump_alloc_t::total_size.

◆ ck_bump_alloc_init()

int ck_bump_alloc_init ( ck_bump_alloc_t *  alloc,
const char *  weights_path,
size_t  total_size,
size_t  weights_base,
size_t  activations_base 
)

Definition at line 286 of file ckernel_alloc.c.

291{
292 int force_mixed = 0;
293 int disable_mixed = 0;
294
295 if (!alloc || total_size == 0 ||
296 total_size > SIZE_MAX - (HUGE_PAGE_SIZE - 1)) {
297 fprintf(stderr, "ck_bump_alloc_init: invalid arguments\n");
298 return -1;
299 }
300
301 ck_bump_alloc_reset(alloc);
302 alloc->total_size = total_size;
303 alloc->weights_base = weights_base;
304 alloc->activations_base = activations_base;
305 force_mixed = env_flag_enabled("CK_BUMP_FORCE_MIXED");
306 disable_mixed = env_flag_enabled("CK_BUMP_DISABLE_MIXED");
307
308 if (weights_base > activations_base || activations_base > total_size) {
309 fprintf(stderr,
310 "ck_bump_alloc_init: invalid bump layout weights=%zu activations=%zu total=%zu\n",
311 weights_base, activations_base, total_size);
312 ck_bump_alloc_reset(alloc);
313 return -1;
314 }
315
316 if (!force_mixed) {
317 void *anon = ck_huge_alloc(total_size);
318 if (anon) {
319 alloc->base = (uint8_t *)anon;
320 alloc->mapped_len = align_up_bytes(total_size, HUGE_PAGE_SIZE);
321 alloc->mode = CK_BUMP_MODE_ANON;
322 return 0;
323 }
324 }
325
326#ifdef __linux__
327 if (!disable_mixed) {
328 if (ck_bump_alloc_try_mixed(alloc, weights_path) == 0) {
329 return 0;
330 }
331 }
332#else
333 (void)weights_path;
334#endif
335
336 fprintf(stderr,
337 "ck_bump_alloc_init: failed to allocate bump arena (%zu bytes, %.2f MiB)\n",
338 total_size, (double)total_size / (1024.0 * 1024.0));
339 ck_bump_alloc_reset(alloc);
340 return -1;
341}
static size_t align_up_bytes(size_t n, size_t align)
static int env_flag_enabled(const char *name)
void * ck_huge_alloc(size_t bytes)
#define HUGE_PAGE_SIZE
size_t activations_base

References ck_bump_alloc_t::activations_base, align_up_bytes(), ck_bump_alloc_t::base, ck_bump_alloc_reset(), CK_BUMP_MODE_ANON, ck_huge_alloc(), env_flag_enabled(), HUGE_PAGE_SIZE, ck_bump_alloc_t::mapped_len, ck_bump_alloc_t::mode, ck_bump_alloc_t::total_size, and ck_bump_alloc_t::weights_base.

◆ ck_bump_alloc_needs_weight_materialization()

int ck_bump_alloc_needs_weight_materialization ( const ck_bump_alloc_t *  alloc)

Definition at line 361 of file ckernel_alloc.c.

362{
363 if (!alloc) {
364 return 1;
365 }
366 return alloc->mode != CK_BUMP_MODE_MIXED_FILE_BACKED;
367}

References CK_BUMP_MODE_MIXED_FILE_BACKED, and ck_bump_alloc_t::mode.

◆ ck_bump_alloc_reset()

static void ck_bump_alloc_reset ( ck_bump_alloc_t *  alloc)
static

Definition at line 142 of file ckernel_alloc.c.

143{
144 if (!alloc) {
145 return;
146 }
147 alloc->base = NULL;
148 alloc->total_size = 0;
149 alloc->weights_base = 0;
150 alloc->activations_base = 0;
151 alloc->mapped_len = 0;
152 alloc->weights_file_size = 0;
154}
@ CK_BUMP_MODE_UNINITIALIZED
size_t weights_file_size

References ck_bump_alloc_t::activations_base, ck_bump_alloc_t::base, CK_BUMP_MODE_UNINITIALIZED, ck_bump_alloc_t::mapped_len, ck_bump_alloc_t::mode, ck_bump_alloc_t::total_size, ck_bump_alloc_t::weights_base, and ck_bump_alloc_t::weights_file_size.

Referenced by ck_bump_alloc_free(), and ck_bump_alloc_init().

◆ ck_huge_alloc()

void * ck_huge_alloc ( size_t  bytes)

Allocate a large, contiguous memory region for model weights/activations.

Implementation strategy:

  • Try to allocate 2MB-aligned memory backed by huge pages where possible.
  • Fall back to aligned_alloc + madvise(MADV_HUGEPAGE) when explicit huge pages are not available.

Returns NULL on failure.

Definition at line 70 of file ckernel_alloc.c.

71{
72 if (bytes == 0 || bytes > SIZE_MAX - (HUGE_PAGE_SIZE - 1)) {
73 errno = ENOMEM;
74 return NULL;
75 }
76 size_t len = align_up_bytes(bytes, HUGE_PAGE_SIZE);
77
78 /* First, try explicit huge pages via mmap + MAP_HUGETLB. */
79 void *p = mmap(NULL, len,
80 PROT_READ | PROT_WRITE,
81 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
82 -1, 0);
83 if (p != MAP_FAILED) {
84 if (!record_allocation(p, len, 1)) {
85 munmap(p, len);
86 return NULL;
87 }
88 return p;
89 }
90
91 /* Fallback: aligned_alloc with transparent hugepage hint. */
92 void *q = aligned_alloc(HUGE_PAGE_SIZE, len);
93 if (!q) {
94 fprintf(stderr, "ck_huge_alloc: aligned_alloc failed for %zu bytes: %s\n",
95 len, strerror(errno));
96 return NULL;
97 }
98
99 /* Best-effort hint; ignore errors. */
100 (void)madvise(q, len, MADV_HUGEPAGE);
101 if (!record_allocation(q, len, 0)) {
102 free(q);
103 return NULL;
104 }
105 return q;
106}
static int record_allocation(void *ptr, size_t len, int was_mmap)

Referenced by ck_bump_alloc_init().

◆ ck_huge_free()

void ck_huge_free ( void *  ptr,
size_t  bytes 
)

Free memory allocated by ck_huge_alloc.

The bytes parameter should be the same size passed to ck_huge_alloc.

Definition at line 108 of file ckernel_alloc.c.

109{
110 if (!ptr || bytes == 0) {
111 return;
112 }
113
114 ck_huge_alloc_entry_t *entry = detach_allocation(ptr);
115 if (!entry) {
116 /* Fall back to malloc/free if the allocation wasn't tracked. */
117 free(ptr);
118 return;
119 }
120
121 if (entry->was_mmap) {
122 munmap(ptr, entry->len);
123 } else {
124 free(ptr);
125 }
126
127 free(entry);
128}
static ck_huge_alloc_entry_t * detach_allocation(void *ptr)

Referenced by ck_bump_alloc_free().

◆ detach_allocation()

static ck_huge_alloc_entry_t * detach_allocation ( void *  ptr)
static

Definition at line 53 of file ckernel_alloc.c.

54{
55 pthread_mutex_lock(&g_alloc_mutex);
56 ck_huge_alloc_entry_t **node = &g_alloc_list;
57 while (*node) {
58 if ((*node)->ptr == ptr) {
59 ck_huge_alloc_entry_t *entry = *node;
60 *node = entry->next;
61 pthread_mutex_unlock(&g_alloc_mutex);
62 return entry;
63 }
64 node = &(*node)->next;
65 }
66 pthread_mutex_unlock(&g_alloc_mutex);
67 return NULL;
68}
static ck_huge_alloc_entry_t * g_alloc_list
static pthread_mutex_t g_alloc_mutex

References g_alloc_list, and g_alloc_mutex.

Referenced by ck_huge_free().

◆ env_flag_enabled()

static int env_flag_enabled ( const char *  name)
static

Definition at line 130 of file ckernel_alloc.c.

131{
132 const char *value = getenv(name);
133 if (!value || !*value) {
134 return 0;
135 }
136 if (strcmp(value, "0") == 0 || strcasecmp(value, "false") == 0 || strcasecmp(value, "no") == 0) {
137 return 0;
138 }
139 return 1;
140}

Referenced by ck_bump_alloc_init().

◆ record_allocation()

static int record_allocation ( void *  ptr,
size_t  len,
int  was_mmap 
)
static

Definition at line 37 of file ckernel_alloc.c.

38{
39 ck_huge_alloc_entry_t *entry = malloc(sizeof(*entry));
40 if (!entry) {
41 return 0;
42 }
43 entry->ptr = ptr;
44 entry->len = len;
45 entry->was_mmap = was_mmap;
46 pthread_mutex_lock(&g_alloc_mutex);
47 entry->next = g_alloc_list;
48 g_alloc_list = entry;
49 pthread_mutex_unlock(&g_alloc_mutex);
50 return 1;
51}

References g_alloc_list, and g_alloc_mutex.

Referenced by ck_huge_alloc().

Variable Documentation

◆ g_alloc_list

ck_huge_alloc_entry_t* g_alloc_list = NULL
static

Definition at line 29 of file ckernel_alloc.c.

Referenced by detach_allocation(), and record_allocation().

◆ g_alloc_mutex

pthread_mutex_t g_alloc_mutex = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 28 of file ckernel_alloc.c.

Referenced by detach_allocation(), and record_allocation().