container.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Copyright 2015 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. package v2
  15. import (
  16. "time"
  17. // TODO(rjnagal): Remove dependency after moving all stats structs from v1.
  18. // using v1 now for easy conversion.
  19. "github.com/google/cadvisor/info/v1"
  20. )
  21. const (
  22. TypeName = "name"
  23. TypeDocker = "docker"
  24. )
  25. type CpuSpec struct {
  26. // Requested cpu shares. Default is 1024.
  27. Limit uint64 `json:"limit"`
  28. // Requested cpu hard limit. Default is unlimited (0).
  29. // Units: milli-cpus.
  30. MaxLimit uint64 `json:"max_limit"`
  31. // Cpu affinity mask.
  32. // TODO(rjnagal): Add a library to convert mask string to set of cpu bitmask.
  33. Mask string `json:"mask,omitempty"`
  34. // CPUQuota Default is disabled
  35. Quota uint64 `json:"quota,omitempty"`
  36. // Period is the CPU reference time in ns e.g the quota is compared against this.
  37. Period uint64 `json:"period,omitempty"`
  38. }
  39. type MemorySpec struct {
  40. // The amount of memory requested. Default is unlimited (-1).
  41. // Units: bytes.
  42. Limit uint64 `json:"limit,omitempty"`
  43. // The amount of guaranteed memory. Default is 0.
  44. // Units: bytes.
  45. Reservation uint64 `json:"reservation,omitempty"`
  46. // The amount of swap space requested. Default is unlimited (-1).
  47. // Units: bytes.
  48. SwapLimit uint64 `json:"swap_limit,omitempty"`
  49. }
  50. type ContainerInfo struct {
  51. // Describes the container.
  52. Spec ContainerSpec `json:"spec,omitempty"`
  53. // Historical statistics gathered from the container.
  54. Stats []*ContainerStats `json:"stats,omitempty"`
  55. }
  56. type ContainerSpec struct {
  57. // Time at which the container was created.
  58. CreationTime time.Time `json:"creation_time,omitempty"`
  59. // Other names by which the container is known within a certain namespace.
  60. // This is unique within that namespace.
  61. Aliases []string `json:"aliases,omitempty"`
  62. // Namespace under which the aliases of a container are unique.
  63. // An example of a namespace is "docker" for Docker containers.
  64. Namespace string `json:"namespace,omitempty"`
  65. // Metadata labels associated with this container.
  66. Labels map[string]string `json:"labels,omitempty"`
  67. // Metadata envs associated with this container. Only whitelisted envs are added.
  68. Envs map[string]string `json:"envs,omitempty"`
  69. HasCpu bool `json:"has_cpu"`
  70. Cpu CpuSpec `json:"cpu,omitempty"`
  71. HasMemory bool `json:"has_memory"`
  72. Memory MemorySpec `json:"memory,omitempty"`
  73. HasCustomMetrics bool `json:"has_custom_metrics"`
  74. CustomMetrics []v1.MetricSpec `json:"custom_metrics,omitempty"`
  75. // Following resources have no associated spec, but are being isolated.
  76. HasNetwork bool `json:"has_network"`
  77. HasFilesystem bool `json:"has_filesystem"`
  78. HasDiskIo bool `json:"has_diskio"`
  79. // Image name used for this container.
  80. Image string `json:"image,omitempty"`
  81. }
  82. type DeprecatedContainerStats struct {
  83. // The time of this stat point.
  84. Timestamp time.Time `json:"timestamp"`
  85. // CPU statistics
  86. HasCpu bool `json:"has_cpu"`
  87. // In nanoseconds (aggregated)
  88. Cpu v1.CpuStats `json:"cpu,omitempty"`
  89. // In nanocores per second (instantaneous)
  90. CpuInst *CpuInstStats `json:"cpu_inst,omitempty"`
  91. // Disk IO statistics
  92. HasDiskIo bool `json:"has_diskio"`
  93. DiskIo v1.DiskIoStats `json:"diskio,omitempty"`
  94. // Memory statistics
  95. HasMemory bool `json:"has_memory"`
  96. Memory v1.MemoryStats `json:"memory,omitempty"`
  97. // Network statistics
  98. HasNetwork bool `json:"has_network"`
  99. Network NetworkStats `json:"network,omitempty"`
  100. // Filesystem statistics
  101. HasFilesystem bool `json:"has_filesystem"`
  102. Filesystem []v1.FsStats `json:"filesystem,omitempty"`
  103. // Task load statistics
  104. HasLoad bool `json:"has_load"`
  105. Load v1.LoadStats `json:"load_stats,omitempty"`
  106. // Custom Metrics
  107. HasCustomMetrics bool `json:"has_custom_metrics"`
  108. CustomMetrics map[string][]v1.MetricVal `json:"custom_metrics,omitempty"`
  109. }
  110. type ContainerStats struct {
  111. // The time of this stat point.
  112. Timestamp time.Time `json:"timestamp"`
  113. // CPU statistics
  114. // In nanoseconds (aggregated)
  115. Cpu *v1.CpuStats `json:"cpu,omitempty"`
  116. // In nanocores per second (instantaneous)
  117. CpuInst *CpuInstStats `json:"cpu_inst,omitempty"`
  118. // Disk IO statistics
  119. DiskIo *v1.DiskIoStats `json:"diskio,omitempty"`
  120. // Memory statistics
  121. Memory *v1.MemoryStats `json:"memory,omitempty"`
  122. // Network statistics
  123. Network *NetworkStats `json:"network,omitempty"`
  124. // Filesystem statistics
  125. Filesystem *FilesystemStats `json:"filesystem,omitempty"`
  126. // Task load statistics
  127. Load *v1.LoadStats `json:"load_stats,omitempty"`
  128. // Metrics for Accelerators. Each Accelerator corresponds to one element in the array.
  129. Accelerators []v1.AcceleratorStats `json:"accelerators,omitempty"`
  130. // Custom Metrics
  131. CustomMetrics map[string][]v1.MetricVal `json:"custom_metrics,omitempty"`
  132. }
  133. type Percentiles struct {
  134. // Indicates whether the stats are present or not.
  135. // If true, values below do not have any data.
  136. Present bool `json:"present"`
  137. // Average over the collected sample.
  138. Mean uint64 `json:"mean"`
  139. // Max seen over the collected sample.
  140. Max uint64 `json:"max"`
  141. // 50th percentile over the collected sample.
  142. Fifty uint64 `json:"fifty"`
  143. // 90th percentile over the collected sample.
  144. Ninety uint64 `json:"ninety"`
  145. // 95th percentile over the collected sample.
  146. NinetyFive uint64 `json:"ninetyfive"`
  147. }
  148. type Usage struct {
  149. // Indicates amount of data available [0-100].
  150. // If we have data for half a day, we'll still process DayUsage,
  151. // but set PercentComplete to 50.
  152. PercentComplete int32 `json:"percent_complete"`
  153. // Mean, Max, and 90p cpu rate value in milliCpus/seconds. Converted to milliCpus to avoid floats.
  154. Cpu Percentiles `json:"cpu"`
  155. // Mean, Max, and 90p memory size in bytes.
  156. Memory Percentiles `json:"memory"`
  157. }
  158. // latest sample collected for a container.
  159. type InstantUsage struct {
  160. // cpu rate in cpu milliseconds/second.
  161. Cpu uint64 `json:"cpu"`
  162. // Memory usage in bytes.
  163. Memory uint64 `json:"memory"`
  164. }
  165. type DerivedStats struct {
  166. // Time of generation of these stats.
  167. Timestamp time.Time `json:"timestamp"`
  168. // Latest instantaneous sample.
  169. LatestUsage InstantUsage `json:"latest_usage"`
  170. // Percentiles in last observed minute.
  171. MinuteUsage Usage `json:"minute_usage"`
  172. // Percentile in last hour.
  173. HourUsage Usage `json:"hour_usage"`
  174. // Percentile in last day.
  175. DayUsage Usage `json:"day_usage"`
  176. }
  177. type FsInfo struct {
  178. // Time of generation of these stats.
  179. Timestamp time.Time `json:"timestamp"`
  180. // The block device name associated with the filesystem.
  181. Device string `json:"device"`
  182. // Path where the filesystem is mounted.
  183. Mountpoint string `json:"mountpoint"`
  184. // Filesystem usage in bytes.
  185. Capacity uint64 `json:"capacity"`
  186. // Bytes available for non-root use.
  187. Available uint64 `json:"available"`
  188. // Number of bytes used on this filesystem.
  189. Usage uint64 `json:"usage"`
  190. // Labels associated with this filesystem.
  191. Labels []string `json:"labels"`
  192. // Number of Inodes.
  193. Inodes *uint64 `json:"inodes,omitempty"`
  194. // Number of available Inodes (if known)
  195. InodesFree *uint64 `json:"inodes_free,omitempty"`
  196. }
  197. type RequestOptions struct {
  198. // Type of container identifier specified - "name", "dockerid", dockeralias"
  199. IdType string `json:"type"`
  200. // Number of stats to return
  201. Count int `json:"count"`
  202. // Whether to include stats for child subcontainers.
  203. Recursive bool `json:"recursive"`
  204. // Update stats if they are older than MaxAge
  205. // nil indicates no update, and 0 will always trigger an update.
  206. MaxAge *time.Duration `json:"max_age"`
  207. }
  208. type ProcessInfo struct {
  209. User string `json:"user"`
  210. Pid int `json:"pid"`
  211. Ppid int `json:"parent_pid"`
  212. StartTime string `json:"start_time"`
  213. PercentCpu float32 `json:"percent_cpu"`
  214. PercentMemory float32 `json:"percent_mem"`
  215. RSS uint64 `json:"rss"`
  216. VirtualSize uint64 `json:"virtual_size"`
  217. Status string `json:"status"`
  218. RunningTime string `json:"running_time"`
  219. CgroupPath string `json:"cgroup_path"`
  220. Cmd string `json:"cmd"`
  221. FdCount int `json:"fd_count"`
  222. }
  223. type TcpStat struct {
  224. Established uint64
  225. SynSent uint64
  226. SynRecv uint64
  227. FinWait1 uint64
  228. FinWait2 uint64
  229. TimeWait uint64
  230. Close uint64
  231. CloseWait uint64
  232. LastAck uint64
  233. Listen uint64
  234. Closing uint64
  235. }
  236. type NetworkStats struct {
  237. // Network stats by interface.
  238. Interfaces []v1.InterfaceStats `json:"interfaces,omitempty"`
  239. // TCP connection stats (Established, Listen...)
  240. Tcp TcpStat `json:"tcp"`
  241. // TCP6 connection stats (Established, Listen...)
  242. Tcp6 TcpStat `json:"tcp6"`
  243. // UDP connection stats
  244. Udp v1.UdpStat `json:"udp"`
  245. // UDP6 connection stats
  246. Udp6 v1.UdpStat `json:"udp6"`
  247. }
  248. // Instantaneous CPU stats
  249. type CpuInstStats struct {
  250. Usage CpuInstUsage `json:"usage"`
  251. }
  252. // CPU usage time statistics.
  253. type CpuInstUsage struct {
  254. // Total CPU usage.
  255. // Units: nanocores per second
  256. Total uint64 `json:"total"`
  257. // Per CPU/core usage of the container.
  258. // Unit: nanocores per second
  259. PerCpu []uint64 `json:"per_cpu_usage,omitempty"`
  260. // Time spent in user space.
  261. // Unit: nanocores per second
  262. User uint64 `json:"user"`
  263. // Time spent in kernel space.
  264. // Unit: nanocores per second
  265. System uint64 `json:"system"`
  266. }
  267. // Filesystem usage statistics.
  268. type FilesystemStats struct {
  269. // Total Number of bytes consumed by container.
  270. TotalUsageBytes *uint64 `json:"totalUsageBytes,omitempty"`
  271. // Number of bytes consumed by a container through its root filesystem.
  272. BaseUsageBytes *uint64 `json:"baseUsageBytes,omitempty"`
  273. // Number of inodes used within the container's root filesystem.
  274. // This only accounts for inodes that are shared across containers,
  275. // and does not include inodes used in mounted directories.
  276. InodeUsage *uint64 `json:"containter_inode_usage,omitempty"`
  277. }