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