Skip to content
Advertisement

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

I am running a process under a cgroup with an OOM Killer. When it performs a kill, dmesg outputs messages such as the following.

[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

I would like to be able to tell how much memory the cgroups OOM Killer believes the process is using at any given time.

Is there a way to query for this quantity?

Advertisement

Answer

I found the following in the official documentation for cgroup-v1, which shows how to query current memory usage, as well as altering limits:

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
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement