1 # Copyright 2013 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 5 import logging 6 import os 7 8 9 LOGGER = logging.getLogger('dmprof') 10 11 12 class Dump(object): 13 """Represents a heap profile dump.""" 14 def __init__(self): 15 pass 16 17 @property 18 def path(self): 19 raise NotImplementedError 20 21 @property 22 def count(self): 23 raise NotImplementedError 24 25 @property 26 def time(self): 27 raise NotImplementedError 28 29 @property 30 def iter_map(self): 31 raise NotImplementedError 32 33 @property 34 def iter_stacktrace(self): 35 raise NotImplementedError 36 37 def global_stat(self, name): 38 raise NotImplementedError 39 40 @property 41 def run_id(self): 42 raise NotImplementedError 43 44 @property 45 def pagesize(self): 46 raise NotImplementedError 47 48 @property 49 def pageframe_length(self): 50 raise NotImplementedError 51 52 @property 53 def pageframe_encoding(self): 54 raise NotImplementedError 55 56 @property 57 def has_pagecount(self): 58 raise NotImplementedError 59 60 @staticmethod 61 def load(path, log_header='Loading a heap profile dump: '): 62 """Loads a heap profile dump. 63 64 Args: 65 path: A file path string to load. 66 log_header: A preceding string for log messages. 67 68 Returns: 69 A loaded Dump object. 70 71 Raises: 72 ParsingException for invalid heap profile dumps. 73 """ 74 from lib.deep_dump import DeepDump 75 dump = DeepDump(path, os.stat(path).st_mtime) 76 with open(path, 'r') as f: 77 dump.load_file(f, log_header) 78 return dump 79 80 81 class DumpList(object): 82 """Represents a sequence of heap profile dumps. 83 84 Individual dumps are loaded into memory lazily as the sequence is accessed, 85 either while being iterated through or randomly accessed. Loaded dumps are 86 not cached, meaning a newly loaded Dump object is returned every time an 87 element in the list is accessed. 88 """ 89 90 def __init__(self, dump_path_list): 91 self._dump_path_list = dump_path_list 92 93 @staticmethod 94 def load(path_list): 95 return DumpList(path_list) 96 97 def __len__(self): 98 return len(self._dump_path_list) 99 100 def __iter__(self): 101 for dump in self._dump_path_list: 102 yield Dump.load(dump) 103 104 def __getitem__(self, index): 105 return Dump.load(self._dump_path_list[index]) 106