Docker 容器统计信息结构体解析 - Stats 和 CPUUsage 结构体详解

本文档将详细解析 Docker 容器的 Stats 结构体,它包含了容器的各种类型的统计信息。

Stats 结构体

Stats 结构体聚合了所有类型的容器统计信息,主要包含以下字段:

  • 通用统计信息

    • Read: 时间戳,表示读取统计信息的时间。
    • PreRead: 时间戳,表示上一次读取统计信息的时间。
  • Linux 特定统计信息 (Windows 上不可用)

    • PidsStats: 用于存储与进程相关的统计数据。
    • BlkioStats: 用于存储与块IO相关的统计数据。
  • Windows 特定统计信息 (Linux 上不可用)

    • NumProcs: 表示容器中的进程数量。
    • StorageStats: 用于存储与存储相关的统计数据。
  • 共享统计信息

    • CPUStats: 包含了容器自创建以来的所有CPU统计数据。
    • PreCPUStats: 表示上一次读取统计信息时的CPU统计数据。
    • MemoryStats: 用于存储与内存相关的统计数据。

CPUUsage 结构体

CPUUsage 结构体是 CPUStats 结构体中的一个字段,用于存储 CPU 的使用情况。它包含以下字段:

  • TotalUsage: 表示自容器创建以来消耗的总 CPU 时间。

  • PercpuUsage: 一个数组,表示每个核心消耗的 CPU 时间(仅在 Linux 上使用)。

  • UsageInKernelmode: 表示在内核模式下消耗的时间(仅在 Linux 上使用)。

  • UsageInUsermode: 表示在用户模式下消耗的时间(仅在 Linux 上使用)。

PreCPUStats 和 CPUStats

PreCPUStatsCPUStats 分别表示前一次和当前的 CPU 统计数据。通过对比这两个结构体,可以了解容器的 CPU 使用情况的变化趋势。

代码示例

以下代码示例展示了 Stats 结构体和 CPUUsage 结构体的定义:

// Stats is Ultimate struct aggregating all types of stats of one container
type Stats struct {
	// Common stats
	Read    time.Time `json:"read"`
	PreRead time.Time `json:"preread"`

	// Linux specific stats, not populated on Windows.
	PidsStats  PidsStats  `json:"pids_stats,omitempty"`
	BlkioStats BlkioStats `json:"blkio_stats,omitempty"`

	// Windows specific stats, not populated on Linux.
	NumProcs     uint32       `json:"num_procs"`
	StorageStats StorageStats `json:"storage_stats,omitempty"`

	// Shared stats
	CPUStats    CPUStats    `json:"cpu_stats,omitempty"`
	PreCPUStats CPUStats    `json:"precpu_stats,omitempty"` // "Pre"="Previous"
	MemoryStats MemoryStats `json:"memory_stats,omitempty"`
}

// CPUUsage stores All CPU stats aggregated since container inception.
type CPUUsage struct {
	// Total CPU time consumed.
	// Units: nanoseconds (Linux)
	// Units: 100's of nanoseconds (Windows)
	TotalUsage uint64 `json:"total_usage"`

	// Total CPU time consumed per core (Linux). Not used on Windows.
	// Units: nanoseconds.
	PercpuUsage []uint64 `json:"percpu_usage,omitempty"`

	// Time spent by tasks of the cgroup in kernel mode (Linux).
	// Time spent by all container processes in kernel mode (Windows).
	// Units: nanoseconds (Linux).
	// Units: 100's of nanoseconds (Windows). Not populated for Hyper-V Containers.
	UsageInKernelmode uint64 `json:"usage_in_kernelmode"`

	// Time spent by tasks of the cgroup in user mode (Linux).
	// Time spent by all container processes in user mode (Windows).
	// Units: nanoseconds (Linux).
	// Units: 100's of nanoseconds (Windows). Not populated for Hyper-V Containers
	UsageInUsermode uint64 `json:"usage_in_usermode"`
}

总结

Stats 结构体提供了对 Docker 容器的全面统计信息,而 CPUUsage 结构体则详细记录了容器的 CPU 使用情况。了解这些结构体有助于更好地理解容器的运行状态,进而更好地优化容器的性能。

Docker 容器统计信息结构体解析 - Stats 和 CPUUsage 结构体详解

原文地址: http://www.cveoy.top/t/topic/fAPV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录