machine.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. package v1
  15. import "time"
  16. type FsInfo struct {
  17. // Block device associated with the filesystem.
  18. Device string `json:"device"`
  19. // DeviceMajor is the major identifier of the device, used for correlation with blkio stats
  20. DeviceMajor uint64 `json:"-"`
  21. // DeviceMinor is the minor identifier of the device, used for correlation with blkio stats
  22. DeviceMinor uint64 `json:"-"`
  23. // Total number of bytes available on the filesystem.
  24. Capacity uint64 `json:"capacity"`
  25. // Type of device.
  26. Type string `json:"type"`
  27. // Total number of inodes available on the filesystem.
  28. Inodes uint64 `json:"inodes"`
  29. // HasInodes when true, indicates that Inodes info will be available.
  30. HasInodes bool `json:"has_inodes"`
  31. }
  32. type Node struct {
  33. Id int `json:"node_id"`
  34. // Per-node memory
  35. Memory uint64 `json:"memory"`
  36. HugePages []HugePagesInfo `json:"hugepages"`
  37. Cores []Core `json:"cores"`
  38. Caches []Cache `json:"caches"`
  39. Distances []uint64 `json:"distances"`
  40. }
  41. type Core struct {
  42. Id int `json:"core_id"`
  43. Threads []int `json:"thread_ids"`
  44. Caches []Cache `json:"caches"`
  45. UncoreCaches []Cache `json:"uncore_caches"`
  46. SocketID int `json:"socket_id"`
  47. }
  48. type Cache struct {
  49. // Id of memory cache
  50. Id int `json:"id"`
  51. // Size of memory cache in bytes.
  52. Size uint64 `json:"size"`
  53. // Type of memory cache: data, instruction, or unified.
  54. Type string `json:"type"`
  55. // Level (distance from cpus) in a multi-level cache hierarchy.
  56. Level int `json:"level"`
  57. }
  58. func (n *Node) FindCore(id int) (bool, int) {
  59. for i, n := range n.Cores {
  60. if n.Id == id {
  61. return true, i
  62. }
  63. }
  64. return false, -1
  65. }
  66. // FindCoreByThread returns bool if found Core with same thread as provided and it's index in Node Core array.
  67. // If it's not found, returns false and -1.
  68. func (n *Node) FindCoreByThread(thread int) (bool, int) {
  69. for i, n := range n.Cores {
  70. for _, t := range n.Threads {
  71. if t == thread {
  72. return true, i
  73. }
  74. }
  75. }
  76. return false, -1
  77. }
  78. func (n *Node) AddThread(thread int, core int) {
  79. var coreIdx int
  80. if core == -1 {
  81. // Assume one hyperthread per core when topology data is missing.
  82. core = thread
  83. }
  84. ok, coreIdx := n.FindCore(core)
  85. if !ok {
  86. // New core
  87. core := Core{Id: core}
  88. n.Cores = append(n.Cores, core)
  89. coreIdx = len(n.Cores) - 1
  90. }
  91. n.Cores[coreIdx].Threads = append(n.Cores[coreIdx].Threads, thread)
  92. }
  93. func (n *Node) AddNodeCache(c Cache) {
  94. n.Caches = append(n.Caches, c)
  95. }
  96. func (n *Node) AddPerCoreCache(c Cache) {
  97. for idx := range n.Cores {
  98. n.Cores[idx].Caches = append(n.Cores[idx].Caches, c)
  99. }
  100. }
  101. type HugePagesInfo struct {
  102. // huge page size (in kB)
  103. PageSize uint64 `json:"page_size"`
  104. // number of huge pages
  105. NumPages uint64 `json:"num_pages"`
  106. }
  107. type DiskInfo struct {
  108. // device name
  109. Name string `json:"name"`
  110. // Major number
  111. Major uint64 `json:"major"`
  112. // Minor number
  113. Minor uint64 `json:"minor"`
  114. // Size in bytes
  115. Size uint64 `json:"size"`
  116. // I/O Scheduler - one of "none", "noop", "cfq", "deadline"
  117. Scheduler string `json:"scheduler"`
  118. }
  119. type NetInfo struct {
  120. // Device name
  121. Name string `json:"name"`
  122. // Mac Address
  123. MacAddress string `json:"mac_address"`
  124. // Speed in MBits/s
  125. Speed int64 `json:"speed"`
  126. // Maximum Transmission Unit
  127. Mtu int64 `json:"mtu"`
  128. }
  129. type CloudProvider string
  130. const (
  131. GCE CloudProvider = "GCE"
  132. AWS CloudProvider = "AWS"
  133. Azure CloudProvider = "Azure"
  134. UnknownProvider CloudProvider = "Unknown"
  135. )
  136. type InstanceType string
  137. const (
  138. UnknownInstance = "Unknown"
  139. )
  140. type InstanceID string
  141. const (
  142. UnNamedInstance InstanceID = "None"
  143. )
  144. type MachineInfo struct {
  145. // The time of this information point.
  146. Timestamp time.Time `json:"timestamp"`
  147. // Vendor id of CPU.
  148. CPUVendorID string `json:"vendor_id"`
  149. // The number of cores in this machine.
  150. NumCores int `json:"num_cores"`
  151. // The number of physical cores in this machine.
  152. NumPhysicalCores int `json:"num_physical_cores"`
  153. // The number of cpu sockets in this machine.
  154. NumSockets int `json:"num_sockets"`
  155. // Maximum clock speed for the cores, in KHz.
  156. CpuFrequency uint64 `json:"cpu_frequency_khz"`
  157. // The amount of memory (in bytes) in this machine
  158. MemoryCapacity uint64 `json:"memory_capacity"`
  159. // Memory capacity and number of DIMMs by memory type
  160. MemoryByType map[string]*MemoryInfo `json:"memory_by_type"`
  161. NVMInfo NVMInfo `json:"nvm"`
  162. // HugePages on this machine.
  163. HugePages []HugePagesInfo `json:"hugepages"`
  164. // The machine id
  165. MachineID string `json:"machine_id"`
  166. // The system uuid
  167. SystemUUID string `json:"system_uuid"`
  168. // The boot id
  169. BootID string `json:"boot_id"`
  170. // Filesystems on this machine.
  171. Filesystems []FsInfo `json:"filesystems"`
  172. // Disk map
  173. DiskMap map[string]DiskInfo `json:"disk_map"`
  174. // Network devices
  175. NetworkDevices []NetInfo `json:"network_devices"`
  176. // Machine Topology
  177. // Describes cpu/memory layout and hierarchy.
  178. Topology []Node `json:"topology"`
  179. // Cloud provider the machine belongs to.
  180. CloudProvider CloudProvider `json:"cloud_provider"`
  181. // Type of cloud instance (e.g. GCE standard) the machine is.
  182. InstanceType InstanceType `json:"instance_type"`
  183. // ID of cloud instance (e.g. instance-1) given to it by the cloud provider.
  184. InstanceID InstanceID `json:"instance_id"`
  185. }
  186. func (m *MachineInfo) Clone() *MachineInfo {
  187. memoryByType := m.MemoryByType
  188. if len(m.MemoryByType) > 0 {
  189. memoryByType = make(map[string]*MemoryInfo)
  190. for memoryType, memoryInfo := range m.MemoryByType {
  191. memoryByType[memoryType] = memoryInfo
  192. }
  193. }
  194. diskMap := m.DiskMap
  195. if len(m.DiskMap) > 0 {
  196. diskMap = make(map[string]DiskInfo)
  197. for k, info := range m.DiskMap {
  198. diskMap[k] = info
  199. }
  200. }
  201. copy := MachineInfo{
  202. CPUVendorID: m.CPUVendorID,
  203. Timestamp: m.Timestamp,
  204. NumCores: m.NumCores,
  205. NumPhysicalCores: m.NumPhysicalCores,
  206. NumSockets: m.NumSockets,
  207. CpuFrequency: m.CpuFrequency,
  208. MemoryCapacity: m.MemoryCapacity,
  209. MemoryByType: memoryByType,
  210. NVMInfo: m.NVMInfo,
  211. HugePages: m.HugePages,
  212. MachineID: m.MachineID,
  213. SystemUUID: m.SystemUUID,
  214. BootID: m.BootID,
  215. Filesystems: m.Filesystems,
  216. DiskMap: diskMap,
  217. NetworkDevices: m.NetworkDevices,
  218. Topology: m.Topology,
  219. CloudProvider: m.CloudProvider,
  220. InstanceType: m.InstanceType,
  221. InstanceID: m.InstanceID,
  222. }
  223. return &copy
  224. }
  225. type MemoryInfo struct {
  226. // The amount of memory (in bytes).
  227. Capacity uint64 `json:"capacity"`
  228. // Number of memory DIMMs.
  229. DimmCount uint `json:"dimm_count"`
  230. }
  231. type NVMInfo struct {
  232. // The total NVM capacity in bytes for memory mode.
  233. MemoryModeCapacity uint64 `json:"memory_mode_capacity"`
  234. //The total NVM capacity in bytes for app direct mode.
  235. AppDirectModeCapacity uint64 `json:"app direct_mode_capacity"`
  236. // Average power budget in watts for NVM devices configured in BIOS.
  237. AvgPowerBudget uint `json:"avg_power_budget"`
  238. }
  239. type VersionInfo struct {
  240. // Kernel version.
  241. KernelVersion string `json:"kernel_version"`
  242. // OS image being used for cadvisor container, or host image if running on host directly.
  243. ContainerOsVersion string `json:"container_os_version"`
  244. // Docker version.
  245. DockerVersion string `json:"docker_version"`
  246. // Docker API Version
  247. DockerAPIVersion string `json:"docker_api_version"`
  248. // cAdvisor version.
  249. CadvisorVersion string `json:"cadvisor_version"`
  250. // cAdvisor git revision.
  251. CadvisorRevision string `json:"cadvisor_revision"`
  252. }
  253. type MachineInfoFactory interface {
  254. GetMachineInfo() (*MachineInfo, error)
  255. GetVersionInfo() (*VersionInfo, error)
  256. }