github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/rpc/core/dev.go (about)

     1  package core
     2  
     3  import (
     4  	"os"
     5  	"runtime/pprof"
     6  
     7  	ctypes "github.com/tendermint/tendermint/rpc/core/types"
     8  	rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
     9  )
    10  
    11  // UnsafeFlushMempool removes all transactions from the mempool.
    12  func UnsafeFlushMempool(ctx *rpctypes.Context) (*ctypes.ResultUnsafeFlushMempool, error) {
    13  	env.Mempool.Flush()
    14  	return &ctypes.ResultUnsafeFlushMempool{}, nil
    15  }
    16  
    17  var profFile *os.File
    18  
    19  // UnsafeStartCPUProfiler starts a pprof profiler using the given filename.
    20  func UnsafeStartCPUProfiler(ctx *rpctypes.Context, filename string) (*ctypes.ResultUnsafeProfile, error) {
    21  	var err error
    22  	profFile, err = os.Create(filename)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	err = pprof.StartCPUProfile(profFile)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return &ctypes.ResultUnsafeProfile{}, nil
    31  }
    32  
    33  // UnsafeStopCPUProfiler stops the running pprof profiler.
    34  func UnsafeStopCPUProfiler(ctx *rpctypes.Context) (*ctypes.ResultUnsafeProfile, error) {
    35  	pprof.StopCPUProfile()
    36  	if err := profFile.Close(); err != nil {
    37  		return nil, err
    38  	}
    39  	return &ctypes.ResultUnsafeProfile{}, nil
    40  }
    41  
    42  // UnsafeWriteHeapProfile dumps a heap profile to the given filename.
    43  func UnsafeWriteHeapProfile(ctx *rpctypes.Context, filename string) (*ctypes.ResultUnsafeProfile, error) {
    44  	memProfFile, err := os.Create(filename)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	if err := pprof.WriteHeapProfile(memProfFile); err != nil {
    49  		return nil, err
    50  	}
    51  	if err := memProfFile.Close(); err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	return &ctypes.ResultUnsafeProfile{}, nil
    56  }