Monitoring Graphics Processing Unit (GPU) with Processing 3

Hello,

Using the: java.lang.management.ManagementFactory and com.sun.management.OperatingSystemMXBean libraries, I was able to read the CPU and RAM usage of my pc. Now I would like to be able to read the Graphics Processing Unit (GPU) usage as well. I would like it to work with NVIDIA and Intel GPUs. Is this possible in Processing? Does anyone have an example on how to do this?

Thanks in advance,
Thomas

This looks like a promising library. Add it to your sketch and go from there.

1 Like

Thank you @micycle for the reply.

I have tried to make it work with the jSensors library, but I’m not experienced enough to know how to import the library in my sketch. The readme file it not very clear about this either.

For the CPU usage, everything worked rightaway, I could just do:

import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
OperatingSystemMXBean osBean;

void setup(){
    size(800, 600);
    osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
}

void draw(){
    println((float)osBean.getSystemCpuLoad());
}

Although I get the warning “No library found for com.sun.management”, everything seems to work just fine. Now, how would I achieve the same for GPU? How to import the library?

Thank you in advance.

Kind regards,
Thomas

It appears that there used to be a .jar download but the URL is no longer working. Otherwise, you could drag-and-drop the library’s .jar onto your sketch.

The library’s README suggests adding it as a maven dependency – this is wholly possible but you’ll need to migrate your project to an IDE (other than the PDE) (which I recommend anyway), and create a Maven project there.

Thanks for the reply @micycle, I appreciate any help.

I am using Atom in combination with the ‘processing’ package from ‘bleikamp’ to compile my project in Windows 10.

I can’t seem to find the .jar file and I’m not sure how to create a Maven dependency. Would you be so kind to help me with that? Do I have to copy this file to the same folder as my .pde files?

I’m making a project which is rendered using the OPENGL renderer of processing, making use of my GPU. I would like to be able to keep track of my hardware usage.

Thank you in advance,
Thomas

A link I found:

Istead of doing it in Java, you could also find a command line program that provides this information and execute that program and get its output from Processing. For example https://medium.com/@george.seif94/the-4-best-command-line-tools-for-monitoring-your-cpu-ram-and-gpu-usage-692e3053000f shows a program called nvidia-smi that gives a lot of data. I just tried intel_gpu_top mentioned at https://awesomedetect.com/how-to-monitor-intel-integrated-gpu-usage-in-linux/ .

As I said about the .jar, the URL on the github is no longer working so you can’t access it.

Atom (since it isn’t an IDE) doesn’t support Maven projects (at least natively). It might be possible to install an extension…

Otherwise, it’s easier to use an IDE (see Processing in Java).

The jar is there, it just changed the name:

1 Like

As @hamoid mentioned, the jar file is there. I downloaded ‘jSensors-2.1.1-jar-with-dependencies.jar’ and put it in my sketch’s ‘libraries’ folder, which I just created. I renamed to file to ‘jSensors.jar’, but when I try to ‘import jSensors.jar’ at the beginning of my sketch (called Testproject.pde), I get the following error:

"
No library found for jSensors

Libraries must be installed in a folder named ‘libraries’ inside the sketchbook folder (see the Preferences window). Testproject.pde:0:0:0:0: The package �jSensors� does not exist. You might be missing a library.
"

What am I doing wrong here?

Thanks in advance.
Thomas

Hi, inside your sketch folder, create a folder called code and place the jar file there. No need to rename it. The processing IDE automatically creates this folder when you drop a jar file into the IDE.

Then, based on one of the files of the project the imports seem to be

import com.profesorfalken.jsensors.JSensors;
import com.profesorfalken.jsensors.model.components.Components;

You can maybe explore the other files in that repo to figure out how it is used.

2 Likes

Thank you @hamoid , that works. I had to import the java.util.List librart as well. Now I can print the cpu model name, like this:

import com.profesorfalken.jsensors.model.components.Components;
import java.util.List;

Components components;


void setup(){
    components = JSensors.get.components();
    size(1000, 600, OPENGL);
}


void draw(){
    background(80);

    List<Cpu> cpus = components.cpus; // works
    for (final Cpu cpu : cpus) {
        println(cpu.name);
    }

    List<Gpu> gpus = components.gpus; // doesn't work
    for (final Gpu gpu : gpus) {
        println(gpu.name);
    }
}

But I cannot print the load of the cpu. How do I do that? Also, when I try to print the name of the gpu, it is not detected but doesn’t give an error either. Do you know what the problem is?

Thanks.

1 Like

I get the same result: zero gpus. I guess the library does not detect it.

This is the output of the command line tool nvidia-smi before and after loading shadertoy:

a@b ~> nvidia-smi 
Mon Dec  2 14:23:27 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.56       Driver Version: 418.56       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 1060    Off  | 00000000:01:00.0  On |                  N/A |
| N/A   53C    P0    27W /  N/A |    353MiB /  6077MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1246      G   /usr/lib/xorg/Xorg                            18MiB |
|    0      1280      G   /usr/bin/gnome-shell                          48MiB |
|    0      2189      G   /usr/lib/xorg/Xorg                           102MiB |
|    0      2333      G   /usr/bin/gnome-shell                         181MiB |
+-----------------------------------------------------------------------------+
a@b ~> nvidia-smi
Mon Dec  2 14:24:35 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.56       Driver Version: 418.56       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 1060    Off  | 00000000:01:00.0  On |                  N/A |
| N/A   57C    P0    46W /  N/A |    549MiB /  6077MiB |     25%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1246      G   /usr/lib/xorg/Xorg                            18MiB |
|    0      1280      G   /usr/bin/gnome-shell                          48MiB |
|    0      2189      G   /usr/lib/xorg/Xorg                           103MiB |
|    0      2333      G   /usr/bin/gnome-shell                         186MiB |
|    0     12806      G   /usr/lib/firefox/firefox                     189MiB |
+-----------------------------------------------------------------------------+

It shows power consumption, ram and GPU usage.

JFTR you never need to use Maven as a build system to use a JAR from Maven. Simply search for what you want at https://search.maven.org and choose the download JAR option (being careful to look for any additional dependencies as well). There is also a download JAR with dependencies option but I’d be careful with that.

1 Like