debug.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package udptransfer
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/signal"
  8. "runtime"
  9. "runtime/pprof"
  10. "sync/atomic"
  11. "syscall"
  12. )
  13. var enable_pprof bool
  14. var enable_stacktrace bool
  15. var debug_inited int32
  16. func set_debug_params(p *Params) {
  17. if atomic.CompareAndSwapInt32(&debug_inited, 0, 1) {
  18. debug = p.Debug
  19. enable_pprof = p.EnablePprof
  20. enable_stacktrace = p.Stacktrace
  21. if enable_pprof {
  22. f, err := os.Create("udptransfer.pprof")
  23. if err != nil {
  24. log.Fatalln(err)
  25. }
  26. pprof.StartCPUProfile(f)
  27. }
  28. }
  29. }
  30. func (c *Conn) PrintState() {
  31. log.Printf("inQ=%d inQReady=%d outQ=%d", c.inQ.size(), len(c.inQReady), c.outQ.size())
  32. log.Printf("inMaxCtnSeq=%d lastAck=%d lastReadSeq=%d", c.inQ.maxCtnSeq, c.lastAck, c.lastReadSeq)
  33. if c.inPkCnt > 0 {
  34. log.Printf("Rx pcnt=%d dups=%d %%d=%f%%", c.inPkCnt, c.inDupCnt, 100*float32(c.inDupCnt)/float32(c.inPkCnt))
  35. }
  36. if c.outPkCnt > 0 {
  37. log.Printf("Tx pcnt=%d dups=%d %%d=%f%%", c.outPkCnt, c.outDupCnt, 100*float32(c.outDupCnt)/float32(c.outPkCnt))
  38. }
  39. log.Printf("current-rtt=%d FastRetransmit=%d", c.rtt, c.fRCnt)
  40. if enable_stacktrace {
  41. var buf = make([]byte, 6400)
  42. for i := 0; i < 3; i++ {
  43. n := runtime.Stack(buf, true)
  44. if n >= len(buf) {
  45. buf = make([]byte, len(buf)<<1)
  46. } else {
  47. buf = buf[:n]
  48. break
  49. }
  50. }
  51. fmt.Println(string(buf))
  52. }
  53. if enable_pprof {
  54. pprof.StopCPUProfile()
  55. }
  56. }
  57. func (c *Conn) internal_state() {
  58. ev := make(chan os.Signal, 10)
  59. signal.Notify(ev, syscall.Signal(12), syscall.SIGINT)
  60. for v := range ev {
  61. c.PrintState()
  62. if v == syscall.SIGINT {
  63. os.Exit(1)
  64. }
  65. }
  66. }
  67. func printBits(b uint64, j, s, d uint32) {
  68. fmt.Printf("bits=%064b j=%d seq=%d dis=%d\n", b, j, s, d)
  69. }
  70. func dumpb(label string, buf []byte) {
  71. log.Println(label, "\n", hex.Dump(buf))
  72. }
  73. func dumpQ(s string, q *linkedMap) {
  74. var seqs = make([]uint32, 0, 20)
  75. n := q.head
  76. for i, m := int32(0), q.size(); i < m && n != nil; i++ {
  77. seqs = append(seqs, n.seq)
  78. n = n.next
  79. if len(seqs) == 20 {
  80. log.Printf("%s: Q=%d", s, seqs)
  81. seqs = seqs[:0]
  82. }
  83. }
  84. if len(seqs) > 0 {
  85. log.Printf("%s: Q=%d", s, seqs)
  86. }
  87. }