mirror of
https://github.com/torvalds/linux.git
synced 2024-12-02 17:11:33 +00:00
221944736f
After the introduction of the dedicated struct slab to describe slab pages by commitd122019bf0
("mm: Split slab into its own type") and the following removal of the corresponding struct page's fields by commit07f910f9b7
("mm: Remove slab from struct page") the memcg_slabinfo tool broke. An attempt to run it produces a trace like this: Traceback (most recent call last): File "/usr/bin/drgn", line 33, in <module> sys.exit(load_entry_point('drgn==0.0.16', 'console_scripts', 'drgn')()) File "/usr/lib64/python3.9/site-packages/drgn/internal/cli.py", line 133, in main runpy.run_path(args.script[0], init_globals=init_globals, run_name="__main__") File "/usr/lib64/python3.9/runpy.py", line 268, in run_path return _run_module_code(code, init_globals, run_name, File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code _run_code(code, mod_globals, init_globals, File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "memcg_slabinfo.py", line 226, in <module> main() File "memcg_slabinfo.py", line 199, in main cache = page.slab_cache AttributeError: 'struct page' has no member 'slab_cache' The problem can be fixed by explicitly casting struct page * to struct slab * for slab pages. The tools works as expected with this fix, e.g.: cred_jar 776 776 192 21 1 : tunables 0 0 0 : slabdata 547 547 0 kmalloc-cg-32 6 6 32 128 1 : tunables 0 0 0 : slabdata 9 9 0 files_cache 3 3 832 39 8 : tunables 0 0 0 : slabdata 8 8 0 kmalloc-cg-512 1 1 512 32 4 : tunables 0 0 0 : slabdata 10 10 0 task_struct 10 10 6720 4 8 : tunables 0 0 0 : slabdata 63 63 0 mm_struct 3 3 1664 19 8 : tunables 0 0 0 : slabdata 9 9 0 kmalloc-cg-16 1 1 16 256 1 : tunables 0 0 0 : slabdata 8 8 0 pde_opener 1 1 40 102 1 : tunables 0 0 0 : slabdata 8 8 0 anon_vma_chain 375 375 64 64 1 : tunables 0 0 0 : slabdata 81 81 0 radix_tree_node 3 3 584 28 4 : tunables 0 0 0 : slabdata 419 419 0 dentry 98 98 312 26 2 : tunables 0 0 0 : slabdata 1420 1420 0 btrfs_inode 3 3 2368 13 8 : tunables 0 0 0 : slabdata 730 730 0 signal_cache 3 3 1600 20 8 : tunables 0 0 0 : slabdata 17 17 0 sighand_cache 3 3 2240 14 8 : tunables 0 0 0 : slabdata 20 20 0 filp 90 90 512 32 4 : tunables 0 0 0 : slabdata 95 95 0 anon_vma 214 214 200 20 1 : tunables 0 0 0 : slabdata 162 162 0 kmalloc-cg-1k 1 1 1024 32 8 : tunables 0 0 0 : slabdata 22 22 0 pid 10 10 256 32 2 : tunables 0 0 0 : slabdata 14 14 0 kmalloc-cg-64 2 2 64 64 1 : tunables 0 0 0 : slabdata 8 8 0 kmalloc-cg-96 3 3 96 42 1 : tunables 0 0 0 : slabdata 8 8 0 sock_inode_cache 5 5 1408 23 8 : tunables 0 0 0 : slabdata 29 29 0 UNIX 7 7 1920 17 8 : tunables 0 0 0 : slabdata 21 21 0 inode_cache 36 36 1152 28 8 : tunables 0 0 0 : slabdata 680 680 0 proc_inode_cache 26 26 1224 26 8 : tunables 0 0 0 : slabdata 64 64 0 kmalloc-cg-2k 2 2 2048 16 8 : tunables 0 0 0 : slabdata 9 9 0 v2: change naming and count_partial()/count_free()/for_each_slab() signatures to work with slabs, suggested by Matthew Wilcox Fixes:07f910f9b7
("mm: Remove slab from struct page") Reported-by: Vasily Averin <vvs@virtuozzo.com> Signed-off-by: Roman Gushchin <guro@fb.com> Tested-by: Vasily Averin <vvs@virtuozzo.com> Signed-off-by: Vlastimil Babka <vbabka@suse.cz> Link: https://lore.kernel.org/linux-patches/Yg2cKKnIboNu7j+p@carbon.DHCP.thefacebook.com/
227 lines
6.5 KiB
Python
227 lines
6.5 KiB
Python
#!/usr/bin/env drgn
|
|
#
|
|
# Copyright (C) 2020 Roman Gushchin <guro@fb.com>
|
|
# Copyright (C) 2020 Facebook
|
|
|
|
from os import stat
|
|
import argparse
|
|
import sys
|
|
|
|
from drgn.helpers.linux import list_for_each_entry, list_empty
|
|
from drgn.helpers.linux import for_each_page
|
|
from drgn.helpers.linux.cpumask import for_each_online_cpu
|
|
from drgn.helpers.linux.percpu import per_cpu_ptr
|
|
from drgn import container_of, FaultError, Object, cast
|
|
|
|
|
|
DESC = """
|
|
This is a drgn script to provide slab statistics for memory cgroups.
|
|
It supports cgroup v2 and v1 and can emulate memory.kmem.slabinfo
|
|
interface of cgroup v1.
|
|
For drgn, visit https://github.com/osandov/drgn.
|
|
"""
|
|
|
|
|
|
MEMCGS = {}
|
|
|
|
OO_SHIFT = 16
|
|
OO_MASK = ((1 << OO_SHIFT) - 1)
|
|
|
|
|
|
def err(s):
|
|
print('slabinfo.py: error: %s' % s, file=sys.stderr, flush=True)
|
|
sys.exit(1)
|
|
|
|
|
|
def find_memcg_ids(css=prog['root_mem_cgroup'].css, prefix=''):
|
|
if not list_empty(css.children.address_of_()):
|
|
for css in list_for_each_entry('struct cgroup_subsys_state',
|
|
css.children.address_of_(),
|
|
'sibling'):
|
|
name = prefix + '/' + css.cgroup.kn.name.string_().decode('utf-8')
|
|
memcg = container_of(css, 'struct mem_cgroup', 'css')
|
|
MEMCGS[css.cgroup.kn.id.value_()] = memcg
|
|
find_memcg_ids(css, name)
|
|
|
|
|
|
def is_root_cache(s):
|
|
try:
|
|
return False if s.memcg_params.root_cache else True
|
|
except AttributeError:
|
|
return True
|
|
|
|
|
|
def cache_name(s):
|
|
if is_root_cache(s):
|
|
return s.name.string_().decode('utf-8')
|
|
else:
|
|
return s.memcg_params.root_cache.name.string_().decode('utf-8')
|
|
|
|
|
|
# SLUB
|
|
|
|
def oo_order(s):
|
|
return s.oo.x >> OO_SHIFT
|
|
|
|
|
|
def oo_objects(s):
|
|
return s.oo.x & OO_MASK
|
|
|
|
|
|
def count_partial(n, fn):
|
|
nr_objs = 0
|
|
for slab in list_for_each_entry('struct slab', n.partial.address_of_(),
|
|
'slab_list'):
|
|
nr_objs += fn(slab)
|
|
return nr_objs
|
|
|
|
|
|
def count_free(slab):
|
|
return slab.objects - slab.inuse
|
|
|
|
|
|
def slub_get_slabinfo(s, cfg):
|
|
nr_slabs = 0
|
|
nr_objs = 0
|
|
nr_free = 0
|
|
|
|
for node in range(cfg['nr_nodes']):
|
|
n = s.node[node]
|
|
nr_slabs += n.nr_slabs.counter.value_()
|
|
nr_objs += n.total_objects.counter.value_()
|
|
nr_free += count_partial(n, count_free)
|
|
|
|
return {'active_objs': nr_objs - nr_free,
|
|
'num_objs': nr_objs,
|
|
'active_slabs': nr_slabs,
|
|
'num_slabs': nr_slabs,
|
|
'objects_per_slab': oo_objects(s),
|
|
'cache_order': oo_order(s),
|
|
'limit': 0,
|
|
'batchcount': 0,
|
|
'shared': 0,
|
|
'shared_avail': 0}
|
|
|
|
|
|
def cache_show(s, cfg, objs):
|
|
if cfg['allocator'] == 'SLUB':
|
|
sinfo = slub_get_slabinfo(s, cfg)
|
|
else:
|
|
err('SLAB isn\'t supported yet')
|
|
|
|
if cfg['shared_slab_pages']:
|
|
sinfo['active_objs'] = objs
|
|
sinfo['num_objs'] = objs
|
|
|
|
print('%-17s %6lu %6lu %6u %4u %4d'
|
|
' : tunables %4u %4u %4u'
|
|
' : slabdata %6lu %6lu %6lu' % (
|
|
cache_name(s), sinfo['active_objs'], sinfo['num_objs'],
|
|
s.size, sinfo['objects_per_slab'], 1 << sinfo['cache_order'],
|
|
sinfo['limit'], sinfo['batchcount'], sinfo['shared'],
|
|
sinfo['active_slabs'], sinfo['num_slabs'],
|
|
sinfo['shared_avail']))
|
|
|
|
|
|
def detect_kernel_config():
|
|
cfg = {}
|
|
|
|
cfg['nr_nodes'] = prog['nr_online_nodes'].value_()
|
|
|
|
if prog.type('struct kmem_cache').members[1].name == 'flags':
|
|
cfg['allocator'] = 'SLUB'
|
|
elif prog.type('struct kmem_cache').members[1].name == 'batchcount':
|
|
cfg['allocator'] = 'SLAB'
|
|
else:
|
|
err('Can\'t determine the slab allocator')
|
|
|
|
cfg['shared_slab_pages'] = False
|
|
try:
|
|
if prog.type('struct obj_cgroup'):
|
|
cfg['shared_slab_pages'] = True
|
|
except:
|
|
pass
|
|
|
|
return cfg
|
|
|
|
|
|
def for_each_slab(prog):
|
|
PGSlab = 1 << prog.constant('PG_slab')
|
|
PGHead = 1 << prog.constant('PG_head')
|
|
|
|
for page in for_each_page(prog):
|
|
try:
|
|
if page.flags.value_() & PGSlab:
|
|
yield cast('struct slab *', page)
|
|
except FaultError:
|
|
pass
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=DESC,
|
|
formatter_class=
|
|
argparse.RawTextHelpFormatter)
|
|
parser.add_argument('cgroup', metavar='CGROUP',
|
|
help='Target memory cgroup')
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
cgroup_id = stat(args.cgroup).st_ino
|
|
find_memcg_ids()
|
|
memcg = MEMCGS[cgroup_id]
|
|
except KeyError:
|
|
err('Can\'t find the memory cgroup')
|
|
|
|
cfg = detect_kernel_config()
|
|
|
|
print('# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>'
|
|
' : tunables <limit> <batchcount> <sharedfactor>'
|
|
' : slabdata <active_slabs> <num_slabs> <sharedavail>')
|
|
|
|
if cfg['shared_slab_pages']:
|
|
obj_cgroups = set()
|
|
stats = {}
|
|
caches = {}
|
|
|
|
# find memcg pointers belonging to the specified cgroup
|
|
obj_cgroups.add(memcg.objcg.value_())
|
|
for ptr in list_for_each_entry('struct obj_cgroup',
|
|
memcg.objcg_list.address_of_(),
|
|
'list'):
|
|
obj_cgroups.add(ptr.value_())
|
|
|
|
# look over all slab folios and look for objects belonging
|
|
# to the given memory cgroup
|
|
for slab in for_each_slab(prog):
|
|
objcg_vec_raw = slab.memcg_data.value_()
|
|
if objcg_vec_raw == 0:
|
|
continue
|
|
cache = slab.slab_cache
|
|
if not cache:
|
|
continue
|
|
addr = cache.value_()
|
|
caches[addr] = cache
|
|
# clear the lowest bit to get the true obj_cgroups
|
|
objcg_vec = Object(prog, 'struct obj_cgroup **',
|
|
value=objcg_vec_raw & ~1)
|
|
|
|
if addr not in stats:
|
|
stats[addr] = 0
|
|
|
|
for i in range(oo_objects(cache)):
|
|
if objcg_vec[i].value_() in obj_cgroups:
|
|
stats[addr] += 1
|
|
|
|
for addr in caches:
|
|
if stats[addr] > 0:
|
|
cache_show(caches[addr], cfg, stats[addr])
|
|
|
|
else:
|
|
for s in list_for_each_entry('struct kmem_cache',
|
|
memcg.kmem_caches.address_of_(),
|
|
'memcg_params.kmem_caches_node'):
|
|
cache_show(s, cfg, None)
|
|
|
|
|
|
main()
|