X Tutup
Skip to content

Commit 8f870c2

Browse files
committed
support cgroup2
* only shim v2 runc v2 ("io.containerd.runc.v2") is supported * only PID metrics is implemented. Others should be implemented in separate PRs. * lots of code duplication in v1 metrics and v2 metrics. Dedupe should be separate PR. Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent f01665a commit 8f870c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+10619
-160
lines changed

cmd/containerd/builtins_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
_ "github.com/containerd/aufs"
2121
_ "github.com/containerd/containerd/metrics/cgroups"
22+
_ "github.com/containerd/containerd/metrics/cgroups/v2"
2223
_ "github.com/containerd/containerd/runtime/v1/linux"
2324
_ "github.com/containerd/containerd/runtime/v2"
2425
_ "github.com/containerd/containerd/runtime/v2/runc/options"

cmd/ctr/commands/tasks/metrics.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
wstats "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats"
2929
v1 "github.com/containerd/cgroups/stats/v1"
30+
v2 "github.com/containerd/cgroups/v2/stats"
3031
"github.com/containerd/containerd/cmd/ctr/commands"
3132
"github.com/containerd/typeurl"
3233
"github.com/urfave/cli"
@@ -80,11 +81,14 @@ var metricsCommand = cli.Command{
8081
}
8182
var (
8283
data *v1.Metrics
84+
data2 *v2.Metrics
8385
windowsStats *wstats.Statistics
8486
)
8587
switch v := anydata.(type) {
8688
case *v1.Metrics:
8789
data = v
90+
case *v2.Metrics:
91+
data2 = v
8892
case *wstats.Statistics:
8993
windowsStats = v
9094
default:
@@ -98,6 +102,8 @@ var metricsCommand = cli.Command{
98102
fmt.Fprintf(w, "%s\t%s\t\n\n", metric.ID, metric.Timestamp)
99103
if data != nil {
100104
printCgroupMetricsTable(w, data)
105+
} else if data2 != nil {
106+
printCgroup2MetricsTable(w, data2)
101107
} else {
102108
if windowsStats.GetLinux() != nil {
103109
printCgroupMetricsTable(w, windowsStats.GetLinux())
@@ -111,7 +117,7 @@ var metricsCommand = cli.Command{
111117
}
112118
return w.Flush()
113119
case formatJSON:
114-
marshaledJSON, err := json.MarshalIndent(data, "", " ")
120+
marshaledJSON, err := json.MarshalIndent(anydata, "", " ")
115121
if err != nil {
116122
return err
117123
}
@@ -140,6 +146,14 @@ func printCgroupMetricsTable(w *tabwriter.Writer, data *v1.Metrics) {
140146
}
141147
}
142148

149+
func printCgroup2MetricsTable(w *tabwriter.Writer, data *v2.Metrics) {
150+
fmt.Fprintf(w, "METRIC\tVALUE\t\n")
151+
if data.Pids != nil {
152+
fmt.Fprintf(w, "pids.current\t%v\t\n", data.Pids.Current)
153+
fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit)
154+
}
155+
}
156+
143157
func printWindowsContainerStatistics(w *tabwriter.Writer, stats *wstats.WindowsContainerStatistics) {
144158
fmt.Fprintf(w, "METRIC\tVALUE\t\n")
145159
fmt.Fprintf(w, "timestamp\t%s\t\n", stats.Timestamp)

metrics/cgroups/v2/cgroups.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// +build linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package v2
20+
21+
import (
22+
"context"
23+
24+
"github.com/containerd/containerd/errdefs"
25+
"github.com/containerd/containerd/events"
26+
"github.com/containerd/containerd/platforms"
27+
"github.com/containerd/containerd/plugin"
28+
"github.com/containerd/containerd/runtime"
29+
"github.com/containerd/containerd/runtime/v1/linux"
30+
metrics "github.com/docker/go-metrics"
31+
)
32+
33+
// Config for the cgroups monitor
34+
type Config struct {
35+
NoPrometheus bool `toml:"no_prometheus"`
36+
}
37+
38+
func init() {
39+
plugin.Register(&plugin.Registration{
40+
Type: plugin.TaskMonitorPlugin,
41+
ID: "cgroups-v2",
42+
InitFn: New,
43+
Config: &Config{},
44+
})
45+
}
46+
47+
// New returns a new cgroups monitor
48+
func New(ic *plugin.InitContext) (interface{}, error) {
49+
var ns *metrics.Namespace
50+
config := ic.Config.(*Config)
51+
if !config.NoPrometheus {
52+
ns = metrics.NewNamespace("container", "", nil)
53+
}
54+
collector := newCollector(ns)
55+
if ns != nil {
56+
metrics.Register(ns)
57+
}
58+
ic.Meta.Platforms = append(ic.Meta.Platforms, platforms.DefaultSpec())
59+
return &cgroupsMonitor{
60+
collector: collector,
61+
context: ic.Context,
62+
publisher: ic.Events,
63+
}, nil
64+
}
65+
66+
type cgroupsMonitor struct {
67+
collector *collector
68+
context context.Context
69+
publisher events.Publisher
70+
}
71+
72+
func (m *cgroupsMonitor) Monitor(c runtime.Task) error {
73+
if err := m.collector.Add(c); err != nil {
74+
return err
75+
}
76+
t, ok := c.(*linux.Task)
77+
if !ok {
78+
return nil
79+
}
80+
cg, err := t.Cgroup()
81+
if err != nil {
82+
if errdefs.IsNotFound(err) {
83+
return nil
84+
}
85+
return err
86+
}
87+
// OOM handler is not implemented yet
88+
_ = cg
89+
return nil
90+
}
91+
92+
func (m *cgroupsMonitor) Stop(c runtime.Task) error {
93+
m.collector.Remove(c)
94+
return nil
95+
}

metrics/cgroups/v2/metric.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// +build linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package v2
20+
21+
import (
22+
v2 "github.com/containerd/containerd/metrics/types/v2"
23+
metrics "github.com/docker/go-metrics"
24+
"github.com/prometheus/client_golang/prometheus"
25+
)
26+
27+
type value struct {
28+
v float64
29+
l []string
30+
}
31+
32+
type metric struct {
33+
name string
34+
help string
35+
unit metrics.Unit
36+
vt prometheus.ValueType
37+
labels []string
38+
// getValues returns the value and labels for the data
39+
getValues func(stats *v2.Metrics) []value
40+
}
41+
42+
func (m *metric) desc(ns *metrics.Namespace) *prometheus.Desc {
43+
// the namespace label is for containerd namespaces
44+
return ns.NewDesc(m.name, m.help, m.unit, append([]string{"container_id", "namespace"}, m.labels...)...)
45+
}
46+
47+
func (m *metric) collect(id, namespace string, stats *v2.Metrics, ns *metrics.Namespace, ch chan<- prometheus.Metric, block bool) {
48+
values := m.getValues(stats)
49+
for _, v := range values {
50+
// block signals to block on the sending the metrics so none are missed
51+
if block {
52+
ch <- prometheus.MustNewConstMetric(m.desc(ns), m.vt, v.v, append([]string{id, namespace}, v.l...)...)
53+
continue
54+
}
55+
// non-blocking metrics can be dropped if the chan is full
56+
select {
57+
case ch <- prometheus.MustNewConstMetric(m.desc(ns), m.vt, v.v, append([]string{id, namespace}, v.l...)...):
58+
default:
59+
}
60+
}
61+
}

metrics/cgroups/v2/metrics.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// +build linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package v2
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"sync"
25+
26+
"github.com/containerd/containerd/log"
27+
v2 "github.com/containerd/containerd/metrics/types/v2"
28+
"github.com/containerd/containerd/namespaces"
29+
"github.com/containerd/containerd/runtime"
30+
"github.com/containerd/typeurl"
31+
metrics "github.com/docker/go-metrics"
32+
"github.com/prometheus/client_golang/prometheus"
33+
)
34+
35+
// newCollector registers the collector with the provided namespace and returns it so
36+
// that cgroups can be added for collection
37+
func newCollector(ns *metrics.Namespace) *collector {
38+
if ns == nil {
39+
return &collector{}
40+
}
41+
c := &collector{
42+
ns: ns,
43+
tasks: make(map[string]runtime.Task),
44+
}
45+
c.metrics = append(c.metrics, pidMetrics...)
46+
c.storedMetrics = make(chan prometheus.Metric, 100*len(c.metrics))
47+
ns.Add(c)
48+
return c
49+
}
50+
51+
func taskID(id, namespace string) string {
52+
return fmt.Sprintf("%s-%s", id, namespace)
53+
}
54+
55+
// collector provides the ability to collect container stats and export
56+
// them in the prometheus format
57+
type collector struct {
58+
mu sync.RWMutex
59+
60+
tasks map[string]runtime.Task
61+
ns *metrics.Namespace
62+
metrics []*metric
63+
storedMetrics chan prometheus.Metric
64+
}
65+
66+
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
67+
for _, m := range c.metrics {
68+
ch <- m.desc(c.ns)
69+
}
70+
}
71+
72+
func (c *collector) Collect(ch chan<- prometheus.Metric) {
73+
c.mu.RLock()
74+
wg := &sync.WaitGroup{}
75+
for _, t := range c.tasks {
76+
wg.Add(1)
77+
go c.collect(t, ch, true, wg)
78+
}
79+
storedLoop:
80+
for {
81+
// read stored metrics until the channel is flushed
82+
select {
83+
case m := <-c.storedMetrics:
84+
ch <- m
85+
default:
86+
break storedLoop
87+
}
88+
}
89+
c.mu.RUnlock()
90+
wg.Wait()
91+
}
92+
93+
func (c *collector) collect(t runtime.Task, ch chan<- prometheus.Metric, block bool, wg *sync.WaitGroup) {
94+
if wg != nil {
95+
defer wg.Done()
96+
}
97+
ctx := namespaces.WithNamespace(context.Background(), t.Namespace())
98+
stats, err := t.Stats(ctx)
99+
if err != nil {
100+
log.L.WithError(err).Errorf("stat task %s", t.ID())
101+
return
102+
}
103+
data, err := typeurl.UnmarshalAny(stats)
104+
if err != nil {
105+
log.L.WithError(err).Errorf("unmarshal stats for %s", t.ID())
106+
return
107+
}
108+
s, ok := data.(*v2.Metrics)
109+
if !ok {
110+
log.L.WithError(err).Errorf("invalid metric type for %s", t.ID())
111+
return
112+
}
113+
for _, m := range c.metrics {
114+
m.collect(t.ID(), t.Namespace(), s, c.ns, ch, block)
115+
}
116+
}
117+
118+
// Add adds the provided cgroup and id so that metrics are collected and exported
119+
func (c *collector) Add(t runtime.Task) error {
120+
if c.ns == nil {
121+
return nil
122+
}
123+
c.mu.Lock()
124+
defer c.mu.Unlock()
125+
id := taskID(t.ID(), t.Namespace())
126+
if _, ok := c.tasks[id]; ok {
127+
return nil // requests to collect metrics should be idempotent
128+
}
129+
c.tasks[id] = t
130+
return nil
131+
}
132+
133+
// Remove removes the provided cgroup by id from the collector
134+
func (c *collector) Remove(t runtime.Task) {
135+
if c.ns == nil {
136+
return
137+
}
138+
c.mu.Lock()
139+
defer c.mu.Unlock()
140+
delete(c.tasks, taskID(t.ID(), t.Namespace()))
141+
}

0 commit comments

Comments
 (0)
X Tutup