xref: /openbmc/linux/scripts/gdb/linux/slab.py (revision 79939c4a)
1*79939c4aSKuan-Ying Lee# SPDX-License-Identifier: GPL-2.0
2*79939c4aSKuan-Ying Lee#
3*79939c4aSKuan-Ying Lee# Copyright (c) 2023 MediaTek Inc.
4*79939c4aSKuan-Ying Lee#
5*79939c4aSKuan-Ying Lee# Authors:
6*79939c4aSKuan-Ying Lee#  Kuan-Ying Lee <Kuan-Ying.Lee@mediatek.com>
7*79939c4aSKuan-Ying Lee#
8*79939c4aSKuan-Ying Lee
9*79939c4aSKuan-Ying Leeimport gdb
10*79939c4aSKuan-Ying Leeimport re
11*79939c4aSKuan-Ying Leeimport traceback
12*79939c4aSKuan-Ying Leefrom linux import lists, utils, stackdepot, constants, mm
13*79939c4aSKuan-Ying Lee
14*79939c4aSKuan-Ying LeeSLAB_RED_ZONE       = constants.LX_SLAB_RED_ZONE
15*79939c4aSKuan-Ying LeeSLAB_POISON         = constants.LX_SLAB_POISON
16*79939c4aSKuan-Ying LeeSLAB_KMALLOC        = constants.LX_SLAB_KMALLOC
17*79939c4aSKuan-Ying LeeSLAB_HWCACHE_ALIGN  = constants.LX_SLAB_HWCACHE_ALIGN
18*79939c4aSKuan-Ying LeeSLAB_CACHE_DMA      = constants.LX_SLAB_CACHE_DMA
19*79939c4aSKuan-Ying LeeSLAB_CACHE_DMA32    = constants.LX_SLAB_CACHE_DMA32
20*79939c4aSKuan-Ying LeeSLAB_STORE_USER     = constants.LX_SLAB_STORE_USER
21*79939c4aSKuan-Ying LeeSLAB_PANIC          = constants.LX_SLAB_PANIC
22*79939c4aSKuan-Ying Lee
23*79939c4aSKuan-Ying LeeOO_SHIFT = 16
24*79939c4aSKuan-Ying LeeOO_MASK = (1 << OO_SHIFT) - 1
25*79939c4aSKuan-Ying Lee
26*79939c4aSKuan-Ying Leeif constants.LX_CONFIG_SLUB_DEBUG:
27*79939c4aSKuan-Ying Lee    slab_type = utils.CachedType("struct slab")
28*79939c4aSKuan-Ying Lee    slab_ptr_type = slab_type.get_type().pointer()
29*79939c4aSKuan-Ying Lee    kmem_cache_type = utils.CachedType("struct kmem_cache")
30*79939c4aSKuan-Ying Lee    kmem_cache_ptr_type = kmem_cache_type.get_type().pointer()
31*79939c4aSKuan-Ying Lee    freeptr_t = utils.CachedType("freeptr_t")
32*79939c4aSKuan-Ying Lee    freeptr_t_ptr = freeptr_t.get_type().pointer()
33*79939c4aSKuan-Ying Lee
34*79939c4aSKuan-Ying Lee    track_type = gdb.lookup_type('struct track')
35*79939c4aSKuan-Ying Lee    track_alloc = int(gdb.parse_and_eval('TRACK_ALLOC'))
36*79939c4aSKuan-Ying Lee    track_free = int(gdb.parse_and_eval('TRACK_FREE'))
37*79939c4aSKuan-Ying Lee
38*79939c4aSKuan-Ying Leedef slab_folio(slab):
39*79939c4aSKuan-Ying Lee    return slab.cast(gdb.lookup_type("struct folio").pointer())
40*79939c4aSKuan-Ying Lee
41*79939c4aSKuan-Ying Leedef slab_address(slab):
42*79939c4aSKuan-Ying Lee    p_ops = mm.page_ops().ops
43*79939c4aSKuan-Ying Lee    folio = slab_folio(slab)
44*79939c4aSKuan-Ying Lee    return p_ops.folio_address(folio)
45*79939c4aSKuan-Ying Lee
46*79939c4aSKuan-Ying Leedef for_each_object(cache, addr, slab_objects):
47*79939c4aSKuan-Ying Lee    p = addr
48*79939c4aSKuan-Ying Lee    if cache['flags'] & SLAB_RED_ZONE:
49*79939c4aSKuan-Ying Lee        p += int(cache['red_left_pad'])
50*79939c4aSKuan-Ying Lee    while p < addr + (slab_objects * cache['size']):
51*79939c4aSKuan-Ying Lee        yield p
52*79939c4aSKuan-Ying Lee        p = p + int(cache['size'])
53*79939c4aSKuan-Ying Lee
54*79939c4aSKuan-Ying Leedef get_info_end(cache):
55*79939c4aSKuan-Ying Lee    if (cache['offset'] >= cache['inuse']):
56*79939c4aSKuan-Ying Lee        return cache['inuse'] + gdb.lookup_type("void").pointer().sizeof
57*79939c4aSKuan-Ying Lee    else:
58*79939c4aSKuan-Ying Lee        return cache['inuse']
59*79939c4aSKuan-Ying Lee
60*79939c4aSKuan-Ying Leedef get_orig_size(cache, obj):
61*79939c4aSKuan-Ying Lee    if cache['flags'] & SLAB_STORE_USER and cache['flags'] & SLAB_KMALLOC:
62*79939c4aSKuan-Ying Lee        p = mm.page_ops().ops.kasan_reset_tag(obj)
63*79939c4aSKuan-Ying Lee        p += get_info_end(cache)
64*79939c4aSKuan-Ying Lee        p += gdb.lookup_type('struct track').sizeof * 2
65*79939c4aSKuan-Ying Lee        p = p.cast(utils.get_uint_type().pointer())
66*79939c4aSKuan-Ying Lee        return p.dereference()
67*79939c4aSKuan-Ying Lee    else:
68*79939c4aSKuan-Ying Lee        return cache['object_size']
69*79939c4aSKuan-Ying Lee
70*79939c4aSKuan-Ying Leedef get_track(cache, object_pointer, alloc):
71*79939c4aSKuan-Ying Lee    p = object_pointer + get_info_end(cache)
72*79939c4aSKuan-Ying Lee    p += (alloc * track_type.sizeof)
73*79939c4aSKuan-Ying Lee    return p
74*79939c4aSKuan-Ying Lee
75*79939c4aSKuan-Ying Leedef oo_objects(x):
76*79939c4aSKuan-Ying Lee    return int(x['x']) & OO_MASK
77*79939c4aSKuan-Ying Lee
78*79939c4aSKuan-Ying Leedef oo_order(x):
79*79939c4aSKuan-Ying Lee    return int(x['x']) >> OO_SHIFT
80*79939c4aSKuan-Ying Lee
81*79939c4aSKuan-Ying Leedef reciprocal_divide(a, R):
82*79939c4aSKuan-Ying Lee    t = (a * int(R['m'])) >> 32
83*79939c4aSKuan-Ying Lee    return (t + ((a - t) >> int(R['sh1']))) >> int(R['sh2'])
84*79939c4aSKuan-Ying Lee
85*79939c4aSKuan-Ying Leedef __obj_to_index(cache, addr, obj):
86*79939c4aSKuan-Ying Lee    return reciprocal_divide(int(mm.page_ops().ops.kasan_reset_tag(obj)) - addr, cache['reciprocal_size'])
87*79939c4aSKuan-Ying Lee
88*79939c4aSKuan-Ying Leedef swab64(x):
89*79939c4aSKuan-Ying Lee    result = (((x & 0x00000000000000ff) << 56) |   \
90*79939c4aSKuan-Ying Lee    ((x & 0x000000000000ff00) << 40) |   \
91*79939c4aSKuan-Ying Lee    ((x & 0x0000000000ff0000) << 24) |   \
92*79939c4aSKuan-Ying Lee    ((x & 0x00000000ff000000) <<  8) |   \
93*79939c4aSKuan-Ying Lee    ((x & 0x000000ff00000000) >>  8) |   \
94*79939c4aSKuan-Ying Lee    ((x & 0x0000ff0000000000) >> 24) |   \
95*79939c4aSKuan-Ying Lee    ((x & 0x00ff000000000000) >> 40) |   \
96*79939c4aSKuan-Ying Lee    ((x & 0xff00000000000000) >> 56))
97*79939c4aSKuan-Ying Lee    return result
98*79939c4aSKuan-Ying Lee
99*79939c4aSKuan-Ying Leedef freelist_ptr_decode(cache, ptr, ptr_addr):
100*79939c4aSKuan-Ying Lee    if constants.LX_CONFIG_SLAB_FREELIST_HARDENED:
101*79939c4aSKuan-Ying Lee        return ptr['v'] ^ cache['random'] ^ swab64(int(ptr_addr))
102*79939c4aSKuan-Ying Lee    else:
103*79939c4aSKuan-Ying Lee        return ptr['v']
104*79939c4aSKuan-Ying Lee
105*79939c4aSKuan-Ying Leedef get_freepointer(cache, obj):
106*79939c4aSKuan-Ying Lee    obj = mm.page_ops().ops.kasan_reset_tag(obj)
107*79939c4aSKuan-Ying Lee    ptr_addr = obj + cache['offset']
108*79939c4aSKuan-Ying Lee    p = ptr_addr.cast(freeptr_t_ptr).dereference()
109*79939c4aSKuan-Ying Lee    return freelist_ptr_decode(cache, p, ptr_addr)
110*79939c4aSKuan-Ying Lee
111*79939c4aSKuan-Ying Leedef loc_exist(loc_track, addr, handle, waste):
112*79939c4aSKuan-Ying Lee    for loc in loc_track:
113*79939c4aSKuan-Ying Lee        if loc['addr'] == addr and loc['handle'] == handle and loc['waste'] == waste:
114*79939c4aSKuan-Ying Lee            return loc
115*79939c4aSKuan-Ying Lee    return None
116*79939c4aSKuan-Ying Lee
117*79939c4aSKuan-Ying Leedef add_location(loc_track, cache, track, orig_size):
118*79939c4aSKuan-Ying Lee    jiffies = gdb.parse_and_eval("jiffies_64")
119*79939c4aSKuan-Ying Lee    age = jiffies - track['when']
120*79939c4aSKuan-Ying Lee    handle = 0
121*79939c4aSKuan-Ying Lee    waste = cache['object_size'] - int(orig_size)
122*79939c4aSKuan-Ying Lee    pid = int(track['pid'])
123*79939c4aSKuan-Ying Lee    cpuid = int(track['cpu'])
124*79939c4aSKuan-Ying Lee    addr = track['addr']
125*79939c4aSKuan-Ying Lee    if constants.LX_CONFIG_STACKDEPOT:
126*79939c4aSKuan-Ying Lee        handle = track['handle']
127*79939c4aSKuan-Ying Lee
128*79939c4aSKuan-Ying Lee    loc = loc_exist(loc_track, addr, handle, waste)
129*79939c4aSKuan-Ying Lee    if loc:
130*79939c4aSKuan-Ying Lee        loc['count'] += 1
131*79939c4aSKuan-Ying Lee        if track['when']:
132*79939c4aSKuan-Ying Lee            loc['sum_time'] += age
133*79939c4aSKuan-Ying Lee            loc['min_time'] = min(loc['min_time'], age)
134*79939c4aSKuan-Ying Lee            loc['max_time'] = max(loc['max_time'], age)
135*79939c4aSKuan-Ying Lee            loc['min_pid'] = min(loc['min_pid'], pid)
136*79939c4aSKuan-Ying Lee            loc['max_pid'] = max(loc['max_pid'], pid)
137*79939c4aSKuan-Ying Lee            loc['cpus'].add(cpuid)
138*79939c4aSKuan-Ying Lee    else:
139*79939c4aSKuan-Ying Lee        loc_track.append({
140*79939c4aSKuan-Ying Lee            'count' : 1,
141*79939c4aSKuan-Ying Lee            'addr' : addr,
142*79939c4aSKuan-Ying Lee            'sum_time' : age,
143*79939c4aSKuan-Ying Lee            'min_time' : age,
144*79939c4aSKuan-Ying Lee            'max_time' : age,
145*79939c4aSKuan-Ying Lee            'min_pid' : pid,
146*79939c4aSKuan-Ying Lee            'max_pid' : pid,
147*79939c4aSKuan-Ying Lee            'handle' : handle,
148*79939c4aSKuan-Ying Lee            'waste' : waste,
149*79939c4aSKuan-Ying Lee            'cpus' : {cpuid}
150*79939c4aSKuan-Ying Lee            }
151*79939c4aSKuan-Ying Lee        )
152*79939c4aSKuan-Ying Lee
153*79939c4aSKuan-Ying Leedef slabtrace(alloc, cache_name):
154*79939c4aSKuan-Ying Lee
155*79939c4aSKuan-Ying Lee    def __fill_map(obj_map, cache, slab):
156*79939c4aSKuan-Ying Lee        p = slab['freelist']
157*79939c4aSKuan-Ying Lee        addr = slab_address(slab)
158*79939c4aSKuan-Ying Lee        while p != gdb.Value(0):
159*79939c4aSKuan-Ying Lee            index = __obj_to_index(cache, addr, p)
160*79939c4aSKuan-Ying Lee            obj_map[index] = True # free objects
161*79939c4aSKuan-Ying Lee            p = get_freepointer(cache, p)
162*79939c4aSKuan-Ying Lee
163*79939c4aSKuan-Ying Lee    # process every slab page on the slab_list (partial and full list)
164*79939c4aSKuan-Ying Lee    def process_slab(loc_track, slab_list, alloc, cache):
165*79939c4aSKuan-Ying Lee        for slab in lists.list_for_each_entry(slab_list, slab_ptr_type, "slab_list"):
166*79939c4aSKuan-Ying Lee            obj_map[:] = [False] * oo_objects(cache['oo'])
167*79939c4aSKuan-Ying Lee            __fill_map(obj_map, cache, slab)
168*79939c4aSKuan-Ying Lee            addr = slab_address(slab)
169*79939c4aSKuan-Ying Lee            for object_pointer in for_each_object(cache, addr, slab['objects']):
170*79939c4aSKuan-Ying Lee                if obj_map[__obj_to_index(cache, addr, object_pointer)] == True:
171*79939c4aSKuan-Ying Lee                    continue
172*79939c4aSKuan-Ying Lee                p = get_track(cache, object_pointer, alloc)
173*79939c4aSKuan-Ying Lee                track = gdb.Value(p).cast(track_type.pointer())
174*79939c4aSKuan-Ying Lee                if alloc == track_alloc:
175*79939c4aSKuan-Ying Lee                    size = get_orig_size(cache, object_pointer)
176*79939c4aSKuan-Ying Lee                else:
177*79939c4aSKuan-Ying Lee                    size = cache['object_size']
178*79939c4aSKuan-Ying Lee                add_location(loc_track, cache, track, size)
179*79939c4aSKuan-Ying Lee                continue
180*79939c4aSKuan-Ying Lee
181*79939c4aSKuan-Ying Lee    slab_caches = gdb.parse_and_eval("slab_caches")
182*79939c4aSKuan-Ying Lee    if mm.page_ops().ops.MAX_NUMNODES > 1:
183*79939c4aSKuan-Ying Lee        nr_node_ids = int(gdb.parse_and_eval("nr_node_ids"))
184*79939c4aSKuan-Ying Lee    else:
185*79939c4aSKuan-Ying Lee        nr_node_ids = 1
186*79939c4aSKuan-Ying Lee
187*79939c4aSKuan-Ying Lee    target_cache = None
188*79939c4aSKuan-Ying Lee    loc_track = []
189*79939c4aSKuan-Ying Lee
190*79939c4aSKuan-Ying Lee    for cache in lists.list_for_each_entry(slab_caches, kmem_cache_ptr_type, 'list'):
191*79939c4aSKuan-Ying Lee        if cache['name'].string() == cache_name:
192*79939c4aSKuan-Ying Lee            target_cache = cache
193*79939c4aSKuan-Ying Lee            break
194*79939c4aSKuan-Ying Lee
195*79939c4aSKuan-Ying Lee    obj_map = [False] * oo_objects(target_cache['oo'])
196*79939c4aSKuan-Ying Lee
197*79939c4aSKuan-Ying Lee    if target_cache['flags'] & SLAB_STORE_USER:
198*79939c4aSKuan-Ying Lee        for i in range(0, nr_node_ids):
199*79939c4aSKuan-Ying Lee            cache_node = target_cache['node'][i]
200*79939c4aSKuan-Ying Lee            if cache_node['nr_slabs']['counter'] == 0:
201*79939c4aSKuan-Ying Lee                continue
202*79939c4aSKuan-Ying Lee            process_slab(loc_track, cache_node['partial'], alloc, target_cache)
203*79939c4aSKuan-Ying Lee            process_slab(loc_track, cache_node['full'], alloc, target_cache)
204*79939c4aSKuan-Ying Lee    else:
205*79939c4aSKuan-Ying Lee        raise gdb.GdbError("SLAB_STORE_USER is not set in %s" % target_cache['name'].string())
206*79939c4aSKuan-Ying Lee
207*79939c4aSKuan-Ying Lee    for loc in sorted(loc_track, key=lambda x:x['count'], reverse=True):
208*79939c4aSKuan-Ying Lee        if loc['addr']:
209*79939c4aSKuan-Ying Lee            addr = loc['addr'].cast(utils.get_ulong_type().pointer())
210*79939c4aSKuan-Ying Lee            gdb.write("%d %s" % (loc['count'], str(addr).split(' ')[-1]))
211*79939c4aSKuan-Ying Lee        else:
212*79939c4aSKuan-Ying Lee            gdb.write("%d <not-available>" % loc['count'])
213*79939c4aSKuan-Ying Lee
214*79939c4aSKuan-Ying Lee        if loc['waste']:
215*79939c4aSKuan-Ying Lee            gdb.write(" waste=%d/%d" % (loc['count'] * loc['waste'], loc['waste']))
216*79939c4aSKuan-Ying Lee
217*79939c4aSKuan-Ying Lee        if loc['sum_time'] != loc['min_time']:
218*79939c4aSKuan-Ying Lee            gdb.write(" age=%d/%d/%d" % (loc['min_time'], loc['sum_time']/loc['count'], loc['max_time']))
219*79939c4aSKuan-Ying Lee        else:
220*79939c4aSKuan-Ying Lee            gdb.write(" age=%d" % loc['min_time'])
221*79939c4aSKuan-Ying Lee
222*79939c4aSKuan-Ying Lee        if loc['min_pid'] != loc['max_pid']:
223*79939c4aSKuan-Ying Lee            gdb.write(" pid=%d-%d" % (loc['min_pid'], loc['max_pid']))
224*79939c4aSKuan-Ying Lee        else:
225*79939c4aSKuan-Ying Lee            gdb.write(" pid=%d" % loc['min_pid'])
226*79939c4aSKuan-Ying Lee
227*79939c4aSKuan-Ying Lee        if constants.LX_NR_CPUS > 1:
228*79939c4aSKuan-Ying Lee            nr_cpu = gdb.parse_and_eval('__num_online_cpus')['counter']
229*79939c4aSKuan-Ying Lee            if nr_cpu > 1:
230*79939c4aSKuan-Ying Lee                gdb.write(" cpus=")
231*79939c4aSKuan-Ying Lee                for i in loc['cpus']:
232*79939c4aSKuan-Ying Lee                    gdb.write("%d," % i)
233*79939c4aSKuan-Ying Lee        gdb.write("\n")
234*79939c4aSKuan-Ying Lee        if constants.LX_CONFIG_STACKDEPOT:
235*79939c4aSKuan-Ying Lee            if loc['handle']:
236*79939c4aSKuan-Ying Lee                stackdepot.stack_depot_print(loc['handle'])
237*79939c4aSKuan-Ying Lee        gdb.write("\n")
238*79939c4aSKuan-Ying Lee
239*79939c4aSKuan-Ying Leedef help():
240*79939c4aSKuan-Ying Lee    t = """Usage: lx-slabtrace --cache_name [cache_name] [Options]
241*79939c4aSKuan-Ying Lee    Options:
242*79939c4aSKuan-Ying Lee        --alloc
243*79939c4aSKuan-Ying Lee            print information of allocation trace of the allocated objects
244*79939c4aSKuan-Ying Lee        --free
245*79939c4aSKuan-Ying Lee            print information of freeing trace of the allocated objects
246*79939c4aSKuan-Ying Lee    Example:
247*79939c4aSKuan-Ying Lee        lx-slabtrace --cache_name kmalloc-1k --alloc
248*79939c4aSKuan-Ying Lee        lx-slabtrace --cache_name kmalloc-1k --free\n"""
249*79939c4aSKuan-Ying Lee    gdb.write("Unrecognized command\n")
250*79939c4aSKuan-Ying Lee    raise gdb.GdbError(t)
251*79939c4aSKuan-Ying Lee
252*79939c4aSKuan-Ying Leeclass LxSlabTrace(gdb.Command):
253*79939c4aSKuan-Ying Lee    """Show specific cache slabtrace"""
254*79939c4aSKuan-Ying Lee
255*79939c4aSKuan-Ying Lee    def __init__(self):
256*79939c4aSKuan-Ying Lee        super(LxSlabTrace, self).__init__("lx-slabtrace", gdb.COMMAND_DATA)
257*79939c4aSKuan-Ying Lee
258*79939c4aSKuan-Ying Lee    def invoke(self, arg, from_tty):
259*79939c4aSKuan-Ying Lee        if not constants.LX_CONFIG_SLUB_DEBUG:
260*79939c4aSKuan-Ying Lee            raise gdb.GdbError("CONFIG_SLUB_DEBUG is not enabled")
261*79939c4aSKuan-Ying Lee
262*79939c4aSKuan-Ying Lee        argv = gdb.string_to_argv(arg)
263*79939c4aSKuan-Ying Lee        alloc = track_alloc # default show alloc_traces
264*79939c4aSKuan-Ying Lee
265*79939c4aSKuan-Ying Lee        if len(argv) == 3:
266*79939c4aSKuan-Ying Lee            if argv[2] == '--alloc':
267*79939c4aSKuan-Ying Lee                alloc = track_alloc
268*79939c4aSKuan-Ying Lee            elif argv[2] == '--free':
269*79939c4aSKuan-Ying Lee                alloc = track_free
270*79939c4aSKuan-Ying Lee            else:
271*79939c4aSKuan-Ying Lee                help()
272*79939c4aSKuan-Ying Lee        if len(argv) >= 2 and argv[0] == '--cache_name':
273*79939c4aSKuan-Ying Lee            slabtrace(alloc, argv[1])
274*79939c4aSKuan-Ying Lee        else:
275*79939c4aSKuan-Ying Lee            help()
276*79939c4aSKuan-Ying LeeLxSlabTrace()
277*79939c4aSKuan-Ying Lee
278*79939c4aSKuan-Ying Leedef slabinfo():
279*79939c4aSKuan-Ying Lee    nr_node_ids = None
280*79939c4aSKuan-Ying Lee
281*79939c4aSKuan-Ying Lee    if not constants.LX_CONFIG_SLUB_DEBUG:
282*79939c4aSKuan-Ying Lee        raise gdb.GdbError("CONFIG_SLUB_DEBUG is not enabled")
283*79939c4aSKuan-Ying Lee
284*79939c4aSKuan-Ying Lee    def count_free(slab):
285*79939c4aSKuan-Ying Lee        total_free = 0
286*79939c4aSKuan-Ying Lee        for slab in lists.list_for_each_entry(slab, slab_ptr_type, 'slab_list'):
287*79939c4aSKuan-Ying Lee            total_free += int(slab['objects'] - slab['inuse'])
288*79939c4aSKuan-Ying Lee        return total_free
289*79939c4aSKuan-Ying Lee
290*79939c4aSKuan-Ying Lee    gdb.write("{:^18} | {:^20} | {:^12} | {:^12} | {:^8} | {:^11} | {:^13}\n".format('Pointer', 'name', 'active_objs', 'num_objs', 'objsize', 'objperslab', 'pagesperslab'))
291*79939c4aSKuan-Ying Lee    gdb.write("{:-^18} | {:-^20} | {:-^12} | {:-^12} | {:-^8} | {:-^11} | {:-^13}\n".format('', '', '', '', '', '', ''))
292*79939c4aSKuan-Ying Lee
293*79939c4aSKuan-Ying Lee    slab_caches = gdb.parse_and_eval("slab_caches")
294*79939c4aSKuan-Ying Lee    if mm.page_ops().ops.MAX_NUMNODES > 1:
295*79939c4aSKuan-Ying Lee        nr_node_ids = int(gdb.parse_and_eval("nr_node_ids"))
296*79939c4aSKuan-Ying Lee    else:
297*79939c4aSKuan-Ying Lee        nr_node_ids = 1
298*79939c4aSKuan-Ying Lee
299*79939c4aSKuan-Ying Lee    for cache in lists.list_for_each_entry(slab_caches, kmem_cache_ptr_type, 'list'):
300*79939c4aSKuan-Ying Lee        nr_objs = 0
301*79939c4aSKuan-Ying Lee        nr_free = 0
302*79939c4aSKuan-Ying Lee        nr_slabs = 0
303*79939c4aSKuan-Ying Lee        for i in range(0, nr_node_ids):
304*79939c4aSKuan-Ying Lee            cache_node = cache['node'][i]
305*79939c4aSKuan-Ying Lee            try:
306*79939c4aSKuan-Ying Lee                nr_slabs += cache_node['nr_slabs']['counter']
307*79939c4aSKuan-Ying Lee                nr_objs = int(cache_node['total_objects']['counter'])
308*79939c4aSKuan-Ying Lee                nr_free = count_free(cache_node['partial'])
309*79939c4aSKuan-Ying Lee            except:
310*79939c4aSKuan-Ying Lee                raise gdb.GdbError(traceback.format_exc())
311*79939c4aSKuan-Ying Lee        active_objs = nr_objs - nr_free
312*79939c4aSKuan-Ying Lee        num_objs = nr_objs
313*79939c4aSKuan-Ying Lee        active_slabs = nr_slabs
314*79939c4aSKuan-Ying Lee        objects_per_slab = oo_objects(cache['oo'])
315*79939c4aSKuan-Ying Lee        cache_order = oo_order(cache['oo'])
316*79939c4aSKuan-Ying Lee        gdb.write("{:18s} | {:20.19s} | {:12} | {:12} | {:8} | {:11} | {:13}\n".format(hex(cache), cache['name'].string(), str(active_objs), str(num_objs), str(cache['size']), str(objects_per_slab), str(1 << cache_order)))
317*79939c4aSKuan-Ying Lee
318*79939c4aSKuan-Ying Leeclass LxSlabInfo(gdb.Command):
319*79939c4aSKuan-Ying Lee    """Show slabinfo"""
320*79939c4aSKuan-Ying Lee
321*79939c4aSKuan-Ying Lee    def __init__(self):
322*79939c4aSKuan-Ying Lee        super(LxSlabInfo, self).__init__("lx-slabinfo", gdb.COMMAND_DATA)
323*79939c4aSKuan-Ying Lee
324*79939c4aSKuan-Ying Lee    def invoke(self, arg, from_tty):
325*79939c4aSKuan-Ying Lee        slabinfo()
326*79939c4aSKuan-Ying LeeLxSlabInfo()
327