github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/internal/debug/trace.go (about)

     1  //go:build go1.5
     2  // +build go1.5
     3  
     4  package debug
     5  
     6  import (
     7  	"errors"
     8  	"os"
     9  	"runtime/trace"
    10  
    11  	"github.com/neatlab/neatio/chain/log"
    12  )
    13  
    14  func (h *HandlerT) StartGoTrace(file string) error {
    15  	h.mu.Lock()
    16  	defer h.mu.Unlock()
    17  	if h.traceW != nil {
    18  		return errors.New("trace already in progress")
    19  	}
    20  	f, err := os.Create(expandHome(file))
    21  	if err != nil {
    22  		return err
    23  	}
    24  	if err := trace.Start(f); err != nil {
    25  		f.Close()
    26  		return err
    27  	}
    28  	h.traceW = f
    29  	h.traceFile = file
    30  	log.Info("Go tracing started", "dump", h.traceFile)
    31  	return nil
    32  }
    33  
    34  func (h *HandlerT) StopGoTrace() error {
    35  	h.mu.Lock()
    36  	defer h.mu.Unlock()
    37  	trace.Stop()
    38  	if h.traceW == nil {
    39  		return errors.New("trace not in progress")
    40  	}
    41  	log.Info("Done writing Go trace", "dump", h.traceFile)
    42  	h.traceW.Close()
    43  	h.traceW = nil
    44  	h.traceFile = ""
    45  	return nil
    46  }