Bug 1646266 - {BASE_,}PROFILER_MARKER{,_TEXT} - r=gregtatum

This is the main public marker API:
- `AddMarkerToBuffer` can be used to store a marker to any buffer. This could be useful to code that wants to store markers outside of the default profiler buffers.
- `baseprofiler::AddMarker`/`profiler_add_marker` store a marker in the appropriate profiler buffer.
- BASE_PROFILER_MARKER and PROFILER_MARKER do the same, but are also defined (and empty) when MOZ_GECKO_PROFILER is not #defined.
All these take a name, marker options, a marker type, and the type's expected arguments if any (as expected by the `StreamJSONMarkerData` function).

Extra helpers for the most common types:
- BASE_PROFILER_MARKER_UNTYPED and PROFILER_MARKER_UNTYPED store a marker with no data payload.
- BASE_PROFILER_MARKER_TEXT and PROFILER_MARKER_TEXT store a text marker. `baseprofiler::markers::Text` is an example of how new marker types can be defined.

Differential Revision: https://phabricator.services.mozilla.com/D87257
This commit is contained in:
Gerald Squelart 2020-09-02 04:03:20 +00:00
Родитель 061abe0d50
Коммит e6bd850ec0
2 изменённых файлов: 177 добавлений и 0 удалений

Просмотреть файл

