forked from DeusData/codebase-memory-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_platform.c
More file actions
332 lines (297 loc) · 10.2 KB
/
Copy pathtest_platform.c
File metadata and controls
332 lines (297 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* test_platform.c — RED phase tests for foundation/platform.
*/
#include "test_framework.h"
#include "../src/foundation/compat.h" /* cbm_setenv / cbm_unsetenv (Windows-portable) */
#include "../src/foundation/platform.h"
#include "../src/foundation/system_info_internal.h"
#include <stdlib.h>
#include <unistd.h>
#ifdef __linux__
/* Linux-only cgroup tests need stdio for FILE*, stdlib for mkdtemp,
* string for strncpy/strchr, sys/stat for mkdir, dirent for the
* shell-free recursive teardown. */
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#endif
TEST(platform_now_ns) {
uint64_t t1 = cbm_now_ns();
ASSERT_GT(t1, 0);
/* Busy-wait a tiny bit */
for (volatile int i = 0; i < 100000; i++) {}
uint64_t t2 = cbm_now_ns();
ASSERT_GT(t2, t1);
PASS();
}
TEST(platform_now_ms) {
uint64_t t1 = cbm_now_ms();
ASSERT_GT(t1, 0);
PASS();
}
TEST(platform_nprocs) {
int n = cbm_nprocs();
ASSERT_GT(n, 0);
ASSERT_LT(n, 10000); /* sanity */
PASS();
}
TEST(platform_file_exists) {
/* This test file should exist */
ASSERT_TRUE(cbm_file_exists("tests/test_platform.c"));
ASSERT_FALSE(cbm_file_exists("nonexistent_file_xyz.txt"));
PASS();
}
TEST(platform_is_dir) {
ASSERT_TRUE(cbm_is_dir("tests"));
ASSERT_FALSE(cbm_is_dir("tests/test_platform.c"));
ASSERT_FALSE(cbm_is_dir("nonexistent_dir"));
PASS();
}
TEST(platform_file_size) {
int64_t sz = cbm_file_size("tests/test_platform.c");
ASSERT_GT(sz, 0);
ASSERT_EQ(cbm_file_size("nonexistent_file_xyz.txt"), -1);
PASS();
}
TEST(platform_mmap) {
/* mmap this test file and verify first bytes */
size_t sz = 0;
void *data = cbm_mmap_read("tests/test_platform.c", &sz);
ASSERT_NOT_NULL(data);
ASSERT_GT(sz, 0);
/* First line should be the comment */
ASSERT(memcmp(data, "/*", 2) == 0);
cbm_munmap(data, sz);
PASS();
}
TEST(platform_mmap_nonexistent) {
size_t sz = 0;
void *data = cbm_mmap_read("nonexistent_xyz.txt", &sz);
ASSERT_NULL(data);
PASS();
}
/*
* CBM_WORKERS env override for cbm_default_worker_count.
*
* Containers running cbm on a host with more CPUs than the cgroup's
* effective quota currently see ~host_cpu workers spawned because
* sysconf(_SC_NPROCESSORS_ONLN) is not cgroup-aware (see GitHub
* issue for the cgroup-detection ask). CBM_WORKERS is the smaller,
* explicit-override path that ships independently.
*/
TEST(platform_default_workers_env_override) {
cbm_setenv("CBM_WORKERS", "4", 1);
int n = cbm_default_worker_count(true);
ASSERT_EQ(n, 4);
/* initial=false should also honor the explicit override. */
int m = cbm_default_worker_count(false);
ASSERT_EQ(m, 4);
cbm_unsetenv("CBM_WORKERS");
PASS();
}
TEST(platform_default_workers_env_invalid) {
/* Out-of-range values (< 1 or > 256) and non-numeric strings
* fall back to the sysconf-derived default. */
int baseline = cbm_default_worker_count(true);
ASSERT_GT(baseline, 0);
cbm_setenv("CBM_WORKERS", "0", 1);
ASSERT_EQ(cbm_default_worker_count(true), baseline);
cbm_setenv("CBM_WORKERS", "-1", 1);
ASSERT_EQ(cbm_default_worker_count(true), baseline);
cbm_setenv("CBM_WORKERS", "9999", 1);
ASSERT_EQ(cbm_default_worker_count(true), baseline);
cbm_setenv("CBM_WORKERS", "not-a-number", 1);
ASSERT_EQ(cbm_default_worker_count(true), baseline);
cbm_unsetenv("CBM_WORKERS");
PASS();
}
TEST(platform_default_workers_env_unset) {
/* When CBM_WORKERS is unset the result matches today's behaviour
* (info.total_cores for initial=true, perf_cores-1 for false). */
cbm_unsetenv("CBM_WORKERS");
cbm_system_info_t info = cbm_system_info();
ASSERT_EQ(cbm_default_worker_count(true), info.total_cores);
PASS();
}
/* ── cgroup-aware detection (Linux only) ─────────────────────────── */
#ifdef __linux__
/* Create a unique tmp directory the caller will own; returns 0 on success. */
static int cgroup_test_setup(char *root, size_t root_sz) {
strncpy(root, "/tmp/cbm_cgroup_test_XXXXXX", root_sz);
return mkdtemp(root) != NULL ? 0 : -1;
}
/* Write `content` to "<root>/<relpath>". Creates parent subdir if needed.
* Returns 0 on success, -1 on any failure. */
static int cgroup_test_write(const char *root, const char *relpath, const char *content) {
char path[1024];
const char *slash = strchr(relpath, '/');
if (slash != NULL) {
char subdir[1024];
size_t n = (size_t)(slash - relpath);
if (n >= sizeof(subdir)) {
return -1;
}
memcpy(subdir, relpath, n);
subdir[n] = '\0';
snprintf(path, sizeof(path), "%s/%s", root, subdir);
(void)mkdir(path, S_IRWXU);
}
snprintf(path, sizeof(path), "%s/%s", root, relpath);
FILE *fp = fopen(path, "we");
if (fp == NULL) {
return -1;
}
size_t n = strlen(content);
int rc = (fwrite(content, 1, n, fp) == n) ? 0 : -1;
fclose(fp);
return rc;
}
/* Recursively remove a tmp dir created by cgroup_test_setup. Best-effort.
* Uses opendir/unlink/rmdir rather than system("rm -rf ...") to avoid
* spawning a shell from the test binary. */
static void cgroup_test_teardown(const char *root) {
DIR *d = opendir(root);
if (d != NULL) {
struct dirent *ent;
while ((ent = readdir(d)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}
char child[1024];
snprintf(child, sizeof(child), "%s/%s", root, ent->d_name);
struct stat st;
if (stat(child, &st) == 0 && S_ISDIR(st.st_mode)) {
cgroup_test_teardown(child); /* recurse into subdir */
} else {
(void)unlink(child);
}
}
closedir(d);
}
(void)rmdir(root);
}
TEST(cgroup_v2_cpu_quota) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
/* 200ms quota in a 100ms period → 2 effective CPUs. */
ASSERT_EQ(cgroup_test_write(root, "cpu.max", "200000 100000\n"), 0);
ASSERT_EQ(cbm_detect_cgroup_cpus(root), 2);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_v2_cpu_quota_rounds_up) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
/* 150ms quota / 100ms period = 1.5 → ceil = 2. */
ASSERT_EQ(cgroup_test_write(root, "cpu.max", "150000 100000\n"), 0);
ASSERT_EQ(cbm_detect_cgroup_cpus(root), 2);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_v2_cpu_unlimited) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
ASSERT_EQ(cgroup_test_write(root, "cpu.max", "max 100000\n"), 0);
ASSERT_EQ(cbm_detect_cgroup_cpus(root), -1);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_v1_cpu_quota) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
ASSERT_EQ(cgroup_test_write(root, "cpu/cpu.cfs_quota_us", "200000"), 0);
ASSERT_EQ(cgroup_test_write(root, "cpu/cpu.cfs_period_us", "100000"), 0);
ASSERT_EQ(cbm_detect_cgroup_cpus(root), 2);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_v1_cpu_unlimited) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
/* quota=-1 is the cgroup-v1 sentinel for "no quota". */
ASSERT_EQ(cgroup_test_write(root, "cpu/cpu.cfs_quota_us", "-1"), 0);
ASSERT_EQ(cgroup_test_write(root, "cpu/cpu.cfs_period_us", "100000"), 0);
ASSERT_EQ(cbm_detect_cgroup_cpus(root), -1);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_no_cpu_files) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
/* Empty tmp dir: no v2 file, no v1 file → fall through to sysconf. */
ASSERT_EQ(cbm_detect_cgroup_cpus(root), -1);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_v2_mem) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
/* 2 GiB. */
ASSERT_EQ(cgroup_test_write(root, "memory.max", "2147483648\n"), 0);
ASSERT_EQ(cbm_detect_cgroup_mem(root), (size_t)2147483648UL);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_v2_mem_unlimited) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
ASSERT_EQ(cgroup_test_write(root, "memory.max", "max\n"), 0);
ASSERT_EQ(cbm_detect_cgroup_mem(root), (size_t)0);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_v1_mem) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
/* 1 GiB. */
ASSERT_EQ(cgroup_test_write(root, "memory/memory.limit_in_bytes", "1073741824"), 0);
ASSERT_EQ(cbm_detect_cgroup_mem(root), (size_t)1073741824UL);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_v1_mem_unlimited_sentinel) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
/* cgroup v1 reports a huge near-ULLONG_MAX value when unlimited
* (PAGE_COUNTER_MAX). Our parser treats anything >= ULLONG_MAX/2
* as effectively unlimited. */
ASSERT_EQ(cgroup_test_write(root, "memory/memory.limit_in_bytes", "9223372036854775807"), 0);
ASSERT_EQ(cbm_detect_cgroup_mem(root), (size_t)0);
cgroup_test_teardown(root);
PASS();
}
TEST(cgroup_no_mem_files) {
char root[64];
ASSERT_EQ(cgroup_test_setup(root, sizeof(root)), 0);
ASSERT_EQ(cbm_detect_cgroup_mem(root), (size_t)0);
cgroup_test_teardown(root);
PASS();
}
#endif /* __linux__ */
SUITE(platform) {
RUN_TEST(platform_now_ns);
RUN_TEST(platform_now_ms);
RUN_TEST(platform_nprocs);
RUN_TEST(platform_file_exists);
RUN_TEST(platform_is_dir);
RUN_TEST(platform_file_size);
RUN_TEST(platform_mmap);
RUN_TEST(platform_mmap_nonexistent);
RUN_TEST(platform_default_workers_env_override);
RUN_TEST(platform_default_workers_env_invalid);
RUN_TEST(platform_default_workers_env_unset);
#ifdef __linux__
RUN_TEST(cgroup_v2_cpu_quota);
RUN_TEST(cgroup_v2_cpu_quota_rounds_up);
RUN_TEST(cgroup_v2_cpu_unlimited);
RUN_TEST(cgroup_v1_cpu_quota);
RUN_TEST(cgroup_v1_cpu_unlimited);
RUN_TEST(cgroup_no_cpu_files);
RUN_TEST(cgroup_v2_mem);
RUN_TEST(cgroup_v2_mem_unlimited);
RUN_TEST(cgroup_v1_mem);
RUN_TEST(cgroup_v1_mem_unlimited_sentinel);
RUN_TEST(cgroup_no_mem_files);
#endif
}