segment_heap.mx raw

   1  // Phase 2 acceptance: the segment heap's observable invariants.
   2  //
   3  //   - a warm function-arena loop maps nothing: fn chunks come from the domain's
   4  //     LIFO pool, so the call depth is paid for once (D1, section 7);
   5  //   - the arena ledger balances at a quiescent point: every push has its
   6  //     release and the frame depth is zero (phase 0b);
   7  //   - an object larger than a root segment takes a LARGE segment instead of
   8  //     becoming the allocation cursor (G4, D8);
   9  //   - the allocation-size histogram exists and has counted the program's
  10  //     allocations (phase 0b, the input to D8/D9);
  11  //   - a sovereign receiver's accumulation is still correct after the change of
  12  //     backing store (section 0: ownership semantics do not change).
  13  //
  14  //:wasmtest skip
  15  package main
  16  
  17  import (
  18  	"fmt"
  19  	"runtime"
  20  )
  21  
  22  func deep(n int32) (out []int64) {
  23  	if n == 0 {
  24  		out = []int64{:8}
  25  		return
  26  	}
  27  	out = deep(n - 1)
  28  	return
  29  }
  30  
  31  type Accum struct {
  32  	n     int64
  33  	items []int64
  34  }
  35  
  36  func (a *Accum) Add(v int64) {
  37  	a.n++
  38  	a.items = push(a.items, v)
  39  }
  40  
  41  func (a *Accum) Total() (t int64) {
  42  	for i := int32(0); i < len(a.items); i++ {
  43  		t += a.items[i]
  44  	}
  45  	return
  46  }
  47  
  48  func main() {
  49  	select {
  50  	case <-shutdown:
  51  		return
  52  	default:
  53  	}
  54  
  55  	// Warm the pool to this program's call depth.
  56  	for i := int32(0); i < 200; i++ {
  57  		deep(24)
  58  	}
  59  	maps0, unmaps0, _, _ := runtime.SysMapStats()
  60  	for i := int32(0); i < 2000; i++ {
  61  		deep(24)
  62  	}
  63  	maps1, unmaps1, _, _ := runtime.SysMapStats()
  64  	if maps1 != maps0 || unmaps1 != unmaps0 {
  65  		panic("a warm function-arena loop made a mapping syscall")
  66  	}
  67  
  68  	var acc Accum
  69  	for i := int32(0); i < 500; i++ {
  70  		acc.Add(int64(i))
  71  	}
  72  	if acc.Total() != 124750 {
  73  		panic("sovereign accumulation incorrect")
  74  	}
  75  
  76  	st := runtime.SegmentStats{}
  77  	runtime.ReadSegmentStats(&st)
  78  	if st.FnCount == 0 || st.FnBytes == 0 {
  79  		panic("no function segments are accounted")
  80  	}
  81  	if st.DomainCount != 1 || st.DomainBytes == 0 {
  82  		panic("the domain segment is missing from the ledger")
  83  	}
  84  
  85  	// A big object is a LARGE segment, never the cursor. Root-lifetime
  86  	// allocation is the existing idiom for a value that must outlive main's
  87  	// own frame.
  88  	runtime.SovereignSetArena(runtime.RootArena())
  89  	big := []byte{:2 * 1024 * 1024}
  90  	if len(big) != 2*1024*1024 || big[len(big)-1] != 0 {
  91  		panic("the large allocation lost its length or its zeroing")
  92  	}
  93  	st2 := runtime.SegmentStats{}
  94  	runtime.ReadSegmentStats(&st2)
  95  	if st2.LargeCount <= st.LargeCount || st2.LargeBytes == st.LargeBytes {
  96  		panic("a 2 MB object did not take a LARGE segment")
  97  	}
  98  
  99  	cnts, bytes := runtime.AllocHistogram()
 100  	if len(cnts) == 0 || len(bytes) == 0 {
 101  		panic("the allocation histogram is not carved")
 102  	}
 103  	total := uint64(0)
 104  	for i := int32(0); i < len(cnts); i++ {
 105  		total += cnts[i]
 106  	}
 107  	if total == 0 {
 108  		panic("the allocation histogram counted nothing")
 109  	}
 110  
 111  	pushed, released, depth, _ := runtime.ArenaLedger()
 112  	fmt.Println("ledger", pushed, released, depth)
 113  	if pushed != released {
 114  		panic("the arena ledger does not balance")
 115  	}
 116  	fmt.Println("segment heap ok", maps0, maps1, st2.FnCount, total)
 117  }
 118