Skip to content

Commit d1f6237

Browse files
legendecasFyko
authored andcommittedSep 15, 2022
src: consolidate environment cleanup queue
Each Realm tracks its own cleanup hooks and drains the hooks when it is going to be destroyed. Moves the implementations of the cleanup queue to its own class so that it can be used in `node::Realm` too. PR-URL: nodejs#44379 Refs: nodejs#44348 Refs: nodejs#42528 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 9a1d370 commit d1f6237

10 files changed

+212
-105
lines changed
 

‎node.gyp

+3
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@
466466
'src/api/utils.cc',
467467
'src/async_wrap.cc',
468468
'src/cares_wrap.cc',
469+
'src/cleanup_queue.cc',
469470
'src/connect_wrap.cc',
470471
'src/connection_wrap.cc',
471472
'src/debug_utils.cc',
@@ -567,6 +568,8 @@
567568
'src/base64-inl.h',
568569
'src/callback_queue.h',
569570
'src/callback_queue-inl.h',
571+
'src/cleanup_queue.h',
572+
'src/cleanup_queue-inl.h',
570573
'src/connect_wrap.h',
571574
'src/connection_wrap.h',
572575
'src/debug_utils.h',

‎src/base_object.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class BaseObject : public MemoryRetainer {
178178
// position of members in memory are predictable. For more information please
179179
// refer to `doc/contributing/node-postmortem-support.md`
180180
friend int GenDebugSymbols();
181-
friend class CleanupHookCallback;
181+
friend class CleanupQueue;
182182
template <typename T, bool kIsWeak>
183183
friend class BaseObjectPtrImpl;
184184

‎src/cleanup_queue-inl.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef SRC_CLEANUP_QUEUE_INL_H_
2+
#define SRC_CLEANUP_QUEUE_INL_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "base_object.h"
7+
#include "cleanup_queue.h"
8+
#include "memory_tracker-inl.h"
9+
#include "util.h"
10+
11+
namespace node {
12+
13+
inline void CleanupQueue::MemoryInfo(MemoryTracker* tracker) const {
14+
ForEachBaseObject([&](BaseObject* obj) {
15+
if (obj->IsDoneInitializing()) tracker->Track(obj);
16+
});
17+
}
18+
19+
inline size_t CleanupQueue::SelfSize() const {
20+
return sizeof(CleanupQueue) +
21+
cleanup_hooks_.size() * sizeof(CleanupHookCallback);
22+
}
23+
24+
bool CleanupQueue::empty() const {
25+
return cleanup_hooks_.empty();
26+
}
27+
28+
void CleanupQueue::Add(Callback cb, void* arg) {
29+
auto insertion_info = cleanup_hooks_.emplace(
30+
CleanupHookCallback{cb, arg, cleanup_hook_counter_++});
31+
// Make sure there was no existing element with these values.
32+
CHECK_EQ(insertion_info.second, true);
33+
}
34+
35+
void CleanupQueue::Remove(Callback cb, void* arg) {
36+
CleanupHookCallback search{cb, arg, 0};
37+
cleanup_hooks_.erase(search);
38+
}
39+
40+
template <typename T>
41+
void CleanupQueue::ForEachBaseObject(T&& iterator) const {
42+
for (const auto& hook : cleanup_hooks_) {
43+
BaseObject* obj = GetBaseObject(hook);
44+
if (obj != nullptr) iterator(obj);
45+
}
46+
}
47+
48+
BaseObject* CleanupQueue::GetBaseObject(
49+
const CleanupHookCallback& callback) const {
50+
if (callback.fn_ == BaseObject::DeleteMe)
51+
return static_cast<BaseObject*>(callback.arg_);
52+
else
53+
return nullptr;
54+
}
55+
56+
} // namespace node
57+
58+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
59+
60+
#endif // SRC_CLEANUP_QUEUE_INL_H_

‎src/cleanup_queue.cc

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "cleanup_queue.h" // NOLINT(build/include_inline)
2+
#include <vector>
3+
#include "cleanup_queue-inl.h"
4+
5+
namespace node {
6+
7+
void CleanupQueue::Drain() {
8+
// Copy into a vector, since we can't sort an unordered_set in-place.
9+
std::vector<CleanupHookCallback> callbacks(cleanup_hooks_.begin(),
10+
cleanup_hooks_.end());
11+
// We can't erase the copied elements from `cleanup_hooks_` yet, because we
12+
// need to be able to check whether they were un-scheduled by another hook.
13+
14+
std::sort(callbacks.begin(),
15+
callbacks.end(),
16+
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
17+
// Sort in descending order so that the most recently inserted
18+
// callbacks are run first.
19+
return a.insertion_order_counter_ > b.insertion_order_counter_;
20+
});
21+
22+
for (const CleanupHookCallback& cb : callbacks) {
23+
if (cleanup_hooks_.count(cb) == 0) {
24+
// This hook was removed from the `cleanup_hooks_` set during another
25+
// hook that was run earlier. Nothing to do here.
26+
continue;
27+
}
28+
29+
cb.fn_(cb.arg_);
30+
cleanup_hooks_.erase(cb);
31+
}
32+
}
33+
34+
size_t CleanupQueue::CleanupHookCallback::Hash::operator()(
35+
const CleanupHookCallback& cb) const {
36+
return std::hash<void*>()(cb.arg_);
37+
}
38+
39+
bool CleanupQueue::CleanupHookCallback::Equal::operator()(
40+
const CleanupHookCallback& a, const CleanupHookCallback& b) const {
41+
return a.fn_ == b.fn_ && a.arg_ == b.arg_;
42+
}
43+
44+
} // namespace node

