Skip to content

Commit 34b19c4

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

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_) {
@@ -1073,6 +1096,11 @@ NAN_MODULE_INIT(WallProfiler::Init) {
10731096
Nan::New<Integer>(kSampleCount),
10741097
ReadOnlyDontDelete)
10751098
.FromJust();
1099+
Nan::DefineOwnProperty(constants,
1100+
Nan::New("kCPEDContextCount").ToLocalChecked(),
1101+
Nan::New<Integer>(kCPEDContextCount),
1102+
ReadOnlyDontDelete)
1103+
.FromJust();
10761104
Nan::DefineOwnProperty(target,
10771105
Nan::New("constants").ToLocalChecked(),
10781106
constants,
@@ -1117,6 +1145,8 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
11171145
return;
11181146
}
11191147

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

11461182
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)