2018-08-07 18:49:18 +00: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"
|
2021-09-22 05:38:34 +00:00
|
|
|
"os"
|
2018-08-07 18:49:18 +00:00
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
2019-06-12 19:41:28 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-08-07 18:49:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DumpMemProfileForUsername dumps a memory profile at pprofDataPath as memprofile_<username>_<temporary id>
|
2019-06-01 15:00:21 +00:00
|
|
|
func DumpMemProfileForUsername(pprofDataPath, username string) error {
|
2021-09-22 05:38:34 +00:00
|
|
|
f, err := os.CreateTemp(pprofDataPath, fmt.Sprintf("memprofile_%s_", username))
|
2018-08-07 18:49:18 +00:00
|
|
|
if err != nil {
|
2019-06-01 15:00:21 +00:00
|
|
|
return err
|
2018-08-07 18:49:18 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
runtime.GC() // get up-to-date statistics
|
2019-06-01 15:00:21 +00:00
|
|
|
return pprof.WriteHeapProfile(f)
|
2018-08-07 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DumpCPUProfileForUsername dumps a CPU profile at pprofDataPath as cpuprofile_<username>_<temporary id>
|
2022-08-31 02:15:45 +00:00
|
|
|
// the stop function it returns stops, writes and closes the CPU profile file
|
2019-06-01 15:00:21 +00:00
|
|
|
func DumpCPUProfileForUsername(pprofDataPath, username string) (func(), error) {
|
2021-09-22 05:38:34 +00:00
|
|
|
f, err := os.CreateTemp(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username))
|
2018-08-07 18:49:18 +00:00
|
|
|
if err != nil {
|
2019-06-01 15:00:21 +00:00
|
|
|
return nil, err
|
2018-08-07 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 19:41:28 +00:00
|
|
|
err = pprof.StartCPUProfile(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("StartCPUProfile: %v", err)
|
|
|
|
}
|
2018-08-07 18:49:18 +00:00
|
|
|
return func() {
|
|
|
|
pprof.StopCPUProfile()
|
2019-06-12 19:41:28 +00:00
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("StopCPUProfile Close: %v", err)
|
|
|
|
}
|
2019-06-01 15:00:21 +00:00
|
|
|
}, nil
|
2018-08-07 18:49:18 +00:00
|
|
|
}
|