/
debug.go
196 lines (190 loc) · 4.69 KB
/
debug.go
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
// -----------------------------------------------------------------------------
// ZR Library zr/[debug.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package zr
import (
"bufio"
"bytes"
"fmt"
"os"
"reflect"
"runtime"
"runtime/debug"
"strings"
"time"
)
// ConsumeGB consumes, then releases a large amount of RAM for testing.
func ConsumeGB(gigabytes float64) {
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
fmt.Printf("before allocation. mem: %s\r\n",
ByteSizeString(int64(mem.HeapSys), false))
{
fmt.Printf("allocating and filling %0.1f GB\r\n", gigabytes)
var (
max = int64(gigabytes * 1024 * 1024 * 1024)
big = make([]byte, max)
b = byte(0)
)
for i := int64(0); i < max; i++ {
big[i] = b
b++
if b == 255 {
b = 0
}
}
runtime.ReadMemStats(&mem)
fmt.Printf("after allocating, mem: %s\r\n",
ByteSizeString(int64(mem.HeapSys), false))
fmt.Printf("some data: %v\r\n", big[:100])
fmt.Println("press ENTER to release memory")
bufio.NewReader(os.Stdin).ReadString('\n')
big = nil
}
debug.FreeOSMemory()
time.Sleep(3 * time.Second)
runtime.ReadMemStats(&mem)
fmt.Printf("after releasing. mem: %s\r\n",
ByteSizeString(int64(mem.HeapSys), false))
fmt.Println("press ENTER to continue")
bufio.NewReader(os.Stdin).ReadString('\n')
} // ConsumeGB
// DebugString takes any kind of value and returns a string.
// Calls itself when describing slices, etc.
func DebugString(value interface{}, optIndentAt ...int) string {
var indentAt int
switch n := len(optIndentAt); {
case n == 1:
{
indentAt = optIndentAt[0]
}
case n > 1:
mod.Error(EInvalidArg, "optIndentAt", ":", optIndentAt)
}
var (
retBuf bytes.Buffer
wr = retBuf.WriteRune
ws = retBuf.WriteString
)
switch v := value.(type) {
// nil:
case nil:
{
return "nil"
}
case bool:
{
if v {
return "true"
}
return "false"
}
case CaseMode:
{
if v == IgnoreCase {
return "IgnoreCase"
}
if v == MatchCase {
return "MatchCase"
}
return "INVALID" + fmt.Sprintf("%v", v)
}
// delegated to String():
case int, int64, int32, int16, int8, float64, float32,
uint, uint64, uint32, uint16, uint8,
*int, *int64, *int32, *int16, *int8, *float64, *float32,
*uint, *uint64, *uint32, *uint16, *uint8,
*bool:
{
return String(v)
}
// string types:
case string:
{
vals := []rune{'\a', '\b', '\f', '\n', '\r', 't', '\v'}
chars := []rune{'a', 'b', 'f', 'n', 'r', 't', 'v'}
mainLoop:
for _, ch := range v {
for i, cc := range vals {
if ch == cc {
wr('\\')
wr(chars[i])
continue mainLoop
}
}
if ch < 32 || ch > 127 {
wr('.')
continue
}
wr(ch)
}
}
case []string:
ws(fmt.Sprintf("[%d] ", len(v)))
isMany := len(v) > 1
for i, v := range v {
if isMany {
ws("\n")
ws(strings.Repeat(TabSpace, indentAt+1))
ws(fmt.Sprintf("%d:", i))
}
ws(DebugString(v, indentAt))
}
case [][]string:
ws(fmt.Sprintf("[%d] ", len(v)))
isMany1 := len(v) > 1
for i1, v := range v { // range [][]string
if isMany1 {
ws("\n")
ws(strings.Repeat(TabSpace, indentAt+1))
}
isMany2 := len(v) > 1
ws(fmt.Sprintf("%d: [%d]", i1, len(v)))
for i2, v := range v { // range []string
if isMany2 {
ws("\n")
ws(strings.Repeat(" ", indentAt+2))
}
ws(fmt.Sprintf(" %d:", i2))
ws(DebugString(v, indentAt+3)) // string
}
}
case []byte:
{
ws(fmt.Sprintf("[%d] ", len(v)))
}
default:
mod.Error("Type", reflect.TypeOf(value), "not handled; =", value)
return "(" + fmt.Sprint(value) + ")"
}
return retBuf.String()
} // DebugString
// DV displays human-friendly values for debugging.
//
// label: the value's label, you should usually specify
// the name of a variable or some tag here.
//
// values: one or more values you want to display.
func DV(label string, values ...interface{}) {
if len(values) == 0 {
fmt.Println(label)
return
}
fmt.Print(label)
for _, v := range values {
typeName := "<" + reflect.TypeOf(v).String() + ">"
changeType := func(find, repl string) {
for strings.Contains(typeName, find) {
typeName = strings.ReplaceAll(typeName, find, repl)
}
}
changeType("bool", "t")
changeType("byte", "b")
changeType("int", "i")
changeType("string", "s")
fmt.Print(" " + typeName + ": " + DebugString(v, 0))
}
fmt.Println()
} // DV
//eof