Skip to content

Commit be2cbcc

Browse files
committed
http2,perf_hooks: perf state using AliasedBuffer
Update performance_state to use AliasedBuffer and update usage sites. PR-URL: nodejs#18300 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 142d623 commit be2cbcc

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

src/env-inl.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ inline Environment::Environment(IsolateData* isolate_data,
344344
AssignToContext(context, ContextInfo(""));
345345

346346
destroy_async_id_list_.reserve(512);
347-
performance_state_ = Calloc<performance::performance_state>(1);
347+
performance_state_.reset(new performance::performance_state(isolate()));
348348
performance_state_->milestones[
349349
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT] =
350350
PERFORMANCE_NOW();
@@ -377,7 +377,6 @@ inline Environment::~Environment() {
377377
delete[] heap_statistics_buffer_;
378378
delete[] heap_space_statistics_buffer_;
379379
delete[] http_parser_buffer_;
380-
free(performance_state_);
381380
}
382381

383382
inline v8::Isolate* Environment::isolate() const {
@@ -583,7 +582,7 @@ void Environment::SetUnrefImmediate(native_immediate_callback cb,
583582
}
584583

585584
inline performance::performance_state* Environment::performance_state() {
586-
return performance_state_;
585+
return performance_state_.get();
587586
}
588587

589588
inline std::map<std::string, uint64_t>* Environment::performance_marks() {

src/env.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct nghttp2_rcbuf;
4848
namespace node {
4949

5050
namespace performance {
51-
struct performance_state;
51+
class performance_state;
5252
}
5353

5454
namespace loader {
@@ -758,7 +758,7 @@ class Environment {
758758

759759
int should_not_abort_scope_counter_ = 0;
760760

761-
performance::performance_state* performance_state_ = nullptr;
761+
std::unique_ptr<performance::performance_state> performance_state_;
762762
std::map<std::string, uint64_t> performance_marks_;
763763

764764
#if HAVE_INSPECTOR

src/node_http2.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,8 @@ Http2Session::~Http2Session() {
554554
}
555555

556556
inline bool HasHttp2Observer(Environment* env) {
557-
uint32_t* observers = env->performance_state()->observers;
557+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
558+
env->performance_state()->observers;
558559
return observers[performance::NODE_PERFORMANCE_ENTRY_TYPE_HTTP2] != 0;
559560
}
560561

src/node_perf.cc

+17-18
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ void PerformanceEntry::Notify(Environment* env,
8585
PerformanceEntryType type,
8686
Local<Value> object) {
8787
Context::Scope scope(env->context());
88-
uint32_t* observers = env->performance_state()->observers;
89-
if (observers != nullptr &&
90-
type != NODE_PERFORMANCE_ENTRY_TYPE_INVALID &&
88+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
89+
env->performance_state()->observers;
90+
if (type != NODE_PERFORMANCE_ENTRY_TYPE_INVALID &&
9191
observers[type]) {
9292
node::MakeCallback(env->isolate(),
9393
env->process_object(),
@@ -130,7 +130,8 @@ void Measure(const FunctionCallbackInfo<Value>& args) {
130130
Utf8Value startMark(env->isolate(), args[1]);
131131
Utf8Value endMark(env->isolate(), args[2]);
132132

133-
double* milestones = env->performance_state()->milestones;
133+
AliasedBuffer<double, v8::Float64Array>& milestones =
134+
env->performance_state()->milestones;
134135

135136
uint64_t startTimestamp = timeOrigin;
136137
uint64_t start = GetPerformanceMark(env, *startMark);
@@ -165,7 +166,8 @@ void Measure(const FunctionCallbackInfo<Value>& args) {
165166
void MarkMilestone(const FunctionCallbackInfo<Value>& args) {
166167
Environment* env = Environment::GetCurrent(args);
167168
Local<Context> context = env->context();
168-
double* milestones = env->performance_state()->milestones;
169+
AliasedBuffer<double, v8::Float64Array>& milestones =
170+
env->performance_state()->milestones;
169171
PerformanceMilestone milestone =
170172
static_cast<PerformanceMilestone>(
171173
args[0]->Int32Value(context).ToChecked());
@@ -187,7 +189,8 @@ void PerformanceGCCallback(Environment* env, void* ptr) {
187189
HandleScope scope(env->isolate());
188190
Local<Context> context = env->context();
189191

190-
uint32_t* observers = env->performance_state()->observers;
192+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
193+
env->performance_state()->observers;
191194
if (observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]) {
192195
Local<Object> obj = entry->ToObject();
193196
v8::PropertyAttribute attr =
@@ -289,8 +292,8 @@ void TimerFunctionCall(const FunctionCallbackInfo<Value>& args) {
289292
args.GetReturnValue().Set(ret.ToLocalChecked());
290293
}
291294

292-
293-
uint32_t* observers = env->performance_state()->observers;
295+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
296+
env->performance_state()->observers;
294297
if (!observers[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION])
295298
return;
296299

@@ -323,16 +326,12 @@ void Init(Local<Object> target,
323326
performance_state* state = env->performance_state();
324327
auto state_ab = ArrayBuffer::New(isolate, state, sizeof(*state));
325328

326-
#define SET_STATE_TYPEDARRAY(name, type, field) \
327-
target->Set(context, \
328-
FIXED_ONE_BYTE_STRING(isolate, (name)), \
329-
type::New(state_ab, \
330-
offsetof(performance_state, field), \
331-
arraysize(state->field))) \
332-
.FromJust()
333-
SET_STATE_TYPEDARRAY("observerCounts", v8::Uint32Array, observers);
334-
SET_STATE_TYPEDARRAY("milestones", v8::Float64Array, milestones);
335-
#undef SET_STATE_TYPEDARRAY
329+
target->Set(context,
330+
FIXED_ONE_BYTE_STRING(isolate, "observerCounts"),
331+
state->observers.GetJSArray()).FromJust();
332+
target->Set(context,
333+
FIXED_ONE_BYTE_STRING(isolate, "milestones"),
334+
state->milestones.GetJSArray()).FromJust();
336335

337336
Local<String> performanceEntryString =
338337
FIXED_ONE_BYTE_STRING(isolate, "PerformanceEntry");

src/node_perf_common.h

+27-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,33 @@ enum PerformanceEntryType {
6161
node::performance::NODE_PERFORMANCE_MILESTONE_##n); \
6262
} while (0);
6363

64-
struct performance_state {
65-
// doubles first so that they are always sizeof(double)-aligned
66-
double milestones[NODE_PERFORMANCE_MILESTONE_INVALID];
67-
uint32_t observers[NODE_PERFORMANCE_ENTRY_TYPE_INVALID];
64+
class performance_state {
65+
public:
66+
explicit performance_state(v8::Isolate* isolate) :
67+
root(
68+
isolate,
69+
sizeof(performance_state_internal)),
70+
milestones(
71+
isolate,
72+
offsetof(performance_state_internal, milestones),
73+
NODE_PERFORMANCE_MILESTONE_INVALID,
74+
root),
75+
observers(
76+
isolate,
77+
offsetof(performance_state_internal, observers),
78+
NODE_PERFORMANCE_ENTRY_TYPE_INVALID,
79+
root) {}
80+
81+
AliasedBuffer<uint8_t, v8::Uint8Array> root;
82+
AliasedBuffer<double, v8::Float64Array> milestones;
83+
AliasedBuffer<uint32_t, v8::Uint32Array> observers;
84+
85+
private:
86+
struct performance_state_internal {
87+
// doubles first so that they are always sizeof(double)-aligned
88+
double milestones[NODE_PERFORMANCE_MILESTONE_INVALID];
89+
uint32_t observers[NODE_PERFORMANCE_ENTRY_TYPE_INVALID];
90+
};
6891
};
6992

7093
} // namespace performance

0 commit comments

Comments
 (0)