21
vendor/github.com/anacrolix/envpprof/LICENSE
generated
vendored
Normal file
21
vendor/github.com/anacrolix/envpprof/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Matt Joiner
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
14
vendor/github.com/anacrolix/envpprof/README.md
generated
vendored
Normal file
14
vendor/github.com/anacrolix/envpprof/README.md
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
# envpprof
|
||||
|
||||
[](https://pkg.go.dev/github.com/anacrolix/envpprof)
|
||||
|
||||
Allows run-time configuration of Go's pprof features and default HTTP mux using the environment variable `GOPPROF`. Import the package with `import _ "github.com/anacrolix/envpprof"`. `envpprof` has an `init` function that will run at process initialization that checks the value of the `GOPPROF` environment variable. The variable can contain a comma-separated list of values, for example `GOPPROF=http,block`. The supported keys are:
|
||||
|
||||
Key | Effect
|
||||
--- | ------
|
||||
http | Exposes the default HTTP muxer [`"net/http".DefaultServeMux`](https://pkg.go.dev/net/http?tab=doc#pkg-variables) to the first free TCP port after `6060` on `localhost`. The process PID, and location are logged automatically when this is enabled. `DefaultServeMux` is frequently the default location to expose status, and debugging endpoints, including those provided by [`net/http/pprof`](https://pkg.go.dev/net/http/pprof?tab=doc). Note that the `net/http/pprof` import is included with `envpprof`, and exposed on `DefaultServeMux`.
|
||||
cpu |Calls [`"runtime/pprof".StartCPUProfile`](https://pkg.go.dev/runtime/pprof?tab=doc#StartCPUProfile), writing to a temporary file in `$HOME/pprof` with the prefix `cpu`. The file is not removed after use. The name of the file is logged when this is enabled. [`envpprof.Stop`](https://pkg.go.dev/github.com/anacrolix/envpprof?tab=doc#Stop) should be deferred from `main` when this will be used, to ensure proper clean up.
|
||||
heap |This is similar to the `cpu` key, but writes heap profile information to a file prefixed with `heap`. The profile will not be written unless `Stop` is invoked. See `cpu` for more.
|
||||
block | This calls [`"runtime".SetBlockProfileRate(1)`](https://pkg.go.dev/runtime?tab=doc#SetBlockProfileRate) enabling the profiling of goroutine blocking events. Note that if `http` is enabled, this exposes the blocking profile at the HTTP path `/debug/pprof/block` per package [`net/http/pprof`](https://pkg.go.dev/net/http/pprof?tab=doc#pkg-overview).
|
||||
mutex | This calls [`"runtime".SetMutexProfileFraction(1)`](https://pkg.go.dev/runtime?tab=doc#SetMutexProfileFraction) enabling profiling of mutex contention events. Note that if `http` is enabled, this exposes the profile at the HTTP path `/debug/pprof/mutex` per package [`net/http/pprof`](https://pkg.go.dev/net/http/pprof?tab=doc#pkg-overview).
|
||||
|
||||
112
vendor/github.com/anacrolix/envpprof/envpprof.go
generated
vendored
Normal file
112
vendor/github.com/anacrolix/envpprof/envpprof.go
generated
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
package envpprof
|
||||
|
||||
import (
|
||||
"expvar"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
"strings"
|
||||
|
||||
"github.com/anacrolix/log"
|
||||
)
|
||||
|
||||
var (
|
||||
pprofDir = filepath.Join(os.Getenv("HOME"), "pprof")
|
||||
heap bool
|
||||
)
|
||||
|
||||
func writeHeapProfile() {
|
||||
os.Mkdir(pprofDir, 0750)
|
||||
f, err := ioutil.TempFile(pprofDir, "heap")
|
||||
if err != nil {
|
||||
log.Printf("error creating heap profile file: %s", err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
pprof.WriteHeapProfile(f)
|
||||
log.Printf("wrote heap profile to %q", f.Name())
|
||||
}
|
||||
|
||||
// Stop ends CPU profiling, waiting for writes to complete. If heap profiling is enabled, it also
|
||||
// writes the heap profile to a file. Stop should be deferred from main if cpu or heap profiling
|
||||
// are to be used through envpprof.
|
||||
func Stop() {
|
||||
// Should we check if CPU profiling was initiated through this package?
|
||||
pprof.StopCPUProfile()
|
||||
if heap {
|
||||
// Can or should we do this concurrently with stopping CPU profiling?
|
||||
writeHeapProfile()
|
||||
}
|
||||
}
|
||||
|
||||
func startHTTP() {
|
||||
var l net.Listener
|
||||
for port := uint16(6061); port != 6060; port++ {
|
||||
var err error
|
||||
l, err = net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if l == nil {
|
||||
log.Print("unable to create envpprof listener for http")
|
||||
return
|
||||
}
|
||||
log.Printf("(pid=%d) envpprof serving http://%s", os.Getpid(), l.Addr())
|
||||
go func() {
|
||||
defer l.Close()
|
||||
log.Printf("error serving http on envpprof listener: %s", http.Serve(l, nil))
|
||||
}()
|
||||
}
|
||||
|
||||
func init() {
|
||||
expvar.Publish("numGoroutine", expvar.Func(func() interface{} { return runtime.NumGoroutine() }))
|
||||
_var := os.Getenv("GOPPROF")
|
||||
if _var == "" {
|
||||
return
|
||||
}
|
||||
for _, item := range strings.Split(os.Getenv("GOPPROF"), ",") {
|
||||
equalsPos := strings.IndexByte(item, '=')
|
||||
var key, value string
|
||||
if equalsPos < 0 {
|
||||
key = item
|
||||
} else {
|
||||
key = item[:equalsPos]
|
||||
value = item[equalsPos+1:]
|
||||
}
|
||||
if value != "" {
|
||||
log.Printf("values not yet supported")
|
||||
}
|
||||
switch key {
|
||||
case "http":
|
||||
startHTTP()
|
||||
case "cpu":
|
||||
os.Mkdir(pprofDir, 0750)
|
||||
f, err := ioutil.TempFile(pprofDir, "cpu")
|
||||
if err != nil {
|
||||
log.Printf("error creating cpu pprof file: %s", err)
|
||||
break
|
||||
}
|
||||
err = pprof.StartCPUProfile(f)
|
||||
if err != nil {
|
||||
log.Printf("error starting cpu profiling: %s", err)
|
||||
break
|
||||
}
|
||||
log.Printf("cpu profiling to file %q", f.Name())
|
||||
case "block":
|
||||
runtime.SetBlockProfileRate(1)
|
||||
case "heap":
|
||||
heap = true
|
||||
case "mutex":
|
||||
runtime.SetMutexProfileFraction(1)
|
||||
default:
|
||||
log.Printf("unexpected GOPPROF key %q", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user