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