Merge pull request #72375 from jiebinn/shared_mutex_optimization

Optimize the mutex with shared_mutex in the memory tracker
This commit is contained in:
János Benjamin Antal 2024-11-29 13:29:13 +00:00 committed by GitHub
commit 62368fb2a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 4 deletions

View File

@ -46,7 +46,7 @@ OvercommitResult OvercommitTracker::needToStopQuery(MemoryTracker * tracker, Int
// always called with already acquired global mutex in
// ProcessListEntry::~ProcessListEntry().
DB::ProcessList::Lock global_lock(process_list->getMutex());
std::unique_lock<std::mutex> lk(overcommit_m);
std::unique_lock lk(overcommit_m);
size_t id = next_id++;
@ -115,7 +115,13 @@ void OvercommitTracker::tryContinueQueryExecutionAfterFree(Int64 amount)
if (OvercommitTrackerBlockerInThread::isBlocked())
return;
std::lock_guard guard(overcommit_m);
{
std::shared_lock read_lock(overcommit_m);
if (cancellation_state == QueryCancellationState::NONE)
return;
}
std::lock_guard lk(overcommit_m);
if (cancellation_state != QueryCancellationState::NONE)
{
freed_memory += amount;
@ -128,6 +134,12 @@ void OvercommitTracker::onQueryStop(MemoryTracker * tracker)
{
DENY_ALLOCATIONS_IN_SCOPE;
{
std::shared_lock read_lock(overcommit_m);
if (picked_tracker != tracker)
return;
}
std::lock_guard lk(overcommit_m);
if (picked_tracker == tracker)
{

View File

@ -9,6 +9,7 @@
#include <condition_variable>
#include <mutex>
#include <unordered_map>
#include <Common/SharedMutex.h>
// This struct is used for the comparison of query memory usage.
struct OvercommitRatio
@ -83,8 +84,8 @@ protected:
// This mutex is used to disallow concurrent access
// to picked_tracker and cancellation_state variables.
std::mutex overcommit_m;
std::condition_variable cv;
DB::SharedMutex overcommit_m;
std::condition_variable_any cv;
// Specifies memory tracker of the chosen to stop query.
// If soft limit is not set, all the queries which reach hard limit must stop.