From ad5e44eaf52b637e94a33d893a58853a4c265cc6 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 15 Jun 2018 22:42:05 +1000 Subject: [PATCH] Add lots of new expvars and perf timers --- client.go | 23 +++++++++++++++++++---- mse/mse.go | 3 +++ socket.go | 4 +++- torrent.go | 8 +++++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index b7d57b09..49142431 100644 --- a/client.go +++ b/client.go @@ -14,6 +14,8 @@ import ( "strings" "time" + "github.com/anacrolix/missinggo/perf" + "github.com/anacrolix/dht" "github.com/anacrolix/dht/krpc" "github.com/anacrolix/log" @@ -541,9 +543,12 @@ func (cl *Client) dialFirst(ctx context.Context, addr string) net.Conn { }() var res dialResult // Wait for a successful connection. - for ; left > 0 && res.Conn == nil; left-- { - res = <-resCh - } + func() { + defer perf.ScopeTimer()() + for ; left > 0 && res.Conn == nil; left-- { + res = <-resCh + } + }() // There are still incompleted dials. go func() { for ; left > 0; left-- { @@ -709,6 +714,7 @@ func (cl *Client) forSkeys(f func([]byte) bool) { // Do encryption and bittorrent handshakes as receiver. func (cl *Client) receiveHandshakes(c *connection) (t *Torrent, err error) { + defer perf.ScopeTimerErr(&err)() var rw io.ReadWriter rw, c.headerEncrypted, c.cryptoMethod, err = handleEncryption(c.rw(), cl.forSkeys, cl.config.EncryptionPolicy) c.setRW(rw) @@ -756,18 +762,27 @@ func (cl *Client) runReceivedConn(c *connection) { } t, err := cl.receiveHandshakes(c) if err != nil { - log.Fmsg("error receiving handshakes: %s", err).AddValue(debugLogValue).Add("network", c.remoteAddr().Network()).Log(cl.logger) + log.Fmsg( + "error receiving handshakes: %s", err, + ).AddValue( + debugLogValue, + ).Add( + "network", c.remoteAddr().Network(), + ).Log(cl.logger) + torrent.Add("error receiving handshake", 1) cl.mu.Lock() cl.onBadAccept(c.remoteAddr()) cl.mu.Unlock() return } if t == nil { + torrent.Add("received handshake for unloaded torrent", 1) cl.mu.Lock() cl.onBadAccept(c.remoteAddr()) cl.mu.Unlock() return } + torrent.Add("received handshake for loaded torrent", 1) cl.mu.Lock() defer cl.mu.Unlock() cl.runHandshookConn(c, t) diff --git a/mse/mse.go b/mse/mse.go index 83454b82..b10090c2 100644 --- a/mse/mse.go +++ b/mse/mse.go @@ -18,6 +18,8 @@ import ( "strconv" "sync" + "github.com/anacrolix/missinggo/perf" + "github.com/bradfitz/iter" ) @@ -537,6 +539,7 @@ func InitiateHandshake(rw io.ReadWriter, skey []byte, initialPayload []byte, cry ia: initialPayload, cryptoProvides: cryptoProvides, } + defer perf.ScopeTimerErr(&err)() return h.Do() } diff --git a/socket.go b/socket.go index 33a19bc2..716b0c62 100644 --- a/socket.go +++ b/socket.go @@ -11,6 +11,7 @@ import ( "golang.org/x/net/proxy" "github.com/anacrolix/missinggo" + "github.com/anacrolix/missinggo/perf" ) type dialer interface { @@ -169,7 +170,8 @@ type utpSocketSocket struct { d proxy.Dialer } -func (me utpSocketSocket) dial(ctx context.Context, addr string) (net.Conn, error) { +func (me utpSocketSocket) dial(ctx context.Context, addr string) (conn net.Conn, err error) { + defer perf.ScopeTimerErr(&err)() if me.d != nil { return me.d.Dial(me.network, addr) } diff --git a/torrent.go b/torrent.go index 2bb5f20b..3c6c3d52 100644 --- a/torrent.go +++ b/torrent.go @@ -1222,6 +1222,7 @@ func (t *Torrent) deleteConnection(c *connection) (ret bool) { } _, ret = t.conns[c] delete(t.conns, c) + torrent.Add("deleted connections", 1) c.deleteAllRequests() if len(t.conns) == 0 { t.assertNoPendingRequests() @@ -1493,7 +1494,12 @@ func (t *Torrent) reconcileHandshakeStats(c *connection) { } // Returns true if the connection is added. -func (t *Torrent) addConnection(c *connection) error { +func (t *Torrent) addConnection(c *connection) (err error) { + defer func() { + if err == nil { + torrent.Add("added connections", 1) + } + }() if t.closed.IsSet() { return errors.New("torrent closed") }