1// +build linux
2
3package cgroups
4
5type ThrottlingData struct {
6	// Number of periods with throttling active
7	Periods uint64 `json:"periods,omitempty"`
8	// Number of periods when the container hit its throttling limit.
9	ThrottledPeriods uint64 `json:"throttled_periods,omitempty"`
10	// Aggregate time the container was throttled for in nanoseconds.
11	ThrottledTime uint64 `json:"throttled_time,omitempty"`
12}
13
14// CpuUsage denotes the usage of a CPU.
15// All CPU stats are aggregate since container inception.
16type CpuUsage struct {
17	// Total CPU time consumed.
18	// Units: nanoseconds.
19	TotalUsage uint64 `json:"total_usage,omitempty"`
20	// Total CPU time consumed per core.
21	// Units: nanoseconds.
22	PercpuUsage []uint64 `json:"percpu_usage,omitempty"`
23	// CPU time consumed per core in kernel mode
24	// Units: nanoseconds.
25	PercpuUsageInKernelmode []uint64 `json:"percpu_usage_in_kernelmode"`
26	// CPU time consumed per core in user mode
27	// Units: nanoseconds.
28	PercpuUsageInUsermode []uint64 `json:"percpu_usage_in_usermode"`
29	// Time spent by tasks of the cgroup in kernel mode.
30	// Units: nanoseconds.
31	UsageInKernelmode uint64 `json:"usage_in_kernelmode"`
32	// Time spent by tasks of the cgroup in user mode.
33	// Units: nanoseconds.
34	UsageInUsermode uint64 `json:"usage_in_usermode"`
35}
36
37type CpuStats struct {
38	CpuUsage       CpuUsage       `json:"cpu_usage,omitempty"`
39	ThrottlingData ThrottlingData `json:"throttling_data,omitempty"`
40}
41
42type CPUSetStats struct {
43	// List of the physical numbers of the CPUs on which processes
44	// in that cpuset are allowed to execute
45	CPUs []uint16 `json:"cpus,omitempty"`
46	// cpu_exclusive flag
47	CPUExclusive uint64 `json:"cpu_exclusive"`
48	// List of memory nodes on which processes in that cpuset
49	// are allowed to allocate memory
50	Mems []uint16 `json:"mems,omitempty"`
51	// mem_hardwall flag
52	MemHardwall uint64 `json:"mem_hardwall"`
53	// mem_exclusive flag
54	MemExclusive uint64 `json:"mem_exclusive"`
55	// memory_migrate flag
56	MemoryMigrate uint64 `json:"memory_migrate"`
57	// memory_spread page flag
58	MemorySpreadPage uint64 `json:"memory_spread_page"`
59	// memory_spread slab flag
60	MemorySpreadSlab uint64 `json:"memory_spread_slab"`
61	// memory_pressure
62	MemoryPressure uint64 `json:"memory_pressure"`
63	// sched_load balance flag
64	SchedLoadBalance uint64 `json:"sched_load_balance"`
65	// sched_relax_domain_level
66	SchedRelaxDomainLevel int64 `json:"sched_relax_domain_level"`
67}
68
69type MemoryData struct {
70	Usage    uint64 `json:"usage,omitempty"`
71	MaxUsage uint64 `json:"max_usage,omitempty"`
72	Failcnt  uint64 `json:"failcnt"`
73	Limit    uint64 `json:"limit"`
74}
75
76type MemoryStats struct {
77	// memory used for cache
78	Cache uint64 `json:"cache,omitempty"`
79	// usage of memory
80	Usage MemoryData `json:"usage,omitempty"`
81	// usage of memory + swap
82	SwapUsage MemoryData `json:"swap_usage,omitempty"`
83	// usage of kernel memory
84	KernelUsage MemoryData `json:"kernel_usage,omitempty"`
85	// usage of kernel TCP memory
86	KernelTCPUsage MemoryData `json:"kernel_tcp_usage,omitempty"`
87	// usage of memory pages by NUMA node
88	// see chapter 5.6 of memory controller documentation
89	PageUsageByNUMA PageUsageByNUMA `json:"page_usage_by_numa,omitempty"`
90	// if true, memory usage is accounted for throughout a hierarchy of cgroups.
91	UseHierarchy bool `json:"use_hierarchy"`
92
93	Stats map[string]uint64 `json:"stats,omitempty"`
94}
95
96type PageUsageByNUMA struct {
97	// Embedding is used as types can't be recursive.
98	PageUsageByNUMAInner
99	Hierarchical PageUsageByNUMAInner `json:"hierarchical,omitempty"`
100}
101
102type PageUsageByNUMAInner struct {
103	Total       PageStats `json:"total,omitempty"`
104	File        PageStats `json:"file,omitempty"`
105	Anon        PageStats `json:"anon,omitempty"`
106	Unevictable PageStats `json:"unevictable,omitempty"`
107}
108
109type PageStats struct {
110	Total uint64           `json:"total,omitempty"`
111	Nodes map[uint8]uint64 `json:"nodes,omitempty"`
112}
113
114type PidsStats struct {
115	// number of pids in the cgroup
116	Current uint64 `json:"current,omitempty"`
117	// active pids hard limit
118	Limit uint64 `json:"limit,omitempty"`
119}
120
121type BlkioStatEntry struct {
122	Major uint64 `json:"major,omitempty"`
123	Minor uint64 `json:"minor,omitempty"`
124	Op    string `json:"op,omitempty"`
125	Value uint64 `json:"value,omitempty"`
126}
127
128type BlkioStats struct {
129	// number of bytes tranferred to and from the block device
130	IoServiceBytesRecursive []BlkioStatEntry `json:"io_service_bytes_recursive,omitempty"`
131	IoServicedRecursive     []BlkioStatEntry `json:"io_serviced_recursive,omitempty"`
132	IoQueuedRecursive       []BlkioStatEntry `json:"io_queue_recursive,omitempty"`
133	IoServiceTimeRecursive  []BlkioStatEntry `json:"io_service_time_recursive,omitempty"`
134	IoWaitTimeRecursive     []BlkioStatEntry `json:"io_wait_time_recursive,omitempty"`
135	IoMergedRecursive       []BlkioStatEntry `json:"io_merged_recursive,omitempty"`
136	IoTimeRecursive         []BlkioStatEntry `json:"io_time_recursive,omitempty"`
137	SectorsRecursive        []BlkioStatEntry `json:"sectors_recursive,omitempty"`
138}
139
140type HugetlbStats struct {
141	// current res_counter usage for hugetlb
142	Usage uint64 `json:"usage,omitempty"`
143	// maximum usage ever recorded.
144	MaxUsage uint64 `json:"max_usage,omitempty"`
145	// number of times hugetlb usage allocation failure.
146	Failcnt uint64 `json:"failcnt"`
147}
148
149type Stats struct {
150	CpuStats    CpuStats    `json:"cpu_stats,omitempty"`
151	CPUSetStats CPUSetStats `json:"cpuset_stats,omitempty"`
152	MemoryStats MemoryStats `json:"memory_stats,omitempty"`
153	PidsStats   PidsStats   `json:"pids_stats,omitempty"`
154	BlkioStats  BlkioStats  `json:"blkio_stats,omitempty"`
155	// the map is in the format "size of hugepage: stats of the hugepage"
156	HugetlbStats map[string]HugetlbStats `json:"hugetlb_stats,omitempty"`
157}
158
159func NewStats() *Stats {
160	memoryStats := MemoryStats{Stats: make(map[string]uint64)}
161	hugetlbStats := make(map[string]HugetlbStats)
162	return &Stats{MemoryStats: memoryStats, HugetlbStats: hugetlbStats}
163}
164