Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting a cgroup path from cgroup ID #1500

Closed
natchaphon-r opened this issue Sep 3, 2020 · 4 comments
Closed

Getting a cgroup path from cgroup ID #1500

natchaphon-r opened this issue Sep 3, 2020 · 4 comments
Labels
enhancement New feature or request, changes on existing features

Comments

@natchaphon-r
Copy link

Is your feature request related to a problem? Please describe.

Use case:
My host machine has multiple running docker containers. Inside a docker container, there is a medic process that can report container health.

I'm looking to extend oomkill.bt to notify the medic process whenever there is an OOM event. bpftrace provides a built-in variable "cgroup" which is a cgroup ID. I'm looking to convert cgroup ID to a full cgroup path. Since the full cgroup path has docker container ID, I can use the path to notify the medic process in the correct container.

Describe the solution you'd like

A new BPF helper function that calls task_cgroup_path

Describe alternative solutions or features you've considered

An alternative is to pass cgroup id to a separate program that can convert cgroup id to cgroup path using a similar approach.

@natchaphon-r natchaphon-r added the enhancement New feature or request, changes on existing features label Sep 3, 2020
@danobi
Copy link
Member

danobi commented Sep 10, 2020

Getting filesystem paths through bpf is notoriously tricky b/c you need to take locks. IIUC from following the kernel discussions, the main issue is deadlocks b/c the bpf prog could try to take a lock in an already locked context. @olsajiri probably has the most insight into feasibility of a cgroup_path() helper.

cgroup id is just inode # in cgroupfs so one workaround could look like this:

$ sudo ./build/src/bpftrace -e 'BEGIN { print(cgroup) }'
Attaching 1 probe...
5505
^C

$ find /sys/fs/cgroup -inum 5505
/sys/fs/cgroup/user.slice/user-1000.slice/[email protected]/gnome-launched-alacritty-9823.scope

@bwplotka
Copy link

bwplotka commented Sep 15, 2021

cgroup id is just inode # in cgroupfs so one workaround could look like this:

Hm @danobi .. it does not make much sense, but my find /sys/fs/cgroup -inum 1730 for one service returns multiple paths.. 🤔 Why? I thought inode number is unique.

/sys/fs/cgroup/blkio/system.slice/fwupd.service/blkio.throttle.io_service_bytes_recursive
/sys/fs/cgroup/cpu,cpuacct/system.slice/systemd-logind.service/cpuacct.usage_percpu
/sys/fs/cgroup/cpu,cpuacct/system.slice/systemd-logind.service/cpuacct.usage_percpu
/sys/fs/cgroup/unified/system.slice/docker.service

Code https://github.com/bwplotka/ebpf_exporter/blob/exp/decoder/cgroup.go#L38

(invoking find inside container)

@danobi
Copy link
Member

danobi commented Sep 15, 2021

@bwplotka It looks like you're using hybrid cgroup1 + cgroup2 setup on your host. I believe under cgroup1, a process may be in multiple cgroups. Under cgroup2, a process may only be in 1 cgroup.

@viktormalik
Copy link
Contributor

There is now the cgroup_path builtin that allows this (implemented in #2055).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request, changes on existing features
Projects
None yet
Development

No branches or pull requests

4 participants