44import winreg
55
66from ctypes import wintypes
7+ from io import BytesIO
78from matplotlib import pyplot as plt , patches
89
910logger = logging .getLogger (__name__ )
@@ -386,7 +387,7 @@ def get_cache_info():
386387 return sorted (cache_info , key = lambda x : x ['level' ])
387388
388389
389- def create_cpu_topology_visualization (p_cores , e_cores , cache_structure ):
390+ def create_cpu_topology_visualization (p_cores , e_cores , cache_structure , display : bool = False ):
390391 plt .style .use ('dark_background' )
391392 fig , ax = plt .subplots (figsize = (20 , 12 ))
392393 ax .set_aspect ('equal' )
@@ -403,9 +404,9 @@ def create_cpu_topology_visualization(p_cores, e_cores, cache_structure):
403404 core_height = 0.8
404405 core_gap = 0.4
405406 x_spacing = core_width + core_gap
406- y_spacing = 1.0
407+ y_spacing = 1.2
407408 l3_height = 0.8
408- l3_spacing = 0.8
409+ l3_spacing = 0
409410
410411 # Calculate layout dimensions
411412 p_cores_per_row = len (p_cores ) // 2
@@ -587,14 +588,22 @@ def format_size(size):
587588 ax .set_ylim (- 4 , max (p_rows , e_rows ) * y_spacing * 3 + margin )
588589 ax .axis ('off' )
589590
590- plt .title ("CPU Topology with Cache Hierarchy" , color = text_color , y = 0.98 )
591+ plt .title (f "CPU Topology with Cache Hierarchy for { get_cpu_name () } " , color = text_color , y = 0.98 )
591592
592593 # Set figure background to dark
593594 fig .patch .set_facecolor ('#1C1C1C' )
594595 ax .set_facecolor ('#1C1C1C' )
595596
596597 plt .tight_layout ()
597- plt .show ()
598+
599+ buf = BytesIO ()
600+ plt .savefig (buf , format = 'png' , facecolor = '#1C1C1C' )
601+ if display :
602+ plt .show ()
603+
604+ plt .close (fig ) # Close the figure to free memory
605+ buf .seek (0 )
606+ return buf
598607
599608
600609if __name__ == '__main__' :
@@ -610,14 +619,17 @@ def format_size(size):
610619 print (f"Scheduling Class { plcass } : { get_cpus_from_affinity (affinity_mask )} " )
611620 print ("\n Cache Information:" )
612621 cache_info = get_cache_info ()
613- for cache in cache_info :
614- cache_type = ['Unified' , 'Instruction' , 'Data' , 'Trace' ][cache ['type' ]]
615- print (f"L{ cache ['level' ]} { cache_type } Cache:" )
616- print (f" Size: { cache ['size' ]/ 1024 :.0f} KB" )
617- print (f" Line Size: { cache ['line_size' ]} bytes" )
618- print (f" Shared by cores: { cache ['cores' ]} " )
622+ try :
623+ for cache in cache_info :
624+ cache_type = ['Unified' , 'Instruction' , 'Data' , 'Trace' ][cache ['type' ]]
625+ print (f"L{ cache ['level' ]} { cache_type } Cache:" )
626+ print (f" Size: { cache ['size' ]/ 1024 :.0f} KB" )
627+ print (f" Line Size: { cache ['line_size' ]} bytes" )
628+ print (f" Shared by cores: { cache ['cores' ]} " )
629+ except Exception :
630+ pass
619631 create_cpu_topology_visualization (get_cpus_from_affinity (p_core_affinity_mask ),
620632 get_cpus_from_affinity (e_core_affinity_mask ),
621- cache_info )
633+ cache_info , True )
622634 except Exception as e :
623635 traceback .print_exc ()
0 commit comments