| 32 | |
| 33 | |
| 34 | def dump_info(signal=None, frame=None, file=sys.stdout): # pragma: no cover |
| 35 | with redirect_stdout(file): |
| 36 | print("****************************************************") |
| 37 | print("Summary") |
| 38 | print("=======") |
| 39 | |
| 40 | try: |
| 41 | import psutil |
| 42 | except ModuleNotFoundError: |
| 43 | print("(psutil not installed, skipping some debug info)") |
| 44 | else: |
| 45 | p = psutil.Process() |
| 46 | print("num threads: ", p.num_threads()) |
| 47 | if hasattr(p, "num_fds"): |
| 48 | print("num fds: ", p.num_fds()) |
| 49 | print("memory: ", p.memory_info()) |
| 50 | |
| 51 | print() |
| 52 | print("Files") |
| 53 | print("=====") |
| 54 | for i in p.open_files(): |
| 55 | print(i) |
| 56 | |
| 57 | print() |
| 58 | print("Connections") |
| 59 | print("===========") |
| 60 | for i in p.connections(): |
| 61 | print(i) |
| 62 | |
| 63 | print() |
| 64 | print("Threads") |
| 65 | print("=======") |
| 66 | bthreads = [] |
| 67 | for i in threading.enumerate(): |
| 68 | if hasattr(i, "_threadinfo"): |
| 69 | bthreads.append(i) |
| 70 | else: |
| 71 | print(i.name) |
| 72 | bthreads.sort(key=lambda x: getattr(x, "_thread_started", 0)) |
| 73 | for i in bthreads: |
| 74 | print(i._threadinfo()) |
| 75 | |
| 76 | print() |
| 77 | print("Memory") |
| 78 | print("======") |
| 79 | gc.collect() |
| 80 | objs = Counter(str(type(i)) for i in gc.get_objects()) |
| 81 | |
| 82 | for cls, count in objs.most_common(20): |
| 83 | print(f"{count} {cls}") |
| 84 | |
| 85 | print() |
| 86 | print("Memory (mitmproxy only)") |
| 87 | print("=======================") |
| 88 | mitm_objs = Counter({k: v for k, v in objs.items() if "mitmproxy" in k}) |
| 89 | for cls, count in mitm_objs.most_common(20): |
| 90 | print(f"{count} {cls}") |
| 91 | |