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.

debug.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "os"
  7. "runtime"
  8. "runtime/debug"
  9. "runtime/pprof"
  10. "time"
  11. "github.com/DanielOaks/girc-go/ircmsg"
  12. )
  13. // DEBUG GCSTATS/NUMGOROUTINE/etc
  14. func debugHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
  15. if !client.flags[Operator] {
  16. return false
  17. }
  18. switch msg.Params[0] {
  19. case "GCSTATS":
  20. stats := debug.GCStats{
  21. Pause: make([]time.Duration, 10),
  22. PauseQuantiles: make([]time.Duration, 5),
  23. }
  24. debug.ReadGCStats(&stats)
  25. client.Notice(fmt.Sprintf("last GC: %s", stats.LastGC.Format(time.RFC1123)))
  26. client.Notice(fmt.Sprintf("num GC: %d", stats.NumGC))
  27. client.Notice(fmt.Sprintf("pause total: %s", stats.PauseTotal))
  28. client.Notice(fmt.Sprintf("pause quantiles min%%: %s", stats.PauseQuantiles[0]))
  29. client.Notice(fmt.Sprintf("pause quantiles 25%%: %s", stats.PauseQuantiles[1]))
  30. client.Notice(fmt.Sprintf("pause quantiles 50%%: %s", stats.PauseQuantiles[2]))
  31. client.Notice(fmt.Sprintf("pause quantiles 75%%: %s", stats.PauseQuantiles[3]))
  32. client.Notice(fmt.Sprintf("pause quantiles max%%: %s", stats.PauseQuantiles[4]))
  33. case "NUMGOROUTINE":
  34. count := runtime.NumGoroutine()
  35. client.Notice(fmt.Sprintf("num goroutines: %d", count))
  36. case "PROFILEHEAP":
  37. profFile := "ergonomadic.mprof"
  38. file, err := os.Create(profFile)
  39. if err != nil {
  40. client.Notice(fmt.Sprintf("error: %s", err))
  41. break
  42. }
  43. defer file.Close()
  44. pprof.Lookup("heap").WriteTo(file, 0)
  45. client.Notice(fmt.Sprintf("written to %s", profFile))
  46. case "STARTCPUPROFILE":
  47. profFile := "ergonomadic.prof"
  48. file, err := os.Create(profFile)
  49. if err != nil {
  50. client.Notice(fmt.Sprintf("error: %s", err))
  51. break
  52. }
  53. if err := pprof.StartCPUProfile(file); err != nil {
  54. defer file.Close()
  55. client.Notice(fmt.Sprintf("error: %s", err))
  56. break
  57. }
  58. client.Notice(fmt.Sprintf("CPU profile writing to %s", profFile))
  59. case "STOPCPUPROFILE":
  60. pprof.StopCPUProfile()
  61. client.Notice(fmt.Sprintf("CPU profiling stopped"))
  62. }
  63. return false
  64. }