1 /* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2  */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef HangReports_h__
8 #define HangReports_h__
9 
10 #include <vector>
11 #include "mozilla/HangAnnotations.h"
12 #include "ProcessedStack.h"
13 #include "nsTArray.h"
14 #include "nsString.h"
15 #include "nsClassHashtable.h"
16 #include "CombinedStacks.h"
17 
18 namespace mozilla {
19 namespace Telemetry {
20 
21 nsresult ComputeAnnotationsKey(const HangMonitor::HangAnnotations& aAnnotations,
22                                nsAString& aKeyOut);
23 
24 class HangReports {
25  public:
26   /**
27    * This struct encapsulates information for an individual ChromeHang
28    * annotation. mHangIndex is the index of the corresponding ChromeHang.
29    */
30   struct AnnotationInfo {
AnnotationInfoAnnotationInfo31     AnnotationInfo(uint32_t aHangIndex,
32                    HangMonitor::HangAnnotations&& aAnnotations)
33         : mAnnotations(Move(aAnnotations)) {
34       mHangIndices.AppendElement(aHangIndex);
35     }
AnnotationInfoAnnotationInfo36     AnnotationInfo(AnnotationInfo&& aOther)
37         : mHangIndices(aOther.mHangIndices),
38           mAnnotations(Move(aOther.mAnnotations)) {}
39     ~AnnotationInfo() = default;
40     AnnotationInfo& operator=(AnnotationInfo&& aOther) {
41       mHangIndices = aOther.mHangIndices;
42       mAnnotations = Move(aOther.mAnnotations);
43       return *this;
44     }
45     // To save memory, a single AnnotationInfo can be associated to multiple
46     // chrome hangs. The following array holds the index of each related chrome
47     // hang.
48     nsTArray<uint32_t> mHangIndices;
49     HangMonitor::HangAnnotations mAnnotations;
50 
51    private:
52     // Force move constructor
53     AnnotationInfo(const AnnotationInfo& aOther) = delete;
54     void operator=(const AnnotationInfo& aOther) = delete;
55   };
56   size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
57 #if defined(MOZ_GECKO_PROFILER)
58   void AddHang(const Telemetry::ProcessedStack& aStack, uint32_t aDuration,
59                int32_t aSystemUptime, int32_t aFirefoxUptime,
60                HangMonitor::HangAnnotations&& aAnnotations);
61   void PruneStackReferences(const size_t aRemovedStackIndex);
62 #endif
63   uint32_t GetDuration(unsigned aIndex) const;
64   int32_t GetSystemUptime(unsigned aIndex) const;
65   int32_t GetFirefoxUptime(unsigned aIndex) const;
66   const nsClassHashtable<nsStringHashKey, AnnotationInfo>& GetAnnotationInfo()
67       const;
68   const CombinedStacks& GetStacks() const;
69 
70  private:
71   /**
72    * This struct encapsulates the data for an individual ChromeHang, excluding
73    * annotations.
74    */
75   struct HangInfo {
76     // Hang duration (in seconds)
77     uint32_t mDuration;
78     // System uptime (in minutes) at the time of the hang
79     int32_t mSystemUptime;
80     // Firefox uptime (in minutes) at the time of the hang
81     int32_t mFirefoxUptime;
82   };
83   std::vector<HangInfo> mHangInfo;
84   nsClassHashtable<nsStringHashKey, AnnotationInfo> mAnnotationInfo;
85   CombinedStacks mStacks;
86 };
87 
88 }  // namespace Telemetry
89 }  // namespace mozilla
90 
91 #endif  // CombinedStacks_h__
92