Skip to content

Commit c89e0e0

Browse files
committed
Switch to total NvidiaGPU usage
While nvml can do per-process data, it does not work with WDDM (the default mode for Windows and WSL). So pull back to just implement overall GPU data, and hopefully we can get more granular later.
1 parent 22bdef1 commit c89e0e0

11 files changed

Lines changed: 133 additions & 193 deletions

File tree

.github/workflows/npm-publish.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ jobs:
1414
include:
1515
- image: ubuntu-latest
1616
kernel: linux-x64
17-
# - image: windows-latest
18-
# kernel: win32-x64
19-
# - image: macos-13
20-
# kernel: darwin-x64
21-
# - image: macos-latest
22-
# kernel: darwin-arm64
17+
- image: windows-latest
18+
kernel: win32-x64
19+
- image: macos-13
20+
kernel: darwin-x64
21+
- image: macos-latest
22+
kernel: darwin-arm64
2323
name: Build ${{ matrix.kernel }}
2424
runs-on: ${{ matrix.image }}
2525
steps:

README.md

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- `memRSS(pid)`: Get current memory resident/working set size for a process in bytes.
2828
- `fileRead(pid)`: Get total file read size for a process in bytes.
2929
- `fileWrite(pid)`: Get total file write size for a process in bytes.
30+
- `NvidiaGPU.allGPUs()`: Get handles for all Nvidia GPUs (total, not process-specific data)
3031

3132
## Installation
3233

@@ -42,23 +43,5 @@ npm install node-ps-data
4243
- More library functions:
4344
- CPU/Memory subcategories (e.g. child process time, pages info, etc.)
4445
- Network usage
45-
- GPU memory and usage
46+
- Process-specific GPU memory and usage
4647
- More!
47-
48-
## License & Copyright
49-
50-
Copyright (c) 2023 Kai Orita
51-
52-
This program is free software: you can redistribute it and/or modify
53-
it under the terms of the GNU General Public License as published by
54-
the Free Software Foundation, either version 3 of the License, or
55-
(at your option) any later version.
56-
57-
This program is distributed in the hope that it will be useful,
58-
but WITHOUT ANY WARRANTY; without even the implied warranty of
59-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60-
GNU General Public License for more details.
61-
62-
You should have received a copy of the GNU General Public License
63-
along with this program. If not, see <https://www.gnu.org/licenses/>.
64-

binding.gyp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
["OS=='win'", {
44
"targets": [{
55
"target_name": "node_ps_data",
6-
"sources": ["napi.cc", "lib_win.cc", "lib_nvml.cc"],
6+
"sources": [
7+
"napi.cc",
8+
"lib_win.cc",
9+
"lib_nvidia_nvml.cc",
10+
],
711
"include_dirs": [
812
"./node_modules/node-addon-api",
913
"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.9/include"
@@ -27,7 +31,7 @@
2731
"OS=='linux'", {
2832
"targets": [{
2933
"target_name": "node_ps_data",
30-
"sources": ["napi.cc", "lib_linux.cc", "lib_nvml.cc"],
34+
"sources": ["napi.cc", "lib_linux.cc", "lib_nvidia_nvml.cc"],
3135
"include_dirs": ["./node_modules/node-addon-api"],
3236
"link_settings": {
3337
"libraries": [
@@ -44,7 +48,7 @@
4448
"OS=='mac'", {
4549
"targets": [{
4650
"target_name": "node_ps_data",
47-
"sources": ["napi.cc", "lib_mac.cc", "lib_nvml_stub.cc"],
51+
"sources": ["napi.cc", "lib_mac.cc", "lib_nvidia_stub.cc"],
4852
"include_dirs": ["./node_modules/node-addon-api"],
4953
"cflags!": [ "-fno-exceptions" ],
5054
"cflags_cc!": [ "-fno-exceptions" ],

index.d.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,28 @@ declare function fileWrite(pid: number): number;
7373
*/
7474
declare interface NvidiaGPUUtilization {
7575
/**
76-
* The utilization of the device's Steaming Multiprocessors (SMs)
76+
* The proportion of the time that ANY kernel is being run
77+
*/
78+
gpu: number;
79+
/**
80+
* The proportion of the *time* that memory is being read/written.
7781
*
78-
* This includes most 3D and compute operations.
82+
* This is NOT the amount of memory currently USED.
7983
*/
80-
smUtilization: number
84+
memoryRW: number;
85+
}
86+
87+
declare interface NvidiaGPUMemory {
88+
/** In bytes */
89+
free: number;
90+
/** In bytes */
91+
total: number;
92+
/** In bytes */
93+
used: number;
8194
}
8295

8396
/**
84-
* A handle for getting process data from a single Nvidia GPU (internally uses [NVML](https://docs.nvidia.com/deploy/nvml-api/nvml-api-reference.html))
97+
* A handle for getting TOTAL data from a single Nvidia GPU (internally uses [NVML](https://docs.nvidia.com/deploy/nvml-api/nvml-api-reference.html))
8598
*/
8699
declare class NvidiaGPU {
87100
/**
@@ -95,16 +108,25 @@ declare class NvidiaGPU {
95108

96109
/**
97110
* @param deviceIdx The index of the GPU to get a handle for.
98-
* @param pid The process id to search for.
99111
*
100112
* @throws An error if `deviceIdx` is invalid or if we do not have permission to access it.
101113
* @throws
102114
*/
103-
constructor(deviceIdx: number, pid: number);
115+
constructor(deviceIdx: number);
116+
117+
/**
118+
* Returns the device name.
119+
*/
120+
name(): string;
104121

105122
/**
106-
* Returns the process's compute utilization since the last query
123+
* Returns the GPU's total compute utilization since the last query
107124
* (or if the last query was a long time ago/never, since the start of the stored buffer).
108125
*/
109126
utilization(): NvidiaGPUUtilization;
127+
128+
/**
129+
* Returns data about the total memory utilization on this GPU.
130+
*/
131+
memory(): NvidiaGPUMemory;
110132
}

lib_nvml.h renamed to lib_nvidia.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file lib_nvml.h
2+
* @file lib_nvidia.h
33
* @author @2kai2kai2
44
*
55
* @copyright Copyright (c) 2025 2kai2kai2
@@ -19,16 +19,17 @@
1919

2020
#pragma once
2121
#include <napi.h>
22+
#include <vector>
2223

2324
struct nvmlDevice_st;
2425
typedef nvmlDevice_st *nvmlDevice_t;
2526

2627
class NvidiaGPU : public Napi::ObjectWrap<NvidiaGPU> {
2728
public:
29+
static Napi::Value allGPUs(const Napi::CallbackInfo &info);
2830
static Napi::Object Init(Napi::Env env, Napi::Object exports);
2931
NvidiaGPU(const Napi::CallbackInfo &info);
3032
~NvidiaGPU();
31-
static Napi::Value allGPUs(const Napi::CallbackInfo &info);
3233

3334
/**
3435
* Gets the device's name as a `Napi::String`.
@@ -37,11 +38,15 @@ class NvidiaGPU : public Napi::ObjectWrap<NvidiaGPU> {
3738
*/
3839
Napi::Value name(const Napi::CallbackInfo &info);
3940
Napi::Value utilization(const Napi::CallbackInfo &info);
41+
/**
42+
* @returns js object `{free: number, total: number, used: number}`
43+
*/
44+
Napi::Value memory(const Napi::CallbackInfo &info);
4045

4146
private:
4247
static Napi::FunctionReference *constructor;
48+
/** Is `NULL` when not using nvml */
4349
nvmlDevice_t device;
50+
size_t deviceIdx;
4451
unsigned long long lastSeenTimeStamp;
45-
uint32_t pid;
46-
// NvidiaGPU(const nvmlDevice_t device);
4752
};

0 commit comments

Comments
 (0)