Bug 1666708 - Only store category pair in MarkerCategory - r=gregtatum

This saves 1 byte when serializing each marker (and removes all the code that was related to the 2nd byte).
Also it will be easier to use it in legacy code that only knows about the category pair.

Added unit tests for the whole of MarkerCategory.

Differential Revision: https://phabricator.services.mozilla.com/D91110
This commit is contained in:
Gerald Squelart 2020-09-24 03:23:28 +00:00
parent 6aebb9ec0a
commit 6b05ae158e
3 changed files with 77 additions and 18 deletions

View File

@ -264,7 +264,7 @@ template <typename NameCallback, typename StackCallback>
aWriter.IntElement(static_cast<int64_t>(options.Timing().MarkerPhase()));
aWriter.IntElement(static_cast<int64_t>(options.Category().Category()));
aWriter.IntElement(static_cast<int64_t>(options.Category().GetCategory()));
if (const auto tag =
aEntryReader.ReadObject<mozilla::base_profiler_markers_detail::
@ -409,14 +409,12 @@ struct ProfileBufferEntryReader::Deserializer<ProfilerStringView<CHAR>> {
template <>
struct ProfileBufferEntryWriter::Serializer<MarkerCategory> {
static Length Bytes(const MarkerCategory& aCategory) {
return ULEB128Size(static_cast<uint32_t>(aCategory.CategoryPair())) +
ULEB128Size(static_cast<uint32_t>(aCategory.Category()));
return ULEB128Size(static_cast<uint32_t>(aCategory.CategoryPair()));
}
static void Write(ProfileBufferEntryWriter& aEW,
const MarkerCategory& aCategory) {
aEW.WriteULEB128(static_cast<uint32_t>(aCategory.CategoryPair()));
aEW.WriteULEB128(static_cast<uint32_t>(aCategory.Category()));
}
};
@ -426,15 +424,11 @@ struct ProfileBufferEntryReader::Deserializer<MarkerCategory> {
MarkerCategory& aCategory) {
aCategory.mCategoryPair = static_cast<baseprofiler::ProfilingCategoryPair>(
aER.ReadULEB128<uint32_t>());
aCategory.mCategory = static_cast<baseprofiler::ProfilingCategory>(
aER.ReadULEB128<uint32_t>());
}
static MarkerCategory Read(ProfileBufferEntryReader& aER) {
return MarkerCategory(static_cast<baseprofiler::ProfilingCategoryPair>(
aER.ReadULEB128<uint32_t>()),
static_cast<baseprofiler::ProfilingCategory>(
aER.ReadULEB128<uint32_t>()));
aER.ReadULEB128<uint32_t>()));
}
};

View File

