aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/fjl/memsize/memsize.go
blob: 2664e87c4604b5d81f2dda2d8551ad32c8b0093f (plain) (blame)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package memsize

import (
    "bytes"
    "fmt"
    "reflect"
    "sort"
    "strings"
    "text/tabwriter"
    "unsafe"
)

// Scan traverses all objects reachable from v and counts how much memory
// is used per type. The value must be a non-nil pointer to any value.
func Scan(v interface{}) Sizes {
    rv := reflect.ValueOf(v)
    if rv.Kind() != reflect.Ptr || rv.IsNil() {
        panic("value to scan must be non-nil pointer")
    }

    stopTheWorld("memsize scan")
    defer startTheWorld()

    ctx := newContext()
    ctx.scan(invalidAddr, rv, false)
    ctx.s.BitmapSize = ctx.seen.size()
    ctx.s.BitmapUtilization = ctx.seen.utilization()
    return *ctx.s
}

// Sizes is the result of a scan.
type Sizes struct {
    Total  uintptr
    ByType map[reflect.Type]*TypeSize
    // Internal stats (for debugging)
    BitmapSize        uintptr
    BitmapUtilization float32
}

type TypeSize struct {
    Total uintptr
    Count uintptr
}

func newSizes() *Sizes {
    return &Sizes{ByType: make(map[reflect.Type]*TypeSize)}
}

// Report returns a human-readable report.
func (s Sizes) Report() string {
    type typLine struct {
        name  string
        count uintptr
        total uintptr
    }
    tab := []typLine{{"ALL", 0, s.Total}}
    for _, typ := range s.ByType {
        tab[0].count += typ.Count
    }
    maxname := 0
    for typ, s := range s.ByType {
        line := typLine{typ.String(), s.Count, s.Total}
        tab = append(tab, line)
        if len(line.name) > maxname {
            maxname = len(line.name)
        }
    }
    sort.Slice(tab, func(i, j int) bool { return tab[i].total > tab[j].total })

    buf := new(bytes.Buffer)
    w := tabwriter.NewWriter(buf, 0, 0, 0, ' ', tabwriter.AlignRight)
    for _, line := range tab {
        namespace := strings.Repeat(" ", maxname-len(line.name))
        fmt.Fprintf(w, "%s%s\t  %v\t  %s\t\n", line.name, namespace, line.count, HumanSize(line.total))
    }
    w.Flush()
    return buf.String()
}

// addValue is called during scan and adds the memory of given object.
func (s *Sizes) addValue(v reflect.Value, size uintptr) {
    s.Total += size
    rs := s.ByType[v.Type()]
    if rs == nil {
        rs = new(TypeSize)
        s.ByType[v.Type()] = rs
    }
    rs.Total += size
    rs.Count++
}

type context struct {
    // We track previously scanned objects to prevent infinite loops
    // when scanning cycles and to prevent counting objects more than once.
    seen *bitmap
    tc   typCache
    s    *Sizes
}

func newContext() *context {
    return &context{seen: newBitmap(), tc: make(typCache), s: newSizes()}
}

// scan walks all objects below v, determining their size. All scan* functions return the
// amount of 'extra' memory (e.g. slice data) that is referenced by the object.
func (c *context) scan(addr address, v reflect.Value, add bool) (extraSize uintptr) {
    size := v.Type().Size()
    var marked uintptr
    if addr.valid() {
        marked = c.seen.countRange(uintptr(addr), size)
        if marked == size {
            return 0 // Skip if we have already seen the whole object.
        }
        c.seen.markRange(uintptr(addr), size)
    }
    // fmt.Printf("%v: %v ⮑ (marked %d)\n", addr, v.Type(), marked)
    if c.tc.needScan(v.Type()) {
        extraSize = c.scanContent(addr, v)
    }
    // fmt.Printf("%v: %v %d (add %v, size %d, marked %d, extra %d)\n", addr, v.Type(), size+extraSize, add, v.Type().Size(), marked, extraSize)
    if add {
        size -= marked
        size += extraSize
        c.s.addValue(v, size)
    }
    return extraSize
}

