1 // Phase 6 acceptance: the unified ring's frame protocol.
2 //
3 // A message several times the ring's data area crosses a spawn channel
4 // byte-identical and in order, through the select path on the receiving side.
5 // The ring is 128 KiB of data (D4), so a 300 KB frame proves the chaining
6 // (D3), and the channel ledger proves the ring is no longer sized by the
7 // worst message: five rings (control + two per direction) must stay under
8 // 1 MiB of address space, where the old 32 MiB reservation would have been
9 // 128 MiB per spawn.
10 //
11 //:wasmtest skip
12 package main
13 14 import (
15 "fmt"
16 "runtime"
17 )
18 19 type Wire struct {
20 ID int64
21 Body []byte `cap:"300000"`
22 }
23 24 func echo(in chan Wire, out chan Wire) (closed chan struct{}) {
25 for {
26 select {
27 case <-closed:
28 return closed
29 case w, ok := <-in:
30 if !ok {
31 return closed
32 }
33 out <- w
34 }
35 }
36 }
37 38 func fill(n int32, seed byte) (b []byte) {
39 b = []byte{:n}
40 for i := int32(0); i < n; i++ {
41 b[i] = byte((i + int32(seed)) & 255)
42 }
43 return
44 }
45 46 func check(b []byte, seed byte) (ok bool) {
47 ok = true
48 for i := int32(0); i < len(b); i++ {
49 if b[i] != byte((i+int32(seed))&255) {
50 ok = false
51 }
52 }
53 return
54 }
55 56 func main() {
57 select {
58 case <-shutdown:
59 return
60 default:
61 }
62 // Chan memory must outlive this frame: the child reads the same channel
63 // storage, so it belongs to the root arena.
64 runtime.SovereignSetArena(runtime.RootArena())
65 in := chan Wire{:4}
66 out := chan Wire{:4}
67 done := spawn(echo, in, out)
68 69 for k := int32(0); k < 3; k++ {
70 n := int32(300000)
71 if k == 1 {
72 n = 100
73 }
74 in <- Wire{ID: int64(k), Body: fill(n, byte(k))}
75 got := <-out
76 if got.ID != int64(k) || len(got.Body) != n || !check(got.Body, byte(k)) {
77 panic("chained message mismatch")
78 }
79 }
80 close(in)
81 <-done
82 83 st := runtime.SegmentStats{}
84 runtime.ReadSegmentStats(&st)
85 if st.ChanCount == 0 {
86 panic("no channel rings are accounted")
87 }
88 if st.ChanBytes > 1<<20 {
89 panic("a spawn's rings reserve more than 1 MiB; frames are not chaining")
90 }
91 fmt.Println("ring chain ok", st.ChanCount, st.ChanBytes)
92 }
93