28

Is there any method to get cgroup of process?

The only one package that I know (cgroup-bin), just provide some manipulations with cgroups and allow to change cgroup of process/list of processes, but no capabilities to know information about cgroup of a particular process.

zerospiel
  • 417
  • 1
  • 5
  • 10

3 Answers3

27

Using systemd (PID 3378 for example):

# systemctl status 3378 | grep CGroup
CGroup: /user.slice/user-1000.slice/session-3.scope

Using proc:

# cat /proc/3378/cgroup 
10:memory:/user.slice/user-1000.slice
9:blkio:/user.slice/user-1000.slice
8:net_cls,net_prio:/
7:cpu,cpuacct:/user.slice/user-1000.slice
6:perf_event:/
5:freezer:/
4:cpuset:/
3:pids:/user.slice/user-1000.slice
2:devices:/user.slice/user-1000.slice
1:name=systemd:/user.slice/user-1000.slice/session-3.scope

Looking into /sys/:

# cd /sys/fs/ && find * -name "*.procs" -exec grep 3378 {} /dev/null \; 2> /dev/null
cgroup/memory/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/blkio/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/net_cls,net_prio/cgroup.procs:3378
cgroup/cpu,cpuacct/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/perf_event/cgroup.procs:3378
cgroup/freezer/cgroup.procs:3378
cgroup/cpuset/cgroup.procs:3378
cgroup/pids/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/devices/user.slice/user-1000.slice/cgroup.procs:3378
cgroup/systemd/user.slice/user-1000.slice/session-3.scope/cgroup.procs:3378
rfmoz
  • 694
  • 9
  • 15
8

The quickest way to view cgroup of a process is by process name, using this bash script:

#!/bin/bash
THISPID=`ps -eo pid,comm | grep $1 | awk '{print $1}'`
cat /proc/$THISPID/cgroup
John Greene
  • 799
  • 7
  • 28
7

From RHEL7 and up, and on some other distro's, I find this util helpful:

$ systemd-cgtop

Before using it, make sure you have DefaultCPUAccounting=yes in /etc/systemd/system.conf.

I even made some improvements to Egbert's script that is still useful and used Patrick's suggestion to use pgrep:

#!/bin/bash
echo "PID  SLICE   SERVICE"
for THISPID in `pgrep $1`; do
  SLICE=$(cat /proc/$THISPID/cgroup | grep '^1:' | awk -F/ '{ print $2 }')
  SERVICE=$(cat /proc/$THISPID/cgroup | grep '^1:' | awk -F/ '{ print $3 }')
  echo "$THISPID $SLICE $SERVICE"
done
slm
  • 7,355
  • 16
  • 54
  • 72
Dirk Krijgsman
  • 171
  • 1
  • 2