native_heap.py revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1# Copyright 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from memory_inspector.core import stacktrace 6 7 8class NativeHeap(object): 9 """A snapshot of outstanding (i.e. not freed) native allocations. 10 11 This is typically obtained by calling |backends.Process|.DumpNativeHeap() 12 """ 13 14 def __init__(self): 15 self.allocations = [] 16 self.stack_frames = {} # absolute_address (int) -> |stacktrace.Frame|. 17 18 def Add(self, allocation): 19 assert(isinstance(allocation, Allocation)) 20 self.allocations += [allocation] 21 22 def GetStackFrame(self, absolute_addr): 23 assert(isinstance(absolute_addr, int)) 24 stack_frame = self.stack_frames.get(absolute_addr) 25 if not stack_frame: 26 stack_frame = stacktrace.Frame(absolute_addr) 27 self.stack_frames[absolute_addr] = stack_frame 28 return stack_frame 29 30 31class Allocation(object): 32 """A Native allocation, modeled in a size*count fashion. 33 34 |count| is the number of identical stack_traces which performed the allocation 35 of |size| bytes. 36 """ 37 38 def __init__(self, size, count, stack_trace): 39 assert(isinstance(stack_trace, stacktrace.Stacktrace)) 40 self.size = size # in bytes. 41 self.count = count 42 self.stack_trace = stack_trace 43 44 @property 45 def total_size(self): 46 return self.size * self.count 47 48 def __str__(self): 49 return '%d x %d : %s' % (self.count, self.size, self.stack_trace) 50