Created
June 8, 2024 03:11
-
-
Save Dragoy/7414adca8cde8faaea908d4f67b5e63c to your computer and use it in GitHub Desktop.
This Python script monitors the GPU activity of NVIDIA graphics cards on a Windows system. It uses the nvidia-smi tool to query the GPU usage and displays this information in a real-time progress bar using the tqdm library. The script also features color-coded progress bars, thanks to the colorama library, to indicate different levels of GPU usa…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import time | |
from tqdm import tqdm | |
from colorama import init, Fore, Back, Style | |
init(autoreset=True) | |
def put_computer_to_sleep(): | |
command = '%windir%\\System32\\rundll32.exe powrprof.dll SetSuspendState 0,1,0' | |
subprocess.run(command, shell=True) | |
def color_bar(activity, bar): | |
if activity < 30: | |
bar.bar_format = "{l_bar}%s{bar}%s| {n_fmt}%%" % (Fore.GREEN, Fore.RESET) | |
elif activity < 70: | |
bar.bar_format = "{l_bar}%s{bar}%s| {n_fmt}%%" % (Fore.YELLOW, Fore.RESET) | |
else: | |
bar.bar_format = "{l_bar}%s{bar}%s| {n_fmt}%%" % (Fore.RED, Fore.RESET) | |
def get_gpu_activity(): | |
last_activity_time = time.time() | |
low_activity_start_time = None | |
gpu_bars = [] | |
first_run = True | |
while True: | |
# Получение названий видеокарт | |
if first_run: | |
name_result = subprocess.run(['nvidia-smi', '--query-gpu=name', '--format=csv,noheader'], capture_output=True, text=True) | |
gpu_names = name_result.stdout.strip().split('\n') | |
# Получение активности видеокарт | |
result = subprocess.run(['nvidia-smi', '--query-gpu=utilization.gpu', '--format=csv,noheader,nounits'], capture_output=True, text=True) | |
gpu_activities = result.stdout.strip().split('\n') | |
gpu_activities = [int(activity) for activity in gpu_activities] | |
if first_run: | |
for i, (name, activity) in enumerate(zip(gpu_names, gpu_activities)): | |
bar = tqdm(total=100, position=i, leave=False, bar_format="{l_bar}{bar}| {n_fmt}%") | |
bar.set_description(f"GPU {i} ({name})") | |
bar.n = activity | |
color_bar(activity, bar) | |
gpu_bars.append(bar) | |
first_run = False | |
else: | |
for i, (name, activity) in enumerate(zip(gpu_names, gpu_activities)): | |
gpu_bars[i].n = activity | |
color_bar(activity, gpu_bars[i]) | |
gpu_bars[i].refresh() | |
all_gpus_below_threshold = all(activity < 10 for activity in gpu_activities) | |
if all_gpus_below_threshold: | |
if low_activity_start_time is None: | |
low_activity_start_time = time.time() | |
elapsed_time = time.time() - low_activity_start_time | |
tqdm.write(f"All GPU activities below 10% for {int(elapsed_time)} seconds...", end='\r') | |
if elapsed_time >= 60: | |
for bar in gpu_bars: | |
bar.close() | |
tqdm.write(Fore.RED + "All GPU activities below 10% for an entire minute, putting computer into sleep mode...") | |
put_computer_to_sleep() | |
break | |
else: | |
low_activity_start_time = None | |
last_activity_time = time.time() | |
time.sleep(1) # Check every second | |
get_gpu_activity() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment