Created
January 27, 2016 08:40
-
-
Save saurabhsharan/8ba4a97eab36813363d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
import os | |
import sys | |
class Mapping: | |
def __init__(self, line): | |
# 00400000-00401000 r-xp 00000000 00:25 169557646 /afs/ir.stanford.edu/users/s/a/saurabh1/research/testproc | |
components = line.strip().split() | |
addrs = components[0].split("-") | |
self.start_addr, self.end_addr = int(addrs[0], base=16), int(addrs[1], base=16) | |
self.perms = components[1] | |
self.can_read = "r" in self.perms | |
self.can_write = "w" in self.perms | |
self.can_execute = "x" in self.perms | |
self.is_shared = "s" in self.perms | |
self.is_private = "p" in self.perms | |
self.offset = int(components[2], base=16) | |
self.device = components[3] | |
self.inode = int(components[4]) | |
if len(components) > 5: | |
self.pathname = components[5] | |
self.is_anonymous = False | |
else: | |
self.pathname = "[anonymous mapping]" | |
self.is_anonymous = True | |
self.origin_mapping = None | |
class MemRequest: | |
def __init__(self, line): | |
components = line.strip().split() | |
self.instr_addr = int(components[0][:-1], base=16) | |
if components[1] == 'W': | |
self.is_write = True | |
self.is_read = False | |
else: | |
self.is_write = False | |
self.is_read = True | |
self.addr = int(components[2], base=16) | |
def find_origin_mappings(strace_filename, addr_map): | |
for line in open(strace_filename): | |
if "mmap" not in line: | |
continue | |
components = line.strip().split() | |
iaddr = int(components[0][1:-1], base=16) | |
returned_addr = int(components[-1], base=16) | |
mapping = mapping_for_address(returned_addr, addr_map) | |
assert mapping, "mapping is None, returned_addr = %r" % hex(returned_addr) | |
# assert mapping.start_addr == returned_addr, "start_addr = %r, returned_addr = %r" % (hex(mapping.start_addr), hex(returned_addr)) | |
assert mapping_for_address(iaddr, addr_map), "could not find mapping for iaddr %r" % hex(iaddr) | |
mapping.origin_mapping = mapping_for_address(iaddr, addr_map) | |
def addrs_map(addrs_filename): | |
result = {} | |
for line in open(addrs_filename): | |
m = Mapping(line) | |
result[(m.start_addr, m.end_addr)] = m | |
return result | |
def mapping_for_address(addr, addr_map): | |
for (start_addr, end_addr) in addr_map: | |
if start_addr <= addr <= end_addr: | |
return addr_map[(start_addr, end_addr)] | |
return None | |
def main(): | |
addr_map = addrs_map("addrs") | |
trace_file = open("pinatrace.out") | |
# find_origin_mappings("strace.4522", addr_map) | |
# c = collections.Counter() | |
statsd = {} | |
for addr_range in addr_map: | |
# (# reads, # writes) | |
statsd[addr_map[addr_range]] = [Counter(), Counter()] | |
# for (start_addr, end_addr) in addr_map: | |
# if addr_map[(start_addr, end_addr)].origin_mapping: | |
# print hex(start_addr), hex(end_addr), addr_map[(start_addr, end_addr)].origin_mapping.pathname | |
# return | |
for line in trace_file: | |
if "eof" in line: | |
break | |
req = MemRequest(line) | |
mapping = mapping_for_address(req.addr, addr_map) | |
imapping = mapping_for_address(req.instr_addr, addr_map) | |
# verifies that the instruction address for each memory read/write comes from a page marked executable | |
# if this assertion fails, something is seriously fucked | |
assert imapping.can_execute | |
if req.is_read: | |
statsd[mapping][0][imapping] += 1 | |
else: | |
statsd[mapping][1][imapping] += 1 | |
for addr_range in addr_map: | |
m = addr_map[addr_range] | |
total_reads = sum(statsd[m][0][k] for k in statsd[m][0]) | |
total_writes = sum(statsd[m][1][k] for k in statsd[m][1]) | |
if total_writes == 0 and total_writes == 0: | |
continue | |
# if all(statsd[(start_addr, end_addr)][i] == 0 for i in range(len(statsd[(start_addr, end_addr)]))): | |
# continue | |
print m.pathname, " ", hex(m.start_addr), "-", hex(m.end_addr) | |
print "Number of reads into this mapping:", total_reads | |
for (m2, count) in statsd[m][0].most_common(): | |
print "\t", m2.pathname.split("/")[-1], ":", count | |
print "Number of writes into this mapping:", total_writes | |
for (m2, count) in statsd[m][1].most_common(): | |
print "\t", m2.pathname.split("/")[-1], ":", count | |
# if "testproc" in mapping_for_address(req.instr_addr, addr_map).pathname: | |
# c[mapping] += 1 | |
# if "num" in mapping.pathname: | |
# print line | |
# for (m, count) in c.most_common(None): | |
# if m.can_execute: | |
# print "(x) ", | |
# print hex(m.start_addr), hex(m.end_addr), m.pathname, count | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment