github.com/demonoid81/containerd@v1.3.4/metrics/cgroups/cpu.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 cgroups
    20  
    21  import (
    22  	"strconv"
    23  
    24  	v1 "github.com/containerd/containerd/metrics/types/v1"
    25  	metrics "github.com/docker/go-metrics"
    26  	"github.com/prometheus/client_golang/prometheus"
    27  )
    28  
    29  var cpuMetrics = []*metric{
    30  	{
    31  		name: "cpu_total",
    32  		help: "The total cpu time",
    33  		unit: metrics.Nanoseconds,
    34  		vt:   prometheus.GaugeValue,
    35  		getValues: func(stats *v1.Metrics) []value {
    36  			if stats.CPU == nil {
    37  				return nil
    38  			}
    39  			return []value{
    40  				{
    41  					v: float64(stats.CPU.Usage.Total),
    42  				},
    43  			}
    44  		},
    45  	},
    46  	{
    47  		name: "cpu_kernel",
    48  		help: "The total kernel cpu time",
    49  		unit: metrics.Nanoseconds,
    50  		vt:   prometheus.GaugeValue,
    51  		getValues: func(stats *v1.Metrics) []value {
    52  			if stats.CPU == nil {
    53  				return nil
    54  			}
    55  			return []value{
    56  				{
    57  					v: float64(stats.CPU.Usage.Kernel),
    58  				},
    59  			}
    60  		},
    61  	},
    62  	{
    63  		name: "cpu_user",
    64  		help: "The total user cpu time",
    65  		unit: metrics.Nanoseconds,
    66  		vt:   prometheus.GaugeValue,
    67  		getValues: func(stats *v1.Metrics) []value {
    68  			if stats.CPU == nil {
    69  				return nil
    70  			}
    71  			return []value{
    72  				{
    73  					v: float64(stats.CPU.Usage.User),
    74  				},
    75  			}
    76  		},
    77  	},
    78  	{
    79  		name:   "per_cpu",
    80  		help:   "The total cpu time per cpu",
    81  		unit:   metrics.Nanoseconds,
    82  		vt:     prometheus.GaugeValue,
    83  		labels: []string{"cpu"},
    84  		getValues: func(stats *v1.Metrics) []value {
    85  			if stats.CPU == nil {
    86  				return nil
    87  			}
    88  			var out []value
    89  			for i, v := range stats.CPU.Usage.PerCPU {
    90  				out = append(out, value{
    91  					v: float64(v),
    92  					l: []string{strconv.Itoa(i)},
    93  				})
    94  			}
    95  			return out
    96  		},
    97  	},
    98  	{
    99  		name: "cpu_throttle_periods",
   100  		help: "The total cpu throttle periods",
   101  		unit: metrics.Total,
   102  		vt:   prometheus.GaugeValue,
   103  		getValues: func(stats *v1.Metrics) []value {
   104  			if stats.CPU == nil {
   105  				return nil
   106  			}
   107  			return []value{
   108  				{
   109  					v: float64(stats.CPU.Throttling.Periods),
   110  				},
   111  			}
   112  		},
   113  	},
   114  	{
   115  		name: "cpu_throttled_periods",
   116  		help: "The total cpu throttled periods",
   117  		unit: metrics.Total,
   118  		vt:   prometheus.GaugeValue,
   119  		getValues: func(stats *v1.Metrics) []value {
   120  			if stats.CPU == nil {
   121  				return nil
   122  			}
   123  			return []value{
   124  				{
   125  					v: float64(stats.CPU.Throttling.ThrottledPeriods),
   126  				},
   127  			}
   128  		},
   129  	},
   130  	{
   131  		name: "cpu_throttled_time",
   132  		help: "The total cpu throttled time",
   133  		unit: metrics.Nanoseconds,
   134  		vt:   prometheus.GaugeValue,
   135  		getValues: func(stats *v1.Metrics) []value {
   136  			if stats.CPU == nil {
   137  				return nil
   138  			}
   139  			return []value{
   140  				{
   141  					v: float64(stats.CPU.Throttling.ThrottledTime),
   142  				},
   143  			}
   144  		},
   145  	},
   146  }