@ -4,15 +4,110 @@
* 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/. */
// This header contains basic definitions necessary to create marker types, and
// to add markers to the profiler buffers.
//
// If basic marker types are needed, #include
// "mozilla/BaseProfilerMarkerTypes.h" instead.
//
// But if you want to create your own marker type locally, you can #include this
// header only; look at mozilla/BaseProfilerMarkerTypes.h for examples of how to
// define types, and mozilla/BaseProfilerMarkerPrerequisites.h for some
// supporting types.
//
// To then record markers:
// - Use `baseprofiler::AddMarker<ChosenMarkerType>(...)` from
// mozglue or other libraries that are outside of xul, especially if they may
// happen outside of xpcom's lifetime (typically startup, shutdown, or tests).
// - Otherwise #include "ProfilerMarkers.h" instead, and use
// `profiler_add_marker<ChosenMarkerType>(...)`.
// See these functions for more details.
#ifndef BaseProfilerMarkers_h
#define BaseProfilerMarkers_h
#include "mozilla/BaseProfilerMarkersDetail.h"
// TODO: Move common stuff to shared header instead.
#include "BaseProfiler.h"
#ifndef MOZ_GECKO_PROFILER
# define BASE_PROFILER_MARKER_UNTYPED(markerName, options)
# define BASE_PROFILER_MARKER(markerName, options, MarkerType, ...)
# define BASE_PROFILER_MARKER_TEXT(markerName, options, text)
#else // ndef MOZ_GECKO_PROFILER
# include "mozilla/ProfileChunkedBuffer.h"
# include "mozilla/TimeStamp.h"
# include <functional>
# include <string>
# include <utility>
namespace mozilla::baseprofiler {
template <typename MarkerType = ::mozilla::baseprofiler::markers::NoPayload,
typename... Ts>
ProfileBufferBlockIndex AddMarkerToBuffer(ProfileChunkedBuffer& aBuffer,
const ProfilerString8View& aName,
MarkerOptions&& aOptions,
const Ts&... aTs) {
return base_profiler_markers_detail::AddMarkerToBuffer<MarkerType>(
aBuffer, aName, std::move(aOptions),
::mozilla::baseprofiler::profiler_capture_backtrace_into, aTs...);
}
template <typename MarkerType = ::mozilla::baseprofiler::markers::NoPayload,
typename... Ts>
ProfileBufferBlockIndex AddMarker(const ProfilerString8View& aName,
MarkerOptions&& aOptions, const Ts&... aTs) {
if (!baseprofiler::profiler_can_accept_markers()) {
return {};
}
return ::mozilla::baseprofiler::AddMarkerToBuffer<MarkerType>(
base_profiler_markers_detail::CachedBaseCoreBuffer(), aName,
std::move(aOptions), aTs...);
}
} // namespace mozilla::baseprofiler
# define BASE_PROFILER_MARKER_UNTYPED(markerName, options) \
do { \
AUTO_PROFILER_STATS(base_add_marker_v2); \
::mozilla::baseprofiler::AddMarker<>( \
markerName, ::mozilla::baseprofiler::category::options); \
} while (false)
# define BASE_PROFILER_MARKER(markerName, options, MarkerType, ...) \
do { \
AUTO_PROFILER_STATS(base_add_marker_v2_with_##MarkerType); \
::mozilla::baseprofiler::AddMarker< \
::mozilla::baseprofiler::markers::MarkerType>( \
markerName, ::mozilla::baseprofiler::category::options, \
##__VA_ARGS__); \
} while (false)
namespace mozilla::baseprofiler::markers {
// Most common marker type. Others are in BaseProfilerMarkerTypes.h.
struct Text {
static constexpr const char* MarkerTypeName() { return "Text"; }
static void StreamJSONMarkerData(JSONWriter& aWriter,
const ProfilerString8View& aText) {
aWriter.StringProperty("name", aText.String().c_str());
}
};
} // namespace mozilla::baseprofiler::markers
# define BASE_PROFILER_MARKER_TEXT(markerName, options, text) \
do { \
AUTO_PROFILER_STATS(base_add_marker_v2_with_Text); \
::mozilla::baseprofiler::AddMarker< \
::mozilla::baseprofiler::markers::Text>( \
markerName, ::mozilla::baseprofiler::category::options, text); \
} while (false)
#endif // nfed MOZ_GECKO_PROFILER else
#endif // BaseProfilerMarkers_h

Просмотреть файл

@ -4,16 +4,98 @@
* 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/. */
// This header contains definitions necessary to add markers to the Gecko
// Profiler buffer.
//
// It #include's "mozilla/BaseProfilerMarkers.h", see that header for base
// definitions necessary to create marker types.
//
// If common marker types are needed, #include "ProfilerMarkerTypes.h" instead.
//
// But if you want to create your own marker type locally, you can #include this
// header only; look at ProfilerMarkerTypes.h for examples of how to define
// types.
//
// To then record markers:
// - Use `baseprofiler::AddMarker<ChosenMarkerType>(...)` from
// mozglue or other libraries that are outside of xul, especially if they may
// happen outside of xpcom's lifetime (typically startup, shutdown, or tests).
// - Otherwise #include "ProfilerMarkers.h" instead, and use
// `profiler_add_marker<ChosenMarkerType>(...)`.
// See these functions for more details.
#ifndef ProfilerMarkers_h
#define ProfilerMarkers_h
#include "mozilla/BaseProfilerMarkers.h"
#include "mozilla/ProfilerMarkersDetail.h"
// TODO: Move common stuff to shared header instead.
#include "GeckoProfiler.h"
#ifndef MOZ_GECKO_PROFILER
# define PROFILER_MARKER_UNTYPED(markerName, options)
# define PROFILER_MARKER(markerName, options, MarkerType, ...)
# define PROFILER_MARKER_TEXT(markerName, options, text)
#else // ndef MOZ_GECKO_PROFILER
// Bring category names from Base Profiler into the geckoprofiler::category
// namespace, for consistency with other Gecko Profiler identifiers.
namespace geckoprofiler::category {
using namespace ::mozilla::baseprofiler::category;
}
template <typename MarkerType = ::mozilla::baseprofiler::markers::NoPayload,
typename... Ts>
mozilla::ProfileBufferBlockIndex AddMarkerToBuffer(
mozilla::ProfileChunkedBuffer& aBuffer,
const mozilla::ProfilerString8View& aName,
mozilla::MarkerOptions&& aOptions, const Ts&... aTs) {
return mozilla::base_profiler_markers_detail::AddMarkerToBuffer<MarkerType>(
aBuffer, aName, std::move(aOptions), ::profiler_capture_backtrace_into,
aTs...);
}
template <typename MarkerType = ::mozilla::baseprofiler::markers::NoPayload,
typename... Ts>
mozilla::ProfileBufferBlockIndex profiler_add_marker(
const mozilla::ProfilerString8View& aName,
mozilla::MarkerOptions&& aOptions, const Ts&... aTs) {
if (!profiler_can_accept_markers()) {
return {};
}
return ::AddMarkerToBuffer<MarkerType>(
profiler_markers_detail::CachedCoreBuffer(), aName, std::move(aOptions),
aTs...);
}
# define PROFILER_MARKER_UNTYPED(markerName, options) \
do { \
AUTO_PROFILER_STATS(add_marker_v2); \
::profiler_add_marker<>(markerName, ::geckoprofiler::category::options); \
} while (false)
# define PROFILER_MARKER(markerName, options, MarkerType, ...) \
do { \
AUTO_PROFILER_STATS(add_marker_v2_with_##MarkerType); \
::profiler_add_marker<::profilermarkers::MarkerType>( \
markerName, ::geckoprofiler::category::options, ##__VA_ARGS__); \
} while (false)
namespace profilermarkers {
// Most common marker type. Others are in ProfilerMarkerTypes.h.
using Text = ::mozilla::baseprofiler::markers::Text;
} // namespace profilermarkers
# define PROFILER_MARKER_TEXT(markerName, options, text) \
do { \
AUTO_PROFILER_STATS(add_marker_v2_with_Text); \
::profiler_add_marker<::geckoprofiler::markers::Text>( \
markerName, ::geckoprofiler::category::options, text); \
} while (false)
#endif // nfed MOZ_GECKO_PROFILER else
#endif // ProfilerMarkers_h