MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / diag_write_allocator_stats

Function diag_write_allocator_stats

src/foundation/diagnostics.c:189–222  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

187}
188
189static void diag_write_allocator_stats(void) {
190 char flag[CBM_SZ_16];
191 if (cbm_safe_getenv("CBM_MEM_STATS", flag, sizeof(flag), NULL) == NULL || flag[0] != '1') {
192 return;
193 }
194 char path[CBM_PATH_MAX];
195 /* Deliberately NOT inside the diagnostics directory: that tree is
196 * owner-private with anchored (openat-based) writes on POSIX, which a plain
197 * path-based open does not satisfy. This file is a developer diagnostic, so
198 * a predictable temp path keeps it working identically on every platform —
199 * diag_open_private_stats_file is what makes that path safe to write. */
200 int written =
201 snprintf(path, sizeof(path), "%s/cbm-allocator-stats-%d.txt", cbm_tmpdir(), (int)getpid());
202 if (written <= 0 || (size_t)written >= sizeof(path)) {
203 return;
204 }
205 FILE *sink = diag_open_private_stats_file(path);
206 if (!sink) {
207 return;
208 }
209 mi_stats_print_out(diag_stats_write, sink);
210 /* Arena slice map: the legend distinguishes free-committed (retained, "_")
211 * from free-reserved (already returned to the OS, ".") and free-purgeable
212 * ("~"). That is what localises retained memory to a specific arena state
213 * instead of inferring it from totals. mi_debug_show_arenas writes through
214 * mimalloc's own output hook, so redirect that into this file. */
215 (void)fputs("\n--- arenas ---\n", sink);
216 mi_register_output(diag_stats_write, sink);
217 mi_debug_show_arenas();
218 mi_register_output(NULL, NULL);
219 (void)fputs("\n--- options ---\n", sink);
220 mi_options_print_out(diag_stats_write, sink);
221 (void)fclose(sink);
222}
223
224static bool diag_set_output_paths(void) {
225 int snapshot = snprintf(g_diag_path, sizeof(g_diag_path), "%s/%s", g_diag_directory_path,

Callers 1

write_diagnosticsFunction · 0.85

Calls 3

cbm_safe_getenvFunction · 0.85
cbm_tmpdirFunction · 0.85

Tested by

no test coverage detected