proc.go 892 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package logic
  2. import (
  3. "os"
  4. "runtime"
  5. "runtime/pprof"
  6. "github.com/gravitl/netmaker/logger"
  7. )
  8. func StartCPUProfiling() *os.File {
  9. f, err := os.OpenFile("/root/data/cpu.prof", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
  10. if err != nil {
  11. logger.Log(0, "could not create CPU profile: ", err.Error())
  12. }
  13. if err := pprof.StartCPUProfile(f); err != nil {
  14. logger.Log(0, "could not start CPU profile: ", err.Error())
  15. }
  16. return f
  17. }
  18. func StopCPUProfiling(f *os.File) {
  19. pprof.StopCPUProfile()
  20. f.Close()
  21. }
  22. func StartMemProfiling() {
  23. f, err := os.OpenFile("/root/data/mem.prof", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
  24. if err != nil {
  25. logger.Log(0, "could not create Memory profile: ", err.Error())
  26. }
  27. defer f.Close()
  28. runtime.GC() // get up-to-date statistics
  29. if err = pprof.WriteHeapProfile(f); err != nil {
  30. logger.Log(0, "could not write memory profile: ", err.Error())
  31. }
  32. }