Skip to content

Commit 8d62228

Browse files
committed
New command /cpuinfo
Competitive: - Match reset fixed
1 parent 8d54bbf commit 8d62228

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

core/utils/cpu.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import winreg
55

66
from ctypes import wintypes
7+
from io import BytesIO
78
from matplotlib import pyplot as plt, patches
89

910
logger = 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

600609
if __name__ == '__main__':
@@ -610,14 +619,17 @@ def format_size(size):
610619
print(f"Scheduling Class {plcass}: {get_cpus_from_affinity(affinity_mask)}")
611620
print("\nCache 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()

plugins/competitive/listener.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ async def onGameEvent(self, server: Server, data: dict) -> None:
420420

421421
@event(name="onMatchFinished")
422422
async def onMatchFinished(self, server: Server, data: dict) -> None:
423+
match = self.matches[server.name].pop(data['match_id'])
424+
match.finished = datetime.now(timezone.utc)
425+
423426
# unlock players
424427
if data['match_id'] == GLOBAL_MATCH_ID:
425428
for ucid in self.in_match[server.name].keys():
@@ -454,7 +457,6 @@ async def check_matches(self):
454457
for server in self.bot.servers.values():
455458
if server.status != Status.RUNNING:
456459
continue
457-
finished: list[Match] = []
458460
for match in self.matches.get(server.name, {}).values():
459461
winner: Side = match.winner
460462
if match.finished or not winner:
@@ -476,17 +478,10 @@ async def check_matches(self):
476478
for player in match.teams[Side.BLUE] + match.teams[Side.RED]:
477479
message += f"- {player.name}: {self.calculate_rating(await self.get_rating(player))}\n"
478480
asyncio.create_task(self.inform_players(match, message, 60))
479-
finished.append(match)
480481

481482
asyncio.create_task(self.bot.bus.send_to_node({
482483
"command": "onMatchFinished",
483484
"match_id": match.match_id,
484485
"winner": winner.name,
485486
"server_name": server.name
486487
}))
487-
488-
# cleanup
489-
for match in finished:
490-
match.finished = datetime.now(timezone.utc)
491-
if match.match_id != GLOBAL_MATCH_ID:
492-
del self.matches[server.name][match.match_id]

plugins/serverstats/commands.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ async def serverstats(self, interaction: discord.Interaction,
133133
except ValueNotInRange as ex:
134134
await interaction.followup.send(ex, ephemeral=utils.get_ephemeral(interaction))
135135

136+
@command(description='Shows CPU topology')
137+
@app_commands.guild_only()
138+
@utils.app_has_role('Admin')
139+
async def cpuinfo(self, interaction: discord.Interaction):
140+
p_core_affinity_mask = utils.get_p_core_affinity()
141+
e_core_affinity_mask = utils.get_e_core_affinity()
142+
buffer = utils.create_cpu_topology_visualization(utils.get_cpus_from_affinity(p_core_affinity_mask),
143+
utils.get_cpus_from_affinity(e_core_affinity_mask),
144+
utils.get_cache_info())
145+
try:
146+
discord.File(fp=buffer, filename='cpuinfo.png')
147+
# noinspection PyUnresolvedReferences
148+
await interaction.response.send_message(file=discord.File(fp=buffer, filename='cpuinfo.png'))
149+
finally:
150+
buffer.close()
151+
136152
@tasks.loop(hours=12.0)
137153
async def cleanup(self):
138154
async with self.apool.connection() as conn:

0 commit comments

Comments
 (0)