有没有办法读取 cgroups 用来终止进程的内存计数器?

Is there a way to read the memory counter used by cgroups to kill processes?

我是 运行 一个 cgroup 下的进程,有一个 OOM Killer。当它执行 kill 时,dmesg 输出如下消息。

[9515117.055227] Call Trace:
 [9515117.058018] [<ffffffffbb325154>] dump_stack+0x63/0x8f
 [9515117.063506] [<ffffffffbb1b2e24>] dump_header+0x65/0x1d4
 [9515117.069113] [<ffffffffbb5c8727>] ? _raw_spin_unlock_irqrestore+0x17/0x20
 [9515117.076193] [<ffffffffbb14af9d>] oom_kill_process+0x28d/0x430
 [9515117.082366] [<ffffffffbb1ae03b>] ? mem_cgroup_iter+0x1db/0x3c0
 [9515117.088578] [<ffffffffbb1b0504>] mem_cgroup_out_of_memory+0x284/0x2d0
 [9515117.095395] [<ffffffffbb1b0f95>] mem_cgroup_oom_synchronize+0x305/0x320
 [9515117.102383] [<ffffffffbb1abf50>] ? memory_high_write+0xc0/0xc0
 [9515117.108591] [<ffffffffbb14b678>] pagefault_out_of_memory+0x38/0xa0
 [9515117.115168] [<ffffffffbb0477b7>] mm_fault_error+0x77/0x150
 [9515117.121027] [<ffffffffbb047ff4>] __do_page_fault+0x414/0x420
 [9515117.127058] [<ffffffffbb048022>] do_page_fault+0x22/0x30
 [9515117.132823] [<ffffffffbb5ca8b8>] page_fault+0x28/0x30
 [9515117.330756] Memory cgroup out of memory: Kill process 13030 (java) score 1631 or sacrifice child
 [9515117.340375] Killed process 13030 (java) total-vm:18259139756kB, anon-rss:2243072kB, file-rss:30004132kB

我希望能够告诉 cgroups OOM Killer 认为进程在任何给定时间正在使用 多少内存。

这个数量有办法查询吗?

我在 official documentation for cgroup-v1 中找到了以下内容,其中显示了如何查询当前内存使用情况以及更改限制:

a. Enable CONFIG_CGROUPS
b. Enable CONFIG_MEMCG
c. Enable CONFIG_MEMCG_SWAP (to use swap extension)
d. Enable CONFIG_MEMCG_KMEM (to use kmem extension)

3.1. Prepare the cgroups (see cgroups.txt, Why are cgroups needed?)
# mount -t tmpfs none /sys/fs/cgroup
# mkdir /sys/fs/cgroup/memory
# mount -t cgroup none /sys/fs/cgroup/memory -o memory

3.2. Make the new group and move bash into it
# mkdir /sys/fs/cgroup/memory/0
# echo $$ > /sys/fs/cgroup/memory/0/tasks

Since now we're in the 0 cgroup, we can alter the memory limit:
# echo 4M > /sys/fs/cgroup/memory/0/memory.limit_in_bytes

NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
mega or gigabytes. (Here, Kilo, Mega, Giga are Kibibytes, Mebibytes, Gibibytes.)

NOTE: We can write "-1" to reset the *.limit_in_bytes(unlimited).
NOTE: We cannot set limits on the root cgroup any more.

# cat /sys/fs/cgroup/memory/0/memory.limit_in_bytes
4194304

We can check the usage:
# cat /sys/fs/cgroup/memory/0/memory.usage_in_bytes
1216512