code wiki / _hdl_build / nx_crawl_callee_probe.nx
nx_crawl_callee_probe.nx source
↩ module page · 203 lines · 9566 B
1// nx_crawl_callee_probe.nx -- PER-CALLEE VmSize PROBE for the crawler's per-page pipeline (debt 1787082132).
2// The +282 MB/min ingest growth survived the seg-store release-list fix; the named per-page mappings
3// (wc_harvest 8 KiB + olh_scan ~9.5 KiB) explain only ~20 KB/page, so the dominant allocator is in the
4// per-page CALLEES. This organ MEASURES each callee instead of reading it (the debt row's own law:
5// "do NOT hoist by reading"): run the SAME page bytes through each stage N times and diff
6// /proc/self/status VmSize around the loop. Per stage it prints BOTH numbers that matter:
7// warm_kb = VmSize cost of the FIRST call (one-time lazy tables/policy loads -- not a leak)
8// steady per-call = (delta over N calls after warm) / N -- the number that scales with pages/min
9// Stages: H2T (nx_html_to_text), FEED (nx_feed_discover + nx_feed_extract), OLH (olh_scan),
10// FETCH (nx_https_fetch_follow -- only when /tmp/mozilla_certdata.txt exists AND the arg is a url;
11// otherwise that stage prints UNOBSERVABLE and abstains rather than acquitting).
12// argv: nx_crawl_callee_probe <page-file-or-https-url> [iters] (file mode = no network, deterministic)
13// Output: one CALLEE line per stage; the LAST line names the worst steady grower (positional anchor).
14// license_tier: ORIGINAL expect_exit: 0
15import "nx_x509_trust_store.nx"
16import "nx_trust_store_load_from_certdata.nx"
17import "nx_https_fetch_follow.nx"
18import "nx_outlink_harvest.nx"
19import "nx_feed_extract.nx"
20
21const CP_STATUSB: i64 = 8192 // /proc/self/status read buffer
22const CP_PAGECAP: i64 = 8388608 // page fixture cap == the crawler's CI_RAWCAP (largest raw file it reads)
23const CP_TXTCAP: i64 = 1048576 // html_to_text output cap (crawler docs are <= ~900 KB)
24const CP_FEEDCAP: i64 = 65536 // feed extractor output cap (== its own K_MAGIC_65536)
25const CP_URLCAP: i64 = 2048 // discovered feed url
26const CP_EDGES: i64 = 1024 // olh_scan edge slots (== OLH_MAXEDGE)
27const CP_ITERS: i64 = 24 // default steady-state iterations per stage
28const CP_CERTDATA: *u8 = "/tmp/mozilla_certdata.txt"
29const CP_FETCH_HOPS: i64 = 5
30
31func cp_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
32static cp_nb_g: *u8
33func cp_wn(v: i64) -> i64 {
34 if (cp_nb_g as i64) == 0 { cp_nb_g = sys_mmap(64) }
35 var m: i64 = v
36 if m < 0 { cp_w("-" as *u8); m = 0 - m }
37 if m == 0 { cp_w("0" as *u8); return 0 }
38 var k: i64 = 0
39 while m > 0 { cp_nb_g[32 + k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
40 var i: i64 = 0
41 while i < k { cp_nb_g[i] = cp_nb_g[32 + k - 1 - i]; i = i + 1 }
42 sys_write(1, cp_nb_g, k)
43 return 0
44}
45
46// VmSize in kB from /proc/self/status. LINE-ANCHORED: the value is taken only from a line that STARTS
47// with the key (offset 0 or preceded by newline) -- the rm_field lesson, so prose can never be the answer.
48static cp_stb_g: *u8
49func cp_vmsize() -> i64 {
50 if (cp_stb_g as i64) == 0 { cp_stb_g = sys_mmap(CP_STATUSB) }
51 let fd: i64 = sys_openat_rd("/proc/self/status" as *u8)
52 if fd < 0 { return 0 - 1 }
53 let n: i64 = sys_read(fd, cp_stb_g, CP_STATUSB - 1)
54 sys_close(fd)
55 if n <= 0 { return 0 - 1 }
56 let key: *u8 = "VmSize:" as *u8
57 var i: i64 = 0
58 while i < n {
59 var atline: i64 = 0
60 if i == 0 { atline = 1 } else { if cp_stb_g[i - 1] == (10 as u8) { atline = 1 } }
61 if atline == 1 {
62 var j: i64 = 0
63 while key[j] != (0 as u8) { if i + j >= n { return 0 - 1 } if cp_stb_g[i + j] != key[j] { j = 0 - 1; break } j = j + 1 }
64 if j > 0 {
65 var p: i64 = i + j
66 var v: i64 = 0
67 var seen: i64 = 0
68 while p < n {
69 let c: i64 = cp_stb_g[p] as i64
70 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); seen = 1; p = p + 1; continue } }
71 if seen == 1 { break }
72 if c == 10 { break }
73 p = p + 1
74 }
75 if seen == 1 { return v }
76 return 0 - 1
77 }
78 }
79 i = i + 1
80 }
81 return 0 - 1
82}
83
84func cp_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
85
86// one probe stage report line. delta values in kB (VmSize granularity).
87func cp_report(name: *u8, warm_kb: i64, steady_kb: i64, iters: i64) -> i64 {
88 cp_w("CALLEE " as *u8); cp_w(name)
89 cp_w(" warm_kb=" as *u8); cp_wn(warm_kb)
90 cp_w(" steady_delta_kb=" as *u8); cp_wn(steady_kb)
91 cp_w(" n=" as *u8); cp_wn(iters)
92 cp_w(" per_call_kb=" as *u8); cp_wn(steady_kb / iters)
93 cp_w("\n" as *u8)
94 return 0
95}
96
97func main(argc: i64, argv: *i64) -> i64 {
98 if argc < 2 {
99 cp_w("usage: nx_crawl_callee_probe <page-file-or-https-url> [iters]\n" as *u8)
100 return 2
101 }
102 let src: *u8 = argv[1] as *u8
103 var iters: i64 = CP_ITERS
104 if argc > 2 {
105 let a2: *u8 = argv[2] as *u8
106 var vv: i64 = 0
107 var ii: i64 = 0
108 while a2[ii] != (0 as u8) { let c: i64 = a2[ii] as i64; if c >= 48 { if c <= 57 { vv = vv * 10 + (c - 48) } } ii = ii + 1 }
109 if vv > 0 { iters = vv }
110 }
111 var is_url: i64 = 0
112 if src[0] == (104 as u8) { if src[1] == (116 as u8) { is_url = 1 } } // "ht..." = url
113
114 // ---- acquire the page fixture (before ANY measurement) ----
115 let page: *u8 = sys_mmap(CP_PAGECAP)
116 var plen: i64 = 0
117 var store: *TrustStore = 0 as *TrustStore
118 if is_url == 1 {
119 let kfd: i64 = sys_openat_rd(CP_CERTDATA)
120 if kfd < 0 {
121 cp_w("FETCH UNOBSERVABLE: no /tmp/mozilla_certdata.txt -- pass a page FILE instead\n" as *u8)
122 return 2
123 }
124 sys_close(kfd)
125 let r: i64 = nx_trust_store_load_from_certdata(CP_CERTDATA, 300, 4194304)
126 if r <= 0 { cp_w("FETCH UNOBSERVABLE: trust store load failed\n" as *u8); return 2 }
127 store = r as *TrustStore
128 let stp: *i64 = sys_mmap(16) as *i64
129 plen = nx_https_fetch_follow(src, store, page, CP_PAGECAP, CP_FETCH_HOPS, stp)
130 cp_w("fixture: fetched status=" as *u8); cp_wn(stp[0]); cp_w(" bytes=" as *u8); cp_wn(plen); cp_w("\n" as *u8)
131 } else {
132 let lp: *i64 = sys_mmap(16) as *i64
133 let b: *u8 = sys_read_file(src, lp)
134 if (b as i64) == 0 { cp_w("PAGE-FILE UNREADABLE\n" as *u8); return 2 }
135 plen = lp[0]
136 if plen > CP_PAGECAP { plen = CP_PAGECAP }
137 var ci: i64 = 0
138 while ci < plen { page[ci] = b[ci]; ci = ci + 1 }
139 cp_w("fixture: file bytes=" as *u8); cp_wn(plen); cp_w("\n" as *u8)
140 }
141 if plen <= 0 { cp_w("EMPTY FIXTURE -- nothing to measure\n" as *u8); return 2 }
142
143 // ---- output buffers, all allocated BEFORE the first measurement ----
144 let txt: *u8 = sys_mmap(CP_TXTCAP)
145 let feed: *u8 = sys_mmap(CP_FEEDCAP)
146 let feedurl: *u8 = sys_mmap(CP_URLCAP)
147 let edges: *i64 = sys_mmap(8 * CP_EDGES + 64) as *i64
148 let base: *u8 = "https://nishifamily.com/" as *u8
149 let blen: i64 = cp_slen(base)
150
151 var worst_kb: i64 = 0 - 1
152 var worst_name: *u8 = "none" as *u8
153
154 // ---- H2T: nx_html_to_text ----
155 var v0: i64 = cp_vmsize()
156 nx_html_to_text(page, plen, txt, CP_TXTCAP)
157 var v1: i64 = cp_vmsize()
158 var it: i64 = 0
159 while it < iters { nx_html_to_text(page, plen, txt, CP_TXTCAP); it = it + 1 }
160 var v2: i64 = cp_vmsize()
161 cp_report("nx_html_to_text" as *u8, v1 - v0, v2 - v1, iters)
162 if (v2 - v1) / iters > worst_kb { worst_kb = (v2 - v1) / iters; worst_name = "nx_html_to_text" as *u8 }
163
164 // ---- FEED: nx_feed_discover + nx_feed_extract ----
165 v0 = cp_vmsize()
166 nx_feed_discover(page, plen, feedurl, CP_URLCAP)
167 nx_feed_extract(page, plen, feed, CP_FEEDCAP)
168 v1 = cp_vmsize()
169 it = 0
170 while it < iters { nx_feed_discover(page, plen, feedurl, CP_URLCAP); nx_feed_extract(page, plen, feed, CP_FEEDCAP); it = it + 1 }
171 v2 = cp_vmsize()
172 cp_report("nx_feed_extract" as *u8, v1 - v0, v2 - v1, iters)
173 if (v2 - v1) / iters > worst_kb { worst_kb = (v2 - v1) / iters; worst_name = "nx_feed_extract" as *u8 }
174
175 // ---- OLH: olh_scan (the known ~9.5 KiB/call pair rides inside) ----
176 v0 = cp_vmsize()
177 olh_scan(page, plen, base, blen, edges, CP_EDGES)
178 v1 = cp_vmsize()
179 it = 0
180 while it < iters { olh_scan(page, plen, base, blen, edges, CP_EDGES); it = it + 1 }
181 v2 = cp_vmsize()
182 cp_report("olh_scan" as *u8, v1 - v0, v2 - v1, iters)
183 if (v2 - v1) / iters > worst_kb { worst_kb = (v2 - v1) / iters; worst_name = "olh_scan" as *u8 }
184
185 // ---- FETCH: nx_https_fetch_follow (url mode only; store already loaded above) ----
186 if is_url == 1 {
187 let stf: *i64 = sys_mmap(16) as *i64
188 v0 = cp_vmsize()
189 nx_https_fetch_follow(src, store, txt, CP_TXTCAP, CP_FETCH_HOPS, stf)
190 v1 = cp_vmsize()
191 it = 0
192 while it < iters { nx_https_fetch_follow(src, store, txt, CP_TXTCAP, CP_FETCH_HOPS, stf); it = it + 1 }
193 v2 = cp_vmsize()
194 cp_report("nx_https_fetch_follow" as *u8, v1 - v0, v2 - v1, iters)
195 if (v2 - v1) / iters > worst_kb { worst_kb = (v2 - v1) / iters; worst_name = "nx_https_fetch_follow" as *u8 }
196 } else {
197 cp_w("CALLEE nx_https_fetch_follow UNOBSERVABLE (file-mode fixture; pass a url + certdata to measure)\n" as *u8)
198 }
199
200 // LAST LINE = the verdict the reader anchors on (positional, gv_last_line discipline).
201 cp_w("WORST " as *u8); cp_w(worst_name); cp_w(" per_call_kb=" as *u8); cp_wn(worst_kb); cp_w("\n" as *u8)
202 return 0
203}