Created
April 2, 2021 06:29
-
-
Save grant-h/d8a544f73a65621cf2587c29b14bf36e to your computer and use it in GitHub Desktop.
Hooking all Python imports to print the increase in pages and memory size (Linux only, Python 3.7 tested)
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
import sys | |
import importlib | |
class ImportInterceptor(importlib.abc.Loader): | |
def __init__(self): | |
pass | |
def find_module(self, fullname, path=None): | |
#sys.stderr.write("LOAD INFO: %s\n" % (fullname)) | |
return self | |
def load_module(self, fullname): | |
with open('/proc/self/statm') as fp: | |
before = int(fp.readline().split(" ")[0]) | |
sys.meta_path = [x for x in sys.meta_path[1:] if x is not self] | |
module = importlib.import_module(fullname) | |
with open('/proc/self/statm') as fp: | |
after = int(fp.readline().split(" ")[0]) | |
delta = after-before | |
delta_kb = delta*4096/1024 | |
total_kb = after*4096/1024 | |
sys.stderr.write("LOAD: %s (+%d pages, %d kB, total %d %d kB)\n" % ( | |
fullname, delta, delta_kb, after, total_kb)) | |
sys.stderr.flush() | |
sys.meta_path = [self] + sys.meta_path | |
return module | |
before = 0 | |
if not hasattr(sys,'frozen'): | |
importer = ImportInterceptor() | |
sys.meta_path = [importer] + sys.meta_path | |
def PathCaller(path): | |
global before | |
with open('/proc/self/statm') as fp: | |
after = int(fp.readline().split(" ")[0]) | |
delta = after-before | |
delta_kb = delta*4096/1024 | |
total_kb = after*4096/1024 | |
sys.stderr.write("LOAD: %s (+%d pages, %d kB, total %d %d kB)\n" % ( | |
path, delta, delta_kb, after, total_kb)) | |
sys.stderr.flush() | |
before = after | |
return sys.path_hooks[1](path) | |
sys.path_hooks = [PathCaller] + sys.path_hooks | |
print("Monitoring imports...") |
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
#!/usr/bin/env python3 | |
# always import before anything else | |
import importmon | |
import sys | |
import os | |
# plus any other imports you want... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment