-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_fancontrol.py
More file actions
executable file
·1115 lines (958 loc) · 43.8 KB
/
gpu_fancontrol.py
File metadata and controls
executable file
·1115 lines (958 loc) · 43.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""GPU Fan Control - Dual GPU fan controller with tkinter GUI.
Uses pynvml via a root helper subprocess for fan control (no Coolbits needed).
Starts minimized to taskbar on boot. Close button minimizes, Quit button exits.
"""
import subprocess
import signal
import sys
import os
import json
import threading
import time
import math
import io
from pathlib import Path
import tkinter as tk
from tkinter import messagebox
import ctypes
import pynvml
import psutil
from PIL import Image, ImageDraw
# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------
SCRIPT_DIR = Path(__file__).parent.resolve()
PYTHON = Path(sys.executable).resolve()
HELPER = SCRIPT_DIR / "fan_helper.py"
CONFIG_PATH = Path.home() / ".config" / "gpu-fancontrol" / "config.json"
DEFAULT_CURVES = {
"RTX 3090": [(0, 30), (50, 35), (60, 50), (65, 60), (70, 75), (75, 85), (80, 100)],
"RTX 5070": [(0, 30), (50, 30), (60, 40), (65, 50), (70, 65), (75, 80), (80, 100)],
"default": [(0, 30), (50, 35), (60, 50), (70, 75), (80, 100)],
}
PROFILES = {
"Silent": [(0, 30), (50, 30), (60, 35), (65, 42), (70, 55), (75, 72), (80, 100)],
"Balanced": [(0, 30), (50, 35), (60, 50), (65, 60), (70, 75), (75, 85), (80, 100)],
"Aggressive": [(0, 40), (50, 50), (55, 65), (60, 75), (65, 85), (70, 95), (75, 100)],
"Max": [(0, 60), (40, 70), (50, 80), (55, 90), (60, 100), (70, 100), (80, 100)],
}
POLL_INTERVAL_MS = 3000
# Dark theme colors (Catppuccin Mocha)
BG = "#1e1e2e"
BG_PANEL = "#2a2a3d"
BG_INPUT = "#363650"
FG = "#cdd6f4"
FG_DIM = "#6c7086"
ACCENT = "#89b4fa"
GREEN = "#a6e3a1"
YELLOW = "#f9e2af"
RED = "#f38ba8"
ORANGE = "#fab387"
BORDER = "#45475a"
# ---------------------------------------------------------------------------
# GPU detection via pynvml
# ---------------------------------------------------------------------------
def get_fan_min_max(handle) -> tuple[int, int]:
"""Return (min_speed, max_speed) percent for the GPU. Falls back to (0, 100)."""
try:
mn, mx = ctypes.c_uint(), ctypes.c_uint()
pynvml.nvmlDeviceGetMinMaxFanSpeed(handle, ctypes.byref(mn), ctypes.byref(mx))
return mn.value, mx.value
except Exception:
return 0, 100
def detect_gpus() -> list[dict]:
pynvml.nvmlInit()
gpus = []
for i in range(pynvml.nvmlDeviceGetCount()):
h = pynvml.nvmlDeviceGetHandleByIndex(i)
name = pynvml.nvmlDeviceGetName(h)
temp = pynvml.nvmlDeviceGetTemperature(h, pynvml.NVML_TEMPERATURE_GPU)
try:
num_fans = pynvml.nvmlDeviceGetNumFans(h)
fan_speed = pynvml.nvmlDeviceGetFanSpeed_v2(h, 0) if num_fans > 0 else 0
except Exception:
num_fans = 0
fan_speed = 0
try:
mem = pynvml.nvmlDeviceGetMemoryInfo(h)
mem_used = mem.used / (1024 ** 3)
mem_total = mem.total / (1024 ** 3)
except Exception:
mem_used = 0.0
mem_total = 0.0
try:
power_usage = pynvml.nvmlDeviceGetPowerUsage(h) / 1000.0
except Exception:
power_usage = 0.0
try:
power_limit = pynvml.nvmlDeviceGetEnforcedPowerLimit(h) / 1000.0
except Exception:
power_limit = 0.0
fan_min, fan_max = get_fan_min_max(h)
gpus.append({
"index": i, "name": name, "temp": temp,
"fan_speed": fan_speed, "num_fans": num_fans,
"mem_used": mem_used, "mem_total": mem_total,
"power_usage": power_usage, "power_limit": power_limit,
"fan_min": fan_min, "fan_max": fan_max,
})
pynvml.nvmlShutdown()
return gpus
def poll_gpu_stats(gpus: list[dict]):
pynvml.nvmlInit()
for gpu in gpus:
h = pynvml.nvmlDeviceGetHandleByIndex(gpu["index"])
gpu["temp"] = pynvml.nvmlDeviceGetTemperature(h, pynvml.NVML_TEMPERATURE_GPU)
try:
if gpu["num_fans"] > 0:
gpu["fan_speed"] = pynvml.nvmlDeviceGetFanSpeed_v2(h, 0)
except Exception:
pass
try:
mem = pynvml.nvmlDeviceGetMemoryInfo(h)
gpu["mem_used"] = mem.used / (1024 ** 3)
gpu["mem_total"] = mem.total / (1024 ** 3)
except Exception:
pass
try:
gpu["power_usage"] = pynvml.nvmlDeviceGetPowerUsage(h) / 1000.0
except Exception:
pass
pynvml.nvmlShutdown()
# ---------------------------------------------------------------------------
# Root helper communication
# ---------------------------------------------------------------------------
class FanHelper:
def __init__(self):
self._proc = None
self._lock = threading.Lock()
def start(self) -> tuple[bool, str]:
with self._lock:
if self._proc and self._proc.poll() is None:
return True, ""
try:
self._proc = subprocess.Popen(
["sudo", "-n", str(PYTHON), str(HELPER)],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True,
)
return True, ""
except Exception as e:
return False, str(e)
def stop(self):
with self._lock:
if self._proc and self._proc.poll() is None:
self._send_raw({"cmd": "quit"})
try:
self._proc.wait(timeout=3)
except Exception:
self._proc.kill()
self._proc = None
def set_fan(self, gpu: int, fan: int, speed: int) -> bool:
return self._send({"cmd": "set", "gpu": gpu, "fan": fan, "speed": speed})
def reset_all(self) -> bool:
return self._send({"cmd": "reset_all"})
def _send(self, cmd: dict) -> bool:
with self._lock:
return self._send_raw(cmd)
def _send_raw(self, cmd: dict) -> bool:
if not self._proc or self._proc.poll() is not None:
return False
try:
self._proc.stdin.write(json.dumps(cmd) + "\n")
self._proc.stdin.flush()
line = self._proc.stdout.readline().strip()
if line:
return json.loads(line).get("ok", False)
return False
except Exception:
return False
# ---------------------------------------------------------------------------
# Fan curve interpolation
# ---------------------------------------------------------------------------
def interpolate_curve(curve: list[tuple[int, int]], temp: int) -> int:
if temp <= curve[0][0]:
return curve[0][1]
if temp >= curve[-1][0]:
return curve[-1][1]
for i in range(len(curve) - 1):
t0, s0 = curve[i]
t1, s1 = curve[i + 1]
if t0 <= temp <= t1:
if t1 == t0:
return s1
return int(s0 + (temp - t0) / (t1 - t0) * (s1 - s0))
return curve[-1][1]
# ---------------------------------------------------------------------------
# Config persistence
# ---------------------------------------------------------------------------
def load_config() -> dict:
if CONFIG_PATH.exists():
try:
return json.loads(CONFIG_PATH.read_text())
except Exception:
pass
return {}
def save_config(cfg: dict):
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
CONFIG_PATH.write_text(json.dumps(cfg, indent=2))
# ---------------------------------------------------------------------------
# Icon
# ---------------------------------------------------------------------------
def create_icon_image() -> Image.Image:
size = 64
img = Image.new("RGBA", (size, size), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
color = (137, 180, 250)
cx, cy = size // 2, size // 2
r = size // 2 - 4
draw.ellipse([cx - r, cy - r, cx + r, cy + r], outline=color, width=3)
hr = 8
draw.ellipse([cx - hr, cy - hr, cx + hr, cy + hr], fill=color)
for angle_deg in [0, 90, 180, 270]:
a = math.radians(angle_deg + 15)
x1 = cx + int(hr * math.cos(a))
y1 = cy + int(hr * math.sin(a))
x2 = cx + int((r - 2) * math.cos(a))
y2 = cy + int((r - 2) * math.sin(a))
draw.line([x1, y1, x2, y2], fill=color, width=4)
return img
# ---------------------------------------------------------------------------
# Temperature color
# ---------------------------------------------------------------------------
def temp_color(temp: int) -> str:
if temp >= 80: return RED
if temp >= 70: return ORANGE
if temp >= 60: return YELLOW
if temp >= 50: return ACCENT
return GREEN
# ---------------------------------------------------------------------------
# System stats (CPU, RAM, network)
# ---------------------------------------------------------------------------
def get_cpu_temp() -> float:
try:
temps = psutil.sensors_temperatures()
for sensor in ("k10temp", "coretemp", "cpu_thermal", "acpitz"):
if sensor not in temps:
continue
entries = temps[sensor]
# Prefer specific labels, fall back to first entry
for preferred in ("Tctl", "Tccd1", "Package id 0", ""):
for e in entries:
if preferred in e.label:
return e.current
except Exception:
pass
return 0.0
def format_speed(bps: float) -> str:
if bps >= 1024 ** 2:
return f"{bps / 1024 ** 2:.1f} MB/s"
if bps >= 1024:
return f"{bps / 1024:.0f} KB/s"
return f"{bps:.0f} B/s"
def poll_sys_stats(stats: dict, last_net, last_time: float):
"""Update stats in-place. Returns (new_net_counters, new_time)."""
stats["cpu_temp"] = get_cpu_temp()
try:
freq = psutil.cpu_freq()
stats["cpu_freq"] = freq.current / 1000.0
stats["cpu_freq_max"] = freq.max / 1000.0
except Exception:
pass
mem = psutil.virtual_memory()
stats["ram_used"] = mem.used / (1024 ** 3)
stats["ram_total"] = mem.total / (1024 ** 3)
stats["ram_percent"] = mem.percent
try:
net = psutil.net_io_counters()
now = time.time()
dt = now - last_time
if dt > 0 and last_net is not None:
stats["net_down"] = max(0.0, (net.bytes_recv - last_net.bytes_recv) / dt)
stats["net_up"] = max(0.0, (net.bytes_sent - last_net.bytes_sent) / dt)
return net, now
except Exception:
return last_net, last_time
# ---------------------------------------------------------------------------
# Main Application
# ---------------------------------------------------------------------------
class GPUFanControlApp:
def __init__(self, start_minimized=False):
self.gpus = detect_gpus()
self.fan_control_enabled = False
self.running = True
self.helper = FanHelper()
self.start_minimized = start_minimized
# System stats
self._sys_stats = {
"cpu_temp": 0.0,
"cpu_freq": 0.0, "cpu_freq_max": 0.0,
"ram_used": 0.0, "ram_total": 0.0, "ram_percent": 0.0,
"net_down": 0.0, "net_up": 0.0,
}
try:
self._last_net = psutil.net_io_counters()
except Exception:
self._last_net = None
self._last_net_time = time.time()
# Drag state for curve canvas interaction
self._drag_states = {} # gpu_idx -> point index being dragged, or None
self._dragging = set() # gpu_idxes currently being dragged (suppresses trace callbacks)
# Per-GPU state
self.gpu_states = {}
cfg = load_config()
for gpu in self.gpus:
idx = gpu["index"]
name = gpu["name"]
saved = cfg.get(f"gpu_{idx}", {})
default_curve = DEFAULT_CURVES["default"]
for key in DEFAULT_CURVES:
if key in name:
default_curve = DEFAULT_CURVES[key]
break
self.gpu_states[idx] = {
"mode": saved.get("mode", "auto"),
"manual_speed": saved.get("manual_speed", 50),
"curve": [tuple(p) for p in saved.get("curve", default_curve)],
"profile": saved.get("profile", None),
"current_speed": 0,
}
def run(self):
self.root = tk.Tk()
self.root.title("GPU Fan Control")
self.root.configure(bg=BG)
self.root.resizable(False, False)
self.root.protocol("WM_DELETE_WINDOW", self._on_close)
# Window icon
try:
img = create_icon_image()
buf = io.BytesIO()
img.save(buf, format="PNG")
self._icon = tk.PhotoImage(data=buf.getvalue())
self.root.iconphoto(True, self._icon)
except Exception:
pass
self._build_ui()
# Center window
self.root.update_idletasks()
w = self.root.winfo_width()
h = self.root.winfo_height()
x = (self.root.winfo_screenwidth() - w) // 2
y = (self.root.winfo_screenheight() - h) // 2
self.root.geometry(f"+{x}+{y}")
if self.start_minimized:
self.root.iconify()
# Start polling
self._poll()
self.root.mainloop()
def _build_ui(self):
self.gui_widgets = {}
main = tk.Frame(self.root, bg=BG, padx=16, pady=16)
main.pack(fill="both", expand=True)
# Header
header = tk.Frame(main, bg=BG)
header.pack(fill="x", pady=(0, 12))
tk.Label(header, text="GPU Fan Control", font=("Sans", 18, "bold"),
fg=ACCENT, bg=BG).pack(side="left")
# Quit button (actually exits)
tk.Button(
header, text="Quit", font=("Sans", 9),
bg=BG_INPUT, fg=FG_DIM, activebackground=RED, activeforeground=BG,
relief="flat", padx=10, pady=2, cursor="hand2",
command=self._on_quit,
).pack(side="right")
self.status_label = tk.Label(header, text="DISABLED", font=("Sans", 11, "bold"),
fg=FG_DIM, bg=BG)
self.status_label.pack(side="right", padx=(0, 8))
self.toggle_btn = tk.Button(
header, text="Enable", font=("Sans", 10, "bold"),
bg=ACCENT, fg=BG, activebackground=GREEN, activeforeground=BG,
relief="flat", padx=16, pady=4, cursor="hand2",
command=self._toggle_control,
)
self.toggle_btn.pack(side="right", padx=(0, 8))
# One unified grid for all panels (sys row=0, GPU row=1)
all_panels = tk.Frame(main, bg=BG)
all_panels.pack(fill="both", expand=True)
all_panels.columnconfigure(0, weight=1)
all_panels.columnconfigure(1, weight=1)
self._build_sys_panel(all_panels)
for i, gpu in enumerate(self.gpus):
self._build_gpu_panel(all_panels, gpu, i)
def _build_sys_panel(self, parent):
s = self._sys_stats
sw = {}
self._sys_widgets = sw
# ── Left panel: CPU + Memory (row=0, col=0) ──────────────────────────
lp = tk.Frame(parent, bg=BG_PANEL, relief="flat", bd=0,
highlightbackground=BORDER, highlightthickness=1)
lp.grid(row=0, column=0, sticky="nsew", pady=(0, 10), ipadx=16, ipady=12)
tk.Label(lp, text="CPU & Memory", font=("Sans", 14, "bold"),
fg=FG, bg=BG_PANEL).pack(anchor="w", padx=12, pady=(8, 2))
tk.Label(lp, text="System", font=("Sans", 9), fg=FG_DIM,
bg=BG_PANEL).pack(anchor="w", padx=12)
# CPU temp row (mirrors the temp + fan layout)
temp_frame = tk.Frame(lp, bg=BG_PANEL)
temp_frame.pack(fill="x", padx=12, pady=(12, 4))
sw["cpu_label"] = tk.Label(temp_frame,
text=f"{s['cpu_temp']:.0f}\u00b0C",
font=("Sans", 36, "bold"),
fg=temp_color(s["cpu_temp"]), bg=BG_PANEL)
sw["cpu_label"].pack(side="left")
ram_info = tk.Frame(temp_frame, bg=BG_PANEL)
ram_info.pack(side="right", anchor="e")
tk.Label(ram_info, text="RAM", font=("Sans", 9), fg=FG_DIM,
bg=BG_PANEL).pack()
sw["ram_label"] = tk.Label(ram_info,
text=f"{s['ram_used']:.1f} / {s['ram_total']:.0f} GB",
font=("Sans", 13, "bold"), fg=FG, bg=BG_PANEL)
sw["ram_label"].pack()
# RAM bar (mirrors fan bar)
bar_bg = tk.Frame(lp, bg=BG_INPUT, height=8)
bar_bg.pack(fill="x", padx=12, pady=(0, 8))
bar_bg.pack_propagate(False)
sw["ram_bar"] = tk.Frame(bar_bg, bg=GREEN, height=8)
sw["ram_bar"].place(relx=0, rely=0, relheight=1.0,
relwidth=max(0.01, s["ram_percent"] / 100))
# CPU frequency row
freq_frame = tk.Frame(lp, bg=BG_PANEL)
freq_frame.pack(fill="x", padx=12, pady=(4, 4))
tk.Label(freq_frame, text="FREQ", font=("Sans", 9), fg=FG_DIM,
bg=BG_PANEL).pack(side="left")
sw["freq_label"] = tk.Label(freq_frame,
text=f"{s['cpu_freq']:.2f} / {s['cpu_freq_max']:.2f} GHz",
font=("Sans", 11, "bold"), fg=FG, bg=BG_PANEL)
sw["freq_label"].pack(side="right")
freq_bar_bg = tk.Frame(lp, bg=BG_INPUT, height=8)
freq_bar_bg.pack(fill="x", padx=12, pady=(0, 12))
freq_bar_bg.pack_propagate(False)
freq_ratio = (s["cpu_freq"] / s["cpu_freq_max"]) if s["cpu_freq_max"] > 0 else 0.01
sw["freq_bar"] = tk.Frame(freq_bar_bg, bg=ACCENT, height=8)
sw["freq_bar"].place(relx=0, rely=0, relheight=1.0,
relwidth=max(0.01, freq_ratio))
# ── Right panel: Network (row=0, col=1) ──────────────────────────────
rp = tk.Frame(parent, bg=BG_PANEL, relief="flat", bd=0,
highlightbackground=BORDER, highlightthickness=1)
rp.grid(row=0, column=1, sticky="nsew", padx=(8, 0), pady=(0, 10), ipadx=16, ipady=12)
tk.Label(rp, text="Network", font=("Sans", 14, "bold"),
fg=FG, bg=BG_PANEL).pack(anchor="w", padx=12, pady=(8, 2))
tk.Label(rp, text="Live traffic", font=("Sans", 9), fg=FG_DIM,
bg=BG_PANEL).pack(anchor="w", padx=12)
# Centered content area
net_center = tk.Frame(rp, bg=BG_PANEL)
net_center.pack(expand=True, fill="both", padx=12, pady=(12, 8))
# Download
dl_frame = tk.Frame(net_center, bg=BG_PANEL)
dl_frame.pack(expand=True, fill="both")
tk.Label(dl_frame, text="\u2193 DOWNLOAD", font=("Sans", 9), fg=FG_DIM,
bg=BG_PANEL).pack(anchor="center")
sw["dl_label"] = tk.Label(dl_frame,
text=format_speed(s["net_down"]),
font=("Sans", 26, "bold"), fg=GREEN, bg=BG_PANEL)
sw["dl_label"].pack(anchor="center")
tk.Frame(net_center, bg=BORDER, height=1).pack(fill="x", pady=6)
# Upload
ul_frame = tk.Frame(net_center, bg=BG_PANEL)
ul_frame.pack(expand=True, fill="both")
tk.Label(ul_frame, text="\u2191 UPLOAD", font=("Sans", 9), fg=FG_DIM,
bg=BG_PANEL).pack(anchor="center")
sw["ul_label"] = tk.Label(ul_frame,
text=format_speed(s["net_up"]),
font=("Sans", 26, "bold"), fg=ACCENT, bg=BG_PANEL)
sw["ul_label"].pack(anchor="center")
def _update_sys_panel(self):
s = self._sys_stats
sw = self._sys_widgets
sw["cpu_label"].config(text=f"{s['cpu_temp']:.0f}\u00b0C",
fg=temp_color(s["cpu_temp"]))
sw["ram_label"].config(text=f"{s['ram_used']:.1f} / {s['ram_total']:.0f} GB")
sw["ram_bar"].place_configure(relwidth=max(0.01, s["ram_percent"] / 100))
ram_pct = s["ram_percent"]
ram_color = RED if ram_pct >= 90 else ORANGE if ram_pct >= 75 else YELLOW if ram_pct >= 50 else GREEN
sw["ram_bar"].config(bg=ram_color)
sw["freq_label"].config(text=f"{s['cpu_freq']:.2f} / {s['cpu_freq_max']:.2f} GHz")
freq_ratio = (s["cpu_freq"] / s["cpu_freq_max"]) if s["cpu_freq_max"] > 0 else 0.01
sw["freq_bar"].place_configure(relwidth=max(0.01, freq_ratio))
sw["dl_label"].config(text=format_speed(s["net_down"]))
sw["ul_label"].config(text=format_speed(s["net_up"]))
def _build_gpu_panel(self, parent, gpu, col):
idx = gpu["index"]
state = self.gpu_states[idx]
panel = tk.Frame(parent, bg=BG_PANEL, relief="flat", bd=0,
highlightbackground=BORDER, highlightthickness=1)
panel.grid(row=1, column=col, padx=(0 if col == 0 else 8, 0),
sticky="nsew", ipadx=16, ipady=12)
w = {}
self.gui_widgets[idx] = w
short_name = gpu["name"].replace("NVIDIA GeForce ", "")
tk.Label(panel, text=short_name, font=("Sans", 14, "bold"),
fg=FG, bg=BG_PANEL).pack(anchor="w", padx=12, pady=(8, 2))
fan_text = f"{gpu['num_fans']} fan{'s' if gpu['num_fans'] != 1 else ''}"
tk.Label(panel, text=f"GPU {idx} \u2022 {fan_text}",
font=("Sans", 9), fg=FG_DIM, bg=BG_PANEL).pack(anchor="w", padx=12)
# Temperature + fan speed
temp_frame = tk.Frame(panel, bg=BG_PANEL)
temp_frame.pack(fill="x", padx=12, pady=(12, 4))
w["temp_label"] = tk.Label(temp_frame, text=f"{gpu['temp']}\u00b0C",
font=("Sans", 36, "bold"),
fg=temp_color(gpu["temp"]), bg=BG_PANEL)
w["temp_label"].pack(side="left")
fan_info = tk.Frame(temp_frame, bg=BG_PANEL)
fan_info.pack(side="right", anchor="e")
tk.Label(fan_info, text="FAN", font=("Sans", 9), fg=FG_DIM, bg=BG_PANEL).pack()
w["fan_label"] = tk.Label(fan_info, text=f"{gpu['fan_speed']}%",
font=("Sans", 20, "bold"), fg=FG, bg=BG_PANEL)
w["fan_label"].pack()
# Fan speed bar
bar_frame = tk.Frame(panel, bg=BG_INPUT, height=8)
bar_frame.pack(fill="x", padx=12, pady=(0, 8))
bar_frame.pack_propagate(False)
w["fan_bar"] = tk.Frame(bar_frame, bg=ACCENT, height=8)
w["fan_bar"].place(relx=0, rely=0, relheight=1.0,
relwidth=max(0.01, gpu["fan_speed"] / 100))
# Memory + Power stats row
stats_frame = tk.Frame(panel, bg=BG_PANEL)
stats_frame.pack(fill="x", padx=12, pady=(0, 8))
mem_col = tk.Frame(stats_frame, bg=BG_PANEL)
mem_col.pack(side="left", expand=True, anchor="w")
tk.Label(mem_col, text="VRAM", font=("Sans", 9), fg=FG_DIM, bg=BG_PANEL).pack(anchor="w")
mem_used = gpu.get("mem_used", 0.0)
mem_total = gpu.get("mem_total", 0.0)
w["mem_label"] = tk.Label(mem_col,
text=f"{mem_used:.1f} / {mem_total:.0f} GB",
font=("Sans", 11, "bold"), fg=FG, bg=BG_PANEL)
w["mem_label"].pack(anchor="w")
pwr_col = tk.Frame(stats_frame, bg=BG_PANEL)
pwr_col.pack(side="right", anchor="e")
tk.Label(pwr_col, text="POWER", font=("Sans", 9), fg=FG_DIM, bg=BG_PANEL).pack(anchor="e")
power_usage = gpu.get("power_usage", 0.0)
power_limit = gpu.get("power_limit", 0.0)
w["pwr_label"] = tk.Label(pwr_col,
text=f"{power_usage:.0f} / {power_limit:.0f} W",
font=("Sans", 11, "bold"), fg=FG, bg=BG_PANEL)
w["pwr_label"].pack(anchor="e")
tk.Frame(panel, bg=BORDER, height=1).pack(fill="x", padx=12, pady=4)
# Mode selector
mode_frame = tk.Frame(panel, bg=BG_PANEL)
mode_frame.pack(fill="x", padx=12, pady=(4, 8))
tk.Label(mode_frame, text="Mode", font=("Sans", 10), fg=FG_DIM,
bg=BG_PANEL).pack(side="left")
w["mode_var"] = tk.StringVar(value=state["mode"])
for label, val in [("Auto", "auto"), ("Manual", "manual")]:
tk.Radiobutton(mode_frame, text=label, variable=w["mode_var"],
value=val, bg=BG_PANEL, fg=FG, selectcolor=BG_INPUT,
activebackground=BG_PANEL, activeforeground=FG,
font=("Sans", 10),
command=lambda i=idx: self._on_mode_change(i)
).pack(side="left", padx=(12 if val == "auto" else 4, 4))
# Manual slider
w["manual_frame"] = mf = tk.Frame(panel, bg=BG_PANEL)
tk.Label(mf, text="Fan Speed", font=("Sans", 10), fg=FG_DIM,
bg=BG_PANEL).pack(anchor="w", padx=12)
sf = tk.Frame(mf, bg=BG_PANEL)
sf.pack(fill="x", padx=12, pady=4)
w["manual_var"] = tk.IntVar(value=state["manual_speed"])
w["manual_label"] = tk.Label(sf, text=f"{state['manual_speed']}%",
font=("Sans", 12, "bold"), fg=ACCENT,
bg=BG_PANEL, width=5)
w["manual_label"].pack(side="right")
tk.Scale(sf, from_=gpu.get("fan_min", 0), to=gpu.get("fan_max", 100), orient="horizontal",
variable=w["manual_var"], showvalue=False,
bg=BG_PANEL, fg=FG, troughcolor=BG_INPUT, highlightthickness=0,
activebackground=ACCENT, sliderrelief="flat", length=200,
command=lambda v, i=idx: self._on_manual_change(i, int(v)),
).pack(side="left", fill="x", expand=True)
# Auto curve editor
w["curve_frame"] = cf = tk.Frame(panel, bg=BG_PANEL)
curve_header = tk.Frame(cf, bg=BG_PANEL)
curve_header.pack(fill="x", padx=12)
tk.Label(curve_header, text="Fan Curve", font=("Sans", 10), fg=FG_DIM,
bg=BG_PANEL).pack(side="left")
tk.Button(
curve_header, text="Reset", font=("Sans", 8),
bg=BG_INPUT, fg=FG_DIM, activebackground=ORANGE, activeforeground=BG,
relief="flat", padx=6, pady=1, cursor="hand2",
command=lambda i=idx: self._reset_curve(i),
).pack(side="right")
# Profile preset buttons
prof_frame = tk.Frame(cf, bg=BG_PANEL)
prof_frame.pack(fill="x", padx=12, pady=(4, 2))
w["profile_btns"] = {}
for pname in PROFILES:
btn = tk.Button(
prof_frame, text=pname, font=("Sans", 8),
bg=BG_INPUT, fg=FG_DIM,
activebackground=ACCENT, activeforeground=BG,
relief="flat", padx=6, pady=2, cursor="hand2",
command=lambda n=pname, i=idx: self._apply_profile(i, n),
)
btn.pack(side="left", padx=(0, 4))
w["profile_btns"][pname] = btn
# Highlight saved profile if any
if state.get("profile") in PROFILES:
w["profile_btns"][state["profile"]].config(bg=ACCENT, fg=BG)
w["curve_canvas"] = tk.Canvas(cf, width=260, height=120,
bg=BG_INPUT, highlightthickness=0,
cursor="crosshair")
w["curve_canvas"].pack(padx=12, pady=(2, 4))
w["curve_canvas"].bind("<ButtonPress-1>",
lambda e, i=idx: self._curve_mouse_down(e, i))
w["curve_canvas"].bind("<B1-Motion>",
lambda e, i=idx: self._curve_mouse_drag(e, i))
w["curve_canvas"].bind("<ButtonRelease-1>",
lambda e, i=idx: self._curve_mouse_up(e, i))
pf = tk.Frame(cf, bg=BG_PANEL)
pf.pack(fill="x", padx=12, pady=(0, 8))
w["curve_entries_frame"] = pf
tk.Label(pf, text="Temp\u00b0C:", font=("Sans", 8), fg=FG_DIM,
bg=BG_PANEL).grid(row=0, column=0, sticky="w")
tk.Label(pf, text="Fan %:", font=("Sans", 8), fg=FG_DIM,
bg=BG_PANEL).grid(row=1, column=0, sticky="w")
w["curve_entries"] = []
for j, (t, s) in enumerate(state["curve"]):
tv = tk.StringVar(value=str(t))
sv = tk.StringVar(value=str(s))
tk.Entry(pf, textvariable=tv, width=4, font=("Sans", 8),
bg=BG_INPUT, fg=FG, insertbackground=FG, relief="flat",
justify="center").grid(row=0, column=j + 1, padx=1)
tk.Entry(pf, textvariable=sv, width=4, font=("Sans", 8),
bg=BG_INPUT, fg=FG, insertbackground=FG, relief="flat",
justify="center").grid(row=1, column=j + 1, padx=1)
w["curve_entries"].append((tv, sv))
tv.trace_add("write", lambda *a, i=idx: self._on_curve_change(i))
sv.trace_add("write", lambda *a, i=idx: self._on_curve_change(i))
self._show_mode_frame(idx)
self._draw_curve(idx)
# -----------------------------------------------------------------------
# Window management
# -----------------------------------------------------------------------
def _on_close(self):
"""Close button minimizes to taskbar."""
self.root.iconify()
def _on_quit(self):
"""Actually quit the app."""
self.running = False
if self.fan_control_enabled:
self.helper.reset_all()
self.helper.stop()
self._save_config()
self.root.destroy()
# -----------------------------------------------------------------------
# Fan control
# -----------------------------------------------------------------------
def _toggle_control(self):
if self.fan_control_enabled:
self._disable_fan_control()
else:
self._enable_fan_control()
def _enable_fan_control(self):
ok, err = self.helper.start()
if not ok:
messagebox.showwarning(
"Fan Control Error",
"Could not start fan control helper.\n\n"
"Make sure passwordless sudo is set up:\n"
" sudo bash ~/gpu_control/setup_sudoers.sh\n\n"
f"Error: {err}"
)
return
self.fan_control_enabled = True
self._update_status()
def _disable_fan_control(self):
if self.fan_control_enabled:
self.helper.reset_all()
self.helper.stop()
self.fan_control_enabled = False
self._update_status()
def _update_status(self):
if self.fan_control_enabled:
self.status_label.config(text="ACTIVE", fg=GREEN)
self.toggle_btn.config(text="Disable", bg=RED)
else:
self.status_label.config(text="DISABLED", fg=FG_DIM)
self.toggle_btn.config(text="Enable", bg=ACCENT)
# -----------------------------------------------------------------------
# Polling (runs on tkinter's after loop - no threading issues)
# -----------------------------------------------------------------------
def _poll(self):
if not self.running:
return
try:
poll_gpu_stats(self.gpus)
self._last_net, self._last_net_time = poll_sys_stats(
self._sys_stats, self._last_net, self._last_net_time)
if self.fan_control_enabled:
for gpu in self.gpus:
idx = gpu["index"]
state = self.gpu_states.get(idx)
if not state:
continue
if state["mode"] == "auto":
target = interpolate_curve(state["curve"], gpu["temp"])
else:
target = state["manual_speed"]
target = max(gpu.get("fan_min", 0), min(gpu.get("fan_max", 100), target))
state["current_speed"] = target
for fan in range(gpu["num_fans"]):
self.helper.set_fan(idx, fan, target)
self._update_readings()
self._update_sys_panel()
except Exception:
pass
self.root.after(POLL_INTERVAL_MS, self._poll)
def _update_readings(self):
for gpu in self.gpus:
idx = gpu["index"]
w = self.gui_widgets.get(idx)
if not w:
continue
temp = gpu["temp"]
fan = gpu["fan_speed"]
w["temp_label"].config(text=f"{temp}\u00b0C", fg=temp_color(temp))
w["fan_label"].config(text=f"{fan}%")
w["fan_bar"].place_configure(relwidth=max(0.01, fan / 100))
if fan >= 80: bar_color = RED
elif fan >= 60: bar_color = ORANGE
elif fan >= 40: bar_color = YELLOW
else: bar_color = GREEN
w["fan_bar"].config(bg=bar_color)
mem_used = gpu.get("mem_used", 0.0)
mem_total = gpu.get("mem_total", 0.0)
w["mem_label"].config(text=f"{mem_used:.1f} / {mem_total:.0f} GB")
power_usage = gpu.get("power_usage", 0.0)
power_limit = gpu.get("power_limit", 0.0)
w["pwr_label"].config(text=f"{power_usage:.0f} / {power_limit:.0f} W")
self._draw_curve(idx)
# -----------------------------------------------------------------------
# Mode / curve controls
# -----------------------------------------------------------------------
def _show_mode_frame(self, gpu_idx):
w = self.gui_widgets[gpu_idx]
if self.gpu_states[gpu_idx]["mode"] == "manual":
w["curve_frame"].pack_forget()
w["manual_frame"].pack(fill="x")
else:
w["manual_frame"].pack_forget()
w["curve_frame"].pack(fill="x")
def _on_mode_change(self, gpu_idx):
self.gpu_states[gpu_idx]["mode"] = self.gui_widgets[gpu_idx]["mode_var"].get()
self._show_mode_frame(gpu_idx)
self._save_config()
def _on_manual_change(self, gpu_idx, value):
self.gpu_states[gpu_idx]["manual_speed"] = value
self.gui_widgets[gpu_idx]["manual_label"].config(text=f"{value}%")
self._save_config()
def _rebuild_curve_entries(self, gpu_idx):
"""Destroy and recreate the entry widgets to match the current curve."""
w = self.gui_widgets[gpu_idx]
pf = w["curve_entries_frame"]
for widget in list(pf.winfo_children()):
if int(widget.grid_info().get("column", 0)) > 0:
widget.destroy()
curve = self.gpu_states[gpu_idx]["curve"]
w["curve_entries"] = []
for j, (t, s) in enumerate(curve):
tv = tk.StringVar(value=str(t))
sv = tk.StringVar(value=str(s))
tk.Entry(pf, textvariable=tv, width=4, font=("Sans", 8),
bg=BG_INPUT, fg=FG, insertbackground=FG, relief="flat",
justify="center").grid(row=0, column=j + 1, padx=1)
tk.Entry(pf, textvariable=sv, width=4, font=("Sans", 8),
bg=BG_INPUT, fg=FG, insertbackground=FG, relief="flat",
justify="center").grid(row=1, column=j + 1, padx=1)
w["curve_entries"].append((tv, sv))
tv.trace_add("write", lambda *a, i=gpu_idx: self._on_curve_change(i))
sv.trace_add("write", lambda *a, i=gpu_idx: self._on_curve_change(i))
def _apply_profile(self, gpu_idx, profile_name):
curve = [tuple(p) for p in PROFILES[profile_name]]
self.gpu_states[gpu_idx]["curve"] = curve
self.gpu_states[gpu_idx]["profile"] = profile_name
self._dragging.add(gpu_idx)
self._rebuild_curve_entries(gpu_idx)
self._dragging.discard(gpu_idx)
# Update button highlights
w = self.gui_widgets[gpu_idx]
for name, btn in w["profile_btns"].items():
btn.config(bg=ACCENT if name == profile_name else BG_INPUT,
fg=BG if name == profile_name else FG_DIM)
self._draw_curve(gpu_idx)
self._save_config()
def _deselect_profiles(self, gpu_idx):
"""Clear profile highlight when user manually edits the curve."""
if self.gpu_states[gpu_idx].get("profile") is not None:
self.gpu_states[gpu_idx]["profile"] = None
w = self.gui_widgets.get(gpu_idx)
if w:
for btn in w["profile_btns"].values():
btn.config(bg=BG_INPUT, fg=FG_DIM)
def _reset_curve(self, gpu_idx):
gpu = next(g for g in self.gpus if g["index"] == gpu_idx)
default_curve = DEFAULT_CURVES["default"]
for key in DEFAULT_CURVES:
if key in gpu["name"]:
default_curve = DEFAULT_CURVES[key]
break
self.gpu_states[gpu_idx]["curve"] = [tuple(p) for p in default_curve]
w = self.gui_widgets[gpu_idx]
self._dragging.add(gpu_idx)
for j, (tv, sv) in enumerate(w["curve_entries"]):
if j < len(default_curve):
tv.set(str(default_curve[j][0]))
sv.set(str(default_curve[j][1]))
self._dragging.discard(gpu_idx)
self._draw_curve(gpu_idx)
self._save_config()
def _on_curve_change(self, gpu_idx):
if gpu_idx in self._dragging:
return
self._deselect_profiles(gpu_idx)
w = self.gui_widgets[gpu_idx]
fan_min = next((g["fan_min"] for g in self.gpus if g["index"] == gpu_idx), 0)
fan_max = next((g["fan_max"] for g in self.gpus if g["index"] == gpu_idx), 100)
curve = []
try:
for tv, sv in w["curve_entries"]:
t = int(tv.get())
s = max(fan_min, min(fan_max, int(sv.get())))
curve.append((t, s))
curve.sort(key=lambda p: p[0])
self.gpu_states[gpu_idx]["curve"] = curve
self._draw_curve(gpu_idx)
self._save_config()
except (ValueError, Exception):
pass
# -----------------------------------------------------------------------
# Curve canvas drag interaction
# -----------------------------------------------------------------------
_CURVE_CW, _CURVE_CH, _CURVE_PAD = 260, 120, 20
def _canvas_to_curve(self, x, y):
"""Convert canvas pixel coords to (temp, fan_speed) clamped 0-100."""
pw = self._CURVE_CW - 2 * self._CURVE_PAD
ph = self._CURVE_CH - 2 * self._CURVE_PAD
t = max(0, min(100, round((x - self._CURVE_PAD) / pw * 100)))
s = max(0, min(100, round((self._CURVE_CH - self._CURVE_PAD - y) / ph * 100)))
return t, s
def _curve_points_px(self, gpu_idx):
"""Return canvas pixel coords for all curve points."""
pw = self._CURVE_CW - 2 * self._CURVE_PAD
ph = self._CURVE_CH - 2 * self._CURVE_PAD
return [
(self._CURVE_PAD + (t / 100) * pw,
self._CURVE_CH - self._CURVE_PAD - (s / 100) * ph)
for t, s in self.gpu_states[gpu_idx]["curve"]
]
def _curve_mouse_down(self, event, gpu_idx):
"""Select nearest curve point within 15px for dragging."""
points = self._curve_points_px(gpu_idx)
best_i, best_d = None, 15
for i, (px, py) in enumerate(points):
d = ((event.x - px) ** 2 + (event.y - py) ** 2) ** 0.5
if d < best_d:
best_d = d
best_i = i
self._drag_states[gpu_idx] = best_i
def _curve_mouse_drag(self, event, gpu_idx):
"""Move the selected point, re-sort curve, redraw."""
point_idx = self._drag_states.get(gpu_idx)
if point_idx is None:
return
fan_min = next((g["fan_min"] for g in self.gpus if g["index"] == gpu_idx), 0)
fan_max = next((g["fan_max"] for g in self.gpus if g["index"] == gpu_idx), 100)
t, s = self._canvas_to_curve(event.x, event.y)
s = max(fan_min, min(fan_max, s))
self._dragging.add(gpu_idx)