Skip to content

Conversation

@Rtoax
Copy link
Contributor

@Rtoax Rtoax commented Aug 5, 2025

Using a simple test program (not shown) called OOM, we performed the
following two tests:

  1. Allocating unlimited memory
  2. Adding the process to a cgroup named oom-memcg and limiting its memory
    usage to 200MB.

When we do not print cgroup information, we can only see process information
and cannot see the difference between memcg and non-memcg.

$ sudo ./oomkill
Tracing OOM kills... Ctrl-C to stop.
14:28:23 Triggered by PID 179201 ("oom"), OOM kill of PID 179201 ("oom"), 6114610 pages, loadavg: loadavg: 0.56 0.51 0.38 2/968 179204

14:28:42 Triggered by PID 179212 ("oom"), OOM kill of PID 179212 ("oom"), 51200 pages, loadavg: loadavg: 0.40 0.47 0.37 3/968 179212

The function implemented by this patch can clearly display cgroup information.

$ sudo ./oomkill  -c
Tracing OOM kills... Ctrl-C to stop.
14:32:59 Triggered by PID 179879 ("oom"), CGROUP 8309 ("/sys/fs/cgroup/user.slice/user-1000.slice/[email protected]/session.slice/[email protected]"), OOM kill of PID 179879 ("oom"), 6114610 pages, loadavg: loadavg: 0.50 0.38 0.35 4/970 179879

14:33:14 Triggered by PID 179884 ("oom"), CGROUP 122547 ("/sys/fs/cgroup/oom-memcg"), MEMCG 122547 ("/sys/fs/cgroup/oom-memcg"), OOM kill of PID 179884 ("oom"), 51200 pages, loadavg: loadavg: 0.47 0.38 0.35 3/971 179884

and add cgroup_helpers.c.

@Rtoax Rtoax changed the title Patch 105 oomkill cgroup libbpf-tools: add cgroup_helpers and oomkill support display cgroup Aug 5, 2025
@Rtoax Rtoax force-pushed the patch-105-oomkill-cgroup branch from e957108 to 33f29d3 Compare August 6, 2025 06:52
@yonghong-song
Copy link
Collaborator

For this one:

Using a simple test program (not shown) called OOM, we performed the
following two tests:
Allocating unlimited memory
Adding the process to a cgroup named oom-memcg and limiting its memory
usage to 200MB.

Could you actually show your simple test? This way, people can reproduce the issue easily.

@Rtoax
Copy link
Contributor Author

Rtoax commented Sep 2, 2025

Could you actually show your simple test? This way, people can reproduce the issue easily.

Yes, thanks, the test code:

oom_minimal.c

#include <errno.h>
#include <malloc.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
	void *mem;

	for (;;) {
		mem = malloc(getpagesize());
		if (!mem && errno == ENOMEM) {
			fprintf(stderr, "OOMing...\n");
		}
		*(int *)mem = 1;
		/* Just leak the memory */
	}
	return 0;
}

cgroup-oom.sh

#!/bin/bash
set -e

readonly pid=$$
readonly CGROUP_NAME=oom-test

[[ -z ${OOMer} ]] && OOMer=oom_minimal

cleanup() {
	printf "\n"
	sudo rmdir /sys/fs/cgroup/${CGROUP_NAME}/
}
trap cleanup EXIT

sudo mkdir -p /sys/fs/cgroup/${CGROUP_NAME}/
echo ${pid} | sudo tee /sys/fs/cgroup/${CGROUP_NAME}/cgroup.procs
echo $((1024*1024*2)) | sudo tee /sys/fs/cgroup/${CGROUP_NAME}/memory.max
echo $((1024*1024*2)) | sudo tee /sys/fs/cgroup/${CGROUP_NAME}/memory.high

eval ./${OOMer} ${@}

compile and run

gcc oom_minimal.c -o oom_minim
./cgroup-oom.sh

then

$ sudo ./oomkill -c
09:31:49 Triggered by PID 53791 ("oom_minimal"), CGROUP 45172 ("/sys/fs/cgroup/oom-test"), MEMCG 45172 ("/sys/fs/cgroup/oom-test"), OOM kill of PID 53791 ("oom_minimal"), 2097663 pages, loadavg: loadavg: 0.19 0.21 0.20 3/1098 53792

@Rtoax Rtoax force-pushed the patch-105-oomkill-cgroup branch from 33f29d3 to 1665706 Compare September 2, 2025 02:04
@Rtoax
Copy link
Contributor Author

Rtoax commented Sep 2, 2025

