Improve debug counters to understand integer keys.

With this new understanding it can sort and align the integers better.

PiperOrigin-RevId: 682449015
pull/18619/head
Protobuf Team Bot 5 months ago committed by Copybara-Service
parent 1481ea06e0
commit 6965653714
  1. 1
      src/google/protobuf/BUILD.bazel
  2. 26
      src/google/protobuf/debug_counter_test.cc
  3. 23
      src/google/protobuf/port.cc

@ -283,6 +283,7 @@ cc_library(
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:variant",
],
)

@ -24,12 +24,16 @@ using testing::Not;
auto MatchOutput(bool expect_output) {
const auto header = HasSubstr("Protobuf debug counters:");
const auto foo = HasSubstr("Foo :");
const auto bar = HasSubstr("Bar : 1 (33.33%)");
const auto baz = HasSubstr("Baz : 2 (66.67%)");
const auto total = HasSubstr("Total : 3");
return expect_output ? testing::Matcher<const std::string&>(
AllOf(header, foo, bar, baz, total))
const auto all = AllOf(header, //
HasSubstr("Foo :"),
HasSubstr(" Bar : 1 (33.33%)"),
HasSubstr(" Baz : 2 (66.67%)"),
HasSubstr(" Total : 3"), //
HasSubstr("Num :"),
HasSubstr(" 32 : 3 (75.00%)"),
HasSubstr(" 128 : 1 (25.00%)"),
HasSubstr(" Total : 4"));
return expect_output ? testing::Matcher<const std::string&>(all)
: testing::Matcher<const std::string&>(Not(header));
}
@ -39,9 +43,15 @@ TEST(DebugCounterTest, RealProvidesReportAtExit) {
{
static google::protobuf::internal::RealDebugCounter counter1("Foo.Bar");
static google::protobuf::internal::RealDebugCounter counter2("Foo.Baz");
static google::protobuf::internal::RealDebugCounter counter3("Num.32");
static google::protobuf::internal::RealDebugCounter counter4("Num.128");
counter1.Inc();
counter2.Inc();
counter2.Inc();
counter3.Inc();
counter3.Inc();
counter3.Inc();
counter4.Inc();
exit(0);
},
ExitedWithCode(0), MatchOutput(true));
@ -79,6 +89,10 @@ TEST(DebugCounterTest, MacroProvidesReportAtExitDependingOnBuild) {
for (int i = 0; i < 2; ++i) {
PROTOBUF_DEBUG_COUNTER("Foo.Baz").Inc();
}
for (int i = 0; i < 3; ++i) {
PROTOBUF_DEBUG_COUNTER("Num.32").Inc();
}
PROTOBUF_DEBUG_COUNTER("Num.128").Inc();
exit(0);
},
ExitedWithCode(0), MatchOutput(match_output));

@ -7,14 +7,17 @@
//
#include "google/protobuf/port.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <utility>
#include "absl/strings/numbers.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/types/variant.h"
// Must be included last
#include "google/protobuf/port_def.inc"
@ -35,7 +38,8 @@ void protobuf_assumption_failed(const char* pred, const char* file, int line) {
static void PrintAllCounters();
static auto& CounterMap() {
using Map = std::map<absl::string_view,
std::map<absl::string_view, const RealDebugCounter*>>;
std::map<absl::variant<int64_t, absl::string_view>,
const RealDebugCounter*>>;
static auto* counter_map = new Map{};
static bool dummy = std::atexit(PrintAllCounters);
(void)dummy;
@ -60,7 +64,15 @@ static void PrintAllCounters() {
}
for (auto& count : category.second) {
size_t value = count.second->value();
absl::FPrintF(stderr, " %-10s: %10zu", count.first, value);
if (absl::holds_alternative<int64_t>(count.first)) {
// For integers, right align
absl::FPrintF(stderr, " %9d : %10zu",
absl::get<int64_t>(count.first), value);
} else {
// For strings, left align
absl::FPrintF(stderr, " %-10s: %10zu",
absl::get<absl::string_view>(count.first), value);
}
if (total != 0 && category.second.size() > 1) {
absl::FPrintF(
stderr, " (%5.2f%%)",
@ -77,7 +89,12 @@ static void PrintAllCounters() {
void RealDebugCounter::Register(absl::string_view name) {
std::pair<absl::string_view, absl::string_view> parts =
absl::StrSplit(name, '.');
CounterMap()[parts.first][parts.second] = this;
int64_t as_int;
if (absl::SimpleAtoi(parts.second, &as_int)) {
CounterMap()[parts.first][as_int] = this;
} else {
CounterMap()[parts.first][parts.second] = this;
}
}
} // namespace internal

Loading…
Cancel
Save