/
mem_usage.go
145 lines (126 loc) · 2.97 KB
/
mem_usage.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
// Copyright 2023 Blink Labs Software
//
// Licensed 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.
package main
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"runtime/pprof"
"time"
ouroboros "github.com/blinklabs-io/gouroboros"
)
type memUsageFlags struct {
flagset *flag.FlagSet
startEra string
tip bool
debugPort int
}
func newMemUsageFlags() *memUsageFlags {
f := &memUsageFlags{
flagset: flag.NewFlagSet("mem-usage", flag.ExitOnError),
}
f.flagset.StringVar(
&f.startEra,
"start-era",
"genesis",
"era which to start chain-sync at",
)
f.flagset.BoolVar(
&f.tip,
"tip",
false,
"start chain-sync at current chain tip",
)
f.flagset.IntVar(&f.debugPort, "debug-port", 8080, "pprof port")
return f
}
func testMemUsage(f *globalFlags) {
memUsageFlags := newMemUsageFlags()
err := memUsageFlags.flagset.Parse(f.flagset.Args()[1:])
if err != nil {
fmt.Printf("failed to parse subcommand args: %s\n", err)
os.Exit(1)
}
// Start pprof listener
log.Printf(
"Starting pprof listener on http://0.0.0.0:%d/debug/pprof\n",
memUsageFlags.debugPort,
)
go func() {
log.Println(
http.ListenAndServe(
fmt.Sprintf(":%d", memUsageFlags.debugPort),
nil,
),
)
}()
for i := 0; i < 10; i++ {
showMemoryStats("open")
conn := createClientConnection(f)
errorChan := make(chan error)
go func() {
for {
err, ok := <-errorChan
if !ok {
return
}
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
}()
o, err := ouroboros.New(
ouroboros.WithConnection(conn),
ouroboros.WithNetworkMagic(uint32(f.networkMagic)),
ouroboros.WithErrorChan(errorChan),
ouroboros.WithNodeToNode(f.ntnProto),
ouroboros.WithKeepAlive(true),
)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
tip, err := o.ChainSync().Client.GetCurrentTip()
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
log.Printf(
"tip: slot = %d, hash = %x\n",
tip.Point.Slot,
tip.Point.Hash,
)
if err := o.Close(); err != nil {
fmt.Printf("ERROR: %s\n", err)
}
showMemoryStats("close")
time.Sleep(5 * time.Second)
runtime.GC()
showMemoryStats("after GC")
}
if err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1); err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
fmt.Printf("waiting forever")
select {}
}
func showMemoryStats(tag string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Printf("[%s] HeapAlloc: %dKiB\n", tag, m.HeapAlloc/1024)
}