1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/debug/thread_heap_usage_tracker.h"
#include <map>
#include "base/allocator/allocator_shim.h"
#include "base/allocator/features.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace debug {
namespace {
class TestingThreadHeapUsageTracker : public ThreadHeapUsageTracker {
public:
using ThreadHeapUsageTracker::DisableHeapTrackingForTesting;
using ThreadHeapUsageTracker::EnsureTLSInitialized;
using ThreadHeapUsageTracker::GetDispatchForTesting;
};
// A fixture class that allows testing the AllocatorDispatch associated with
// the ThreadHeapUsageTracker class in isolation against a mocked
// underlying
// heap implementation.
class ThreadHeapUsageTrackerTest : public testing::Test {
public:
using AllocatorDispatch = base::allocator::AllocatorDispatch;
static const size_t kAllocationPadding;
enum SizeFunctionKind {
EXACT_SIZE_FUNCTION,
PADDING_SIZE_FUNCTION,
ZERO_SIZE_FUNCTION,
};
ThreadHeapUsageTrackerTest() : size_function_kind_(EXACT_SIZE_FUNCTION) {
EXPECT_EQ(nullptr, g_self);
g_self = this;
}
~ThreadHeapUsageTrackerTest() override {
EXPECT_EQ(this, g_self);
g_self = nullptr;
}
void set_size_function_kind(SizeFunctionKind kind) {
size_function_kind_ = kind;
}
void SetUp() override {
TestingThreadHeapUsageTracker::EnsureTLSInitialized();
dispatch_under_test_ =
TestingThreadHeapUsageTracker::GetDispatchForTesting();
ASSERT_EQ(nullptr, dispatch_under_test_->next);
dispatch_under_test_->next = &g_mock_dispatch;
}
void TearDown() override {
ASSERT_EQ(&g_mock_dispatch, dispatch_under_test_->next);
dispatch_under_test_->next = nullptr;
}
void* MockMalloc(size_t size) {
return dispatch_under_test_->alloc_function(dispatch_under_test_, size);
}
void* MockCalloc(size_t n, size_t size) {
return dispatch_under_test_->alloc_zero_initialized_function(
dispatch_under_test_, n, size);
}
void* MockAllocAligned(size_t alignment, size_t size) {
return dispatch_under_test_->alloc_aligned_function(dispatch_under_test_,
alignment, size);
}
void* MockRealloc(void* address, size_t size) {
return dispatch_under_test_->realloc_function(dispatch_under_test_, address,
size);
}
void MockFree(void* address) {
dispatch_under_test_->free_function(dispatch_under_test_, address);
}
size_t MockGetSizeEstimate(void* address) {
return dispatch_under_test_->get_size_estimate_function(
dispatch_under_test_, address);
}
private:
void RecordAlloc(void* address, size_t size) {
if (address != nullptr)
allocation_size_map_[address] = size;
}
void DeleteAlloc(void* address) {
if (address != nullptr)
EXPECT_EQ(1U, allocation_size_map_.erase(address));
}
size_t GetSizeEstimate(void* address) {
auto it = allocation_size_map_.find(address);
if (it == allocation_size_map_.end())
return 0;
size_t ret = it->second;
switch (size_function_kind_) {
case EXACT_SIZE_FUNCTION:
break;
case PADDING_SIZE_FUNCTION:
ret += kAllocationPadding;
break;
case ZERO_SIZE_FUNCTION:
ret = 0;
break;
}
return ret;
}
static void* OnAllocFn(const AllocatorDispatch* self, size_t size) {
EXPECT_EQ(&g_mock_dispatch, self);
void* ret = malloc(size);
g_self->RecordAlloc(ret, size);
return ret;
}
static void* OnAllocZeroInitializedFn(const AllocatorDispatch* self,
size_t n,
size_t size) {
EXPECT_EQ(&g_mock_dispatch, self);
void* ret = calloc(n, size);
g_self->RecordAlloc(ret, n * size);
return ret;
}
static void* OnAllocAlignedFn(const AllocatorDispatch* self,
size_t alignment,
size_t size) {
EXPECT_EQ(&g_mock_dispatch, self);
// This is a cheat as it doesn't return aligned allocations. This has the
// advantage of working for all platforms for this test.
void* ret = malloc(size);
g_self->RecordAlloc(ret, size);
return ret;
}
static void* OnReallocFn(const AllocatorDispatch* self,
void* address,
size_t size) {
EXPECT_EQ(&g_mock_dispatch, self);
g_self->DeleteAlloc(address);
void* ret = realloc(address, size);
g_self->RecordAlloc(ret, size);
return ret;
}
static void OnFreeFn(const AllocatorDispatch* self, void* address) {
EXPECT_EQ(&g_mock_dispatch, self);
g_self->DeleteAlloc(address);
free(address);
}
static size_t OnGetSizeEstimateFn(const AllocatorDispatch* self,
void* address) {
EXPECT_EQ(&g_mock_dispatch, self);
return g_self->GetSizeEstimate(address);
}
using AllocationSizeMap = std::map<void*, size_t>;
SizeFunctionKind size_function_kind_;
AllocationSizeMap allocation_size_map_;
AllocatorDispatch* dispatch_under_test_;
static base::allocator::AllocatorDispatch g_mock_dispatch;
static ThreadHeapUsageTrackerTest* g_self;
};
const size_t ThreadHeapUsageTrackerTest::kAllocationPadding = 23;
ThreadHeapUsageTrackerTest* ThreadHeapUsageTrackerTest::g_self = nullptr;
base::allocator::AllocatorDispatch ThreadHeapUsageTrackerTest::g_mock_dispatch =
{
&ThreadHeapUsageTrackerTest::OnAllocFn, // alloc_function
&ThreadHeapUsageTrackerTest::
OnAllocZeroInitializedFn, // alloc_zero_initialized_function
&ThreadHeapUsageTrackerTest::
OnAllocAlignedFn, // alloc_aligned_function
&ThreadHeapUsageTrackerTest::OnReallocFn, // realloc_function
&ThreadHeapUsageTrackerTest::OnFreeFn, // free_function
&ThreadHeapUsageTrackerTest::
OnGetSizeEstimateFn, // get_size_estimate_function
nullptr, // next
};
} // namespace
TEST_F(ThreadHeapUsageTrackerTest, SimpleUsageWithExactSizeFunction) {
set_size_function_kind(EXACT_SIZE_FUNCTION);
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
ThreadHeapUsage u1 = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(0U, u1.alloc_ops);
EXPECT_EQ(0U, u1.alloc_bytes);
EXPECT_EQ(0U, u1.alloc_overhead_bytes);
EXPECT_EQ(0U, u1.free_ops);
EXPECT_EQ(0U, u1.free_bytes);
EXPECT_EQ(0U, u1.max_allocated_bytes);
const size_t kAllocSize = 1029U;
void* ptr = MockMalloc(kAllocSize);
MockFree(ptr);
usage_tracker.Stop(false);
ThreadHeapUsage u2 = usage_tracker.usage();
EXPECT_EQ(1U, u2.alloc_ops);
EXPECT_EQ(kAllocSize, u2.alloc_bytes);
EXPECT_EQ(0U, u2.alloc_overhead_bytes);
EXPECT_EQ(1U, u2.free_ops);
EXPECT_EQ(kAllocSize, u2.free_bytes);
EXPECT_EQ(kAllocSize, u2.max_allocated_bytes);
}
TEST_F(ThreadHeapUsageTrackerTest, SimpleUsageWithPaddingSizeFunction) {
set_size_function_kind(PADDING_SIZE_FUNCTION);
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
ThreadHeapUsage u1 = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(0U, u1.alloc_ops);
EXPECT_EQ(0U, u1.alloc_bytes);
EXPECT_EQ(0U, u1.alloc_overhead_bytes);
EXPECT_EQ(0U, u1.free_ops);
EXPECT_EQ(0U, u1.free_bytes);
EXPECT_EQ(0U, u1.max_allocated_bytes);
const size_t kAllocSize = 1029U;
void* ptr = MockMalloc(kAllocSize);
MockFree(ptr);
usage_tracker.Stop(false);
ThreadHeapUsage u2 = usage_tracker.usage();
EXPECT_EQ(1U, u2.alloc_ops);
EXPECT_EQ(kAllocSize + kAllocationPadding, u2.alloc_bytes);
EXPECT_EQ(kAllocationPadding, u2.alloc_overhead_bytes);
EXPECT_EQ(1U, u2.free_ops);
EXPECT_EQ(kAllocSize + kAllocationPadding, u2.free_bytes);
EXPECT_EQ(kAllocSize + kAllocationPadding, u2.max_allocated_bytes);
}
TEST_F(ThreadHeapUsageTrackerTest, SimpleUsageWithZeroSizeFunction) {
set_size_function_kind(ZERO_SIZE_FUNCTION);
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
ThreadHeapUsage u1 = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(0U, u1.alloc_ops);
EXPECT_EQ(0U, u1.alloc_bytes);
EXPECT_EQ(0U, u1.alloc_overhead_bytes);
EXPECT_EQ(0U, u1.free_ops);
EXPECT_EQ(0U, u1.free_bytes);
EXPECT_EQ(0U, u1.max_allocated_bytes);
const size_t kAllocSize = 1029U;
void* ptr = MockMalloc(kAllocSize);
MockFree(ptr);
usage_tracker.Stop(false);
ThreadHeapUsage u2 = usage_tracker.usage();
// With a get-size function that returns zero, there's no way to get the size
// of an allocation that's being freed, hence the shim can't tally freed bytes
// nor the high-watermark allocated bytes.
EXPECT_EQ(1U, u2.alloc_ops);
EXPECT_EQ(kAllocSize, u2.alloc_bytes);
EXPECT_EQ(0U, u2.alloc_overhead_bytes);
EXPECT_EQ(1U, u2.free_ops);
EXPECT_EQ(0U, u2.free_bytes);
EXPECT_EQ(0U, u2.max_allocated_bytes);
}
TEST_F(ThreadHeapUsageTrackerTest, ReallocCorrectlyTallied) {
const size_t kAllocSize = 237U;
{
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
// Reallocating nullptr should count as a single alloc.
void* ptr = MockRealloc(nullptr, kAllocSize);
ThreadHeapUsage usage = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(1U, usage.alloc_ops);
EXPECT_EQ(kAllocSize, usage.alloc_bytes);
EXPECT_EQ(0U, usage.alloc_overhead_bytes);
EXPECT_EQ(0U, usage.free_ops);
EXPECT_EQ(0U, usage.free_bytes);
EXPECT_EQ(kAllocSize, usage.max_allocated_bytes);
// Reallocating a valid pointer to a zero size should count as a single
// free.
ptr = MockRealloc(ptr, 0U);
usage_tracker.Stop(false);
EXPECT_EQ(1U, usage_tracker.usage().alloc_ops);
EXPECT_EQ(kAllocSize, usage_tracker.usage().alloc_bytes);
EXPECT_EQ(0U, usage_tracker.usage().alloc_overhead_bytes);
EXPECT_EQ(1U, usage_tracker.usage().free_ops);
EXPECT_EQ(kAllocSize, usage_tracker.usage().free_bytes);
EXPECT_EQ(kAllocSize, usage_tracker.usage().max_allocated_bytes);
// Realloc to zero size may or may not return a nullptr - make sure to
// free the zero-size alloc in the latter case.
if (ptr != nullptr)
MockFree(ptr);
}
{
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
void* ptr = MockMalloc(kAllocSize);
ThreadHeapUsage usage = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(1U, usage.alloc_ops);
// Now try reallocating a valid pointer to a larger size, this should count
// as one free and one alloc.
const size_t kLargerAllocSize = kAllocSize + 928U;
ptr = MockRealloc(ptr, kLargerAllocSize);
usage_tracker.Stop(false);
EXPECT_EQ(2U, usage_tracker.usage().alloc_ops);
EXPECT_EQ(kAllocSize + kLargerAllocSize, usage_tracker.usage().alloc_bytes);
EXPECT_EQ(0U, usage_tracker.usage().alloc_overhead_bytes);
EXPECT_EQ(1U, usage_tracker.usage().free_ops);
EXPECT_EQ(kAllocSize, usage_tracker.usage().free_bytes);
EXPECT_EQ(kLargerAllocSize, usage_tracker.usage().max_allocated_bytes);
MockFree(ptr);
}
}
TEST_F(ThreadHeapUsageTrackerTest, NestedMaxWorks) {
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
const size_t kOuterAllocSize = 1029U;
void* ptr = MockMalloc(kOuterAllocSize);
MockFree(ptr);
EXPECT_EQ(kOuterAllocSize,
ThreadHeapUsageTracker::GetUsageSnapshot().max_allocated_bytes);
{
ThreadHeapUsageTracker inner_usage_tracker;
inner_usage_tracker.Start();
const size_t kInnerAllocSize = 673U;
ptr = MockMalloc(kInnerAllocSize);
MockFree(ptr);
inner_usage_tracker.Stop(false);
EXPECT_EQ(kInnerAllocSize, inner_usage_tracker.usage().max_allocated_bytes);
}
// The greater, outer allocation size should have been restored.
EXPECT_EQ(kOuterAllocSize,
ThreadHeapUsageTracker::GetUsageSnapshot().max_allocated_bytes);
const size_t kLargerInnerAllocSize = kOuterAllocSize + 673U;
{
ThreadHeapUsageTracker inner_usage_tracker;
inner_usage_tracker.Start();
ptr = MockMalloc(kLargerInnerAllocSize);
MockFree(ptr);
inner_usage_tracker.Stop(false);
EXPECT_EQ(kLargerInnerAllocSize,
inner_usage_tracker.usage().max_allocated_bytes);
}
// The greater, inner allocation size should have been preserved.
EXPECT_EQ(kLargerInnerAllocSize,
ThreadHeapUsageTracker::GetUsageSnapshot().max_allocated_bytes);
// Now try the case with an outstanding net alloc size when entering the
// inner scope.
void* outer_ptr = MockMalloc(kOuterAllocSize);
EXPECT_EQ(kLargerInnerAllocSize,
ThreadHeapUsageTracker::GetUsageSnapshot().max_allocated_bytes);
{
ThreadHeapUsageTracker inner_usage_tracker;
inner_usage_tracker.Start();
ptr = MockMalloc(kLargerInnerAllocSize);
MockFree(ptr);
inner_usage_tracker.Stop(false);
EXPECT_EQ(kLargerInnerAllocSize,
inner_usage_tracker.usage().max_allocated_bytes);
}
// While the inner scope saw only the inner net outstanding allocation size,
// the outer scope saw both outstanding at the same time.
EXPECT_EQ(kOuterAllocSize + kLargerInnerAllocSize,
ThreadHeapUsageTracker::GetUsageSnapshot().max_allocated_bytes);
MockFree(outer_ptr);
// Test a net-negative scope.
ptr = MockMalloc(kLargerInnerAllocSize);
{
ThreadHeapUsageTracker inner_usage_tracker;
inner_usage_tracker.Start();
MockFree(ptr);
const size_t kInnerAllocSize = 1;
ptr = MockMalloc(kInnerAllocSize);
inner_usage_tracker.Stop(false);
// Since the scope is still net-negative, the max is clamped at zero.
EXPECT_EQ(0U, inner_usage_tracker.usage().max_allocated_bytes);
}
MockFree(ptr);
}
TEST_F(ThreadHeapUsageTrackerTest, NoStopImpliesInclusive) {
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
const size_t kOuterAllocSize = 1029U;
void* ptr = MockMalloc(kOuterAllocSize);
MockFree(ptr);
ThreadHeapUsage usage = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(kOuterAllocSize, usage.max_allocated_bytes);
const size_t kInnerLargerAllocSize = kOuterAllocSize + 673U;
{
ThreadHeapUsageTracker inner_usage_tracker;
inner_usage_tracker.Start();
// Make a larger allocation than the outer scope.
ptr = MockMalloc(kInnerLargerAllocSize);
MockFree(ptr);
// inner_usage_tracker goes out of scope without a Stop().
}
ThreadHeapUsage current = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(usage.alloc_ops + 1, current.alloc_ops);
EXPECT_EQ(usage.alloc_bytes + kInnerLargerAllocSize, current.alloc_bytes);
EXPECT_EQ(usage.free_ops + 1, current.free_ops);
EXPECT_EQ(usage.free_bytes + kInnerLargerAllocSize, current.free_bytes);
EXPECT_EQ(kInnerLargerAllocSize, current.max_allocated_bytes);
}
TEST_F(ThreadHeapUsageTrackerTest, ExclusiveScopesWork) {
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
const size_t kOuterAllocSize = 1029U;
void* ptr = MockMalloc(kOuterAllocSize);
MockFree(ptr);
ThreadHeapUsage usage = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(kOuterAllocSize, usage.max_allocated_bytes);
{
ThreadHeapUsageTracker inner_usage_tracker;
inner_usage_tracker.Start();
// Make a larger allocation than the outer scope.
ptr = MockMalloc(kOuterAllocSize + 673U);
MockFree(ptr);
// This tracker is exlusive, all activity should be private to this scope.
inner_usage_tracker.Stop(true);
}
ThreadHeapUsage current = ThreadHeapUsageTracker::GetUsageSnapshot();
EXPECT_EQ(usage.alloc_ops, current.alloc_ops);
EXPECT_EQ(usage.alloc_bytes, current.alloc_bytes);
EXPECT_EQ(usage.alloc_overhead_bytes, current.alloc_overhead_bytes);
EXPECT_EQ(usage.free_ops, current.free_ops);
EXPECT_EQ(usage.free_bytes, current.free_bytes);
EXPECT_EQ(usage.max_allocated_bytes, current.max_allocated_bytes);
}
TEST_F(ThreadHeapUsageTrackerTest, AllShimFunctionsAreProvided) {
const size_t kAllocSize = 100;
void* alloc = MockMalloc(kAllocSize);
size_t estimate = MockGetSizeEstimate(alloc);
ASSERT_TRUE(estimate == 0 || estimate >= kAllocSize);
MockFree(alloc);
alloc = MockCalloc(kAllocSize, 1);
estimate = MockGetSizeEstimate(alloc);
ASSERT_TRUE(estimate == 0 || estimate >= kAllocSize);
MockFree(alloc);
alloc = MockAllocAligned(1, kAllocSize);
estimate = MockGetSizeEstimate(alloc);
ASSERT_TRUE(estimate == 0 || estimate >= kAllocSize);
alloc = MockRealloc(alloc, kAllocSize);
estimate = MockGetSizeEstimate(alloc);
ASSERT_TRUE(estimate == 0 || estimate >= kAllocSize);
MockFree(alloc);
}
#if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
TEST(ThreadHeapUsageShimTest, HooksIntoMallocWhenShimAvailable) {
ASSERT_FALSE(ThreadHeapUsageTracker::IsHeapTrackingEnabled());
ThreadHeapUsageTracker::EnableHeapTracking();
ASSERT_TRUE(ThreadHeapUsageTracker::IsHeapTrackingEnabled());
const size_t kAllocSize = 9993;
// This test verifies that the scoped heap data is affected by malloc &
// free only when the shim is available.
ThreadHeapUsageTracker usage_tracker;
usage_tracker.Start();
ThreadHeapUsage u1 = ThreadHeapUsageTracker::GetUsageSnapshot();
void* ptr = malloc(kAllocSize);
// Prevent the compiler from optimizing out the malloc/free pair.
ASSERT_NE(nullptr, ptr);
ThreadHeapUsage u2 = ThreadHeapUsageTracker::GetUsageSnapshot();
free(ptr);
usage_tracker.Stop(false);
ThreadHeapUsage u3 = usage_tracker.usage();
// Verify that at least one allocation operation was recorded, and that free
// operations are at least monotonically growing.
EXPECT_LE(0U, u1.alloc_ops);
EXPECT_LE(u1.alloc_ops + 1, u2.alloc_ops);
EXPECT_LE(u1.alloc_ops + 1, u3.alloc_ops);
// Verify that at least the bytes above were recorded.
EXPECT_LE(u1.alloc_bytes + kAllocSize, u2.alloc_bytes);
// Verify that at least the one free operation above was recorded.
EXPECT_LE(u2.free_ops + 1, u3.free_ops);
TestingThreadHeapUsageTracker::DisableHeapTrackingForTesting();
ASSERT_FALSE(ThreadHeapUsageTracker::IsHeapTrackingEnabled());
}
#endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
} // namespace debug
} // namespace base
|