@ -218,16 +218,16 @@ class MarkerOptions;
class MarkerCategory {
public:
// Constructor from category pair (aka sub-category) and category.
constexpr MarkerCategory(baseprofiler::ProfilingCategoryPair aCategoryPair,
baseprofiler::ProfilingCategory aCategory)
: mCategoryPair(aCategoryPair), mCategory(aCategory) {}
constexpr explicit MarkerCategory(
baseprofiler::ProfilingCategoryPair aCategoryPair)
: mCategoryPair(aCategoryPair) {}
constexpr baseprofiler::ProfilingCategoryPair CategoryPair() const {
return mCategoryPair;
}
constexpr baseprofiler::ProfilingCategory Category() const {
return mCategory;
baseprofiler::ProfilingCategory GetCategory() const {
return GetProfilingCategoryPairInfo(mCategoryPair).mCategory;
}
// Create a MarkerOptions object from this category and options.
@ -245,8 +245,6 @@ class MarkerCategory {
baseprofiler::ProfilingCategoryPair mCategoryPair =
baseprofiler::ProfilingCategoryPair::OTHER;
baseprofiler::ProfilingCategory mCategory =
baseprofiler::ProfilingCategory::OTHER;
};
namespace baseprofiler::category {
@ -257,8 +255,7 @@ namespace baseprofiler::category {
// E.g.: `PROFILER_MARKER_UNTYPED("name", OTHER_Profiling)`
# define CATEGORY_ENUM_BEGIN_CATEGORY(name, labelAsString, color)
# define CATEGORY_ENUM_SUBCATEGORY(supercategory, name, labelAsString) \
static constexpr MarkerCategory name{ProfilingCategoryPair::name, \
ProfilingCategory::supercategory};
static constexpr MarkerCategory name{ProfilingCategoryPair::name};
# define CATEGORY_ENUM_END_CATEGORY
MOZ_PROFILING_CATEGORY_LIST(CATEGORY_ENUM_BEGIN_CATEGORY,
CATEGORY_ENUM_SUBCATEGORY,

View File

@ -3645,6 +3645,73 @@ void PrintMarkers(const mozilla::ProfileChunkedBuffer& aBuffer) {
StreamMarkers(aBuffer, writer);
}
static void SubTestMarkerCategory(
const mozilla::MarkerCategory& aMarkerCategory,
const mozilla::baseprofiler::ProfilingCategoryPair& aProfilingCategoryPair,
const mozilla::baseprofiler::ProfilingCategory& aProfilingCategory) {
MOZ_RELEASE_ASSERT(aMarkerCategory.CategoryPair() == aProfilingCategoryPair,
"Unexpected MarkerCategory::CategoryPair()");
MOZ_RELEASE_ASSERT(
mozilla::MarkerCategory(aProfilingCategoryPair).CategoryPair() ==
aProfilingCategoryPair,
"MarkerCategory(<name>).CategoryPair() should return <name>");
MOZ_RELEASE_ASSERT(aMarkerCategory.GetCategory() == aProfilingCategory,
"Unexpected MarkerCategory::GetCategory()");
mozilla::ProfileBufferChunkManagerSingle chunkManager(512);
mozilla::ProfileChunkedBuffer buffer(
mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager);
mozilla::ProfileBufferBlockIndex i = buffer.PutObject(aMarkerCategory);
MOZ_RELEASE_ASSERT(i != mozilla::ProfileBufferBlockIndex{},
"Failed serialization");
buffer.ReadEach([&](mozilla::ProfileBufferEntryReader& aER,
mozilla::ProfileBufferBlockIndex aIndex) {
MOZ_RELEASE_ASSERT(aIndex == i, "Unexpected deserialization index");
const auto readCategory = aER.ReadObject<mozilla::MarkerCategory>();
MOZ_RELEASE_ASSERT(aER.RemainingBytes() == 0,
"Unexpected extra serialized bytes");
MOZ_RELEASE_ASSERT(readCategory.CategoryPair() == aProfilingCategoryPair,
"Incorrect deserialization value");
});
}
void TestMarkerCategory() {
printf("TestMarkerCategory...\n");
mozilla::ProfileBufferChunkManagerSingle chunkManager(512);
mozilla::ProfileChunkedBuffer buffer(
mozilla::ProfileChunkedBuffer::ThreadSafety::WithoutMutex, chunkManager);
# define CATEGORY_ENUM_BEGIN_CATEGORY(name, labelAsString, color)
# define CATEGORY_ENUM_SUBCATEGORY(supercategory, name, labelAsString) \
static_assert( \
std::is_same_v<decltype(mozilla::baseprofiler::category::name), \
const mozilla::MarkerCategory>, \
"baseprofiler::category::<name> should be a const MarkerCategory"); \
\
SubTestMarkerCategory( \
mozilla::baseprofiler::category::name, \
mozilla::baseprofiler::ProfilingCategoryPair::name, \
mozilla::baseprofiler::ProfilingCategory::supercategory);
# define CATEGORY_ENUM_END_CATEGORY
MOZ_PROFILING_CATEGORY_LIST(CATEGORY_ENUM_BEGIN_CATEGORY,
CATEGORY_ENUM_SUBCATEGORY,
CATEGORY_ENUM_END_CATEGORY)
# undef CATEGORY_ENUM_BEGIN_CATEGORY
# undef CATEGORY_ENUM_SUBCATEGORY
# undef CATEGORY_ENUM_END_CATEGORY
static_assert(
std::is_same_v<decltype(
std::declval<mozilla::MarkerCategory>().WithOptions()),
mozilla::MarkerOptions>,
"MarkerCategory::WithOptions() should return a MarkerOptions");
printf("TestMarkerCategory done\n");
}
void TestMarkerNoPayload() {
printf("TestMarkerNoPayload...\n");
@ -3852,6 +3919,7 @@ void TestProfilerMarkers() {
mozilla::baseprofiler::profiler_current_thread_id());
// ::SleepMilli(10000);
TestMarkerCategory();
TestMarkerNoPayload();
TestUserMarker();
TestPredefinedMarkers();