github.com/google/cadvisor@v0.49.1/info/v1/container.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v1
    16  
    17  import (
    18  	"reflect"
    19  	"time"
    20  )
    21  
    22  type CpuSpec struct {
    23  	Limit    uint64 `json:"limit"`
    24  	MaxLimit uint64 `json:"max_limit"`
    25  	Mask     string `json:"mask,omitempty"`
    26  	Quota    uint64 `json:"quota,omitempty"`
    27  	Period   uint64 `json:"period,omitempty"`
    28  }
    29  
    30  type MemorySpec struct {
    31  	// The amount of memory requested. Default is unlimited (-1).
    32  	// Units: bytes.
    33  	Limit uint64 `json:"limit,omitempty"`
    34  
    35  	// The amount of guaranteed memory.  Default is 0.
    36  	// Units: bytes.
    37  	Reservation uint64 `json:"reservation,omitempty"`
    38  
    39  	// The amount of swap space requested. Default is unlimited (-1).
    40  	// Units: bytes.
    41  	SwapLimit uint64 `json:"swap_limit,omitempty"`
    42  }
    43  
    44  type ProcessSpec struct {
    45  	Limit uint64 `json:"limit,omitempty"`
    46  }
    47  
    48  type ContainerSpec struct {
    49  	// Time at which the container was created.
    50  	CreationTime time.Time `json:"creation_time,omitempty"`
    51  
    52  	// Metadata labels associated with this container.
    53  	Labels map[string]string `json:"labels,omitempty"`
    54  	// Metadata envs associated with this container. Only whitelisted envs are added.
    55  	Envs map[string]string `json:"envs,omitempty"`
    56  
    57  	HasCpu bool    `json:"has_cpu"`
    58  	Cpu    CpuSpec `json:"cpu,omitempty"`
    59  
    60  	HasMemory bool       `json:"has_memory"`
    61  	Memory    MemorySpec `json:"memory,omitempty"`
    62  
    63  	HasHugetlb bool `json:"has_hugetlb"`
    64  
    65  	HasNetwork bool `json:"has_network"`
    66  
    67  	HasProcesses bool        `json:"has_processes"`
    68  	Processes    ProcessSpec `json:"processes,omitempty"`
    69  
    70  	HasFilesystem bool `json:"has_filesystem"`
    71  
    72  	// HasDiskIo when true, indicates that DiskIo stats will be available.
    73  	HasDiskIo bool `json:"has_diskio"`
    74  
    75  	HasCustomMetrics bool         `json:"has_custom_metrics"`
    76  	CustomMetrics    []MetricSpec `json:"custom_metrics,omitempty"`
    77  
    78  	// Image name used for this container.
    79  	Image string `json:"image,omitempty"`
    80  }
    81  
    82  // Container reference contains enough information to uniquely identify a container
    83  type ContainerReference struct {
    84  	// The container id
    85  	Id string `json:"id,omitempty"`
    86  
    87  	// The absolute name of the container. This is unique on the machine.
    88  	Name string `json:"name"`
    89  
    90  	// Other names by which the container is known within a certain namespace.
    91  	// This is unique within that namespace.
    92  	Aliases []string `json:"aliases,omitempty"`
    93  
    94  	// Namespace under which the aliases of a container are unique.
    95  	// An example of a namespace is "docker" for Docker containers.
    96  	Namespace string `json:"namespace,omitempty"`
    97  }
    98  
    99  // Sorts by container name.
   100  type ContainerReferenceSlice []ContainerReference
   101  
   102  func (s ContainerReferenceSlice) Len() int           { return len(s) }
   103  func (s ContainerReferenceSlice) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
   104  func (s ContainerReferenceSlice) Less(i, j int) bool { return s[i].Name < s[j].Name }
   105  
   106  // ContainerInfoRequest is used when users check a container info from the REST API.
   107  // It specifies how much data users want to get about a container
   108  type ContainerInfoRequest struct {
   109  	// Max number of stats to return. Specify -1 for all stats currently available.
   110  	// Default: 60
   111  	NumStats int `json:"num_stats,omitempty"`
   112  
   113  	// Start time for which to query information.
   114  	// If omitted, the beginning of time is assumed.
   115  	Start time.Time `json:"start,omitempty"`
   116  
   117  	// End time for which to query information.
   118  	// If omitted, current time is assumed.
   119  	End time.Time `json:"end,omitempty"`
   120  }
   121  
   122  // Returns a ContainerInfoRequest with all default values specified.
   123  func DefaultContainerInfoRequest() ContainerInfoRequest {
   124  	return ContainerInfoRequest{
   125  		NumStats: 60,
   126  	}
   127  }
   128  
   129  func (r *ContainerInfoRequest) Equals(other ContainerInfoRequest) bool {
   130  	return r.NumStats == other.NumStats &&
   131  		r.Start.Equal(other.Start) &&
   132  		r.End.Equal(other.End)
   133  }
   134  
   135  type ContainerInfo struct {
   136  	ContainerReference
   137  
   138  	// The direct subcontainers of the current container.
   139  	Subcontainers []ContainerReference `json:"subcontainers,omitempty"`
   140  
   141  	// The isolation used in the container.
   142  	Spec ContainerSpec `json:"spec,omitempty"`
   143  
   144  	// Historical statistics gathered from the container.
   145  	Stats []*ContainerStats `json:"stats,omitempty"`
   146  }
   147  
   148  // TODO(vmarmol): Refactor to not need this equality comparison.
   149  // ContainerInfo may be (un)marshaled by json or other en/decoder. In that
   150  // case, the Timestamp field in each stats/sample may not be precisely
   151  // en/decoded.  This will lead to small but acceptable differences between a
   152  // ContainerInfo and its encode-then-decode version.  Eq() is used to compare
   153  // two ContainerInfo accepting small difference (<10ms) of Time fields.
   154  func (ci *ContainerInfo) Eq(b *ContainerInfo) bool {
   155  
   156  	// If both ci and b are nil, then Eq() returns true
   157  	if ci == nil {
   158  		return b == nil
   159  	}
   160  	if b == nil {
   161  		return ci == nil
   162  	}
   163  
   164  	// For fields other than time.Time, we will compare them precisely.
   165  	// This would require that any slice should have same order.
   166  	if !reflect.DeepEqual(ci.ContainerReference, b.ContainerReference) {
   167  		return false
   168  	}
   169  	if !reflect.DeepEqual(ci.Subcontainers, b.Subcontainers) {
   170  		return false
   171  	}
   172  	if !ci.Spec.Eq(&b.Spec) {
   173  		return false
   174  	}
   175  
   176  	for i, expectedStats := range b.Stats {
   177  		selfStats := ci.Stats[i]
   178  		if !expectedStats.Eq(selfStats) {
   179  			return false
   180  		}
   181  	}
   182  
   183  	return true
   184  }
   185  
   186  func (s *ContainerSpec) Eq(b *ContainerSpec) bool {
   187  	// Creation within 1s of each other.
   188  	diff := s.CreationTime.Sub(b.CreationTime)
   189  	if (diff > time.Second) || (diff < -time.Second) {
   190  		return false
   191  	}
   192  
   193  	if s.HasCpu != b.HasCpu {
   194  		return false
   195  	}
   196  	if !reflect.DeepEqual(s.Cpu, b.Cpu) {
   197  		return false
   198  	}
   199  	if s.HasMemory != b.HasMemory {
   200  		return false
   201  	}
   202  	if !reflect.DeepEqual(s.Memory, b.Memory) {
   203  		return false
   204  	}
   205  	if s.HasHugetlb != b.HasHugetlb {
   206  		return false
   207  	}
   208  	if s.HasNetwork != b.HasNetwork {
   209  		return false
   210  	}
   211  	if s.HasProcesses != b.HasProcesses {
   212  		return false
   213  	}
   214  	if s.HasFilesystem != b.HasFilesystem {
   215  		return false
   216  	}
   217  	if s.HasDiskIo != b.HasDiskIo {
   218  		return false
   219  	}
   220  	if s.HasCustomMetrics != b.HasCustomMetrics {
   221  		return false
   222  	}
   223  	if s.Image != b.Image {
   224  		return false
   225  	}
   226  	return true
   227  }
   228  
   229  func (ci *ContainerInfo) StatsAfter(ref time.Time) []*ContainerStats {
   230  	n := len(ci.Stats) + 1
   231  	for i, s := range ci.Stats {
   232  		if s.Timestamp.After(ref) {
   233  			n = i
   234  			break
   235  		}
   236  	}
   237  	if n > len(ci.Stats) {
   238  		return nil
   239  	}
   240  	return ci.Stats[n:]
   241  }
   242  
   243  func (ci *ContainerInfo) StatsStartTime() time.Time {
   244  	var ret time.Time
   245  	for _, s := range ci.Stats {
   246  		if s.Timestamp.Before(ret) || ret.IsZero() {
   247  			ret = s.Timestamp
   248  		}
   249  	}
   250  	return ret
   251  }
   252  
   253  func (ci *ContainerInfo) StatsEndTime() time.Time {
   254  	var ret time.Time
   255  	for i := len(ci.Stats) - 1; i >= 0; i-- {
   256  		s := ci.Stats[i]
   257  		if s.Timestamp.After(ret) {
   258  			ret = s.Timestamp
   259  		}
   260  	}
   261  	return ret
   262  }
   263  
   264  // This mirrors kernel internal structure.
   265  type LoadStats struct {
   266  	// Number of sleeping tasks.
   267  	NrSleeping uint64 `json:"nr_sleeping"`
   268  
   269  	// Number of running tasks.
   270  	NrRunning uint64 `json:"nr_running"`
   271  
   272  	// Number of tasks in stopped state
   273  	NrStopped uint64 `json:"nr_stopped"`
   274  
   275  	// Number of tasks in uninterruptible state
   276  	NrUninterruptible uint64 `json:"nr_uninterruptible"`
   277  
   278  	// Number of tasks waiting on IO
   279  	NrIoWait uint64 `json:"nr_io_wait"`
   280  }
   281  
   282  // CPU usage time statistics.
   283  type CpuUsage struct {
   284  	// Total CPU usage.
   285  	// Unit: nanoseconds.
   286  	Total uint64 `json:"total"`
   287  
   288  	// Per CPU/core usage of the container.
   289  	// Unit: nanoseconds.
   290  	PerCpu []uint64 `json:"per_cpu_usage,omitempty"`
   291  
   292  	// Time spent in user space.
   293  	// Unit: nanoseconds.
   294  	User uint64 `json:"user"`
   295  
   296  	// Time spent in kernel space.
   297  	// Unit: nanoseconds.
   298  	System uint64 `json:"system"`
   299  }
   300  
   301  // Cpu Completely Fair Scheduler statistics.
   302  type CpuCFS struct {
   303  	// Total number of elapsed enforcement intervals.
   304  	Periods uint64 `json:"periods"`
   305  
   306  	// Total number of times tasks in the cgroup have been throttled.
   307  	ThrottledPeriods uint64 `json:"throttled_periods"`
   308  
   309  	// Total time duration for which tasks in the cgroup have been throttled.
   310  	// Unit: nanoseconds.
   311  	ThrottledTime uint64 `json:"throttled_time"`
   312  }
   313  
   314  // Cpu Aggregated scheduler statistics
   315  type CpuSchedstat struct {
   316  	// https://www.kernel.org/doc/Documentation/scheduler/sched-stats.txt
   317  
   318  	// time spent on the cpu
   319  	RunTime uint64 `json:"run_time"`
   320  	// time spent waiting on a runqueue
   321  	RunqueueTime uint64 `json:"runqueue_time"`
   322  	// # of timeslices run on this cpu
   323  	RunPeriods uint64 `json:"run_periods"`
   324  }
   325  
   326  // All CPU usage metrics are cumulative from the creation of the container
   327  type CpuStats struct {
   328  	Usage     CpuUsage     `json:"usage"`
   329  	CFS       CpuCFS       `json:"cfs"`
   330  	Schedstat CpuSchedstat `json:"schedstat"`
   331  	// Smoothed average of number of runnable threads x 1000.
   332  	// We multiply by thousand to avoid using floats, but preserving precision.
   333  	// Load is smoothed over the last 10 seconds. Instantaneous value can be read
   334  	// from LoadStats.NrRunning.
   335  	LoadAverage int32 `json:"load_average"`
   336  }
   337  
   338  type PerDiskStats struct {
   339  	Device string            `json:"device"`
   340  	Major  uint64            `json:"major"`
   341  	Minor  uint64            `json:"minor"`
   342  	Stats  map[string]uint64 `json:"stats"`
   343  }
   344  
   345  type DiskIoStats struct {
   346  	IoServiceBytes []PerDiskStats `json:"io_service_bytes,omitempty"`
   347  	IoServiced     []PerDiskStats `json:"io_serviced,omitempty"`
   348  	IoQueued       []PerDiskStats `json:"io_queued,omitempty"`
   349  	Sectors        []PerDiskStats `json:"sectors,omitempty"`
   350  	IoServiceTime  []PerDiskStats `json:"io_service_time,omitempty"`
   351  	IoWaitTime     []PerDiskStats `json:"io_wait_time,omitempty"`
   352  	IoMerged       []PerDiskStats `json:"io_merged,omitempty"`
   353  	IoTime         []PerDiskStats `json:"io_time,omitempty"`
   354  }
   355  
   356  type HugetlbStats struct {
   357  	// current res_counter usage for hugetlb
   358  	Usage uint64 `json:"usage,omitempty"`
   359  	// maximum usage ever recorded.
   360  	MaxUsage uint64 `json:"max_usage,omitempty"`
   361  	// number of times hugetlb usage allocation failure.
   362  	Failcnt uint64 `json:"failcnt"`
   363  }
   364  
   365  type MemoryStats struct {
   366  	// Current memory usage, this includes all memory regardless of when it was
   367  	// accessed.
   368  	// Units: Bytes.
   369  	Usage uint64 `json:"usage"`
   370  
   371  	// Maximum memory usage recorded.
   372  	// Units: Bytes.
   373  	MaxUsage uint64 `json:"max_usage"`
   374  
   375  	// Number of bytes of page cache memory.
   376  	// Units: Bytes.
   377  	Cache uint64 `json:"cache"`
   378  
   379  	// The amount of anonymous and swap cache memory (includes transparent
   380  	// hugepages).
   381  	// Units: Bytes.
   382  	RSS uint64 `json:"rss"`
   383  
   384  	// The amount of swap currently used by the processes in this cgroup
   385  	// Units: Bytes.
   386  	Swap uint64 `json:"swap"`
   387  
   388  	// The amount of memory used for mapped files (includes tmpfs/shmem)
   389  	MappedFile uint64 `json:"mapped_file"`
   390  
   391  	// The amount of working set memory, this includes recently accessed memory,
   392  	// dirty memory, and kernel memory. Working set is <= "usage".
   393  	// Units: Bytes.
   394  	WorkingSet uint64 `json:"working_set"`
   395  
   396  	Failcnt uint64 `json:"failcnt"`
   397  
   398  	// Size of kernel memory allocated in bytes.
   399  	// Units: Bytes.
   400  	KernelUsage uint64 `json:"kernel"`
   401  
   402  	ContainerData    MemoryStatsMemoryData `json:"container_data,omitempty"`
   403  	HierarchicalData MemoryStatsMemoryData `json:"hierarchical_data,omitempty"`
   404  }
   405  
   406  type CPUSetStats struct {
   407  	MemoryMigrate uint64 `json:"memory_migrate"`
   408  }
   409  
   410  type MemoryNumaStats struct {
   411  	File        map[uint8]uint64 `json:"file,omitempty"`
   412  	Anon        map[uint8]uint64 `json:"anon,omitempty"`
   413  	Unevictable map[uint8]uint64 `json:"unevictable,omitempty"`
   414  }
   415  
   416  type MemoryStatsMemoryData struct {
   417  	Pgfault    uint64          `json:"pgfault"`
   418  	Pgmajfault uint64          `json:"pgmajfault"`
   419  	NumaStats  MemoryNumaStats `json:"numa_stats,omitempty"`
   420  }
   421  
   422  type InterfaceStats struct {
   423  	// The name of the interface.
   424  	Name string `json:"name"`
   425  	// Cumulative count of bytes received.
   426  	RxBytes uint64 `json:"rx_bytes"`
   427  	// Cumulative count of packets received.
   428  	RxPackets uint64 `json:"rx_packets"`
   429  	// Cumulative count of receive errors encountered.
   430  	RxErrors uint64 `json:"rx_errors"`
   431  	// Cumulative count of packets dropped while receiving.
   432  	RxDropped uint64 `json:"rx_dropped"`
   433  	// Cumulative count of bytes transmitted.
   434  	TxBytes uint64 `json:"tx_bytes"`
   435  	// Cumulative count of packets transmitted.
   436  	TxPackets uint64 `json:"tx_packets"`
   437  	// Cumulative count of transmit errors encountered.
   438  	TxErrors uint64 `json:"tx_errors"`
   439  	// Cumulative count of packets dropped while transmitting.
   440  	TxDropped uint64 `json:"tx_dropped"`
   441  }
   442  
   443  type NetworkStats struct {
   444  	InterfaceStats `json:",inline"`
   445  	Interfaces     []InterfaceStats `json:"interfaces,omitempty"`
   446  	// TCP connection stats (Established, Listen...)
   447  	Tcp TcpStat `json:"tcp"`
   448  	// TCP6 connection stats (Established, Listen...)
   449  	Tcp6 TcpStat `json:"tcp6"`
   450  	// UDP connection stats
   451  	Udp UdpStat `json:"udp"`
   452  	// UDP6 connection stats
   453  	Udp6 UdpStat `json:"udp6"`
   454  	// TCP advanced stats
   455  	TcpAdvanced TcpAdvancedStat `json:"tcp_advanced"`
   456  }
   457  
   458  type TcpStat struct {
   459  	// Count of TCP connections in state "Established"
   460  	Established uint64
   461  	// Count of TCP connections in state "Syn_Sent"
   462  	SynSent uint64
   463  	// Count of TCP connections in state "Syn_Recv"
   464  	SynRecv uint64
   465  	// Count of TCP connections in state "Fin_Wait1"
   466  	FinWait1 uint64
   467  	// Count of TCP connections in state "Fin_Wait2"
   468  	FinWait2 uint64
   469  	// Count of TCP connections in state "Time_Wait
   470  	TimeWait uint64
   471  	// Count of TCP connections in state "Close"
   472  	Close uint64
   473  	// Count of TCP connections in state "Close_Wait"
   474  	CloseWait uint64
   475  	// Count of TCP connections in state "Listen_Ack"
   476  	LastAck uint64
   477  	// Count of TCP connections in state "Listen"
   478  	Listen uint64
   479  	// Count of TCP connections in state "Closing"
   480  	Closing uint64
   481  }
   482  
   483  type TcpAdvancedStat struct {
   484  	// The algorithm used to determine the timeout value used for
   485  	// retransmitting unacknowledged octets, ref: RFC2698, default 1
   486  	RtoAlgorithm uint64
   487  	// The minimum value permitted by a TCP implementation for the
   488  	// retransmission timeout, measured in milliseconds, default 200ms
   489  	RtoMin uint64
   490  	// The maximum value permitted by a TCP implementation for the
   491  	// retransmission timeout, measured in milliseconds, default 120s
   492  	RtoMax uint64
   493  	// The limit on the total number of TCP connections the entity
   494  	// can support., default -1, i.e. infinity
   495  	MaxConn int64
   496  
   497  	// The number of times TCP connections have made a direct
   498  	// transition to the SYN-SENT state from the CLOSED state.
   499  	ActiveOpens uint64
   500  	// The number of times TCP connections have made a direct
   501  	// transition to the SYN-RCVD state from the LISTEN state.
   502  	PassiveOpens uint64
   503  	// The number of times TCP connections have made a direct
   504  	// transition to the CLOSED state from either the SYN-SENT
   505  	// state or the SYN-RCVD state, plus the number of times TCP
   506  	// connections have made a direct transition to the LISTEN
   507  	// state from the SYN-RCVD state.
   508  	AttemptFails uint64
   509  	// The number of times TCP connections have made a direct
   510  	// transition to the CLOSED state from either the ESTABLISHED
   511  	// state or the CLOSE-WAIT state.
   512  	EstabResets uint64
   513  	// The number of TCP connections for which the current state
   514  	// is either ESTABLISHED or CLOSE- WAIT.
   515  	CurrEstab uint64
   516  
   517  	// The total number of segments received, including those
   518  	// received in error.
   519  	InSegs uint64
   520  	// The total number of segments sent, including those on
   521  	// current connections but excluding those containing only
   522  	// retransmitted octets.
   523  	OutSegs uint64
   524  	// The total number of segments retransmitted - that is, the
   525  	// number of TCP segments transmitted containing one or more
   526  	// previously transmitted octets.
   527  	RetransSegs uint64
   528  	// The total number of segments received in error (e.g., bad
   529  	// TCP checksums).
   530  	InErrs uint64
   531  	// The number of TCP segments sent containing the RST flag.
   532  	OutRsts uint64
   533  	// The number of IP Packets with checksum errors
   534  	InCsumErrors uint64
   535  	// The number of resets received for embryonic SYN_RECV sockets
   536  	EmbryonicRsts uint64
   537  
   538  	// The number of SYN cookies sent
   539  	SyncookiesSent uint64
   540  	// The number of SYN cookies received
   541  	SyncookiesRecv uint64
   542  	// The number of invalid SYN cookies received
   543  	SyncookiesFailed uint64
   544  
   545  	// The number of packets pruned from receive queue because of socket buffer overrun
   546  	PruneCalled uint64
   547  	// The number of packets pruned from receive queue
   548  	RcvPruned uint64
   549  	// The number of packets dropped from out-of-order queue because of socket buffer overrun
   550  	OfoPruned uint64
   551  	// The number of ICMP packets dropped because they were out-of-window
   552  	OutOfWindowIcmps uint64
   553  	// The number of ICMP packets dropped because socket was locked
   554  	LockDroppedIcmps uint64
   555  
   556  	// The number of TCP sockets finished time wait in fast timer
   557  	TW uint64
   558  	// The number of time wait sockets recycled by time stamp
   559  	TWRecycled uint64
   560  	// The number of TCP sockets finished time wait in slow timer
   561  	TWKilled uint64
   562  	// counter, if no more mem for TIME-WAIT struct, +1
   563  	TCPTimeWaitOverflow uint64
   564  
   565  	// The number of RTO timer first timeout times
   566  	TCPTimeouts uint64
   567  	// The number of fake timeouts detected by F-RTO
   568  	TCPSpuriousRTOs uint64
   569  	// The number of send Tail Loss Probe (TLP) times by Probe Timeout(PTO)
   570  	TCPLossProbes uint64
   571  	// The number of recovery times by TLP
   572  	TCPLossProbeRecovery uint64
   573  	// The number of RTO failed times when in Recovery state, and remote end has no sack
   574  	TCPRenoRecoveryFail uint64
   575  	// The number of RTO failed times when in Recovery state, and remote end has sack
   576  	TCPSackRecoveryFail uint64
   577  	// The number of RTO failed times when in TCP_CA_Disorder state, and remote end has no sack
   578  	TCPRenoFailures uint64
   579  	// The number of RTO failed times when in TCP_CA_Disorder state, and remote end has sack
   580  	TCPSackFailures uint64
   581  	// The number of RTO failed times when in TCP_CA_Loss state,
   582  	TCPLossFailures uint64
   583  
   584  	// The number of delayed acks sent
   585  	DelayedACKs uint64
   586  	// The number of delayed acks further delayed because of locked socket
   587  	DelayedACKLocked uint64
   588  	// The number of quick ack mode was activated times
   589  	DelayedACKLost uint64
   590  	// The number of times the listen queue of a socket overflowed
   591  	ListenOverflows uint64
   592  	// The number of SYNs to LISTEN sockets dropped
   593  	ListenDrops uint64
   594  	// The number of packet headers predicted
   595  	TCPHPHits uint64
   596  	// The number of acknowledgments not containing data payload received
   597  	TCPPureAcks uint64
   598  	// The number of predicted acknowledgments
   599  	TCPHPAcks uint64
   600  	// The number of times recovered from packet loss due to fast retransmit
   601  	TCPRenoRecovery uint64
   602  	// The number of SACK retransmits failed
   603  	TCPSackRecovery uint64
   604  	// The number of bad SACK blocks received
   605  	TCPSACKReneging uint64
   606  	// The number of detected reordering times using FACK
   607  	TCPFACKReorder uint64
   608  	// The number of detected reordering times using SACK
   609  	TCPSACKReorder uint64
   610  	// The number of detected reordering times using Reno
   611  	TCPRenoReorder uint64
   612  	// The number of detected reordering times using time stamp
   613  	TCPTSReorder uint64
   614  	// The number of congestion windows fully recovered without slow start
   615  	TCPFullUndo uint64
   616  	// The number of congestion windows partially recovered using Hoe heuristic
   617  	TCPPartialUndo uint64
   618  	// The number of congestion windows recovered without slow start by DSACK
   619  	TCPDSACKUndo uint64
   620  	// The number of congestion windows recovered without slow start after partial ack
   621  	TCPLossUndo uint64
   622  
   623  	// The number of fast retransmits
   624  	TCPFastRetrans uint64
   625  	// The number of retransmits in slow start
   626  	TCPSlowStartRetrans uint64
   627  	// The number of retransmits lost
   628  	TCPLostRetransmit uint64
   629  	// The number of retransmits failed, including FastRetrans, SlowStartRetrans
   630  	TCPRetransFail uint64
   631  
   632  	// he number of packets collapsed in receive queue due to low socket buffer
   633  	TCPRcvCollapsed uint64
   634  	// The number of DSACKs sent for old packets
   635  	TCPDSACKOldSent uint64
   636  	// The number of DSACKs sent for out of order packets
   637  	TCPDSACKOfoSent uint64
   638  	// The number of DSACKs received
   639  	TCPDSACKRecv uint64
   640  	// The number of DSACKs for out of order packets received
   641  	TCPDSACKOfoRecv uint64
   642  	// The number of connections reset due to unexpected data
   643  	TCPAbortOnData uint64
   644  	// The number of connections reset due to early user close
   645  	TCPAbortOnClose uint64
   646  	// The number of connections aborted due to memory pressure
   647  	TCPAbortOnMemory uint64
   648  	// The number of connections aborted due to timeout
   649  	TCPAbortOnTimeout uint64
   650  	// The number of connections aborted after user close in linger timeout
   651  	TCPAbortOnLinger uint64
   652  	// The number of times unable to send RST due to no memory
   653  	TCPAbortFailed uint64
   654  	// The number of TCP ran low on memory times
   655  	TCPMemoryPressures uint64
   656  	// The number of TCP cumulative duration of
   657  	// memory pressure events, by ms
   658  	TCPMemoryPressuresChrono uint64
   659  	// The number of SACKs discard
   660  	TCPSACKDiscard uint64
   661  	// The number of DSACKs ignore old
   662  	TCPDSACKIgnoredOld uint64
   663  	// The number of DSACKs ignore no undo
   664  	TCPDSACKIgnoredNoUndo uint64
   665  
   666  	// The number of MD5 not found
   667  	TCPMD5NotFound uint64
   668  	// The number of MD5 unexpected
   669  	TCPMD5Unexpected uint64
   670  	// The number of MD5 failed
   671  	TCPMD5Failure uint64
   672  	// The number of Sack shifted
   673  	TCPSackShifted uint64
   674  	// The number of Sack merged
   675  	TCPSackMerged uint64
   676  	// The number of Sack shift fall back
   677  	TCPSackShiftFallback uint64
   678  	// The number of Backlog drop
   679  	TCPBacklogDrop uint64
   680  	// The number of PFmemalloc drop
   681  	PFMemallocDrop uint64
   682  	// The number of memalloc drop
   683  	TCPMinTTLDrop uint64
   684  	// The number of DeferAccept drop
   685  	TCPDeferAcceptDrop uint64
   686  	// The number of IP reverse path filter
   687  	IPReversePathFilter uint64
   688  
   689  	// The number of request full do cookies
   690  	TCPReqQFullDoCookies uint64
   691  	// The number of request full drop
   692  	TCPReqQFullDrop uint64
   693  
   694  	// number of successful outbound TFO connections
   695  	TCPFastOpenActive uint64
   696  	// number of SYN-ACK packets received that did not acknowledge data
   697  	// sent in the SYN packet and caused a retransmissions without SYN data.
   698  	TCPFastOpenActiveFail uint64
   699  	// number of successful inbound TFO connections
   700  	TCPFastOpenPassive uint64
   701  	// number of inbound SYN packets with TFO cookie that was invalid
   702  	TCPFastOpenPassiveFail uint64
   703  	// number of inbound SYN packets that will have TFO disabled because
   704  	// the socket has exceeded the max queue length
   705  	TCPFastOpenListenOverflow uint64
   706  	// number of inbound SYN packets requesting TFO with TFO set but no cookie
   707  	TCPFastOpenCookieReqd uint64
   708  
   709  	// number of SYN and SYN/ACK retransmits to break down retransmissions
   710  	// into SYN, fast-retransmits, timeout retransmits, etc.
   711  	TCPSynRetrans uint64
   712  	// number of outgoing packets with original data
   713  	// (excluding retransmission but including data-in-SYN).
   714  	TCPOrigDataSent uint64
   715  
   716  	// The number of active connections rejected because of time stamp
   717  	PAWSActive uint64
   718  	// The number of packetes rejected in established connections because of timestamp
   719  	PAWSEstab uint64
   720  }
   721  
   722  type UdpStat struct {
   723  	// Count of UDP sockets in state "Listen"
   724  	Listen uint64
   725  
   726  	// Count of UDP packets dropped by the IP stack
   727  	Dropped uint64
   728  
   729  	// Count of packets Queued for Receieve
   730  	RxQueued uint64
   731  
   732  	// Count of packets Queued for Transmit
   733  	TxQueued uint64
   734  }
   735  
   736  type FsStats struct {
   737  	// The block device name associated with the filesystem.
   738  	Device string `json:"device,omitempty"`
   739  
   740  	// Type of the filesytem.
   741  	Type string `json:"type"`
   742  
   743  	// Number of bytes that can be consumed by the container on this filesystem.
   744  	Limit uint64 `json:"capacity"`
   745  
   746  	// Number of bytes that is consumed by the container on this filesystem.
   747  	Usage uint64 `json:"usage"`
   748  
   749  	// Base Usage that is consumed by the container's writable layer.
   750  	// This field is only applicable for docker container's as of now.
   751  	BaseUsage uint64 `json:"base_usage"`
   752  
   753  	// Number of bytes available for non-root user.
   754  	Available uint64 `json:"available"`
   755  
   756  	// HasInodes when true, indicates that Inodes info will be available.
   757  	HasInodes bool `json:"has_inodes"`
   758  
   759  	// Number of Inodes
   760  	Inodes uint64 `json:"inodes"`
   761  
   762  	// Number of available Inodes
   763  	InodesFree uint64 `json:"inodes_free"`
   764  
   765  	// Number of reads completed
   766  	// This is the total number of reads completed successfully.
   767  	ReadsCompleted uint64 `json:"reads_completed"`
   768  
   769  	// Number of reads merged
   770  	// Reads and writes which are adjacent to each other may be merged for
   771  	// efficiency.  Thus two 4K reads may become one 8K read before it is
   772  	// ultimately handed to the disk, and so it will be counted (and queued)
   773  	// as only one I/O.  This field lets you know how often this was done.
   774  	ReadsMerged uint64 `json:"reads_merged"`
   775  
   776  	// Number of sectors read
   777  	// This is the total number of sectors read successfully.
   778  	SectorsRead uint64 `json:"sectors_read"`
   779  
   780  	// Number of milliseconds spent reading
   781  	// This is the total number of milliseconds spent by all reads (as
   782  	// measured from __make_request() to end_that_request_last()).
   783  	ReadTime uint64 `json:"read_time"`
   784  
   785  	// Number of writes completed
   786  	// This is the total number of writes completed successfully.
   787  	WritesCompleted uint64 `json:"writes_completed"`
   788  
   789  	// Number of writes merged
   790  	// See the description of reads merged.
   791  	WritesMerged uint64 `json:"writes_merged"`
   792  
   793  	// Number of sectors written
   794  	// This is the total number of sectors written successfully.
   795  	SectorsWritten uint64 `json:"sectors_written"`
   796  
   797  	// Number of milliseconds spent writing
   798  	// This is the total number of milliseconds spent by all writes (as
   799  	// measured from __make_request() to end_that_request_last()).
   800  	WriteTime uint64 `json:"write_time"`
   801  
   802  	// Number of I/Os currently in progress
   803  	// The only field that should go to zero. Incremented as requests are
   804  	// given to appropriate struct request_queue and decremented as they finish.
   805  	IoInProgress uint64 `json:"io_in_progress"`
   806  
   807  	// Number of milliseconds spent doing I/Os
   808  	// This field increases so long as field 9 is nonzero.
   809  	IoTime uint64 `json:"io_time"`
   810  
   811  	// weighted number of milliseconds spent doing I/Os
   812  	// This field is incremented at each I/O start, I/O completion, I/O
   813  	// merge, or read of these stats by the number of I/Os in progress
   814  	// (field 9) times the number of milliseconds spent doing I/O since the
   815  	// last update of this field.  This can provide an easy measure of both
   816  	// I/O completion time and the backlog that may be accumulating.
   817  	WeightedIoTime uint64 `json:"weighted_io_time"`
   818  }
   819  
   820  type AcceleratorStats struct {
   821  	// Make of the accelerator (nvidia, amd, google etc.)
   822  	Make string `json:"make"`
   823  
   824  	// Model of the accelerator (tesla-p100, tesla-k80 etc.)
   825  	Model string `json:"model"`
   826  
   827  	// ID of the accelerator.
   828  	ID string `json:"id"`
   829  
   830  	// Total accelerator memory.
   831  	// unit: bytes
   832  	MemoryTotal uint64 `json:"memory_total"`
   833  
   834  	// Total accelerator memory allocated.
   835  	// unit: bytes
   836  	MemoryUsed uint64 `json:"memory_used"`
   837  
   838  	// Percent of time over the past sample period during which
   839  	// the accelerator was actively processing.
   840  	DutyCycle uint64 `json:"duty_cycle"`
   841  }
   842  
   843  // PerfStat represents value of a single monitored perf event.
   844  type PerfStat struct {
   845  	PerfValue
   846  
   847  	// CPU that perf event was measured on.
   848  	Cpu int `json:"cpu"`
   849  }
   850  
   851  type PerfValue struct {
   852  	// Indicates scaling ratio for an event: time_running/time_enabled
   853  	// (amount of time that event was being measured divided by
   854  	// amount of time that event was enabled for).
   855  	// value 1.0 indicates that no multiplexing occurred. Value close
   856  	// to 0 indicates that event was measured for short time and event's
   857  	// value might be inaccurate.
   858  	// See: https://lwn.net/Articles/324756/
   859  	ScalingRatio float64 `json:"scaling_ratio"`
   860  
   861  	// Value represents value of perf event retrieved from OS. It is
   862  	// normalized against ScalingRatio and takes multiplexing into
   863  	// consideration.
   864  	Value uint64 `json:"value"`
   865  
   866  	// Name is human readable name of an event.
   867  	Name string `json:"name"`
   868  }
   869  
   870  // MemoryBandwidthStats corresponds to MBM (Memory Bandwidth Monitoring).
   871  // See: https://01.org/cache-monitoring-technology
   872  // See: https://www.kernel.org/doc/Documentation/x86/intel_rdt_ui.txt
   873  type MemoryBandwidthStats struct {
   874  	// The 'mbm_total_bytes'.
   875  	TotalBytes uint64 `json:"mbm_total_bytes,omitempty"`
   876  
   877  	// The 'mbm_local_bytes'.
   878  	LocalBytes uint64 `json:"mbm_local_bytes,omitempty"`
   879  }
   880  
   881  // CacheStats corresponds to CMT (Cache Monitoring Technology).
   882  // See: https://01.org/cache-monitoring-technology
   883  // See: https://www.kernel.org/doc/Documentation/x86/intel_rdt_ui.txt
   884  type CacheStats struct {
   885  	// The 'llc_occupancy'.
   886  	LLCOccupancy uint64 `json:"llc_occupancy,omitempty"`
   887  }
   888  
   889  // ResctrlStats corresponds to statistics from Resource Control.
   890  type ResctrlStats struct {
   891  	// Each NUMA Node statistics corresponds to one element in the array.
   892  	MemoryBandwidth []MemoryBandwidthStats `json:"memory_bandwidth,omitempty"`
   893  	Cache           []CacheStats           `json:"cache,omitempty"`
   894  }
   895  
   896  // PerfUncoreStat represents value of a single monitored perf uncore event.
   897  type PerfUncoreStat struct {
   898  	PerfValue
   899  
   900  	// Socket that perf event was measured on.
   901  	Socket int `json:"socket"`
   902  
   903  	// PMU is Performance Monitoring Unit which collected these stats.
   904  	PMU string `json:"pmu"`
   905  }
   906  
   907  type UlimitSpec struct {
   908  	Name      string `json:"name"`
   909  	SoftLimit int64  `json:"soft_limit"`
   910  	HardLimit int64  `json:"hard_limit"`
   911  }
   912  
   913  type ProcessStats struct {
   914  	// Number of processes
   915  	ProcessCount uint64 `json:"process_count"`
   916  
   917  	// Number of open file descriptors
   918  	FdCount uint64 `json:"fd_count"`
   919  
   920  	// Number of sockets
   921  	SocketCount uint64 `json:"socket_count"`
   922  
   923  	// Number of threads currently in container
   924  	ThreadsCurrent uint64 `json:"threads_current,omitempty"`
   925  
   926  	// Maxium number of threads allowed in container
   927  	ThreadsMax uint64 `json:"threads_max,omitempty"`
   928  
   929  	// Ulimits for the top-level container process
   930  	Ulimits []UlimitSpec `json:"ulimits,omitempty"`
   931  }
   932  
   933  type ContainerStats struct {
   934  	// The time of this stat point.
   935  	Timestamp time.Time               `json:"timestamp"`
   936  	Cpu       CpuStats                `json:"cpu,omitempty"`
   937  	DiskIo    DiskIoStats             `json:"diskio,omitempty"`
   938  	Memory    MemoryStats             `json:"memory,omitempty"`
   939  	Hugetlb   map[string]HugetlbStats `json:"hugetlb,omitempty"`
   940  	Network   NetworkStats            `json:"network,omitempty"`
   941  	// Filesystem statistics
   942  	Filesystem []FsStats `json:"filesystem,omitempty"`
   943  
   944  	// Task load stats
   945  	TaskStats LoadStats `json:"task_stats,omitempty"`
   946  
   947  	// Metrics for Accelerators. Each Accelerator corresponds to one element in the array.
   948  	Accelerators []AcceleratorStats `json:"accelerators,omitempty"`
   949  
   950  	// ProcessStats for Containers
   951  	Processes ProcessStats `json:"processes,omitempty"`
   952  
   953  	// Custom metrics from all collectors
   954  	CustomMetrics map[string][]MetricVal `json:"custom_metrics,omitempty"`
   955  
   956  	// Statistics originating from perf events
   957  	PerfStats []PerfStat `json:"perf_stats,omitempty"`
   958  
   959  	// Statistics originating from perf uncore events.
   960  	// Applies only for root container.
   961  	PerfUncoreStats []PerfUncoreStat `json:"perf_uncore_stats,omitempty"`
   962  
   963  	// Referenced memory
   964  	ReferencedMemory uint64 `json:"referenced_memory,omitempty"`
   965  
   966  	// Resource Control (resctrl) statistics
   967  	Resctrl ResctrlStats `json:"resctrl,omitempty"`
   968  
   969  	CpuSet CPUSetStats `json:"cpuset,omitempty"`
   970  
   971  	OOMEvents uint64 `json:"oom_events,omitempty"`
   972  }
   973  
   974  func timeEq(t1, t2 time.Time, tolerance time.Duration) bool {
   975  	// t1 should not be later than t2
   976  	if t1.After(t2) {
   977  		t1, t2 = t2, t1
   978  	}
   979  	diff := t2.Sub(t1)
   980  	return diff <= tolerance
   981  }
   982  
   983  const (
   984  	// 10ms, i.e. 0.01s
   985  	timePrecision time.Duration = 10 * time.Millisecond
   986  )
   987  
   988  // This function is useful because we do not require precise time
   989  // representation.
   990  func (a *ContainerStats) Eq(b *ContainerStats) bool {
   991  	if !timeEq(a.Timestamp, b.Timestamp, timePrecision) {
   992  		return false
   993  	}
   994  	return a.StatsEq(b)
   995  }
   996  
   997  // Checks equality of the stats values.
   998  func (a *ContainerStats) StatsEq(b *ContainerStats) bool {
   999  	// TODO(vmarmol): Consider using this through reflection.
  1000  	if !reflect.DeepEqual(a.Cpu, b.Cpu) {
  1001  		return false
  1002  	}
  1003  	if !reflect.DeepEqual(a.Memory, b.Memory) {
  1004  		return false
  1005  	}
  1006  	if !reflect.DeepEqual(a.Hugetlb, b.Hugetlb) {
  1007  		return false
  1008  	}
  1009  	if !reflect.DeepEqual(a.DiskIo, b.DiskIo) {
  1010  		return false
  1011  	}
  1012  	if !reflect.DeepEqual(a.Network, b.Network) {
  1013  		return false
  1014  	}
  1015  	if !reflect.DeepEqual(a.Processes, b.Processes) {
  1016  		return false
  1017  	}
  1018  	if !reflect.DeepEqual(a.Filesystem, b.Filesystem) {
  1019  		return false
  1020  	}
  1021  	if !reflect.DeepEqual(a.TaskStats, b.TaskStats) {
  1022  		return false
  1023  	}
  1024  	if !reflect.DeepEqual(a.Accelerators, b.Accelerators) {
  1025  		return false
  1026  	}
  1027  	if !reflect.DeepEqual(a.CustomMetrics, b.CustomMetrics) {
  1028  		return false
  1029  	}
  1030  	return true
  1031  }
  1032  
  1033  // Event contains information general to events such as the time at which they
  1034  // occurred, their specific type, and the actual event. Event types are
  1035  // differentiated by the EventType field of Event.
  1036  type Event struct {
  1037  	// the absolute container name for which the event occurred
  1038  	ContainerName string `json:"container_name"`
  1039  
  1040  	// the time at which the event occurred
  1041  	Timestamp time.Time `json:"timestamp"`
  1042  
  1043  	// the type of event. EventType is an enumerated type
  1044  	EventType EventType `json:"event_type"`
  1045  
  1046  	// the original event object and all of its extraneous data, ex. an
  1047  	// OomInstance
  1048  	EventData EventData `json:"event_data,omitempty"`
  1049  }
  1050  
  1051  // EventType is an enumerated type which lists the categories under which
  1052  // events may fall. The Event field EventType is populated by this enum.
  1053  type EventType string
  1054  
  1055  const (
  1056  	EventOom               EventType = "oom"
  1057  	EventOomKill           EventType = "oomKill"
  1058  	EventContainerCreation EventType = "containerCreation"
  1059  	EventContainerDeletion EventType = "containerDeletion"
  1060  )
  1061  
  1062  // Extra information about an event. Only one type will be set.
  1063  type EventData struct {
  1064  	// Information about an OOM kill event.
  1065  	OomKill *OomKillEventData `json:"oom,omitempty"`
  1066  }
  1067  
  1068  // Information related to an OOM kill instance
  1069  type OomKillEventData struct {
  1070  	// process id of the killed process
  1071  	Pid int `json:"pid"`
  1072  
  1073  	// The name of the killed process
  1074  	ProcessName string `json:"process_name"`
  1075  }