2018-08-08 00:19:18 +05:30
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2018-08-08 00:19:18 +05:30
|
|
|
|
|
|
|
package pprof
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-22 11:08:34 +05:30
|
|
|
"os"
|
2018-08-08 00:19:18 +05:30
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
2019-06-13 01:11:28 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-08-08 00:19:18 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// DumpMemProfileForUsername dumps a memory profile at pprofDataPath as memprofile_<username>_<temporary id>
|
2019-06-01 20:30:21 +05:30
|
|
|
func DumpMemProfileForUsername(pprofDataPath, username string) error {
|
2021-09-22 11:08:34 +05:30
|
|
|
f, err := os.CreateTemp(pprofDataPath, fmt.Sprintf("memprofile_%s_", username))
|
2018-08-08 00:19:18 +05:30
|
|
|
if err != nil {
|
2019-06-01 20:30:21 +05:30
|
|
|
return err
|
2018-08-08 00:19:18 +05:30
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
runtime.GC() // get up-to-date statistics
|
2019-06-01 20:30:21 +05:30
|
|
|
return pprof.WriteHeapProfile(f)
|
2018-08-08 00:19:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DumpCPUProfileForUsername dumps a CPU profile at pprofDataPath as cpuprofile_<username>_<temporary id>
|
2022-08-31 07:45:45 +05:30
|
|
|
// the stop function it returns stops, writes and closes the CPU profile file
|
2019-06-01 20:30:21 +05:30
|
|
|
func DumpCPUProfileForUsername(pprofDataPath, username string) (func(), error) {
|
2021-09-22 11:08:34 +05:30
|
|
|
f, err := os.CreateTemp(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username))
|
2018-08-08 00:19:18 +05:30
|
|
|
if err != nil {
|
2019-06-01 20:30:21 +05:30
|
|
|
return nil, err
|
2018-08-08 00:19:18 +05:30
|
|
|
}
|
|
|
|
|
2019-06-13 01:11:28 +05:30
|
|
|
err = pprof.StartCPUProfile(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("StartCPUProfile: %v", err)
|
|
|
|
}
|
2018-08-08 00:19:18 +05:30
|
|
|
return func() {
|
|
|
|
pprof.StopCPUProfile()
|
2019-06-13 01:11:28 +05:30
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("StopCPUProfile Close: %v", err)
|
|
|
|
}
|
2019-06-01 20:30:21 +05:30
|
|
|
}, nil
|
2018-08-08 00:19:18 +05:30
|
|
|
}
|