-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_frag.py
256 lines (200 loc) · 7 KB
/
mem_frag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import sys, drgn
from drgn import NULL, Object, cast, container_of, execscript, reinterpret, sizeof
from drgn.helpers.linux import *
from drgn.helpers.linux.mm import for_each_page, pfn_to_page, _vmemmap
from drgn.helpers.linux.fs import inode_paths
PGLocked = 1 << prog.constant('PG_locked')
PGReferenced = 1 << prog.constant('PG_referenced')
PGUptodate = 1 << prog.constant('PG_uptodate')
PGDirty = 1 << prog.constant('PG_dirty')
PGLru = 1 << prog.constant('PG_lru')
PGActive = 1 << prog.constant('PG_active')
PGWorkingset = 1 << prog.constant('PG_workingset')
PGWaiters = 1 << prog.constant('PG_waiters')
PGError = 1 << prog.constant('PG_error')
PGSlab = 1 << prog.constant('PG_slab')
PGOwner_priv_1 = 1 << prog.constant('PG_owner_priv_1')
PGArch_1 = 1 << prog.constant('PG_arch_1')
PGReserved = 1 << prog.constant('PG_reserved')
PGPrivate = 1 << prog.constant('PG_private')
PGPrivate_2 = 1 << prog.constant('PG_private_2')
PGWriteback = 1 << prog.constant('PG_writeback')
PGHead = 1 << prog.constant('PG_head')
PGMappedtodisk = 1 << prog.constant('PG_mappedtodisk')
PGReclaim = 1 << prog.constant('PG_reclaim')
PGSwapbacked = 1 << prog.constant('PG_swapbacked')
PGUnevictable = 1 << prog.constant('PG_unevictable')
PGMlocked = 1 << prog.constant('PG_mlocked')
PGUncached = 1 << prog.constant('PG_uncached')
PGHwpoison = 1 << prog.constant('PG_hwpoison')
def page_flags(page):
flags = page.flags
ret = []
if flags & PGHead:
ret += ['head']
if flags & PGSlab:
ret += ['slab']
if flags & PGLocked:
ret += ['locked']
if flags & PGReferenced:
ret += ['referenced']
if flags & PGUptodate:
ret += ['uptodate']
if flags & PGDirty:
ret += ['dirty']
if flags & PGLru:
ret += ['lru']
if flags & PGActive:
ret += ['active']
if flags & PGWorkingset:
ret += ['workingset']
if flags & PGWaiters:
ret += ['waiters']
if flags & PGError:
ret += ['error']
if flags & PGSlab:
ret += ['slab']
if flags & PGOwner_priv_1:
ret += ['owner_priv_1']
if flags & PGArch_1:
ret += ['arch_1']
if flags & PGReserved:
ret += ['reserved']
if flags & PGPrivate:
ret += ['private']
if flags & PGPrivate_2:
ret += ['private_2']
if flags & PGWriteback:
ret += ['writeback']
if flags & PGHead:
ret += ['head']
if flags & PGMappedtodisk:
ret += ['mappedtodisk']
if flags & PGReclaim:
ret += ['reclaim']
if flags & PGSwapbacked:
ret += ['swapbacked']
if flags & PGUnevictable:
ret += ['unevictable']
if flags & PGMlocked:
ret += ['mlocked']
if flags & PGUncached:
ret += ['uncached']
if flags & PGHwpoison:
ret += ['hwpoison']
return ret
def pages_to_GB(pages):
return int(pages) * 4096 / 1024 / 1024 / 1024
def for_each_online_node():
online_mask = prog['node_states'][prog.constant('N_ONLINE')].bits[0]
for nid in range(32):
if online_mask & (1 << nid):
yield prog['node_data'][nid]
def next_page(page):
return Object(prog, page.type_, value=page + 1)
def PageHead(page):
return page.flags & PGHead
def PageSlab(page):
return page.flags & PGSlab
def PageHeadHuge(page):
if PageHead(page):
return next_page(page).compound_dtor == prog.constant('HUGETLB_PAGE_DTOR')
else:
return False
def PageBuddy(page):
type = page.page_type & 0x7ff
return type & 0x80
def compound_order(page):
if PageHead(page):
return int(next_page(page).compound_order)
else:
return 0
def inspect_slab_page(page):
return "slab %s" % page.slab_cache.name.string_().decode('ascii')
def inspect_page(page):
if PageSlab(page):
return inspect_slab_page(page)
desc = ""
if page._refcount.counter > 1:
if int(page.mapping) & 0x1 == 0:
desc = "file (deleted?)"
for p in inode_paths(page.mapping.host):
desc = 'file %s' % p.decode('ascii')
break
else:
desc = "anon (%s)" % page.mem_cgroup.css.cgroup.kn.name.string_().decode('ascii')
return desc
if PageBuddy(page):
return None
return None
def inspect_pfn_range(zone, start_pfn, step):
huge = False
page = cast('struct page *', _vmemmap(prog) + start_pfn)
desc = ''
try:
order = compound_order(page)
huge = PageHeadHuge(page)
if huge:
if order == 18:
desc = '1GB hugepage'
elif order == 9:
desc = '2MB hugepage'
else:
desc = 'unknown?? hugepage'
print(' %-10d (%s)' % (start_pfn, desc))
if not huge:
prev = set()
for pfn in range(start_pfn, start_pfn + step):
page = cast('struct page *', _vmemmap(prog) + pfn)
desc = inspect_page(page)
if desc:
if desc in prev:
continue
print(' %s' % desc)
prev.add(desc)
if len(prev) > 10:
print(' ...')
break
except drgn.FaultError as e:
print('error: %s' % e)
return huge
def main():
# 1GB step
step = 1024 * 1024 * 1024 // 4096
# 2MB step
#step = 2 * 1024 * 1024 // 4096
potential = 0
found = 0
total = -1
for i in range(0, 2):
if prog['hstates'][i].order == 18:
total = prog['hstates'][i].nr_huge_pages
for node in for_each_online_node():
print('Node %d' % node.node_id)
for zid in range(node.nr_zones):
zone = node.node_zones[zid]
managed = int(zone.managed_pages.counter.value_())
if managed == 0:
continue
spanned = int(zone.spanned_pages)
present = int(zone.present_pages)
free = int(zone.vm_stat[prog.constant('NR_FREE_PAGES')].counter.value_())
print(' Zone %s' % zone.name.string_().decode('ascii'))
# print(' spanned %10d (%.1f GB)' % (spanned, pages_to_GB(spanned)))
# print(' present %10d (%.1f GB)' % (present, pages_to_GB(present)))
# print(' managed %10d (%.1f GB)' % (managed, pages_to_GB(managed)))
# print(' free %10d (%.1f Gb, %.1f%%)' %
# (free, pages_to_GB(free), free * 100 / managed))
start = zone.zone_start_pfn
if start % step:
start = start + step - start % step
for pfn in range(start, zone.zone_start_pfn + zone.present_pages,
step):
if pfn + step > zone.zone_start_pfn + zone.present_pages:
break
potential += 1
if inspect_pfn_range(zone, pfn, step):
found += 1
print('--')
print('found %d of %d, potential max %d' % (found, total, potential))
main()