Skip to content

Instantly share code, notes, and snippets.

@smihir
Created November 27, 2015 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smihir/fd455f2f541045cfcebc to your computer and use it in GitHub Desktop.
Save smihir/fd455f2f541045cfcebc to your computer and use it in GitHub Desktop.
BuddyAllocator Analysis Scripts
- Data to be collected on Ubuntu vm running on VirtualBox. (2 GB RAM)
#uname -a
Linux adminuser-VirtualBox 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:08:14 UTC 2014 i686 i686 i686 GNU/Linux
- The 'heart' of the zoned buddy allocator is the following function(in mm/page_alloc.c)
struct page *
__alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
struct zonelist *zonelist, nodemask_t *nodemask)
- The following tracepoint is present in this function:
trace_mm_page_alloc(page, order, gfp_mask, migratetype)
This tracepoint is enabled by "kmem:mm_page_alloc" ftrace event
- ftrace events can be enabled at boottime by:
trace_event=[event-list] trace_buf_size=400M
event-list is a comma separated list of events. See section 2.1 for event
format.(https://www.kernel.org/doc/Documentation/trace/events.txt)
- Strategy to collect the ftrace event logs:
1. Enable the event at boottime
trace_event=kmem:mm_page_alloc
2. After the Machine boots and we can access shell, collect the boottime event logs
trace-cmd extract
3. Start the tracer again to collect the logs and browse some websites on chrome
#sudo trace-cmd record -e kmem:mm_page_alloc
4. Store the logs, and get the kernel logs as well
import sys
import re
import copy
import plotly.plotly as py
import plotly.graph_objs as go
def parse(fhandle):
mm_page_alloc = re.compile('mm_page_alloc')
regex_ts = re.compile('[0-9]+\.[0-9]+')
regex_procname = re.compile('([^ ].+)[-]')
regex_order = re.compile('order=([0-9]+)')
parsed_trace = list()
for line in fhandle:
if mm_page_alloc.search(line):
item = list()
metadata, trace = mm_page_alloc.split(line, maxsplit = 2)
ts = regex_ts.search(metadata)
item.append(float(ts.group(0)))
procname = regex_procname.search(metadata)
item.append(procname.group(1))
order = regex_order.search(trace)
item.append(int(order.group(1)))
parsed_trace.append(item)
#print ts.group(0), procname.group(1), order.group(1)
return parsed_trace
if __name__ == "__main__":
fname = sys.argv[1]
with open(fname) as f:
trace = parse(f)
quantum_start = None
order_in_quantum = [0] * 10
quantum = list()
for t in trace:
if quantum_start is None:
quantum_start = t[0]
if quantum_start + 1 < t[0]:
#print "new quantum starts here"
quantum_element = list()
quantum_element.append(quantum_start)
quantum_element.append(copy.deepcopy(order_in_quantum))
quantum.append(quantum_element)
quantum_start = t[0]
#order_in_quantum = [0] * 10
order_in_quantum[t[2]] += 1
data = list()
for i in range(10):
trace = go.Scatter(
y = [q[1][i] for q in quantum],
x = [q[0] + 1 for q in quantum],
mode = 'lines',
name = 'order-' + str(i) + ' ' + 'allocations'
)
data.append(trace)
# Plot and embed in ipython notebook!
py.plot(data, filename='buddy-allocation-analysis')
print "Done"
#!/bin/bash
trace-cmd extract && mv trace.dat trace-boot.dat && trace-cmd record -e kmem:mm_page_alloc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment