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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/heap_profiling/multi_process/test_driver.h"
#include <memory>
#include <string>
#include "base/allocator/partition_allocator/src/partition_alloc/partition_root.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/sampling_heap_profiler/poisson_allocation_sampler.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/threading/platform_thread.h"
#include "base/trace_event/heap_profiler.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/heap_profiling/multi_process/supervisor.h"
#include "components/services/heap_profiling/public/cpp/controller.h"
#include "components/services/heap_profiling/public/cpp/profiling_client.h"
#include "components/services/heap_profiling/public/cpp/settings.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/tracing_controller.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace heap_profiling {
namespace {
constexpr const char kTestCategory[] = "kTestCategory";
const char kMallocEvent[] = "kMallocEvent";
const char kMallocTypeTag[] = "kMallocTypeTag";
const char kMallocVariadicTypeTag[] = "kMallocVariadicTypeTag";
const char kPAEvent[] = "kPAEvent";
const char kVariadicEvent[] = "kVariadicEvent";
const char kThreadName[] = "kThreadName";
// Make some specific allocations in Browser to do a deeper test of the
// allocation tracking.
constexpr int kMallocAllocSize = 700;
constexpr int kMallocAllocCount = 1570;
constexpr int kVariadicAllocCount = 1000;
// The sample rate should not affect the sampled allocations. Intentionally
// choose an odd number.
constexpr int kSampleRate = 777;
// Test fixed-size partition alloc. The size must be aligned to system pointer
// size.
constexpr int kPartitionAllocSize = 8 * 25;
constexpr int kPartitionAllocCount = 2000;
static const char* kPartitionAllocTypeName = "kPartitionAllocTypeName";
// Ideally, we'd check to see that at least one renderer exists, and all
// renderers are being profiled, but something odd seems to be happening with
// warm-up/spare renderers.
//
// Whether at least 1 renderer exists, and at least 1 renderer is being
// profiled.
bool RenderersAreBeingProfiled(
const std::vector<base::ProcessId>& profiled_pids) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
for (auto iter = content::RenderProcessHost::AllHostsIterator();
!iter.IsAtEnd(); iter.Advance()) {
if (iter.GetCurrentValue()->GetProcess().Handle() ==
base::kNullProcessHandle)
continue;
base::ProcessId pid = iter.GetCurrentValue()->GetProcess().Pid();
if (base::Contains(profiled_pids, pid)) {
return true;
}
}
return false;
}
// On success, populates |pid|.
int NumProcessesWithName(const base::Value::Dict& dump_json,
std::string name,
std::vector<int>* pids) {
const base::Value::List* events = dump_json.FindList("traceEvents");
if (!events) {
return 0;
}
int num_processes = 0;
for (const base::Value& event : *events) {
const base::Value::Dict* event_dict = event.GetIfDict();
if (!event_dict) {
continue;
}
const std::string* found_name = event_dict->FindString("name");
if (!found_name) {
continue;
}
if (*found_name != "process_name") {
continue;
}
const base::Value::Dict* found_args = event_dict->FindDict("args");
if (!found_args) {
continue;
}
const std::string* found_process_name = found_args->FindString("name");
if (!found_process_name) {
continue;
}
if (*found_process_name != name) {
continue;
}
if (pids) {
absl::optional<int> found_pid = event_dict->FindInt("pid");
if (!found_pid) {
LOG(ERROR) << "Process missing pid.";
return 0;
}
pids->push_back(found_pid.value());
}
++num_processes;
}
return num_processes;
}
const base::Value::Dict* FindArgDump(base::ProcessId pid,
const base::Value::Dict& dump_json,
const char* arg) {
const base::Value::List* events = dump_json.FindList("traceEvents");
if (!events) {
return nullptr;
}
for (const base::Value& event : *events) {
const base::Value::Dict* event_dict = event.GetIfDict();
if (!event_dict) {
continue;
}
const std::string* found_name = event_dict->FindString("name");
if (!found_name) {
continue;
}
if (*found_name != "periodic_interval") {
continue;
}
absl::optional<int> found_pid = event_dict->FindInt("pid");
if (!found_pid) {
continue;
}
if (static_cast<base::ProcessId>(found_pid.value()) != pid) {
continue;
}
const base::Value::Dict* dumps =
event_dict->FindDictByDottedPath("args.dumps");
if (!dumps) {
continue;
}
const base::Value::Dict* heaps = dumps->FindDict(arg);
if (heaps) {
return heaps;
}
}
return nullptr;
}
constexpr uint64_t kNullParent = std::numeric_limits<int>::max();
struct Node {
int name_id;
std::string name;
int parent_id = kNullParent;
};
using NodeMap = std::unordered_map<uint64_t, Node>;
// Parses maps.types and maps.strings. Returns |true| on success.
bool ParseTypes(const base::Value::Dict* heaps_v2, NodeMap* output) {
const base::Value::List* types = heaps_v2->FindListByDottedPath("maps.types");
if (!types) {
LOG(ERROR) << "maps.type not a list";
return false;
}
for (const base::Value& type_value : *types) {
const base::Value::Dict* type_dict = type_value.GetIfDict();
if (!type_dict) {
continue;
}
const absl::optional<int> id = type_dict->FindInt("id");
const absl::optional<int> name_sid = type_dict->FindInt("name_sid");
if (!id || !name_sid) {
LOG(ERROR) << "Node missing id or name_sid field";
return false;
}
Node node;
node.name_id = *name_sid;
(*output)[*id] = node;
}
const base::Value::List* strings =
heaps_v2->FindListByDottedPath("maps.strings");
if (!types) {
LOG(ERROR) << "maps.strings not a list";
return false;
}
for (const base::Value& string_value : *strings) {
const base::Value::Dict* string_dict = string_value.GetIfDict();
if (!string_dict) {
continue;
}
const absl::optional<int> id = string_dict->FindInt("id");
const std::string* string = string_dict->FindString("string");
if (!id || !string) {
LOG(ERROR) << "String struct missing id or string field";
return false;
}
for (auto& pair : *output) {
if (pair.second.name_id == id.value()) {
pair.second.name = *string;
break;
}
}
}
return true;
}
// |expected_size| of 0 means no expectation.
bool GetAllocatorSubarray(const base::Value::Dict* heaps_v2,
const char* allocator_name,
const char* subarray_name,
size_t expected_size,
const base::Value::List*& output) {
const base::Value::Dict* allocators = heaps_v2->FindDict("allocators");
if (!allocators) {
LOG(ERROR) << "Failed to find allocators array in heaps v2";
return false;
}
const base::Value::Dict* allocator = allocators->FindDict(allocator_name);
if (!allocator) {
LOG(ERROR) << "Failed to find allocator_name " << allocator_name
<< " in heaps v2";
return false;
}
const base::Value::List* subarray = allocator->FindList(subarray_name);
if (!subarray) {
LOG(ERROR) << "Failed to find path: 'allocators." << allocator_name << "."
<< subarray_name << "' in heaps v2";
return false;
}
if (expected_size && subarray->size() != expected_size) {
LOG(ERROR) << subarray_name << " has wrong size";
return false;
}
output = subarray;
return true;
}
bool ValidateSamplingAllocations(const base::Value::Dict* heaps_v2,
const char* allocator_name,
int approximate_size,
int approximate_count,
const char* type_name) {
// Maps type ids to strings.
NodeMap type_map;
if (!ParseTypes(heaps_v2, &type_map))
return false;
bool found = false;
int id_of_type = 0;
for (auto& pair : type_map) {
if (pair.second.name == type_name) {
id_of_type = pair.first;
found = true;
}
}
if (!found) {
LOG(ERROR) << "Failed to find type with name: " << type_name;
return false;
}
// Find the type with the appropriate id.
const base::Value::List* types_list = nullptr;
if (!GetAllocatorSubarray(heaps_v2, allocator_name, "types", 0, types_list)) {
return false;
}
// Look up the size.
const base::Value::List* sizes = nullptr;
if (!GetAllocatorSubarray(heaps_v2, allocator_name, "sizes",
types_list->size(), sizes)) {
return false;
}
// Look up the count.
const base::Value::List* counts = nullptr;
if (!GetAllocatorSubarray(heaps_v2, allocator_name, "counts",
types_list->size(), counts)) {
return false;
}
int allocations_with_matching_type = 0;
size_t index = 0;
for (size_t i = 0; i < types_list->size(); ++i) {
if ((*types_list)[i].GetInt() == id_of_type) {
index = i;
++allocations_with_matching_type;
}
}
if (allocations_with_matching_type != 1) {
LOG(ERROR) << "Expected 1 but found " << allocations_with_matching_type
<< " allocations with matching type";
return false;
}
if ((*sizes)[index].GetInt() < approximate_size / 2 ||
(*sizes)[index].GetInt() > approximate_size * 2) {
LOG(ERROR) << "sampling size " << (*sizes)[index].GetInt()
<< " was not within a factor of 2 of expected size "
<< approximate_size;
return false;
}
if ((*counts)[index].GetInt() < approximate_count / 2 ||
(*counts)[index].GetInt() > approximate_count * 2) {
LOG(ERROR) << "sampling size " << (*counts)[index].GetInt()
<< " was not within a factor of 2 of expected count "
<< approximate_count;
return false;
}
return true;
}
bool ValidateProcessMmaps(const base::Value::Dict* process_mmaps,
bool should_have_contents) {
const base::Value::List* vm_regions = nullptr;
size_t count = 0;
if (process_mmaps) {
vm_regions = process_mmaps->FindList("vm_regions");
if (vm_regions) {
count = vm_regions->size();
}
}
if (!should_have_contents) {
if (count != 0) {
LOG(ERROR) << "vm_regions should be empty, but has contents";
return false;
}
return true;
}
if (count == 0) {
LOG(ERROR) << "vm_regions should have contents, but doesn't";
return false;
}
// File paths may contain PII. Make sure that "mf" entries only contain the
// basename, rather than a full path.
for (const base::Value& vm_region : *vm_regions) {
const std::string* file_path_value = vm_region.GetDict().FindString("mf");
if (file_path_value) {
const std::string& file_path = *file_path_value;
base::FilePath::StringType path(file_path.begin(), file_path.end());
if (base::FilePath(path).BaseName().AsUTF8Unsafe() != file_path) {
LOG(ERROR) << "vm_region should not contain file path: " << file_path;
return false;
}
}
}
return true;
}
void HandleOOM(size_t unused_size) {
LOG(FATAL) << "Out of memory.";
}
} // namespace
TestDriver::TestDriver()
: wait_for_ui_thread_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED) {
partition_alloc::PartitionAllocGlobalInit(HandleOOM);
partition_allocator_.init(partition_alloc::PartitionOptions{});
}
TestDriver::~TestDriver() {
partition_alloc::PartitionAllocGlobalUninitForTesting();
}
bool TestDriver::RunTest(const Options& options) {
options_ = options;
running_on_ui_thread_ =
content::BrowserThread::CurrentlyOn(content::BrowserThread::UI);
// The only thing to test for Mode::kNone is that profiling hasn't started.
if (options_.mode == Mode::kNone) {
if (running_on_ui_thread_) {
has_started_ = Supervisor::GetInstance()->HasStarted();
} else {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&TestDriver::GetHasStartedOnUIThread,
base::Unretained(this)));
wait_for_ui_thread_.Wait();
}
if (has_started_) {
LOG(ERROR) << "Profiling should not have started";
return false;
}
return true;
}
if (running_on_ui_thread_) {
if (!CheckOrStartProfilingOnUIThreadWithNestedRunLoops())
return false;
if (ShouldProfileRenderer())
WaitForProfilingToStartForAllRenderersUIThread();
if (ShouldProfileBrowser())
MakeTestAllocations();
CollectResults(true);
} else {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&TestDriver::CheckOrStartProfilingOnUIThreadAndSignal,
base::Unretained(this)));
wait_for_ui_thread_.Wait();
if (!initialization_success_)
return false;
if (ShouldProfileRenderer()) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&TestDriver::
WaitForProfilingToStartForAllRenderersUIThreadAndSignal,
base::Unretained(this)));
wait_for_ui_thread_.Wait();
}
if (ShouldProfileBrowser()) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&TestDriver::MakeTestAllocations,
base::Unretained(this)));
}
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&TestDriver::CollectResults,
base::Unretained(this), false));
wait_for_ui_thread_.Wait();
}
absl::optional<base::Value> dump_json =
base::JSONReader::Read(serialized_trace_);
if (!dump_json || !dump_json->is_dict()) {
LOG(ERROR) << "Failed to deserialize trace.";
return false;
}
if (!ValidateBrowserAllocations(dump_json->GetDict())) {
LOG(ERROR) << "Failed to validate browser allocations";
return false;
}
if (!ValidateRendererAllocations(dump_json->GetDict())) {
LOG(ERROR) << "Failed to validate renderer allocations";
return false;
}
return true;
}
void TestDriver::GetHasStartedOnUIThread() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
has_started_ = Supervisor::GetInstance()->HasStarted();
wait_for_ui_thread_.Signal();
}
void TestDriver::CheckOrStartProfilingOnUIThreadAndSignal() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
initialization_success_ =
CheckOrStartProfilingOnUIThreadWithAsyncSignalling();
// If the flag is true, then the WaitableEvent will be signaled after
// profiling has started.
if (!wait_for_profiling_to_start_)
wait_for_ui_thread_.Signal();
}
bool TestDriver::CheckOrStartProfilingOnUIThreadWithAsyncSignalling() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (options_.profiling_already_started) {
if (!Supervisor::GetInstance()->HasStarted()) {
LOG(ERROR) << "Profiling should have been started, but wasn't";
return false;
}
// Even if profiling has started, it's possible that the allocator shim
// has not yet been initialized. Wait for it.
if (ShouldProfileBrowser()) {
bool already_initialized = SetOnInitAllocatorShimCallbackForTesting(
base::BindOnce(&base::WaitableEvent::Signal,
base::Unretained(&wait_for_ui_thread_)),
base::SingleThreadTaskRunner::GetCurrentDefault());
if (!already_initialized) {
wait_for_profiling_to_start_ = true;
}
}
return true;
}
wait_for_profiling_to_start_ = true;
base::OnceClosure start_callback;
// If we're going to profile the browser, then wait for the allocator shim to
// start. Otherwise, wait for the Supervisor to start.
if (ShouldProfileBrowser()) {
SetOnInitAllocatorShimCallbackForTesting(
base::BindOnce(&base::WaitableEvent::Signal,
base::Unretained(&wait_for_ui_thread_)),
base::SingleThreadTaskRunner::GetCurrentDefault());
} else {
start_callback = base::BindOnce(&base::WaitableEvent::Signal,
base::Unretained(&wait_for_ui_thread_));
}
Supervisor::GetInstance()->Start(options_.mode, options_.stack_mode,
kSampleRate, std::move(start_callback));
return true;
}
bool TestDriver::CheckOrStartProfilingOnUIThreadWithNestedRunLoops() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (options_.profiling_already_started) {
if (!Supervisor::GetInstance()->HasStarted()) {
LOG(ERROR) << "Profiling should have been started, but wasn't";
return false;
}
// Even if profiling has started, it's possible that the allocator shim
// has not yet been initialized. Wait for it.
if (ShouldProfileBrowser()) {
WaitForProfilingToStartForBrowserUIThread();
}
return true;
}
// When this is not-null, initialization should wait for the QuitClosure to be
// called.
std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop);
base::OnceClosure start_callback;
// If we're going to profile the browser, then wait for the allocator shim to
// start. Otherwise, wait for the Supervisor to start.
if (ShouldProfileBrowser()) {
SetOnInitAllocatorShimCallbackForTesting(
run_loop->QuitClosure(),
base::SingleThreadTaskRunner::GetCurrentDefault());
} else {
start_callback = run_loop->QuitClosure();
}
Supervisor::GetInstance()->Start(options_.mode, options_.stack_mode,
kSampleRate, std::move(start_callback));
run_loop->Run();
if (ShouldProfileBrowser()) {
WaitForProfilingToStartForBrowserUIThread();
}
return true;
}
void TestDriver::MakeTestAllocations() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
base::PlatformThread::SetName(kThreadName);
// Profiling is turned on by default with a sampling interval of 1MB. We reset
// the sampling interval, but the change isn't picked by the thread-local
// cache until we make a sufficient number of allocations. This is an
// implementation limitation, but it's easy to work around by making a couple
// of large allocations.
constexpr int kFourMegsAllocation = 1 << 22;
for (int i = 0; i < 10; ++i) {
leaks_.push_back(static_cast<char*>(malloc(kFourMegsAllocation)));
}
// We must reserve vector size in advance so that the vector allocation does
// not get tagged as TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION.
leaks_.reserve(leaks_.size() + kPartitionAllocCount);
{
TRACE_EVENT0(kTestCategory, kPAEvent);
for (int i = 0; i < kPartitionAllocCount; ++i) {
leaks_.push_back(static_cast<char*>(partition_allocator_.root()->Alloc(
kPartitionAllocSize, kPartitionAllocTypeName)));
}
}
leaks_.reserve(leaks_.size() + kMallocAllocCount);
{
TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event(kMallocTypeTag);
TRACE_EVENT0(kTestCategory, kMallocEvent);
for (int i = 0; i < kMallocAllocCount; ++i) {
leaks_.push_back(new char[kMallocAllocSize]);
}
}
leaks_.reserve(leaks_.size() + kVariadicAllocCount);
{
TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event(kMallocVariadicTypeTag);
TRACE_EVENT0(kTestCategory, kVariadicEvent);
for (int i = 0; i < kVariadicAllocCount; ++i) {
leaks_.push_back(new char[i + kMallocAllocSize]); // Variadic allocation.
total_variadic_allocations_ += i + kMallocAllocSize;
}
}
}
void TestDriver::CollectResults(bool synchronous) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
base::OnceClosure finish_tracing_closure;
std::unique_ptr<base::RunLoop> run_loop;
if (synchronous) {
run_loop = std::make_unique<base::RunLoop>();
finish_tracing_closure = run_loop->QuitClosure();
} else {
finish_tracing_closure = base::BindOnce(
&base::WaitableEvent::Signal, base::Unretained(&wait_for_ui_thread_));
}
Supervisor::GetInstance()->RequestTraceWithHeapDump(
base::BindOnce(&TestDriver::TraceFinished, base::Unretained(this),
std::move(finish_tracing_closure)),
/* anonymize= */ true);
if (synchronous)
run_loop->Run();
}
void TestDriver::TraceFinished(base::OnceClosure closure,
bool success,
std::string trace_json) {
serialized_trace_.swap(trace_json);
std::move(closure).Run();
}
bool TestDriver::ValidateBrowserAllocations(
const base::Value::Dict& dump_json) {
const base::Value::Dict* heaps_v2 =
FindArgDump(base::Process::Current().Pid(), dump_json, "heaps_v2");
if (!ShouldProfileBrowser()) {
if (heaps_v2) {
LOG(ERROR) << "There should be no heap dump for the browser.";
return false;
}
return true;
}
if (!heaps_v2) {
LOG(ERROR) << "Browser heap dump missing.";
return false;
}
bool result = false;
bool should_validate_dumps = true;
#if BUILDFLAG(IS_ANDROID) && !defined(OFFICIAL_BUILD)
// TODO(ajwong): This step fails on Nexus 5X devices running kit-kat. It works
// on Nexus 5X devices running oreo. The problem is that all allocations have
// the same [an effectively empty] backtrace and get glommed together. More
// investigation is necessary. For now, I'm turning this off for Android.
// https://crbug.com/786450.
should_validate_dumps = false;
#endif
std::string thread_name = ShouldIncludeNativeThreadNames() ? kThreadName : "";
if (should_validate_dumps) {
result = ValidateSamplingAllocations(heaps_v2, "malloc",
kMallocAllocSize * kMallocAllocCount,
kMallocAllocCount, kMallocTypeTag);
if (!result) {
LOG(ERROR) << "Failed to validate malloc fixed allocations";
return false;
}
result = ValidateSamplingAllocations(
heaps_v2, "malloc", total_variadic_allocations_, kVariadicAllocCount,
kMallocVariadicTypeTag);
if (!result) {
LOG(ERROR) << "Failed to validate malloc variadic allocations";
return false;
}
result = ValidateSamplingAllocations(
heaps_v2, "partition_alloc", kPartitionAllocSize * kPartitionAllocCount,
kPartitionAllocCount, kPartitionAllocTypeName);
if (!result) {
LOG(ERROR) << "Failed to validate sampling allocations";
return false;
}
}
int process_count = NumProcessesWithName(dump_json, "Browser", nullptr);
if (process_count != 1) {
LOG(ERROR) << "Found " << process_count
<< " processes with name: Browser. Expected 1.";
return false;
}
const base::Value::Dict* process_mmaps =
FindArgDump(base::Process::Current().Pid(), dump_json, "process_mmaps");
if (!ValidateProcessMmaps(process_mmaps, HasNativeFrames())) {
LOG(ERROR) << "Failed to validate browser process mmaps.";
return false;
}
return true;
}
bool TestDriver::ValidateRendererAllocations(
const base::Value::Dict& dump_json) {
// On Android Webview, there is may not be a separate Renderer process. If we
// are not asked to profile the Renderer, do not perform any Renderer checks.
if (!ShouldProfileRenderer())
return true;
std::vector<int> pids;
bool result = NumProcessesWithName(dump_json, "Renderer", &pids) >= 1;
if (!result) {
LOG(ERROR) << "Failed to find process with name Renderer";
return false;
}
for (int pid : pids) {
base::ProcessId renderer_pid = static_cast<base::ProcessId>(pid);
const base::Value::Dict* heaps_v2 =
FindArgDump(renderer_pid, dump_json, "heaps_v2");
if (!heaps_v2) {
LOG(ERROR) << "Failed to find heaps v2 for renderer";
return false;
}
const base::Value::Dict* process_mmaps =
FindArgDump(renderer_pid, dump_json, "process_mmaps");
if (!ValidateProcessMmaps(process_mmaps, HasNativeFrames())) {
LOG(ERROR) << "Failed to validate renderer process mmaps.";
return false;
}
}
return true;
}
bool TestDriver::ShouldProfileBrowser() {
return options_.mode == Mode::kAll || options_.mode == Mode::kBrowser ||
options_.mode == Mode::kMinimal ||
options_.mode == Mode::kUtilityAndBrowser;
}
bool TestDriver::ShouldProfileRenderer() {
return options_.mode == Mode::kAll || options_.mode == Mode::kAllRenderers;
}
bool TestDriver::ShouldIncludeNativeThreadNames() {
return options_.stack_mode == mojom::StackMode::NATIVE_WITH_THREAD_NAMES;
}
bool TestDriver::HasNativeFrames() {
return options_.stack_mode == mojom::StackMode::NATIVE_WITH_THREAD_NAMES ||
options_.stack_mode == mojom::StackMode::NATIVE_WITHOUT_THREAD_NAMES;
}
void TestDriver::WaitForProfilingToStartForBrowserUIThread() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
while (true) {
std::vector<base::ProcessId> profiled_pids;
base::RunLoop run_loop;
auto callback = base::BindLambdaForTesting(
[&profiled_pids, &run_loop](std::vector<base::ProcessId> pids) {
profiled_pids = std::move(pids);
run_loop.Quit();
});
Supervisor::GetInstance()->GetProfiledPids(std::move(callback));
run_loop.Run();
if (base::Contains(profiled_pids, base::GetCurrentProcId())) {
break;
}
}
}
void TestDriver::WaitForProfilingToStartForAllRenderersUIThread() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
while (true) {
std::vector<base::ProcessId> profiled_pids;
base::RunLoop run_loop;
auto callback = base::BindOnce(
[](std::vector<base::ProcessId>* results, base::OnceClosure finished,
std::vector<base::ProcessId> pids) {
results->swap(pids);
std::move(finished).Run();
},
&profiled_pids, run_loop.QuitClosure());
Supervisor::GetInstance()->GetProfiledPids(std::move(callback));
run_loop.Run();
if (RenderersAreBeingProfiled(profiled_pids))
break;
}
}
void TestDriver::WaitForProfilingToStartForAllRenderersUIThreadAndSignal() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
Supervisor::GetInstance()->GetProfiledPids(base::BindOnce(
&TestDriver::WaitForProfilingToStartForAllRenderersUIThreadCallback,
base::Unretained(this)));
}
void TestDriver::WaitForProfilingToStartForAllRenderersUIThreadCallback(
std::vector<base::ProcessId> results) {
if (RenderersAreBeingProfiled(results)) {
wait_for_ui_thread_.Signal();
return;
}
// Brief sleep to prevent spamming the task queue, since this code is called
// in a tight loop.
base::PlatformThread::Sleep(base::Microseconds(100));
WaitForProfilingToStartForAllRenderersUIThreadAndSignal();
}
} // namespace heap_profiling
|