From d934ec7e30fe1ba2ad8fe0ab45ad0adfc3796796 Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Fri, 1 Sep 2017 10:36:43 +1000
Subject: [PATCH] Add some variables to track fillBuffer effectiveness

---
 connection.go | 11 +++++++++++
 global.go     |  4 ++++
 2 files changed, 15 insertions(+)

diff --git a/connection.go b/connection.go
index 20fec26f..45c2c20a 100644
--- a/connection.go
+++ b/connection.go
@@ -346,12 +346,15 @@ var (
 )
 
 func (cn *connection) fillWriteBuffer(msg func(pp.Message) bool) {
+	numFillBuffers.Add(1)
 	rs, i := cn.desiredRequestState()
 	if !cn.SetInterested(i, msg) {
 		return
 	}
+	sentCancels := false
 	for r := range cn.requests {
 		if _, ok := rs[r]; !ok {
+			sentCancels = true
 			delete(cn.requests, r)
 			// log.Printf("%p: cancelling request: %v", cn, r)
 			if !msg(pp.Message{
@@ -364,12 +367,17 @@ func (cn *connection) fillWriteBuffer(msg func(pp.Message) bool) {
 			}
 		}
 	}
+	if sentCancels {
+		fillBufferSentCancels.Add(1)
+	}
+	sentRequests := false
 	for r := range rs {
 		if _, ok := cn.requests[r]; !ok {
 			if cn.requests == nil {
 				cn.requests = make(map[request]struct{}, cn.nominalMaxRequests())
 			}
 			cn.requests[r] = struct{}{}
+			sentRequests = true
 			// log.Printf("%p: requesting %v", cn, r)
 			if !msg(pp.Message{
 				Type:   pp.Request,
@@ -381,6 +389,9 @@ func (cn *connection) fillWriteBuffer(msg func(pp.Message) bool) {
 			}
 		}
 	}
+	if sentRequests {
+		fillBufferSentRequests.Add(1)
+	}
 }
 
 // Writes buffers to the socket from the write channel.
diff --git a/global.go b/global.go
index 0d241128..a89659ae 100644
--- a/global.go
+++ b/global.go
@@ -93,4 +93,8 @@ var (
 	pieceInclinationsReused = expvar.NewInt("pieceInclinationsReused")
 	pieceInclinationsNew    = expvar.NewInt("pieceInclinationsNew")
 	pieceInclinationsPut    = expvar.NewInt("pieceInclinationsPut")
+
+	fillBufferSentCancels  = expvar.NewInt("fillBufferSentCancels")
+	fillBufferSentRequests = expvar.NewInt("fillBufferSentRequests")
+	numFillBuffers         = expvar.NewInt("numFillBuffers")
 )
-- 
2.51.0