Skip to content

Commit dea44b9

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

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

src/env-inl.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ inline Environment::Environment(IsolateData* isolate_data,
348348
AssignToContext(context);
349349

350350
destroy_async_id_list_.reserve(512);
351-
performance_state_ = Calloc<performance::performance_state>(1);
351+
performance_state_.reset(new performance::performance_state(isolate()));
352352
performance_state_->milestones[
353353
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT] =
354354
PERFORMANCE_NOW();
@@ -378,7 +378,6 @@ inline Environment::~Environment() {
378378
delete[] heap_statistics_buffer_;
379379
delete[] heap_space_statistics_buffer_;
380380
delete[] http_parser_buffer_;
381-
free(performance_state_);
382381
}
383382

384383
inline v8::Isolate* Environment::isolate() const {
@@ -550,7 +549,7 @@ void Environment::SetImmediate(native_immediate_callback cb, void* data) {
550549
}
551550

552551
inline performance::performance_state* Environment::performance_state() {
553-
return performance_state_;
552+
return performance_state_.get();
554553
}
555554

556555
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 {
@@ -717,7 +717,7 @@ class Environment {
717717

718718
AliasedBuffer<uint32_t, v8::Uint32Array> scheduled_immediate_count_;
719719

720-
performance::performance_state* performance_state_ = nullptr;
720+
std::unique_ptr<performance::performance_state> performance_state_;
721721
std::map<std::string, uint64_t> performance_marks_;
722722

723723
#if HAVE_INSPECTOR

src/node_perf.cc

+17-18
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ void PerformanceEntry::New(const FunctionCallbackInfo<Value>& args) {
4141

4242
void PerformanceEntry::NotifyObservers(Environment* env,
4343
PerformanceEntry* entry) {
44-
uint32_t* observers = env->performance_state()->observers;
44+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
45+
env->performance_state()->observers;
4546
PerformanceEntryType type = ToPerformanceEntryTypeEnum(entry->type().c_str());
46-
if (observers == nullptr ||
47-
type == NODE_PERFORMANCE_ENTRY_TYPE_INVALID ||
47+
if (type == NODE_PERFORMANCE_ENTRY_TYPE_INVALID ||
4848
!observers[type]) {
4949
return;
5050
}
@@ -88,7 +88,8 @@ void Measure(const FunctionCallbackInfo<Value>& args) {
8888
Utf8Value startMark(isolate, args[1]);
8989
Utf8Value endMark(isolate, args[2]);
9090

91-
double* milestones = env->performance_state()->milestones;
91+
AliasedBuffer<double, v8::Float64Array>& milestones =
92+
env->performance_state()->milestones;
9293

9394
uint64_t startTimestamp = timeOrigin;
9495
uint64_t start = GetPerformanceMark(env, *startMark);
@@ -155,7 +156,8 @@ void GetPerformanceEntryDuration(const Local<String> prop,
155156
void MarkMilestone(const FunctionCallbackInfo<Value>& args) {
156157
Environment* env = Environment::GetCurrent(args);
157158
Local<Context> context = env->context();
158-
double* milestones = env->performance_state()->milestones;
159+
AliasedBuffer<double, v8::Float64Array>& milestones =
160+
env->performance_state()->milestones;
159161
PerformanceMilestone milestone =
160162
static_cast<PerformanceMilestone>(
161163
args[0]->Int32Value(context).ToChecked());
@@ -182,7 +184,8 @@ void PerformanceGCCallback(uv_async_t* handle) {
182184
Local<Object> obj;
183185
PerformanceGCKind kind = static_cast<PerformanceGCKind>(data->data());
184186

185-
uint32_t* observers = env->performance_state()->observers;
187+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
188+
env->performance_state()->observers;
186189
if (!observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]) {
187190
goto cleanup;
188191
}
@@ -285,8 +288,8 @@ void TimerFunctionCall(const FunctionCallbackInfo<Value>& args) {
285288
args.GetReturnValue().Set(ret.ToLocalChecked());
286289
}
287290

288-
289-
uint32_t* observers = env->performance_state()->observers;
291+
AliasedBuffer<uint32_t, v8::Uint32Array>& observers =
292+
env->performance_state()->observers;
290293
if (!observers[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION])
291294
return;
292295

@@ -319,16 +322,12 @@ void Init(Local<Object> target,
319322
performance_state* state = env->performance_state();
320323
auto state_ab = ArrayBuffer::New(isolate, state, sizeof(*state));
321324

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

333332
Local<String> performanceEntryString =
334333
FIXED_ONE_BYTE_STRING(isolate, "PerformanceEntry");

src/node_perf_common.h

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

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

6992
} // namespace performance

0 commit comments

Comments
 (0)