2022-07-14 23:44:39 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package mempool
|
|
|
|
|
|
|
|
import (
|
2022-07-18 17:52:15 +02:00
|
|
|
"fmt"
|
2022-07-14 23:44:39 +02:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2022-07-18 17:52:15 +02:00
|
|
|
var (
|
|
|
|
dynamicMemUsageAssert = false
|
|
|
|
dynamicMemUsageDebug = false
|
|
|
|
dynamicMemUsageMaxDepth = 10
|
|
|
|
)
|
|
|
|
|
2022-07-15 00:06:27 +02:00
|
|
|
func dynamicMemUsage(v reflect.Value) uintptr {
|
2022-07-18 17:52:15 +02:00
|
|
|
return dynamicMemUsageCrawl(v, 0)
|
2022-07-15 00:06:27 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:52:15 +02:00
|
|
|
func dynamicMemUsageCrawl(v reflect.Value, depth int) uintptr {
|
2022-07-14 23:44:39 +02:00
|
|
|
t := v.Type()
|
|
|
|
bytes := t.Size()
|
2022-07-18 17:52:15 +02:00
|
|
|
if dynamicMemUsageDebug {
|
|
|
|
println("[", depth, "]", t.Kind().String(), "(", t.String(), ") ->", t.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
if depth >= dynamicMemUsageMaxDepth {
|
|
|
|
if dynamicMemUsageAssert {
|
|
|
|
panic("crawl reached maximum depth")
|
|
|
|
}
|
|
|
|
return bytes
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:52:15 +02:00
|
|
|
// For complex types, we need to peek inside slices/arrays/structs and chase pointers.
|
2022-07-14 23:44:39 +02:00
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Pointer, reflect.Interface:
|
|
|
|
if !v.IsNil() {
|
2022-07-18 17:52:15 +02:00
|
|
|
bytes += dynamicMemUsageCrawl(v.Elem(), depth+1)
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
for j := 0; j < v.Len(); j++ {
|
|
|
|
vi := v.Index(j)
|
|
|
|
k := vi.Type().Kind()
|
2022-07-18 17:52:15 +02:00
|
|
|
if dynamicMemUsageDebug {
|
|
|
|
println("[", depth, "] index:", j, "kind:", k.String())
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
2022-07-18 17:52:15 +02:00
|
|
|
elemBytes := uintptr(0)
|
2022-07-14 23:44:39 +02:00
|
|
|
if t.Kind() == reflect.Array {
|
|
|
|
if (k == reflect.Pointer || k == reflect.Interface) && !vi.IsNil() {
|
2022-07-18 17:52:15 +02:00
|
|
|
elemBytes += dynamicMemUsageCrawl(vi.Elem(), depth+1)
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
|
|
|
} else { // slice
|
2022-07-18 17:52:15 +02:00
|
|
|
elemBytes += dynamicMemUsageCrawl(vi, depth+1)
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
|
|
|
if k == reflect.Uint8 {
|
|
|
|
// short circuit for byte slice/array
|
2022-07-18 17:52:15 +02:00
|
|
|
bytes += elemBytes * uintptr(v.Len())
|
|
|
|
if dynamicMemUsageDebug {
|
2022-07-14 23:44:39 +02:00
|
|
|
println("...", v.Len(), "elements")
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-07-18 17:52:15 +02:00
|
|
|
bytes += elemBytes
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
for _, f := range reflect.VisibleFields(t) {
|
|
|
|
vf := v.FieldByIndex(f.Index)
|
|
|
|
k := vf.Type().Kind()
|
2022-07-18 17:52:15 +02:00
|
|
|
if dynamicMemUsageDebug {
|
|
|
|
println("[", depth, "] field:", f.Name, "kind:", k.String())
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
|
|
|
if (k == reflect.Pointer || k == reflect.Interface) && !vf.IsNil() {
|
2022-07-18 17:52:15 +02:00
|
|
|
bytes += dynamicMemUsageCrawl(vf.Elem(), depth+1)
|
2022-07-14 23:44:39 +02:00
|
|
|
} else if k == reflect.Array || k == reflect.Slice {
|
|
|
|
bytes -= vf.Type().Size()
|
2022-07-18 17:52:15 +02:00
|
|
|
bytes += dynamicMemUsageCrawl(vf, depth+1)
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-18 17:52:15 +02:00
|
|
|
case reflect.Uint8:
|
|
|
|
default:
|
|
|
|
if dynamicMemUsageAssert {
|
|
|
|
panic(fmt.Sprintf("unsupported kind: %v", t.Kind()))
|
|
|
|
}
|
2022-07-14 23:44:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return bytes
|
|
|
|
}
|