|
|
@ -32,7 +32,11 @@ import ( |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"reflect" |
|
|
|
"reflect" |
|
|
|
|
|
|
|
"runtime" |
|
|
|
|
|
|
|
"runtime/pprof" |
|
|
|
|
|
|
|
"runtime/trace" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"sync" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/beevik/ntp" |
|
|
|
"github.com/beevik/ntp" |
|
|
@ -42,7 +46,6 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize" |
|
|
|
humanize "github.com/dustin/go-humanize" |
|
|
|
"github.com/gorilla/mux" |
|
|
|
"github.com/gorilla/mux" |
|
|
|
"github.com/pkg/profile" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// IsErrIgnored returns whether given error is ignored or not.
|
|
|
|
// IsErrIgnored returns whether given error is ignored or not.
|
|
|
@ -187,94 +190,130 @@ func contains(slice interface{}, elem interface{}) bool { |
|
|
|
// provide any API to calculate the profiler file path in the
|
|
|
|
// provide any API to calculate the profiler file path in the
|
|
|
|
// disk since the name of this latter is randomly generated.
|
|
|
|
// disk since the name of this latter is randomly generated.
|
|
|
|
type profilerWrapper struct { |
|
|
|
type profilerWrapper struct { |
|
|
|
stopFn func() |
|
|
|
stopFn func() ([]byte, error) |
|
|
|
pathFn func() string |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (p profilerWrapper) Stop() { |
|
|
|
func (p profilerWrapper) Stop() ([]byte, error) { |
|
|
|
p.stopFn() |
|
|
|
return p.stopFn() |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p profilerWrapper) Path() string { |
|
|
|
|
|
|
|
return p.pathFn() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Returns current profile data, returns error if there is no active
|
|
|
|
// Returns current profile data, returns error if there is no active
|
|
|
|
// profiling in progress. Stops an active profile.
|
|
|
|
// profiling in progress. Stops an active profile.
|
|
|
|
func getProfileData() ([]byte, error) { |
|
|
|
func getProfileData() (map[string][]byte, error) { |
|
|
|
if globalProfiler == nil { |
|
|
|
globalProfilerMu.Lock() |
|
|
|
|
|
|
|
defer globalProfilerMu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(globalProfiler) == 0 { |
|
|
|
return nil, errors.New("profiler not enabled") |
|
|
|
return nil, errors.New("profiler not enabled") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
profilerPath := globalProfiler.Path() |
|
|
|
dst := make(map[string][]byte, len(globalProfiler)) |
|
|
|
|
|
|
|
for typ, prof := range globalProfiler { |
|
|
|
// Stop the profiler
|
|
|
|
// Stop the profiler
|
|
|
|
globalProfiler.Stop() |
|
|
|
var err error |
|
|
|
|
|
|
|
buf, err := prof.Stop() |
|
|
|
profilerFile, err := os.Open(profilerPath) |
|
|
|
delete(globalProfiler, typ) |
|
|
|
if err != nil { |
|
|
|
if err == nil { |
|
|
|
return nil, err |
|
|
|
dst[typ] = buf |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return dst, nil |
|
|
|
return ioutil.ReadAll(profilerFile) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Starts a profiler returns nil if profiler is not enabled, caller needs to handle this.
|
|
|
|
// Starts a profiler returns nil if profiler is not enabled, caller needs to handle this.
|
|
|
|
func startProfiler(profilerType, dirPath string) (minioProfiler, error) { |
|
|
|
func startProfiler(profilerType string) (minioProfiler, error) { |
|
|
|
var err error |
|
|
|
var prof profilerWrapper |
|
|
|
if dirPath == "" { |
|
|
|
|
|
|
|
dirPath, err = ioutil.TempDir("", "profile") |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var profiler interface { |
|
|
|
|
|
|
|
Stop() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var profilerFileName string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enable profiler and set the name of the file that pkg/pprof
|
|
|
|
// Enable profiler and set the name of the file that pkg/pprof
|
|
|
|
// library creates to store profiling data.
|
|
|
|
// library creates to store profiling data.
|
|
|
|
switch profilerType { |
|
|
|
switch profilerType { |
|
|
|
case "cpu": |
|
|
|
case "cpu": |
|
|
|
profiler = profile.Start(profile.CPUProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath)) |
|
|
|
dirPath, err := ioutil.TempDir("", "profile") |
|
|
|
profilerFileName = "cpu.pprof" |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fn := filepath.Join(dirPath, "cpu.out") |
|
|
|
|
|
|
|
f, err := os.Create(fn) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
err = pprof.StartCPUProfile(f) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
prof.stopFn = func() ([]byte, error) { |
|
|
|
|
|
|
|
pprof.StopCPUProfile() |
|
|
|
|
|
|
|
err := f.Close() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
defer os.RemoveAll(dirPath) |
|
|
|
|
|
|
|
return ioutil.ReadFile(fn) |
|
|
|
|
|
|
|
} |
|
|
|
case "mem": |
|
|
|
case "mem": |
|
|
|
profiler = profile.Start(profile.MemProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath)) |
|
|
|
old := runtime.MemProfileRate |
|
|
|
profilerFileName = "mem.pprof" |
|
|
|
runtime.MemProfileRate = 1 |
|
|
|
|
|
|
|
prof.stopFn = func() ([]byte, error) { |
|
|
|
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
runtime.MemProfileRate = old |
|
|
|
|
|
|
|
err := pprof.Lookup("heap").WriteTo(&buf, 0) |
|
|
|
|
|
|
|
return buf.Bytes(), err |
|
|
|
|
|
|
|
} |
|
|
|
case "block": |
|
|
|
case "block": |
|
|
|
profiler = profile.Start(profile.BlockProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath)) |
|
|
|
runtime.SetBlockProfileRate(1) |
|
|
|
profilerFileName = "block.pprof" |
|
|
|
prof.stopFn = func() ([]byte, error) { |
|
|
|
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
runtime.SetBlockProfileRate(0) |
|
|
|
|
|
|
|
err := pprof.Lookup("block").WriteTo(&buf, 0) |
|
|
|
|
|
|
|
return buf.Bytes(), err |
|
|
|
|
|
|
|
} |
|
|
|
case "mutex": |
|
|
|
case "mutex": |
|
|
|
profiler = profile.Start(profile.MutexProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath)) |
|
|
|
runtime.SetMutexProfileFraction(1) |
|
|
|
profilerFileName = "mutex.pprof" |
|
|
|
prof.stopFn = func() ([]byte, error) { |
|
|
|
|
|
|
|
var buf bytes.Buffer |
|
|
|
|
|
|
|
runtime.SetMutexProfileFraction(0) |
|
|
|
|
|
|
|
err := pprof.Lookup("mutex").WriteTo(&buf, 0) |
|
|
|
|
|
|
|
return buf.Bytes(), err |
|
|
|
|
|
|
|
} |
|
|
|
case "trace": |
|
|
|
case "trace": |
|
|
|
profiler = profile.Start(profile.TraceProfile, profile.NoShutdownHook, profile.ProfilePath(dirPath)) |
|
|
|
dirPath, err := ioutil.TempDir("", "profile") |
|
|
|
profilerFileName = "trace.out" |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fn := filepath.Join(dirPath, "trace.out") |
|
|
|
|
|
|
|
f, err := os.Create(fn) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
err = trace.Start(f) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
prof.stopFn = func() ([]byte, error) { |
|
|
|
|
|
|
|
trace.Stop() |
|
|
|
|
|
|
|
err := f.Close() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
defer os.RemoveAll(dirPath) |
|
|
|
|
|
|
|
return ioutil.ReadFile(fn) |
|
|
|
|
|
|
|
} |
|
|
|
default: |
|
|
|
default: |
|
|
|
return nil, errors.New("profiler type unknown") |
|
|
|
return nil, errors.New("profiler type unknown") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return &profilerWrapper{ |
|
|
|
return prof, nil |
|
|
|
stopFn: profiler.Stop, |
|
|
|
|
|
|
|
pathFn: func() string { |
|
|
|
|
|
|
|
return filepath.Join(dirPath, profilerFileName) |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}, nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// minioProfiler - minio profiler interface.
|
|
|
|
// minioProfiler - minio profiler interface.
|
|
|
|
type minioProfiler interface { |
|
|
|
type minioProfiler interface { |
|
|
|
// Stop the profiler
|
|
|
|
// Stop the profiler
|
|
|
|
Stop() |
|
|
|
Stop() ([]byte, error) |
|
|
|
// Return the path of the profiling file
|
|
|
|
|
|
|
|
Path() string |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Global profiler to be used by service go-routine.
|
|
|
|
// Global profiler to be used by service go-routine.
|
|
|
|
var globalProfiler minioProfiler |
|
|
|
var globalProfiler map[string]minioProfiler |
|
|
|
|
|
|
|
var globalProfilerMu sync.Mutex |
|
|
|
|
|
|
|
|
|
|
|
// dump the request into a string in JSON format.
|
|
|
|
// dump the request into a string in JSON format.
|
|
|
|
func dumpRequest(r *http.Request) string { |
|
|
|
func dumpRequest(r *http.Request) string { |
|
|
|