16 #ifndef IMPALA_RUNTIME_MEM_TRACKER_H
17 #define IMPALA_RUNTIME_MEM_TRACKER_H
22 #include <boost/shared_ptr.hpp>
23 #include <boost/weak_ptr.hpp>
24 #include <boost/thread/mutex.hpp>
25 #include <boost/unordered_map.hpp>
35 #include "gen-cpp/Types_types.h"
40 class QueryResourceMgr;
67 MemTracker(int64_t byte_limit = -1, int64_t rm_reserved_limit = -1,
69 bool log_usage_if_zero =
true);
79 int64_t byte_limit = -1, int64_t rm_reserved_limit = -1,
80 const std::string&
label = std::string());
129 if (bytes == 0)
return;
133 (*tracker)->consumption_->Add(bytes);
134 if ((*tracker)->consumption_metric_ == NULL) {
135 DCHECK_GE((*tracker)->consumption_->current_value(), 0);
165 if (bytes <= 0)
return true;
191 if (i == -1)
return true;
224 if (bytes == 0)
return;
228 (*tracker)->consumption_->Add(-bytes);
235 if ((*tracker)->consumption_metric_ == NULL) {
236 DCHECK_GE((*tracker)->consumption_->current_value(), 0)
237 << std::endl << (*tracker)->LogUsage();
249 if ((*tracker)->LimitExceeded())
return true;
271 int64_t result = std::numeric_limits<int64_t>::max();
274 int64_t mem_left = (*tracker)->limit() - (*tracker)->consumption();
275 result = std::min(result, mem_left);
289 int64_t v = std::numeric_limits<int64_t>::max();
320 std::string
LogUsage(
const std::string& prefix =
"")
const;
335 bool GcMemory(int64_t max_consumption);
356 void LogUpdate(
bool is_consume, int64_t bytes)
const;
358 static std::string
LogUsage(
const std::string& prefix,
359 const std::list<MemTracker*>& trackers);
387 typedef boost::unordered_map<TUniqueId, boost::weak_ptr<MemTracker> >
boost::unordered_map< std::string, MemTracker * > PoolTrackersMap
T UpdateAndFetch(T delta)
Increments by delta (i.e. += delta) and returns the new val.
virtual int64_t value() const
static boost::shared_ptr< MemTracker > GetQueryMemTracker(const TUniqueId &id, int64_t byte_limit, int64_t rm_reserved_limit, MemTracker *parent, QueryResourceMgr *res_mgr)
MemTracker(int64_t byte_limit=-1, int64_t rm_reserved_limit=-1, const std::string &label=std::string(), MemTracker *parent=NULL, bool log_usage_if_zero=true)
UIntGauge * consumption_metric_
int64_t effective_limit() const
Returns the minimum of limit and rm_reserved_limit.
int64_t current_value() const
int64_t consumption() const
Returns the memory consumed in bytes.
std::string pool_name_
Only valid for MemTrackers returned from GetRequestPoolMemTracker()
void UnregisterFromParent()
Removes this tracker from parent_->child_trackers_.
const std::string & label() const
void EnableLogging(bool enable, bool log_stack)
static boost::mutex static_mem_trackers_lock_
Protects request_to_mem_trackers_ and pool_to_mem_trackers_.
int64_t rm_reserved_limit_
boost::mutex resource_acquisition_lock_
bool TryConsume(int64_t bytes)
bool CheckLimitExceeded() const
class SimpleMetric< int64_t, TMetricKind::COUNTER > IntCounter
SpinLock gc_lock_
Lock to protect GcMemory(). This prevents many GCs from occurring at once.
static RequestTrackersMap request_to_mem_trackers_
MetricGroups may be organised hierarchically as a tree.
void ConsumeLocal(int64_t bytes, MemTracker *end_tracker)
TUniqueId query_id_
Only valid for MemTrackers returned from GetQueryMemTracker()
std::list< MemTracker * > child_trackers_
bool log_stack_
If true, log the stack as well.
std::vector< MemTracker * > all_trackers_
int64_t SpareCapacity() const
bool GcMemory(int64_t max_consumption)
RuntimeProfile::HighWaterMarkCounter * consumption_
in bytes; not owned
bool TryAdd(int64_t delta, int64_t max)
virtual void Add(int64_t delta)
void AddGcFunction(GcFunction f)
bool ExpandRmReservation(int64_t bytes)
void AddChildTracker(MemTracker *tracker)
Adds tracker to child_trackers_.
int64_t peak_consumption() const
IntGauge * bytes_freed_by_last_gc_metric_
boost::mutex child_trackers_lock_
static AtomicInt< int64_t > released_memory_since_gc_
bool enable_logging_
If true, logs to INFO every consume/release called. Used for debugging.
std::list< MemTracker * >::iterator child_tracker_it_
int64_t lowest_limit() const
IntGauge * bytes_over_limit_metric_
This class is thread-safe.
static const int64_t GC_RELEASE_SIZE
void Release(int64_t bytes)
Decreases consumption of this tracker and its ancestors by 'bytes'.
boost::unordered_map< TUniqueId, boost::weak_ptr< MemTracker > > RequestTrackersMap
IntCounter * num_gcs_metric_
The number of times the GcFunctions were called.
RuntimeProfile::HighWaterMarkCounter local_counter_
holds consumption_ counter if not tied to a profile
void RegisterMetrics(MetricGroup *metrics, const std::string &prefix)
static MemTracker * GetRequestPoolMemTracker(const std::string &pool_name, MemTracker *parent)
std::vector< MemTracker * > limit_trackers_
static const std::string COUNTER_NAME
std::vector< GcFunction > gc_functions_
Functions to call after the limit is reached to free memory.
static PoolTrackersMap pool_to_mem_trackers_
void LogUpdate(bool is_consume, int64_t bytes) const
Logs the stack of the current consume/release. Used for debugging only.
boost::function< void()> GcFunction
Signature for function that can be called to free some memory after limit is reached.
void SetQueryResourceMgr(QueryResourceMgr *context)
Set the resource mgr to allow expansion of limits (if NULL, no expansion is possible) ...
virtual void Set(int64_t v)
std::string LogUsage(const std::string &prefix="") const
Logs the usage of this tracker and all of its children (recursively).
void Consume(int64_t bytes)
Increases consumption of this tracker and its ancestors by 'bytes'.
QueryResourceMgr * query_resource_mgr_
Only CPU-heavy threads need be managed using this class.
void ReleaseLocal(int64_t bytes, MemTracker *end_tracker)
MemTracker * parent() const