laconicd/rpc/ethereum/namespaces/debug/utils.go

61 lines
1.4 KiB
Go
Raw Normal View History

rpc: implement internal `debug_` API namespace functions (#313) * API Hello World * Added all the debug functions + more data to try implementing the GC functions * Getting transactions information * Added cpu profile first approach functions * new struct for cpuprofile and read filename from params * cpuprofile, gcstats and memstats * added comment * All endpoints returns error instead of string * Code cleanup * Changed errors messages to match go-eth returns * Removed activated flag and just using the file to check if it's running * Added new endpoints to the json_rpc.md file * GoTrace debug endpoints added * Block profile endpoint added * missing goeth calls * added debug logs * divide debug and internal api * Using ExpandHome on server configuration * Added rpc changes to changelog * Logging go trace status * Removed logger functions and moved logger errors to debug * Added more logs to go trace * Added more datailed changelog * Removed trace debug api interface * added comments * cleanup * Updated changelog * disable lint on cpuprofile rename Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopCpuProfile Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopGoTrace Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * implement suggested changes Co-authored-by: ramacarlucho <ramirocarlucho@gmail.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
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.
func ExpandHome(p string) (string, error) {
rpc: implement internal `debug_` API namespace functions (#313) * API Hello World * Added all the debug functions + more data to try implementing the GC functions * Getting transactions information * Added cpu profile first approach functions * new struct for cpuprofile and read filename from params * cpuprofile, gcstats and memstats * added comment * All endpoints returns error instead of string * Code cleanup * Changed errors messages to match go-eth returns * Removed activated flag and just using the file to check if it's running * Added new endpoints to the json_rpc.md file * GoTrace debug endpoints added * Block profile endpoint added * missing goeth calls * added debug logs * divide debug and internal api * Using ExpandHome on server configuration * Added rpc changes to changelog * Logging go trace status * Removed logger functions and moved logger errors to debug * Added more logs to go trace * Added more datailed changelog * Removed trace debug api interface * added comments * cleanup * Updated changelog * disable lint on cpuprofile rename Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopCpuProfile Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopGoTrace Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * implement suggested changes Co-authored-by: ramacarlucho <ramirocarlucho@gmail.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2021-07-20 12:50:17 +00:00
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
usr, err := user.Current()
if err != nil {
return p, err
rpc: implement internal `debug_` API namespace functions (#313) * API Hello World * Added all the debug functions + more data to try implementing the GC functions * Getting transactions information * Added cpu profile first approach functions * new struct for cpuprofile and read filename from params * cpuprofile, gcstats and memstats * added comment * All endpoints returns error instead of string * Code cleanup * Changed errors messages to match go-eth returns * Removed activated flag and just using the file to check if it's running * Added new endpoints to the json_rpc.md file * GoTrace debug endpoints added * Block profile endpoint added * missing goeth calls * added debug logs * divide debug and internal api * Using ExpandHome on server configuration * Added rpc changes to changelog * Logging go trace status * Removed logger functions and moved logger errors to debug * Added more logs to go trace * Added more datailed changelog * Removed trace debug api interface * added comments * cleanup * Updated changelog * disable lint on cpuprofile rename Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopCpuProfile Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopGoTrace Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * implement suggested changes Co-authored-by: ramacarlucho <ramirocarlucho@gmail.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2021-07-20 12:50:17 +00:00
}
home := usr.HomeDir
p = home + p[1:]
rpc: implement internal `debug_` API namespace functions (#313) * API Hello World * Added all the debug functions + more data to try implementing the GC functions * Getting transactions information * Added cpu profile first approach functions * new struct for cpuprofile and read filename from params * cpuprofile, gcstats and memstats * added comment * All endpoints returns error instead of string * Code cleanup * Changed errors messages to match go-eth returns * Removed activated flag and just using the file to check if it's running * Added new endpoints to the json_rpc.md file * GoTrace debug endpoints added * Block profile endpoint added * missing goeth calls * added debug logs * divide debug and internal api * Using ExpandHome on server configuration * Added rpc changes to changelog * Logging go trace status * Removed logger functions and moved logger errors to debug * Added more logs to go trace * Added more datailed changelog * Removed trace debug api interface * added comments * cleanup * Updated changelog * disable lint on cpuprofile rename Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopCpuProfile Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopGoTrace Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * implement suggested changes Co-authored-by: ramacarlucho <ramirocarlucho@gmail.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2021-07-20 12:50:17 +00:00
}
return filepath.Clean(p), nil
rpc: implement internal `debug_` API namespace functions (#313) * API Hello World * Added all the debug functions + more data to try implementing the GC functions * Getting transactions information * Added cpu profile first approach functions * new struct for cpuprofile and read filename from params * cpuprofile, gcstats and memstats * added comment * All endpoints returns error instead of string * Code cleanup * Changed errors messages to match go-eth returns * Removed activated flag and just using the file to check if it's running * Added new endpoints to the json_rpc.md file * GoTrace debug endpoints added * Block profile endpoint added * missing goeth calls * added debug logs * divide debug and internal api * Using ExpandHome on server configuration * Added rpc changes to changelog * Logging go trace status * Removed logger functions and moved logger errors to debug * Added more logs to go trace * Added more datailed changelog * Removed trace debug api interface * added comments * cleanup * Updated changelog * disable lint on cpuprofile rename Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopCpuProfile Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopGoTrace Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * implement suggested changes Co-authored-by: ramacarlucho <ramirocarlucho@gmail.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
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)
fp, err := ExpandHome(file)
if err != nil {
return err
}
f, err := os.Create(fp)
rpc: implement internal `debug_` API namespace functions (#313) * API Hello World * Added all the debug functions + more data to try implementing the GC functions * Getting transactions information * Added cpu profile first approach functions * new struct for cpuprofile and read filename from params * cpuprofile, gcstats and memstats * added comment * All endpoints returns error instead of string * Code cleanup * Changed errors messages to match go-eth returns * Removed activated flag and just using the file to check if it's running * Added new endpoints to the json_rpc.md file * GoTrace debug endpoints added * Block profile endpoint added * missing goeth calls * added debug logs * divide debug and internal api * Using ExpandHome on server configuration * Added rpc changes to changelog * Logging go trace status * Removed logger functions and moved logger errors to debug * Added more logs to go trace * Added more datailed changelog * Removed trace debug api interface * added comments * cleanup * Updated changelog * disable lint on cpuprofile rename Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopCpuProfile Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopGoTrace Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * implement suggested changes Co-authored-by: ramacarlucho <ramirocarlucho@gmail.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2021-07-20 12:50:17 +00:00
if err != nil {
return err
}
if err := p.WriteTo(f, 0); err != nil {
if err := f.Close(); err != nil {
return err
}
return err
}
return f.Close()
rpc: implement internal `debug_` API namespace functions (#313) * API Hello World * Added all the debug functions + more data to try implementing the GC functions * Getting transactions information * Added cpu profile first approach functions * new struct for cpuprofile and read filename from params * cpuprofile, gcstats and memstats * added comment * All endpoints returns error instead of string * Code cleanup * Changed errors messages to match go-eth returns * Removed activated flag and just using the file to check if it's running * Added new endpoints to the json_rpc.md file * GoTrace debug endpoints added * Block profile endpoint added * missing goeth calls * added debug logs * divide debug and internal api * Using ExpandHome on server configuration * Added rpc changes to changelog * Logging go trace status * Removed logger functions and moved logger errors to debug * Added more logs to go trace * Added more datailed changelog * Removed trace debug api interface * added comments * cleanup * Updated changelog * disable lint on cpuprofile rename Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopCpuProfile Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * return error in StopGoTrace Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * implement suggested changes Co-authored-by: ramacarlucho <ramirocarlucho@gmail.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
2021-07-20 12:50:17 +00:00
}