Skip to content

Commit 604518f

Browse files
committed
Review feedback:
- SignalMutex -> SignalGuard - Use defer instead of ContextCountMetricUpdater - make PersistentContextPtr methods public instead of declaring a friend class
1 parent 5b06bbe commit 604518f

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

bindings/profilers/wall.cc

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <type_traits>
2626
#include <vector>
2727

28+
#include "defer.hh"
2829
#include "per-isolate-data.hh"
2930
#include "translate-time-profile.hh"
3031
#include "wall.hh"
@@ -58,39 +59,18 @@ using namespace v8;
5859

5960
namespace dd {
6061

61-
class SignalMutex {
62-
std::atomic<bool>& mutex_;
62+
class SignalGuard {
63+
std::atomic<bool>& guard_;
6364

6465
inline void store(bool value) {
6566
std::atomic_signal_fence(std::memory_order_release);
66-
mutex_.store(value, std::memory_order_relaxed);
67+
guard_.store(value, std::memory_order_relaxed);
6768
}
6869

6970
public:
70-
inline SignalMutex(std::atomic<bool>& mutex) : mutex_(mutex) { store(true); }
71+
inline SignalGuard(std::atomic<bool>& guard) : guard_(guard) { store(true); }
7172

72-
inline ~SignalMutex() { store(false); }
73-
};
74-
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-
}
73+
inline ~SignalGuard() { store(false); }
9474
};
9575

9676
void SetContextPtr(ContextPtr& contextPtr,
@@ -108,6 +88,7 @@ class PersistentContextPtr {
10888
std::vector<PersistentContextPtr*>* dead;
10989
Persistent<Object> per;
11090

91+
public:
11192
PersistentContextPtr(std::vector<PersistentContextPtr*>* dead) : dead(dead) {}
11293

11394
void UnregisterFromGC() {
@@ -137,8 +118,6 @@ class PersistentContextPtr {
137118
}
138119

139120
ContextPtr Get() const { return context; }
140-
141-
friend class WallProfiler;
142121
};
143122

144123
// Maximum number of rounds in the GetV8ToEpochOffset
@@ -677,6 +656,14 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
677656
}
678657
}
679658

659+
void WallProfiler::UpdateContextCount() {
660+
std::atomic_store_explicit(
661+
reinterpret_cast<std::atomic<uint32_t>*>(
662+
&fields_[WallProfiler::Fields::kCPEDContextCount]),
663+
liveContextPtrs_.size(),
664+
std::memory_order_relaxed);
665+
}
666+
680667
void WallProfiler::Dispose(Isolate* isolate, bool removeFromMap) {
681668
if (cpuProfiler_ != nullptr) {
682669
cpuProfiler_->Dispose();
@@ -694,13 +681,13 @@ void WallProfiler::Dispose(Isolate* isolate, bool removeFromMap) {
694681
node::RemoveEnvironmentCleanupHook(
695682
isolate, &WallProfiler::CleanupHook, isolate);
696683

697-
ContextCountMetricUpdater updater(fields_, liveContextPtrs_);
698684
for (auto ptr : liveContextPtrs_) {
699685
ptr->UnregisterFromGC();
700686
delete ptr;
701687
}
702688
liveContextPtrs_.clear();
703689
deadContextPtrs_.clear();
690+
UpdateContextCount();
704691
}
705692
}
706693

@@ -1133,7 +1120,7 @@ Local<Value> WallProfiler::GetContext(Isolate* isolate) {
11331120
}
11341121

11351122
void WallProfiler::SetCurrentContextPtr(Isolate* isolate, Local<Value> value) {
1136-
SignalMutex m(setInProgress_);
1123+
SignalGuard m(setInProgress_);
11371124
SetContextPtr(curContext_, isolate, value);
11381125
}
11391126

@@ -1144,7 +1131,9 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
11441131
return;
11451132
}
11461133

1147-
ContextCountMetricUpdater updater(fields_, liveContextPtrs_);
1134+
defer {
1135+
UpdateContextCount();
1136+
};
11481137

11491138
// Clean up dead context pointers
11501139
for (auto ptr : deadContextPtrs_) {
@@ -1168,7 +1157,7 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
11681157

11691158
PersistentContextPtr* contextPtr = nullptr;
11701159
auto profData = maybeProfData.ToLocalChecked();
1171-
SignalMutex m(setInProgress_);
1160+
SignalGuard m(setInProgress_);
11721161
if (profData->IsUndefined()) {
11731162
if (value->IsNullOrUndefined()) {
11741163
// Don't go to the trouble of mutating the CPED for null or undefined as

bindings/profilers/wall.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class WallProfiler : public Nan::ObjectWrap {
118118
ContextPtr GetContextPtrSignalSafe(v8::Isolate* isolate);
119119

120120
void SetCurrentContextPtr(v8::Isolate* isolate, v8::Local<v8::Value> context);
121+
void UpdateContextCount();
121122

122123
public:
123124
/**

0 commit comments

Comments
 (0)