DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
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
/* -*- 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"
#include "mozilla/BackgroundHangMonitor.h"

namespace mozilla {
namespace HangMonitor {

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

void HangAnnotations::AddAnnotation(const nsAString& aName,
                                    const int32_t aData) {
  nsAutoString dataString;
  dataString.AppendInt(aData);
  AppendElement(Annotation(aName, dataString));
}

void HangAnnotations::AddAnnotation(const nsAString& aName,
                                    const double aData) {
  nsAutoString dataString;
  dataString.AppendFloat(aData);
  AppendElement(Annotation(aName, dataString));
}

void HangAnnotations::AddAnnotation(const nsAString& aName,
                                    const nsAString& aData) {
  AppendElement(Annotation(aName, aData));
}

void HangAnnotations::AddAnnotation(const nsAString& aName,
                                    const nsACString& aData) {
  NS_ConvertUTF8toUTF16 dataString(aData);
  AppendElement(Annotation(aName, dataString));
}

void HangAnnotations::AddAnnotation(const nsAString& aName, const bool aData) {
  if (aData) {
    AppendElement(Annotation(aName, NS_LITERAL_STRING("true")));
  } else {
    AppendElement(Annotation(aName, NS_LITERAL_STRING("false")));
  }
}

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();
}

HangAnnotations Annotators::GatherAnnotations() {
  HangAnnotations annotations;
  {  // Scope for lock
    MutexAutoLock lock(mMutex);
    for (std::set<Annotator*>::iterator i = mAnnotators.begin(),
                                        e = mAnnotators.end();
         i != e; ++i) {
      (*i)->AnnotateHang(annotations);
    }
  }
  return 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;
    }
  }
}

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

}  // namespace HangMonitor
}  // namespace mozilla

namespace IPC {

using mozilla::HangMonitor::Annotation;

void ParamTraits<Annotation>::Write(Message* aMsg, const Annotation& aParam) {
  WriteParam(aMsg, aParam.mName);
  WriteParam(aMsg, aParam.mValue);
}

bool ParamTraits<Annotation>::Read(const Message* aMsg, PickleIterator* aIter,
                                   Annotation* aResult) {
  if (!ReadParam(aMsg, aIter, &aResult->mName)) {
    return false;
  }
  if (!ReadParam(aMsg, aIter, &aResult->mValue)) {
    return false;
  }
  return true;
}

}  // namespace IPC