code.gitea.io/gitea@v1.19.3/modules/pprof/pprof.go (about) 1 // Copyright 2018 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package pprof 5 6 import ( 7 "fmt" 8 "os" 9 "runtime" 10 "runtime/pprof" 11 12 "code.gitea.io/gitea/modules/log" 13 ) 14 15 // DumpMemProfileForUsername dumps a memory profile at pprofDataPath as memprofile_<username>_<temporary id> 16 func DumpMemProfileForUsername(pprofDataPath, username string) error { 17 f, err := os.CreateTemp(pprofDataPath, fmt.Sprintf("memprofile_%s_", username)) 18 if err != nil { 19 return err 20 } 21 defer f.Close() 22 runtime.GC() // get up-to-date statistics 23 return pprof.WriteHeapProfile(f) 24 } 25 26 // DumpCPUProfileForUsername dumps a CPU profile at pprofDataPath as cpuprofile_<username>_<temporary id> 27 // the stop function it returns stops, writes and closes the CPU profile file 28 func DumpCPUProfileForUsername(pprofDataPath, username string) (func(), error) { 29 f, err := os.CreateTemp(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username)) 30 if err != nil { 31 return nil, err 32 } 33 34 err = pprof.StartCPUProfile(f) 35 if err != nil { 36 log.Fatal("StartCPUProfile: %v", err) 37 } 38 return func() { 39 pprof.StopCPUProfile() 40 err = f.Close() 41 if err != nil { 42 log.Fatal("StopCPUProfile Close: %v", err) 43 } 44 }, nil 45 }