summaryrefslogtreecommitdiffstats
path: root/xpcom/threads/HangAnnotations.cpp
blob: 529b57b8e31b0a0ef8e0893e69d9e2af8ade56cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/HangAnnotations.h"

#include <vector>

#include "MainThreadUtils.h"
#include "mozilla/DebugOnly.h"
#include "nsXULAppAPI.h"

namespace mozilla {
namespace HangMonitor {

// Chrome hang annotators. This can go away once BHR has completely replaced
// ChromeHangs.
static StaticAutoPtr<Observer::Annotators> gChromehangAnnotators;

class BrowserHangAnnotations : public HangAnnotations
{
public:
  BrowserHangAnnotations();
  ~BrowserHangAnnotations();

  void AddAnnotation(const nsAString& aName, const int32_t aData) override;
  void AddAnnotation(const nsAString& aName, const double aData) override;
  void AddAnnotation(const nsAString& aName, const nsAString& aData) override;
  void AddAnnotation(const nsAString& aName, const nsACString& aData) override;
  void AddAnnotation(const nsAString& aName, const bool aData) override;

  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const override;
  bool IsEmpty() const override;
  UniquePtr<Enumerator> GetEnumerator() override;

  typedef std::pair<nsString, nsString> AnnotationType;
  typedef std::vector<AnnotationType> VectorType;
  typedef VectorType::const_iterator IteratorType;

private:
  VectorType  mAnnotations;
};

BrowserHangAnnotations::BrowserHangAnnotations()
{
  MOZ_COUNT_CTOR(BrowserHangAnnotations);
}

BrowserHangAnnotations::~BrowserHangAnnotations()
{
  MOZ_COUNT_DTOR(BrowserHangAnnotations);
}

void
BrowserHangAnnotations::AddAnnotation(const nsAString& aName, const int32_t aData)
{
  nsString dataString;
  dataString.AppendInt(aData);
  AnnotationType annotation = std::make_pair(nsString(aName), dataString);
  mAnnotations.push_back(annotation);
}

void
BrowserHangAnnotations::AddAnnotation(const nsAString& aName, const double aData)
{
  nsString dataString;
  dataString.AppendFloat(aData);
  AnnotationType annotation = std::make_pair(nsString(aName), dataString);
  mAnnotations.push_back(annotation);
}

void
BrowserHangAnnotations::AddAnnotation(const nsAString& aName, const nsAString& aData)
{
  AnnotationType annotation = std::make_pair(nsString(aName), nsString(aData));
  mAnnotations.push_back(annotation);
}

void
BrowserHangAnnotations::AddAnnotation(const nsAString& aName, const nsACString& aData)
{
  nsString dataString;
  AppendUTF8toUTF16(aData, dataString);
  AnnotationType annotation = std::make_pair(nsString(aName), dataString);
  mAnnotations.push_back(annotation);
}

void
BrowserHangAnnotations::AddAnnotation(const nsAString& aName, const bool aData)
{
  nsString dataString;
  dataString += aData ? NS_LITERAL_STRING("true") : NS_LITERAL_STRING("false");
  AnnotationType annotation = std::make_pair(nsString(aName), dataString);
  mAnnotations.push_back(annotation);
}

/**
 * This class itself does not use synchronization but it (and its parent object)
 * should be protected by mutual exclusion in some way. In Telemetry the chrome
 * hang data is protected via TelemetryImpl::mHangReportsMutex.
 */
class ChromeHangAnnotationEnumerator : public HangAnnotations::Enumerator
{
public:
  explicit ChromeHangAnnotationEnumerator(const BrowserHangAnnotations::VectorType& aAnnotations);
  ~ChromeHangAnnotationEnumerator();

