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.2KB

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