GeezerVison is showing github.com — perf_hooks: fix truncation of uvMetricsInfo counters · nodejs/node@c418cf6
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_perf.cc +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -280,10 +280,12 @@ void UvMetricsInfo(const FunctionCallbackInfo<Value>& args) { |
280 | 280 | uv_metrics_t metrics; |
281 | 281 | // uv_metrics_info always return 0 |
282 | 282 | CHECK_EQ(uv_metrics_info(env->event_loop(), &metrics), 0); |
283 | | - AliasedInt32Array& buffer = env->performance_state()->uv_metrics; |
284 | | - buffer[0] = static_cast<int32_t>(metrics.loop_count); |
285 | | - buffer[1] = static_cast<int32_t>(metrics.events); |
286 | | - buffer[2] = static_cast<int32_t>(metrics.events_waiting); |
| 283 | + // libuv reports 64-bit counters. Store them as doubles so that they are |
| 284 | + // exact up to Number.MAX_SAFE_INTEGER instead of wrapping at 2^31. |
| 285 | + AliasedFloat64Array& buffer = env->performance_state()->uv_metrics; |
| 286 | + buffer[0] = static_cast<double>(metrics.loop_count); |
| 287 | + buffer[1] = static_cast<double>(metrics.events); |
| 288 | + buffer[2] = static_cast<double>(metrics.events_waiting); |
287 | 289 | } |
288 | 290 |
|
289 | 291 | void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) { |
|
Collapse file
Copy file name to clipboardExpand all lines: src/node_perf_common.h +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,7 +79,7 @@ class PerformanceState { |
79 | 79 | AliasedUint8Array root; |
80 | 80 | AliasedFloat64Array milestones; |
81 | 81 | AliasedUint32Array observers; |
82 | | - AliasedInt32Array uv_metrics; |
| 82 | + AliasedFloat64Array uv_metrics; |
83 | 83 |
|
84 | 84 | uint64_t performance_last_gc_start_mark = 0; |
85 | 85 | uint16_t current_gc_type = 0; |
@@ -93,8 +93,8 @@ class PerformanceState { |
93 | 93 | struct performance_state_internal { |
94 | 94 | // doubles first so that they are always sizeof(double)-aligned |
95 | 95 | double milestones[NODE_PERFORMANCE_MILESTONE_INVALID]; |
| 96 | + double uv_metrics[3]; |
96 | 97 | uint32_t observers[NODE_PERFORMANCE_ENTRY_TYPE_INVALID]; |
97 | | - int32_t uv_metrics[3]; |
98 | 98 | }; |
99 | 99 | }; |
100 | 100 |
|
|
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +require('../common'); |
| 5 | +const assert = require('node:assert'); |
| 6 | +const { internalBinding } = require('internal/test/binding'); |
| 7 | + |
| 8 | +// The event loop metrics reported by libuv are 64-bit counters. The buffer |
| 9 | +// used to transfer them to JavaScript must not truncate them to 32 bits. |
| 10 | +const { uvMetricsBuffer, uvMetricsInfo } = internalBinding('performance'); |
| 11 | +assert.ok(uvMetricsBuffer instanceof Float64Array); |
| 12 | +assert.strictEqual(uvMetricsBuffer.length, 3); |
| 13 | + |
| 14 | +uvMetricsInfo(); |
| 15 | +for (const value of uvMetricsBuffer) { |
| 16 | + assert.ok(Number.isSafeInteger(value), `${value} is not a safe integer`); |
| 17 | + assert.ok(value >= 0, `${value} is negative`); |
| 18 | +} |
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-performance-nodetiming-uvmetricsinfo.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,10 +13,13 @@ const fixtures = require('../common/fixtures'); |
13 | 13 |
|
14 | 14 | const file = fixtures.path('test-nodetiming-uvmetricsinfo.js'); |
15 | 15 |
|
16 | | -{ |
| 16 | +// Run both with and without the built-in startup snapshot, as the |
| 17 | +// performance state buffers are initialized differently in each case. |
| 18 | +for (const execArgv of [[], ['--no-node-snapshot']]) { |
17 | 19 | const { status, stderr } = spawnSync( |
18 | 20 | process.execPath, |
19 | 21 | [ |
| 22 | + ...execArgv, |
20 | 23 | file, |
21 | 24 | ], |
22 | 25 | ); |
|
Collapse file
Copy file name to clipboardExpand all lines: typings/internalBinding/performance.d.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -146,6 +146,6 @@ export interface PerformanceBinding { |
146 | 146 | ): InternalPerformanceBinding.ELDHistogram; |
147 | 147 | markBootstrapComplete(): void; |
148 | 148 | uvMetricsInfo(): void; |
149 | | - uvMetricsBuffer: Int32Array; |
| 149 | + uvMetricsBuffer: Float64Array; |
150 | 150 | now(): number; |
151 | 151 | } |