omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / ng / memo / memo_policy.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/memo/memo_policy.om: Profitability policy, disk-floor alarm, and explicit oldest-first |
| 3 | note memo_prune for Stage 1 OMEGA-MEMO (Sovereign Intelligence 2.0 Pillar 2). |
| 4 | note |
| 5 | note Pure seeded OM-2-compatible library unit (no om2 header, no fn main; prefix mp_ / MP_). |
| 6 | note Zero C/Python/Rust. Zero arbitrary hard entry cap on disk: walks sharded <root>/<2hex>/*.ome |
| 7 | note directly via Linux AArch64 fopen/getdnts/stat/unlink. |
| 8 | |
| 9 | note Profitability decisions |
| 10 | const MP_DEC_ADMIT ulong 1 |
| 11 | const MP_DEC_SKIP_TRIVIAL ulong 2 |
| 12 | const MP_DEC_FORCE_ADMIT ulong 3 |
| 13 | const MP_DEC_DISABLED ulong 4 |
| 14 | |
| 15 | note Policy override modes |
| 16 | const MP_OVR_AUTO ulong 0 |
| 17 | const MP_OVR_FORCE ulong 1 |
| 18 | const MP_OVR_DISABLE ulong 2 |
| 19 | |
| 20 | note Disk-floor check status codes |
| 21 | const MP_DISK_OK ulong 0 |
| 22 | const MP_ALARM_DISK_FLOOR ulong 1 |
| 23 | const MP_DISK_ERR_IO ulong 2 |
| 24 | |
| 25 | note Default policy thresholds |
| 26 | const MP_DEF_MIN_COMPUTE_NS ulong 5000 |
| 27 | const MP_DEF_RATIO_NUM ulong 2 |
| 28 | const MP_DEF_RATIO_DEN ulong 1 |
| 29 | const MP_DEF_WARMUP ulong 1 |
| 30 | const MP_DEF_MAX_STORE_BYTES ulong 1073741824 |
| 31 | const MP_DEF_FREE_FLOOR_BYTES ulong 67108864 |
| 32 | const MP_DEF_FS_CAP_BYTES ulong 2147483648 |
| 33 | |
| 34 | note In-memory per-SEMANTIC_ID profitability telemetry cache (LRU-recycled; NEVER caps disk entries) |
| 35 | const MP_PROF_SLOTS ulong 256 |
| 36 | const MP_PRUNE_WIN ulong 16 |
| 37 | const MP_PATH_STRIDE ulong 4096 |
| 38 | |
| 39 | note Linux ARM64 O_RDONLY | O_DIRECTORY | O_NOFOLLOW = 0 | 16384 | 32768 = 49152 |
| 40 | const MP_O_DIR int 49152 |
| 41 | |
| 42 | note Policy config: |
| 43 | note [0]=override [1]=min_compute_ns [2]=ratio_num [3]=ratio_den [4]=warmup |
| 44 | note [5]=max_store_bytes [6]=free_floor_bytes [7]=fs_cap_bytes [8]=has_ext_free |
| 45 | note [9]=ext_free_bytes [10]=initialized [11]=alarm_silent |
| 46 | global mp_cfg ulong 16 |
| 47 | |
| 48 | note Scan, alarm, prune, and profitability telemetry: |
| 49 | note [0]=total_entries [1]=total_store_bytes [2]=computed_free_bytes [3]=alarm_active |
| 50 | note [4]=alarm_count [5]=prune_removed_entries [6]=prune_reclaimed_bytes |
| 51 | note [7]=total_skips [8]=total_admits [9]=total_force_admits |
| 52 | global mp_stat ulong 16 |
| 53 | |
| 54 | note Structured alarm buffer (always populated on alarm; also written to stderr unless silent) |
| 55 | global mp_alarm_buf uchar 1024 |
| 56 | global mp_alarm_len ulong 1 |
| 57 | |
| 58 | note Per-SEMANTIC_ID profitability profiler tables |
| 59 | global mp_p_used uchar 256 |
| 60 | global mp_p_sid uchar 8192 |
| 61 | global mp_p_calls ulong 256 |
| 62 | global mp_p_cns ulong 256 |
| 63 | global mp_p_ons ulong 256 |
| 64 | global mp_p_skips ulong 256 |
| 65 | global mp_p_hits ulong 256 |
| 66 | global mp_p_last ulong 256 |
| 67 | global mp_tick ulong 1 |
| 68 | global mp_q_sid uchar 32 |
| 69 | |
| 70 | note Store root & streaming directory walk scratch buffers |
| 71 | global mp_root uchar 4096 |
| 72 | global mp_root_len ulong 1 |
| 73 | global mp_dir_path uchar 4096 |
| 74 | global mp_dir_plen ulong 1 |
| 75 | global mp_file_path uchar 4096 |
| 76 | global mp_file_plen ulong 1 |
| 77 | global mp_dents uchar 65536 |
| 78 | global mp_sdents uchar 65536 |
| 79 | global mp_stbuf uchar 128 |
| 80 | global mp_ndig uchar 24 |
| 81 | |
| 82 | note Streaming oldest-K candidate window for explicit memo_prune (no cap on total store size) |
| 83 | global mp_cw_used uchar 16 |
| 84 | global mp_cw_sec ulong 16 |
| 85 | global mp_cw_nsec ulong 16 |
| 86 | global mp_cw_size ulong 16 |
| 87 | global mp_cw_plen ulong 16 |
| 88 | global mp_cw_path uchar 65536 |
| 89 | global mp_cw_cnt ulong 1 |
| 90 | global mp_cw_ord ulong 16 |
| 91 | |
| 92 | text mp_al_pfx "OMEGA-MEMO ALARM disk_floor root=" |
| 93 | text mp_al_ent " entries=" |
| 94 | text mp_al_stb " store_bytes=" |
| 95 | text mp_al_max " max_bytes=" |
| 96 | text mp_al_fre " free_bytes=" |
| 97 | text mp_al_flr " floor_bytes=" |
| 98 | text mp_al_act " action=EXPLICIT_PRUNE_REQUIRED\n" |
| 99 | |
| 100 | note ---- Initialization & Configuration ---- |
| 101 | |
| 102 | fn mp_init to void |
| 103 | put mp_cfg 0 MP_OVR_AUTO |
| 104 | put mp_cfg 1 MP_DEF_MIN_COMPUTE_NS |
| 105 | put mp_cfg 2 MP_DEF_RATIO_NUM |
| 106 | put mp_cfg 3 MP_DEF_RATIO_DEN |
| 107 | put mp_cfg 4 MP_DEF_WARMUP |
| 108 | put mp_cfg 5 MP_DEF_MAX_STORE_BYTES |
| 109 | put mp_cfg 6 MP_DEF_FREE_FLOOR_BYTES |
| 110 | put mp_cfg 7 MP_DEF_FS_CAP_BYTES |
| 111 | put mp_cfg 8 0 |
| 112 | put mp_cfg 9 0 |
| 113 | put mp_cfg 10 1 |
| 114 | put mp_cfg 11 0 |
| 115 | for i ulong 16 |
| 116 | put mp_stat i 0 |
| 117 | end |
| 118 | put mp_alarm_len 0 0 |
| 119 | put mp_tick 0 0 |
| 120 | for s ulong 256 |
| 121 | put mp_p_used s 0 |
| 122 | put mp_p_calls s 0 |
| 123 | put mp_p_cns s 0 |
| 124 | put mp_p_ons s 0 |
| 125 | put mp_p_skips s 0 |
| 126 | put mp_p_hits s 0 |
| 127 | put mp_p_last s 0 |
| 128 | end |
| 129 | end |
| 130 | |
| 131 | fn mp_ensure_init to void |
| 132 | if eq get mp_cfg 10 0 |
| 133 | mp_init |
| 134 | end |
| 135 | end |
| 136 | |
| 137 | fn mp_set_override ovr ulong to void |
| 138 | mp_ensure_init |
| 139 | put mp_cfg 0 ovr |
| 140 | end |
| 141 | |
| 142 | fn mp_set_thresholds min_cns ulong rnum ulong rden ulong to void |
| 143 | mp_ensure_init |
| 144 | put mp_cfg 1 min_cns |
| 145 | put mp_cfg 2 rnum |
| 146 | if eq rden 0 |
| 147 | put mp_cfg 3 1 |
| 148 | else |
| 149 | put mp_cfg 3 rden |
| 150 | end |
| 151 | end |
| 152 | |
| 153 | fn mp_set_disk_limits max_bytes ulong floor_bytes ulong cap_bytes ulong to void |
| 154 | mp_ensure_init |
| 155 | put mp_cfg 5 max_bytes |
| 156 | put mp_cfg 6 floor_bytes |
| 157 | put mp_cfg 7 cap_bytes |
| 158 | end |
| 159 | |
| 160 | fn mp_set_ext_free enabled ulong free_bytes ulong to void |
| 161 | mp_ensure_init |
| 162 | put mp_cfg 8 enabled |
| 163 | put mp_cfg 9 free_bytes |
| 164 | end |
| 165 | |
| 166 | fn mp_set_alarm_silent silent ulong to void |
| 167 | mp_ensure_init |
| 168 | put mp_cfg 11 silent |
| 169 | end |
| 170 | |
| 171 | note ---- Per-SEMANTIC_ID Profitability Policy ---- |
| 172 | |
| 173 | fn mp_sid_match slot ulong to bool |
| 174 | if eq get mp_p_used slot 0 |
| 175 | ret false |
| 176 | end |
| 177 | base = mul slot 32 |
| 178 | for i ulong 32 |
| 179 | if ne get mp_p_sid add base i get mp_q_sid i |
| 180 | ret false |
| 181 | end |
| 182 | end |
| 183 | ret true |
| 184 | end |
| 185 | |
| 186 | fn mp_find_prof to ulong |
| 187 | for s ulong 256 |
| 188 | if mp_sid_match s |
| 189 | ret s |
| 190 | end |
| 191 | end |
| 192 | ret 999999 |
| 193 | end |
| 194 | |
| 195 | fn mp_alloc_prof to ulong |
| 196 | for s ulong 256 |
| 197 | if eq get mp_p_used s 0 |
| 198 | ret s |
| 199 | end |
| 200 | end |
| 201 | best = ulong 0 |
| 202 | btick = get mp_p_last 0 |
| 203 | for s ulong 256 |
| 204 | tk = get mp_p_last s |
| 205 | if lt tk btick |
| 206 | btick = tk |
| 207 | best = s |
| 208 | end |
| 209 | end |
| 210 | ret best |
| 211 | end |
| 212 | |
| 213 | fn mp_get_or_create_prof to ulong |
| 214 | ex = mp_find_prof |
| 215 | if ne ex 999999 |
| 216 | ret ex |
| 217 | end |
| 218 | sl = mp_alloc_prof |
| 219 | put mp_p_used sl 1 |
| 220 | base = mul sl 32 |
| 221 | for i ulong 32 |
| 222 | put mp_p_sid add base i get mp_q_sid i |
| 223 | end |
| 224 | put mp_p_calls sl 0 |
| 225 | put mp_p_cns sl 0 |
| 226 | put mp_p_ons sl 0 |
| 227 | put mp_p_skips sl 0 |
| 228 | put mp_p_hits sl 0 |
| 229 | put mp_p_last sl 0 |
| 230 | ret sl |
| 231 | end |
| 232 | |
| 233 | fn mp_is_profitable avg_cns ulong avg_ons ulong to bool |
| 234 | min_c = get mp_cfg 1 |
| 235 | if lt avg_cns min_c |
| 236 | ret false |
| 237 | end |
| 238 | if eq avg_ons 0 |
| 239 | ret true |
| 240 | end |
| 241 | rnum = get mp_cfg 2 |
| 242 | rden = get mp_cfg 3 |
| 243 | if eq rden 0 |
| 244 | rden = ulong 1 |
| 245 | end |
| 246 | lhs = mul avg_cns rden |
| 247 | rhs = mul avg_ons rnum |
| 248 | ret ge lhs rhs |
| 249 | end |
| 250 | |
| 251 | fn mp_precheck to ulong |
| 252 | mp_ensure_init |
| 253 | ovr = get mp_cfg 0 |
| 254 | if eq ovr MP_OVR_FORCE |
| 255 | ret MP_DEC_FORCE_ADMIT |
| 256 | end |
| 257 | if eq ovr MP_OVR_DISABLE |
| 258 | ret MP_DEC_DISABLED |
| 259 | end |
| 260 | sl = mp_find_prof |
| 261 | if eq sl 999999 |
| 262 | ret MP_DEC_ADMIT |
| 263 | end |
| 264 | calls = get mp_p_calls sl |
| 265 | if lt calls get mp_cfg 4 |
| 266 | ret MP_DEC_ADMIT |
| 267 | end |
| 268 | avg_c = div get mp_p_cns sl calls |
| 269 | avg_o = div get mp_p_ons sl calls |
| 270 | if not mp_is_profitable avg_c avg_o |
| 271 | put mp_p_skips sl add get mp_p_skips sl 1 |
| 272 | put mp_stat 7 add get mp_stat 7 1 |
| 273 | ret MP_DEC_SKIP_TRIVIAL |
| 274 | end |
| 275 | ret MP_DEC_ADMIT |
| 276 | end |
| 277 | |
| 278 | fn mp_should_memoize cns ulong ons ulong to ulong |
| 279 | mp_ensure_init |
| 280 | sl = mp_get_or_create_prof |
| 281 | tk = add get mp_tick 0 1 |
| 282 | put mp_tick 0 tk |
| 283 | put mp_p_last sl tk |
| 284 | calls = add get mp_p_calls sl 1 |
| 285 | tot_c = add get mp_p_cns sl cns |
| 286 | tot_o = add get mp_p_ons sl ons |
| 287 | put mp_p_calls sl calls |
| 288 | put mp_p_cns sl tot_c |
| 289 | put mp_p_ons sl tot_o |
| 290 | ovr = get mp_cfg 0 |
| 291 | if eq ovr MP_OVR_FORCE |
| 292 | put mp_stat 9 add get mp_stat 9 1 |
| 293 | ret MP_DEC_FORCE_ADMIT |
| 294 | end |
| 295 | if eq ovr MP_OVR_DISABLE |
| 296 | put mp_p_skips sl add get mp_p_skips sl 1 |
| 297 | put mp_stat 7 add get mp_stat 7 1 |
| 298 | ret MP_DEC_DISABLED |
| 299 | end |
| 300 | avg_c = div tot_c calls |
| 301 | avg_o = div tot_o calls |
| 302 | if not mp_is_profitable avg_c avg_o |
| 303 | put mp_p_skips sl add get mp_p_skips sl 1 |
| 304 | put mp_stat 7 add get mp_stat 7 1 |
| 305 | ret MP_DEC_SKIP_TRIVIAL |
| 306 | end |
| 307 | put mp_stat 8 add get mp_stat 8 1 |
| 308 | ret MP_DEC_ADMIT |
| 309 | end |
| 310 | |
| 311 | fn mp_record_hit to void |
| 312 | mp_ensure_init |
| 313 | sl = mp_get_or_create_prof |
| 314 | tk = add get mp_tick 0 1 |
| 315 | put mp_tick 0 tk |
| 316 | put mp_p_last sl tk |
| 317 | put mp_p_hits sl add get mp_p_hits sl 1 |
| 318 | end |
| 319 | |
| 320 | note ---- Alarm Line Formatting ---- |
| 321 | |
| 322 | fn mp_al_push_b ch uchar to void |
| 323 | p = get mp_alarm_len 0 |
| 324 | if lt p 1020 |
| 325 | put mp_alarm_buf p ch |
| 326 | put mp_alarm_len 0 add p 1 |
| 327 | end |
| 328 | end |
| 329 | |
| 330 | fn mp_al_push_tx id ulong to void |
| 331 | if eq id 0 |
| 332 | for i ulong len mp_al_pfx bound 64 |
| 333 | mp_al_push_b get mp_al_pfx i |
| 334 | end |
| 335 | ret |
| 336 | end |
| 337 | if eq id 1 |
| 338 | for i ulong len mp_al_ent bound 32 |
| 339 | mp_al_push_b get mp_al_ent i |
| 340 | end |
| 341 | ret |
| 342 | end |
| 343 | if eq id 2 |
| 344 | for i ulong len mp_al_stb bound 32 |
| 345 | mp_al_push_b get mp_al_stb i |
| 346 | end |
| 347 | ret |
| 348 | end |
| 349 | if eq id 3 |
| 350 | for i ulong len mp_al_max bound 32 |
| 351 | mp_al_push_b get mp_al_max i |
| 352 | end |
| 353 | ret |
| 354 | end |
| 355 | if eq id 4 |
| 356 | for i ulong len mp_al_fre bound 32 |
| 357 | mp_al_push_b get mp_al_fre i |
| 358 | end |
| 359 | ret |
| 360 | end |
| 361 | if eq id 5 |
| 362 | for i ulong len mp_al_flr bound 32 |
| 363 | mp_al_push_b get mp_al_flr i |
| 364 | end |
| 365 | ret |
| 366 | end |
| 367 | for i ulong len mp_al_act bound 64 |
| 368 | mp_al_push_b get mp_al_act i |
| 369 | end |
| 370 | end |
| 371 | |
| 372 | fn mp_al_push_num v ulong to void |
| 373 | x = v |
| 374 | p = ulong 24 |
| 375 | for i ulong 21 |
| 376 | p = sub p 1 |
| 377 | put mp_ndig p uchar add 48 rem x 10 |
| 378 | x = div x 10 |
| 379 | if eq x 0 |
| 380 | break |
| 381 | end |
| 382 | end |
| 383 | cnt = sub 24 p |
| 384 | for j ulong cnt bound 24 |
| 385 | mp_al_push_b get mp_ndig add p j |
| 386 | end |
| 387 | end |
| 388 | |
| 389 | fn mp_emit_alarm entries ulong sbytes ulong maxb ulong freeb ulong flrb ulong to void |
| 390 | put mp_alarm_len 0 0 |
| 391 | mp_al_push_tx 0 |
| 392 | rlen = get mp_root_len 0 |
| 393 | if gt rlen 256 |
| 394 | rlen = ulong 256 |
| 395 | end |
| 396 | for i ulong rlen bound 256 |
| 397 | mp_al_push_b get mp_root i |
| 398 | end |
| 399 | mp_al_push_tx 1 |
| 400 | mp_al_push_num entries |
| 401 | mp_al_push_tx 2 |
| 402 | mp_al_push_num sbytes |
| 403 | mp_al_push_tx 3 |
| 404 | mp_al_push_num maxb |
| 405 | mp_al_push_tx 4 |
| 406 | mp_al_push_num freeb |
| 407 | mp_al_push_tx 5 |
| 408 | mp_al_push_num flrb |
| 409 | mp_al_push_tx 6 |
| 410 | if eq get mp_cfg 11 0 |
| 411 | w = write 2 mp_alarm_buf 0 get mp_alarm_len 0 |
| 412 | end |
| 413 | end |
| 414 | |
| 415 | note ---- ARM64 struct stat & getdents64 Helpers ---- |
| 416 | |
| 417 | fn mp_st_u64 off ulong to ulong |
| 418 | v = ulong 0 |
| 419 | for j ulong 8 |
| 420 | b = ulong get mp_stbuf add off j |
| 421 | v = or v shl b mul j 8 |
| 422 | end |
| 423 | ret v |
| 424 | end |
| 425 | |
| 426 | fn mp_is_lhex ch uchar to bool |
| 427 | if and ge ch 48 le ch 57 |
| 428 | ret true |
| 429 | end |
| 430 | ret and ge ch 97 le ch 102 |
| 431 | end |
| 432 | |
| 433 | note Check whether mp_dents[off..] has a 2-lowercase-hex shard name |
| 434 | fn mp_dent_is_hex2 off ulong lim ulong to bool |
| 435 | p = add off 19 |
| 436 | if gt add p 3 lim |
| 437 | ret false |
| 438 | end |
| 439 | c0 = get mp_dents p |
| 440 | c1 = get mp_dents add p 1 |
| 441 | c2 = get mp_dents add p 2 |
| 442 | if ne c2 0 |
| 443 | ret false |
| 444 | end |
| 445 | if not mp_is_lhex c0 |
| 446 | ret false |
| 447 | end |
| 448 | ret mp_is_lhex c1 |
| 449 | end |
| 450 | |
| 451 | note Return name length of sdent at off (0 if not ending with ".ome" or too long) |
| 452 | fn mp_sdent_ome_len off ulong lim ulong to ulong |
| 453 | p = add off 19 |
| 454 | n = ulong 0 |
| 455 | while lt add p n lim bound 512 |
| 456 | ch = get mp_sdents add p n |
| 457 | if eq ch 0 |
| 458 | break |
| 459 | end |
| 460 | n = add n 1 |
| 461 | end |
| 462 | if lt n 5 |
| 463 | ret 0 |
| 464 | end |
| 465 | e0 = get mp_sdents add p sub n 4 |
| 466 | e1 = get mp_sdents add p sub n 3 |
| 467 | e2 = get mp_sdents add p sub n 2 |
| 468 | e3 = get mp_sdents add p sub n 1 |
| 469 | if or ne e0 46 ne e1 111 |
| 470 | ret 0 |
| 471 | end |
| 472 | if or ne e2 109 ne e3 101 |
| 473 | ret 0 |
| 474 | end |
| 475 | ret n |
| 476 | end |
| 477 | |
| 478 | note Build mp_dir_path = <mp_root>/<c0><c1> |
| 479 | fn mp_build_shard_dir c0 uchar c1 uchar to bool |
| 480 | rl = get mp_root_len 0 |
| 481 | if or eq rl 0 gt add rl 4 4090 |
| 482 | ret false |
| 483 | end |
| 484 | for i ulong rl bound 4090 |
| 485 | put mp_dir_path i get mp_root i |
| 486 | end |
| 487 | put mp_dir_path rl 47 |
| 488 | put mp_dir_path add rl 1 c0 |
| 489 | put mp_dir_path add rl 2 c1 |
| 490 | put mp_dir_plen 0 add rl 3 |
| 491 | ret true |
| 492 | end |
| 493 | |
| 494 | note Build mp_file_path = <mp_dir_path>/<mp_sdents[off+19 .. +nlen)> |
| 495 | fn mp_build_file_path off ulong nlen ulong to bool |
| 496 | dl = get mp_dir_plen 0 |
| 497 | if gt add add dl 1 nlen 4094 |
| 498 | ret false |
| 499 | end |
| 500 | for i ulong dl bound 4094 |
| 501 | put mp_file_path i get mp_dir_path i |
| 502 | end |
| 503 | put mp_file_path dl 47 |
| 504 | base = add off 19 |
| 505 | for j ulong nlen bound 512 |
| 506 | put mp_file_path add add dl 1 j get mp_sdents add base j |
| 507 | end |
| 508 | put mp_file_plen 0 add add dl 1 nlen |
| 509 | ret true |
| 510 | end |
| 511 | |
| 512 | note ---- Oldest-K Candidate Window Helpers for Uncapped Streaming memo_prune ---- |
| 513 | |
| 514 | fn mp_cw_reset to void |
| 515 | put mp_cw_cnt 0 0 |
| 516 | for i ulong 16 |
| 517 | put mp_cw_used i 0 |
| 518 | put mp_cw_sec i 0 |
| 519 | put mp_cw_nsec i 0 |
| 520 | put mp_cw_size i 0 |
| 521 | put mp_cw_plen i 0 |
| 522 | end |
| 523 | end |
| 524 | |
| 525 | note Compare mp_file_path against candidate slot s lexicographically (<0 if file_path < slot) |
| 526 | fn mp_cmp_path_slot s ulong to int |
| 527 | pl1 = get mp_file_plen 0 |
| 528 | pl2 = get mp_cw_plen s |
| 529 | m = pl1 |
| 530 | if lt pl2 m |
| 531 | m = pl2 |
| 532 | end |
| 533 | base = mul s MP_PATH_STRIDE |
| 534 | for i ulong m bound 4096 |
| 535 | b1 = get mp_file_path i |
| 536 | b2 = get mp_cw_path add base i |
| 537 | if lt b1 b2 |
| 538 | ret neg int 1 |
| 539 | end |
| 540 | if gt b1 b2 |
| 541 | ret int 1 |
| 542 | end |
| 543 | end |
| 544 | if lt pl1 pl2 |
| 545 | ret neg int 1 |
| 546 | end |
| 547 | if gt pl1 pl2 |
| 548 | ret int 1 |
| 549 | end |
| 550 | ret int 0 |
| 551 | end |
| 552 | |
| 553 | note Compare two candidate slot paths lexicographically: true if slot a < slot b |
| 554 | fn mp_slot_path_lt a ulong b ulong to bool |
| 555 | la = get mp_cw_plen a |
| 556 | lb = get mp_cw_plen b |
| 557 | m = la |
| 558 | if lt lb m |
| 559 | m = lb |
| 560 | end |
| 561 | ba = mul a MP_PATH_STRIDE |
| 562 | bb = mul b MP_PATH_STRIDE |
| 563 | for i ulong m bound 4096 |
| 564 | ca = get mp_cw_path add ba i |
| 565 | cb = get mp_cw_path add bb i |
| 566 | if lt ca cb |
| 567 | ret true |
| 568 | end |
| 569 | if gt ca cb |
| 570 | ret false |
| 571 | end |
| 572 | end |
| 573 | ret lt la lb |
| 574 | end |
| 575 | |
| 576 | note Compare two candidate slots a and b: true if slot a is older than slot b |
| 577 | fn mp_slot_older a ulong b ulong to bool |
| 578 | x = get mp_cw_sec a |
| 579 | y = get mp_cw_sec b |
| 580 | if lt x y |
| 581 | ret true |
| 582 | end |
| 583 | if gt x y |
| 584 | ret false |
| 585 | end |
| 586 | x = get mp_cw_nsec a |
| 587 | y = get mp_cw_nsec b |
| 588 | if lt x y |
| 589 | ret true |
| 590 | end |
| 591 | if gt x y |
| 592 | ret false |
| 593 | end |
| 594 | ret mp_slot_path_lt a b |
| 595 | end |
| 596 | |
| 597 | note True if current mp_file_path with (sec, nsec) is strictly older than slot s |
| 598 | fn mp_cur_older_than_slot sec ulong nsec ulong s ulong to bool |
| 599 | ss = get mp_cw_sec s |
| 600 | if lt sec ss |
| 601 | ret true |
| 602 | end |
| 603 | if gt sec ss |
| 604 | ret false |
| 605 | end |
| 606 | sn = get mp_cw_nsec s |
| 607 | if lt nsec sn |
| 608 | ret true |
| 609 | end |
| 610 | if gt nsec sn |
| 611 | ret false |
| 612 | end |
| 613 | ret lt mp_cmp_path_slot s 0 |
| 614 | end |
| 615 | |
| 616 | fn mp_cw_store_cur s ulong sec ulong nsec ulong sz ulong to void |
| 617 | put mp_cw_used s 1 |
| 618 | put mp_cw_sec s sec |
| 619 | put mp_cw_nsec s nsec |
| 620 | put mp_cw_size s sz |
| 621 | pl = get mp_file_plen 0 |
| 622 | put mp_cw_plen s pl |
| 623 | base = mul s MP_PATH_STRIDE |
| 624 | for i ulong pl bound 4096 |
| 625 | put mp_cw_path add base i get mp_file_path i |
| 626 | end |
| 627 | end |
| 628 | |
| 629 | fn mp_cw_consider sec ulong nsec ulong sz ulong to void |
| 630 | cnt = get mp_cw_cnt 0 |
| 631 | if lt cnt MP_PRUNE_WIN |
| 632 | mp_cw_store_cur cnt sec nsec sz |
| 633 | put mp_cw_cnt 0 add cnt 1 |
| 634 | ret |
| 635 | end |
| 636 | newest = ulong 0 |
| 637 | for s ulong 16 |
| 638 | if mp_slot_older newest s |
| 639 | newest = s |
| 640 | end |
| 641 | end |
| 642 | if mp_cur_older_than_slot sec nsec newest |
| 643 | mp_cw_store_cur newest sec nsec sz |
| 644 | end |
| 645 | end |
| 646 | |
| 647 | note ---- Uncapped Store Scanner & Disk-Floor Alarm ---- |
| 648 | |
| 649 | fn mp_scan_one_ome o ulong lim ulong collect_prune bool to void |
| 650 | nlen = mp_sdent_ome_len o lim |
| 651 | if eq nlen 0 |
| 652 | ret |
| 653 | end |
| 654 | if not mp_build_file_path o nlen |
| 655 | ret |
| 656 | end |
| 657 | sr = stat mp_file_path 0 get mp_file_plen 0 mp_stbuf 0 128 |
| 658 | if ne sr 0 |
| 659 | ret |
| 660 | end |
| 661 | sz = mp_st_u64 48 |
| 662 | put mp_stat 0 add get mp_stat 0 1 |
| 663 | put mp_stat 1 add get mp_stat 1 sz |
| 664 | if collect_prune |
| 665 | mp_cw_consider mp_st_u64 88 mp_st_u64 96 sz |
| 666 | end |
| 667 | end |
| 668 | |
| 669 | fn mp_sdent_recl o ulong to ulong |
| 670 | b0 = ulong get mp_sdents add o 16 |
| 671 | b1 = shl ulong get mp_sdents add o 17 8 |
| 672 | ret or b0 b1 |
| 673 | end |
| 674 | |
| 675 | fn mp_dent_recl o ulong to ulong |
| 676 | b0 = ulong get mp_dents add o 16 |
| 677 | b1 = shl ulong get mp_dents add o 17 8 |
| 678 | ret or b0 b1 |
| 679 | end |
| 680 | |
| 681 | fn mp_scan_shard_buf lim ulong collect_prune bool to void |
| 682 | o = ulong 0 |
| 683 | while lt o lim bound 1000000 |
| 684 | rl = mp_sdent_recl o |
| 685 | if eq rl 0 |
| 686 | break |
| 687 | end |
| 688 | mp_scan_one_ome o lim collect_prune |
| 689 | o = add o rl |
| 690 | end |
| 691 | end |
| 692 | |
| 693 | fn mp_scan_shard collect_prune bool to void |
| 694 | dfd = fopen mp_dir_path 0 get mp_dir_plen 0 MP_O_DIR 0 |
| 695 | if lt dfd 0 |
| 696 | ret |
| 697 | end |
| 698 | going = true |
| 699 | while going bound 1000000 |
| 700 | nr = getdnts int dfd mp_sdents 0 65536 |
| 701 | if le nr 0 |
| 702 | going = false |
| 703 | else |
| 704 | mp_scan_shard_buf ulong nr collect_prune |
| 705 | end |
| 706 | end |
| 707 | rc = fclose int dfd |
| 708 | end |
| 709 | |
| 710 | fn mp_walk_one_dent o ulong lim ulong collect_prune bool to void |
| 711 | if not mp_dent_is_hex2 o lim |
| 712 | ret |
| 713 | end |
| 714 | c0 = get mp_dents add o 19 |
| 715 | c1 = get mp_dents add o 20 |
| 716 | if mp_build_shard_dir c0 c1 |
| 717 | mp_scan_shard collect_prune |
| 718 | end |
| 719 | end |
| 720 | |
| 721 | fn mp_walk_buf lim ulong collect_prune bool to void |
| 722 | o = ulong 0 |
| 723 | while lt o lim bound 100000 |
| 724 | recl = mp_dent_recl o |
| 725 | if eq recl 0 |
| 726 | break |
| 727 | end |
| 728 | mp_walk_one_dent o lim collect_prune |
| 729 | o = add o recl |
| 730 | end |
| 731 | end |
| 732 | |
| 733 | fn mp_walk_store collect_prune bool to ulong |
| 734 | mp_ensure_init |
| 735 | put mp_stat 0 0 |
| 736 | put mp_stat 1 0 |
| 737 | if collect_prune |
| 738 | mp_cw_reset |
| 739 | end |
| 740 | rl = get mp_root_len 0 |
| 741 | if eq rl 0 |
| 742 | ret MP_DISK_ERR_IO |
| 743 | end |
| 744 | rfd = fopen mp_root 0 rl MP_O_DIR 0 |
| 745 | if lt rfd 0 |
| 746 | if eq rfd neg long 2 |
| 747 | ret MP_DISK_OK |
| 748 | end |
| 749 | ret MP_DISK_ERR_IO |
| 750 | end |
| 751 | going = true |
| 752 | while going bound 100000 |
| 753 | nr = getdnts int rfd mp_dents 0 65536 |
| 754 | if le nr 0 |
| 755 | going = false |
| 756 | else |
| 757 | mp_walk_buf ulong nr collect_prune |
| 758 | end |
| 759 | end |
| 760 | rc = fclose int rfd |
| 761 | ret MP_DISK_OK |
| 762 | end |
| 763 | |
| 764 | fn mp_scan_store to ulong |
| 765 | ret mp_walk_store false |
| 766 | end |
| 767 | |
| 768 | fn mp_compute_free sbytes ulong to ulong |
| 769 | if ne get mp_cfg 8 0 |
| 770 | ret get mp_cfg 9 |
| 771 | end |
| 772 | cap = get mp_cfg 7 |
| 773 | if ge sbytes cap |
| 774 | ret 0 |
| 775 | end |
| 776 | ret sub cap sbytes |
| 777 | end |
| 778 | |
| 779 | fn mp_check_disk_floor to ulong |
| 780 | mp_ensure_init |
| 781 | wrc = mp_scan_store |
| 782 | if ne wrc MP_DISK_OK |
| 783 | ret wrc |
| 784 | end |
| 785 | ent = get mp_stat 0 |
| 786 | sbytes = get mp_stat 1 |
| 787 | freeb = mp_compute_free sbytes |
| 788 | put mp_stat 2 freeb |
| 789 | maxb = get mp_cfg 5 |
| 790 | flrb = get mp_cfg 6 |
| 791 | breach = false |
| 792 | if and gt maxb 0 gt sbytes maxb |
| 793 | breach = true |
| 794 | end |
| 795 | if and gt flrb 0 lt freeb flrb |
| 796 | breach = true |
| 797 | end |
| 798 | if breach |
| 799 | put mp_stat 3 1 |
| 800 | put mp_stat 4 add get mp_stat 4 1 |
| 801 | mp_emit_alarm ent sbytes maxb freeb flrb |
| 802 | ret MP_ALARM_DISK_FLOOR |
| 803 | end |
| 804 | put mp_stat 3 0 |
| 805 | ret MP_DISK_OK |
| 806 | end |
| 807 | |
| 808 | fn mp_cw_find_oldest start ulong cnt ulong to ulong |
| 809 | best = start |
| 810 | j = add start 1 |
| 811 | while lt j cnt bound 16 |
| 812 | if mp_slot_older get mp_cw_ord j get mp_cw_ord best |
| 813 | best = j |
| 814 | end |
| 815 | j = add j 1 |
| 816 | end |
| 817 | ret best |
| 818 | end |
| 819 | |
| 820 | note Sort mp_cw_ord[0..cnt) oldest-first (selection sort over <= 16 entries) |
| 821 | fn mp_cw_sort to void |
| 822 | cnt = get mp_cw_cnt 0 |
| 823 | for i ulong cnt bound 16 |
| 824 | put mp_cw_ord i i |
| 825 | end |
| 826 | for i ulong cnt bound 16 |
| 827 | best = mp_cw_find_oldest i cnt |
| 828 | if ne best i |
| 829 | tmp = get mp_cw_ord i |
| 830 | put mp_cw_ord i get mp_cw_ord best |
| 831 | put mp_cw_ord best tmp |
| 832 | end |
| 833 | end |
| 834 | end |
| 835 | |
| 836 | fn mp_unlink_cw_slot s ulong to bool |
| 837 | pl = get mp_cw_plen s |
| 838 | base = mul s MP_PATH_STRIDE |
| 839 | for i ulong pl bound 4096 |
| 840 | put mp_file_path i get mp_cw_path add base i |
| 841 | end |
| 842 | ur = unlink mp_file_path 0 pl |
| 843 | ret eq ur 0 |
| 844 | end |
| 845 | |
| 846 | fn mp_prune_batch cur_bytes ulong target_bytes ulong to ulong |
| 847 | mp_cw_sort |
| 848 | cnt = get mp_cw_cnt 0 |
| 849 | rem_cnt = ulong 0 |
| 850 | cur = cur_bytes |
| 851 | for i ulong cnt bound 16 |
| 852 | if le cur target_bytes |
| 853 | break |
| 854 | end |
| 855 | s = get mp_cw_ord i |
| 856 | sz = get mp_cw_size s |
| 857 | if mp_unlink_cw_slot s |
| 858 | rem_cnt = add rem_cnt 1 |
| 859 | put mp_stat 5 add get mp_stat 5 1 |
| 860 | put mp_stat 6 add get mp_stat 6 sz |
| 861 | if ge cur sz |
| 862 | cur = sub cur sz |
| 863 | else |
| 864 | cur = ulong 0 |
| 865 | end |
| 866 | end |
| 867 | end |
| 868 | ret rem_cnt |
| 869 | end |
| 870 | |
| 871 | fn mp_refresh_alarm_state to ulong |
| 872 | rc = mp_scan_store |
| 873 | if ne rc MP_DISK_OK |
| 874 | ret rc |
| 875 | end |
| 876 | sbytes = get mp_stat 1 |
| 877 | freeb = mp_compute_free sbytes |
| 878 | put mp_stat 2 freeb |
| 879 | maxb = get mp_cfg 5 |
| 880 | flrb = get mp_cfg 6 |
| 881 | breach = false |
| 882 | if and gt maxb 0 gt sbytes maxb |
| 883 | breach = true |
| 884 | end |
| 885 | if and gt flrb 0 lt freeb flrb |
| 886 | breach = true |
| 887 | end |
| 888 | if breach |
| 889 | put mp_stat 3 1 |
| 890 | else |
| 891 | put mp_stat 3 0 |
| 892 | end |
| 893 | ret MP_DISK_OK |
| 894 | end |
| 895 | |
| 896 | note Explicit oldest-first memo_prune down to target_bytes (no hard entry cap; never silent) |
| 897 | fn mp_memo_prune target_bytes ulong to ulong |
| 898 | mp_ensure_init |
| 899 | put mp_stat 5 0 |
| 900 | put mp_stat 6 0 |
| 901 | pass = ulong 0 |
| 902 | while lt pass 1000000 bound 1000000 |
| 903 | rc = mp_walk_store true |
| 904 | if ne rc MP_DISK_OK |
| 905 | ret rc |
| 906 | end |
| 907 | cur_bytes = get mp_stat 1 |
| 908 | if le cur_bytes target_bytes |
| 909 | break |
| 910 | end |
| 911 | if eq get mp_cw_cnt 0 0 |
| 912 | break |
| 913 | end |
| 914 | if eq mp_prune_batch cur_bytes target_bytes 0 |
| 915 | break |
| 916 | end |
| 917 | pass = add pass 1 |
| 918 | end |
| 919 | ret mp_refresh_alarm_state |
| 920 | end |