/
last-minute.go
208 lines (184 loc) · 4.85 KB
/
last-minute.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) 2015-2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//go:generate msgp -file=$GOFILE -unexported
package cmd
import (
"time"
"github.com/minio/madmin-go/v3"
)
const (
sizeLessThan1KiB = iota
sizeLessThan1MiB
sizeLessThan10MiB
sizeLessThan100MiB
sizeLessThan1GiB
sizeGreaterThan1GiB
// Add new entries here
sizeLastElemMarker
)
// sizeToTag converts a size to a tag.
func sizeToTag(size int64) int {
switch {
case size < 1024:
return sizeLessThan1KiB
case size < 1024*1024:
return sizeLessThan1MiB
case size < 10*1024*1024:
return sizeLessThan10MiB
case size < 100*1024*1024:
return sizeLessThan100MiB
case size < 1024*1024*1024:
return sizeLessThan1GiB
default:
return sizeGreaterThan1GiB
}
}
func sizeTagToString(tag int) string {
switch tag {
case sizeLessThan1KiB:
return "LESS_THAN_1_KiB"
case sizeLessThan1MiB:
return "LESS_THAN_1_MiB"
case sizeLessThan10MiB:
return "LESS_THAN_10_MiB"
case sizeLessThan100MiB:
return "LESS_THAN_100_MiB"
case sizeLessThan1GiB:
return "LESS_THAN_1_GiB"
case sizeGreaterThan1GiB:
return "GREATER_THAN_1_GiB"
default:
return "unknown"
}
}
// AccElem holds information for calculating an average value
type AccElem struct {
Total int64
Size int64
N int64
}
// Add a duration to a single element.
func (a *AccElem) add(dur time.Duration) {
if dur < 0 {
dur = 0
}
a.Total += int64(dur)
a.N++
}
// Merge b into a.
func (a *AccElem) merge(b AccElem) {
a.N += b.N
a.Total += b.Total
a.Size += b.Size
}
// Avg returns average time spent.
func (a AccElem) avg() time.Duration {
if a.N >= 1 && a.Total > 0 {
return time.Duration(a.Total / a.N)
}
return 0
}
// asTimedAction returns the element as a madmin.TimedAction.
func (a AccElem) asTimedAction() madmin.TimedAction {
return madmin.TimedAction{AccTime: uint64(a.Total), Count: uint64(a.N), Bytes: uint64(a.Size)}
}
// lastMinuteLatency keeps track of last minute latency.
type lastMinuteLatency struct {
Totals [60]AccElem
LastSec int64
}
// Merge data of two lastMinuteLatency structure
func (l lastMinuteLatency) merge(o lastMinuteLatency) (merged lastMinuteLatency) {
if l.LastSec > o.LastSec {
o.forwardTo(l.LastSec)
merged.LastSec = l.LastSec
} else {
l.forwardTo(o.LastSec)
merged.LastSec = o.LastSec
}
for i := range merged.Totals {
merged.Totals[i] = AccElem{
Total: l.Totals[i].Total + o.Totals[i].Total,
N: l.Totals[i].N + o.Totals[i].N,
Size: l.Totals[i].Size + o.Totals[i].Size,
}
}
return merged
}
// Add a new duration data
func (l *lastMinuteLatency) add(t time.Duration) {
sec := time.Now().Unix()
l.forwardTo(sec)
winIdx := sec % 60
l.Totals[winIdx].add(t)
l.LastSec = sec
}
// Add a new duration data
func (l *lastMinuteLatency) addAll(sec int64, a AccElem) {
l.forwardTo(sec)
winIdx := sec % 60
l.Totals[winIdx].merge(a)
l.LastSec = sec
}
// Merge all recorded latencies of last minute into one
func (l *lastMinuteLatency) getTotal() AccElem {
var res AccElem
sec := time.Now().Unix()
l.forwardTo(sec)
for _, elem := range l.Totals[:] {
res.merge(elem)
}
return res
}
// forwardTo time t, clearing any entries in between.
func (l *lastMinuteLatency) forwardTo(t int64) {
if l.LastSec >= t {
return
}
if t-l.LastSec >= 60 {
l.Totals = [60]AccElem{}
return
}
for l.LastSec != t {
// Clear next element.
idx := (l.LastSec + 1) % 60
l.Totals[idx] = AccElem{}
l.LastSec++
}
}
// LastMinuteHistogram keeps track of last minute sizes added.
type LastMinuteHistogram [sizeLastElemMarker]lastMinuteLatency
// Merge safely merges two LastMinuteHistogram structures into one
func (l LastMinuteHistogram) Merge(o LastMinuteHistogram) (merged LastMinuteHistogram) {
for i := range l {
merged[i] = l[i].merge(o[i])
}
return merged
}
// Add latency t from object with the specified size.
func (l *LastMinuteHistogram) Add(size int64, t time.Duration) {
l[sizeToTag(size)].add(t)
}
// GetAvgData will return the average for each bucket from the last time minute.
// The number of objects is also included.
func (l *LastMinuteHistogram) GetAvgData() [sizeLastElemMarker]AccElem {
var res [sizeLastElemMarker]AccElem
for i, elem := range l[:] {
res[i] = elem.getTotal()
}
return res
}