You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pprof.go 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package pprof
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "runtime"
  9. "runtime/pprof"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. // DumpMemProfileForUsername dumps a memory profile at pprofDataPath as memprofile_<username>_<temporary id>
  13. func DumpMemProfileForUsername(pprofDataPath, username string) error {
  14. f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("memprofile_%s_", username))
  15. if err != nil {
  16. return err
  17. }
  18. defer f.Close()
  19. runtime.GC() // get up-to-date statistics
  20. return pprof.WriteHeapProfile(f)
  21. }
  22. // DumpCPUProfileForUsername dumps a CPU profile at pprofDataPath as cpuprofile_<username>_<temporary id>
  23. // it returns the stop function which stops, writes and closes the CPU profile file
  24. func DumpCPUProfileForUsername(pprofDataPath, username string) (func(), error) {
  25. f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username))
  26. if err != nil {
  27. return nil, err
  28. }
  29. err = pprof.StartCPUProfile(f)
  30. if err != nil {
  31. log.Fatal("StartCPUProfile: %v", err)
  32. }
  33. return func() {
  34. pprof.StopCPUProfile()
  35. err = f.Close()
  36. if err != nil {
  37. log.Fatal("StopCPUProfile Close: %v", err)
  38. }
  39. }, nil
  40. }