lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220422202644.799732-8-roman.gushchin@linux.dev>
Date:   Fri, 22 Apr 2022 13:26:44 -0700
From:   Roman Gushchin <roman.gushchin@...ux.dev>
To:     Andrew Morton <akpm@...ux-foundation.org>, linux-mm@...ck.org
Cc:     Dave Chinner <dchinner@...hat.com>, linux-kernel@...r.kernel.org,
        Yang Shi <shy828301@...il.com>,
        Kent Overstreet <kent.overstreet@...il.com>,
        Hillf Danton <hdanton@...a.com>,
        Roman Gushchin <roman.gushchin@...ux.dev>
Subject: [PATCH v2 7/7] tools: add memcg_shrinker.py

Add a simple tool which prints a sorted list of shrinker lists
in the following format: (number of objects, shrinker name, cgroup).

Example:
  $ ./memcg_shrinker.py | head -n 10
    2092     sb-sysfs-26          /sys/fs/cgroup/system.slice
    1809     sb-sysfs-26          /sys/fs/cgroup/system.slice/systemd-udevd.service
    1350     sb-btrfs-24          /sys/fs/cgroup/system.slice
    1016     sb-btrfs-24          /sys/fs/cgroup/system.slice/system-dbus\x2d:1.3\x2dorg.fedoraproject.Setroubleshootd.slice
    861      sb-btrfs-24          /sys/fs/cgroup/system.slice/system-dbus\x2d:1.3\x2dorg.fedoraproject.SetroubleshootPrivileged.slice
    672      sb-btrfs-24          /sys/fs/cgroup/system.slice/firewalld.service
    655      sb-cgroup2-30        /sys/fs/cgroup/init.scope
    275      sb-sysfs-26          /
    232      sb-btrfs-24          /
    221      sb-proc-25           /sys/fs/cgroup/system.slice/systemd-journald.service

Signed-off-by: Roman Gushchin <roman.gushchin@...ux.dev>
---
 tools/cgroup/memcg_shrinker.py | 70 ++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100755 tools/cgroup/memcg_shrinker.py

diff --git a/tools/cgroup/memcg_shrinker.py b/tools/cgroup/memcg_shrinker.py
new file mode 100755
index 000000000000..9c32bf0247b2
--- /dev/null
+++ b/tools/cgroup/memcg_shrinker.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2022 Roman Gushchin <roman.gushchin@...ux.dev>
+# Copyright (C) 2022 Meta
+
+import os
+import argparse
+import sys
+
+
+def scan_cgroups(cgroup_root):
+    cgroups = {}
+
+    for root, subdirs, _ in os.walk(cgroup_root):
+        for cgroup in subdirs:
+            path = os.path.join(root, cgroup)
+            ino = os.stat(path).st_ino
+            cgroups[ino] = path
+
+    # (memcg ino, path)
+    return cgroups
+
+
+def scan_shrinkers(shrinker_debugfs):
+    shrinkers = []
+
+    for root, subdirs, _ in os.walk(shrinker_debugfs):
+        for shrinker in subdirs:
+            count_memcg_path = os.path.join(root, shrinker, "count_memcg")
+            try:
+                with open(count_memcg_path) as f:
+                    for line in f.readlines():
+                        items = line.split(' ')
+                        ino = int(items[0])
+                        shrinkers.append((int(items[1]), shrinker, ino))
+            except FileNotFoundError:
+                count_path = os.path.join(root, shrinker, "count")
+                with open(count_path) as f:
+                    shrinkers.append((int(f.readline()), shrinker, 0))
+
+    # (count, shrinker, memcg ino)
+    return shrinkers
+
+
+def main():
+    cgroups = scan_cgroups("/sys/fs/cgroup/")
+    shrinkers = scan_shrinkers("/sys/kernel/debug/shrinker/")
+    shrinkers = sorted(shrinkers, reverse = True, key = lambda x: x[0])
+
+    for s in shrinkers:
+        count = s[0]
+        name = s[1]
+        ino = s[2]
+
+        if count == 0:
+            break
+
+        if ino == 0 or ino == 1:
+            cg = "/"
+        else:
+            try:
+                cg = cgroups[ino]
+            except KeyError:
+                cg = "unknown (%d)" % ino
+
+        print("%-8s %-20s %s" % (count, name, cg))
+
+
+if __name__ == '__main__':
+    main()
-- 
2.35.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