All of lore.kernel.org
 help / color / mirror / Atom feed
From: Roman Gushchin <roman.gushchin@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>, linux-mm@kvack.org
Cc: Dave Chinner <dchinner@redhat.com>,
	linux-kernel@vger.kernel.org,
	Kent Overstreet <kent.overstreet@gmail.com>,
	Hillf Danton <hdanton@sina.com>,
	Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
	Roman Gushchin <roman.gushchin@linux.dev>
Subject: [PATCH v3 5/6] tools: add memcg_shrinker.py
Date: Mon,  9 May 2022 11:38:19 -0700	[thread overview]
Message-ID: <20220509183820.573666-6-roman.gushchin@linux.dev> (raw)
In-Reply-To: <20220509183820.573666-1-roman.gushchin@linux.dev>

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 -n 10
  2090     sb-sysfs-26          /sys/fs/cgroup/system.slice
  1809     sb-sysfs-26          /sys/fs/cgroup/system.slice/systemd-udevd.service
  1044     sb-btrfs:vda2-24     /sys/fs/cgroup/system.slice/system-dbus\x2d:1.3\...
  861      sb-btrfs:vda2-24     /sys/fs/cgroup/system.slice/system-dbus\x2d:1.3\...
  804      sb-btrfs:vda2-24     /sys/fs/cgroup/system.slice
  643      sb-btrfs:vda2-24     /sys/fs/cgroup/system.slice/firewalld.service
  616      sb-cgroup2-30        /sys/fs/cgroup/init.scope
  275      sb-sysfs-26          /
  238      sb-proc-25           /sys/fs/cgroup/system.slice/systemd-journald.service
  225      sb-proc-25           /sys/fs/cgroup/system.slice/abrtd.service

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
---
 tools/cgroup/memcg_shrinker.py | 71 ++++++++++++++++++++++++++++++++++
 1 file changed, 71 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..706ab27666a4
--- /dev/null
+++ b/tools/cgroup/memcg_shrinker.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2022 Roman Gushchin <roman.gushchin@linux.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_path = os.path.join(root, shrinker, "count")
+            with open(count_path) as f:
+                for line in f.readlines():
+                    items = line.split(' ')
+                    ino = int(items[0])
+                    # (count, shrinker, memcg ino)
+                    shrinkers.append((int(items[1]), shrinker, ino))
+    return shrinkers
+
+
+def main():
+    parser = argparse.ArgumentParser(description='Display biggest shrinkers')
+    parser.add_argument('-n', '--lines', type=int, help='Number of lines to print')
+
+    args = parser.parse_args()
+
+    cgroups = scan_cgroups("/sys/fs/cgroup/")
+    shrinkers = scan_shrinkers("/sys/kernel/debug/shrinker/")
+    shrinkers = sorted(shrinkers, reverse = True, key = lambda x: x[0])
+
+    n = 0
+    for s in shrinkers:
+        count, name, ino = (s[0], s[1], 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))
+
+        n += 1
+        if args.lines and n >= args.lines:
+            break
+
+
+if __name__ == '__main__':
+    main()
-- 
2.35.3


  parent reply	other threads:[~2022-05-09 18:38 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-09 18:38 [PATCH v3 0/6] mm: introduce shrinker debugfs interface Roman Gushchin
2022-05-09 18:38 ` [PATCH v3 1/6] mm: memcontrol: introduce mem_cgroup_ino() and mem_cgroup_get_from_ino() Roman Gushchin
2022-05-22  7:05   ` Muchun Song
2022-05-23 18:12     ` Roman Gushchin
2022-05-24  2:00       ` Muchun Song
2022-05-09 18:38 ` [PATCH v3 2/6] mm: shrinkers: introduce debugfs interface for memory shrinkers Roman Gushchin
2022-05-20 16:45   ` Kent Overstreet
2022-05-21  0:27     ` Roman Gushchin
2022-05-20 16:58   ` Christophe JAILLET
2022-05-20 17:00     ` Kent Overstreet
2022-05-21  0:27     ` Roman Gushchin
2022-05-22 10:36   ` Muchun Song
2022-05-23 18:24     ` Roman Gushchin
2022-05-24  2:06       ` Muchun Song
2022-05-09 18:38 ` [PATCH v3 3/6] mm: shrinkers: provide shrinkers with names Roman Gushchin
2022-05-20 16:41   ` Kent Overstreet
2022-05-21  0:31     ` Roman Gushchin
2022-05-22 11:08   ` Muchun Song
2022-05-23 22:06     ` Roman Gushchin
2022-05-24  9:12       ` Muchun Song
2022-05-22 22:13   ` Dave Chinner
2022-05-24  2:18     ` Roman Gushchin
2022-05-24 23:54       ` Roman Gushchin
2022-05-09 18:38 ` [PATCH v3 4/6] mm: docs: document shrinker debugfs Roman Gushchin
2022-05-09 18:38 ` Roman Gushchin [this message]
2022-05-09 18:38 ` [PATCH v3 6/6] mm: shrinkers: add scan interface for " Roman Gushchin
2022-05-22 11:35   ` Muchun Song
2022-05-23 20:54     ` Roman Gushchin
2022-05-24  2:23       ` Muchun Song
2022-05-19 17:15 ` [PATCH v3 0/6] mm: introduce shrinker debugfs interface Roman Gushchin
2022-05-20  4:33   ` Dave Chinner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220509183820.573666-6-roman.gushchin@linux.dev \
    --to=roman.gushchin@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=dchinner@redhat.com \
    --cc=hdanton@sina.com \
    --cc=kent.overstreet@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.