-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathstdout.go
251 lines (217 loc) · 8.61 KB
/
stdout.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2015 Google Inc. 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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 stdout
import (
"bytes"
"fmt"
"strconv"
"time"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/storage"
)
func init() {
storage.RegisterStorageDriver("stdout", new)
}
type stdoutStorage struct {
Namespace string
}
const (
serTimestamp string = "timestamp"
// Cumulative CPU usage
// To be deprecated in 0.39
// https://github.com/google/cadvisor/issues/2637
colCPUCumulativeUsage string = "cpu_cumulative_usage"
// Cumulative CPU usage
serCPUUsageTotal string = "cpu_usage_total"
serCPUUsageSystem string = "cpu_usage_system"
serCPUUsageUser string = "cpu_usage_user"
serCPUUsagePerCPU string = "cpu_usage_per_cpu"
// Smoothed average of number of runnable threads x 1000.
serLoadAverage string = "load_average"
// Memory Usage
serMemoryUsage string = "memory_usage"
// Maximum memory usage recorded
serMemoryMaxUsage string = "memory_max_usage"
// Number of bytes of page cache memory
serMemoryCache string = "memory_cache"
// Size of RSS
serMemoryRss string = "memory_rss"
// Container swap usage
serMemorySwap string = "memory_swap"
// Size of memory mapped files in bytes
serMemoryMappedFile string = "memory_mapped_file"
// Size of socket memory in bytes
serMemorySocket string = "memory_socket"
// Working set size
serMemoryWorkingSet string = "memory_working_set"
// Total active file
serMemoryTotalActiveFile string = "memory_total_active_file"
// Total inactive file
serMemoryTotalInactiveFile string = "memory_total_inactive_file"
// Number of memory usage hits limits
serMemoryFailcnt string = "memory_failcnt"
// Cumulative count of memory allocation failures
serMemoryFailure string = "memory_failure"
// Cumulative count of bytes received.
serRxBytes string = "rx_bytes"
// Cumulative count of receive errors encountered.
serRxErrors string = "rx_errors"
// Cumulative count of bytes transmitted.
serTxBytes string = "tx_bytes"
// Cumulative count of transmit errors encountered.
serTxErrors string = "tx_errors"
// Filesystem summary
serFsSummary string = "fs_summary"
// Filesystem limit.
serFsLimit string = "fs_limit"
// Filesystem usage.
serFsUsage string = "fs_usage"
// Hugetlb stat - current res_counter usage for hugetlb
setHugetlbUsage string = "hugetlb_usage"
// Hugetlb stat - maximum usage ever recorded
setHugetlbMaxUsage string = "hugetlb_max_usage"
// Hugetlb stat - number of times hugetlb usage allocation failure
setHugetlbFailcnt string = "hugetlb_failcnt"
// Perf statistics
serPerfStat string = "perf_stat"
// Referenced memory
serReferencedMemory string = "referenced_memory"
// Resctrl - Total memory bandwidth
serResctrlMemoryBandwidthTotal string = "resctrl_memory_bandwidth_total"
// Resctrl - Local memory bandwidth
serResctrlMemoryBandwidthLocal string = "resctrl_memory_bandwidth_local"
// Resctrl - Last level cache usage
serResctrlLLCOccupancy string = "resctrl_llc_occupancy"
)
func new() (storage.StorageDriver, error) {
return newStorage(*storage.ArgDbHost)
}
func (driver *stdoutStorage) containerStatsToValues(stats *info.ContainerStats) (series map[string]uint64) {
series = make(map[string]uint64)
// Unix Timestamp
series[serTimestamp] = uint64(time.Now().UnixNano())
// Total usage in nanoseconds
series[serCPUUsageTotal] = stats.Cpu.Usage.Total
// To be deprecated in 0.39
series[colCPUCumulativeUsage] = series[serCPUUsageTotal]
// CPU usage: Time spend in system space (in nanoseconds)
series[serCPUUsageSystem] = stats.Cpu.Usage.System
// CPU usage: Time spent in user space (in nanoseconds)
series[serCPUUsageUser] = stats.Cpu.Usage.User
// CPU usage per CPU
for i := 0; i < len(stats.Cpu.Usage.PerCpu); i++ {
series[serCPUUsagePerCPU+"."+strconv.Itoa(i)] = stats.Cpu.Usage.PerCpu[i]
}
// Load Average
series[serLoadAverage] = uint64(stats.Cpu.LoadAverage)
// Network stats.
series[serRxBytes] = stats.Network.RxBytes
series[serRxErrors] = stats.Network.RxErrors
series[serTxBytes] = stats.Network.TxBytes
series[serTxErrors] = stats.Network.TxErrors
// Referenced Memory
series[serReferencedMemory] = stats.ReferencedMemory
return series
}
func (driver *stdoutStorage) containerFsStatsToValues(series *map[string]uint64, stats *info.ContainerStats) {
for _, fsStat := range stats.Filesystem {
// Summary stats.
(*series)[serFsSummary+"."+serFsLimit] += fsStat.Limit
(*series)[serFsSummary+"."+serFsUsage] += fsStat.Usage
// Per device stats.
(*series)[fsStat.Device+"."+serFsLimit] = fsStat.Limit
(*series)[fsStat.Device+"."+serFsUsage] = fsStat.Usage
}
}
func (driver *stdoutStorage) memoryStatsToValues(series *map[string]uint64, stats *info.ContainerStats) {
// Memory Usage
(*series)[serMemoryUsage] = stats.Memory.Usage
// Maximum memory usage recorded
(*series)[serMemoryMaxUsage] = stats.Memory.MaxUsage
//Number of bytes of page cache memory
(*series)[serMemoryCache] = stats.Memory.Cache
// Size of RSS
(*series)[serMemoryRss] = stats.Memory.RSS
// Container swap usage
(*series)[serMemorySwap] = stats.Memory.Swap
// Size of memory mapped files in bytes
(*series)[serMemoryMappedFile] = stats.Memory.MappedFile
// Size of socket memory in bytes
(*series)[serMemorySocket] = stats.Memory.Socket
// Working Set Size
(*series)[serMemoryWorkingSet] = stats.Memory.WorkingSet
// Total Active File
(*series)[serMemoryTotalActiveFile] = stats.Memory.TotalActiveFile
// Total Inactive File
(*series)[serMemoryTotalInactiveFile] = stats.Memory.TotalInactiveFile
// Number of memory usage hits limits
(*series)[serMemoryFailcnt] = stats.Memory.Failcnt
// Cumulative count of memory allocation failures
(*series)[serMemoryFailure+".container.pgfault"] = stats.Memory.ContainerData.Pgfault
(*series)[serMemoryFailure+".container.pgmajfault"] = stats.Memory.ContainerData.Pgmajfault
(*series)[serMemoryFailure+".hierarchical.pgfault"] = stats.Memory.HierarchicalData.Pgfault
(*series)[serMemoryFailure+".hierarchical.pgmajfault"] = stats.Memory.HierarchicalData.Pgmajfault
}
func (driver *stdoutStorage) hugetlbStatsToValues(series *map[string]uint64, stats *info.ContainerStats) {
for pageSize, hugetlbStat := range stats.Hugetlb {
(*series)[setHugetlbUsage+"."+pageSize] = hugetlbStat.Usage
(*series)[setHugetlbMaxUsage+"."+pageSize] = hugetlbStat.MaxUsage
(*series)[setHugetlbFailcnt+"."+pageSize] = hugetlbStat.Failcnt
}
}
func (driver *stdoutStorage) perfStatsToValues(series *map[string]uint64, stats *info.ContainerStats) {
for _, perfStat := range stats.PerfStats {
(*series)[serPerfStat+"."+perfStat.Name+"."+strconv.Itoa(perfStat.Cpu)] = perfStat.Value
}
}
func (driver *stdoutStorage) resctrlStatsToValues(series *map[string]uint64, stats *info.ContainerStats) {
for nodeID, rdtMemoryBandwidth := range stats.Resctrl.MemoryBandwidth {
(*series)[serResctrlMemoryBandwidthTotal+"."+strconv.Itoa(nodeID)] = rdtMemoryBandwidth.TotalBytes
(*series)[serResctrlMemoryBandwidthLocal+"."+strconv.Itoa(nodeID)] = rdtMemoryBandwidth.LocalBytes
}
for nodeID, rdtCache := range stats.Resctrl.Cache {
(*series)[serResctrlLLCOccupancy+"."+strconv.Itoa(nodeID)] = rdtCache.LLCOccupancy
}
}
func (driver *stdoutStorage) AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error {
if stats == nil {
return nil
}
containerName := cInfo.ContainerReference.Name
if len(cInfo.ContainerReference.Aliases) > 0 {
containerName = cInfo.ContainerReference.Aliases[0]
}
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("cName=%s host=%s", containerName, driver.Namespace))
series := driver.containerStatsToValues(stats)
driver.containerFsStatsToValues(&series, stats)
driver.memoryStatsToValues(&series, stats)
driver.hugetlbStatsToValues(&series, stats)
driver.perfStatsToValues(&series, stats)
driver.resctrlStatsToValues(&series, stats)
for key, value := range series {
buffer.WriteString(fmt.Sprintf(" %s=%v", key, value))
}
_, err := fmt.Println(buffer.String())
return err
}
func (driver *stdoutStorage) Close() error {
return nil
}
func newStorage(namespace string) (*stdoutStorage, error) {
stdoutStorage := &stdoutStorage{
Namespace: namespace,
}
return stdoutStorage, nil
}