1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/telemetry/ThreadHangStats.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_BackgroundHangTelemetry_h 1.10 +#define mozilla_BackgroundHangTelemetry_h 1.11 + 1.12 +#include "mozilla/Array.h" 1.13 +#include "mozilla/Assertions.h" 1.14 +#include "mozilla/Move.h" 1.15 +#include "mozilla/Mutex.h" 1.16 +#include "mozilla/PodOperations.h" 1.17 +#include "mozilla/Vector.h" 1.18 + 1.19 +#include "nsString.h" 1.20 +#include "prinrval.h" 1.21 + 1.22 +namespace mozilla { 1.23 +namespace Telemetry { 1.24 + 1.25 +static const size_t kTimeHistogramBuckets = 8 * sizeof(PRIntervalTime); 1.26 + 1.27 +/* TimeHistogram is an efficient histogram that puts time durations into 1.28 + exponential (base 2) buckets; times are accepted in PRIntervalTime and 1.29 + stored in milliseconds. */ 1.30 +class TimeHistogram : public mozilla::Array<uint32_t, kTimeHistogramBuckets> 1.31 +{ 1.32 +public: 1.33 + TimeHistogram() 1.34 + { 1.35 + mozilla::PodArrayZero(*this); 1.36 + } 1.37 + // Get minimum (inclusive) range of bucket in milliseconds 1.38 + uint32_t GetBucketMin(size_t aBucket) const { 1.39 + MOZ_ASSERT(aBucket < ArrayLength(*this)); 1.40 + return (1u << aBucket) & ~1u; // Bucket 0 starts at 0, not 1 1.41 + } 1.42 + // Get maximum (inclusive) range of bucket in milliseconds 1.43 + uint32_t GetBucketMax(size_t aBucket) const { 1.44 + MOZ_ASSERT(aBucket < ArrayLength(*this)); 1.45 + return (1u << (aBucket + 1u)) - 1u; 1.46 + } 1.47 + void Add(PRIntervalTime aTime); 1.48 +}; 1.49 + 1.50 +/* A hang histogram consists of a stack associated with the 1.51 + hang, along with a time histogram of the hang times. */ 1.52 +class HangHistogram : public TimeHistogram 1.53 +{ 1.54 +public: 1.55 + typedef mozilla::Vector<const char*, 8> Stack; 1.56 + 1.57 +private: 1.58 + static uint32_t GetHash(const Stack& aStack); 1.59 + 1.60 + Stack mStack; 1.61 + // Use a hash to speed comparisons 1.62 + const uint32_t mHash; 1.63 + 1.64 +public: 1.65 + explicit HangHistogram(Stack&& aStack) 1.66 + : mStack(mozilla::Move(aStack)) 1.67 + , mHash(GetHash(mStack)) 1.68 + { 1.69 + } 1.70 + HangHistogram(HangHistogram&& aOther) 1.71 + : TimeHistogram(mozilla::Move(aOther)) 1.72 + , mStack(mozilla::Move(aOther.mStack)) 1.73 + , mHash(mozilla::Move(aOther.mHash)) 1.74 + { 1.75 + } 1.76 + bool operator==(const HangHistogram& aOther) const; 1.77 + bool operator!=(const HangHistogram& aOther) const 1.78 + { 1.79 + return !operator==(aOther); 1.80 + } 1.81 + const Stack& GetStack() const { 1.82 + return mStack; 1.83 + } 1.84 +}; 1.85 + 1.86 +/* Thread hang stats consist of 1.87 + - thread name 1.88 + - time histogram of all task run times 1.89 + - hang histograms of individual hangs. */ 1.90 +class ThreadHangStats 1.91 +{ 1.92 +private: 1.93 + nsAutoCString mName; 1.94 + 1.95 +public: 1.96 + TimeHistogram mActivity; 1.97 + mozilla::Vector<HangHistogram, 4> mHangs; 1.98 + 1.99 + explicit ThreadHangStats(const char* aName) 1.100 + : mName(aName) 1.101 + { 1.102 + } 1.103 + ThreadHangStats(ThreadHangStats&& aOther) 1.104 + : mName(mozilla::Move(aOther.mName)) 1.105 + , mActivity(mozilla::Move(aOther.mActivity)) 1.106 + , mHangs(mozilla::Move(aOther.mHangs)) 1.107 + { 1.108 + } 1.109 + const char* GetName() const { 1.110 + return mName.get(); 1.111 + } 1.112 +}; 1.113 + 1.114 +} // namespace Telemetry 1.115 +} // namespace mozilla 1.116 +#endif // mozilla_BackgroundHangTelemetry_h