container.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. HasProcesses bool `json:"has_processes"`
  76. Processes v1.ProcessSpec `json:"processes,omitempty"`
  77. // Following resources have no associated spec, but are being isolated.
  78. HasNetwork bool `json:"has_network"`
  79. HasFilesystem bool `json:"has_filesystem"`
  80. HasDiskIo bool `json:"has_diskio"`
  81. // Image name used for this container.
  82. Image string `json:"image,omitempty"`
  83. }
  84. type DeprecatedContainerStats struct {
  85. // The time of this stat point.
  86. Timestamp time.Time `json:"timestamp"`
  87. // CPU statistics
  88. HasCpu bool `json:"has_cpu"`
  89. // In nanoseconds (aggregated)
  90. Cpu v1.CpuStats `json:"cpu,omitempty"`
  91. // In nanocores per second (instantaneous)
  92. CpuInst *CpuInstStats `json:"cpu_inst,omitempty"`
  93. // Disk IO statistics
  94. HasDiskIo bool `json:"has_diskio"`
  95. DiskIo v1.DiskIoStats `json:"diskio,omitempty"`
  96. // Memory statistics
  97. HasMemory bool `json:"has_memory"`
  98. Memory v1.MemoryStats `json:"memory,omitempty"`
  99. // Network statistics
  100. HasNetwork bool `json:"has_network"`
  101. Network NetworkStats `json:"network,omitempty"`
  102. // Processes statistics
  103. HasProcesses bool `json:"has_processes"`
  104. Processes v1.ProcessStats `json:"processes,omitempty"`
  105. // Filesystem statistics
  106. HasFilesystem bool `json:"has_filesystem"`
  107. Filesystem []v1.FsStats `json:"filesystem,omitempty"`
  108. // Task load statistics
  109. HasLoad bool `json:"has_load"`
  110. Load v1.LoadStats `json:"load_stats,omitempty"`
  111. // Custom Metrics
  112. HasCustomMetrics bool `json:"has_custom_metrics"`
  113. CustomMetrics map[string][]v1.MetricVal `json:"custom_metrics,omitempty"`
  114. }
  115. type ContainerStats struct {
  116. // The time of this stat point.
  117. Timestamp time.Time `json:"timestamp"`
  118. // CPU statistics
  119. // In nanoseconds (aggregated)
  120. Cpu *v1.CpuStats `json:"cpu,omitempty"`
  121. // In nanocores per second (instantaneous)
  122. CpuInst *CpuInstStats `json:"cpu_inst,omitempty"`
  123. // Disk IO statistics
  124. DiskIo *v1.DiskIoStats `json:"diskio,omitempty"`
  125. // Memory statistics
  126. Memory *v1.MemoryStats `json:"memory,omitempty"`
  127. // Network statistics
  128. Network *NetworkStats `json:"network,omitempty"`
  129. // Processes statistics
  130. Processes *v1.ProcessStats `json:"processes,omitempty"`
  131. // Filesystem statistics
  132. Filesystem *FilesystemStats `json:"filesystem,omitempty"`
  133. // Task load statistics
  134. Load *v1.LoadStats `json:"load_stats,omitempty"`
  135. // Metrics for Accelerators. Each Accelerator corresponds to one element in the array.
  136. Accelerators []v1.AcceleratorStats `json:"accelerators,omitempty"`
  137. // Custom Metrics
  138. CustomMetrics map[string][]v1.MetricVal `json:"custom_metrics,omitempty"`
  139. }
  140. type Percentiles struct {
  141. // Indicates whether the stats are present or not.
  142. // If true, values below do not have any data.
  143. Present bool `json:"present"`
  144. // Average over the collected sample.
  145. Mean uint64 `json:"mean"`
  146. // Max seen over the collected sample.
  147. Max uint64 `json:"max"`
  148. // 50th percentile over the collected sample.
  149. Fifty uint64 `json:"fifty"`
  150. // 90th percentile over the collected sample.
  151. Ninety uint64 `json:"ninety"`
  152. // 95th percentile over the collected sample.
  153. NinetyFive uint64 `json:"ninetyfive"`
  154. }
  155. type Usage struct {
  156. // Indicates amount of data available [0-100].
  157. // If we have data for half a day, we'll still process DayUsage,
  158. // but set PercentComplete to 50.
  159. PercentComplete int32 `json:"percent_complete"`
  160. // Mean, Max, and 90p cpu rate value in milliCpus/seconds. Converted to milliCpus to avoid floats.
  161. Cpu Percentiles `json:"cpu"`
  162. // Mean, Max, and 90p memory size in bytes.
  163. Memory Percentiles `json:"memory"`
  164. }
  165. // latest sample collected for a container.
  166. type InstantUsage struct {
  167. // cpu rate in cpu milliseconds/second.
  168. Cpu uint64 `json:"cpu"`
  169. // Memory usage in bytes.
  170. Memory uint64 `json:"memory"`
  171. }
  172. type DerivedStats struct {
  173. // Time of generation of these stats.
  174. Timestamp time.Time `json:"timestamp"`
  175. // Latest instantaneous sample.
  176. LatestUsage InstantUsage `json:"latest_usage"`
  177. // Percentiles in last observed minute.
  178. MinuteUsage Usage `json:"minute_usage"`
  179. // Percentile in last hour.
  180. HourUsage Usage `json:"hour_usage"`
  181. // Percentile in last day.
  182. DayUsage Usage `json:"day_usage"`
  183. }
  184. type FsInfo struct {
  185. // Time of generation of these stats.
  186. Timestamp time.Time `json:"timestamp"`
  187. // The block device name associated with the filesystem.
  188. Device string `json:"device"`
  189. // Path where the filesystem is mounted.
  190. Mountpoint string `json:"mountpoint"`
  191. // Filesystem usage in bytes.
  192. Capacity uint64 `json:"capacity"`
  193. // Bytes available for non-root use.
  194. Available uint64 `json:"available"`
  195. // Number of bytes used on this filesystem.
  196. Usage uint64 `json:"usage"`
  197. // Labels associated with this filesystem.
  198. Labels []string `json:"labels"`
  199. // Number of Inodes.
  200. Inodes *uint64 `json:"inodes,omitempty"`
  201. // Number of available Inodes (if known)
  202. InodesFree *uint64 `json:"inodes_free,omitempty"`
  203. }
  204. type RequestOptions struct {
  205. // Type of container identifier specified - "name", "dockerid", dockeralias"
  206. IdType string `json:"type"`
  207. // Number of stats to return
  208. Count int `json:"count"`
  209. // Whether to include stats for child subcontainers.
  210. Recursive bool `json:"recursive"`
  211. // Update stats if they are older than MaxAge
  212. // nil indicates no update, and 0 will always trigger an update.
  213. MaxAge *time.Duration `json:"max_age"`
  214. }
  215. type ProcessInfo struct {
  216. User string `json:"user"`
  217. Pid int `json:"pid"`
  218. Ppid int `json:"parent_pid"`
  219. StartTime string `json:"start_time"`
  220. PercentCpu float32 `json:"percent_cpu"`
  221. PercentMemory float32 `json:"percent_mem"`
  222. RSS uint64 `json:"rss"`
  223. VirtualSize uint64 `json:"virtual_size"`
  224. Status string `json:"status"`
  225. RunningTime string `json:"running_time"`
  226. CgroupPath string `json:"cgroup_path"`
  227. Cmd string `json:"cmd"`
  228. FdCount int `json:"fd_count"`
  229. }
  230. type TcpStat struct {
  231. Established uint64
  232. SynSent uint64
  233. SynRecv uint64
  234. FinWait1 uint64
  235. FinWait2 uint64
  236. TimeWait uint64
  237. Close uint64
  238. CloseWait uint64
  239. LastAck uint64
  240. Listen uint64
  241. Closing uint64
  242. }
  243. type NetworkStats struct {
  244. // Network stats by interface.
  245. Interfaces []v1.InterfaceStats `json:"interfaces,omitempty"`
  246. // TCP connection stats (Established, Listen...)
  247. Tcp TcpStat `json:"tcp"`
  248. // TCP6 connection stats (Established, Listen...)
  249. Tcp6 TcpStat `json:"tcp6"`
  250. // UDP connection stats
  251. Udp v1.UdpStat `json:"udp"`
  252. // UDP6 connection stats
  253. Udp6 v1.UdpStat `json:"udp6"`
  254. }
  255. // Instantaneous CPU stats
  256. type CpuInstStats struct {
  257. Usage CpuInstUsage `json:"usage"`
  258. }
  259. // CPU usage time statistics.
  260. type CpuInstUsage struct {
  261. // Total CPU usage.
  262. // Units: nanocores per second
  263. Total uint64 `json:"total"`
  264. // Per CPU/core usage of the container.
  265. // Unit: nanocores per second
  266. PerCpu []uint64 `json:"per_cpu_usage,omitempty"`
  267. // Time spent in user space.
  268. // Unit: nanocores per second
  269. User uint64 `json:"user"`
  270. // Time spent in kernel space.
  271. // Unit: nanocores per second
  272. System uint64 `json:"system"`
  273. }
  274. // Filesystem usage statistics.
  275. type FilesystemStats struct {
  276. // Total Number of bytes consumed by container.
  277. TotalUsageBytes *uint64 `json:"totalUsageBytes,omitempty"`
  278. // Number of bytes consumed by a container through its root filesystem.
  279. BaseUsageBytes *uint64 `json:"baseUsageBytes,omitempty"`
  280. // Number of inodes used within the container's root filesystem.
  281. // This only accounts for inodes that are shared across containers,
  282. // and does not include inodes used in mounted directories.
  283. InodeUsage *uint64 `json:"containter_inode_usage,omitempty"`
  284. }