Skip to content

Commit f587bef

Browse files
authored
Merge pull request #202 from bizob2828/fix-mark-sweep-node-18
updated GC mapping to account for new enum types in node 18(v8 10)
2 parents 3f4a576 + 68387cb commit f587bef

File tree

11 files changed

+90
-74
lines changed

11 files changed

+90
-74
lines changed

THIRD_PARTY_NOTICES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
102102

103103
### @newrelic/eslint-config
104104

105-
This product includes source derived from [@newrelic/eslint-config](https://github.com/newrelic/eslint-config-newrelic) ([v0.0.2](https://github.com/newrelic/eslint-config-newrelic/tree/v0.0.2)), distributed under the [Apache-2.0 License](https://github.com/newrelic/eslint-config-newrelic/blob/v0.0.2/LICENSE):
105+
This product includes source derived from [@newrelic/eslint-config](https://github.com/newrelic/eslint-config-newrelic) ([v0.0.4](https://github.com/newrelic/eslint-config-newrelic/tree/v0.0.4)), distributed under the [Apache-2.0 License](https://github.com/newrelic/eslint-config-newrelic/blob/v0.0.4/LICENSE):
106106

107107
```
108108
Apache License

index.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,36 @@
55

66
'use strict'
77

8-
var EventEmitter = require('events').EventEmitter
9-
var util = require('util')
10-
var preBuild = require('./lib/pre-build')
11-
var natives = preBuild.load('native_metrics')
12-
13-
var DEFAULT_TIMEOUT = 15 * 1000 // 15 seconds
14-
var GC_TYPE_NAMES = {
15-
1: 'Scavenge',
16-
2: 'MarkSweepCompact',
17-
4: 'IncrementalMarking',
18-
8: 'ProcessWeakCallbacks',
19-
20-
3: 'All', // Node v4 and earlier only have Scavenge and MarkSweepCompact.
21-
15: 'All'
8+
const EventEmitter = require('events').EventEmitter
9+
const util = require('util')
10+
const preBuild = require('./lib/pre-build')
11+
const natives = preBuild.load('native_metrics')
12+
const semver = require('semver')
13+
14+
const DEFAULT_TIMEOUT = 15 * 1000 // 15 seconds
15+
let GC_TYPE_NAMES = null
16+
17+
// In Node 18(v8 10) the GCType enum added `MinorMarkCompact`
18+
// we have to update our mapping to properly account for this
19+
if (semver.satisfies(process.version, '>=18')) {
20+
GC_TYPE_NAMES = {
21+
1: 'Scavenge',
22+
2: 'MinorMarkCompact',
23+
4: 'MarkSweepCompact',
24+
8: 'IncrementalMarking',
25+
16: 'ProcessWeakCallbacks',
26+
31: 'All'
27+
}
28+
} else {
29+
GC_TYPE_NAMES = {
30+
1: 'Scavenge',
31+
2: 'MarkSweepCompact',
32+
4: 'IncrementalMarking',
33+
8: 'ProcessWeakCallbacks',
34+
35+
3: 'All', // Node v4 and earlier only have Scavenge and MarkSweepCompact.
36+
15: 'All'
37+
}
2238
}
2339

2440
/**
@@ -135,11 +151,11 @@ NativeMetricEmitter.prototype.getLoopMetrics = function getLoopMetrics() {
135151
* information on the GC events that happened.
136152
*/
137153
NativeMetricEmitter.prototype.getGCMetrics = function getGCMetrics() {
138-
var gcMetrics = this._gcBinder.read()
139-
var results = Object.create(null)
140-
for (var typeId in gcMetrics) {
154+
const gcMetrics = this._gcBinder.read()
155+
const results = Object.create(null)
156+
for (const typeId in gcMetrics) {
141157
if (gcMetrics.hasOwnProperty(typeId) && gcMetrics[typeId].count > 0) {
142-
var typeName = GC_TYPE_NAMES[String(typeId)]
158+
const typeName = GC_TYPE_NAMES[String(typeId)]
143159
results[typeName] = {
144160
typeId: parseInt(typeId, 10),
145161
type: typeName,
@@ -151,7 +167,7 @@ NativeMetricEmitter.prototype.getGCMetrics = function getGCMetrics() {
151167
return results
152168
}
153169

154-
var emitter = null
170+
let emitter = null
155171

156172
/**
157173
* Retrieves the {@link NativeMetricEmitter} singleton instance.

lib/pre-build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ function download(target, cb) {
237237
return cb(new Error('Failed to download ' + url + ': code ' + res.statusCode))
238238
}
239239

240-
var unzip = zlib.createGunzip()
241-
var buffers = []
240+
const unzip = zlib.createGunzip()
241+
const buffers = []
242242
let size = 0
243243
res.pipe(unzip).on('data', function onResData(data) {
244244
buffers.push(data)

lib/upload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const path = require('path')
2323
// XXX AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be set in the environment.
2424
const AWS = require('aws-sdk')
2525
const s3 = new AWS.S3()
26-
var S3_BUCKET = 'nr-downloads-main'
26+
const S3_BUCKET = 'nr-downloads-main'
2727
const CMD = 'upload'
2828

2929
if (require.main === module) {

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"npm": ">=6"
7878
},
7979
"devDependencies": {
80-
"@newrelic/eslint-config": "^0.0.2",
80+
"@newrelic/eslint-config": "^0.0.4",
8181
"@newrelic/newrelic-oss-cli": "^0.1.2",
8282
"@newrelic/proxy": "^2.0.0",
8383
"async": "^3.2.2",

tests/integration/loop-performance.tap.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
'use strict'
77

8-
var tap = require('tap')
8+
const tap = require('tap')
99

10-
var TEST_DURATION = 30 * 1000
10+
const TEST_DURATION = 30 * 1000
1111

1212
tap.test('loop performance test', { timeout: 2 * TEST_DURATION + 1000 }, function (t) {
13-
var timeout = null
14-
var callCount = 0
15-
var natives = null
13+
let timeout = null
14+
let callCount = 0
15+
let natives = null
1616

1717
t.teardown(function () {
1818
if (natives) {
@@ -23,19 +23,19 @@ tap.test('loop performance test', { timeout: 2 * TEST_DURATION + 1000 }, functio
2323
t.comment('measuring without loop counter')
2424
setTimeoutCount()
2525
setTimeout(function () {
26-
var noMetricsCount = callCount
26+
const noMetricsCount = callCount
2727
callCount = 0
2828
clearTimeout(timeout)
2929

3030
natives = require('../../')()
31-
var readInterval = setInterval(function () {
31+
const readInterval = setInterval(function () {
3232
natives.getLoopMetrics() // To reset the metrics
3333
}, 1000)
3434

3535
t.comment('measuring with loop counter')
3636
setTimeoutCount()
3737
setTimeout(function () {
38-
var withMetricsCount = callCount
38+
const withMetricsCount = callCount
3939
callCount = 0
4040
clearTimeout(timeout)
4141
clearInterval(readInterval)

tests/unit/gc-metrics.tap.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55

66
'use strict'
77

8-
var tap = require('tap')
8+
const tap = require('tap')
99

1010
tap.test('GC Metrics', function (t) {
1111
t.plan(17)
12-
var metricEmitter = require('../../')()
12+
const metricEmitter = require('../../')()
1313

1414
global.gc()
1515

16-
var gcs = metricEmitter.getGCMetrics()
17-
var keys = Object.keys(gcs)
16+
const gcs = metricEmitter.getGCMetrics()
17+
const keys = Object.keys(gcs)
1818
if (!t.ok(keys.length > 0, 'should notice at least one GC')) {
1919
return t.end()
2020
}
2121
t.type(keys[0], 'string', 'should have strings as keys')
2222

2323
t.comment('GC stats objects')
24-
var stats = gcs[keys[0]]
24+
const stats = gcs[keys[0]]
2525
t.type(stats, 'object', 'should have stats objects')
2626
t.type(stats.typeId, 'number', 'should have the type ID')
2727
t.type(stats.type, 'string', 'should have the type name')
@@ -30,7 +30,7 @@ tap.test('GC Metrics', function (t) {
3030
}
3131

3232
t.comment('GC stats metrics')
33-
var metrics = stats.metrics
33+
const metrics = stats.metrics
3434
t.type(metrics.total, 'number', 'should have total field')
3535
t.type(metrics.min, 'number', 'should have min field')
3636
t.type(metrics.max, 'number', 'should have max field')

tests/unit/licenses.tap.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55

66
'use strict'
77

8-
var a = require('async')
9-
var fs = require('fs')
10-
var path = require('path')
11-
var pkg = require('../../package')
12-
var tap = require('tap')
8+
const a = require('async')
9+
const fs = require('fs')
10+
const path = require('path')
11+
const pkg = require('../../package')
12+
const tap = require('tap')
1313

14-
var MODULE_DIR = path.resolve(__dirname, '../../node_modules')
15-
var LICENSES = {
14+
const MODULE_DIR = path.resolve(__dirname, '../../node_modules')
15+
const LICENSES = {
1616
'nan': 'MIT',
1717
'https-proxy-agent': 'MIT',
1818
'semver': 'ISC'
1919
}
2020

2121
tap.test('Dependency licenses', function (t) {
22-
var deps = Object.keys(pkg.dependencies || {})
22+
const deps = Object.keys(pkg.dependencies || {})
2323
deps.push.apply(deps, Object.keys(pkg.optionalDependencies || {}))
2424
a.map(
2525
deps,
@@ -35,8 +35,8 @@ tap.test('Dependency licenses', function (t) {
3535
},
3636
function parsePkg(depPackage, parsePkgCb) {
3737
try {
38-
var parsedPackage = JSON.parse(depPackage)
39-
var license = parsedPackage.license || parsedPackage.licenses
38+
const parsedPackage = JSON.parse(depPackage)
39+
const license = parsedPackage.license || parsedPackage.licenses
4040
process.nextTick(function () {
4141
parsePkgCb(null, [dep, license])
4242
})
@@ -50,7 +50,7 @@ tap.test('Dependency licenses', function (t) {
5050
},
5151
function (err, depLicensesArray) {
5252
if (t.error(err, 'should not fail to retrieve licenses')) {
53-
var depLicenses = depLicensesArray.reduce(function (obj, dep) {
53+
const depLicenses = depLicensesArray.reduce(function (obj, dep) {
5454
obj[dep[0]] = dep[1]
5555
return obj
5656
}, {})

tests/unit/loop-metrics.tap.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55

66
'use strict'
77

8-
var tap = require('tap')
8+
const tap = require('tap')
99

1010
tap.test('Loop Metrics', function (t) {
11-
var MICRO_TO_MILLIS = 1e-3
12-
var SPIN_TIME = 2000
13-
var CPU_EPSILON = SPIN_TIME * 0.05 // Allowed fudge factor for CPU times in MS
14-
var metricEmitter = require('../../')()
15-
var testStart = Date.now()
11+
const MICRO_TO_MILLIS = 1e-3
12+
const SPIN_TIME = 2000
13+
const CPU_EPSILON = SPIN_TIME * 0.05 // Allowed fudge factor for CPU times in MS
14+
const metricEmitter = require('../../')()
15+
const testStart = Date.now()
1616

1717
t.teardown(function () {
1818
metricEmitter.unbind()
1919
})
2020

2121
// Check the structure of the metric object.
22-
var metric = metricEmitter.getLoopMetrics().usage
22+
let metric = metricEmitter.getLoopMetrics().usage
2323
t.type(metric, Object, 'should provide a metric object')
2424
t.type(metric.total, 'number', 'should have a total')
2525
t.type(metric.min, 'number', 'should have a min')
@@ -53,17 +53,17 @@ tap.test('Loop Metrics', function (t) {
5353
// the actual loop time because the process isn't doing anything.
5454
setTimeout(function spinner() {
5555
t.comment('spinning cpu...')
56-
var start = Date.now()
56+
const start = Date.now()
5757
while (Date.now() - start < SPIN_TIME) {} // Spin the CPU for 2 seconds.
5858

5959
// Finally, wait another tick and then check the loop stats.
6060
setTimeout(function () {
6161
metric = metricEmitter.getLoopMetrics()
62-
var testDuration = Date.now() - testStart + CPU_EPSILON
63-
var durationSquare = testDuration * testDuration
64-
var usage = metric.usage
62+
const testDuration = Date.now() - testStart + CPU_EPSILON
63+
const durationSquare = testDuration * testDuration
64+
const usage = metric.usage
6565

66-
var meanTime = usage.total / usage.count
66+
const meanTime = usage.total / usage.count
6767
t.ok(
6868
usage.total * MICRO_TO_MILLIS > SPIN_TIME - CPU_EPSILON,
6969
'should have total greater than spin time'

0 commit comments

Comments
 (0)