| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 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 mozilla_AutoProfilerLabel_h |
| 8 |
#define mozilla_AutoProfilerLabel_h |
| 9 |
|
| 10 |
#include "mozilla/Attributes.h" |
| 11 |
|
| 12 |
#include "mozilla/Types.h" |
| 13 |
#include <tuple> |
| 14 |
|
| 15 |
// The Gecko Profiler defines AutoProfilerLabel, an RAII class for |
| 16 |
// pushing/popping frames to/from the ProfilingStack. |
| 17 |
// |
| 18 |
// This file defines a class of the same name that does much the same thing, |
| 19 |
// but which can be used in (and only in) mozglue. A different class is |
| 20 |
// necessary because mozglue cannot directly access sProfilingStack. |
| 21 |
// |
| 22 |
// Note that this class is slightly slower than the other AutoProfilerLabel, |
| 23 |
// and it lacks the macro wrappers. It also is effectively hardwired to use |
| 24 |
// JS::ProfilingCategory::OTHER as the category pair, because that's what |
| 25 |
// the callbacks provided by the profiler use. (Specifying the categories in |
| 26 |
// this file would require #including ProfilingCategory.h in mozglue, which we |
| 27 |
// don't want to do.) |
| 28 |
|
| 29 |
namespace mozilla { |
| 30 |
|
| 31 |
// Enter should return a pointer that will be given to Exit. |
| 32 |
typedef void* (*ProfilerLabelEnter)(const char* aLabel, |
| 33 |
const char* aDynamicString, void* aSp); |
| 34 |
typedef void (*ProfilerLabelExit)(void* EntryContext); |
| 35 |
|
| 36 |
// Register callbacks that do the entry/exit work involving sProfilingStack. |
| 37 |
MFBT_API void RegisterProfilerLabelEnterExit(ProfilerLabelEnter aEnter, |
| 38 |
ProfilerLabelExit aExit); |
| 39 |
|
| 40 |
// This #ifdef prevents this AutoProfilerLabel from being defined in libxul, |
| 41 |
// which would conflict with the one in the profiler. |
| 42 |
#ifdef IMPL_MFBT |
| 43 |
|
| 44 |
class MOZ_RAII AutoProfilerLabel { |
| 45 |
public: |
| 46 |
AutoProfilerLabel(const char* aLabel, const char* aDynamicString); |
| 47 |
~AutoProfilerLabel(); |
| 48 |
|
| 49 |
private: |
| 50 |
void* mEntryContext; |
| 51 |
// Number of RegisterProfilerLabelEnterExit calls, to avoid giving an entry |
| 52 |
// context from one generation to the next. |
| 53 |
uint32_t mGeneration; |
| 54 |
}; |
| 55 |
|
| 56 |
using ProfilerLabel = std::tuple<void*, uint32_t>; |
| 57 |
|
| 58 |
bool IsProfilerPresent(); |
| 59 |
ProfilerLabel ProfilerLabelBegin(const char* aLabelName, |
| 60 |
const char* aDynamicString, void* aSp); |
| 61 |
void ProfilerLabelEnd(const ProfilerLabel& aLabel); |
| 62 |
|
| 63 |
inline bool IsValidProfilerLabel(const ProfilerLabel& aLabel) { |
| 64 |
return !!std::get<0>(aLabel); |
| 65 |
} |
| 66 |
|
| 67 |
#endif |
| 68 |
|
| 69 |
} // namespace mozilla |
| 70 |
|
| 71 |
#endif // mozilla_AutoProfilerLabel_h |