Skip to content

Commit 3ee75be

Browse files
committed
cmd/evm: added mem/cpu profiling
1 parent 04b668b commit 3ee75be

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎cmd/evm/main.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ var (
3535
Name: "debug",
3636
Usage: "output full trace logs",
3737
}
38+
MemProfileFlag = cli.StringFlag{
39+
Name: "memprofile",
40+
Usage: "creates a memory profile at the given path",
41+
}
42+
CPUProfileFlag = cli.StringFlag{
43+
Name: "cpuprofile",
44+
Usage: "creates a CPU profile at the given path",
45+
}
46+
StatDumpFlag = cli.BoolFlag{
47+
Name: "statdump",
48+
Usage: "displays stack and heap memory information",
49+
}
3850
CodeFlag = cli.StringFlag{
3951
Name: "code",
4052
Usage: "EVM code",
@@ -93,6 +105,9 @@ func init() {
93105
DumpFlag,
94106
InputFlag,
95107
DisableGasMeteringFlag,
108+
MemProfileFlag,
109+
CPUProfileFlag,
110+
StatDumpFlag,
96111
}
97112
app.Commands = []cli.Command{
98113
compileCommand,

‎cmd/evm/runner.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"os"
24+
"runtime/pprof"
2425
"time"
2526

2627
goruntime "runtime"
@@ -108,6 +109,19 @@ func runCmd(ctx *cli.Context) error {
108109
},
109110
}
110111

112+
if cpuProfilePath := ctx.GlobalString(CPUProfileFlag.Name); cpuProfilePath != "" {
113+
f, err := os.Create(cpuProfilePath)
114+
if err != nil {
115+
fmt.Println("could not create CPU profile: ", err)
116+
os.Exit(1)
117+
}
118+
if err := pprof.StartCPUProfile(f); err != nil {
119+
fmt.Println("could not start CPU profile: ", err)
120+
os.Exit(1)
121+
}
122+
defer pprof.StopCPUProfile()
123+
}
124+
111125
tstart := time.Now()
112126
if ctx.GlobalBool(CreateFlag.Name) {
113127
input := append(code, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name))...)
@@ -125,12 +139,27 @@ func runCmd(ctx *cli.Context) error {
125139
fmt.Println(string(statedb.Dump()))
126140
}
127141

142+
if memProfilePath := ctx.GlobalString(MemProfileFlag.Name); memProfilePath != "" {
143+
f, err := os.Create(memProfilePath)
144+
if err != nil {
145+
fmt.Println("could not create memory profile: ", err)
146+
os.Exit(1)
147+
}
148+
if err := pprof.WriteHeapProfile(f); err != nil {
149+
fmt.Println("could not write memory profile: ", err)
150+
os.Exit(1)
151+
}
152+
f.Close()
153+
}
154+
128155
if ctx.GlobalBool(DebugFlag.Name) {
129156
fmt.Fprintln(os.Stderr, "#### TRACE ####")
130157
vm.WriteTrace(os.Stderr, logger.StructLogs())
131158
fmt.Fprintln(os.Stderr, "#### LOGS ####")
132159
vm.WriteLogs(os.Stderr, statedb.Logs())
160+
}
133161

162+
if ctx.GlobalBool(StatDumpFlag.Name) {
134163
var mem goruntime.MemStats
135164
goruntime.ReadMemStats(&mem)
136165
fmt.Fprintf(os.Stderr, `evm execution time: %v

0 commit comments

Comments
 (0)