mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-04 21:42:39 +00:00
Merge pull request #72375 from jiebinn/shared_mutex_optimization
Optimize the mutex with shared_mutex in the memory tracker
This commit is contained in:
commit
62368fb2a3
@ -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)
|
||||
{
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user