Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix incorrect gc types in newer versions of node #61

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"compile_commands": "node-gyp configure --release -- -f gyp.generator.compile_commands_json.py && mv Release/compile_commands.json ./",
"rebuild": "node-gyp rebuild -j max",
"lint": "node scripts/check_licenses.js && eslint .",
"test": "mocha 'test/**/*.spec.js' && node test/main"
"test": "mocha -n expose-gc 'test/**/*.spec.js' && node test/main"
},
"keywords": [
"datadog",
Expand All @@ -34,7 +34,6 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-standard": "^5.0.0",
"mocha": "^9.0.3",
"nan": "^2.17.0"
"mocha": "^9.0.3"
}
}
66 changes: 47 additions & 19 deletions src/metrics/GarbageCollection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Napi::Env;
using Napi::Object;
using Napi::Value;
using Napi::VersionManagement;

namespace datadog {
class GarbageCollection {
Expand All @@ -27,28 +28,14 @@ namespace datadog {
Value ToJSON(Env env);
private:
std::map<v8::GCType, Histogram> pause_;
Histogram pause_all_;
std::map<unsigned char, const char*> types_;
uint64_t start_time_;

const char* ToType(Env env, v8::GCType);
};

GarbageCollection::GarbageCollection() {
#if NODE_MODULE_VERSION >= 108
types_[1] = "scavenge";
types_[2] = "minor_mark_compact";
types_[4] = "mark_sweep_compact";
types_[8] = "incremental_marking";
types_[16] = "process_weak_callbacks";
types_[31] = "all";
#else
types_[1] = "scavenge";
types_[2] = "mark_sweep_compact";
types_[3] = "all";
types_[4] = "incremental_marking";
types_[8] = "process_weak_callbacks";
types_[15] = "all";
#endif

pause_[v8::GCType::kGCTypeAll] = Histogram();
start_time_ = uv_hrtime();
}

Expand Down Expand Up @@ -86,15 +73,56 @@ namespace datadog {
}

pause_[type].add(usage);
pause_[v8::GCType::kGCTypeAll].add(usage);
pause_all_.add(usage);
}

Value GarbageCollection::ToJSON(Env env) {
Object gc = Object::New(env);

for (auto &it : pause_) {
gc.Set(types_[it.first], it.second.ToJSON(env));
auto type = this->ToType(env, it.first);
gc.Set(type, it.second.ToJSON(env));
it.second.reset();
}

gc.Set("all", pause_all_.ToJSON(env));
pause_all_.reset();

return gc;
}

const char* GarbageCollection::ToType(Env env, v8::GCType type) {
auto version = VersionManagement::GetNodeVersion(env);
auto type_bit = static_cast<char>(type);

if (version->major >= 22) {
switch (type_bit) {
case 1: return "scavenge";
case 2: return "minor_mark_sweep";
case 4: return "mark_sweep_compact"; // Deprecated, might be removed soon.
case 8: return "incremental_marking";
case 16: return "process_weak_callbacks";
case 31: return "all";
}
} else if (version->major >= 18) {
switch (type_bit) {
case 1: return "scavenge";
case 2: return "minor_mark_compact";
case 4: return "mark_sweep_compact";
case 8: return "incremental_marking";
case 16: return "process_weak_callbacks";
case 31: return "all";
}
} else {
switch (type_bit) {
case 1: return "scavenge";
case 2: return "mark_sweep_compact";
case 4: return "incremental_marking";
case 8: return "process_weak_callbacks";
case 15: return "all";
}
}

return "unknown";
}
}
10 changes: 6 additions & 4 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const path = require('path')
const { Worker } = require('worker_threads')

const worker = new Worker(path.join(__dirname, 'worker.js'))
for (let i = 0; i < 100; i++) {
const worker = new Worker(path.join(__dirname, 'worker.js'))

setTimeout(() => {
worker.terminate()
}, 1000)
setTimeout(() => {
worker.terminate()
}, 1000)
}
3 changes: 3 additions & 0 deletions test/metrics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('metrics', () => {
})

it('should collect stats', () => {
globalThis.gc()

const stats = nativeMetrics.stats()

expect(stats).to.have.property('cpu')
Expand All @@ -53,6 +55,7 @@ describe('metrics', () => {

expect(stats).to.have.property('gc')
expect(stats.gc).to.have.property('all')
expect(stats.gc).to.not.have.property('unknown')
expect(stats.gc.all).to.have.property('min')
expect(stats.gc.all.min).to.be.a('number')
expect(stats.gc.all).to.have.property('max')
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1178,11 +1178,6 @@ [email protected]:
resolved "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

nan@^2.17.0:
version "2.17.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.17.0.tgz#c0150a2368a182f033e9aa5195ec76ea41a199cb"
integrity sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ==

[email protected]:
version "3.3.1"
resolved "https://registry.npmjs.org/nanoid/-/nanoid-3.3.1.tgz"
Expand Down
Loading