← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
ckernel_alloc.c
Go to the documentation of this file.
1#define _GNU_SOURCE
2#include "ckernel_alloc.h"
3
4#include <errno.h>
5#include <fcntl.h>
6#include <pthread.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <strings.h>
11#include <sys/mman.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
15
16/* 2MB huge page size on Linux. */
17#ifndef HUGE_PAGE_SIZE
18#define HUGE_PAGE_SIZE (2UL * 1024UL * 1024UL)
19#endif
20
21typedef struct ck_huge_alloc_entry {
22 void *ptr;
23 size_t len;
24 int was_mmap;
25 struct ck_huge_alloc_entry *next;
26} ck_huge_alloc_entry_t;
27
28static pthread_mutex_t g_alloc_mutex = PTHREAD_MUTEX_INITIALIZER;
29static ck_huge_alloc_entry_t *g_alloc_list = NULL;
30
31static size_t align_up_bytes(size_t n, size_t align)
32{
33 if (align == 0) return n;
34 return (n + align - 1) & ~(align - 1);
35}
36
37static int record_allocation(void *ptr, size_t len, int was_mmap)
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}
52
53static ck_huge_alloc_entry_t *detach_allocation(void *ptr)
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}
69
70void *ck_huge_alloc(size_t bytes)
71{
72 size_t len = align_up_bytes(bytes, HUGE_PAGE_SIZE);
73
74 /* First, try explicit huge pages via mmap + MAP_HUGETLB. */
75 void *p = mmap(NULL, len,
76 PROT_READ | PROT_WRITE,
77 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
78 -1, 0);
79 if (p != MAP_FAILED) {
80 if (!record_allocation(p, len, 1)) {
81 munmap(p, len);
82 return NULL;
83 }
84 return p;
85 }
86
87 /* Fallback: aligned_alloc with transparent hugepage hint. */
88 void *q = aligned_alloc(HUGE_PAGE_SIZE, len);
89 if (!q) {
90 fprintf(stderr, "ck_huge_alloc: aligned_alloc failed for %zu bytes: %s\n",
91 len, strerror(errno));
92 return NULL;
93 }
94
95 /* Best-effort hint; ignore errors. */
96 (void)madvise(q, len, MADV_HUGEPAGE);
97 if (!record_allocation(q, len, 0)) {
98 free(q);
99 return NULL;
100 }
101 return q;
102}
103
104void ck_huge_free(void *ptr, size_t bytes)
105{
106 if (!ptr || bytes == 0) {
107 return;
108 }
109
110 ck_huge_alloc_entry_t *entry = detach_allocation(ptr);
111 if (!entry) {
112 /* Fall back to malloc/free if the allocation wasn't tracked. */
113 free(ptr);
114 return;
115 }
116
117 if (entry->was_mmap) {
118 munmap(ptr, entry->len);
119 } else {
120 free(ptr);
121 }
122
123 free(entry);
124}
125
126static int env_flag_enabled(const char *name)
127{
128 const char *value = getenv(name);
129 if (!value || !*value) {
130 return 0;
131 }
132 if (strcmp(value, "0") == 0 || strcasecmp(value, "false") == 0 || strcasecmp(value, "no") == 0) {
133 return 0;
134 }
135 return 1;
136}
137
139{
140 if (!alloc) {
141 return;
142 }
143 alloc->base = NULL;
144 alloc->total_size = 0;
145 alloc->weights_base = 0;
146 alloc->activations_base = 0;
147 alloc->mapped_len = 0;
148 alloc->weights_file_size = 0;
150}
151
152#ifdef __linux__
153static int ck_bump_alloc_try_mixed(ck_bump_alloc_t *alloc, const char *weights_path)
154{
155 long page_size_raw = sysconf(_SC_PAGESIZE);
156 size_t page_size = page_size_raw > 0 ? (size_t)page_size_raw : 4096U;
157 size_t mapped_len = 0;
158 size_t prefix_len = 0;
159 size_t weights_len = 0;
160 size_t weights_map_len = 0;
161 size_t runtime_map_start = 0;
162 int fd = -1;
163 struct stat st;
164 uint8_t *base = NULL;
165
166 if (!alloc || !weights_path || !*weights_path) {
167 return -1;
168 }
169 if (alloc->weights_base > alloc->activations_base || alloc->activations_base > alloc->total_size) {
170 fprintf(stderr, "ck_bump_alloc_init: invalid bump layout for mixed fallback\n");
171 return -1;
172 }
173
174 mapped_len = align_up_bytes(alloc->total_size, page_size);
175 prefix_len = 0;
176 weights_len = alloc->activations_base;
177 weights_map_len = align_up_bytes(weights_len, page_size);
178 runtime_map_start = align_up_bytes(alloc->activations_base, page_size);
179
180 fd = open(weights_path, O_RDONLY | O_CLOEXEC);
181 if (fd < 0) {
182 fprintf(stderr, "ck_bump_alloc_init: failed to open %s: %s\n", weights_path, strerror(errno));
183 return -1;
184 }
185 if (fstat(fd, &st) != 0) {
186 fprintf(stderr, "ck_bump_alloc_init: fstat failed for %s: %s\n", weights_path, strerror(errno));
187 close(fd);
188 return -1;
189 }
190 if (!S_ISREG(st.st_mode)) {
191 fprintf(stderr, "ck_bump_alloc_init: %s is not a regular file\n", weights_path);
192 close(fd);
193 return -1;
194 }
195 alloc->weights_file_size = (size_t)st.st_size;
196 if (weights_len > 0 && weights_map_len > align_up_bytes(alloc->weights_file_size, page_size)) {
197 fprintf(stderr,
198 "ck_bump_alloc_init: weights file too small for mixed mapping (%zu < %zu bytes)\n",
199 alloc->weights_file_size, weights_map_len);
200 close(fd);
201 return -1;
202 }
203
204 base = mmap(NULL, mapped_len, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
205 if (base == MAP_FAILED) {
206 fprintf(stderr, "ck_bump_alloc_init: reserve mmap failed for %zu bytes: %s\n",
207 mapped_len, strerror(errno));
208 close(fd);
209 return -1;
210 }
211
212 if (prefix_len > 0) {
213 void *prefix = mmap(base, prefix_len, PROT_READ | PROT_WRITE,
214 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
215 if (prefix == MAP_FAILED) {
216 fprintf(stderr, "ck_bump_alloc_init: prefix mmap failed: %s\n", strerror(errno));
217 munmap(base, mapped_len);
218 close(fd);
219 return -1;
220 }
221 }
222
223 if (weights_map_len > 0) {
224 void *mapped = mmap(base, weights_map_len,
225 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd, 0);
226 if (mapped == MAP_FAILED) {
227 fprintf(stderr, "ck_bump_alloc_init: weights mmap failed: %s\n", strerror(errno));
228 munmap(base, mapped_len);
229 close(fd);
230 return -1;
231 }
232 }
233
234 if (runtime_map_start < mapped_len) {
235 void *runtime = mmap(base + runtime_map_start, mapped_len - runtime_map_start,
236 PROT_READ | PROT_WRITE,
237 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
238 if (runtime == MAP_FAILED) {
239 fprintf(stderr, "ck_bump_alloc_init: runtime mmap failed: %s\n", strerror(errno));
240 munmap(base, mapped_len);
241 close(fd);
242 return -1;
243 }
244 }
245
246 close(fd);
247 alloc->base = base;
248 alloc->mapped_len = mapped_len;
250 fprintf(stderr,
251 "ck_bump_alloc_init: fallback to mixed-backed bump (%zu MiB total, weights file-backed=%zu MiB)\n",
252 alloc->total_size / (1024U * 1024U), weights_len / (1024U * 1024U));
253 return 0;
254}
255#endif
256
258 const char *weights_path,
259 size_t total_size,
260 size_t weights_base,
261 size_t activations_base)
262{
263 int force_mixed = 0;
264 int disable_mixed = 0;
265
266 if (!alloc || total_size == 0) {
267 fprintf(stderr, "ck_bump_alloc_init: invalid arguments\n");
268 return -1;
269 }
270
271 ck_bump_alloc_reset(alloc);
272 alloc->total_size = total_size;
273 alloc->weights_base = weights_base;
274 alloc->activations_base = activations_base;
275 force_mixed = env_flag_enabled("CK_BUMP_FORCE_MIXED");
276 disable_mixed = env_flag_enabled("CK_BUMP_DISABLE_MIXED");
277
278 if (weights_base > activations_base || activations_base > total_size) {
279 fprintf(stderr,
280 "ck_bump_alloc_init: invalid bump layout weights=%zu activations=%zu total=%zu\n",
281 weights_base, activations_base, total_size);
282 ck_bump_alloc_reset(alloc);
283 return -1;
284 }
285
286 if (!force_mixed) {
287 void *anon = ck_huge_alloc(total_size);
288 if (anon) {
289 alloc->base = (uint8_t *)anon;
290 alloc->mapped_len = align_up_bytes(total_size, HUGE_PAGE_SIZE);
291 alloc->mode = CK_BUMP_MODE_ANON;
292 return 0;
293 }
294 }
295
296#ifdef __linux__
297 if (!disable_mixed) {
298 if (ck_bump_alloc_try_mixed(alloc, weights_path) == 0) {
299 return 0;
300 }
301 }
302#else
303 (void)weights_path;
304#endif
305
306 fprintf(stderr,
307 "ck_bump_alloc_init: failed to allocate bump arena (%zu bytes, %.2f MiB)\n",
308 total_size, (double)total_size / (1024.0 * 1024.0));
309 ck_bump_alloc_reset(alloc);
310 return -1;
311}
312
314{
315 if (!alloc || !alloc->base) {
316 ck_bump_alloc_reset(alloc);
317 return;
318 }
319
320 if (alloc->mode == CK_BUMP_MODE_MIXED_FILE_BACKED) {
321 if (alloc->mapped_len > 0) {
322 munmap(alloc->base, alloc->mapped_len);
323 }
324 } else if (alloc->mode == CK_BUMP_MODE_ANON) {
325 ck_huge_free(alloc->base, alloc->total_size);
326 }
327
328 ck_bump_alloc_reset(alloc);
329}
330
332{
333 if (!alloc) {
334 return 1;
335 }
336 return alloc->mode != CK_BUMP_MODE_MIXED_FILE_BACKED;
337}
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)
static size_t align_up_bytes(size_t n, size_t align)
static ck_huge_alloc_entry_t * g_alloc_list
static int record_allocation(void *ptr, size_t len, int was_mmap)
static void ck_bump_alloc_reset(ck_bump_alloc_t *alloc)
static int env_flag_enabled(const char *name)
static ck_huge_alloc_entry_t * detach_allocation(void *ptr)
void * ck_huge_alloc(size_t bytes)
static pthread_mutex_t g_alloc_mutex
#define HUGE_PAGE_SIZE
void ck_huge_free(void *ptr, size_t bytes)
int ck_bump_alloc_needs_weight_materialization(const ck_bump_alloc_t *alloc)
@ CK_BUMP_MODE_ANON
@ CK_BUMP_MODE_MIXED_FILE_BACKED
@ CK_BUMP_MODE_UNINITIALIZED
size_t activations_base
size_t weights_file_size
ck_bump_mode_t mode