udp_linux_writebatch_alloc_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //go:build linux && !android && !e2e_testing
  2. package udp
  3. import (
  4. "net/netip"
  5. "testing"
  6. )
  7. // TestWriteBatchNoAllocs verifies the sendmmsg/UDP-GSO transmit path performs
  8. // no per-packet heap allocations on the happy path: all mmsghdr/iovec/cmsg
  9. // scratch is preallocated in newBatchWriter and WriteBatch may only rewrite
  10. // it. The batch deliberately mixes a GSO-eligible run, a short tail segment,
  11. // destination changes, and zero/nonzero outer ECN so the planner, sockaddr,
  12. // and cmsg paths are all exercised.
  13. func TestWriteBatchNoAllocs(t *testing.T) {
  14. for _, tc := range []struct {
  15. name string
  16. addr string
  17. }{
  18. {"v4", "127.0.0.1"},
  19. {"v6", "::1"},
  20. } {
  21. t.Run(tc.name, func(t *testing.T) {
  22. ip := netip.MustParseAddr(tc.addr)
  23. newConn := func() Conn {
  24. c, err := NewListener(testLogger(), ip, 0, false, 8)
  25. if err != nil {
  26. t.Fatalf("NewListener: %v", err)
  27. }
  28. t.Cleanup(func() { _ = c.Close() })
  29. return c
  30. }
  31. tx := newConn()
  32. rxA := newConn()
  33. rxB := newConn()
  34. if sc, ok := tx.(*StdConn); ok {
  35. // Records which planner path the measurement covered; GSO
  36. // support depends on the running kernel.
  37. t.Logf("gsoSupported=%v maxGSOSegments=%d", sc.bw.gsoSupported, sc.bw.maxGSOSegments)
  38. }
  39. dstA, err := rxA.LocalAddr()
  40. if err != nil {
  41. t.Fatalf("LocalAddr: %v", err)
  42. }
  43. dstB, err := rxB.LocalAddr()
  44. if err != nil {
  45. t.Fatalf("LocalAddr: %v", err)
  46. }
  47. payload := make([]byte, 1200)
  48. short := make([]byte, 900)
  49. var bufs [][]byte
  50. var addrs []netip.AddrPort
  51. var ecns []byte
  52. add := func(b []byte, dst netip.AddrPort, ecn byte) {
  53. bufs = append(bufs, b)
  54. addrs = append(addrs, dst)
  55. ecns = append(ecns, ecn)
  56. }
  57. // GSO-eligible run with a short tail, all ECT(0).
  58. for k := 0; k < 8; k++ {
  59. add(payload, dstA, 0b10)
  60. }
  61. add(short, dstA, 0b10)
  62. // ECN change on the same destination forces a run boundary.
  63. add(payload, dstA, 0)
  64. // Alternating destinations defeat coalescing entirely.
  65. for k := 0; k < 4; k++ {
  66. dst := dstA
  67. if k%2 == 0 {
  68. dst = dstB
  69. }
  70. add(payload, dst, 0)
  71. }
  72. send := func(ecns []byte) {
  73. t.Helper()
  74. var werr error
  75. // Warm-up outside the measured runs.
  76. if err := tx.WriteBatch(bufs, addrs, ecns); err != nil {
  77. t.Fatalf("WriteBatch warm-up: %v", err)
  78. }
  79. allocs := testing.AllocsPerRun(100, func() {
  80. if err := tx.WriteBatch(bufs, addrs, ecns); err != nil {
  81. werr = err
  82. }
  83. })
  84. if werr != nil {
  85. t.Fatalf("WriteBatch: %v", werr)
  86. }
  87. if allocs != 0 {
  88. t.Fatalf("WriteBatch allocated %.1f times per call, want 0", allocs)
  89. }
  90. }
  91. send(ecns)
  92. send(nil)
  93. })
  94. }
  95. }