/** * @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 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 TraceBase { public: TraceBase(const char* name, const char* description); virtual ~TraceBase() {}; 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 TraceType : public TraceBase, public LLInstanceTracker, std::string> { public: TraceType(const char* name, const char* description = NULL) : LLInstanceTracker, std::string>(name), TraceBase(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 TraceType : public TraceType { public: TraceType(const char* name, const char* description = "") : TraceType(name, description) {} }; template<> class TraceType : public TraceType { public: TraceType(const char* name, const char* description = "") : TraceType(name, description) {} }; template class EventStatHandle : public TraceType { public: typedef F64 storage_t; typedef TraceType trace_t; typedef EventStatHandle self_t; EventStatHandle(const char* name, const char* description = NULL) : trace_t(name, description) {} /*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel::getUnitLabel(); } }; template void record(EventStatHandle& measurement, VALUE_T value) { T converted_value(value); measurement.getCurrentAccumulator().record(storage_value(converted_value)); } template class SampleStatHandle : public TraceType { public: typedef F64 storage_t; typedef TraceType trace_t; typedef SampleStatHandle self_t; SampleStatHandle(const char* name, const char* description = NULL) : trace_t(name, description) {} /*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel::getUnitLabel(); } }; template void sample(SampleStatHandle& measurement, VALUE_T value) { T converted_value(value); measurement.getCurrentAccumulator().sample(storage_value(converted_value)); } template void add(SampleStatHandle& measurement, VALUE_T value) { T converted_value(value); SampleAccumulator& acc = measurement.getCurrentAccumulator(); if (acc.hasValue()) { acc.sample(acc.getLastValue() + converted_value); } else { acc.sample(converted_value); } } template class CountStatHandle : public TraceType { public: typedef F64 storage_t; typedef TraceType trace_t; typedef CountStatHandle self_t; CountStatHandle(const char* name, const char* description = NULL) : trace_t(name, description) {} /*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel::getUnitLabel(); } }; template void add(CountStatHandle& count, VALUE_T value) { T converted_value(value); count.getCurrentAccumulator().add(storage_value(converted_value)); } template<> class TraceType : public TraceType { public: TraceType(const char* name, const char* description = "") : TraceType(name, description) {} }; template<> class TraceType : public TraceType { public: TraceType(const char* name, const char* description = "") : TraceType(name, description) {} }; template<> class TraceType : public TraceType { public: TraceType(const char* name, const char* description = "") : TraceType(name, description) {} }; class MemStatHandle : public TraceType { public: typedef TraceType trace_t; MemStatHandle(const char* name) : trace_t(name) {} void setName(const char* name) { mName = name; setKey(name); } /*virtual*/ const char* getUnitLabel() const { return "B"; } TraceType& allocationCount() { return static_cast&>(*(TraceType*)this); } TraceType& deallocationCount() { return static_cast&>(*(TraceType*)this); } TraceType& childMem() { return static_cast&>(*(TraceType*)this); } }; inline void track_alloc(MemStatHandle& measurement, size_t size) { MemStatAccumulator& accumulator = measurement.getCurrentAccumulator(); accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() + (F64)size : (F64)size); accumulator.mAllocatedCount++; } inline void track_dealloc(MemStatHandle& measurement, size_t size) { MemStatAccumulator& accumulator = measurement.getCurrentAccumulator(); accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() - (F64)size : -(F64)size); accumulator.mAllocatedCount--; accumulator.mDeallocatedCount++; } inline void claim_mem(MemStatHandle& measurement, size_t size) { MemStatAccumulator& accumulator = measurement.getCurrentAccumulator(); accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() + (F64)size : (F64)size); } inline void disclaim_mem(MemStatHandle& measurement, size_t size) { MemStatAccumulator& accumulator = measurement.getCurrentAccumulator(); accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() - (F64)size : -(F64)size); } inline void claim_shadow_mem(MemStatHandle& measurement, size_t size) { MemStatAccumulator& accumulator = measurement.getCurrentAccumulator(); accumulator.mShadowSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() + (F64)size : (F64)size); } inline void disclaim_shadow_mem(MemStatHandle& measurement, size_t size) { MemStatAccumulator& accumulator = measurement.getCurrentAccumulator(); accumulator.mShadowSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() - (F64)size : -(F64)size); } // measures effective memory footprint of specified type // specialize to cover different types template struct MemFootprint { static size_t measure(const T& value) { return sizeof(T); } static size_t measure() { return sizeof(T); } }; template struct MemFootprint { static size_t measure(const T* value) { if (!value) { return 0; } return MemFootprint::measure(*value); } static size_t measure() { return MemFootprint::measure(); } }; template struct MemFootprint > { static size_t measure(const std::basic_string& value) { return value.capacity() * sizeof(T); } static size_t measure() { return sizeof(std::basic_string); } }; template struct MemFootprint > { static size_t measure(const std::vector& value) { return value.capacity() * MemFootprint::measure(); } static size_t measure() { return sizeof(std::vector); } }; template struct MemFootprint > { static size_t measure(const std::list& value) { return value.size() * (MemFootprint::measure() + sizeof(void*) * 2); } static size_t measure() { return sizeof(std::list); } }; template class MemTrackable { template struct TrackMemImpl; typedef MemTrackable mem_trackable_t; static MemStatHandle sMemStat; public: typedef void mem_trackable_tag_t; MemTrackable() { static bool name_initialized = false; if (!name_initialized) { name_initialized = true; sMemStat.setName(typeid(DERIVED).name()); } } virtual ~MemTrackable() { memDisclaim(mMemFootprint); } void* operator new(size_t size) { track_alloc(sMemStat, size); if (ALIGNMENT == LL_DEFAULT_HEAP_ALIGN) { return ::operator new(size); } else if (ALIGNMENT == 16) { return ll_aligned_malloc_16(size); } else if (ALIGNMENT == 32) { return ll_aligned_malloc_32(size); } else { return ll_aligned_malloc(size, ALIGNMENT); } } void operator delete(void* ptr, size_t size) { track_dealloc(sMemStat, size); if (ALIGNMENT == LL_DEFAULT_HEAP_ALIGN) { ::operator delete(ptr); } else if (ALIGNMENT == 16) { ll_aligned_free_16(ptr); } else if (ALIGNMENT == 32) { return ll_aligned_free_32(ptr); } else { return ll_aligned_free(ptr); } } void *operator new [](size_t size) { track_alloc(sMemStat, size); if (ALIGNMENT == LL_DEFAULT_HEAP_ALIGN) { return ::operator new[](size); } else if (ALIGNMENT == 16) { return ll_aligned_malloc_16(size); } else if (ALIGNMENT == 32) { return ll_aligned_malloc_32(size); } else { return ll_aligned_malloc(size, ALIGNMENT); } } void operator delete[](void* ptr, size_t size) { MemStatAccumulator& accumulator = sMemStat.getCurrentAccumulator(); accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() - (F64)size : -(F64)size); accumulator.mAllocatedCount--; accumulator.mDeallocatedCount++; if (ALIGNMENT == LL_DEFAULT_HEAP_ALIGN) { ::operator delete[](ptr); } else if (ALIGNMENT == 16) { ll_aligned_free_16(ptr); } else if (ALIGNMENT == 32) { return ll_aligned_free_32(ptr); } else { return ll_aligned_free(ptr); } } // claim memory associated with other objects/data as our own, adding to our calculated footprint template CLAIM_T& memClaim(CLAIM_T& value) { TrackMemImpl::claim(*this, value); return value; } template const CLAIM_T& memClaim(const CLAIM_T& value) { TrackMemImpl::claim(*this, value); return value; } const size_t& memClaim(const size_t& size) { claim_mem(sMemStat, size); mMemFootprint += size; return size; } size_t& memClaim(size_t& size) { claim_mem(sMemStat, size); mMemFootprint += size; return size; } // remove memory we had claimed from our calculated footprint template CLAIM_T& memDisclaim(CLAIM_T& value) { TrackMemImpl::disclaim(*this, value); return value; } template const CLAIM_T& memDisclaim(const CLAIM_T& value) { TrackMemImpl::disclaim(*this, value); return value; } const size_t& memDisclaim(const size_t& size) { disclaim_mem(sMemStat, size); mMemFootprint -= size; return size; } size_t& memDisclaim(size_t& size) { disclaim_mem(sMemStat, size); mMemFootprint -= size; return size; } private: size_t mMemFootprint; template struct TrackMemImpl { static void claim(mem_trackable_t& tracker, const TRACKED& tracked) { size_t footprint = MemFootprint::measure(tracked); claim_mem(sMemStat, footprint); tracker.mMemFootprint += footprint; } static void disclaim(mem_trackable_t& tracker, const TRACKED& tracked) { size_t footprint = MemFootprint::measure(tracked); disclaim_mem(sMemStat, footprint); tracker.mMemFootprint -= footprint; } }; template struct TrackMemImpl { static void claim(mem_trackable_t& tracker, TRACKED& tracked) { claim_shadow_mem( (F64)MemFootprint::measure(tracked)); } static void disclaim(mem_trackable_t& tracker, TRACKED& tracked) { disclaim_shadow_mem((F64)MemFootprint::measure(tracked)); } }; }; template MemStatHandle MemTrackable::sMemStat(""); } #endif // LL_LLTRACE_H