You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.5 KiB

  1. package core
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime/pprof"
  6. "strconv"
  7. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  8. )
  9. func UnsafeSetConfig(typ, key, value string) (*ctypes.ResultUnsafeSetConfig, error) {
  10. switch typ {
  11. case "string":
  12. config.Set(key, value)
  13. case "int":
  14. val, err := strconv.Atoi(value)
  15. if err != nil {
  16. return nil, fmt.Errorf("non-integer value found. key:%s; value:%s; err:%v", key, value, err)
  17. }
  18. config.Set(key, val)
  19. case "bool":
  20. switch value {
  21. case "true":
  22. config.Set(key, true)
  23. case "false":
  24. config.Set(key, false)
  25. default:
  26. return nil, fmt.Errorf("bool value must be true or false. got %s", value)
  27. }
  28. default:
  29. return nil, fmt.Errorf("Unknown type %s", typ)
  30. }
  31. return &ctypes.ResultUnsafeSetConfig{}, nil
  32. }
  33. var profFile *os.File
  34. func UnsafeStartCPUProfiler(filename string) (*ctypes.ResultUnsafeProfile, error) {
  35. var err error
  36. profFile, err = os.Create(filename)
  37. if err != nil {
  38. return nil, err
  39. }
  40. err = pprof.StartCPUProfile(profFile)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &ctypes.ResultUnsafeProfile{}, nil
  45. }
  46. func UnsafeStopCPUProfiler() (*ctypes.ResultUnsafeProfile, error) {
  47. pprof.StopCPUProfile()
  48. profFile.Close()
  49. return &ctypes.ResultUnsafeProfile{}, nil
  50. }
  51. func UnsafeWriteHeapProfile(filename string) (*ctypes.ResultUnsafeProfile, error) {
  52. memProfFile, err := os.Create(filename)
  53. if err != nil {
  54. return nil, err
  55. }
  56. pprof.WriteHeapProfile(memProfFile)
  57. memProfFile.Close()
  58. return &ctypes.ResultUnsafeProfile{}, nil
  59. }