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

Cgroup scripts #423

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 80 additions & 15 deletions contrib/cgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

"""List the paths of all descendants of a cgroup v2"""

import argparse
from contextlib import contextmanager
import os
import sys
from collections import Counter

from drgn import cast
from drgn.helpers.common.type import enum_type_to_class
Expand All @@ -24,6 +26,11 @@
exclude=("__MAX_BPF_ATTACH_TYPE",),
)

CgroupSubsysId = enum_type_to_class(
prog.type("enum cgroup_subsys_id"),
"CgroupSubsysId",
exclude=("CGROUP_SUBSYS_COUNT",),
)

@contextmanager
def open_dir(*args, **kwds):
Expand All @@ -35,16 +42,6 @@ def open_dir(*args, **kwds):
os.close(fd)


def get_cgroup():
if len(sys.argv) == 1:
return prog["cgrp_dfl_root"].cgrp
task = find_task(prog, os.getpid())
with open_dir(sys.argv[1], os.O_RDONLY) as fd:
file_ = fget(task, fd)
kn = cast("struct kernfs_node *", file_.f_path.dentry.d_inode.i_private)
return cast("struct cgroup *", kn.priv)


def print_cgroup_bpf_progs(cgrp):
cgroup_printed = False
for attach_type in BpfAttachType:
Expand All @@ -62,10 +59,78 @@ def print_cgroup_bpf_progs(cgrp):
)


css = get_cgroup().self.address_of_()
def get_cgroup(path):
task = find_task(prog, os.getpid())
try:
with open_dir(path, os.O_RDONLY) as fd:
file_ = fget(task, fd)
kn = cast("struct kernfs_node *", file_.f_path.dentry.d_inode.i_private)
return cast("struct cgroup *", kn.priv)
except FileNotFoundError as e:
raise argparse.ArgumentTypeError(e)


def cmd_tree(cgroup):
css = cgroup.self.address_of_()

for pos in css_for_each_descendant_pre(css):
if sys.argv[-1] == "bpf":
print_cgroup_bpf_progs(pos.cgroup)
else:
for pos in css_for_each_descendant_pre(css):
if not pos.flags & prog["CSS_ONLINE"]:
continue
print(cgroup_path(pos.cgroup).decode())


def cmd_bpf(cgroup):
css = cgroup.self.address_of_()

for pos in css_for_each_descendant_pre(css):
if not pos.flags & prog["CSS_ONLINE"]:
continue
print_cgroup_bpf_progs(pos.cgroup)


def cmd_stat(cgroup):
stat = Counter()
stat_dying = Counter()

for ssid in CgroupSubsysId:
css = cgroup.subsys[ssid.value]
# XXX if subsys of offlined or cgroup rmdir'd under our hands we won't see its subtree
if not css:
continue
for pos in css_for_each_descendant_pre(css):
stat[ssid] +=1
if not pos.flags & prog["CSS_ONLINE"]:
stat_dying[ssid] += 1

for ssid in CgroupSubsysId:
if stat[ssid.value] == 0:
continue
print("nr_{:<30} {:>4}".format(
ssid.name,
stat[ssid.value]
)
)
for ssid in CgroupSubsysId:
if stat_dying[ssid.value] == 0:
continue
print("nr_dying_{:<24} {:>4}".format(
ssid.name,
stat_dying[ssid.value]
)
)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("command", choices=["tree", "bpf", "stat"])
parser.add_argument("cgroups", help="Cgroups", nargs="*", type=get_cgroup)
args = parser.parse_args()

if len(args.cgroups) == 0:
args.cgroups.append(prog["cgrp_dfl_root"].cgrp)

for cg in args.cgroups:
if len(args.cgroups) > 1:
print(cg.kn.name.string_())
locals()["cmd_" + args.command](cg)

7 changes: 3 additions & 4 deletions drgn/helpers/linux/cgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,12 @@ def _css_for_each_impl(
pos = next_fn(pos, css)
if not pos:
break
if pos.flags & pos.prog_["CSS_ONLINE"]:
yield pos
yield pos


def css_for_each_child(css: Object) -> Iterator[Object]:
"""
Iterate through children of the given css.
Iterate through children (offline included) of the given css.

:param css: ``struct cgroup_subsys_state *``
:return: Iterator of ``struct cgroup_subsys_state *`` objects.
Expand All @@ -175,7 +174,7 @@ def css_for_each_child(css: Object) -> Iterator[Object]:

def css_for_each_descendant_pre(css: Object) -> Iterator[Object]:
"""
Iterate through the given css's descendants in pre-order.
Iterate through the given css's descendants (offline included) in pre-order.

:param css: ``struct cgroup_subsys_state *``
:return: Iterator of ``struct cgroup_subsys_state *`` objects.
Expand Down