18#define HUGE_PAGE_SIZE (2UL * 1024UL * 1024UL)
21typedef struct ck_huge_alloc_entry {
25 struct ck_huge_alloc_entry *next;
26} ck_huge_alloc_entry_t;
33 if (align == 0)
return n;
34 return (n + align - 1) & ~(align - 1);
39 ck_huge_alloc_entry_t *entry = malloc(
sizeof(*entry));
45 entry->was_mmap = was_mmap;
58 if ((*node)->ptr == ptr) {
59 ck_huge_alloc_entry_t *entry = *node;
64 node = &(*node)->next;
75 void *p = mmap(NULL, len,
76 PROT_READ | PROT_WRITE,
77 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
79 if (p != MAP_FAILED) {
90 fprintf(stderr,
"ck_huge_alloc: aligned_alloc failed for %zu bytes: %s\n",
91 len, strerror(errno));
96 (void)madvise(q, len, MADV_HUGEPAGE);
106 if (!ptr || bytes == 0) {
117 if (entry->was_mmap) {
118 munmap(ptr, entry->len);
128 const char *value = getenv(name);
129 if (!value || !*value) {
132 if (strcmp(value,
"0") == 0 || strcasecmp(value,
"false") == 0 || strcasecmp(value,
"no") == 0) {
153static int ck_bump_alloc_try_mixed(
ck_bump_alloc_t *alloc,
const char *weights_path)
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;
164 uint8_t *base = NULL;
166 if (!alloc || !weights_path || !*weights_path) {
170 fprintf(stderr,
"ck_bump_alloc_init: invalid bump layout for mixed fallback\n");
180 fd = open(weights_path, O_RDONLY | O_CLOEXEC);
182 fprintf(stderr,
"ck_bump_alloc_init: failed to open %s: %s\n", weights_path, strerror(errno));
185 if (fstat(fd, &st) != 0) {
186 fprintf(stderr,
"ck_bump_alloc_init: fstat failed for %s: %s\n", weights_path, strerror(errno));
190 if (!S_ISREG(st.st_mode)) {
191 fprintf(stderr,
"ck_bump_alloc_init: %s is not a regular file\n", weights_path);
198 "ck_bump_alloc_init: weights file too small for mixed mapping (%zu < %zu bytes)\n",
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));
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);
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);
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);
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));
258 const char *weights_path,
261 size_t activations_base)
264 int disable_mixed = 0;
266 if (!alloc || total_size == 0) {
267 fprintf(stderr,
"ck_bump_alloc_init: invalid arguments\n");
278 if (weights_base > activations_base || activations_base > total_size) {
280 "ck_bump_alloc_init: invalid bump layout weights=%zu activations=%zu total=%zu\n",
281 weights_base, activations_base, total_size);
289 alloc->
base = (uint8_t *)anon;
297 if (!disable_mixed) {
298 if (ck_bump_alloc_try_mixed(alloc, weights_path) == 0) {
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));
315 if (!alloc || !alloc->
base) {
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
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_MIXED_FILE_BACKED
@ CK_BUMP_MODE_UNINITIALIZED