From bd9a6bd6256dd394f06bb406596d98299f4b2781 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Mon, 30 Jul 2018 21:41:16 -0700 Subject: [PATCH] htlcswitch/link: conditional batch ticker In this commit, we prevent the htlcManager from being woken up by the batchTicker when there is no work to be done. Profiling has shown a significant portion of CPU time idling, since the batch ticker endlessly demands resources. We resolve this by only selecting on the batch ticker when we have a non-empty batch of downstream packets from the switch. --- htlcswitch/link.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 22de510e8..2914f9f20 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -846,6 +846,13 @@ func (l *channelLink) htlcManager() { batchTick := l.cfg.BatchTicker.Start() defer l.cfg.BatchTicker.Stop() + // We'll only need the batch ticker if we have outgoing updates that are + // not covered by our last signature. This value will be nil unless a + // downstream packet forces the batchCounter to be positive. After the + // batch is cleared, it will return to nil to prevent wasteful CPU time + // caused by the batch ticker waking up the htlcManager needlessly. + var maybeBatchTick <-chan time.Time + out: for { // We must always check if we failed at some point processing @@ -926,10 +933,12 @@ out: break out } - case <-batchTick: + case <-maybeBatchTick: // If the current batch is empty, then we have no work - // here. + // here. We also disable the batch ticker from waking up + // the htlcManager while the batch is empty. if l.batchCounter == 0 { + maybeBatchTick = nil continue } @@ -955,6 +964,13 @@ out: l.handleDownStreamPkt(packet, true) + // If the downstream packet resulted in a non-empty + // batch, reinstate the batch ticker so that it can be + // cleared. + if l.batchCounter > 0 { + maybeBatchTick = batchTick + } + // A message from the switch was just received. This indicates // that the link is an intermediate hop in a multi-hop HTLC // circuit. @@ -977,6 +993,13 @@ out: l.handleDownStreamPkt(pkt, false) + // If the downstream packet resulted in a non-empty + // batch, reinstate the batch ticker so that it can be + // cleared. + if l.batchCounter > 0 { + maybeBatchTick = batchTick + } + // A message from the connected peer was just received. This // indicates that we have a new incoming HTLC, either directly // for us, or part of a multi-hop HTLC circuit.