Skip to content

Commit e3fa09f

Browse files
committed
MacOS implementation
That's right. I finally have access to a Mac, and (not) the time to work on this. Yep, I'm procrastinating something else. But I think this works, probably.
1 parent 55a4df3 commit e3fa09f

10 files changed

Lines changed: 280 additions & 320 deletions

File tree

.github/workflows/npm-publish.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,22 @@ jobs:
3131
with:
3232
name: win32-x64
3333
path: prebuilds/win32-x64/
34+
build-darwin-x64:
35+
runs-on: macos-latest
36+
steps:
37+
- uses: actions/checkout@v2
38+
- uses: actions/setup-node@v2
39+
with:
40+
node-version: '14'
41+
- run: npm install --ignore-scripts
42+
- run: npm run prebuild
43+
- uses: actions/upload-artifact@v2
44+
with:
45+
name: darwin-x64
46+
path: prebuilds/darwin-x64/
3447
publish-npm:
3548
runs-on: ubuntu-latest
36-
needs: [build-linux-x64, build-win32-x64]
49+
needs: [build-linux-x64, build-win32-x64, build-darwin-x64]
3750
steps:
3851
- uses: actions/checkout@v2
3952
- uses: actions/setup-node@v2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ node-addon-api/
33
build/
44
test/test_linux
55
test/test_win.exe
6+
test/test_mac
7+
test/test_mem
68
prebuilds/

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ npm install node-ps-data
3838

3939
## Known Issues and Future Plans
4040

41-
- MacOS implementation.
4241
- Build more binaries for other platforms and architectures.
4342
- More library functions:
4443
- CPU/Memory subcategories (e.g. child process time, pages info, etc.)

binding.gyp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@
3232
"OS=='mac'", {
3333
"targets": [{
3434
"target_name": "node_ps_data",
35-
"sources": ["mac.cc"],
35+
"sources": ["napi.cc", "lib_mac.cc"],
3636
"include_dirs": ["./node_modules/node-addon-api"],
3737
"cflags!": [ "-fno-exceptions" ],
3838
"cflags_cc!": [ "-fno-exceptions" ],
3939
"xcode_settings": {
4040
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
4141
"CLANG_CXX_LIBRARY": "libc++",
4242
"MACOSX_DEPLOYMENT_TARGET": "10.7"
43-
},
44-
"msvs_settings": {
45-
"VCCLCompilerTool": { "ExceptionHandling": 1 }
4643
}
4744
}]
4845
}]

lib_mac.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "lib.h"
2+
#include <iostream>
3+
#include <libproc.h>
4+
5+
/**
6+
Gets the CPU time used by a process in milliseconds.
7+
*/
8+
bool cpuTimeCpp(const size_t& pid, size_t& user, size_t& kernel) {
9+
proc_taskinfo info;
10+
int ret = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, (void*)&info,
11+
PROC_PIDTASKINFO_SIZE);
12+
if (ret > 0) {
13+
// Success!
14+
user = info.pti_total_user / 1000000;
15+
kernel = info.pti_total_system / 1000000;
16+
return true;
17+
} else {
18+
std::cerr << "Failed to get data about process " << pid
19+
<< " while trying to access cpu data." << std::endl;
20+
return false;
21+
}
22+
return true;
23+
}
24+
25+
/**
26+
Gets memory info in bytes.
27+
*/
28+
bool memInfoCpp(const size_t& pid, size_t& total, size_t& workingSet) {
29+
proc_taskinfo info;
30+
int ret = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, (void*)&info,
31+
PROC_PIDTASKINFO_SIZE);
32+
if (ret > 0) {
33+
// Success!
34+
total = info.pti_resident_size + info.pti_virtual_size;
35+
workingSet = info.pti_resident_size;
36+
return true;
37+
} else {
38+
std::cerr << "Failed to get data about process " << pid
39+
<< " while trying to access memory data." << std::endl;
40+
return false;
41+
}
42+
return true;
43+
}

0 commit comments

Comments
 (0)