|
| 1 | +// Flags: --no-warnings --expose-internals --expose-gc |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// Tests the HEAP_EXCEEDS_POINTER_COMPRESSION_LIMIT warning, which is emitted |
| 5 | +// once per thread when the heap that survives a full GC would not fit in the |
| 6 | +// 4 GiB pointer compression cage. It says the heap "is likely to exceed" the |
| 7 | +// limit from 6.5 GiB and "will exceed" it from 8 GiB. The thresholds are |
| 8 | +// lowered through an internal binding so the test does not need to allocate a |
| 9 | +// large heap. |
| 10 | + |
| 11 | +const common = require('../common'); |
| 12 | + |
| 13 | +common.skipIf32Bits(); |
| 14 | +if (process.config.variables.v8_enable_pointer_compression) { |
| 15 | + common.skip('the warning is only emitted without pointer compression'); |
| 16 | +} |
| 17 | + |
| 18 | +const assert = require('assert'); |
| 19 | +const { internalBinding } = require('internal/test/binding'); |
| 20 | +const { spawnSyncAndAssert } = require('../common/child_process'); |
| 21 | +const { Worker } = require('worker_threads'); |
| 22 | + |
| 23 | +const kCode = 'HEAP_EXCEEDS_POINTER_COMPRESSION_LIMIT'; |
| 24 | +const kLikely = /is likely to exceed that limit, and the application may run out of memory/; |
| 25 | +const kCertain = /will exceed that limit, and the application will run out of memory/; |
| 26 | +const childFlags = ['--expose-internals', '--expose-gc']; |
| 27 | + |
| 28 | +// Larger than any heap this test can have. |
| 29 | +const kNever = Number.MAX_SAFE_INTEGER; |
| 30 | + |
| 31 | +function triggerWarning(tier) { |
| 32 | + const { setPointerCompressionHeapWarningThresholds } = internalBinding('v8'); |
| 33 | + if (tier === 'likely') { |
| 34 | + setPointerCompressionHeapWarningThresholds(1, kNever); |
| 35 | + } else { |
| 36 | + setPointerCompressionHeapWarningThresholds(1, 1); |
| 37 | + } |
| 38 | + globalThis.gc(); |
| 39 | + // The warning is emitted from an unref'ed immediate, so keep the event loop |
| 40 | + // alive for one more iteration. |
| 41 | + setImmediate(() => {}); |
| 42 | +} |
| 43 | + |
| 44 | +switch (process.argv[2]) { |
| 45 | + case 'child': |
| 46 | + triggerWarning(process.argv[3]); |
| 47 | + return; |
| 48 | + case 'worker': { |
| 49 | + const { parentPort } = require('worker_threads'); |
| 50 | + process.on('warning', (warning) => { |
| 51 | + if (warning.code !== kCode) return; |
| 52 | + parentPort.postMessage({ name: warning.name, message: warning.message }); |
| 53 | + }); |
| 54 | + triggerWarning('certain'); |
| 55 | + return; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Collect only this warning; requiring internal/test/binding emits its own. |
| 60 | +const warnings = []; |
| 61 | +process.on('warning', (warning) => { |
| 62 | + if (warning.code === kCode) warnings.push(warning); |
| 63 | +}); |
| 64 | + |
| 65 | +// With the default thresholds, an ordinary heap does not trigger the warning. |
| 66 | +globalThis.gc(); |
| 67 | +setImmediate(common.mustCall(() => { |
| 68 | + assert.strictEqual(warnings.length, 0); |
| 69 | + |
| 70 | + // A heap between the two thresholds gets the "likely" wording. |
| 71 | + triggerWarning('likely'); |
| 72 | + |
| 73 | + setImmediate(common.mustCall(() => { |
| 74 | + // The warning is emitted only once per thread, and its wording is decided |
| 75 | + // when it is first triggered: a heap that later passes the "certain" |
| 76 | + // threshold does not emit a second warning. |
| 77 | + internalBinding('v8').setPointerCompressionHeapWarningThresholds(1, 1); |
| 78 | + globalThis.gc(); |
| 79 | + setImmediate(() => {}); |
| 80 | + })); |
| 81 | +})); |
| 82 | + |
| 83 | +process.on('exit', () => { |
| 84 | + assert.strictEqual(warnings.length, 1); |
| 85 | + const [warning] = warnings; |
| 86 | + assert.strictEqual(warning.name, 'Warning'); |
| 87 | + assert.match(warning.message, /The V8 heap of the main thread is using \d+ MiB/); |
| 88 | + assert.match(warning.message, /limited to 4096 MiB/); |
| 89 | + assert.match(warning.message, kLikely); |
| 90 | + assert.doesNotMatch(warning.message, kCertain); |
| 91 | +}); |
| 92 | + |
| 93 | +// A heap at or above the "certain" threshold gets the "will exceed" wording, |
| 94 | +// and the default stderr output includes the code. |
| 95 | +spawnSyncAndAssert(process.execPath, [ |
| 96 | + ...childFlags, |
| 97 | + __filename, |
| 98 | + 'child', |
| 99 | + 'certain', |
| 100 | +], { |
| 101 | + stderr(output) { |
| 102 | + assert.match( |
| 103 | + output, |
| 104 | + new RegExp(`\\[${kCode}\\] Warning: The V8 heap of the main thread`)); |
| 105 | + assert.match(output, kCertain); |
| 106 | + assert.doesNotMatch(output, kLikely); |
| 107 | + }, |
| 108 | +}); |
| 109 | + |
| 110 | +// The code can be used to disable the warning. |
| 111 | +spawnSyncAndAssert(process.execPath, [ |
| 112 | + ...childFlags, |
| 113 | + `--disable-warning=${kCode}`, |
| 114 | + __filename, |
| 115 | + 'child', |
| 116 | + 'certain', |
| 117 | +], { |
| 118 | + stderr(output) { |
| 119 | + assert.doesNotMatch(output, new RegExp(kCode)); |
| 120 | + }, |
| 121 | +}); |
| 122 | + |
| 123 | +// Each worker thread has its own heap and its own warning. |
| 124 | +const worker = new Worker(__filename, { argv: ['worker'] }); |
| 125 | +worker.on('message', common.mustCall((warning) => { |
| 126 | + assert.strictEqual(warning.name, 'Warning'); |
| 127 | + assert.match( |
| 128 | + warning.message, |
| 129 | + new RegExp(`The V8 heap of worker thread ${worker.threadId} is using`)); |
| 130 | + assert.match(warning.message, kCertain); |
| 131 | +})); |
0 commit comments