55
66import psutil
77
8- from logicytics import log
8+ from logicytics import log , config
99
1010# TODO v3.4.1
1111# psutil.virtual_memory(): used, free, percent, total
1212# psutil.swap_memory(): used, free, percent, total
1313
14- # If the file size exceeds this limit, the file will be truncated with a message
15- # Put 0 to disable the limit
16- # TODO v3.4.1: Make this take from config.ini
17- LIMIT_FILE_SIZE = 20 # Always in MiB
14+ LIMIT_FILE_SIZE = config .getint ("DumpMemory Settings" , "file_size_limit" ) # Always in MiB
15+ SAFETY_MARGIN = config .getfloat ("DumpMemory Settings" , "file_size_safety" ) # Always in MiB
16+ if SAFETY_MARGIN < 1 :
17+ log .critical ("Invalid Safety Margin Inputted - Cannot proceed with dump memory" )
18+ exit (1 )
1819
1920
2021# Capture RAM Snapshot
@@ -41,7 +42,7 @@ def capture_ram_snapshot():
4142 log .info ("Capturing RAM Snapshot..." )
4243 memory = psutil .virtual_memory ()
4344 swap = psutil .swap_memory ()
44- with open ("Ram_Snapshot.txt" , "w" ) as file :
45+ with open ("memory_dumps/ Ram_Snapshot.txt" , "w" ) as file :
4546 try :
4647 file .write (f"Total RAM: { memory .total / (1024 ** 3 ):.2f} GB\n " )
4748 file .write (f"Used RAM: { memory .used / (1024 ** 3 ):.2f} GB\n " )
@@ -91,13 +92,13 @@ def gather_system_info():
9192 except Exception as e :
9293 log .error (f"Error gathering system information: { e } " )
9394 sys_info = {'Error' : 'Failed to gather system information' }
94- with open ("SystemRam_Info.txt" , "w" ) as file :
95+ with open ("memory_dumps/ SystemRam_Info.txt" , "w" ) as file :
9596 for key , value in sys_info .items ():
9697 file .write (f"{ key } : { value } \n " )
9798 log .info ("System Information saved to SystemRam_Info.txt" )
9899
99100
100- # Memory Dump (Windows-specific, using psutil)
101+ # Memory Dump
101102def memory_dump ():
102103 """
103104 Perform a memory dump of the current process, capturing detailed metadata for each readable memory region.
@@ -127,12 +128,12 @@ def memory_dump():
127128
128129 try :
129130 process = psutil .Process (pid )
130- with open ("Ram_Dump.txt" , "wb" ) as dump_file :
131+ with open ("memory_dumps/ Ram_Dump.txt" , "wb" ) as dump_file :
131132 total_size = 0
132133 for mem_region in process .memory_maps (grouped = False ):
133134 # Check available disk space
134135 if os .path .exists ("Ram_Dump.txt" ):
135- required_space = LIMIT_FILE_SIZE * 1024 * 1024 * 1.5 # 2x safety margin
136+ required_space = LIMIT_FILE_SIZE * 1024 * 1024 * SAFETY_MARGIN # 2x safety margin
136137 free_space = psutil .disk_usage ("." ).free
137138 if free_space < required_space :
138139 log .error (f"Not enough disk space. Need { required_space / 1024 / 1024 :.2f} MB" )
@@ -206,6 +207,7 @@ def main():
206207 No parameters.
207208 No return value.
208209 """
210+ os .makedirs ("memory_dumps" , exist_ok = True )
209211 log .info ("Starting system memory collection tasks..." )
210212 capture_ram_snapshot ()
211213 gather_system_info ()
0 commit comments