code a real time updating python scripts for monitoring gpu cpu ram usage
- User
code a real time updating python scripts for monitoring gpu cpu ram usage
- Assistant
code a real time updating python scripts for monitoring gpu cpu ram usage
Sure! Below is a Python script that monitors your GPU, CPU, and RAM usage in real-time. The script updates every second and displays the usage statistics in the console.
You'll need to install the following Python libraries:
Install them using pip:
pip install psutil GPUtil
import psutil
import GPUtil
import time
import os
def get_cpu_usage():
"""Returns the current system-wide CPU utilization as a percentage."""
return psutil.cpu_percent(interval=None)
def get_ram_usage():
"""Returns the current system-wide RAM utilization as a percentage."""
return psutil.virtual_memory().percent
def get_gpu_usage():
"""Returns the current GPU utilization as a percentage."""
gpus = GPUtil.getGPUs()
if gpus:
gpu = gpus[0] # Assuming you have at least one GPU
return gpu.load * 100 # GPU load is a value between 0 and 1
else:
return None # No GPU found
def clear_console():
"""Clears the console output."""
os.system('cls' if os.name == 'nt' else 'clear')
try:
while True:
clear_console()
cpu_usage = get_cpu_usage()
ram_usage = get_ram_usage()
gpu_usage = get_gpu_usage()
print(f"CPU Usage: {cpu_usage:.1f}%")
print(f"RAM Usage: {ram_usage:.1f}%")
if gpu_usage is not None:
print(f"GPU Usage: {gpu_usage:.1f}%")
else:
print("GPU Usage: No GPU found")
time.sleep(1) # Update every second
except KeyboardInterrupt:
print("Monitoring stopped.")
Importing Libraries:
psutil is used to retrieve CPU and RAM usage.GPUtil is used to get GPU load information.time for controlling the update frequency.os to clear the console for a real-time updating effect.Functions:
get_cpu_usage(): Returns the current CPU usage percentage.get_ram_usage(): Returns the current RAM usage percentage.get_gpu_usage(): Returns the current GPU usage percentage if a GPU is available.clear_console(): Clears the console output to refresh the display.Main Loop:
Exception Handling:
KeyboardInterrupt (e.g., when you press Ctrl+C) to exit gracefully.Save the Script:
Save the script to a file, for example, monitor.py.
Run the Script:
python monitor.py
Make sure you're running it in an environment where you have installed the required libraries.
GPU Monitoring:
GPUtil, which works with NVIDIA GPUs. If you have an NVIDIA GPU, ensure that the NVIDIA drivers and NVIDIA Management Library (NVML) are properly installed.rocm-smi or access system tools directly.Permissions:
Customization:
time.sleep(1) value to adjust the update frequency.get_gpu_usage() function to iterate over all available GPUs.CPU Usage: 15.3%
RAM Usage: 45.2%
GPU Usage: 12.7%
The console output will refresh every second, giving you a real-time update on your system's resource usage.
Import Errors:
ImportError, ensure that you've installed the required libraries in the correct Python environment. Use pip list to check installed packages.No GPU Detected:
nvidia-smi is working.This script provides a simple way to monitor your system's GPU, CPU, and RAM usage in real-time using Python. It's easily customizable and can be expanded upon to include additional system metrics as needed.
If you have any questions or need further assistance, feel free to ask!
Not yet included in a published release.