Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: components/browser_watcher/postmortem_report_collector_unittest.cc

Issue 2467103002: Collect an initial set of stability activities (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/browser_watcher/postmortem_report_collector.h" 5 #include "components/browser_watcher/postmortem_report_collector.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/debug/activity_tracker.h" 15 #include "base/debug/activity_tracker.h"
16 #include "base/files/file.h" 16 #include "base/files/file.h"
17 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
18 #include "base/files/file_util.h" 18 #include "base/files/file_util.h"
19 #include "base/files/memory_mapped_file.h" 19 #include "base/files/memory_mapped_file.h"
20 #include "base/files/scoped_file.h" 20 #include "base/files/scoped_file.h"
21 #include "base/files/scoped_temp_dir.h" 21 #include "base/files/scoped_temp_dir.h"
22 #include "base/memory/ptr_util.h" 22 #include "base/memory/ptr_util.h"
23 #include "base/metrics/persistent_memory_allocator.h" 23 #include "base/metrics/persistent_memory_allocator.h"
24 #include "base/process/process_handle.h"
24 #include "base/threading/platform_thread.h" 25 #include "base/threading/platform_thread.h"
25 #include "testing/gmock/include/gmock/gmock.h" 26 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/crashpad/crashpad/client/crash_report_database.h" 28 #include "third_party/crashpad/crashpad/client/crash_report_database.h"
28 29
29 namespace browser_watcher { 30 namespace browser_watcher {
30 31
31 using base::debug::Activity;
32 using base::debug::ActivityData; 32 using base::debug::ActivityData;
33 using base::debug::GlobalActivityTracker; 33 using base::debug::GlobalActivityTracker;
34 using base::debug::ThreadActivityTracker; 34 using base::debug::ThreadActivityTracker;
35 using base::File; 35 using base::File;
36 using base::FilePersistentMemoryAllocator; 36 using base::FilePersistentMemoryAllocator;
37 using base::MemoryMappedFile; 37 using base::MemoryMappedFile;
38 using base::PersistentMemoryAllocator; 38 using base::PersistentMemoryAllocator;
39 using base::WrapUnique; 39 using base::WrapUnique;
40 using crashpad::CrashReportDatabase; 40 using crashpad::CrashReportDatabase;
41 using crashpad::Settings; 41 using crashpad::Settings;
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 kChannelName); 319 kChannelName);
320 std::unique_ptr<StabilityReport> report; 320 std::unique_ptr<StabilityReport> report;
321 ASSERT_EQ(PostmortemReportCollector::DEBUG_FILE_NO_DATA, 321 ASSERT_EQ(PostmortemReportCollector::DEBUG_FILE_NO_DATA,
322 collector.Collect(file_path, &report)); 322 collector.Collect(file_path, &report));
323 } 323 }
324 324
325 namespace { 325 namespace {
326 326
327 // Parameters for the activity tracking. 327 // Parameters for the activity tracking.
328 const size_t kFileSize = 2 * 1024; 328 const size_t kFileSize = 2 * 1024;
329 const int kStackDepth = 4; 329 const int kStackDepth = 5;
330 const uint64_t kAllocatorId = 0; 330 const uint64_t kAllocatorId = 0;
331 const char kAllocatorName[] = "PostmortemReportCollectorCollectionTest"; 331 const char kAllocatorName[] = "PostmortemReportCollectorCollectionTest";
332 const int kTaskSequenceNum = 42;
333 const uintptr_t kTaskOrigin = 1000U;
334 const uintptr_t kLockAddress = 1001U;
335 const uintptr_t kEventAddress = 1002U;
336 const int kThreadId = 43;
337 const int kProcessId = 44;
338 const int kAnotherThreadId = 45;
332 339
333 } // namespace 340 } // namespace
334 341
335 class PostmortemReportCollectorCollectionTest : public testing::Test { 342 class PostmortemReportCollectorCollectionTest : public testing::Test {
336 public: 343 public:
337 // Create a proper debug file. 344 // Create a proper debug file.
338 void SetUp() override { 345 void SetUp() override {
339 testing::Test::SetUp(); 346 testing::Test::SetUp();
340 347
341 // Create a file backed allocator. 348 // Create a file backed allocator.
342 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 349 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
343 debug_file_path_ = temp_dir_.GetPath().AppendASCII("debug_file.pma"); 350 debug_file_path_ = temp_dir_.GetPath().AppendASCII("debug_file.pma");
344 std::unique_ptr<PersistentMemoryAllocator> allocator = CreateAllocator(); 351 std::unique_ptr<PersistentMemoryAllocator> allocator = CreateAllocator();
345 ASSERT_NE(nullptr, allocator); 352 ASSERT_NE(nullptr, allocator);
346 353
347 size_t tracker_mem_size = 354 size_t tracker_mem_size =
348 ThreadActivityTracker::SizeForStackDepth(kStackDepth); 355 ThreadActivityTracker::SizeForStackDepth(kStackDepth);
349 ASSERT_GT(kFileSize, tracker_mem_size); 356 ASSERT_GT(kFileSize, tracker_mem_size);
350 357
351 // Create some debug data using trackers. 358 // Create some debug data using trackers.
352 std::unique_ptr<ThreadActivityTracker> tracker = 359 std::unique_ptr<ThreadActivityTracker> tracker =
353 CreateTracker(allocator.get(), tracker_mem_size); 360 CreateTracker(allocator.get(), tracker_mem_size);
354 ASSERT_NE(nullptr, tracker); 361 ASSERT_NE(nullptr, tracker);
355 ASSERT_TRUE(tracker->IsValid()); 362 ASSERT_TRUE(tracker->IsValid());
356 363
357 const void* dummy_task_origin = reinterpret_cast<void*>(0xCAFE); 364 tracker->PushActivity(reinterpret_cast<void*>(kTaskOrigin),
bcwhite 2016/11/02 16:52:37 This is far away from where it's tested. How abou
manzagop (departed) 2016/11/02 20:15:23 Done.
bcwhite 2016/11/03 17:56:25 Acknowledged.
358 const int dummy_task_sequence_num = 42; 365 base::debug::Activity::ACT_TASK_RUN,
359 tracker->PushActivity(dummy_task_origin, Activity::ACT_TASK_RUN, 366 ActivityData::ForTask(kTaskSequenceNum));
360 ActivityData::ForTask(dummy_task_sequence_num)); 367 tracker->PushActivity(
361 368 nullptr, base::debug::Activity::ACT_LOCK_ACQUIRE,
362 // TODO(manzagop): flesh out the data (more trackers and content). 369 ActivityData::ForLock(reinterpret_cast<void*>(kLockAddress)));
370 tracker->PushActivity(
371 nullptr, base::debug::Activity::ACT_EVENT_WAIT,
372 ActivityData::ForEvent(reinterpret_cast<void*>(kEventAddress)));
373 tracker->PushActivity(nullptr, base::debug::Activity::ACT_THREAD_JOIN,
374 ActivityData::ForThread(kThreadId));
375 tracker->PushActivity(nullptr, base::debug::Activity::ACT_PROCESS_WAIT,
376 ActivityData::ForProcess(kProcessId));
377 // Note: this exceeds the stack's capacity.
378 tracker->PushActivity(nullptr, base::debug::Activity::ACT_THREAD_JOIN,
379 ActivityData::ForThread(kAnotherThreadId));
363 } 380 }
364 381
365 std::unique_ptr<PersistentMemoryAllocator> CreateAllocator() { 382 std::unique_ptr<PersistentMemoryAllocator> CreateAllocator() {
366 // Create the memory mapped file. 383 // Create the memory mapped file.
367 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); 384 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile());
368 bool success = mmfile->Initialize( 385 bool success = mmfile->Initialize(
369 File(debug_file_path_, File::FLAG_CREATE | File::FLAG_READ | 386 File(debug_file_path_, File::FLAG_CREATE | File::FLAG_READ |
370 File::FLAG_WRITE | File::FLAG_SHARE_DELETE), 387 File::FLAG_WRITE | File::FLAG_SHARE_DELETE),
371 {0, static_cast<int64_t>(kFileSize)}, 388 {0, static_cast<int64_t>(kFileSize)},
372 MemoryMappedFile::READ_WRITE_EXTEND); 389 MemoryMappedFile::READ_WRITE_EXTEND);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 427
411 TEST_F(PostmortemReportCollectorCollectionTest, CollectSuccess) { 428 TEST_F(PostmortemReportCollectorCollectionTest, CollectSuccess) {
412 // Validate collection returns the expected report. 429 // Validate collection returns the expected report.
413 PostmortemReportCollector collector(kProductName, kVersionNumber, 430 PostmortemReportCollector collector(kProductName, kVersionNumber,
414 kChannelName); 431 kChannelName);
415 std::unique_ptr<StabilityReport> report; 432 std::unique_ptr<StabilityReport> report;
416 ASSERT_EQ(PostmortemReportCollector::SUCCESS, 433 ASSERT_EQ(PostmortemReportCollector::SUCCESS,
417 collector.Collect(debug_file_path(), &report)); 434 collector.Collect(debug_file_path(), &report));
418 ASSERT_NE(nullptr, report); 435 ASSERT_NE(nullptr, report);
419 436
420 // Build the expected report. 437 // Validate the report.
421 StabilityReport expected_report; 438 ASSERT_EQ(1, report->process_states_size());
422 ProcessState* process_state = expected_report.add_process_states(); 439 const ProcessState& process_state = report->process_states(0);
423 ThreadState* thread_state = process_state->add_threads(); 440 EXPECT_EQ(base::GetCurrentProcId(), process_state.process_id());
424 thread_state->set_thread_name(base::PlatformThread::GetName()); 441 ASSERT_EQ(1, process_state.threads_size());
425 442
426 ASSERT_EQ(expected_report.SerializeAsString(), report->SerializeAsString()); 443 const ThreadState& thread_state = process_state.threads(0);
444 EXPECT_EQ(base::PlatformThread::GetName(), thread_state.thread_name());
445 #if defined(OS_WIN)
446 EXPECT_EQ(base::PlatformThread::CurrentId(), thread_state.thread_id());
447 #elif defined(OS_POSIX)
448 EXPECT_EQ(base::PlatformThread::CurrentHandle().platform_handle(),
449 thread_state.thread_id());
450 #endif
451
452 EXPECT_EQ(6, thread_state.activity_cnt());
453 ASSERT_EQ(5, thread_state.activities_size());
454 {
455 const Activity& activity = thread_state.activities(0);
456 EXPECT_EQ(Activity::ACT_TASK_RUN, activity.type());
457 EXPECT_EQ(kTaskOrigin, activity.origin_address());
458 EXPECT_EQ(kTaskSequenceNum, activity.task_sequence_id());
459 }
460 {
461 const Activity& activity = thread_state.activities(1);
462 EXPECT_EQ(Activity::ACT_LOCK_ACQUIRE, activity.type());
463 EXPECT_EQ(kLockAddress, activity.lock_address());
464 }
465 {
466 const Activity& activity = thread_state.activities(2);
467 EXPECT_EQ(Activity::ACT_EVENT_WAIT, activity.type());
468 EXPECT_EQ(kEventAddress, activity.event_address());
469 }
470 {
471 const Activity& activity = thread_state.activities(3);
472 EXPECT_EQ(Activity::ACT_THREAD_JOIN, activity.type());
473 EXPECT_EQ(kThreadId, activity.thread_id());
474 }
475 {
476 const Activity& activity = thread_state.activities(4);
477 EXPECT_EQ(Activity::ACT_PROCESS_WAIT, activity.type());
478 EXPECT_EQ(kProcessId, activity.process_id());
479 }
427 } 480 }
428 481
429 } // namespace browser_watcher 482 } // namespace browser_watcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698