Skip to content

Commit 39612a8

Browse files
kfarnungevanlucas
authored andcommitted
http2,perf_hooks: perf state using AliasedBuffer
Update performance_state to use AliasedBuffer and update usage sites. PR-URL: #18300 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 574d3b9 commit 39612a8

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
@@ -313,7 +313,7 @@ inline Environment::Environment(IsolateData* isolate_data,
313313
AssignToContext(context, ContextInfo(""));
314314

315315
destroy_async_id_list_.reserve(512);
316-
performance_state_ = Calloc<performance::performance_state>(1);
316+
performance_state_.reset(new performance::performance_state(isolate()));
317317
performance_state_->milestones[
318318
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT] =
319319
PERFORMANCE_NOW();
@@ -346,7 +346,6 @@ inline Environment::~Environment() {
346346
delete[] heap_statistics_buffer_;
347347
delete[] heap_space_statistics_buffer_;
348348
delete[] http_parser_buffer_;
349-
free(performance_state_);
350349
}
351350

352351
inline v8::Isolate* Environment::isolate() const {
@@ -541,7 +540,7 @@ void Environment::SetImmediate(native_immediate_callback cb,
541540
}
542541

543542
inline performance::performance_state* Environment::performance_state() {
544-
return performance_state_;
543+
return performance_state_.get();
545544
}
546545

547546
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 {
@@ -709,7 +709,7 @@ class Environment {
709709

710710
int should_not_abort_scope_counter_ = 0;
711711

712-
performance::performance_state* performance_state_ = nullptr;
712+
std::unique_ptr<performance::performance_state> performance_state_;
713713
std::map<std::string, uint64_t> performance_marks_;
714714

715715
#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());
@@ -188,7 +190,8 @@ void PerformanceGCCallback(uv_async_t* handle) {
188190
HandleScope scope(env->isolate());
189191
Local<Context> context = env->context();
190192

191-
uint32_t* observers = env->performance_state()->observers;
193+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
194+
env->performance_state()->observers;
192195
if (observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]) {
193196
Local<Object> obj = entry->ToObject();
194197
v8::PropertyAttribute attr =
@@ -297,8 +300,8 @@ void TimerFunctionCall(const FunctionCallbackInfo<Value>& args) {
297300
args.GetReturnValue().Set(ret.ToLocalChecked());
298301
}
299302

300-
301-
uint32_t* observers = env->performance_state()->observers;
303+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
304+
env->performance_state()->observers;
302305
if (!observers[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION])
303306
return;
304307

@@ -331,16 +334,12 @@ void Init(Local<Object> target,
331334
performance_state* state = env->performance_state();
332335
auto state_ab = ArrayBuffer::New(isolate, state, sizeof(*state));
333336

334-
#define SET_STATE_TYPEDARRAY(name, type, field) \
335-
target->Set(context, \
336-
FIXED_ONE_BYTE_STRING(isolate, (name)), \
337-
type::New(state_ab, \
338-
offsetof(performance_state, field), \
339-
arraysize(state->field))) \
340-
.FromJust()
341-
SET_STATE_TYPEDARRAY("observerCounts", v8::Uint32Array, observers);
342-
SET_STATE_TYPEDARRAY("milestones", v8::Float64Array, milestones);
343-
#undef SET_STATE_TYPEDARRAY
337+
target->Set(context,
338+
FIXED_ONE_BYTE_STRING(isolate, "observerCounts"),
339+
state->observers.GetJSArray()).FromJust();
340+
target->Set(context,
341+
FIXED_ONE_BYTE_STRING(isolate, "milestones"),
342+
state->milestones.GetJSArray()).FromJust();
344343

345344
Local<String> performanceEntryString =
346345
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)