‎src/cleanup_queue.h

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#ifndef SRC_CLEANUP_QUEUE_H_
2+
#define SRC_CLEANUP_QUEUE_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include <cstddef>
7+
#include <cstdint>
8+
#include <unordered_set>
9+
10+
#include "memory_tracker.h"
11+
12+
namespace node {
13+
14+
class BaseObject;
15+
16+
class CleanupQueue : public MemoryRetainer {
17+
public:
18+
typedef void (*Callback)(void*);
19+
20+
CleanupQueue() {}
21+
22+
// Not copyable.
23+
CleanupQueue(const CleanupQueue&) = delete;
24+
25+
SET_MEMORY_INFO_NAME(CleanupQueue)
26+
inline void MemoryInfo(node::MemoryTracker* tracker) const override;
27+
inline size_t SelfSize() const override;
28+
29+
inline bool empty() const;
30+
31+
inline void Add(Callback cb, void* arg);
32+
inline void Remove(Callback cb, void* arg);
33+
void Drain();
34+
35+
template <typename T>
36+
inline void ForEachBaseObject(T&& iterator) const;
37+
38+
private:
39+
class CleanupHookCallback {
40+
public:
41+
CleanupHookCallback(Callback fn,
42+
void* arg,
43+
uint64_t insertion_order_counter)
44+
: fn_(fn),
45+
arg_(arg),
46+
insertion_order_counter_(insertion_order_counter) {}
47+
48+
// Only hashes `arg_`, since that is usually enough to identify the hook.
49+
struct Hash {
50+
size_t operator()(const CleanupHookCallback& cb) const;
51+
};
52+
53+
// Compares by `fn_` and `arg_` being equal.
54+
struct Equal {
55+
bool operator()(const CleanupHookCallback& a,
56+
const CleanupHookCallback& b) const;
57+
};
58+
59+
private:
60+
friend class CleanupQueue;
61+
Callback fn_;
62+
void* arg_;
63+
64+
// We keep track of the insertion order for these objects, so that we can
65+
// call the callbacks in reverse order when we are cleaning up.
66+
uint64_t insertion_order_counter_;
67+
};
68+
69+
inline BaseObject* GetBaseObject(const CleanupHookCallback& callback) const;
70+
71+
// Use an unordered_set, so that we have efficient insertion and removal.
72+
std::unordered_set<CleanupHookCallback,
73+
CleanupHookCallback::Hash,
74+
CleanupHookCallback::Equal>
75+
cleanup_hooks_;
76+
uint64_t cleanup_hook_counter_ = 0;
77+
};
78+
79+
} // namespace node
80+
81+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
82+
83+
#endif // SRC_CLEANUP_QUEUE_H_

‎src/env-inl.h

+5-31
Original file line numberDiff line numberDiff line change
@@ -781,43 +781,17 @@ inline void Environment::ThrowUVException(int errorno,
781781
UVException(isolate(), errorno, syscall, message, path, dest));
782782
}
783783