func (c *context) scanContent(addr address, v reflect.Value) uintptr {
    switch v.Kind() {
    case reflect.Array:
        return c.scanArray(addr, v)
    case reflect.Chan:
        return c.scanChan(v)
    case reflect.Func:
        // can't do anything here
        return 0
    case reflect.Interface:
        return c.scanInterface(v)
    case reflect.Map:
        return c.scanMap(v)
    case reflect.Ptr:
        if !v.IsNil() {
            c.scan(address(v.Pointer()), v.Elem(), true)
        }
        return 0
    case reflect.Slice:
        return c.scanSlice(v)
    case reflect.String:
        return uintptr(v.Len())
    case reflect.Struct:
        return c.scanStruct(addr, v)
    default:
        unhandledKind(v.Kind())
        return 0
    }
}

func (c *context) scanChan(v reflect.Value) uintptr {
    etyp := v.Type().Elem()
    extra := uintptr(0)
    if c.tc.needScan(etyp) {
        // Scan the channel buffer. This is unsafe but doesn't race because
        // the world is stopped during scan.
        hchan := unsafe.Pointer(v.Pointer())
        for i := uint(0); i < uint(v.Cap()); i++ {
            addr := chanbuf(hchan, i)
            elem := reflect.NewAt(etyp, addr).Elem()
            extra += c.scanContent(address(addr), elem)
        }
    }
    return uintptr(v.Cap())*etyp.Size() + extra
}

func (c *context) scanStruct(base address, v reflect.Value) uintptr {
    extra := uintptr(0)
    for i := 0; i < v.NumField(); i++ {
        f := v.Type().Field(i)
        if c.tc.needScan(f.Type) {
            addr := base.addOffset(f.Offset)
            extra += c.scanContent(addr, v.Field(i))
        }
    }
    return extra
}

func (c *context) scanArray(addr address, v reflect.Value) uintptr {
    esize := v.Type().Elem().Size()
    extra := uintptr(0)
    for i := 0; i < v.Len(); i++ {
        extra += c.scanContent(addr, v.Index(i))
        addr = addr.addOffset(esize)
    }
    return extra
}

func (c *context) scanSlice(v reflect.Value) uintptr {
    slice := v.Slice(0, v.Cap())
    esize := slice.Type().Elem().Size()
    base := slice.Pointer()
    // Add size of the unscanned portion of the backing array to extra.
    blen := uintptr(slice.Len()) * esize
    marked := c.seen.countRange(base, blen)
    extra := blen - marked
    c.seen.markRange(uintptr(base), blen)
    if c.tc.needScan(slice.Type().Elem()) {
        // Elements may contain pointers, scan them individually.
        addr := address(base)
        for i := 0; i < slice.Len(); i++ {
            extra += c.scanContent(addr, slice.Index(i))
            addr = addr.addOffset(esize)
        }
    }
    return extra
}

func (c *context) scanMap(v reflect.Value) uintptr {
    var (
        typ   = v.Type()
        len   = uintptr(v.Len())
        extra = uintptr(0)
    )
    if c.tc.needScan(typ.Key()) || c.tc.needScan(typ.Elem()) {
        for _, k := range v.MapKeys() {
            extra += c.scan(invalidAddr, k, false)
            extra += c.scan(invalidAddr, v.MapIndex(k), false)
        }
    }
    return len*typ.Key().Size() + len*typ.Elem().Size() + extra
}

func (c *context) scanInterface(v reflect.Value) uintptr {
    elem := v.Elem()
    if !elem.IsValid() {
        return 0 // nil interface
    }
    c.scan(invalidAddr, elem, false)
    if !c.tc.isPointer(elem.Type()) {
        // Account for non-pointer size of the value.
        return elem.Type().Size()
    }
    return 0
}