github.com/luckypickle/go-ethereum-vet@v1.14.2/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/luckypickle/go-ethereum-vet/metrics"
    21  	"github.com/luckypickle/go-ethereum-vet/p2p"
    22  )
    23  
    24  var (
    25  	/*	propTxnInPacketsMeter     = metrics.NewMeter("eth/prop/txns/in/packets")
    26  		propTxnInTrafficMeter     = metrics.NewMeter("eth/prop/txns/in/traffic")
    27  		propTxnOutPacketsMeter    = metrics.NewMeter("eth/prop/txns/out/packets")
    28  		propTxnOutTrafficMeter    = metrics.NewMeter("eth/prop/txns/out/traffic")
    29  		propHashInPacketsMeter    = metrics.NewMeter("eth/prop/hashes/in/packets")
    30  		propHashInTrafficMeter    = metrics.NewMeter("eth/prop/hashes/in/traffic")
    31  		propHashOutPacketsMeter   = metrics.NewMeter("eth/prop/hashes/out/packets")
    32  		propHashOutTrafficMeter   = metrics.NewMeter("eth/prop/hashes/out/traffic")
    33  		propBlockInPacketsMeter   = metrics.NewMeter("eth/prop/blocks/in/packets")
    34  		propBlockInTrafficMeter   = metrics.NewMeter("eth/prop/blocks/in/traffic")
    35  		propBlockOutPacketsMeter  = metrics.NewMeter("eth/prop/blocks/out/packets")
    36  		propBlockOutTrafficMeter  = metrics.NewMeter("eth/prop/blocks/out/traffic")
    37  		reqHashInPacketsMeter     = metrics.NewMeter("eth/req/hashes/in/packets")
    38  		reqHashInTrafficMeter     = metrics.NewMeter("eth/req/hashes/in/traffic")
    39  		reqHashOutPacketsMeter    = metrics.NewMeter("eth/req/hashes/out/packets")
    40  		reqHashOutTrafficMeter    = metrics.NewMeter("eth/req/hashes/out/traffic")
    41  		reqBlockInPacketsMeter    = metrics.NewMeter("eth/req/blocks/in/packets")
    42  		reqBlockInTrafficMeter    = metrics.NewMeter("eth/req/blocks/in/traffic")
    43  		reqBlockOutPacketsMeter   = metrics.NewMeter("eth/req/blocks/out/packets")
    44  		reqBlockOutTrafficMeter   = metrics.NewMeter("eth/req/blocks/out/traffic")
    45  		reqHeaderInPacketsMeter   = metrics.NewMeter("eth/req/headers/in/packets")
    46  		reqHeaderInTrafficMeter   = metrics.NewMeter("eth/req/headers/in/traffic")
    47  		reqHeaderOutPacketsMeter  = metrics.NewMeter("eth/req/headers/out/packets")
    48  		reqHeaderOutTrafficMeter  = metrics.NewMeter("eth/req/headers/out/traffic")
    49  		reqBodyInPacketsMeter     = metrics.NewMeter("eth/req/bodies/in/packets")
    50  		reqBodyInTrafficMeter     = metrics.NewMeter("eth/req/bodies/in/traffic")
    51  		reqBodyOutPacketsMeter    = metrics.NewMeter("eth/req/bodies/out/packets")
    52  		reqBodyOutTrafficMeter    = metrics.NewMeter("eth/req/bodies/out/traffic")
    53  		reqStateInPacketsMeter    = metrics.NewMeter("eth/req/states/in/packets")
    54  		reqStateInTrafficMeter    = metrics.NewMeter("eth/req/states/in/traffic")
    55  		reqStateOutPacketsMeter   = metrics.NewMeter("eth/req/states/out/packets")
    56  		reqStateOutTrafficMeter   = metrics.NewMeter("eth/req/states/out/traffic")
    57  		reqReceiptInPacketsMeter  = metrics.NewMeter("eth/req/receipts/in/packets")
    58  		reqReceiptInTrafficMeter  = metrics.NewMeter("eth/req/receipts/in/traffic")
    59  		reqReceiptOutPacketsMeter = metrics.NewMeter("eth/req/receipts/out/packets")
    60  		reqReceiptOutTrafficMeter = metrics.NewMeter("eth/req/receipts/out/traffic")*/
    61  	miscInPacketsMeter  = metrics.NewRegisteredMeter("les/misc/in/packets", nil)
    62  	miscInTrafficMeter  = metrics.NewRegisteredMeter("les/misc/in/traffic", nil)
    63  	miscOutPacketsMeter = metrics.NewRegisteredMeter("les/misc/out/packets", nil)
    64  	miscOutTrafficMeter = metrics.NewRegisteredMeter("les/misc/out/traffic", nil)
    65  )
    66  
    67  // meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of
    68  // accumulating the above defined metrics based on the data stream contents.
    69  type meteredMsgReadWriter struct {
    70  	p2p.MsgReadWriter     // Wrapped message stream to meter
    71  	version           int // Protocol version to select correct meters
    72  }
    73  
    74  // newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the
    75  // metrics system is disabled, this function returns the original object.
    76  func newMeteredMsgWriter(rw p2p.MsgReadWriter) p2p.MsgReadWriter {
    77  	if !metrics.Enabled {
    78  		return rw
    79  	}
    80  	return &meteredMsgReadWriter{MsgReadWriter: rw}
    81  }
    82  
    83  // Init sets the protocol version used by the stream to know which meters to
    84  // increment in case of overlapping message ids between protocol versions.
    85  func (rw *meteredMsgReadWriter) Init(version int) {
    86  	rw.version = version
    87  }
    88  
    89  func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) {
    90  	// Read the message and short circuit in case of an error
    91  	msg, err := rw.MsgReadWriter.ReadMsg()
    92  	if err != nil {
    93  		return msg, err
    94  	}
    95  	// Account for the data traffic
    96  	packets, traffic := miscInPacketsMeter, miscInTrafficMeter
    97  	packets.Mark(1)
    98  	traffic.Mark(int64(msg.Size))
    99  
   100  	return msg, err
   101  }
   102  
   103  func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error {
   104  	// Account for the data traffic
   105  	packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter
   106  	packets.Mark(1)
   107  	traffic.Mark(int64(msg.Size))
   108  
   109  	// Send the packet to the p2p layer
   110  	return rw.MsgReadWriter.WriteMsg(msg)
   111  }