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

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