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

Function cbm_scale_end

src/foundation/profile.c:134–187  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

132}
133
134void cbm_scale_end(cbm_scale_probe_t *probe) {
135 if (!probe || probe->total < SCALE_MIN_ITEMS) {
136 return;
137 }
138 int recorded = atomic_load_explicit(&probe->next_cp, memory_order_relaxed);
139 if (recorded < 2) {
140 return; /* need two points to speak about growth at all */
141 }
142 long first_us = probe->cp_us[0];
143 long first_n = probe->cp_items[0];
144 long last_us = probe->cp_us[recorded - 1];
145 long last_n = probe->cp_items[recorded - 1];
146 if (first_us < SCALE_MIN_FIRST_US || first_n <= 0 || last_n <= first_n || last_us <= 0) {
147 return;
148 }
149
150 double k = cbm_scale_fit_k(first_n, first_us, last_n, last_us);
151 if (k < 0.0) {
152 return;
153 }
154 long us_per_item = last_us / last_n;
155
156 char k_buf[PROF_BUF_LEN];
157 char n_buf[PROF_BUF_LEN];
158 char per_buf[PROF_BUF_LEN];
159 char ms_buf[PROF_BUF_LEN];
160 snprintf(k_buf, sizeof(k_buf), "%.2f", k);
161 snprintf(n_buf, sizeof(n_buf), "%ld", last_n);
162 snprintf(per_buf, sizeof(per_buf), "%ld", us_per_item);
163 snprintf(ms_buf, sizeof(ms_buf), "%ld", last_us / PROF_US_PER_MS);
164
165 if (k >= CBM_SCALE_WARN_K) {
166 /* Unflagged on purpose: this is the alarm the next accidental O(n^2)
167 * should trip in an ordinary user's log. */
168 cbm_log_warn("scaling.superlinear", "phase", probe->phase, "k", k_buf, "items", n_buf,
169 "elapsed_ms", ms_buf, "us_per_item", per_buf);
170 }
171 if (!cbm_profile_active) {
172 return;
173 }
174 char curve[PROF_BUF_LEN * CBM_SCALE_CHECKPOINTS];
175 int used = 0;
176 for (int i = 0; i < recorded && used < (int)sizeof(curve) - 1; i++) {
177 int wrote =
178 snprintf(curve + used, sizeof(curve) - (size_t)used, "%s%ld:%ld", i == 0 ? "" : ",",
179 probe->cp_items[i], probe->cp_us[i] / PROF_US_PER_MS);
180 if (wrote < 0) {
181 break;
182 }
183 used += wrote;
184 }
185 cbm_log_info("scaling", "phase", probe->phase, "k", k_buf, "items", n_buf, "elapsed_ms", ms_buf,
186 "us_per_item", per_buf, "curve_items_ms", curve);
187}

Callers 3

cbm_parallel_extract_exFunction · 0.85
cbm_parallel_resolveFunction · 0.85
test_diagnostics.cFile · 0.85

Calls 1

cbm_scale_fit_kFunction · 0.85

Tested by

no test coverage detected