github.com/tdcblockchain/tdcblockchain@v0.0.0-20191111034745-805c65ade158/les/metrics.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/metrics"
    21  	"github.com/ethereum/go-ethereum/p2p"
    22  )
    23  
    24  var (
    25  	miscInPacketsMeter           = metrics.NewRegisteredMeter("les/misc/in/packets/total", nil)
    26  	miscInTrafficMeter           = metrics.NewRegisteredMeter("les/misc/in/traffic/total", nil)
    27  	miscInHeaderPacketsMeter     = metrics.NewRegisteredMeter("les/misc/in/packets/header", nil)
    28  	miscInHeaderTrafficMeter     = metrics.NewRegisteredMeter("les/misc/in/traffic/header", nil)
    29  	miscInBodyPacketsMeter       = metrics.NewRegisteredMeter("les/misc/in/packets/body", nil)
    30  	miscInBodyTrafficMeter       = metrics.NewRegisteredMeter("les/misc/in/traffic/body", nil)
    31  	miscInCodePacketsMeter       = metrics.NewRegisteredMeter("les/misc/in/packets/code", nil)
    32  	miscInCodeTrafficMeter       = metrics.NewRegisteredMeter("les/misc/in/traffic/code", nil)
    33  	miscInReceiptPacketsMeter    = metrics.NewRegisteredMeter("les/misc/in/packets/receipt", nil)
    34  	miscInReceiptTrafficMeter    = metrics.NewRegisteredMeter("les/misc/in/traffic/receipt", nil)
    35  	miscInTrieProofPacketsMeter  = metrics.NewRegisteredMeter("les/misc/in/packets/proof", nil)
    36  	miscInTrieProofTrafficMeter  = metrics.NewRegisteredMeter("les/misc/in/traffic/proof", nil)
    37  	miscInHelperTriePacketsMeter = metrics.NewRegisteredMeter("les/misc/in/packets/helperTrie", nil)
    38  	miscInHelperTrieTrafficMeter = metrics.NewRegisteredMeter("les/misc/in/traffic/helperTrie", nil)
    39  	miscInTxsPacketsMeter        = metrics.NewRegisteredMeter("les/misc/in/packets/txs", nil)
    40  	miscInTxsTrafficMeter        = metrics.NewRegisteredMeter("les/misc/in/traffic/txs", nil)
    41  	miscInTxStatusPacketsMeter   = metrics.NewRegisteredMeter("les/misc/in/packets/txStatus", nil)
    42  	miscInTxStatusTrafficMeter   = metrics.NewRegisteredMeter("les/misc/in/traffic/txStatus", nil)
    43  
    44  	miscOutPacketsMeter           = metrics.NewRegisteredMeter("les/misc/out/packets/total", nil)
    45  	miscOutTrafficMeter           = metrics.NewRegisteredMeter("les/misc/out/traffic/total", nil)
    46  	miscOutHeaderPacketsMeter     = metrics.NewRegisteredMeter("les/misc/out/packets/header", nil)
    47  	miscOutHeaderTrafficMeter     = metrics.NewRegisteredMeter("les/misc/out/traffic/header", nil)
    48  	miscOutBodyPacketsMeter       = metrics.NewRegisteredMeter("les/misc/out/packets/body", nil)
    49  	miscOutBodyTrafficMeter       = metrics.NewRegisteredMeter("les/misc/out/traffic/body", nil)
    50  	miscOutCodePacketsMeter       = metrics.NewRegisteredMeter("les/misc/out/packets/code", nil)
    51  	miscOutCodeTrafficMeter       = metrics.NewRegisteredMeter("les/misc/out/traffic/code", nil)
    52  	miscOutReceiptPacketsMeter    = metrics.NewRegisteredMeter("les/misc/out/packets/receipt", nil)
    53  	miscOutReceiptTrafficMeter    = metrics.NewRegisteredMeter("les/misc/out/traffic/receipt", nil)
    54  	miscOutTrieProofPacketsMeter  = metrics.NewRegisteredMeter("les/misc/out/packets/proof", nil)
    55  	miscOutTrieProofTrafficMeter  = metrics.NewRegisteredMeter("les/misc/out/traffic/proof", nil)
    56  	miscOutHelperTriePacketsMeter = metrics.NewRegisteredMeter("les/misc/out/packets/helperTrie", nil)
    57  	miscOutHelperTrieTrafficMeter = metrics.NewRegisteredMeter("les/misc/out/traffic/helperTrie", nil)
    58  	miscOutTxsPacketsMeter        = metrics.NewRegisteredMeter("les/misc/out/packets/txs", nil)
    59  	miscOutTxsTrafficMeter        = metrics.NewRegisteredMeter("les/misc/out/traffic/txs", nil)
    60  	miscOutTxStatusPacketsMeter   = metrics.NewRegisteredMeter("les/misc/out/packets/txStatus", nil)
    61  	miscOutTxStatusTrafficMeter   = metrics.NewRegisteredMeter("les/misc/out/traffic/txStatus", nil)
    62  
    63  	connectionTimer       = metrics.NewRegisteredTimer("les/connection/duration", nil)
    64  	serverConnectionGauge = metrics.NewRegisteredGauge("les/connection/server", nil)
    65  	clientConnectionGauge = metrics.NewRegisteredGauge("les/connection/client", nil)
    66  
    67  	totalCapacityGauge   = metrics.NewRegisteredGauge("les/server/totalCapacity", nil)
    68  	totalRechargeGauge   = metrics.NewRegisteredGauge("les/server/totalRecharge", nil)
    69  	totalConnectedGauge  = metrics.NewRegisteredGauge("les/server/totalConnected", nil)
    70  	blockProcessingTimer = metrics.NewRegisteredTimer("les/server/blockProcessingTime", nil)
    71  
    72  	requestServedMeter    = metrics.NewRegisteredMeter("les/server/req/avgServedTime", nil)
    73  	requestServedTimer    = metrics.NewRegisteredTimer("les/server/req/servedTime", nil)
    74  	requestEstimatedMeter = metrics.NewRegisteredMeter("les/server/req/avgEstimatedTime", nil)
    75  	requestEstimatedTimer = metrics.NewRegisteredTimer("les/server/req/estimatedTime", nil)
    76  	relativeCostHistogram = metrics.NewRegisteredHistogram("les/server/req/relative", nil, metrics.NewExpDecaySample(1028, 0.015))
    77  
    78  	recentServedGauge    = metrics.NewRegisteredGauge("les/server/recentRequestServed", nil)
    79  	recentEstimatedGauge = metrics.NewRegisteredGauge("les/server/recentRequestEstimated", nil)
    80  	sqServedGauge        = metrics.NewRegisteredGauge("les/server/servingQueue/served", nil)
    81  	sqQueuedGauge        = metrics.NewRegisteredGauge("les/server/servingQueue/queued", nil)
    82  
    83  	clientConnectedMeter    = metrics.NewRegisteredMeter("les/server/clientEvent/connected", nil)
    84  	clientRejectedMeter     = metrics.NewRegisteredMeter("les/server/clientEvent/rejected", nil)
    85  	clientKickedMeter       = metrics.NewRegisteredMeter("les/server/clientEvent/kicked", nil)
    86  	clientDisconnectedMeter = metrics.NewRegisteredMeter("les/server/clientEvent/disconnected", nil)
    87  	clientFreezeMeter       = metrics.NewRegisteredMeter("les/server/clientEvent/freeze", nil)
    88  	clientErrorMeter        = metrics.NewRegisteredMeter("les/server/clientEvent/error", nil)
    89  
    90  	requestRTT       = metrics.NewRegisteredTimer("les/client/req/rtt", nil)
    91  	requestSendDelay = metrics.NewRegisteredTimer("les/client/req/sendDelay", nil)
    92  )
    93  
    94  // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of
    95  // accumulating the above defined metrics based on the data stream contents.
    96  type meteredMsgReadWriter struct {
    97  	p2p.MsgReadWriter     // Wrapped message stream to meter
    98  	version           int // Protocol version to select correct meters
    99  }
   100  
   101  // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the
   102  // metrics system is disabled, this function returns the original object.
   103  func newMeteredMsgWriter(rw p2p.MsgReadWriter, version int) p2p.MsgReadWriter {
   104  	if !metrics.Enabled {
   105  		return rw
   106  	}
   107  	return &meteredMsgReadWriter{MsgReadWriter: rw, version: version}
   108  }
   109  
   110  func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) {
   111  	// Read the message and short circuit in case of an error
   112  	msg, err := rw.MsgReadWriter.ReadMsg()
   113  	if err != nil {
   114  		return msg, err
   115  	}
   116  	// Account for the data traffic
   117  	packets, traffic := miscInPacketsMeter, miscInTrafficMeter
   118  	packets.Mark(1)
   119  	traffic.Mark(int64(msg.Size))
   120  
   121  	return msg, err
   122  }
   123  
   124  func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error {
   125  	// Account for the data traffic
   126  	packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter
   127  	packets.Mark(1)
   128  	traffic.Mark(int64(msg.Size))
   129  
   130  	// Send the packet to the p2p layer
   131  	return rw.MsgReadWriter.WriteMsg(msg)
   132  }