Skip to content

Instantly share code, notes, and snippets.

@Sigmanificient
Created January 25, 2025 10:32
Show Gist options
  • Save Sigmanificient/8547a8803b3d4c936afeba41113c4c0b to your computer and use it in GitHub Desktop.
Save Sigmanificient/8547a8803b3d4c936afeba41113c4c0b to your computer and use it in GitHub Desktop.
Plot CPU freqs in real time
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [pygame psutil])"
import pygame
import psutil
pygame.init()
width = 1800
height = 1000
screen = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()
FREQ_MAX = 5_400
OFFSET_Y = 20
font = pygame.font.Font('font.ttf', 12)
cpu_count = psutil.cpu_count()
freqs = [[0.0] * width] * cpu_count
frame = 0
colors = [
"696969",
"a52a2a",
"2e8b57",
"808000",
"00fa9a",
"ff0000",
"ff8c00",
"ffd700",
"7cfc00",
"ba55d3",
"00ffff",
"0000ff",
"ff00ff",
"1e90ff",
"dda0dd",
"ff1493",
"ffa07a",
"98fb98",
"87cefa",
"faebd7",
]
colors = [
tuple(int(c,16) for c in [col[i:i+2] for i in (0,2,4)])
for col in colors
]
is_running = True
surfw = width - 260
surf = pygame.surface.Surface((surfw, height)).convert_alpha()
avg_len = 100
avg = [[0.0]*avg_len for _ in range(cpu_count)]
avg2_len = 1200
avg2 = [[0.0]*avg2_len for _ in range(cpu_count)]
for y in range(100, FREQ_MAX, 100):
rely = height - round(y * (height / FREQ_MAX))
pygame.draw.rect(surf, (32, 32, 32), ((0, rely), (surfw, 1)), 1)
for y in range(0, FREQ_MAX, 1000):
rely = height - round(y * (height / FREQ_MAX))
pygame.draw.rect(surf, (64, 64, 64), ((0, rely), (surfw, 1)), 1)
while is_running:
screen.fill((0,0,0))
for y in range(100, FREQ_MAX, 100):
rely = height - round(y * (height / FREQ_MAX))
pygame.draw.rect(screen, (32, 32, 32), ((50, rely), (surfw, 1)), 1)
col = (128, 128, 128) if (y % 1000) else (255, 255, 255)
text = font.render(f"{y}", True, col)
text_rect = text.get_rect()
text_rect.right = 40
text_rect.centery = rely
screen.blit(text, text_rect)
for y in range(0, FREQ_MAX, 1000):
rely = height - round(y * (height / FREQ_MAX))
pygame.draw.rect(screen, (64, 64, 64), ((50, rely), (surfw, 1)), 1)
pygame.draw.rect(screen, (64, 64, 64), ((59, 0), (50, height)), 1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
new_freqs = psutil.cpu_freq(percpu=True)
pygame.draw.rect(surf, (0,0,0), ((surfw - 1, 0), (1, height)))
for y in range(0, FREQ_MAX, 100):
rely = height - round(y * (height / FREQ_MAX))
pygame.draw.rect(surf, (32, 32, 32), ((surfw - 1, rely), (1, 1)), 1)
for y in range(0, FREQ_MAX, 1000):
rely = height - round(y * (height / FREQ_MAX))
pygame.draw.rect(surf, (64, 64, 64), ((surfw - 1, rely), (1, 1)), 1)
for i, freq in enumerate(new_freqs):
y = height - round((freq.current / FREQ_MAX) * height)
pygame.draw.circle(surf, colors[i], (surfw - 1, y), 1)
avg[i][frame % avg_len] = freq.current
avg2[i][frame % avg2_len] = freq.current
screen.blit(surf, (60,0))
surf.scroll(dx=-1)
avgs = []
for i, avg_freqs in enumerate(avg):
f = sum(avg_freqs)/avg_len
avgs.append(f)
text = font.render(f"{f / 1000:.3f}", True, (196, 196, 196))
text_rect = text.get_rect()
screen.blit(text, (surfw + 80, 40 + i * 40))
avgs_2 = []
for i, avg2_freqs in enumerate(avg2):
f = sum(avg2_freqs)/avg2_len
avgs_2.append(f)
text = font.render(f"{f / 1000:.3f}", True, (196, 196, 196))
text_rect = text.get_rect()
screen.blit(text, (surfw + 160, 40 + i * 40))
avg_avgs = sum(avgs) / cpu_count
text = font.render(f"{avg_avgs / 1000:.3f}", True, (255, 255, 255))
text_rect = text.get_rect()
screen.blit(text, (surfw + 80, 40 + (cpu_count + 2) * 40))
avg2_avgs = sum(avgs_2) / cpu_count
text = font.render(f"{avg2_avgs / 1000:.3f}", True, (255, 255, 255))
text_rect = text.get_rect()
screen.blit(text, (surfw + 160, 40 + (cpu_count + 2) * 40))
pygame.display.update()
clock.tick(20)
frame += 1
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment