Skip to content

Commit b384363

Browse files
committed
Initial nvml code
Currently there are a few *issues* related to the environment/build. - Windows build is failing. Gotta figure that out. - Getting process utilization returns nothing. This may be due to the fact I'm using WSL, meaning there's WDDM running. Or maybe it's just a mistake on my part.
1 parent 38fe45c commit b384363

13 files changed

Lines changed: 668 additions & 130 deletions

.github/workflows/npm-publish.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ jobs:
4949
with:
5050
path: prebuilds/
5151
- run: npm run install-dependencies
52-
- run: npm run build-ts
5352
- run: echo "tgz_name=$(npm pack)" >> "$GITHUB_OUTPUT"
5453
id: pack
5554
- uses: actions/upload-artifact@v2

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ test/test_mac
99
test/test_mem
1010
prebuilds/
1111
*.obj
12-
node-ps-data-*.tgz
13-
*.js
14-
*.d.ts
12+
node-ps-data-*.tgz

binding.gyp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
["OS=='win'", {
44
"targets": [{
55
"target_name": "node_ps_data",
6-
"sources": ["napi.cc", "lib_win.cc"],
7-
"include_dirs": ["./node_modules/node-addon-api"],
6+
"sources": ["napi.cc", "lib_win.cc", "lib_nvml.cc"],
7+
"include_dirs": [
8+
"./node_modules/node-addon-api",
9+
"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.9/include"
10+
],
811
"ldflags": [
9-
"-lpsapi" # gcc/g++
12+
"-lpsapi", # gcc/g++
13+
"-lnvidia-ml",
1014
],
1115
"link_settings": {
1216
"libraries": [
@@ -23,16 +27,24 @@
2327
"OS=='linux'", {
2428
"targets": [{
2529
"target_name": "node_ps_data",
26-
"sources": ["napi.cc", "lib_linux.cc"],
30+
"sources": ["napi.cc", "lib_linux.cc", "lib_nvml.cc"],
2731
"include_dirs": ["./node_modules/node-addon-api"],
32+
"link_settings": {
33+
"libraries": [
34+
"/usr/lib/x86_64-linux-gnu/libnvidia-ml.so",
35+
],
36+
},
37+
"ldflags": [
38+
"-lnvidia-ml",
39+
],
2840
"cflags!": [ "-fno-exceptions" ],
2941
"cflags_cc!": [ "-fno-exceptions" ]
3042
}]
3143
},
3244
"OS=='mac'", {
3345
"targets": [{
3446
"target_name": "node_ps_data",
35-
"sources": ["napi.cc", "lib_mac.cc"],
47+
"sources": ["napi.cc", "lib_mac.cc", "lib_nvml_stub.cc"],
3648
"include_dirs": ["./node_modules/node-addon-api"],
3749
"cflags!": [ "-fno-exceptions" ],
3850
"cflags_cc!": [ "-fno-exceptions" ],

index.d.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
/**
3+
* @file index.d.ts
4+
* @author @2kai2kai2
5+
*
6+
* @copyright Copyright (c) 2025 2kai2kai2
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
/**
22+
* @param pid Process ID to search for.
23+
* @returns Process user-mode CPU usage time in ms, or 0 if not found.
24+
*/
25+
declare function cpuUserTime(pid: number): number;
26+
27+
/**
28+
* @param pid Process ID to search for.
29+
* @returns Process kernel-mode (system-mode) CPU usage time in ms, or 0 if not found.
30+
*/
31+
declare function cpuKernelTime(pid: number): number;
32+
33+
/**
34+
* @param pid Process ID to search for.
35+
* @returns Process total CPU usage time in ms, or 0 if not found.
36+
*/
37+
declare function cpuTime(pid: number): number;
38+
39+
/**
40+
* @param pid Process ID to search for.
41+
* @returns Total usage in bytes. Windows includes shared working set while linux is more strict.
42+
*
43+
* Note that as this includes virtual memory, it will often be extremely high on MacOS.
44+
*/
45+
declare function memInfo(pid: number): number;
46+
47+
/**
48+
* @param pid Process ID to search for.
49+
* @returns Resident/working set size in bytes. Windows includes shared working set while linux is more strict.
50+
*
51+
* Depending on operating system, this may be approximate. It represents the total amount of physical memory being used,
52+
* which does not include any vitual memory.
53+
*/
54+
declare function memRSS(pid: number): number;
55+
56+
/**
57+
* @param pid Process ID to search for.
58+
* @returns Number of bytes process has read from file.
59+
*/
60+
declare function fileRead(pid: number): number;
61+
62+
/**
63+
* @param pid Process ID to search for.
64+
* @returns Number of bytes process has written to file.
65+
*/
66+
declare function fileWrite(pid: number): number;
67+
68+
/**
69+
* The return type for `NvidiaGPU::utilization` that shows utilization of computing resources.
70+
*
71+
* NOTE: currently only contains smUtilization (which is what most people need),
72+
* but things like video encode/decode accelerator data is not yet implemented.
73+
*/
74+
declare interface NvidiaGPUUtilization {
75+
/**
76+
* The utilization of the device's Steaming Multiprocessors (SMs)
77+
*
78+
* This includes most 3D and compute operations.
79+
*/
80+
smUtilization: number
81+
}
82+
83+
/**
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))
85+
*/
86+
declare class NvidiaGPU {
87+
/**
88+
* A good entrypoint, will return handles for all Nvidia GPUs on this computer.
89+
*
90+
* If the computer does not have Nvidia GPUs (or drivers) it will be an empty array.
91+
*
92+
* @throws
93+
*/
94+
static allGPUs(): NvidiaGPU[];
95+
96+
/**
97+
* @param deviceIdx The index of the GPU to get a handle for.
98+
* @param pid The process id to search for.
99+
*
100+
* @throws An error if `deviceIdx` is invalid or if we do not have permission to access it.
101+
* @throws
102+
*/
103+
constructor(deviceIdx: number, pid: number);
104+
105+
/**
106+
* Returns the process's compute utilization since the last query
107+
* (or if the last query was a long time ago/never, since the start of the stored buffer).
108+
*/
109+
utilization(): NvidiaGPUUtilization;
110+
}

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @file index.js
3+
* @author @2kai2kai2
4+
*
5+
* @copyright Copyright (c) 2025 2kai2kai2
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
module.exports = require("node-gyp-build")(__dirname);

index.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)