2018-08-08 02:49:18 +08:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package pprof
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DumpMemProfileForUsername dumps a memory profile at pprofDataPath as memprofile_<username>_<temporary id>
|
2019-06-01 23:00:21 +08:00
|
|
|
func DumpMemProfileForUsername(pprofDataPath, username string) error {
|
2018-08-08 02:49:18 +08:00
|
|
|
f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("memprofile_%s_", username))
|
|
|
|
if err != nil {
|
2019-06-01 23:00:21 +08:00
|
|
|
return err
|
2018-08-08 02:49:18 +08:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
runtime.GC() // get up-to-date statistics
|
2019-06-01 23:00:21 +08:00
|
|
|
return pprof.WriteHeapProfile(f)
|
2018-08-08 02:49:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DumpCPUProfileForUsername dumps a CPU profile at pprofDataPath as cpuprofile_<username>_<temporary id>
|
|
|
|
// it returns the stop function which stops, writes and closes the CPU profile file
|
2019-06-01 23:00:21 +08:00
|
|
|
func DumpCPUProfileForUsername(pprofDataPath, username string) (func(), error) {
|
2018-08-08 02:49:18 +08:00
|
|
|
f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username))
|
|
|
|
if err != nil {
|
2019-06-01 23:00:21 +08:00
|
|
|
return nil, err
|
2018-08-08 02:49:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
return func() {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
f.Close()
|
2019-06-01 23:00:21 +08:00
|
|
|
}, nil
|
2018-08-08 02:49:18 +08:00
|
|
|
}
|