github.com/containerd/Containerd@v1.4.13/metrics/cgroups/v1/cgroups.go (about)

     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 v1
    20  
    21  import (
    22  	"context"
    23  
    24  	"github.com/containerd/cgroups"
    25  	eventstypes "github.com/containerd/containerd/api/events"
    26  	"github.com/containerd/containerd/errdefs"
    27  	"github.com/containerd/containerd/events"
    28  	"github.com/containerd/containerd/log"
    29  	"github.com/containerd/containerd/namespaces"
    30  	"github.com/containerd/containerd/runtime"
    31  	"github.com/containerd/containerd/runtime/v1/linux"
    32  	metrics "github.com/docker/go-metrics"
    33  	"github.com/sirupsen/logrus"
    34  )
    35  
    36  // NewTaskMonitor returns a new cgroups monitor
    37  func NewTaskMonitor(ctx context.Context, publisher events.Publisher, ns *metrics.Namespace) (runtime.TaskMonitor, error) {
    38  	collector := NewCollector(ns)
    39  	oom, err := newOOMCollector(ns)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return &cgroupsMonitor{
    44  		collector: collector,
    45  		oom:       oom,
    46  		context:   ctx,
    47  		publisher: publisher,
    48  	}, nil
    49  }
    50  
    51  type cgroupsMonitor struct {
    52  	collector *Collector
    53  	oom       *oomCollector
    54  	context   context.Context
    55  	publisher events.Publisher
    56  }
    57  
    58  func (m *cgroupsMonitor) Monitor(c runtime.Task) error {
    59  	if err := m.collector.Add(c); err != nil {
    60  		return err
    61  	}
    62  	t, ok := c.(*linux.Task)
    63  	if !ok {
    64  		return nil
    65  	}
    66  	cg, err := t.Cgroup()
    67  	if err != nil {
    68  		if errdefs.IsNotFound(err) {
    69  			return nil
    70  		}
    71  		return err
    72  	}
    73  	err = m.oom.Add(c.ID(), c.Namespace(), cg, m.trigger)
    74  	if err == cgroups.ErrMemoryNotSupported {
    75  		logrus.WithError(err).Warn("OOM monitoring failed")
    76  		return nil
    77  	}
    78  	return err
    79  }
    80  
    81  func (m *cgroupsMonitor) Stop(c runtime.Task) error {
    82  	m.collector.Remove(c)
    83  	return nil
    84  }
    85  
    86  func (m *cgroupsMonitor) trigger(id, namespace string, cg cgroups.Cgroup) {
    87  	ctx := namespaces.WithNamespace(m.context, namespace)
    88  	if err := m.publisher.Publish(ctx, runtime.TaskOOMEventTopic, &eventstypes.TaskOOM{
    89  		ContainerID: id,
    90  	}); err != nil {
    91  		log.G(m.context).WithError(err).Error("post OOM event")
    92  	}
    93  }