-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.go
More file actions
147 lines (113 loc) · 3.45 KB
/
Copy pathmetrics.go
File metadata and controls
147 lines (113 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright © 2018 J. Strobus White.
// This file is part of the blocktop blockchain development kit.
//
// Blocktop is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blocktop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with blocktop. If not, see <http://www.gnu.org/licenses/>.
package consensus
import (
"sync"
"time"
spec "github.com/blocktop/go-spec"
mtr "github.com/rcrowley/go-metrics"
)
type metricItems struct {
BlockAddTime mtr.Histogram
BlockConfirmTime mtr.Histogram
BlockDepthExit mtr.Histogram
BlockDepthEntry mtr.Histogram
TotalBlocks mtr.Counter
ActiveBlocks mtr.Counter
UpdatedTimestamp int64
MaxBlockNumber uint64
statBlocks map[string]*statBlock
registry mtr.Registry
sync.Mutex
}
type statBlock struct {
block spec.Block
timeEntered int64
disqualifed bool
}
var metrics *metricItems = &metricItems{}
func init() {
m := metrics
registry := mtr.NewPrefixedRegistry("consensus - ")
m.registry = registry
m.BlockAddTime = mtr.GetOrRegisterHistogram("block add time", registry, mtr.NewUniformSample(500))
m.BlockConfirmTime = mtr.GetOrRegisterHistogram("block add time", registry, mtr.NewUniformSample(500))
m.BlockDepthExit = mtr.GetOrRegisterHistogram("block depth at exit", registry, mtr.NewUniformSample(500))
m.BlockDepthEntry = mtr.GetOrRegisterHistogram("block depth at entry", registry, mtr.NewUniformSample(500))
m.TotalBlocks = mtr.GetOrRegisterCounter("total blocks", registry)
m.ActiveBlocks = mtr.GetOrRegisterCounter("active blocks", registry)
m.statBlocks = make(map[string]*statBlock)
m.updateTimestamp()
}
func (m *metricItems) updateTimestamp() {
m.UpdatedTimestamp = time.Now().UnixNano() / int64(time.Millisecond)
}
func (m *metricItems) AddBlock(b spec.Block) {
blockID := b.Hash()
blockNumber := b.BlockNumber()
if blockNumber > m.MaxBlockNumber {
m.MaxBlockNumber = blockNumber
}
depth := int64(m.MaxBlockNumber - blockNumber)
m.Lock()
defer m.Unlock()
if m.statBlocks[blockID] != nil {
return
}
m.statBlocks[blockID] = &statBlock{
block: b,
timeEntered: time.Now().UnixNano(),
disqualifed: false}
m.ActiveBlocks.Inc(1)
m.TotalBlocks.Inc(1)
m.BlockDepthEntry.Update(depth)
m.updateTimestamp()
tree.add(b)
}
func (m *metricItems) BlockAddDuration(duration int64) {
m.Lock()
defer m.Unlock()
m.BlockAddTime.Update(duration)
}
func (m *metricItems) DisqualifyBlock(b spec.Block) {
blockID := b.Hash()
m.Lock()
defer m.Unlock()
statB := m.statBlocks[blockID]
if statB == nil || statB.disqualifed {
return
}
m.updateTimestamp()
tree.disqualify(b)
}
func (m *metricItems) RemoveBlock(b spec.Block) {
blockID := b.Hash()
m.Lock()
defer m.Unlock()
statB := m.statBlocks[blockID]
if statB == nil {
return
}
m.ActiveBlocks.Dec(1)
if !statB.disqualifed {
duration := time.Now().UnixNano() - statB.timeEntered
depth := m.MaxBlockNumber - b.BlockNumber()
m.BlockDepthExit.Update(int64(depth))
m.BlockConfirmTime.Update(duration)
}
delete(m.statBlocks, blockID)
tree.remove(b)
}