Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp987914pxb; Fri, 22 Apr 2022 16:04:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwI1uog9V5ytZsXWArxZnWNqnm471KXvLl3YVZ/StkYRWXCL1XB6XZsOjpRsLhJdysPCIdy X-Received: by 2002:a63:a61:0:b0:39c:b654:b753 with SMTP id z33-20020a630a61000000b0039cb654b753mr5666010pgk.117.1650668662456; Fri, 22 Apr 2022 16:04:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650668662; cv=none; d=google.com; s=arc-20160816; b=owIqW44tmNlFMzjRjUvL6l0HibogF2PkZBmtxGkxv/+wXy4H88Ca8bx8+JOV4zfnWY nvRAonMVxnOsMpFzufDVuUB1WLgBw315tNMWvEBRmEBUFBQkuKpsfeqvpkw0Bt/mSrtu JGd4NAH09iscGv5D3CmrOqkbHAvwrMY7YbB4kkTga4jnIJ/4Jti50f4MH6JLmvxWFPt6 OiTXlOacZ+QykuC7GBZRr2FzO+DCgWMtVvUOYrj+25HBu1Cf0HSTS4L5ovWOQwMfIQqP q9jzUvrt5LNMCwEHSjeCnRujE+CVU61eBWtjcdmlcB08UQaAYhTXQfYiwPRKt1/LaW5q vKHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nIcEPpdyLCFHehrF6EjwfH/S8rR+FWD7FuLOrsoav4I=; b=cOnC8sLoNMZqN+9MFMjRO4IIfaJdenPKobybAd0Mp0j4j1fnjqpp3KwsX+TQH8D+Dy hGijCuMKRCrSTcP29irxmUDTp37XM/0VYySx+B6vhaMD9jZ4BIGMl2CyP3N8Wm6xQb29 eW23y2K5lRLMU8b16d9ePx0QcuqsEzgjBM58RWrGX1/nl+8J36S3POTBRCpIEP9LhFjI 5KleB+aZm+mA94E0FcZhCGcxYn/OSxgI/sn+rgBv5wR378u8e1q2HOfAVk4vVRVaPaDx cfP2SeQ61YiIxaWcd06hh4JI84uhnJO5Rs8ztjHxfi/0YyCn6QVbnluhp51zf2oTXQYl 1pQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=LYCD2BdG; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id 201-20020a6217d2000000b0050a73701075si9232827pfx.159.2022.04.22.16.04.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 16:04:22 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=LYCD2BdG; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1485B138C8B; Fri, 22 Apr 2022 14:52:53 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230302AbiDVVzl (ORCPT + 99 others); Fri, 22 Apr 2022 17:55:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230230AbiDVVzj (ORCPT ); Fri, 22 Apr 2022 17:55:39 -0400 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C70761E5F7A for ; Fri, 22 Apr 2022 13:38:06 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1650659237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nIcEPpdyLCFHehrF6EjwfH/S8rR+FWD7FuLOrsoav4I=; b=LYCD2BdGVPfWh3NvaVHOatAgiYmQELzvA6f2uljVJALCl2Y667IjSVrZTJyc3jlXv2JbvP cWB6Ep+t9wpg1Z0UPJC0GseF1KAS7gyUNhXQzk0cduiOIY/y2Bl9n2FFqLQK/0hur4ikoy 0GoBMKQXmj0q7nvZQYfJIFudeUnygSM= From: Roman Gushchin To: Andrew Morton , linux-mm@kvack.org Cc: Dave Chinner , linux-kernel@vger.kernel.org, Yang Shi , Kent Overstreet , Hillf Danton , Roman Gushchin Subject: [PATCH v2 7/7] tools: add memcg_shrinker.py Date: Fri, 22 Apr 2022 13:26:44 -0700 Message-Id: <20220422202644.799732-8-roman.gushchin@linux.dev> In-Reply-To: <20220422202644.799732-1-roman.gushchin@linux.dev> References: <20220422202644.799732-1-roman.gushchin@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 +# 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