github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/metrics/metrics.go (about)

     1  // Copyright 2015 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 metrics centralizes the registration.
    18  package metrics
    19  
    20  import (
    21  	"bufio"
    22  	"encoding/json"
    23  	"os"
    24  	"runtime"
    25  	"time"
    26  
    27  	"bytes"
    28  
    29  	"github.com/ethereumproject/go-ethereum/logger/glog"
    30  	"github.com/rcrowley/go-metrics"
    31  )
    32  
    33  // Reg is the metrics destination.
    34  var reg = metrics.NewRegistry()
    35  
    36  var (
    37  	MsgTXNIn           = metrics.NewRegisteredMeter("msg/txn/in", reg)
    38  	MsgTXNInBytes      = metrics.NewRegisteredMeter("msg/txn/in/bytes", reg)
    39  	MsgTXNOut          = metrics.NewRegisteredMeter("msg/txn/out", reg)
    40  	MsgTXNOutBytes     = metrics.NewRegisteredMeter("msg/txn/out/bytes", reg)
    41  	MsgHashIn          = metrics.NewRegisteredMeter("msg/hash/in", reg)
    42  	MsgHashInBytes     = metrics.NewRegisteredMeter("msg/hash/out/bytes", reg)
    43  	MsgHashOut         = metrics.NewRegisteredMeter("msg/hash/in", reg)
    44  	MsgHashOutBytes    = metrics.NewRegisteredMeter("msg/hash/out/bytes", reg)
    45  	MsgBlockIn         = metrics.NewRegisteredMeter("msg/block/in", reg)
    46  	MsgBlockInBytes    = metrics.NewRegisteredMeter("msg/block/in/bytes", reg)
    47  	MsgBlockOut        = metrics.NewRegisteredMeter("msg/block/out", reg)
    48  	MsgBlockOutBytes   = metrics.NewRegisteredMeter("msg/block/out/bytes", reg)
    49  	MsgHeaderIn        = metrics.NewRegisteredMeter("msg/header/in", reg)
    50  	MsgHeaderInBytes   = metrics.NewRegisteredMeter("msg/header/in/bytes", reg)
    51  	MsgHeaderOut       = metrics.NewRegisteredMeter("msg/header/out", reg)
    52  	MsgHeaderOutBytes  = metrics.NewRegisteredMeter("msg/header/out/bytes", reg)
    53  	MsgBodyIn          = metrics.NewRegisteredMeter("msg/body/in", reg)
    54  	MsgBodyInBytes     = metrics.NewRegisteredMeter("msg/body/in/bytes", reg)
    55  	MsgBodyOut         = metrics.NewRegisteredMeter("msg/body/out", reg)
    56  	MsgBodyOutBytes    = metrics.NewRegisteredMeter("msg/body/out/bytes", reg)
    57  	MsgStateIn         = metrics.NewRegisteredMeter("msg/state/in", reg)
    58  	MsgStateInBytes    = metrics.NewRegisteredMeter("msg/state/in/bytes", reg)
    59  	MsgStateOut        = metrics.NewRegisteredMeter("msg/state/out", reg)
    60  	MsgStateOutBytes   = metrics.NewRegisteredMeter("msg/state/out/bytes", reg)
    61  	MsgReceiptIn       = metrics.NewRegisteredMeter("msg/receipt/in", reg)
    62  	MsgReceiptInBytes  = metrics.NewRegisteredMeter("msg/receipt/in/bytes", reg)
    63  	MsgReceiptOut      = metrics.NewRegisteredMeter("msg/receipt/out", reg)
    64  	MsgReceiptOutBytes = metrics.NewRegisteredMeter("msg/receipt/out/bytes", reg)
    65  	MsgMiscIn          = metrics.NewRegisteredMeter("msg/misc/in", reg)
    66  	MsgMiscInBytes     = metrics.NewRegisteredMeter("msg/misc/in/bytes", reg)
    67  	MsgMiscOut         = metrics.NewRegisteredMeter("msg/misc/out", reg)
    68  	MsgMiscOutBytes    = metrics.NewRegisteredMeter("msg/misc/out/bytes", reg)
    69  )
    70  
    71  var (
    72  	DLHeaders        = metrics.NewRegisteredMeter("download/header", reg)
    73  	DLHeaderTimer    = metrics.NewRegisteredTimer("download/header", reg)
    74  	DLHeaderDrops    = metrics.NewRegisteredMeter("download/header/drop", reg)
    75  	DLHeaderTimeouts = metrics.NewRegisteredMeter("download/header/timeout", reg)
    76  
    77  	DLBodies       = metrics.NewRegisteredMeter("download/body", reg)
    78  	DLBodyTimer    = metrics.NewRegisteredTimer("download/body", reg)
    79  	DLBodyDrops    = metrics.NewRegisteredMeter("download/body/drop", reg)
    80  	DLBodyTimeouts = metrics.NewRegisteredMeter("download/body/timeout", reg)
    81  
    82  	DLReceipts        = metrics.NewRegisteredMeter("download/receipt", reg)
    83  	DLReceiptTimer    = metrics.NewRegisteredTimer("download/receipt", reg)
    84  	DLReceiptDrops    = metrics.NewRegisteredMeter("download/receipt/drop", reg)
    85  	DLReceiptTimeouts = metrics.NewRegisteredMeter("download/receipt/timeout", reg)
    86  
    87  	DLStates        = metrics.NewRegisteredMeter("download/state", reg)
    88  	DLStateTimer    = metrics.NewRegisteredTimer("download/state", reg)
    89  	DLStateDrops    = metrics.NewRegisteredMeter("download/state/drop", reg)
    90  	DLStateTimeouts = metrics.NewRegisteredMeter("download/state/timeout", reg)
    91  )
    92  
    93  var (
    94  	FetchBlocks  = metrics.NewRegisteredMeter("fetch/block", reg)
    95  	FetchHeaders = metrics.NewRegisteredMeter("fetch/header", reg)
    96  	FetchBodies  = metrics.NewRegisteredMeter("fetch/body", reg)
    97  
    98  	FetchFilterBlockIns   = metrics.NewRegisteredMeter("fetch/filter/block/in", reg)
    99  	FetchFilterBlockOuts  = metrics.NewRegisteredMeter("fetch/filter/block/out", reg)
   100  	FetchFilterHeaderIns  = metrics.NewRegisteredMeter("fetch/filter/header/in", reg)
   101  	FetchFilterHeaderOuts = metrics.NewRegisteredMeter("fetch/filter/header/out", reg)
   102  	FetchFilterBodyIns    = metrics.NewRegisteredMeter("fetch/filter/body/in", reg)
   103  	FetchFilterBodyOuts   = metrics.NewRegisteredMeter("fetch/filter/body/out", reg)
   104  
   105  	FetchAnnounces     = metrics.NewRegisteredMeter("fetch/announce", reg)
   106  	FetchAnnounceTimer = metrics.NewRegisteredTimer("fetch/announce", reg)
   107  	FetchAnnounceDrops = metrics.NewRegisteredMeter("fetch/announce/drop", reg)
   108  	FetchAnnounceDOS   = metrics.NewRegisteredMeter("fetch/announce/dos", reg)
   109  
   110  	FetchBroadcasts     = metrics.NewRegisteredMeter("fetch/broadcast", reg)
   111  	FetchBroadcastTimer = metrics.NewRegisteredTimer("fetch/broadcast", reg)
   112  	FetchBroadcastDrops = metrics.NewRegisteredMeter("fetch/broadcast/drop", reg)
   113  	FetchBroadcastDOS   = metrics.NewRegisteredMeter("fetch/broadcast/dos", reg)
   114  )
   115  
   116  var (
   117  	P2PIn       = metrics.NewRegisteredMeter("p2p/in", reg)
   118  	P2PInBytes  = metrics.NewRegisteredMeter("p2p/in/bytes", reg)
   119  	P2POut      = metrics.NewRegisteredMeter("p2p/out", reg)
   120  	P2POutBytes = metrics.NewRegisteredMeter("p2p/out/bytes", reg)
   121  )
   122  
   123  var (
   124  	MemAllocs = metrics.GetOrRegisterGauge("memory/allocs", reg)
   125  	MemFrees  = metrics.GetOrRegisterGauge("memory/frees", reg)
   126  	MemInuse  = metrics.GetOrRegisterGauge("memory/inuse", reg)
   127  	MemPauses = metrics.GetOrRegisterGauge("memory/pauses", reg)
   128  
   129  	NumGoRoutines = metrics.GetOrRegisterGauge("runtime/goroutines", reg)
   130  )
   131  
   132  // diskStats is the per process disk I/O statistics.
   133  type diskStats struct {
   134  	ReadCount  int64 // Number of read operations executed
   135  	ReadBytes  int64 // Total number of bytes read
   136  	WriteCount int64 // Number of write operations executed
   137  	WriteBytes int64 // Total number of byte written
   138  }
   139  
   140  func UpdateSysMetrics() {
   141  	var mem runtime.MemStats
   142  	runtime.ReadMemStats(&mem)
   143  	MemAllocs.Update(int64(mem.Mallocs))
   144  	MemFrees.Update(int64(mem.Frees))
   145  	MemInuse.Update(int64(mem.Alloc))
   146  	MemPauses.Update(int64(mem.PauseTotalNs))
   147  
   148  	NumGoRoutines.Update(int64(runtime.NumGoroutine()))
   149  }
   150  
   151  func CollectToJSON() ([]byte, error) {
   152  	UpdateSysMetrics()
   153  
   154  	var b bytes.Buffer
   155  	writer := bufio.NewWriter(&b)
   156  	defer writer.Flush()
   157  	metrics.WriteJSONOnce(reg, writer)
   158  
   159  	return b.Bytes(), nil
   160  }
   161  
   162  // CollectToFile writes metrics to the given file.
   163  func CollectToFile(file string) {
   164  	f, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
   165  	if err != nil {
   166  		glog.Fatal(err)
   167  	}
   168  	defer f.Close()
   169  
   170  	encoder := json.NewEncoder(bufio.NewWriter(f))
   171  
   172  	for range time.Tick(3 * time.Second) {
   173  		UpdateSysMetrics()
   174  		if err := encoder.Encode(reg); err != nil {
   175  			glog.Errorf("metrics: log to %q: %s", file, err)
   176  		}
   177  	}
   178  }