make a constantly updating gpu, cpu and memory tracker/monitor for me to run in jupyterlab
- User
make a constantly updating gpu, cpu and memory tracker/monitor for me to run in jupyterlab
- Assistant
make a constantly updating gpu, cpu and memory tracker/monitor for me to run in jupyterlab
Certainly! Below is a Python script that you can run in JupyterLab to create a constantly updating monitor for your GPU, CPU, and memory usage. This script uses the psutil library to monitor CPU and memory usage and the GPUtil library to monitor GPU usage. It updates the output every second to provide real-time monitoring.
Prerequisites:
Before running the script, make sure to install the required libraries:
pip install psutil GPUtil
JupyterLab Monitoring Script:
import psutil
import GPUtil
import time
from IPython.display import clear_output
try:
while True:
# Get CPU usage
cpu_percent = psutil.cpu_percent(interval=None)
# Get memory usage
memory = psutil.virtual_memory()
memory_total = memory.total / (1024 ** 3) # Convert bytes to GB
memory_used = memory.used / (1024 ** 3)
memory_percent = memory.percent
# Get GPU usage
gpus = GPUtil.getGPUs()
gpu_info = []
for gpu in gpus:
gpu_load = gpu.load * 100 # Convert fraction to percentage
gpu_memory_total = gpu.memoryTotal # In MB
gpu_memory_used = gpu.memoryUsed
gpu_memory_percent = gpu.memoryUtil * 100
gpu_info.append({
'id': gpu.id,
'name': gpu.name,
'load': gpu_load,
'memory_total': gpu_memory_total,
'memory_used': gpu_memory_used,
'memory_percent': gpu_memory_percent,
'temperature': gpu.temperature # In Celsius
})
# Clear the previous output
clear_output(wait=True)
# Display CPU and Memory usage
print(f"CPU Usage: {cpu_percent}%")
print(f"Memory Usage: {memory_used:.2f} GB / {memory_total:.2f} GB ({memory_percent}%)\n")
# Display GPU usage
for gpu in gpu_info:
print(f"GPU {gpu['id']} - {gpu['name']}:")
print(f" Load: {gpu['load']:.1f}%")
print(f" Memory Usage: {gpu['memory_used']:.1f} MB / {gpu['memory_total']:.1f} MB ({gpu['memory_percent']:.1f}%)")
print(f" Temperature: {gpu['temperature']} °C\n")
# Wait for 1 second before the next update
time.sleep(1)
except KeyboardInterrupt:
print("Monitoring stopped.")
Explanation:
Import Libraries:
psutil for CPU and memory monitoring.GPUtil for GPU monitoring.time for delaying updates.clear_output from IPython.display to update the output in place.Main Loop:
psutil.cpu_percent() to get the current CPU utilization percentage.GPUtil.getGPUs() to get a list of GPU objects and extracts relevant information such as load, memory usage, and temperature.Exit Mechanism:
try-except block allows you to stop the monitoring gracefully by interrupting the kernel (e.g., by pressing the stop button in JupyterLab or using Ctrl + C in a terminal interface).Running the Script:
Install Required Libraries:
Run the following command in a code cell or terminal in JupyterLab:
!pip install psutil GPUtil
Execute the Monitoring Script:
Copy the monitoring script into a new code cell and run it. It will start displaying the CPU, memory, and GPU usage, updating every second.
Stopping the Monitoring:
To stop the monitoring, interrupt the kernel by clicking the "Stop" button or pressing I twice in quick succession in Command Mode.
Sample Output:
CPU Usage: 15.0%
Memory Usage: 6.32 GB / 16.00 GB (39.5%)
GPU 0 - NVIDIA GeForce RTX 3080:
Load: 12.0%
Memory Usage: 1024.0 MB / 10024.0 MB (10.2%)
Temperature: 58 °C
Notes:
GPUtil, you may need to adjust the GPU monitoring section or install specific GPU monitoring tools for your hardware.time.sleep(1) value if you want to change the update interval.Customization:
Update Interval:
Change the sleep duration to adjust how frequently the statistics update:
time.sleep(2) # Updates every 2 seconds
Display Formatting:
Modify the print statements to change how the information is displayed, or add additional details as needed.
Error Handling:
For more robust monitoring, you can add error handling to manage exceptions that may occur if the system resources are temporarily unavailable.
Alternative Visualization (Optional):
For a more graphical representation, you can use the matplotlib library to plot the usage statistics over time. However, this requires additional setup to handle live updating plots in JupyterLab and is more complex.
Let me know if you need assistance with enhancing the script or adding graphical visualization!
Not yet included in a published release.