github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/util/profile/profile.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package profile
    23  
    24  import (
    25  	"io"
    26  	"os"
    27  	"runtime"
    28  	"runtime/pprof"
    29  
    30  	flag "github.com/juju/gnuflag"
    31  
    32  	"github.com/dolthub/dolt/go/store/d"
    33  )
    34  
    35  var (
    36  	cpuProfile      string
    37  	memProfile      string
    38  	blockProfile    string
    39  	flagsRegistered = false
    40  )
    41  
    42  func RegisterProfileFlags(flags *flag.FlagSet) {
    43  	if !flagsRegistered {
    44  		flagsRegistered = true
    45  		flags.StringVar(&cpuProfile, "cpuprofile", "", "write cpu profile to file")
    46  		flags.StringVar(&memProfile, "memprofile", "", "write memory profile to this file")
    47  		flags.StringVar(&blockProfile, "blockprofile", "", "write block profile to this file")
    48  	}
    49  }
    50  
    51  func ApplyProfileFlags(cpuProfileVal *string, memProfileVal *string, blockProfileVal *string) {
    52  	if cpuProfileVal != nil {
    53  		cpuProfile = *cpuProfileVal
    54  	}
    55  	if memProfileVal != nil {
    56  		memProfile = *memProfileVal
    57  	}
    58  	if blockProfileVal != nil {
    59  		blockProfile = *blockProfileVal
    60  	}
    61  }
    62  
    63  // MaybeStartProfile checks the -blockProfile, -cpuProfile, and -memProfile flag and, for each that is set, attempts to start gathering profiling data into the appropriate files. It returns an object with one method, Stop(), that must be called in order to flush profile data to disk before the process terminates.
    64  func MaybeStartProfile() interface {
    65  	Stop()
    66  } {
    67  	p := &prof{}
    68  	if blockProfile != "" {
    69  		f, err := os.Create(blockProfile)
    70  		d.PanicIfError(err)
    71  		runtime.SetBlockProfileRate(1)
    72  		p.bp = f
    73  	}
    74  	if cpuProfile != "" {
    75  		f, err := os.Create(cpuProfile)
    76  		d.PanicIfError(err)
    77  		pprof.StartCPUProfile(f)
    78  		p.cpu = f
    79  	}
    80  	if memProfile != "" {
    81  		f, err := os.Create(memProfile)
    82  		d.PanicIfError(err)
    83  		p.mem = f
    84  	}
    85  	return p
    86  }
    87  
    88  type prof struct {
    89  	bp  io.WriteCloser
    90  	cpu io.Closer
    91  	mem io.WriteCloser
    92  }
    93  
    94  func (p *prof) Stop() {
    95  	if p.bp != nil {
    96  		pprof.Lookup("block").WriteTo(p.bp, 0)
    97  		p.bp.Close()
    98  		runtime.SetBlockProfileRate(0)
    99  	}
   100  	if p.cpu != nil {
   101  		pprof.StopCPUProfile()
   102  		p.cpu.Close()
   103  	}
   104  	if p.mem != nil {
   105  		pprof.WriteHeapProfile(p.mem)
   106  		p.mem.Close()
   107  	}
   108  }