tracemalloc.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. from collections.abc import Sequence, Iterable
  2. from functools import total_ordering
  3. import fnmatch
  4. import linecache
  5. import os.path
  6. import pickle
  7. # Import types and functions implemented in C
  8. from _tracemalloc import *
  9. from _tracemalloc import _get_object_traceback, _get_traces
  10. def _format_size(size, sign):
  11. for unit in ('B', 'KiB', 'MiB', 'GiB', 'TiB'):
  12. if abs(size) < 100 and unit != 'B':
  13. # 3 digits (xx.x UNIT)
  14. if sign:
  15. return "%+.1f %s" % (size, unit)
  16. else:
  17. return "%.1f %s" % (size, unit)
  18. if abs(size) < 10 * 1024 or unit == 'TiB':
  19. # 4 or 5 digits (xxxx UNIT)
  20. if sign:
  21. return "%+.0f %s" % (size, unit)
  22. else:
  23. return "%.0f %s" % (size, unit)
  24. size /= 1024
  25. class Statistic:
  26. """
  27. Statistic difference on memory allocations between two Snapshot instance.
  28. """
  29. __slots__ = ('traceback', 'size', 'count')
  30. def __init__(self, traceback, size, count):
  31. self.traceback = traceback
  32. self.size = size
  33. self.count = count
  34. def __hash__(self):
  35. return hash((self.traceback, self.size, self.count))
  36. def __eq__(self, other):
  37. return (self.traceback == other.traceback
  38. and self.size == other.size
  39. and self.count == other.count)
  40. def __str__(self):
  41. text = ("%s: size=%s, count=%i"
  42. % (self.traceback,
  43. _format_size(self.size, False),
  44. self.count))
  45. if self.count:
  46. average = self.size / self.count
  47. text += ", average=%s" % _format_size(average, False)
  48. return text
  49. def __repr__(self):
  50. return ('<Statistic traceback=%r size=%i count=%i>'
  51. % (self.traceback, self.size, self.count))
  52. def _sort_key(self):
  53. return (self.size, self.count, self.traceback)
  54. class StatisticDiff:
  55. """
  56. Statistic difference on memory allocations between an old and a new
  57. Snapshot instance.
  58. """
  59. __slots__ = ('traceback', 'size', 'size_diff', 'count', 'count_diff')
  60. def __init__(self, traceback, size, size_diff, count, count_diff):
  61. self.traceback = traceback
  62. self.size = size
  63. self.size_diff = size_diff
  64. self.count = count
  65. self.count_diff = count_diff
  66. def __hash__(self):
  67. return hash((self.traceback, self.size, self.size_diff,
  68. self.count, self.count_diff))
  69. def __eq__(self, other):
  70. return (self.traceback == other.traceback
  71. and self.size == other.size
  72. and self.size_diff == other.size_diff
  73. and self.count == other.count
  74. and self.count_diff == other.count_diff)
  75. def __str__(self):
  76. text = ("%s: size=%s (%s), count=%i (%+i)"
  77. % (self.traceback,
  78. _format_size(self.size, False),
  79. _format_size(self.size_diff, True),
  80. self.count,
  81. self.count_diff))
  82. if self.count:
  83. average = self.size / self.count
  84. text += ", average=%s" % _format_size(average, False)
  85. return text
  86. def __repr__(self):
  87. return ('<StatisticDiff traceback=%r size=%i (%+i) count=%i (%+i)>'
  88. % (self.traceback, self.size, self.size_diff,
  89. self.count, self.count_diff))
  90. def _sort_key(self):
  91. return (abs(self.size_diff), self.size,
  92. abs(self.count_diff), self.count,
  93. self.traceback)
  94. def _compare_grouped_stats(old_group, new_group):
  95. statistics = []
  96. for traceback, stat in new_group.items():
  97. previous = old_group.pop(traceback, None)
  98. if previous is not None:
  99. stat = StatisticDiff(traceback,
  100. stat.size, stat.size - previous.size,
  101. stat.count, stat.count - previous.count)
  102. else:
  103. stat = StatisticDiff(traceback,
  104. stat.size, stat.size,
  105. stat.count, stat.count)
  106. statistics.append(stat)
  107. for traceback, stat in old_group.items():
  108. stat = StatisticDiff(traceback, 0, -stat.size, 0, -stat.count)
  109. statistics.append(stat)
  110. return statistics
  111. @total_ordering
  112. class Frame:
  113. """
  114. Frame of a traceback.
  115. """
  116. __slots__ = ("_frame",)
  117. def __init__(self, frame):
  118. # frame is a tuple: (filename: str, lineno: int)
  119. self._frame = frame
  120. @property
  121. def filename(self):
  122. return self._frame[0]
  123. @property
  124. def lineno(self):
  125. return self._frame[1]
  126. def __eq__(self, other):
  127. return (self._frame == other._frame)
  128. def __lt__(self, other):
  129. return (self._frame < other._frame)
  130. def __hash__(self):
  131. return hash(self._frame)
  132. def __str__(self):
  133. return "%s:%s" % (self.filename, self.lineno)
  134. def __repr__(self):
  135. return "<Frame filename=%r lineno=%r>" % (self.filename, self.lineno)
  136. @total_ordering
  137. class Traceback(Sequence):
  138. """
  139. Sequence of Frame instances sorted from the oldest frame
  140. to the most recent frame.
  141. """
  142. __slots__ = ("_frames",)
  143. def __init__(self, frames):
  144. Sequence.__init__(self)
  145. # frames is a tuple of frame tuples: see Frame constructor for the
  146. # format of a frame tuple; it is reversed, because _tracemalloc
  147. # returns frames sorted from most recent to oldest, but the
  148. # Python API expects oldest to most recent
  149. self._frames = tuple(reversed(frames))
  150. def __len__(self):
  151. return len(self._frames)
  152. def __getitem__(self, index):
  153. if isinstance(index, slice):
  154. return tuple(Frame(trace) for trace in self._frames[index])
  155. else:
  156. return Frame(self._frames[index])
  157. def __contains__(self, frame):
  158. return frame._frame in self._frames
  159. def __hash__(self):
  160. return hash(self._frames)
  161. def __eq__(self, other):
  162. return (self._frames == other._frames)
  163. def __lt__(self, other):
  164. return (self._frames < other._frames)
  165. def __str__(self):
  166. return str(self[0])
  167. def __repr__(self):
  168. return "<Traceback %r>" % (tuple(self),)
  169. def format(self, limit=None, most_recent_first=False):
  170. lines = []
  171. if limit is not None:
  172. if limit > 0:
  173. frame_slice = self[-limit:]
  174. else:
  175. frame_slice = self[:limit]
  176. else:
  177. frame_slice = self
  178. if most_recent_first:
  179. frame_slice = reversed(frame_slice)
  180. for frame in frame_slice:
  181. lines.append(' File "%s", line %s'
  182. % (frame.filename, frame.lineno))
  183. line = linecache.getline(frame.filename, frame.lineno).strip()
  184. if line:
  185. lines.append(' %s' % line)
  186. return lines
  187. def get_object_traceback(obj):
  188. """
  189. Get the traceback where the Python object *obj* was allocated.
  190. Return a Traceback instance.
  191. Return None if the tracemalloc module is not tracing memory allocations or
  192. did not trace the allocation of the object.
  193. """
  194. frames = _get_object_traceback(obj)
  195. if frames is not None:
  196. return Traceback(frames)
  197. else:
  198. return None
  199. class Trace:
  200. """
  201. Trace of a memory block.
  202. """
  203. __slots__ = ("_trace",)
  204. def __init__(self, trace):
  205. # trace is a tuple: (domain: int, size: int, traceback: tuple).
  206. # See Traceback constructor for the format of the traceback tuple.
  207. self._trace = trace
  208. @property
  209. def domain(self):
  210. return self._trace[0]
  211. @property
  212. def size(self):
  213. return self._trace[1]
  214. @property
  215. def traceback(self):
  216. return Traceback(self._trace[2])
  217. def __eq__(self, other):
  218. return (self._trace == other._trace)
  219. def __hash__(self):
  220. return hash(self._trace)
  221. def __str__(self):
  222. return "%s: %s" % (self.traceback, _format_size(self.size, False))
  223. def __repr__(self):
  224. return ("<Trace domain=%s size=%s, traceback=%r>"
  225. % (self.domain, _format_size(self.size, False), self.traceback))
  226. class _Traces(Sequence):
  227. def __init__(self, traces):
  228. Sequence.__init__(self)
  229. # traces is a tuple of trace tuples: see Trace constructor
  230. self._traces = traces
  231. def __len__(self):
  232. return len(self._traces)
  233. def __getitem__(self, index):
  234. if isinstance(index, slice):
  235. return tuple(Trace(trace) for trace in self._traces[index])
  236. else:
  237. return Trace(self._traces[index])
  238. def __contains__(self, trace):
  239. return trace._trace in self._traces
  240. def __eq__(self, other):
  241. return (self._traces == other._traces)
  242. def __repr__(self):
  243. return "<Traces len=%s>" % len(self)
  244. def _normalize_filename(filename):
  245. filename = os.path.normcase(filename)
  246. if filename.endswith('.pyc'):
  247. filename = filename[:-1]
  248. return filename
  249. class BaseFilter:
  250. def __init__(self, inclusive):
  251. self.inclusive = inclusive
  252. def _match(self, trace):
  253. raise NotImplementedError
  254. class Filter(BaseFilter):
  255. def __init__(self, inclusive, filename_pattern,
  256. lineno=None, all_frames=False, domain=None):
  257. super().__init__(inclusive)
  258. self.inclusive = inclusive
  259. self._filename_pattern = _normalize_filename(filename_pattern)
  260. self.lineno = lineno
  261. self.all_frames = all_frames
  262. self.domain = domain
  263. @property
  264. def filename_pattern(self):
  265. return self._filename_pattern
  266. def _match_frame_impl(self, filename, lineno):
  267. filename = _normalize_filename(filename)
  268. if not fnmatch.fnmatch(filename, self._filename_pattern):
  269. return False
  270. if self.lineno is None:
  271. return True
  272. else:
  273. return (lineno == self.lineno)
  274. def _match_frame(self, filename, lineno):
  275. return self._match_frame_impl(filename, lineno) ^ (not self.inclusive)
  276. def _match_traceback(self, traceback):
  277. if self.all_frames:
  278. if any(self._match_frame_impl(filename, lineno)
  279. for filename, lineno in traceback):
  280. return self.inclusive
  281. else:
  282. return (not self.inclusive)
  283. else:
  284. filename, lineno = traceback[0]
  285. return self._match_frame(filename, lineno)
  286. def _match(self, trace):
  287. domain, size, traceback = trace
  288. res = self._match_traceback(traceback)
  289. if self.domain is not None:
  290. if self.inclusive:
  291. return res and (domain == self.domain)
  292. else:
  293. return res or (domain != self.domain)
  294. return res
  295. class DomainFilter(BaseFilter):
  296. def __init__(self, inclusive, domain):
  297. super().__init__(inclusive)
  298. self._domain = domain
  299. @property
  300. def domain(self):
  301. return self._domain
  302. def _match(self, trace):
  303. domain, size, traceback = trace
  304. return (domain == self.domain) ^ (not self.inclusive)
  305. class Snapshot:
  306. """
  307. Snapshot of traces of memory blocks allocated by Python.
  308. """
  309. def __init__(self, traces, traceback_limit):
  310. # traces is a tuple of trace tuples: see _Traces constructor for
  311. # the exact format
  312. self.traces = _Traces(traces)
  313. self.traceback_limit = traceback_limit
  314. def dump(self, filename):
  315. """
  316. Write the snapshot into a file.
  317. """
  318. with open(filename, "wb") as fp:
  319. pickle.dump(self, fp, pickle.HIGHEST_PROTOCOL)
  320. @staticmethod
  321. def load(filename):
  322. """
  323. Load a snapshot from a file.
  324. """
  325. with open(filename, "rb") as fp:
  326. return pickle.load(fp)
  327. def _filter_trace(self, include_filters, exclude_filters, trace):
  328. if include_filters:
  329. if not any(trace_filter._match(trace)
  330. for trace_filter in include_filters):
  331. return False
  332. if exclude_filters:
  333. if any(not trace_filter._match(trace)
  334. for trace_filter in exclude_filters):
  335. return False
  336. return True
  337. def filter_traces(self, filters):
  338. """
  339. Create a new Snapshot instance with a filtered traces sequence, filters
  340. is a list of Filter or DomainFilter instances. If filters is an empty
  341. list, return a new Snapshot instance with a copy of the traces.
  342. """
  343. if not isinstance(filters, Iterable):
  344. raise TypeError("filters must be a list of filters, not %s"
  345. % type(filters).__name__)
  346. if filters:
  347. include_filters = []
  348. exclude_filters = []
  349. for trace_filter in filters:
  350. if trace_filter.inclusive:
  351. include_filters.append(trace_filter)
  352. else:
  353. exclude_filters.append(trace_filter)
  354. new_traces = [trace for trace in self.traces._traces
  355. if self._filter_trace(include_filters,
  356. exclude_filters,
  357. trace)]
  358. else:
  359. new_traces = self.traces._traces.copy()
  360. return Snapshot(new_traces, self.traceback_limit)
  361. def _group_by(self, key_type, cumulative):
  362. if key_type not in ('traceback', 'filename', 'lineno'):
  363. raise ValueError("unknown key_type: %r" % (key_type,))
  364. if cumulative and key_type not in ('lineno', 'filename'):
  365. raise ValueError("cumulative mode cannot by used "
  366. "with key type %r" % key_type)
  367. stats = {}
  368. tracebacks = {}
  369. if not cumulative:
  370. for trace in self.traces._traces:
  371. domain, size, trace_traceback = trace
  372. try:
  373. traceback = tracebacks[trace_traceback]
  374. except KeyError:
  375. if key_type == 'traceback':
  376. frames = trace_traceback
  377. elif key_type == 'lineno':
  378. frames = trace_traceback[:1]
  379. else: # key_type == 'filename':
  380. frames = ((trace_traceback[0][0], 0),)
  381. traceback = Traceback(frames)
  382. tracebacks[trace_traceback] = traceback
  383. try:
  384. stat = stats[traceback]
  385. stat.size += size
  386. stat.count += 1
  387. except KeyError:
  388. stats[traceback] = Statistic(traceback, size, 1)
  389. else:
  390. # cumulative statistics
  391. for trace in self.traces._traces:
  392. domain, size, trace_traceback = trace
  393. for frame in trace_traceback:
  394. try:
  395. traceback = tracebacks[frame]
  396. except KeyError:
  397. if key_type == 'lineno':
  398. frames = (frame,)
  399. else: # key_type == 'filename':
  400. frames = ((frame[0], 0),)
  401. traceback = Traceback(frames)
  402. tracebacks[frame] = traceback
  403. try:
  404. stat = stats[traceback]
  405. stat.size += size
  406. stat.count += 1
  407. except KeyError:
  408. stats[traceback] = Statistic(traceback, size, 1)
  409. return stats
  410. def statistics(self, key_type, cumulative=False):
  411. """
  412. Group statistics by key_type. Return a sorted list of Statistic
  413. instances.
  414. """
  415. grouped = self._group_by(key_type, cumulative)
  416. statistics = list(grouped.values())
  417. statistics.sort(reverse=True, key=Statistic._sort_key)
  418. return statistics
  419. def compare_to(self, old_snapshot, key_type, cumulative=False):
  420. """
  421. Compute the differences with an old snapshot old_snapshot. Get
  422. statistics as a sorted list of StatisticDiff instances, grouped by
  423. group_by.
  424. """
  425. new_group = self._group_by(key_type, cumulative)
  426. old_group = old_snapshot._group_by(key_type, cumulative)
  427. statistics = _compare_grouped_stats(old_group, new_group)
  428. statistics.sort(reverse=True, key=StatisticDiff._sort_key)
  429. return statistics
  430. def take_snapshot():
  431. """
  432. Take a snapshot of traces of memory blocks allocated by Python.
  433. """
  434. if not is_tracing():
  435. raise RuntimeError("the tracemalloc module must be tracing memory "
  436. "allocations to take a snapshot")
  437. traces = _get_traces()
  438. traceback_limit = get_traceback_limit()
  439. return Snapshot(traces, traceback_limit)