Skip to content

Commit 5b06bbe

Browse files
committed
Expose a counter of number of live context pointers.
Also optimize away some setting of null/undefined as context
1 parent 5e2043c commit 5b06bbe

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

bindings/profilers/wall.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ class SignalMutex {
7272
inline ~SignalMutex() { store(false); }
7373
};
7474

75+
// This class is used to update the context count metric when it goes out of
76+
// scope.
77+
class ContextCountMetricUpdater {
78+
uint32_t* fields_;
79+
std::unordered_set<PersistentContextPtr*>& liveContextPtrs_;
80+
81+
public:
82+
inline ContextCountMetricUpdater(
83+
uint32_t* fields,
84+
std::unordered_set<PersistentContextPtr*>& liveContextPtrs)
85+
: fields_(fields), liveContextPtrs_(liveContextPtrs) {}
86+
87+
inline ~ContextCountMetricUpdater() {
88+
std::atomic_store_explicit(
89+
reinterpret_cast<std::atomic<uint32_t>*>(
90+
&fields_[WallProfiler::Fields::kCPEDContextCount]),
91+
liveContextPtrs_.size(),
92+
std::memory_order_relaxed);
93+
}
94+
};
95+
7596
void SetContextPtr(ContextPtr& contextPtr,
7697
Isolate* isolate,
7798
Local<Value> value) {
@@ -673,6 +694,7 @@ void WallProfiler::Dispose(Isolate* isolate, bool removeFromMap) {
673694
node::RemoveEnvironmentCleanupHook(
674695
isolate, &WallProfiler::CleanupHook, isolate);
675696

697+
ContextCountMetricUpdater updater(fields_, liveContextPtrs_);
676698
for (auto ptr : liveContextPtrs_) {
677699
ptr->UnregisterFromGC();
678700
delete ptr;
@@ -853,6 +875,7 @@ v8::ProfilerId WallProfiler::StartInternal() {
853875
if (withContexts_ || workaroundV8Bug_) {
854876
SignalHandler::IncreaseUseCount();
855877
fields_[kSampleCount] = 0;
878+
fields_[kCPEDContextCount] = 0;
856879
}
857880

858881
if (collectCpuTime_) {
@@ -1072,6 +1095,11 @@ NAN_MODULE_INIT(WallProfiler::Init) {
10721095
Nan::New<Integer>(kSampleCount),
10731096
ReadOnlyDontDelete)
10741097
.FromJust();
1098+
Nan::DefineOwnProperty(constants,
1099+
Nan::New("kCPEDContextCount").ToLocalChecked(),
1100+
Nan::New<Integer>(kCPEDContextCount),
1101+
ReadOnlyDontDelete)
1102+
.FromJust();
10751103
Nan::DefineOwnProperty(target,
10761104
Nan::New("constants").ToLocalChecked(),
10771105
constants,
@@ -1116,6 +1144,8 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
11161144
return;
11171145
}
11181146

1147+
ContextCountMetricUpdater updater(fields_, liveContextPtrs_);
1148+
11191149
// Clean up dead context pointers
11201150
for (auto ptr : deadContextPtrs_) {
11211151
liveContextPtrs_.erase(ptr);
@@ -1140,6 +1170,12 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
11401170
auto profData = maybeProfData.ToLocalChecked();
11411171
SignalMutex m(setInProgress_);
11421172
if (profData->IsUndefined()) {
1173+
if (value->IsNullOrUndefined()) {
1174+
// Don't go to the trouble of mutating the CPED for null or undefined as
1175+
// the absence of a sample context will be interpreted as undefined in
1176+
// GetContextPtr anyway.
1177+
return;
1178+
}
11431179
contextPtr = new PersistentContextPtr(&deadContextPtrs_);
11441180

11451181
auto external = External::New(isolate, contextPtr);

bindings/profilers/wall.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ class PersistentContextPtr;
4343
class WallProfiler : public Nan::ObjectWrap {
4444
public:
4545
enum class CollectionMode { kNoCollect, kPassThrough, kCollectContexts };
46+
enum Fields { kSampleCount, kCPEDContextCount, kFieldCount };
4647

4748
private:
48-
enum Fields { kSampleCount, kFieldCount };
49-
5049
std::chrono::microseconds samplingPeriod_{0};
5150
v8::CpuProfiler* cpuProfiler_ = nullptr;
5251

ts/src/time-profiler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
import {GenerateTimeLabelsFunction} from './v8-types';
3131
import {isMainThread} from 'worker_threads';
3232

33-
const {kSampleCount} = profilerConstants;
33+
const {kSampleCount, kCPEDContextCount} = profilerConstants;
3434

3535
const DEFAULT_INTERVAL_MICROS: Microseconds = 1000;
3636
const DEFAULT_DURATION_MILLIS: Milliseconds = 60000;
@@ -178,6 +178,7 @@ export function v8ProfilerStuckEventLoopDetected() {
178178

179179
export const constants = {
180180
kSampleCount,
181+
kCPEDContextCount,
181182
GARBAGE_COLLECTION_FUNCTION_NAME,
182183
NON_JS_THREADS_FUNCTION_NAME,
183184
};

0 commit comments

Comments
 (0)