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.

42 lines
1.3 KiB

  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) {
  14. f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("memprofile_%s_", username))
  15. if err != nil {
  16. log.GitLogger.Fatal(4, "Could not create memory profile: %v", err)
  17. }
  18. defer f.Close()
  19. runtime.GC() // get up-to-date statistics
  20. if err := pprof.WriteHeapProfile(f); err != nil {
  21. log.GitLogger.Fatal(4, "Could not write memory profile: %v", err)
  22. }
  23. }
  24. // DumpCPUProfileForUsername dumps a CPU profile at pprofDataPath as cpuprofile_<username>_<temporary id>
  25. // it returns the stop function which stops, writes and closes the CPU profile file
  26. func DumpCPUProfileForUsername(pprofDataPath, username string) func() {
  27. f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username))
  28. if err != nil {
  29. log.GitLogger.Fatal(4, "Could not create cpu profile: %v", err)
  30. }
  31. pprof.StartCPUProfile(f)
  32. return func() {
  33. pprof.StopCPUProfile()
  34. f.Close()
  35. }
  36. }