vile / scheme / arena_usage_host_test.v
118 lines · 111 sloc · 3.58 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5@[heap]
6struct UsageStatsProbe {
7mut:
8 peak i64
9 shrunk i64
10}
11
12fn test_sampled_usage_can_shrink_and_miss_a_temporary_peak() {
13 mut probe := &UsageStatsProbe{}
14 mut region := arena.new(chunk_size: 1024 * 1024)
15 defer { region.free() }
16 region_ref := &region
17 work := fn [mut probe, region_ref] (mut _vm VM) !Value {
18 // Exercise the standard allocator's documented latest-allocation shrink.
19 // No VM value points at this raw test buffer, and it remains arena owned.
20 buffer := unsafe { malloc(128 * 1024) }
21 probe.peak = i64(region_ref.used())
22 _ := unsafe { realloc_data(buffer, 128 * 1024, 8) }
23 probe.shrunk = i64(region_ref.used())
24 return integer(42)
25 }
26 region.push()
27 defer { region.pop() }
28 mut vm := new()
29 limit := i64(region.used()) + 32768
30 value := vm.with_arena_usage_limit(&region, limit, work) or { panic(err.msg()) }
31 assert value.whole() == 42
32 assert probe.peak > limit && probe.shrunk < limit && probe.shrunk < probe.peak
33 assert region.capacity() >= 1024 * 1024 && region.used() < region.capacity()
34 assert isnil(vm.system.evaluation)
35}
36
37struct UsageExport {
38 text string
39 limit i64
40 used i64
41}
42
43fn usage_export_borrowed(region &arena.Arena) UsageExport {
44 region.push()
45 defer { region.pop() }
46 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 4 }) or { panic(err.msg()) }
47 limit := i64(region.used()) + 128 * 1024
48 if _ := vm.with_arena_usage_limit(region, limit, fn (mut vm VM) !Value {
49 return vm.eval('(let loop ((n 0)) (cons n n) (loop (+ n 1)))')
50 }) {
51 panic('allocating loop unexpectedly returned')
52 } else {
53 assert err is ArenaUsageLimitRequest
54 if err is ArenaUsageLimitRequest {
55 return UsageExport{ text: err.msg(), limit: err.limit, used: err.used }
56 }
57 panic(err.msg())
58 }
59}
60
61fn test_usage_request_text_exports_after_pop_and_survives_repeated_reset_free() {
62 mut region := arena.new()
63 mut capacity := 0
64 mut retained := ''
65 mut steady_used := 0
66 mut first_capacity := 0
67 for index in 0 .. 24 {
68 borrowed := usage_export_borrowed(&region)
69 assert borrowed.used > borrowed.limit
70 text := borrowed.text.clone()
71 if index == 0 { first_capacity = region.capacity() }
72 assert region.capacity() <= first_capacity * 4
73 // Reset retains the largest chunk, so the next scope can need a
74 // second growth before its working set fits that retained chunk.
75 if index == 8 {
76 capacity = region.capacity()
77 steady_used = region.used()
78 }
79 if index > 8 {
80 assert region.capacity() == capacity && region.used() == steady_used
81 }
82 region.reset()
83 assert region.used() == 0
84 assert text.contains('arena usage limit exceeded')
85 if index == 0 { retained = text } else { unsafe { text.free() } }
86 }
87 region.free()
88 assert retained.contains('bytes used; limit')
89 unsafe { retained.free() }
90}
91
92fn test_usage_scope_preserves_foreign_procedure_error_and_vm_ownership() {
93 mut other := new()
94 foreign := other.eval('(lambda () 42)') or { panic(err.msg()) }
95 work := fn [foreign] (mut vm VM) !Value {
96 return vm.call(foreign, [])
97 }
98 mut region := arena.new()
99 defer { region.free() }
100 region.push()
101 defer { region.pop() }
102 mut vm := new()
103 if _ := vm.with_arena_usage_limit(&region, i64(region.used()) + 65536, work) {
104 assert false
105 } else {
106 assert err is EvalError && err.msg().contains('another VM')
107 }
108 assert isnil(vm.system.evaluation) && vm.active.len == 0
109 assert other.call(foreign, []) or { panic(err.msg()) }.whole() == 42
110 mut uninitialized := VM{}
111 if _ := uninitialized.with_arena_usage_limit(&region, 0, fn (mut _vm VM) !Value {
112 return integer(1)
113 }) {
114 assert false
115 } else {
116 assert err is EvalError && err.msg().contains('uninitialized VM')
117 }
118}
119