Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_BackgroundHangTelemetry_h |
michael@0 | 7 | #define mozilla_BackgroundHangTelemetry_h |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/Array.h" |
michael@0 | 10 | #include "mozilla/Assertions.h" |
michael@0 | 11 | #include "mozilla/Move.h" |
michael@0 | 12 | #include "mozilla/Mutex.h" |
michael@0 | 13 | #include "mozilla/PodOperations.h" |
michael@0 | 14 | #include "mozilla/Vector.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "nsString.h" |
michael@0 | 17 | #include "prinrval.h" |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace Telemetry { |
michael@0 | 21 | |
michael@0 | 22 | static const size_t kTimeHistogramBuckets = 8 * sizeof(PRIntervalTime); |
michael@0 | 23 | |
michael@0 | 24 | /* TimeHistogram is an efficient histogram that puts time durations into |
michael@0 | 25 | exponential (base 2) buckets; times are accepted in PRIntervalTime and |
michael@0 | 26 | stored in milliseconds. */ |
michael@0 | 27 | class TimeHistogram : public mozilla::Array<uint32_t, kTimeHistogramBuckets> |
michael@0 | 28 | { |
michael@0 | 29 | public: |
michael@0 | 30 | TimeHistogram() |
michael@0 | 31 | { |
michael@0 | 32 | mozilla::PodArrayZero(*this); |
michael@0 | 33 | } |
michael@0 | 34 | // Get minimum (inclusive) range of bucket in milliseconds |
michael@0 | 35 | uint32_t GetBucketMin(size_t aBucket) const { |
michael@0 | 36 | MOZ_ASSERT(aBucket < ArrayLength(*this)); |
michael@0 | 37 | return (1u << aBucket) & ~1u; // Bucket 0 starts at 0, not 1 |
michael@0 | 38 | } |
michael@0 | 39 | // Get maximum (inclusive) range of bucket in milliseconds |
michael@0 | 40 | uint32_t GetBucketMax(size_t aBucket) const { |
michael@0 | 41 | MOZ_ASSERT(aBucket < ArrayLength(*this)); |
michael@0 | 42 | return (1u << (aBucket + 1u)) - 1u; |
michael@0 | 43 | } |
michael@0 | 44 | void Add(PRIntervalTime aTime); |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | /* A hang histogram consists of a stack associated with the |
michael@0 | 48 | hang, along with a time histogram of the hang times. */ |
michael@0 | 49 | class HangHistogram : public TimeHistogram |
michael@0 | 50 | { |
michael@0 | 51 | public: |
michael@0 | 52 | typedef mozilla::Vector<const char*, 8> Stack; |
michael@0 | 53 | |
michael@0 | 54 | private: |
michael@0 | 55 | static uint32_t GetHash(const Stack& aStack); |
michael@0 | 56 | |
michael@0 | 57 | Stack mStack; |
michael@0 | 58 | // Use a hash to speed comparisons |
michael@0 | 59 | const uint32_t mHash; |
michael@0 | 60 | |
michael@0 | 61 | public: |
michael@0 | 62 | explicit HangHistogram(Stack&& aStack) |
michael@0 | 63 | : mStack(mozilla::Move(aStack)) |
michael@0 | 64 | , mHash(GetHash(mStack)) |
michael@0 | 65 | { |
michael@0 | 66 | } |
michael@0 | 67 | HangHistogram(HangHistogram&& aOther) |
michael@0 | 68 | : TimeHistogram(mozilla::Move(aOther)) |
michael@0 | 69 | , mStack(mozilla::Move(aOther.mStack)) |
michael@0 | 70 | , mHash(mozilla::Move(aOther.mHash)) |
michael@0 | 71 | { |
michael@0 | 72 | } |
michael@0 | 73 | bool operator==(const HangHistogram& aOther) const; |
michael@0 | 74 | bool operator!=(const HangHistogram& aOther) const |
michael@0 | 75 | { |
michael@0 | 76 | return !operator==(aOther); |
michael@0 | 77 | } |
michael@0 | 78 | const Stack& GetStack() const { |
michael@0 | 79 | return mStack; |
michael@0 | 80 | } |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | /* Thread hang stats consist of |
michael@0 | 84 | - thread name |
michael@0 | 85 | - time histogram of all task run times |
michael@0 | 86 | - hang histograms of individual hangs. */ |
michael@0 | 87 | class ThreadHangStats |
michael@0 | 88 | { |
michael@0 | 89 | private: |
michael@0 | 90 | nsAutoCString mName; |
michael@0 | 91 | |
michael@0 | 92 | public: |
michael@0 | 93 | TimeHistogram mActivity; |
michael@0 | 94 | mozilla::Vector<HangHistogram, 4> mHangs; |
michael@0 | 95 | |
michael@0 | 96 | explicit ThreadHangStats(const char* aName) |
michael@0 | 97 | : mName(aName) |
michael@0 | 98 | { |
michael@0 | 99 | } |
michael@0 | 100 | ThreadHangStats(ThreadHangStats&& aOther) |
michael@0 | 101 | : mName(mozilla::Move(aOther.mName)) |
michael@0 | 102 | , mActivity(mozilla::Move(aOther.mActivity)) |
michael@0 | 103 | , mHangs(mozilla::Move(aOther.mHangs)) |
michael@0 | 104 | { |
michael@0 | 105 | } |
michael@0 | 106 | const char* GetName() const { |
michael@0 | 107 | return mName.get(); |
michael@0 | 108 | } |
michael@0 | 109 | }; |
michael@0 | 110 | |
michael@0 | 111 | } // namespace Telemetry |
michael@0 | 112 | } // namespace mozilla |
michael@0 | 113 | #endif // mozilla_BackgroundHangTelemetry_h |