github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/pcsverbose/pcsdebug/cpu.go (about)

     1  // Package pcsdebug 调试包
     2  package pcsdebug
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  	"os"
     8  	"runtime/pprof"
     9  )
    10  
    11  //StartCPUProfile 收集cpu信息
    12  func StartCPUProfile(ctx context.Context, cpuProfile string) {
    13  	if cpuProfile != "" {
    14  		f, err := os.Create(cpuProfile)
    15  		if err != nil {
    16  			fmt.Fprintf(os.Stderr, "Can not create cpu profile output file: %s", err)
    17  			return
    18  		}
    19  		if err := pprof.StartCPUProfile(f); err != nil {
    20  			fmt.Fprintf(os.Stderr, "Can not start cpu profile: %s", err)
    21  			f.Close()
    22  			return
    23  		}
    24  		defer pprof.StopCPUProfile()
    25  	}
    26  	select {
    27  	case <-ctx.Done():
    28  		return
    29  	}
    30  }