16 #ifndef IMPALA_UTIL_RUNTIME_PROFILE_H
17 #define IMPALA_UTIL_RUNTIME_PROFILE_H
19 #include <boost/function.hpp>
20 #include <boost/scoped_ptr.hpp>
21 #include <boost/thread/mutex.hpp>
22 #include <boost/unordered_map.hpp>
25 #include <sys/resource.h>
33 #include "gen-cpp/RuntimeProfile_types.h"
40 #define ENABLE_COUNTERS 1
43 #define CONCAT_IMPL(x, y) x##y
44 #define MACRO_CONCAT(x, y) CONCAT_IMPL(x, y)
47 #define ADD_COUNTER(profile, name, unit) (profile)->AddCounter(name, unit)
48 #define ADD_TIME_SERIES_COUNTER(profile, name, src_counter) \
49 (profile)->AddTimeSeriesCounter(name, src_counter)
50 #define ADD_TIMER(profile, name) (profile)->AddCounter(name, TUnit::TIME_NS)
51 #define ADD_CHILD_TIMER(profile, name, parent) \
52 (profile)->AddCounter(name, TUnit::TIME_NS, parent)
53 #define SCOPED_TIMER(c) \
54 ScopedTimer<MonotonicStopWatch> MACRO_CONCAT(SCOPED_TIMER, __COUNTER__)(c)
55 #define COUNTER_ADD(c, v) (c)->Add(v)
56 #define COUNTER_SET(c, v) (c)->Set(v)
57 #define ADD_THREAD_COUNTERS(profile, prefix) (profile)->AddThreadCounters(prefix)
58 #define SCOPED_THREAD_COUNTER_MEASUREMENT(c) \
59 ThreadCounterMeasurement \
60 MACRO_CONCAT(SCOPED_THREAD_COUNTER_MEASUREMENT, __COUNTER__)(c)
62 #define ADD_COUNTER(profile, name, unit) NULL
63 #define ADD_TIME_SERIES_COUNTER(profile, name, src_counter) NULL
64 #define ADD_TIMER(profile, name) NULL
65 #define ADD_CHILD_TIMER(profile, name, parent) NULL
66 #define SCOPED_TIMER(c)
67 #define COUNTER_ADD(c, v)
68 #define COUNTER_SET(c, v)
69 #define ADD_THREAD_COUNTERS(profile, prefix) NULL
70 #define SCOPED_THREAD_COUNTER_MEASUREMENT(c)
93 virtual void Add(int64_t delta) {
111 return *
reinterpret_cast<const double*
>(&
value_);
129 virtual void Add(int64_t delta) {
136 bool TryAdd(int64_t delta, int64_t max) {
139 int64_t new_val = old_val + delta;
140 if (new_val > max)
return false;
148 virtual void Set(int64_t v) {
197 boost::unordered_map<Counter*, int64_t>::iterator it =
201 old_val = it->second;
202 it->second = new_counter->
value_;
207 if (
unit_ == TUnit::DOUBLE_VALUE) {
208 double old_double_val = *
reinterpret_cast<double*
>(&old_val);
211 value_ = *
reinterpret_cast<int64_t*
>(&result_val);
228 virtual void Add(int64_t delta) {
273 const std::vector<std::string>& labels) {
274 DCHECK(timestamps.size() == labels.size());
275 for (
int i = 0; i < timestamps.size(); ++i) {
276 events_.push_back(make_pair(labels[i], timestamps[i]));
287 boost::lock_guard<boost::mutex> event_lock(
lock_);
294 typedef std::pair<std::string, int64_t>
Event;
303 boost::lock_guard<boost::mutex> event_lock(
lock_);
307 void ToThrift(TEventSequence* seq)
const;
341 const std::vector<int64_t>& values)
345 void ToThrift(TTimeSeriesCounter* counter);
359 bool is_averaged_profile =
false);
365 const TRuntimeProfileTree& profiles);
379 template <
class Compare>
398 void Update(
const TRuntimeProfileTree& thrift_profile);
406 const std::string& parent_counter_name =
"");
411 TUnit::type unit,
const std::string& parent_counter_name =
"");
420 const std::string& parent_counter_name =
"");
432 void GetCounters(
const std::string&
name, std::vector<Counter*>* counters);
436 void AddInfoString(
const std::string& key,
const std::string& value);
443 EventSequence*
AddEventSequence(
const std::string& key,
const TEventSequence& from);
450 const std::string*
GetInfoString(
const std::string& key)
const;
461 void PrettyPrint(std::ostream* s,
const std::string& prefix=
"")
const;
465 void ToThrift(TRuntimeProfileTree* tree)
const;
466 void ToThrift(std::vector<TRuntimeProfileNode>* nodes)
const;
478 void GetChildren(std::vector<RuntimeProfile*>* children);
498 const Counter* total_counter,
const Counter* timer);
501 static int64_t
CounterSum(
const std::vector<Counter*>* counters);
584 typedef std::map<std::string, RuntimeProfile*>
ChildMap;
587 typedef std::vector<std::pair<RuntimeProfile*, bool> >
ChildVector;
630 void Update(
const std::vector<TRuntimeProfileNode>& nodes,
int*
idx);
645 const std::vector<TRuntimeProfileNode>& nodes,
int* node_idx);
649 const std::string& counter_name,
const CounterMap& counter_map,
683 if (counter == NULL)
return;
709 if (counter == NULL)
return;
710 DCHECK(counter->
unit() == TUnit::TIME_NS);
751 DCHECK(counters != NULL);
763 int ret = getrusage(RUSAGE_THREAD, &usage);
766 (usage.ru_utime.tv_sec -
usage_base_.ru_utime.tv_sec) * 1000L * 1000L * 1000L +
767 (usage.ru_utime.tv_usec -
usage_base_.ru_utime.tv_usec) * 1000L;
769 (usage.ru_stime.tv_sec -
usage_base_.ru_stime.tv_sec) * 1000L * 1000L * 1000L +
770 (usage.ru_stime.tv_usec -
usage_base_.ru_stime.tv_usec) * 1000L;
T UpdateAndFetch(T delta)
Increments by delta (i.e. += delta) and returns the new val.
Counter * AddCounter(const std::string &name, TUnit::type unit, const std::string &parent_counter_name="")
boost::unordered_map< Counter *, int64_t > counter_value_map_
Map from counters to their existing values. Modified via UpdateCounter().
virtual int64_t value() const
void GetEvents(std::vector< Event > *events)
DerivedCounter * AddDerivedCounter(const std::string &name, TUnit::type unit, const DerivedCounterFunction &counter_fn, const std::string &parent_counter_name="")
void AddSample(int ms_elapsed)
EventSequence * AddEventSequence(const std::string &key)
ChildCounterMap child_counter_map_
DerivedCounter(TUnit::type unit, const DerivedCounterFunction &counter_fn)
void set_metadata(int64_t md)
DerivedCounterFunction sample_fn_
TimeSeriesCounter(const std::string &name, TUnit::type unit, DerivedCounterFunction fn)
int64_t current_value() const
A set of counters that measure thread info, such as total time, user time, sys time.
void SortChildren(const Compare &cmp)
ScopedTimer(RuntimeProfile::Counter *counter)
Counter * AddSamplingCounter(const std::string &name, Counter *src_counter)
Counter total_async_timer_
virtual double double_value() const
void AddInfoString(const std::string &key, const std::string &value)
client RuntimeProfile::EventSequence * events
void ComputeTimeInProfile()
HighWaterMarkCounter(TUnit::type unit)
std::map< std::string, RuntimeProfile * > ChildMap
std::string SerializeToArchiveString() const
InfoStrings info_strings_
boost::mutex event_sequence_lock_
static RuntimeProfile * CreateFromThrift(ObjectPool *pool, const TRuntimeProfileTree &profiles)
Deserialize from thrift. Runtime profiles are allocated from the pool.
std::map< std::string, std::set< std::string > > ChildCounterMap
std::pair< std::string, int64_t > Event
An Event is a <label, timestamp> pair.
RuntimeProfile(ObjectPool *pool, const std::string &name, bool is_averaged_profile=false)
void RegisterBucketingCounters(Counter *src_counter, std::vector< Counter * > *buckets)
AtomicInt< int64_t > value_
std::string name_
Name for this runtime profile.
void GetCounters(const std::string &name, std::vector< Counter * > *counters)
EventSequenceMap event_sequence_map_
boost::function< int64_t()> DerivedCounterFunction
std::map< std::string, Counter * > CounterMap
RuntimeProfile::Counter * counter_
double current_double_sum_
bool TryAdd(int64_t delta, int64_t max)
Counter * AddRateCounter(const std::string &name, Counter *src_counter)
static int64_t UnitsPerSecond(const Counter *total_counter, const Counter *timer)
Derived counter function: return measured throughput as input_value/second.
AveragedCounter(TUnit::type unit)
virtual void Add(int64_t delta)
Counter * inactive_timer()
void UpdateMax(T value)
Updates the int to 'value' if value is larger.
std::map< std::string, TimeSeriesCounter * > TimeSeriesCounterMap
int num_counters() const
Returns the number of counters in this profile.
void UpdateCounter(Counter *new_counter)
virtual void Set(double value)
virtual void Set(int64_t value)
std::vector< std::string > InfoStringsDisplayOrder
Keeps track of the order in which InfoStrings are displayed when printed.
boost::mutex time_series_counter_map_lock_
StreamingSampler< int64_t, 64 > StreamingCounterSampler
EventSequence * GetEventSequence(const std::string &name) const
Returns event sequence with the provided name if it exists, otherwise NULL.
virtual void Set(int64_t value)
static int64_t CounterSum(const std::vector< Counter * > *counters)
Derived counter function: return aggregated value.
void ToThrift(TTimeSeriesCounter *counter)
void Stop()
Stop and update the counter.
double local_time_percent_
void UpdateAverage(RuntimeProfile *src)
void MarkEvent(const std::string &label)
void Divide(int n)
Divides all counters by n.
bool CompareAndSwap(T old_val, T new_val)
Returns true if the atomic compare-and-swap was successful.
bool own_pool_
True if we have to delete the pool_ on destruction.
std::map< std::string, EventSequence * > EventSequenceMap
ScopedCounter & operator=(const ScopedCounter &counter)
~ScopedCounter()
Increment the counter when object is destroyed.
static const std::string ASYNC_TIME_COUNTER_NAME
MonotonicStopWatch sw_
Timer which allows events to be timestamped when they are recorded.
void Start()
Starts the timer without resetting it.
ScopedEvent & operator=(const ScopedEvent &event)
ThreadCounters * AddThreadCounters(const std::string &prefix)
virtual int64_t value() const
ThreadCounterMeasurement(RuntimeProfile::ThreadCounters *counters)
Counter * voluntary_context_switches_
ScopedCounter(RuntimeProfile::Counter *counter, int64_t val)
boost::mutex lock_
Protect access to events_.
boost::mutex children_lock_
DerivedCounterFunction counter_fn_
~ThreadCounterMeasurement()
Update counter when object is destroyed.
uint64_t ElapsedTime() const
Returns time in nanosecond.
RuntimeProfile::ThreadCounters * counters_
RuntimeProfile::EventSequence * event_sequence_
void Update(const TRuntimeProfileTree &thrift_profile)
void ToThrift(TEventSequence *seq) const
Counter * total_async_timer()
AtomicInt< int64_t > current_value_
std::vector< Event > EventList
An EventList is a sequence of Events, in increasing timestamp order.
std::set< std::vector< Counter * > * > bucketing_counters_
A set of bucket counters registered in this runtime profile.
TimeSeriesCounter * AddTimeSeriesCounter(const std::string &name, TUnit::type unit, DerivedCounterFunction sample_fn)
static void PrintChildCounters(const std::string &prefix, const std::string &counter_name, const CounterMap &counter_map, const ChildCounterMap &child_counter_map, std::ostream *s)
Print the child counters of the given counter name.
std::vector< std::pair< RuntimeProfile *, bool > > ChildVector
vector of (profile, indentation flag)
int64_t metadata_
user-supplied, uninterpreted metadata.
Counter counter_total_time_
HighWaterMarkCounter * AddHighWaterMarkCounter(const std::string &name, TUnit::type unit, const std::string &parent_counter_name="")
ThreadCounterMeasurement & operator=(const ThreadCounterMeasurement &timer)
Counter(TUnit::type unit, int64_t value=0)
~ScopedTimer()
Update counter when object is destroyed.
virtual void Set(int64_t v)
~ScopedEvent()
Mark the event when the object is destroyed.
Counter * GetCounter(const std::string &name)
TimeSeriesCounter(const std::string &name, TUnit::type unit, int period, const std::vector< int64_t > &values)
void PrettyPrint(std::ostream *s, const std::string &prefix="") const
RuntimeProfile::Counter * counter_
virtual void Set(double value)
Utility class to mark an event when the object is destroyed.
ScopedTimer & operator=(const ScopedTimer &timer)
boost::mutex counter_map_lock_
protects counter_map_, counter_child_map_ and bucketing_counters_
ScopedEvent(RuntimeProfile::EventSequence *event_sequence, const std::string &label)
TimeSeriesCounterMap time_series_counter_map_
static const std::string INACTIVE_TIME_COUNTER_NAME
InfoStringsDisplayOrder info_strings_display_order_
EventSequence(const std::vector< int64_t > ×tamps, const std::vector< std::string > &labels)
Helper constructor for building from Thrift.
EventList events_
Stored in increasing time order.
std::string DebugString() const
std::map< std::string, std::string > InfoStrings
void AddSample(T sample, int ms)
virtual void Add(int64_t delta)
static const std::string TOTAL_TIME_COUNTER_NAME
Name of the counter maintaining the total time.
void AddChild(RuntimeProfile *child, bool indent=true, RuntimeProfile *location=NULL)
const std::string & name() const
Returns name of this profile.
const std::string * GetInfoString(const std::string &key) const
void GetChildren(std::vector< RuntimeProfile * > *children)
boost::mutex info_strings_lock_
Protects info_strings_ and info_strings_display_order_.
void ReleaseCounter()
Updates the underlying counter for the final time and clears the pointer to it.
void BitOr(int64_t delta)
Use this to update if the counter is a bitmap.
void set_name(const std::string &name)
StreamingCounterSampler samples_
virtual void Add(int64_t delta)
Counter * involuntary_context_switches_
void GetAllChildren(std::vector< RuntimeProfile * > *children)
Gets all profiles in tree, including this one.
bool is_averaged_profile_
void ToThrift(TRuntimeProfileTree *tree) const
Counter * total_time_counter()
Returns the counter for the total elapsed time.