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.

72 lines
1.6 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 UnsafeFlushMempool() (*ctypes.ResultUnsafeFlushMempool, error) {
  10. mempoolReactor.Mempool.Flush()
  11. return &ctypes.ResultUnsafeFlushMempool{}, nil
  12. }
  13. func UnsafeSetConfig(typ, key, value string) (*ctypes.ResultUnsafeSetConfig, error) {
  14. switch typ {
  15. case "string":
  16. config.Set(key, value)
  17. case "int":
  18. val, err := strconv.Atoi(value)
  19. if err != nil {
  20. return nil, fmt.Errorf("non-integer value found. key:%s; value:%s; err:%v", key, value, err)
  21. }
  22. config.Set(key, val)
  23. case "bool":
  24. switch value {
  25. case "true":
  26. config.Set(key, true)
  27. case "false":
  28. config.Set(key, false)
  29. default:
  30. return nil, fmt.Errorf("bool value must be true or false. got %s", value)
  31. }
  32. default:
  33. return nil, fmt.Errorf("Unknown type %s", typ)
  34. }
  35. return &ctypes.ResultUnsafeSetConfig{}, nil
  36. }
  37. var profFile *os.File
  38. func UnsafeStartCPUProfiler(filename string) (*ctypes.ResultUnsafeProfile, error) {
  39. var err error
  40. profFile, err = os.Create(filename)
  41. if err != nil {
  42. return nil, err
  43. }
  44. err = pprof.StartCPUProfile(profFile)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return &ctypes.ResultUnsafeProfile{}, nil
  49. }
  50. func UnsafeStopCPUProfiler() (*ctypes.ResultUnsafeProfile, error) {
  51. pprof.StopCPUProfile()
  52. profFile.Close()
  53. return &ctypes.ResultUnsafeProfile{}, nil
  54. }
  55. func UnsafeWriteHeapProfile(filename string) (*ctypes.ResultUnsafeProfile, error) {
  56. memProfFile, err := os.Create(filename)
  57. if err != nil {
  58. return nil, err
  59. }
  60. pprof.WriteHeapProfile(memProfFile)
  61. memProfFile.Close()
  62. return &ctypes.ResultUnsafeProfile{}, nil
  63. }