-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.py
70 lines (58 loc) · 2.3 KB
/
memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright 2024.
# This file is part of Amity.
# Amity is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
import tools
log = tools.logger(__name__)
import asyncio, linecache, tracemalloc
class Monitor(object):
def __init__(self, period=None):
self.last_snapshot = None
if period is None:
period = 60*60
self.period = period
self.taskit = tools.Tasker('Memory Monitor')
self.done = False
def log_top(self, snapshot, key_type='lineno', limit=10):
snapshot = snapshot.filter_traces((
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
tracemalloc.Filter(False, "<unknown>"),
))
top_stats = snapshot.statistics(key_type)
log.info(f'Top {limit} lines')
for index, stat in enumerate(top_stats[:limit], 1):
frame = stat.traceback[0]
log.info('#%s: %s:%s: %.1f KiB'
% (index, frame.filename, frame.lineno, stat.size / 1024))
line = linecache.getline(frame.filename, frame.lineno).strip()
if line:
log.info(' %s' % line)
other = top_stats[limit:]
if other:
size = sum(stat.size for stat in other)
log.info('%s other: %.1f KiB' % (len(other), size / 1024))
total = sum(stat.size for stat in top_stats)
log.info('Total allocated size: %.1f KiB' % (total / 1024))
def checkpoint(self):
snapshot = tracemalloc.take_snapshot()
self.log_top(snapshot)
if self.last_snapshot is not None:
top_stats = snapshot.compare_to(self.last_snapshot, 'lineno')
log.info('Top 10 differences')
for stat in top_stats[:10]:
log.info(stat)
self.last_snapshot = snapshot
async def task(self):
while not self.done:
self.checkpoint()
await asyncio.sleep(self.period)
def stop(self):
self.done = True
tracemalloc.stop()
def start(self):
tracemalloc.start()
self.done = False
self.taskit(self.task())
def wait_on(self):
return self.taskit.tasks