How to find out cgroup of a particular process?

42,389

Solution 1

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

Solution 2

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

Solution 3

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
Share:
42,389

Related videos on Youtube

zerospiel
Author by

zerospiel

Freelancer as a Golang developer

Updated on September 18, 2022

Comments

  • zerospiel
    zerospiel almost 2 years

    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.

    • Matthew Ife
      Matthew Ife over 10 years
      Try ps -o cgroup <pid>
    • c4f4t0r
      c4f4t0r over 10 years
      cat /proc/<pid>/cgroup
    • zerospiel
      zerospiel over 10 years
      @MIfe, yes, you are right, ty very much. This is what I need
    • zerospiel
      zerospiel over 10 years
      @c4f4t0r, your method are not so good, it just output to me all available controllers, but not cgroup of a process, but ty you too.
    • John Greene
      John Greene about 8 years
      @Zerospiel, does the answer below answers your question?
  • phemmer
    phemmer over 6 years
    use pgrep, not ps | grep | awk