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

pprof.go 1.1 kB

1234567891011121314151617181920212223242526272829303132333435363738
  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. )
  11. // DumpMemProfileForUsername dumps a memory profile at pprofDataPath as memprofile_<username>_<temporary id>
  12. func DumpMemProfileForUsername(pprofDataPath, username string) error {
  13. f, err := ioutil.TempFile(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. // it returns the stop function which stops, writes and closes the CPU profile file
  23. func DumpCPUProfileForUsername(pprofDataPath, username string) (func(), error) {
  24. f, err := ioutil.TempFile(pprofDataPath, fmt.Sprintf("cpuprofile_%s_", username))
  25. if err != nil {
  26. return nil, err
  27. }
  28. pprof.StartCPUProfile(f)
  29. return func() {
  30. pprof.StopCPUProfile()
  31. f.Close()
  32. }, nil
  33. }