Skip to content

Commit 23cd71f

Browse files
committedFeb 5, 2025·
rewrite most of txQueue.String; move around couple things
1 parent c535961 commit 23cd71f

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed
 

‎tcp/txqueue.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,23 @@ func (tx *ringTx) MakePacket(b []byte) (int, Value, error) {
123123
if err != nil {
124124
return n, 0, err
125125
}
126+
pkt := &tx.packets[nxtpkt]
126127

127128
off := tx.addEnd(tx.unsentoff, n)
128129
tx.unsentoff = off
129130
tx.sentend = off
130131
if off == tx.unsentend {
131132
tx.unsentend = 0 // Mark unsent as being empty.
132133
}
133-
134-
pkt := &tx.packets[nxtpkt]
135134
pkt.off = start
136135
pkt.end = off
137136

138137
// Sequence number updates.
139-
seq := tx.seq
140-
newseq := Add(seq, Size(n))
138+
oldseq := tx.seq
139+
newseq := Add(oldseq, Size(n))
141140
tx.seq = newseq
142141
pkt.seq = newseq
143-
return n, seq, nil
142+
return n, oldseq, nil
144143
}
145144

146145
// RecvSegment processes an incoming segment and updates the sent packet queue

‎tcp/txqueue_test.go

+58-46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tcp
22

33
import (
44
"bytes"
5+
"fmt"
56
"math/rand"
67
"testing"
78
)
@@ -149,17 +150,23 @@ func testTxQueue_SequentialMessages(t *testing.T, rtx *ringTx, msgs [][]byte, bu
149150
}
150151

151152
func testQueueSanity(t *testing.T, rtx *ringTx) {
152-
t.Helper()
153+
// t.Helper()
154+
defer func() {
155+
if t.Failed() {
156+
t.Log("\n" + rtx.string())
157+
}
158+
}()
153159
if rtx.emptyRing != (ringidx{}) {
154160
t.Fatalf("empty ring not empty")
155161
}
162+
156163
free := rtx.Free()
157164
sent := rtx.BufferedSent()
158165
unsent := rtx.Buffered()
159166
sz := rtx.Size()
160167
gotSz := free + sent + unsent
161168
if gotSz != sz {
162-
t.Fatal("\n", rtx.string())
169+
t.Fatal("\n" + rtx.string())
163170
t.Fatalf("want size=%d, got size=%d (free+sent+unsent=%d+%d+%d)", sz, gotSz, free, sent, unsent)
164171
}
165172
freeStart, freeEnd, sentEnd := rtx.lims()
@@ -176,18 +183,30 @@ func testQueueSanity(t *testing.T, rtx *ringTx) {
176183
}
177184

178185
func (rx *ringTx) string() string {
179-
return ""
186+
sz := rx.Size()
187+
unsent, _ := rx.unsentRing()
188+
sent, _ := rx.sentRing()
189+
all := rx.sentAndUnsentBuffer()
190+
if all.End == 0 || // Empty buffer, set offset so that free zone occupies whole buffer.
191+
all.Off == 0 { // Buffer offset starts at zero which would set Free.End to 0 making it empty, patch that.
192+
all.Off = sz
193+
}
180194
type zone struct {
181-
name string
182-
start, end int
183-
printStart, printEnd bool
195+
name string
196+
start, end int
197+
}
198+
zcontains := func(off int, z *zone) bool {
199+
if z.end == 0 {
200+
return false // Empty
201+
} else if z.end < z.start {
202+
return off < z.end || off >= z.start
203+
}
204+
return off >= z.start && off < z.end
184205
}
185-
186-
fs, fe, us := rx.lims()
187206
var zones = []zone{
188-
{name: "free", start: fs, end: fe},
189-
{name: "usnt", start: us, end: fs},
190-
{name: "sent", start: fe, end: us},
207+
{name: "free", start: all.End, end: all.Off},
208+
{name: "usnt", start: unsent.Off, end: unsent.End},
209+
{name: "sent", start: sent.Off, end: sent.End},
191210
}
192211
var wrapZone *zone
193212
for i := range zones {
@@ -199,43 +218,36 @@ func (rx *ringTx) string() string {
199218
wrapZone = &zones[i]
200219
}
201220
}
202-
203-
var b1, b2 bytes.Buffer
204-
b1.WriteByte('|')
205-
b2.WriteByte(' ')
206-
b2.WriteByte(' ')
207-
for i := 0; i < len(rx.rawbuf); {
208-
var printedThisline int
209-
var zoneName string
210-
for k := range zones {
211-
z := &zones[k]
212-
if z.end == 0 {
213-
continue // No data in zone.
214-
}
215-
if !z.printStart && i >= z.start {
216-
zoneName = z.name
217-
if printedThisline > 0 {
218-
b2.WriteByte('/')
219-
printedThisline++
220-
}
221-
b2.WriteString(zoneName + "_s")
222-
printedThisline += len(zoneName) + 2
223-
z.printStart = true
221+
var currentZone *zone
222+
var lastPrintedZone *zone
223+
var l1, l2 bytes.Buffer
224+
changes := 0
225+
for ib := 0; ib < sz; ib++ {
226+
currentContainsIdx := currentZone != nil && zcontains(ib, currentZone)
227+
for iz := 0; !currentContainsIdx && iz < len(zones); iz++ {
228+
z := &zones[iz]
229+
if zcontains(ib, z) {
230+
currentZone = z
224231
}
225-
226232
}
227-
if printedThisline > 0 {
228-
b1.WriteByte('|')
229-
b2.WriteByte(' ')
230-
b2.WriteByte(' ')
231-
for j := 0; j < printedThisline+1; j++ {
232-
b1.WriteByte('-')
233-
}
233+
if currentZone == lastPrintedZone {
234+
continue
235+
}
236+
changes++
237+
if changes > 4 {
238+
panic("found too many zone changes")
239+
}
240+
lastPrintedZone = currentZone
241+
// Change of zone.
242+
top := "|-----" + currentZone.name + "-----"
243+
l2.WriteString(top)
244+
n, _ := fmt.Fprintf(&l1, "%d", currentZone.start)
245+
for i := 0; i < len(top)-n; i++ {
246+
l1.WriteByte(' ')
234247
}
235-
b2.WriteByte(' ')
236-
b1.WriteByte('-')
237248
}
238-
b1.WriteString("|\n")
239-
b1.Write(b2.Bytes())
240-
return b1.String()
249+
l2.WriteByte('|')
250+
fmt.Fprintf(&l1, "%d\n", currentZone.end)
251+
l2.WriteTo(&l1)
252+
return l1.String()
241253
}

0 commit comments

Comments
 (0)
Please sign in to comment.