github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/runtime/mwbbuf.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // This implements the write barrier buffer. The write barrier itself 6 // is gcWriteBarrier and is implemented in assembly. 7 // 8 // The write barrier has a fast path and a slow path. The fast path 9 // simply enqueues to a per-P write barrier buffer. It's written in 10 // assembly and doesn't clobber any general purpose registers, so it 11 // doesn't have the usual overheads of a Go call. 12 // 13 // When the buffer fills up, the write barrier invokes the slow path 14 // (wbBufFlush) to flush the buffer to the GC work queues. In this 15 // path, since the compiler didn't spill registers, we spill *all* 16 // registers and disallow any GC safe points that could observe the 17 // stack frame (since we don't know the types of the spilled 18 // registers). 19 20 package runtime 21 22 import ( 23 "runtime/internal/sys" 24 "unsafe" 25 ) 26 27 // testSmallBuf forces a small write barrier buffer to stress write 28 // barrier flushing. 29 const testSmallBuf = false 30 31 // wbBuf is a per-P buffer of pointers queued by the write barrier. 32 // This buffer is flushed to the GC workbufs when it fills up and on 33 // various GC transitions. 34 // 35 // This is closely related to a "sequential store buffer" (SSB), 36 // except that SSBs are usually used for maintaining remembered sets, 37 // while this is used for marking. 38 type wbBuf struct { 39 // next points to the next slot in buf. It must not be a 40 // pointer type because it can point past the end of buf and 41 // must be updated without write barriers. 42 // 43 // This is a pointer rather than an index to optimize the 44 // write barrier assembly. 45 next uintptr 46 47 // end points to just past the end of buf. It must not be a 48 // pointer type because it points past the end of buf and must 49 // be updated without write barriers. 50 end uintptr 51 52 // buf stores a series of pointers to execute write barriers 53 // on. This must be a multiple of wbBufEntryPointers because 54 // the write barrier only checks for overflow once per entry. 55 buf [wbBufEntryPointers * wbBufEntries]uintptr 56 } 57 58 const ( 59 // wbBufEntries is the number of write barriers between 60 // flushes of the write barrier buffer. 61 // 62 // This trades latency for throughput amortization. Higher 63 // values amortize flushing overhead more, but increase the 64 // latency of flushing. Higher values also increase the cache 65 // footprint of the buffer. 66 // 67 // TODO: What is the latency cost of this? Tune this value. 68 wbBufEntries = 256 69 70 // wbBufEntryPointers is the number of pointers added to the 71 // buffer by each write barrier. 72 wbBufEntryPointers = 2 73 ) 74 75 // reset empties b by resetting its next and end pointers. 76 func (b *wbBuf) reset() { 77 start := uintptr(unsafe.Pointer(&b.buf[0])) 78 b.next = start 79 if gcBlackenPromptly || writeBarrier.cgo { 80 // Effectively disable the buffer by forcing a flush 81 // on every barrier. 82 b.end = uintptr(unsafe.Pointer(&b.buf[wbBufEntryPointers])) 83 } else if testSmallBuf { 84 // For testing, allow two barriers in the buffer. If 85 // we only did one, then barriers of non-heap pointers 86 // would be no-ops. This lets us combine a buffered 87 // barrier with a flush at a later time. 88 b.end = uintptr(unsafe.Pointer(&b.buf[2*wbBufEntryPointers])) 89 } else { 90 b.end = start + uintptr(len(b.buf))*unsafe.Sizeof(b.buf[0]) 91 } 92 93 if (b.end-b.next)%(wbBufEntryPointers*unsafe.Sizeof(b.buf[0])) != 0 { 94 throw("bad write barrier buffer bounds") 95 } 96 } 97 98 // putFast adds old and new to the write barrier buffer and returns 99 // false if a flush is necessary. Callers should use this as: 100 // 101 // buf := &getg().m.p.ptr().wbBuf 102 // if !buf.putFast(old, new) { 103 // wbBufFlush(...) 104 // } 105 // 106 // The arguments to wbBufFlush depend on whether the caller is doing 107 // its own cgo pointer checks. If it is, then this can be 108 // wbBufFlush(nil, 0). Otherwise, it must pass the slot address and 109 // new. 110 // 111 // Since buf is a per-P resource, the caller must ensure there are no 112 // preemption points while buf is in use. 113 // 114 // It must be nowritebarrierrec to because write barriers here would 115 // corrupt the write barrier buffer. It (and everything it calls, if 116 // it called anything) has to be nosplit to avoid scheduling on to a 117 // different P and a different buffer. 118 // 119 //go:nowritebarrierrec 120 //go:nosplit 121 func (b *wbBuf) putFast(old, new uintptr) bool { 122 p := (*[2]uintptr)(unsafe.Pointer(b.next)) 123 p[0] = old 124 p[1] = new 125 b.next += 2 * sys.PtrSize 126 return b.next != b.end 127 } 128 129 // wbBufFlush flushes the current P's write barrier buffer to the GC 130 // workbufs. It is passed the slot and value of the write barrier that 131 // caused the flush so that it can implement cgocheck. 132 // 133 // This must not have write barriers because it is part of the write 134 // barrier implementation. 135 // 136 // This and everything it calls must be nosplit because 1) the stack 137 // contains untyped slots from gcWriteBarrier and 2) there must not be 138 // a GC safe point between the write barrier test in the caller and 139 // flushing the buffer. 140 // 141 // TODO: A "go:nosplitrec" annotation would be perfect for this. 142 // 143 //go:nowritebarrierrec 144 //go:nosplit 145 func wbBufFlush(dst *uintptr, src uintptr) { 146 if getg().m.dying > 0 { 147 // We're going down. Not much point in write barriers 148 // and this way we can allow write barriers in the 149 // panic path. 150 return 151 } 152 153 if writeBarrier.cgo && dst != nil { 154 // This must be called from the stack that did the 155 // write. It's nosplit all the way down. 156 cgoCheckWriteBarrier(dst, src) 157 if !writeBarrier.needed { 158 // We were only called for cgocheck. 159 b := &getg().m.p.ptr().wbBuf 160 b.next = uintptr(unsafe.Pointer(&b.buf[0])) 161 return 162 } 163 } 164 165 // Switch to the system stack so we don't have to worry about 166 // the untyped stack slots or safe points. 167 systemstack(func() { 168 wbBufFlush1(getg().m.p.ptr()) 169 }) 170 } 171 172 // wbBufFlush1 flushes p's write barrier buffer to the GC work queue. 173 // 174 // This must not have write barriers because it is part of the write 175 // barrier implementation, so this may lead to infinite loops or 176 // buffer corruption. 177 // 178 // This must be non-preemptible because it uses the P's workbuf. 179 // 180 //go:nowritebarrierrec 181 //go:systemstack 182 func wbBufFlush1(_p_ *p) { 183 // Get the buffered pointers. 184 start := uintptr(unsafe.Pointer(&_p_.wbBuf.buf[0])) 185 n := (_p_.wbBuf.next - start) / unsafe.Sizeof(_p_.wbBuf.buf[0]) 186 ptrs := _p_.wbBuf.buf[:n] 187 188 // Reset the buffer. 189 _p_.wbBuf.reset() 190 191 if useCheckmark { 192 // Slow path for checkmark mode. 193 for _, ptr := range ptrs { 194 shade(ptr) 195 } 196 return 197 } 198 199 // Mark all of the pointers in the buffer and record only the 200 // pointers we greyed. We use the buffer itself to temporarily 201 // record greyed pointers. 202 // 203 // TODO: Should scanobject/scanblock just stuff pointers into 204 // the wbBuf? Then this would become the sole greying path. 205 gcw := &_p_.gcw 206 pos := 0 207 arenaStart := mheap_.arena_start 208 for _, ptr := range ptrs { 209 if ptr < arenaStart { 210 // nil pointers are very common, especially 211 // for the "old" values. Filter out these and 212 // other "obvious" non-heap pointers ASAP. 213 // 214 // TODO: Should we filter out nils in the fast 215 // path to reduce the rate of flushes? 216 continue 217 } 218 // TODO: This doesn't use hbits, so calling 219 // heapBitsForObject seems a little silly. We could 220 // easily separate this out since heapBitsForObject 221 // just calls heapBitsForAddr(obj) to get hbits. 222 obj, _, span, objIndex := heapBitsForObject(ptr, 0, 0) 223 if obj == 0 { 224 continue 225 } 226 // TODO: Consider making two passes where the first 227 // just prefetches the mark bits. 228 mbits := span.markBitsForIndex(objIndex) 229 if mbits.isMarked() { 230 continue 231 } 232 mbits.setMarked() 233 if span.spanclass.noscan() { 234 gcw.bytesMarked += uint64(span.elemsize) 235 continue 236 } 237 ptrs[pos] = obj 238 pos++ 239 } 240 241 // Enqueue the greyed objects. 242 gcw.putBatch(ptrs[:pos]) 243 if gcphase == _GCmarktermination || gcBlackenPromptly { 244 // Ps aren't allowed to cache work during mark 245 // termination. 246 gcw.dispose() 247 } 248 }