Append one compact JSON line to the persistent NDJSON trajectory, rotating to * .1 once it passes the cap. Best-effort: a failed append never disrupts * the server. The trajectory (a monotonic rss/committed climb over hours) is * what reveals a slow leak like #581 — the single latest-snapshot file cannot. */
| 100 | * the server. The trajectory (a monotonic rss/committed climb over hours) is |
| 101 | * what reveals a slow leak like #581 — the single latest-snapshot file cannot. */ |
| 102 | static void append_trajectory(long uptime, size_t rss, size_t peak_rss, size_t commit, |
| 103 | size_t peak_commit, size_t page_faults, int fds, int qcount) { |
| 104 | if (g_diag_ndjson_path[0] == '\0') { |
| 105 | return; |
| 106 | } |
| 107 | if (g_diag_ndjson_size > DIAG_NDJSON_CAP_BYTES) { |
| 108 | char rot[sizeof(g_diag_ndjson_path) + DIAG_PATH_EXTRA]; |
| 109 | snprintf(rot, sizeof(rot), "%s.1", g_diag_ndjson_path); |
| 110 | (void)rename(g_diag_ndjson_path, rot); /* keep one previous generation */ |
| 111 | g_diag_ndjson_size = 0; |
| 112 | } |
| 113 | FILE *f = fopen(g_diag_ndjson_path, "a"); |
| 114 | if (!f) { |
| 115 | return; |
| 116 | } |
| 117 | int n = fprintf(f, |
| 118 | "{\"uptime_s\":%ld,\"rss\":%zu,\"peak_rss\":%zu,\"committed\":%zu," |
| 119 | "\"peak_committed\":%zu,\"page_faults\":%zu,\"fd\":%d,\"queries\":%d}\n", |
| 120 | uptime, rss, peak_rss, commit, peak_commit, page_faults, fds, qcount); |
| 121 | if (n > 0) { |
| 122 | g_diag_ndjson_size += (size_t)n; |
| 123 | } |
| 124 | (void)fclose(f); |
| 125 | } |
| 126 | |
| 127 | static void write_diagnostics(void) { |
| 128 | /* Collect mimalloc stats */ |