@yonghong-song Thanks a lot, i just submit all changes and add test code above, please review, :)

@Rtoax Rtoax requested a review from yonghong-song September 2, 2025 02:24
Rtoax added a commit to Rtoax/bcc that referenced this pull request Sep 3, 2025
Using a simple test program (not shown) called OOM, we performed the
following two tests:

   1. Allocating unlimited memory
   2. Adding the process to a cgroup named oom-memcg and limiting its memory
      usage to 200MB.

When we do not print cgroup information, we can only see process information
and cannot see the difference between memcg and non-memcg.

    $ sudo ./oomkill
    Tracing OOM kills... Ctrl-C to stop.
    14:28:23 Triggered by PID 179201 ("oom"), OOM kill of PID 179201 ("oom"), 6114610 pages, loadavg: loadavg: 0.56 0.51 0.38 2/968 179204

    14:28:42 Triggered by PID 179212 ("oom"), OOM kill of PID 179212 ("oom"), 51200 pages, loadavg: loadavg: 0.40 0.47 0.37 3/968 179212

The function implemented by this patch can clearly display cgroup information.

    $ sudo ./oomkill  -c
    Tracing OOM kills... Ctrl-C to stop.
    14:32:59 Triggered by PID 179879 ("oom"), CGROUP 8309 ("/sys/fs/cgroup/user.slice/user-1000.slice/[email protected]/session.slice/[email protected]"), OOM kill of PID 179879 ("oom"), 6114610 pages, loadavg: loadavg: 0.50 0.38 0.35 4/970 179879

    14:33:14 Triggered by PID 179884 ("oom"), CGROUP 122547 ("/sys/fs/cgroup/oom-memcg"), MEMCG 122547 ("/sys/fs/cgroup/oom-memcg"), OOM kill of PID 179884 ("oom"), 51200 pages, loadavg: loadavg: 0.47 0.38 0.35 3/971 179884

Link: iovisor#5384
Signed-off-by: Rong Tao <[email protected]>
@Rtoax Rtoax force-pushed the patch-105-oomkill-cgroup branch from 1665706 to 86b0c14 Compare September 3, 2025 11:10
Rtoax added 2 commits October 1, 2025 13:34
Some tools need to obtain cgroup information. For example, oomkill currently
only supports tracking process information and cannot obtain cgroup
information. It would be better if it could obtain memcg information.

For ease of maintenance, a separate commit is kept just for adding
cgroup_helpers.

Added interfaces:
    cgroup_cgroupid_of_path() - Get cgroupid from cgroup absolute path
    get_cgroupid_path() - Get cgroup path from cgroupid

Signed-off-by: Rong Tao <[email protected]>
Using a simple test program (not shown) called OOM, we performed the
following two tests:

   1. Allocating unlimited memory
   2. Adding the process to a cgroup named oom-memcg and limiting its memory
      usage to 200MB.

When we do not print cgroup information, we can only see process information
and cannot see the difference between memcg and non-memcg.

    $ sudo ./oomkill
    Tracing OOM kills... Ctrl-C to stop.
    14:28:23 Triggered by PID 179201 ("oom"), OOM kill of PID 179201 ("oom"), 6114610 pages, loadavg: loadavg: 0.56 0.51 0.38 2/968 179204

    14:28:42 Triggered by PID 179212 ("oom"), OOM kill of PID 179212 ("oom"), 51200 pages, loadavg: loadavg: 0.40 0.47 0.37 3/968 179212

The function implemented by this patch can clearly display cgroup information.

    $ sudo ./oomkill  -c
    Tracing OOM kills... Ctrl-C to stop.
    14:32:59 Triggered by PID 179879 ("oom"), CGROUP 8309 ("/sys/fs/cgroup/user.slice/user-1000.slice/[email protected]/session.slice/[email protected]"), OOM kill of PID 179879 ("oom"), 6114610 pages, loadavg: loadavg: 0.50 0.38 0.35 4/970 179879

    14:33:14 Triggered by PID 179884 ("oom"), CGROUP 122547 ("/sys/fs/cgroup/oom-memcg"), MEMCG 122547 ("/sys/fs/cgroup/oom-memcg"), OOM kill of PID 179884 ("oom"), 51200 pages, loadavg: loadavg: 0.47 0.38 0.35 3/971 179884

Link: iovisor#5384
Signed-off-by: Rong Tao <[email protected]>
@Rtoax Rtoax force-pushed the patch-105-oomkill-cgroup branch from 86b0c14 to 374fdfb Compare October 1, 2025 05:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants