Spaces:
Running
Running
Delete mew_log/mem_utils.py
Browse files- mew_log/mem_utils.py +0 -144
mew_log/mem_utils.py
DELETED
@@ -1,144 +0,0 @@
|
|
1 |
-
|
2 |
-
#from ..mew_core import prettify
|
3 |
-
from .ansi_utils import ansi_color_str
|
4 |
-
from .attr_utils import get_caller_info, get_var_value
|
5 |
-
from .table_utils import format_table
|
6 |
-
|
7 |
-
import sys
|
8 |
-
import types
|
9 |
-
import objgraph
|
10 |
-
|
11 |
-
one_mb = 2**20
|
12 |
-
|
13 |
-
def bytes_to_mb(size_in_bytes, inv_bytes_in_mb=(1.0 / (1024 * 1024))):
|
14 |
-
"""
|
15 |
-
Convert size from bytes to megabytes.
|
16 |
-
"""
|
17 |
-
return size_in_bytes * inv_bytes_in_mb
|
18 |
-
|
19 |
-
|
20 |
-
def bytes_to_kb(size_in_bytes, inv_bytes_in_kb=(1.0 / (1024))):
|
21 |
-
"""
|
22 |
-
Convert size from bytes to kilobytes.
|
23 |
-
"""
|
24 |
-
return size_in_bytes * inv_bytes_in_kb
|
25 |
-
|
26 |
-
def format_mem_size_value(size_bytes, use_color=True):
|
27 |
-
size_b = size_bytes
|
28 |
-
size_mb = bytes_to_mb(size_b)
|
29 |
-
size_kb = bytes_to_kb(size_b)
|
30 |
-
|
31 |
-
size_mb_str = f"{size_mb:.4f} MB"
|
32 |
-
size_kb_str = f"{size_kb:.4f} KB"
|
33 |
-
size_b_str = f"{size_b} B"
|
34 |
-
|
35 |
-
# Determine which size to display based on the criteria
|
36 |
-
if size_mb > 0.1: # More than 0.1 MB
|
37 |
-
display_str = size_mb_str
|
38 |
-
elif size_kb > 0.1: # More than 0.1 KB but less than 0.1 MB
|
39 |
-
display_str = size_kb_str
|
40 |
-
else: # Less than 0.1 KB
|
41 |
-
display_str = size_b_str
|
42 |
-
|
43 |
-
# Apply color if use_color is True
|
44 |
-
if use_color:
|
45 |
-
display_str = ansi_color_str(display_str, fg='cyan')
|
46 |
-
|
47 |
-
return f"Mem Size: {display_str}" # Display the selected size
|
48 |
-
|
49 |
-
|
50 |
-
def format_size_bytes(obj, use_color=True):
|
51 |
-
size_b, size_mb = get_mem_size(obj)
|
52 |
-
size_kb = bytes_to_kb(size_b)
|
53 |
-
size_mb_str = f"{size_mb:.4f} MB"
|
54 |
-
size_kb_str = f"{size_kb:.4f} KB"
|
55 |
-
size_b_str = f"{size_b} B"
|
56 |
-
|
57 |
-
# Determine which size to display based on the criteria
|
58 |
-
if size_mb > 0.1: # More than 0.1 MB
|
59 |
-
display_str = size_mb_str
|
60 |
-
elif size_kb > 0.1: # More than 0.1 KB but less than 0.1 MB
|
61 |
-
display_str = size_kb_str
|
62 |
-
else: # Less than 0.1 KB
|
63 |
-
display_str = size_b_str
|
64 |
-
|
65 |
-
# Apply color if use_color is True
|
66 |
-
if use_color:
|
67 |
-
display_str = ansi_color_str(display_str, fg='cyan')
|
68 |
-
|
69 |
-
return f"Mem Size: {display_str}" # Display the selected size
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
def get_mem_size(obj) -> tuple:
|
74 |
-
"""
|
75 |
-
Get the size of an object as a tuple ( bytes , megabytes )
|
76 |
-
"""
|
77 |
-
def _get_mem_size(obj):
|
78 |
-
size = sys.getsizeof(obj)
|
79 |
-
|
80 |
-
if isinstance(obj, types.GeneratorType):
|
81 |
-
# Generators don't have __sizeof__, so calculate size recursively
|
82 |
-
size += sum(_get_mem_size(item) for item in obj)
|
83 |
-
elif isinstance(obj, dict):
|
84 |
-
size += sum(_get_mem_size(key) + _get_mem_size(value) for key, value in obj.items())
|
85 |
-
elif hasattr(obj, '__dict__'):
|
86 |
-
# For objects with __dict__, include the size of their attributes
|
87 |
-
size += _get_mem_size(obj.__dict__)
|
88 |
-
|
89 |
-
return size
|
90 |
-
|
91 |
-
size_b = _get_mem_size(obj)
|
92 |
-
size_mb = bytes_to_mb(size_b)
|
93 |
-
|
94 |
-
return size_b, size_mb
|
95 |
-
|
96 |
-
def get_mem_size_breakdown(obj, do_recursive=False) -> dict:
|
97 |
-
"""
|
98 |
-
Get a breakdown of the sizes of the properties of an object.
|
99 |
-
"""
|
100 |
-
|
101 |
-
size_b, size_mb = get_mem_size(obj) #'name': obj, 'value': get_variable_value(obj)
|
102 |
-
breakdown = { 'name': obj,'type': type(obj).__name__, 'size (B, MB)': f"{size_b} B | {size_mb:.3f} MB", 'value': get_var_value(obj), }
|
103 |
-
|
104 |
-
if do_recursive:
|
105 |
-
if isinstance(obj, types.GeneratorType):
|
106 |
-
for i, item in enumerate(obj):
|
107 |
-
breakdown['generator_'+type(item).__name__][i] = get_mem_size_breakdown(item)
|
108 |
-
elif isinstance(obj, dict):
|
109 |
-
for key, value in obj.items():
|
110 |
-
if key != 'stat':
|
111 |
-
breakdown[f'[{key}]'] = get_mem_size_breakdown(value)
|
112 |
-
elif hasattr(obj, '__dict__'):
|
113 |
-
breakdown['vars'] = {}
|
114 |
-
for key, value in obj.__dict__.items():
|
115 |
-
if key != 'stat':
|
116 |
-
breakdown['vars'][f'[{key}]'] = get_mem_size_breakdown(value)
|
117 |
-
return breakdown
|
118 |
-
|
119 |
-
def save_obj_graph_img(obj):
|
120 |
-
# Generate and save a graph of objects referencing a specific object
|
121 |
-
graph_filename = f'data/image/obj_graph_{obj.__class__.__name__}.png'
|
122 |
-
objgraph.show_refs([obj], filename=graph_filename)
|
123 |
-
return graph_filename
|
124 |
-
|
125 |
-
def take_mem_growth_snapshot():
|
126 |
-
# Take a snapshot of the objects that have grown since the last snapshot
|
127 |
-
growth_info = objgraph.show_growth()
|
128 |
-
if growth_info:
|
129 |
-
# Convert the growth information to a dictionary
|
130 |
-
growth_dict = {}
|
131 |
-
for line in growth_info.split('\n'):
|
132 |
-
if line.strip(): # Skip empty lines
|
133 |
-
parts = line.split()
|
134 |
-
obj_type = parts[0]
|
135 |
-
prev_count = int(parts[1])
|
136 |
-
growth_count = int(parts[2])
|
137 |
-
growth_dict[obj_type] = {'prev_count': prev_count, 'growth_count': growth_count}
|
138 |
-
#print(format_table(growth_dict))
|
139 |
-
return growth_dict
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|