1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !tinygo
// +build !tinygo
package memory
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"unsafe"
)
type CheckedAllocator struct {
mem Allocator
sz int64
allocs sync.Map
}
func NewCheckedAllocator(mem Allocator) *CheckedAllocator {
return &CheckedAllocator{mem: mem}
}
func (a *CheckedAllocator) CurrentAlloc() int { return int(atomic.LoadInt64(&a.sz)) }
func (a *CheckedAllocator) Allocate(size int) []byte {
atomic.AddInt64(&a.sz, int64(size))
out := a.mem.Allocate(size)
if size == 0 {
return out
}
ptr := uintptr(unsafe.Pointer(&out[0]))
pcs := make([]uintptr, maxRetainedFrames)
// For historical reasons the meaning of the skip argument
// differs between Caller and Callers. For Callers, 0 identifies
// the frame for the caller itself. We skip 2 additional frames
// here to get to the caller right before the call to Allocate.
runtime.Callers(allocFrames+2, pcs)
callersFrames := runtime.CallersFrames(pcs)
if pc, _, l, ok := runtime.Caller(allocFrames); ok {
a.allocs.Store(ptr, &dalloc{pc: pc, line: l, sz: size, callersFrames: callersFrames})
}
return out
}
func (a *CheckedAllocator) Reallocate(size int, b []byte) []byte {
atomic.AddInt64(&a.sz, int64(size-len(b)))
oldptr := uintptr(unsafe.Pointer(&b[0]))
out := a.mem.Reallocate(size, b)
if size == 0 {
return out
}
newptr := uintptr(unsafe.Pointer(&out[0]))
a.allocs.Delete(oldptr)
pcs := make([]uintptr, maxRetainedFrames)
// For historical reasons the meaning of the skip argument
// differs between Caller and Callers. For Callers, 0 identifies
// the frame for the caller itself. We skip 2 additional frames
// here to get to the caller right before the call to Reallocate.
runtime.Callers(reallocFrames+2, pcs)
callersFrames := runtime.CallersFrames(pcs)
if pc, _, l, ok := runtime.Caller(reallocFrames); ok {
a.allocs.Store(newptr, &dalloc{pc: pc, line: l, sz: size, callersFrames: callersFrames})
}
return out
}
func (a *CheckedAllocator) Free(b []byte) {
atomic.AddInt64(&a.sz, int64(len(b)*-1))
defer a.mem.Free(b)
if len(b) == 0 {
return
}
ptr := uintptr(unsafe.Pointer(&b[0]))
a.allocs.Delete(ptr)
}
// typically the allocations are happening in memory.Buffer, not by consumers calling
// allocate/reallocate directly. As a result, we want to skip the caller frames
// of the inner workings of Buffer in order to find the caller that actually triggered
// the allocation via a call to Resize/Reserve/etc.
const (
defAllocFrames = 4
defReallocFrames = 3
defMaxRetainedFrames = 0
)
// Use the environment variables ARROW_CHECKED_ALLOC_FRAMES and ARROW_CHECKED_REALLOC_FRAMES
// to control how many frames it skips when storing the caller for allocations/reallocs
// when using this to find memory leaks. Use ARROW_CHECKED_MAX_RETAINED_FRAMES to control how
// many frames are retained for printing the stack trace of a leak.
var allocFrames, reallocFrames, maxRetainedFrames int = defAllocFrames, defReallocFrames, defMaxRetainedFrames
func init() {
if val, ok := os.LookupEnv("ARROW_CHECKED_ALLOC_FRAMES"); ok {
if f, err := strconv.Atoi(val); err == nil {
allocFrames = f
}
}
if val, ok := os.LookupEnv("ARROW_CHECKED_REALLOC_FRAMES"); ok {
if f, err := strconv.Atoi(val); err == nil {
reallocFrames = f
}
}
if val, ok := os.LookupEnv("ARROW_CHECKED_MAX_RETAINED_FRAMES"); ok {
if f, err := strconv.Atoi(val); err == nil {
maxRetainedFrames = f
}
}
}
type dalloc struct {
pc uintptr
line int
sz int
callersFrames *runtime.Frames
}
type TestingT interface {
Errorf(format string, args ...interface{})
Helper()
}
func (a *CheckedAllocator) AssertSize(t TestingT, sz int) {
a.allocs.Range(func(_, value interface{}) bool {
info := value.(*dalloc)
f := runtime.FuncForPC(info.pc)
frames := info.callersFrames
var callersMsg strings.Builder
for {
frame, more := frames.Next()
if frame.Line == 0 {
break
}
callersMsg.WriteString("\t")
// frame.Func is a useful source of information if it's present.
// It may be nil for non-Go code or fully inlined functions.
if fn := frame.Func; fn != nil {
// format as func name + the offset in bytes from func entrypoint
callersMsg.WriteString(fmt.Sprintf("%s+%x", fn.Name(), frame.PC-fn.Entry()))
} else {
// fallback to outer func name + file line
callersMsg.WriteString(fmt.Sprintf("%s, line %d", frame.Function, frame.Line))
}
// Write a proper file name + line, so it's really easy to find the leak
callersMsg.WriteString("\n\t\t")
callersMsg.WriteString(frame.File + ":" + strconv.Itoa(frame.Line))
callersMsg.WriteString("\n")
if !more {
break
}
}
file, line := f.FileLine(info.pc)
t.Errorf("LEAK of %d bytes FROM\n\t%s+%x\n\t\t%s:%d\n%v",
info.sz,
f.Name(), info.pc-f.Entry(), // func name + offset in bytes between frame & entrypoint to func
file, line, // a proper file name + line, so it's really easy to find the leak
callersMsg.String(),
)
return true
})
if int(atomic.LoadInt64(&a.sz)) != sz {
t.Helper()
t.Errorf("invalid memory size exp=%d, got=%d", sz, a.sz)
}
}
type CheckedAllocatorScope struct {
alloc *CheckedAllocator
sz int
}
func NewCheckedAllocatorScope(alloc *CheckedAllocator) *CheckedAllocatorScope {
sz := atomic.LoadInt64(&alloc.sz)
return &CheckedAllocatorScope{alloc: alloc, sz: int(sz)}
}
func (c *CheckedAllocatorScope) CheckSize(t TestingT) {
sz := int(atomic.LoadInt64(&c.alloc.sz))
if c.sz != sz {
t.Helper()
t.Errorf("invalid memory size exp=%d, got=%d", c.sz, sz)
}
}
var (
_ Allocator = (*CheckedAllocator)(nil)
)
|