forked from kubernetes-sigs/cri-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_stats.go
257 lines (234 loc) · 6.75 KB
/
container_stats.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
252
253
254
255
256
257
/*
Copyright 2017 The Kubernetes Authors.
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 main
import (
"context"
"fmt"
"sort"
"time"
"github.com/docker/go-units"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
internalapi "k8s.io/cri-api/pkg/apis"
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
)
type statsOptions struct {
// all containers
all bool
// id of container
id string
// podID of container
podID string
// sample is the duration for sampling cpu usage.
sample time.Duration
// labels are selectors for the sandbox
labels map[string]string
// output format
output string
// live watch
watch bool
}
var statsCommand = &cli.Command{
Name: "stats",
Usage: "List container(s) resource usage statistics",
UseShortOptionHandling: true,
ArgsUsage: "[ID]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Usage: "Show all containers (default shows just running)",
},
&cli.StringFlag{
Name: "id",
Value: "",
Usage: "Filter by container id",
},
&cli.StringFlag{
Name: "pod",
Aliases: []string{"p"},
Value: "",
Usage: "Filter by pod id",
},
&cli.StringSliceFlag{
Name: "label",
Usage: "Filter by key=value label",
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Output format, One of: json|yaml|table",
},
&cli.IntFlag{
Name: "seconds",
Aliases: []string{"s"},
Value: 1,
Usage: "Sample duration for CPU usage in seconds",
},
&cli.BoolFlag{
Name: "watch",
Aliases: []string{"w"},
Usage: "Watch pod resources",
},
},
Action: func(context *cli.Context) error {
runtimeClient, err := getRuntimeService(context, 0)
if err != nil {
return err
}
id := context.String("id")
if id == "" && context.NArg() > 0 {
id = context.Args().Get(0)
}
opts := statsOptions{
all: context.Bool("all"),
id: id,
podID: context.String("pod"),
sample: time.Duration(context.Int("seconds")) * time.Second,
output: context.String("output"),
watch: context.Bool("watch"),
}
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
if err != nil {
return err
}
if err = ContainerStats(runtimeClient, opts); err != nil {
return fmt.Errorf("get container stats: %w", err)
}
return nil
},
}
type containerStatsByID []*pb.ContainerStats
func (c containerStatsByID) Len() int { return len(c) }
func (c containerStatsByID) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c containerStatsByID) Less(i, j int) bool {
return c[i].Attributes.Id < c[j].Attributes.Id
}
// ContainerStats sends a ListContainerStatsRequest to the server, and
// parses the returned ListContainerStatsResponse.
func ContainerStats(client internalapi.RuntimeService, opts statsOptions) error {
filter := &pb.ContainerStatsFilter{}
if opts.id != "" {
filter.Id = opts.id
}
if opts.podID != "" {
filter.PodSandboxId = opts.podID
}
if opts.labels != nil {
filter.LabelSelector = opts.labels
}
request := &pb.ListContainerStatsRequest{
Filter: filter,
}
display := newTableDisplay(20, 1, 3, ' ', 0)
if !opts.watch {
if err := displayStats(context.TODO(), client, request, display, opts); err != nil {
return err
}
} else {
displayErrCh := make(chan error, 1)
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
watchCtx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
// Put the displayStats in another goroutine.
// because it might be time consuming with lots of containers.
// and we want to cancel it ASAP when user hit CtrlC
go func() {
for range ticker.C {
if err := displayStats(watchCtx, client, request, display, opts); err != nil {
displayErrCh <- err
break
}
}
}()
// listen for CtrlC or error
select {
case <-SetupInterruptSignalHandler():
cancelFn()
return nil
case err := <-displayErrCh:
return err
}
}
return nil
}
func getContainerStats(ctx context.Context, client internalapi.RuntimeService, request *pb.ListContainerStatsRequest) (*pb.ListContainerStatsResponse, error) {
logrus.Debugf("ListContainerStatsRequest: %v", request)
r, err := client.ListContainerStats(context.TODO(), request.Filter)
logrus.Debugf("ListContainerResponse: %v", r)
if err != nil {
return nil, err
}
sort.Sort(containerStatsByID(r))
return &pb.ListContainerStatsResponse{Stats: r}, nil
}
func displayStats(ctx context.Context, client internalapi.RuntimeService, request *pb.ListContainerStatsRequest, display *display, opts statsOptions) error {
r, err := getContainerStats(ctx, client, request)
if err != nil {
return err
}
switch opts.output {
case "json":
return outputProtobufObjAsJSON(r)
case "yaml":
return outputProtobufObjAsYAML(r)
}
oldStats := make(map[string]*pb.ContainerStats)
for _, s := range r.GetStats() {
if ctx.Err() != nil {
return ctx.Err()
}
oldStats[s.Attributes.Id] = s
}
time.Sleep(opts.sample)
r, err = getContainerStats(ctx, client, request)
if err != nil {
return err
}
display.AddRow([]string{columnContainer, columnName, columnCPU, columnMemory, columnDisk, columnInodes})
for _, s := range r.GetStats() {
if ctx.Err() != nil {
return ctx.Err()
}
id := getTruncatedID(s.Attributes.Id, "")
name := s.GetAttributes().GetMetadata().GetName()
cpu := s.GetCpu().GetUsageCoreNanoSeconds().GetValue()
mem := s.GetMemory().GetWorkingSetBytes().GetValue()
disk := s.GetWritableLayer().GetUsedBytes().GetValue()
inodes := s.GetWritableLayer().GetInodesUsed().GetValue()
if !opts.all && cpu == 0 && mem == 0 {
// Skip non-running container
continue
}
old, ok := oldStats[s.Attributes.Id]
if !ok {
// Skip new container
continue
}
var cpuPerc float64
if cpu != 0 {
// Only generate cpuPerc for running container
duration := s.GetCpu().GetTimestamp() - old.GetCpu().GetTimestamp()
if duration == 0 {
return fmt.Errorf("cpu stat is not updated during sample")
}
cpuPerc = float64(cpu-old.GetCpu().GetUsageCoreNanoSeconds().GetValue()) / float64(duration) * 100
}
display.AddRow([]string{id, name, fmt.Sprintf("%.2f", cpuPerc), units.HumanSize(float64(mem)),
units.HumanSize(float64(disk)), fmt.Sprintf("%d", inodes)})
}
display.ClearScreen()
display.Flush()
return nil
}