/** * @file lltrace.h * @brief Runtime statistics accumulation. * * $LicenseInfo:firstyear=2001&license=viewerlgpl$ * Second Life Viewer Source Code * Copyright (C) 2012, Linden Research, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License only. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA * $/LicenseInfo$ */ #ifndef LL_LLTRACE_H #define LL_LLTRACE_H #include "stdtypes.h" #include "llpreprocessor.h" #include "llmemory.h" #include "llrefcount.h" #include "lltraceaccumulators.h" #include "llthreadlocalstorage.h" #include "lltimer.h" #include "llpointer.h" #include "llunits.h" #define LL_TRACE_ENABLED 1 namespace LLTrace { class Recording; template T storage_value(T val) { return val; } template STORAGE_TYPE storage_value(LLUnit val) { return val.value(); } template STORAGE_TYPE storage_value(LLUnitImplicit val) { return val.value(); } class StatBase { public: StatBase(const char* name, const char* description); virtual ~StatBase() {}; virtual const char* getUnitLabel() const; const std::string& getName() const { return mName; } const std::string& getDescription() const { return mDescription; } protected: std::string mName; std::string mDescription; }; template class StatType : public StatBase, public LLInstanceTracker, std::string> { public: typedef LLInstanceTracker, std::string> instance_tracker_t; StatType(const char* name, const char* description) : instance_tracker_t(name), StatBase(name, description), mAccumulatorIndex(AccumulatorBuffer::getDefaultBuffer()->reserveSlot()) {} LL_FORCE_INLINE ACCUMULATOR& getCurrentAccumulator() const { ACCUMULATOR* accumulator_storage = LLThreadLocalSingletonPointer::getInstance(); return accumulator_storage ? accumulator_storage[mAccumulatorIndex] : (*AccumulatorBuffer::getDefaultBuffer())[mAccumulatorIndex]; } size_t getIndex() const { return mAccumulatorIndex; } static size_t getNumIndices() { return AccumulatorBuffer::getNumIndices(); } protected: const size_t mAccumulatorIndex; }; template<> class StatType : public StatType { public: StatType(const char* name, const char* description = "") : StatType(name, description) {} }; template<> class StatType : public StatType { public: StatType(const char* name, const char* description = "") : StatType(name, description) {} }; template class EventStatHandle : public StatType { public: typedef F64 storage_t; typedef StatType stat_t; typedef EventStatHandle self_t; EventStatHandle(const char* name, const char* description = NULL) : stat_t(name, description) {} /*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel::getUnitLabel(); } }; template void record(EventStatHandle& measurement, VALUE_T value) { #if LL_TRACE_ENABLED T converted_value(value); measurement.getCurrentAccumulator().record(storage_value(converted_value)); #endif } template class SampleStatHandle : public StatType { public: typedef F64 storage_t; typedef StatType stat_t; typedef SampleStatHandle self_t; SampleStatHandle(const char* name, const char* description = NULL) : stat_t(name, description) {} /*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel::getUnitLabel(); } }; template void sample(SampleStatHandle& measurement, VALUE_T value) { #if LL_TRACE_ENABLED T converted_value(value); measurement.getCurrentAccumulator().sample(storage_value(converted_value)); #endif } template class CountStatHandle : public StatType { public: typedef F64 storage_t; typedef StatType stat_t; typedef CountStatHandle self_t; CountStatHandle(const char* name, const char* description = NULL) : stat_t(name, description) {} /*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel::getUnitLabel(); } }; template void add(CountStatHandle& count, VALUE_T value) { #if LL_TRACE_ENABLED T converted_value(value); count.getCurrentAccumulator().add(storage_value(converted_value)); #endif } template<> class StatType : public StatType { public: StatType(const char* name, const char* description = "") : StatType(name, description) {} }; template<> class StatType : public StatType { public: StatType(const char* name, const char* description = "") : StatType(name, description) {} }; class MemStatHandle : public StatType { public: typedef StatType stat_t; MemStatHandle(const char* name, const char* description = "") : stat_t(name, description) { mName = name; } void setName(const char* name) { mName = name; setKey(name); } /*virtual*/ const char* getUnitLabel() const { return "KB"; } StatType& allocations() { return static_cast&>(*(StatType*)this); } StatType& deallocations() { return static_cast&>(*(StatType*)this); } }; // measures effective memory footprint of specified type // specialize to cover different types template struct MeasureMem { static size_t measureFootprint(const T& value) { return sizeof(T); } }; template struct MeasureMem { static size_t measureFootprint(const T& value) { return sizeof(T) + value.getMemFootprint(); } }; template struct MeasureMem { static size_t measureFootprint(const T& value) { return U32Bytes(value).value(); } }; template struct MeasureMem { static size_t measureFootprint(const T* value) { if (!value) { return 0; } return MeasureMem::measureFootprint(*value); } }; template struct MeasureMem, IS_MEM_TRACKABLE, IS_BYTES> { static size_t measureFootprint(const LLPointer value) { if (value.isNull()) { return 0; } return MeasureMem::measureFootprint(*value); } }; template struct MeasureMem { static size_t measureFootprint(S32 value) { return value; } }; template struct MeasureMem { static size_t measureFootprint(U32 value) { return value; } }; template struct MeasureMem, IS_MEM_TRACKABLE, IS_BYTES> { static size_t measureFootprint(const std::basic_string& value) { return value.capacity() * sizeof(T); } }; template inline void claim_alloc(MemStatHandle& measurement, const T& value) { #if LL_TRACE_ENABLED S32 size = MeasureMem::measureFootprint(value); if(size == 0) return; MemAccumulator& accumulator = measurement.getCurrentAccumulator(); accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() + (F64)size : (F64)size); accumulator.mAllocations.record(size); #endif } template inline void disclaim_alloc(MemStatHandle& measurement, const T& value) { #if LL_TRACE_ENABLED S32 size = MeasureMem::measureFootprint(value); if(size == 0) return; MemAccumulator& accumulator = measurement.getCurrentAccumulator(); accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() - (F64)size : -(F64)size); accumulator.mDeallocations.add(size); #endif } template class MemTrackableNonVirtual { public: typedef void mem_trackable_tag_t; MemTrackableNonVirtual(const char* name) #if LL_TRACE_ENABLED : mMemFootprint(0) #endif { #if LL_TRACE_ENABLED static bool name_initialized = false; if (!name_initialized) { name_initialized = true; sMemStat.setName(name); } #endif } #if LL_TRACE_ENABLED ~MemTrackableNonVirtual() { disclaimMem(mMemFootprint); } static MemStatHandle& getMemStatHandle() { return sMemStat; } S32 getMemFootprint() const { return mMemFootprint; } #endif void* operator new(size_t size) { #if LL_TRACE_ENABLED claim_alloc(sMemStat, size); #endif return ll_aligned_malloc(size); } template static void* aligned_new(size_t size) { #if LL_TRACE_ENABLED claim_alloc(sMemStat, size); #endif return ll_aligned_malloc(size); } void operator delete(void* ptr, size_t size) { #if LL_TRACE_ENABLED disclaim_alloc(sMemStat, size); #endif ll_aligned_free(ptr); } template static void aligned_delete(void* ptr, size_t size) { #if LL_TRACE_ENABLED disclaim_alloc(sMemStat, size); #endif ll_aligned_free(ptr); } void* operator new [](size_t size) { #if LL_TRACE_ENABLED claim_alloc(sMemStat, size); #endif return ll_aligned_malloc(size); } void operator delete[](void* ptr, size_t size) { #if LL_TRACE_ENABLED disclaim_alloc(sMemStat, size); #endif ll_aligned_free(ptr); } // claim memory associated with other objects/data as our own, adding to our calculated footprint template void claimMem(const CLAIM_T& value) const { #if LL_TRACE_ENABLED S32 size = MeasureMem::measureFootprint(value); claim_alloc(sMemStat, size); mMemFootprint += size; #endif } // remove memory we had claimed from our calculated footprint template void disclaimMem(const CLAIM_T& value) const { #if LL_TRACE_ENABLED S32 size = MeasureMem::measureFootprint(value); disclaim_alloc(sMemStat, size); mMemFootprint -= size; #endif } private: #if LL_TRACE_ENABLED // use signed values so that we can temporarily go negative // and reconcile in destructor // NB: this assumes that no single class is responsible for > 2GB of allocations mutable S32 mMemFootprint; static MemStatHandle sMemStat; #endif }; #if LL_TRACE_ENABLED template MemStatHandle MemTrackableNonVirtual::sMemStat(typeid(MemTrackableNonVirtual).name()); #endif template class MemTrackable : public MemTrackableNonVirtual { public: MemTrackable(const char* name) : MemTrackableNonVirtual(name) {} virtual ~MemTrackable() {} }; } #endif // LL_LLTRACE_H