forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
160 lines (134 loc) · 4.15 KB
/
queue.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
// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package stats
import (
"fmt"
"math"
"sync"
"github.com/aws/amazon-ecs-agent/agent/tcs/model/ecstcs"
)
const (
// BytesInMiB is the number of bytes in a MebiByte.
BytesInMiB = 1024 * 1024
)
// Queue abstracts a queue using UsageStats slice.
type Queue struct {
buffer []UsageStats
maxSize int
bufferLock sync.RWMutex
}
// NewQueue creates a queue.
func NewQueue(maxSize int) *Queue {
return &Queue{
buffer: make([]UsageStats, 0, maxSize),
maxSize: maxSize,
}
}
// Reset resets the stats queue.
func (queue *Queue) Reset() {
queue.buffer = queue.buffer[:0]
}
// Add adds a new set of container stats to the queue.
func (queue *Queue) Add(rawStat *ContainerStats) {
queue.bufferLock.Lock()
defer queue.bufferLock.Unlock()
queueLength := len(queue.buffer)
stat := UsageStats{
CPUUsagePerc: float32(nan32()),
MemoryUsageInMegs: uint32(rawStat.memoryUsage / BytesInMiB),
Timestamp: rawStat.timestamp,
cpuUsage: rawStat.cpuUsage,
}
if queueLength != 0 {
// % utilization can be calculated only when queue is non-empty.
lastStat := queue.buffer[queueLength-1]
stat.CPUUsagePerc = 100 * float32(rawStat.cpuUsage-lastStat.cpuUsage) / float32(rawStat.timestamp.Sub(lastStat.Timestamp).Nanoseconds())
if queue.maxSize == queueLength {
// Remove first element if queue is full.
queue.buffer = queue.buffer[1:queueLength]
}
}
queue.buffer = append(queue.buffer, stat)
}
// GetCPUStatsSet gets the stats set for CPU utilization.
func (queue *Queue) GetCPUStatsSet() (*ecstcs.CWStatsSet, error) {
return queue.getCWStatsSet(getCPUUsagePerc)
}
// GetMemoryStatsSet gets the stats set for memory utilization.
func (queue *Queue) GetMemoryStatsSet() (*ecstcs.CWStatsSet, error) {
return queue.getCWStatsSet(getMemoryUsagePerc)
}
// GetRawUsageStats gets the array of most recent raw UsageStats, in descending
// order of timestamps.
func (queue *Queue) GetRawUsageStats(numStats int) ([]UsageStats, error) {
queue.bufferLock.Lock()
defer queue.bufferLock.Unlock()
queueLength := len(queue.buffer)
if queueLength == 0 {
return nil, fmt.Errorf("No data in the queue")
}
if numStats > queueLength {
numStats = queueLength
}
usageStats := make([]UsageStats, numStats)
for i := 0; i < numStats; i++ {
// Order such that usageStats[i].timestamp > usageStats[i+1].timestamp
rawUsageStat := queue.buffer[queueLength-i-1]
usageStats[i] = UsageStats{
CPUUsagePerc: rawUsageStat.CPUUsagePerc,
MemoryUsageInMegs: rawUsageStat.MemoryUsageInMegs,
Timestamp: rawUsageStat.Timestamp,
}
}
return usageStats, nil
}
func getCPUUsagePerc(s *UsageStats) float64 {
return float64(s.CPUUsagePerc)
}
func getMemoryUsagePerc(s *UsageStats) float64 {
return float64(s.MemoryUsageInMegs)
}
type getUsageFunc func(*UsageStats) float64
// getCWStatsSet gets the stats set for either CPU or Memory based on the
// function pointer.
func (queue *Queue) getCWStatsSet(f getUsageFunc) (*ecstcs.CWStatsSet, error) {
queue.bufferLock.Lock()
defer queue.bufferLock.Unlock()
queueLength := len(queue.buffer)
if queueLength < 2 {
// Need at least 2 data points to calculate this.
return nil, fmt.Errorf("No data in the queue")
}
var min, max, sum float64
var sampleCount int64
min = math.MaxFloat64
max = -math.MaxFloat64
sum = 0
sampleCount = 0
for _, stat := range queue.buffer {
perc := f(&stat)
if math.IsNaN(perc) {
continue
}
min = math.Min(min, perc)
max = math.Max(max, perc)
sampleCount++
sum += perc
}
return &ecstcs.CWStatsSet{
Max: &max,
Min: &min,
SampleCount: &sampleCount,
Sum: &sum,
}, nil
}