2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2021-07-20 12:50:17 +00:00
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime/pprof"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// isCPUProfileConfigurationActivated checks if cpuprofile was configured via flag
|
|
|
|
func isCPUProfileConfigurationActivated(ctx *server.Context) bool {
|
|
|
|
// TODO: use same constants as server/start.go
|
|
|
|
// constant declared in start.go cannot be imported (cyclical dependency)
|
|
|
|
const flagCPUProfile = "cpu-profile"
|
|
|
|
if cpuProfile := ctx.Viper.GetString(flagCPUProfile); cpuProfile != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpandHome expands home directory in file paths.
|
|
|
|
// ~someuser/tmp will not be expanded.
|
2021-10-13 10:52:05 +00:00
|
|
|
func ExpandHome(p string) (string, error) {
|
2021-07-20 12:50:17 +00:00
|
|
|
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
|
2021-10-13 10:52:05 +00:00
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return p, err
|
2021-07-20 12:50:17 +00:00
|
|
|
}
|
2021-10-13 10:52:05 +00:00
|
|
|
home := usr.HomeDir
|
|
|
|
p = home + p[1:]
|
2021-07-20 12:50:17 +00:00
|
|
|
}
|
2021-10-13 10:52:05 +00:00
|
|
|
return filepath.Clean(p), nil
|
2021-07-20 12:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeProfile writes the data to a file
|
|
|
|
func writeProfile(name, file string, log log.Logger) error {
|
|
|
|
p := pprof.Lookup(name)
|
|
|
|
log.Info("Writing profile records", "count", p.Count(), "type", name, "dump", file)
|
2021-10-13 10:52:05 +00:00
|
|
|
fp, err := ExpandHome(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Create(fp)
|
2021-07-20 12:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-19 08:55:55 +00:00
|
|
|
if err := p.WriteTo(f, 0); err != nil {
|
2021-11-25 15:12:57 +00:00
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-19 08:55:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
2021-07-20 12:50:17 +00:00
|
|
|
}
|