Fix incorrect max retry calculation. Bug: 160681127 (this is a cherry-pick of 5cffa459a75e4875ddf4c57c9efb19bef3cd3d9f) Change-Id: I64ed9ffc30978e21662f6be6f32b3c8fa7dd364f Merged-In: I64ed9ffc30978e21662f6be6f32b3c8fa7dd364f
diff --git a/src/profiling/memory/client.cc b/src/profiling/memory/client.cc index a194f75..d7ea370 100644 --- a/src/profiling/memory/client.cc +++ b/src/profiling/memory/client.cc
@@ -86,19 +86,17 @@ return 0; } -constexpr uint64_t kInfiniteTries = 0; +} // namespace uint64_t GetMaxTries(const ClientConfiguration& client_config) { if (!client_config.block_client) return 1u; if (client_config.block_client_timeout_us == 0) return kInfiniteTries; - return std::min<uint64_t>( + return std::max<uint64_t>( 1ul, client_config.block_client_timeout_us / kResendBackoffUs); } -} // namespace - const char* GetThreadStackBase() { pthread_attr_t attr; if (pthread_getattr_np(pthread_self(), &attr) != 0)
diff --git a/src/profiling/memory/client.h b/src/profiling/memory/client.h index c6e581e..0ef00be 100644 --- a/src/profiling/memory/client.h +++ b/src/profiling/memory/client.h
@@ -35,8 +35,10 @@ namespace perfetto { namespace profiling { +uint64_t GetMaxTries(const ClientConfiguration& client_config); const char* GetThreadStackBase(); +constexpr uint64_t kInfiniteTries = 0; constexpr uint32_t kClientSockTimeoutMs = 1000; // Profiling client, used to sample and record the malloc/free family of calls,
diff --git a/src/profiling/memory/client_unittest.cc b/src/profiling/memory/client_unittest.cc index 68b2ee7..62f0909 100644 --- a/src/profiling/memory/client_unittest.cc +++ b/src/profiling/memory/client_unittest.cc
@@ -20,6 +20,7 @@ #include "perfetto/base/thread_utils.h" #include "perfetto/ext/base/unix_socket.h" +#include "src/profiling/memory/wire_protocol.h" #include "test/gtest_and_gmock.h" namespace perfetto { @@ -50,6 +51,41 @@ th.join(); } +TEST(ClientTest, GetMaxTriesBlock) { + ClientConfiguration cfg = {}; + cfg.block_client = true; + cfg.block_client_timeout_us = 200; + EXPECT_EQ(GetMaxTries(cfg), 2u); +} + +TEST(ClientTest, GetMaxTriesBlockSmall) { + ClientConfiguration cfg = {}; + cfg.block_client = true; + cfg.block_client_timeout_us = 99; + EXPECT_EQ(GetMaxTries(cfg), 1u); +} + +TEST(ClientTest, GetMaxTriesBlockVerySmall) { + ClientConfiguration cfg = {}; + cfg.block_client = true; + cfg.block_client_timeout_us = 1; + EXPECT_EQ(GetMaxTries(cfg), 1u); +} + +TEST(ClientTest, GetMaxTriesBlockInfinite) { + ClientConfiguration cfg = {}; + cfg.block_client = true; + cfg.block_client_timeout_us = 0; + EXPECT_EQ(GetMaxTries(cfg), kInfiniteTries); +} + +TEST(ClientTest, GetMaxTriesNoBlock) { + ClientConfiguration cfg = {}; + cfg.block_client = false; + cfg.block_client_timeout_us = 200; + EXPECT_EQ(GetMaxTries(cfg), 1u); +} + } // namespace } // namespace profiling } // namespace perfetto