-
Notifications
You must be signed in to change notification settings - Fork 9
Release query-engine memory back to the OS
#1171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f13f9e
Return memory back to OS
ccgsnet 20b48b7
Revert run_toy.sh
ccgsnet 57b1b4c
CodeRabbit changes
ccgsnet 667c39d
CodeRabbit changes
ccgsnet 5d3efc6
Merge branch 'master' into malloc_trim
ccgsnet c9b8ac4
Add lock_guard to ThreadSafeHeap::snapshot()
ccgsnet f64566e
CodeRabbit changes
ccgsnet a02a465
get_id() before detach()
ccgsnet 25ef22c
Merge branch 'master' into malloc_trim
ccgsnet 38628a2
Merge branch 'master' into malloc_trim
ccgsnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| #include <map> | ||
| #include <memory> | ||
| #include <set> | ||
| #include <thread> | ||
|
|
||
| #include "BusCommandProcessor.h" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| #include <map> | ||
| #include <memory> | ||
| #include <set> | ||
| #include <stack> | ||
| #include <thread> | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #include <atomic> | ||
| #include <map> | ||
| #include <mutex> | ||
| #include <string> | ||
| #include <thread> | ||
|
|
||
| #include "StoppableThread.h" | ||
| #include "Utils.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using namespace std; | ||
| using namespace commons; | ||
|
|
||
| // Reproduces the self-reaping pattern used by PatternMatchingQueryProcessor and | ||
| // QueryEvolutionProcessor: a pool of worker threads where each worker, when it finishes, detaches | ||
| // itself and removes its own entry from a shared map. This is the "burst-then-idle" case: after a | ||
| // burst of workers completes, the map must drain to empty WITHOUT any external/non-worker reaping | ||
| // trigger. | ||
| TEST(StoppableThread, self_reap_burst_then_idle) { | ||
| map<string, shared_ptr<StoppableThread>> threads; | ||
| mutex threads_mutex; | ||
| atomic<int> completed{0}; | ||
|
|
||
| const int burst = 16; | ||
| for (int i = 0; i < burst; i++) { | ||
| string id = "worker_" + std::to_string(i); | ||
| lock_guard<mutex> guard(threads_mutex); | ||
| auto stoppable_thread = make_shared<StoppableThread>(id); | ||
| stoppable_thread->attach(new thread( | ||
| [&threads, &threads_mutex, &completed](shared_ptr<StoppableThread> monitor) { | ||
| Utils::sleep(10); // simulate a short unit of work | ||
| completed++; | ||
| // Self-reap (mirrors the processors): a thread can't join itself, so detach and | ||
| // drop its own entry. completed++ happens before the erase, so once the map is | ||
| // empty every worker is guaranteed to have finished its work. | ||
| lock_guard<mutex> guard(threads_mutex); | ||
| monitor->detach(); | ||
| threads.erase(monitor->get_id()); | ||
| }, | ||
| stoppable_thread)); | ||
| threads[id] = stoppable_thread; | ||
| } | ||
|
|
||
| // Bounded wait for the burst to drain, issuing no further "commands". | ||
| for (int i = 0; i < 500; i++) { | ||
| { | ||
| lock_guard<mutex> guard(threads_mutex); | ||
| if (threads.empty()) { | ||
| break; | ||
| } | ||
| } | ||
| Utils::sleep(10); | ||
| } | ||
|
|
||
| lock_guard<mutex> guard(threads_mutex); | ||
| EXPECT_EQ(completed.load(), burst); | ||
| EXPECT_TRUE(threads.empty()); | ||
| } | ||
|
|
||
| // detach() must make the thread non-joinable so that a subsequent stop() (e.g. from the | ||
| // StoppableThread destructor) is a safe no-op rather than a join-on-detached error. | ||
| TEST(StoppableThread, stop_after_detach_is_noop) { | ||
| auto stoppable_thread = make_shared<StoppableThread>("detach_then_stop"); | ||
| atomic<bool> ran{false}; | ||
| stoppable_thread->attach(new thread( | ||
| [&ran](shared_ptr<StoppableThread> monitor) { | ||
| ran = true; | ||
| while (!monitor->stopped()) { | ||
| Utils::sleep(5); | ||
| } | ||
| }, | ||
| stoppable_thread)); | ||
|
|
||
| while (!ran.load()) { | ||
| Utils::sleep(5); | ||
| } | ||
| stoppable_thread->detach(); // releases the OS thread; also flips the stop flag | ||
| EXPECT_TRUE(stoppable_thread->stopped()); | ||
| EXPECT_NO_THROW(stoppable_thread->stop()); // must not join/double-free a detached thread | ||
| } | ||
|
|
||
| // stop_flag means "stop requested", not "thread reaped". stop(false) only requests the stop; a later | ||
| // stop() must still join/reap the (still joinable) thread instead of being skipped because the flag | ||
| // is already set. This covers the "request-stop-now, reap-later" ordering. | ||
| TEST(StoppableThread, stop_false_then_stop_reaps) { | ||
| auto stoppable_thread = make_shared<StoppableThread>("request_then_reap"); | ||
| atomic<bool> ran{false}; | ||
| atomic<bool> exited{false}; | ||
| stoppable_thread->attach(new thread( | ||
| [&ran, &exited](shared_ptr<StoppableThread> monitor) { | ||
| ran = true; | ||
| while (!monitor->stopped()) { | ||
| Utils::sleep(5); | ||
| } | ||
| exited = true; | ||
| }, | ||
| stoppable_thread)); | ||
|
|
||
| while (!ran.load()) { | ||
| Utils::sleep(5); | ||
| } | ||
|
|
||
| stoppable_thread->stop(false); // request stop only: flag is set, but nothing is reaped yet | ||
| EXPECT_TRUE(stoppable_thread->stopped()); | ||
|
|
||
| // The worker observes the request and exits on its own. | ||
| for (int i = 0; i < 200 && !exited.load(); i++) { | ||
| Utils::sleep(5); | ||
| } | ||
| EXPECT_TRUE(exited.load()); | ||
|
|
||
| // The thread is still joinable; the full stop() must reap it (and the destructor must stay a | ||
| // safe no-op afterwards). If stop_flag short-circuited reaping, this would leak the thread. | ||
| EXPECT_NO_THROW(stoppable_thread->stop()); | ||
| EXPECT_NO_THROW(stoppable_thread->stop()); // idempotent: second reap is a no-op | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.