OSDN Git Service

add cpu log
authorpaladz <453256728@qq.com>
Fri, 26 Oct 2018 09:55:03 +0000 (17:55 +0800)
committerpaladz <453256728@qq.com>
Fri, 26 Oct 2018 09:55:03 +0000 (17:55 +0800)
cmd/bytomd/commands/run_node.go
database/leveldb/store.go
p2p/switch.go
protocol/validation/block.go

index 0b1f369..8ea64f2 100644 (file)
@@ -1,13 +1,12 @@
 package commands
 
 import (
-       "fmt"
+       "strings"
+       "time"
 
        log "github.com/sirupsen/logrus"
        "github.com/spf13/cobra"
 
-       "strings"
-
        "github.com/bytom/node"
 )
 
@@ -48,37 +47,41 @@ func init() {
        RootCmd.AddCommand(runNodeCmd)
 }
 
-func getLogLevel(level string) log.Level {
+func setLogLevel(level string) {
        switch strings.ToLower(level) {
        case "debug":
-               return log.DebugLevel
+               log.SetLevel(log.DebugLevel)
        case "info":
-               return log.InfoLevel
+               log.SetLevel(log.InfoLevel)
        case "warn":
-               return log.WarnLevel
+               log.SetLevel(log.WarnLevel)
        case "error":
-               return log.ErrorLevel
+               log.SetLevel(log.ErrorLevel)
        case "fatal":
-               return log.FatalLevel
+               log.SetLevel(log.FatalLevel)
        default:
-               return log.InfoLevel
+               log.SetLevel(log.InfoLevel)
        }
 }
 
 func runNode(cmd *cobra.Command, args []string) error {
-       // Set log level by config.LogLevel
-       log.SetLevel(getLogLevel(config.LogLevel))
+       startTime := time.Now()
+       setLogLevel(config.LogLevel)
 
        // Create & start node
        n := node.NewNode(config)
        if _, err := n.Start(); err != nil {
-               return fmt.Errorf("Failed to start node: %v", err)
-       } else {
-               log.Info("Start node ", n.SyncManager().NodeInfo())
+               log.WithField("err", err).Fatal("failed to start node")
        }
 
+       nodeInfo := n.SyncManager().NodeInfo()
+       log.WithFields(log.Fields{
+               "version":  nodeInfo.Version,
+               "network":  nodeInfo.Network,
+               "duration": time.Since(startTime),
+       }).Info("start node complete")
+
        // Trap signal, run forever.
        n.RunForever()
-
        return nil
 }
index e3dd9e5..fab9b1c 100644 (file)
@@ -3,6 +3,7 @@ package leveldb
 import (
        "encoding/binary"
        "encoding/json"
+       "time"
 
        "github.com/golang/protobuf/proto"
        log "github.com/sirupsen/logrus"
@@ -17,6 +18,8 @@ import (
        "github.com/bytom/protocol/state"
 )
 
+const logModule = "leveldb"
+
 var (
        blockStoreKey     = []byte("blockStore")
        blockPrefix       = []byte("B:")
@@ -123,6 +126,7 @@ func (s *Store) GetStoreStatus() *protocol.BlockStoreState {
 }
 
 func (s *Store) LoadBlockIndex(stateBestHeight uint64) (*state.BlockIndex, error) {
+       startTime := time.Now()
        blockIndex := state.NewBlockIndex()
        bhIter := s.db.IteratorPrefix(blockHeaderPrefix)
        defer bhIter.Release()
@@ -133,7 +137,7 @@ func (s *Store) LoadBlockIndex(stateBestHeight uint64) (*state.BlockIndex, error
                if err := bh.UnmarshalText(bhIter.Value()); err != nil {
                        return nil, err
                }
-               
+
                // If a block with a height greater than the best height of state is added to the index,
                // It may cause a bug that the new block cant not be process properly.
                if bh.Height > stateBestHeight {
@@ -156,11 +160,17 @@ func (s *Store) LoadBlockIndex(stateBestHeight uint64) (*state.BlockIndex, error
                lastNode = node
        }
 
+       log.WithFields(log.Fields{
+               "module":   logModule,
+               "height":   stateBestHeight,
+               "duration": time.Since(startTime),
+       }).Debug("initialize load history block index from database")
        return blockIndex, nil
 }
 
 // SaveBlock persists a new block in the protocol.
 func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error {
+       startTime := time.Now()
        binaryBlock, err := block.MarshalText()
        if err != nil {
                return errors.Wrap(err, "Marshal block meta")
@@ -183,7 +193,12 @@ func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error {
        batch.Set(calcTxStatusKey(&blockHash), binaryTxStatus)
        batch.Write()
 
-       log.WithFields(log.Fields{"height": block.Height, "hash": blockHash.String()}).Info("block saved on disk")
+       log.WithFields(log.Fields{
+               "module":   logModule,
+               "height":   block.Height,
+               "hash":     blockHash.String(),
+               "duration": time.Since(startTime),
+       }).Info("block saved on disk")
        return nil
 }
 
index f2afbd0..4e747a4 100644 (file)
@@ -24,7 +24,7 @@ import (
 const (
        bannedPeerKey       = "BannedPeer"
        defaultBanDuration  = time.Hour * 1
-       minNumOutboundPeers = 5
+       minNumOutboundPeers = 3
 )
 
 //pre-define errors for connecting fail
index 8f81737..8b5fefe 100644 (file)
@@ -3,6 +3,8 @@ package validation
 import (
        "time"
 
+       log "github.com/sirupsen/logrus"
+
        "github.com/bytom/consensus"
        "github.com/bytom/consensus/difficulty"
        "github.com/bytom/errors"
@@ -11,6 +13,8 @@ import (
        "github.com/bytom/protocol/state"
 )
 
+const logModule = "leveldb"
+
 var (
        errBadTimestamp          = errors.New("block timestamp is not in the valid range")
        errBadBits               = errors.New("block bits is invalid")
@@ -75,6 +79,7 @@ func ValidateBlockHeader(b *bc.Block, parent *state.BlockNode) error {
 
 // ValidateBlock validates a block and the transactions within.
 func ValidateBlock(b *bc.Block, parent *state.BlockNode) error {
+       startTime := time.Now()
        if err := ValidateBlockHeader(b, parent); err != nil {
                return err
        }
@@ -117,5 +122,12 @@ func ValidateBlock(b *bc.Block, parent *state.BlockNode) error {
        if txStatusHash != *b.TransactionStatusHash {
                return errors.WithDetailf(errMismatchedMerkleRoot, "transaction status merkle root")
        }
+
+       log.WithFields(log.Fields{
+               "module":   logModule,
+               "height":   b.Height,
+               "hash":     b.ID.String(),
+               "duration": time.Since(startTime),
+       }).Debug("finish validate block")
        return nil
 }