@@ -44,9 +44,18 @@ def for_each_child_cache(root_cache: drgn.Object) -> Iterable[drgn.Object]:
4444 "memcg_params.children_node" )
4545
4646
47+ def for_each_node (cache : drgn .Object ) -> Iterable [drgn .Object ]:
48+ assert sdb .type_canonical_name (cache .type_ ) == 'struct kmem_cache *'
49+ node_num = sdb .get_object ('nr_node_ids' )
50+ for i in range (node_num ):
51+ yield cache .node [i ]
52+
53+
4754def nr_slabs (cache : drgn .Object ) -> int :
4855 assert sdb .type_canonical_name (cache .type_ ) == 'struct kmem_cache *'
49- nslabs : int = cache .node [0 ].nr_slabs .counter .value_ ()
56+ nslabs = 0
57+ for node in for_each_node (cache ):
58+ nslabs += node .nr_slabs .counter .value_ ()
5059 if is_root_cache (cache ):
5160 for child in for_each_child_cache (cache ):
5261 nslabs += nr_slabs (child )
@@ -78,7 +87,9 @@ def total_memory(cache: drgn.Object) -> int:
7887
7988def objs (cache : drgn .Object ) -> int :
8089 assert sdb .type_canonical_name (cache .type_ ) == 'struct kmem_cache *'
81- count : int = cache .node [0 ].total_objects .counter .value_ ()
90+ count = 0
91+ for node in for_each_node (cache ):
92+ count += node .total_objects .counter .value_ ()
8293 if is_root_cache (cache ):
8394 for child in for_each_child_cache (cache ):
8495 count += objs (child )
@@ -87,10 +98,12 @@ def objs(cache: drgn.Object) -> int:
8798
8899def inactive_objs (cache : drgn .Object ) -> int :
89100 assert sdb .type_canonical_name (cache .type_ ) == 'struct kmem_cache *'
90- node = cache .node [0 ].partial # assumption nr_node_ids == 0
91101 free = 0
92- for page in list_for_each_entry ("struct page" , node .address_of_ (), "lru" ):
93- free += page .objects .value_ () - page .inuse .value_ ()
102+ for node in for_each_node (cache ):
103+ node_partial = node .partial
104+ for page in list_for_each_entry ("struct page" ,
105+ node_partial .address_of_ (), "lru" ):
106+ free += page .objects .value_ () - page .inuse .value_ ()
94107 if is_root_cache (cache ):
95108 for child in for_each_child_cache (cache ):
96109 free += inactive_objs (child )
@@ -178,9 +191,10 @@ def for_each_freeobj_in_slab(cache: drgn.Object,
178191
179192def for_each_partial_slab_in_cache (cache : drgn .Object ) -> Iterable [drgn .Object ]:
180193 assert sdb .type_canonical_name (cache .type_ ) == 'struct kmem_cache *'
181-
182- node = cache .node [0 ].partial # assumption nr_node_ids == 0
183- yield from list_for_each_entry ("struct page" , node .address_of_ (), "lru" )
194+ for node in for_each_node (cache ):
195+ node_partial = node .partial
196+ yield from list_for_each_entry ("struct page" ,
197+ node_partial .address_of_ (), "lru" )
184198
185199 if is_root_cache (cache ):
186200 for child in for_each_child_cache (cache ):
0 commit comments