  virtual bool Next(nsAString& aOutName, nsAString& aOutValue);

private:
  BrowserHangAnnotations::IteratorType mIterator;
  BrowserHangAnnotations::IteratorType mEnd;
};

ChromeHangAnnotationEnumerator::ChromeHangAnnotationEnumerator(
                          const BrowserHangAnnotations::VectorType& aAnnotations)
  : mIterator(aAnnotations.begin())
  , mEnd(aAnnotations.end())
{
  MOZ_COUNT_CTOR(ChromeHangAnnotationEnumerator);
}

ChromeHangAnnotationEnumerator::~ChromeHangAnnotationEnumerator()
{
  MOZ_COUNT_DTOR(ChromeHangAnnotationEnumerator);
}

bool
ChromeHangAnnotationEnumerator::Next(nsAString& aOutName, nsAString& aOutValue)
{
  aOutName.Truncate();
  aOutValue.Truncate();
  if (mIterator == mEnd) {
    return false;
  }
  aOutName = mIterator->first;
  aOutValue = mIterator->second;
  ++mIterator;
  return true;
}

bool
BrowserHangAnnotations::IsEmpty() const
{
  return mAnnotations.empty();
}

size_t
BrowserHangAnnotations::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
  size_t result = sizeof(mAnnotations) +
                  mAnnotations.capacity() * sizeof(AnnotationType);
  for (IteratorType i = mAnnotations.begin(), e = mAnnotations.end(); i != e;
       ++i) {
    result += i->first.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
    result += i->second.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
  }

  return result;
}

UniquePtr<HangAnnotations::Enumerator>
BrowserHangAnnotations::GetEnumerator()
{
  if (mAnnotations.empty()) {
    return nullptr;
  }
  return MakeUnique<ChromeHangAnnotationEnumerator>(mAnnotations);
}

namespace Observer {

Annotators::Annotators()
  : mMutex("HangMonitor::Annotators::mMutex")
{
  MOZ_COUNT_CTOR(Annotators);
}

Annotators::~Annotators()
{
  MOZ_ASSERT(mAnnotators.empty());
  MOZ_COUNT_DTOR(Annotators);
}

bool
Annotators::Register(Annotator& aAnnotator)
{
  MutexAutoLock lock(mMutex);
  auto result = mAnnotators.insert(&aAnnotator);
  return result.second;
}

bool
Annotators::Unregister(Annotator& aAnnotator)
{
  MutexAutoLock lock(mMutex);
  DebugOnly<std::set<Annotator*>::size_type> numErased;
  numErased = mAnnotators.erase(&aAnnotator);
  MOZ_ASSERT(numErased == 1);
  return mAnnotators.empty();
}

UniquePtr<HangAnnotations>
Annotators::GatherAnnotations()
{
  auto annotations = MakeUnique<BrowserHangAnnotations>();
  { // Scope for lock
    MutexAutoLock lock(mMutex);
    for (std::set<Annotator*>::iterator i = mAnnotators.begin(),
                                        e = mAnnotators.end();
         i != e; ++i) {
      (*i)->AnnotateHang(*annotations);
    }
  }
  if (annotations->IsEmpty()) {
    return nullptr;
  }
  return Move(annotations);
}

} // namespace Observer

void
RegisterAnnotator(Annotator& aAnnotator)
{
  BackgroundHangMonitor::RegisterAnnotator(aAnnotator);
  // We still register annotators for ChromeHangs
  if (NS_IsMainThread() &&
      GeckoProcessType_Default == XRE_GetProcessType()) {
    if (!gChromehangAnnotators) {
      gChromehangAnnotators = new Observer::Annotators();
    }
    gChromehangAnnotators->Register(aAnnotator);
  }
}

void
UnregisterAnnotator(Annotator& aAnnotator)
{
  BackgroundHangMonitor::UnregisterAnnotator(aAnnotator);
  // We still register annotators for ChromeHangs
  if (NS_IsMainThread() &&
      GeckoProcessType_Default == XRE_GetProcessType()) {
    if (gChromehangAnnotators->Unregister(aAnnotator)) {
      gChromehangAnnotators = nullptr;
    }
  }
}

UniquePtr<HangAnnotations>
ChromeHangAnnotatorCallout()
{
  if (!gChromehangAnnotators) {
    return nullptr;
  }
  return gChromehangAnnotators->GatherAnnotations();
}

} // namespace HangMonitor
} // namespace mozilla