784-
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
785-
auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
786-
fn, arg, cleanup_hook_counter_++
787-
});
788-
// Make sure there was no existing element with these values.
789-
CHECK_EQ(insertion_info.second, true);
790-
}
791-
792-
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
793-
CleanupHookCallback search { fn, arg, 0 };
794-
cleanup_hooks_.erase(search);
795-
}
796-
797-
size_t CleanupHookCallback::Hash::operator()(
798-
const CleanupHookCallback& cb) const {
799-
return std::hash<void*>()(cb.arg_);
784+
void Environment::AddCleanupHook(CleanupQueue::Callback fn, void* arg) {
785+
cleanup_queue_.Add(fn, arg);
800786
}
801787

802-
bool CleanupHookCallback::Equal::operator()(
803-
const CleanupHookCallback& a, const CleanupHookCallback& b) const {
804-
return a.fn_ == b.fn_ && a.arg_ == b.arg_;
805-
}
806-
807-
BaseObject* CleanupHookCallback::GetBaseObject() const {
808-
if (fn_ == BaseObject::DeleteMe)
809-
return static_cast<BaseObject*>(arg_);
810-
else
811-
return nullptr;
788+
void Environment::RemoveCleanupHook(CleanupQueue::Callback fn, void* arg) {
789+
cleanup_queue_.Remove(fn, arg);
812790
}
813791

814792
template <typename T>
815793
void Environment::ForEachBaseObject(T&& iterator) {
816-
for (const auto& hook : cleanup_hooks_) {
817-
BaseObject* obj = hook.GetBaseObject();
818-
if (obj != nullptr)
819-
iterator(obj);
820-
}
794+
cleanup_queue_.ForEachBaseObject(std::forward<T>(iterator));
821795
}
822796

823797
void Environment::modify_base_object_count(int64_t delta) {

‎src/env.cc

+4-31
Original file line numberDiff line numberDiff line change
@@ -1000,33 +1000,10 @@ void Environment::RunCleanup() {
10001000
bindings_.clear();
10011001
CleanupHandles();
10021002

1003-
while (!cleanup_hooks_.empty() ||
1004-
native_immediates_.size() > 0 ||
1003+
while (!cleanup_queue_.empty() || native_immediates_.size() > 0 ||
10051004
native_immediates_threadsafe_.size() > 0 ||
10061005
native_immediates_interrupts_.size() > 0) {
1007-
// Copy into a vector, since we can't sort an unordered_set in-place.
1008-
std::vector<CleanupHookCallback> callbacks(
1009-
cleanup_hooks_.begin(), cleanup_hooks_.end());
1010-
// We can't erase the copied elements from `cleanup_hooks_` yet, because we
1011-
// need to be able to check whether they were un-scheduled by another hook.
1012-
1013-
std::sort(callbacks.begin(), callbacks.end(),
1014-
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
1015-
// Sort in descending order so that the most recently inserted callbacks
1016-
// are run first.
1017-
return a.insertion_order_counter_ > b.insertion_order_counter_;
1018-
});
1019-
1020-
for (const CleanupHookCallback& cb : callbacks) {
1021-
if (cleanup_hooks_.count(cb) == 0) {
1022-
// This hook was removed from the `cleanup_hooks_` set during another
1023-
// hook that was run earlier. Nothing to do here.
1024-
continue;
1025-
}
1026-
1027-
cb.fn_(cb.arg_);
1028-
cleanup_hooks_.erase(cb);
1029-
}
1006+
cleanup_queue_.Drain();
10301007
CleanupHandles();
10311008
}
10321009

@@ -1730,10 +1707,6 @@ void Environment::BuildEmbedderGraph(Isolate* isolate,
17301707
MemoryTracker tracker(isolate, graph);
17311708
Environment* env = static_cast<Environment*>(data);
17321709
tracker.Track(env);
1733-
env->ForEachBaseObject([&](BaseObject* obj) {
1734-
if (obj->IsDoneInitializing())
1735-
tracker.Track(obj);
1736-
});
17371710
}
17381711

17391712
size_t Environment::NearHeapLimitCallback(void* data,
@@ -1868,6 +1841,7 @@ inline size_t Environment::SelfSize() const {
18681841
// this can be done for common types within the Track* calls automatically
18691842
// if a certain scope is entered.
18701843
size -= sizeof(async_hooks_);
1844+
size -= sizeof(cleanup_queue_);
18711845
size -= sizeof(tick_info_);
18721846
size -= sizeof(immediate_info_);
18731847
return size;
@@ -1885,8 +1859,7 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
18851859
tracker->TrackField("should_abort_on_uncaught_toggle",
18861860
should_abort_on_uncaught_toggle_);
18871861
tracker->TrackField("stream_base_state", stream_base_state_);
1888-
tracker->TrackFieldWithSize(
1889-
"cleanup_hooks", cleanup_hooks_.size() * sizeof(CleanupHookCallback));
1862+
tracker->TrackField("cleanup_queue", cleanup_queue_);
18901863
tracker->TrackField("async_hooks", async_hooks_);
18911864
tracker->TrackField("immediate_info", immediate_info_);
18921865
tracker->TrackField("tick_info", tick_info_);

0 commit comments

Comments
 (0)
Please sign in to comment.