Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 7e636a7
  • /
  • tools
  • /
  • cgroup
  • /
  • memcg_shrinker.py
Raw File Download
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:e81c3017ada9643de72d9fcb5e8fb6808382a31b
directory badge Iframe embedding
swh:1:dir:97f9b79b96b9de1ec325e4764640e3e548d79fd6
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
memcg_shrinker.py
#!/usr/bin/env python3
#
# Copyright (C) 2022 Roman Gushchin <roman.gushchin@linux.dev>
# Copyright (C) 2022 Meta

import os
import argparse


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.sort(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()

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API

back to top