OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/trace_event/memory_dump_manager.h" | 5 #include "base/trace_event/memory_dump_manager.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 quit_closure.Run(); | 102 quit_closure.Run(); |
103 } | 103 } |
104 | 104 |
105 // Posts |task| to |task_runner| and blocks until it is executed. | 105 // Posts |task| to |task_runner| and blocks until it is executed. |
106 void PostTaskAndWait(const tracked_objects::Location& from_here, | 106 void PostTaskAndWait(const tracked_objects::Location& from_here, |
107 SequencedTaskRunner* task_runner, | 107 SequencedTaskRunner* task_runner, |
108 base::OnceClosure task) { | 108 base::OnceClosure task) { |
109 base::WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, | 109 base::WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
110 WaitableEvent::InitialState::NOT_SIGNALED); | 110 WaitableEvent::InitialState::NOT_SIGNALED); |
111 task_runner->PostTask(from_here, std::move(task)); | 111 task_runner->PostTask(from_here, std::move(task)); |
112 task_runner->PostTask( | 112 task_runner->PostTask(FROM_HERE, base::BindOnce(&WaitableEvent::Signal, |
113 FROM_HERE, base::Bind(&WaitableEvent::Signal, base::Unretained(&event))); | 113 base::Unretained(&event))); |
114 // The SequencedTaskRunner guarantees that |event| will only be signaled after | 114 // The SequencedTaskRunner guarantees that |event| will only be signaled after |
115 // |task| is executed. | 115 // |task| is executed. |
116 event.Wait(); | 116 event.Wait(); |
117 } | 117 } |
118 | 118 |
119 // Testing MemoryDumpManagerDelegate which, by default, short-circuits dump | 119 // Testing MemoryDumpManagerDelegate which, by default, short-circuits dump |
120 // requests locally to the MemoryDumpManager instead of performing IPC dances. | 120 // requests locally to the MemoryDumpManager instead of performing IPC dances. |
121 class MemoryDumpManagerDelegateForTesting : public MemoryDumpManagerDelegate { | 121 class MemoryDumpManagerDelegateForTesting : public MemoryDumpManagerDelegate { |
122 public: | 122 public: |
123 MemoryDumpManagerDelegateForTesting(bool is_coordinator) | 123 MemoryDumpManagerDelegateForTesting(bool is_coordinator) |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 // When OnMemoryDump is called on either of the dump providers, it will | 727 // When OnMemoryDump is called on either of the dump providers, it will |
728 // unregister the other one. | 728 // unregister the other one. |
729 for (const std::unique_ptr<MockMemoryDumpProvider>& mdp : mdps) { | 729 for (const std::unique_ptr<MockMemoryDumpProvider>& mdp : mdps) { |
730 int other_idx = (mdps.front() == mdp); | 730 int other_idx = (mdps.front() == mdp); |
731 // TestIOThread's task runner must be obtained from the main thread but can | 731 // TestIOThread's task runner must be obtained from the main thread but can |
732 // then be used from other threads. | 732 // then be used from other threads. |
733 scoped_refptr<SingleThreadTaskRunner> other_runner = | 733 scoped_refptr<SingleThreadTaskRunner> other_runner = |
734 threads[other_idx]->task_runner(); | 734 threads[other_idx]->task_runner(); |
735 MockMemoryDumpProvider* other_mdp = mdps[other_idx].get(); | 735 MockMemoryDumpProvider* other_mdp = mdps[other_idx].get(); |
736 auto on_dump = [this, other_runner, other_mdp, &on_memory_dump_call_count]( | 736 auto on_dump = [this, other_runner, other_mdp, &on_memory_dump_call_count]( |
737 const MemoryDumpArgs& args, ProcessMemoryDump* pmd) { | 737 const MemoryDumpArgs& args, ProcessMemoryDump* pmd) { |
738 PostTaskAndWait(FROM_HERE, other_runner.get(), | 738 PostTaskAndWait(FROM_HERE, other_runner.get(), |
739 base::Bind(&MemoryDumpManager::UnregisterDumpProvider, | 739 base::BindOnce(&MemoryDumpManager::UnregisterDumpProvider, |
740 base::Unretained(&*mdm_), other_mdp)); | 740 base::Unretained(&*mdm_), other_mdp)); |
741 on_memory_dump_call_count++; | 741 on_memory_dump_call_count++; |
742 return true; | 742 return true; |
743 }; | 743 }; |
744 | 744 |
745 // OnMemoryDump is called once for the provider that dumps first, and zero | 745 // OnMemoryDump is called once for the provider that dumps first, and zero |
746 // times for the other provider. | 746 // times for the other provider. |
747 EXPECT_CALL(*mdp, OnMemoryDump(_, _)) | 747 EXPECT_CALL(*mdp, OnMemoryDump(_, _)) |
748 .Times(AtMost(1)) | 748 .Times(AtMost(1)) |
749 .WillOnce(Invoke(on_dump)); | 749 .WillOnce(Invoke(on_dump)); |
750 } | 750 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 | 848 |
849 // When OnMemoryDump is called on either of the dump providers, it will | 849 // When OnMemoryDump is called on either of the dump providers, it will |
850 // tear down the thread of the other one. | 850 // tear down the thread of the other one. |
851 for (const std::unique_ptr<MockMemoryDumpProvider>& mdp : mdps) { | 851 for (const std::unique_ptr<MockMemoryDumpProvider>& mdp : mdps) { |
852 int other_idx = (mdps.front() == mdp); | 852 int other_idx = (mdps.front() == mdp); |
853 TestIOThread* other_thread = threads[other_idx].get(); | 853 TestIOThread* other_thread = threads[other_idx].get(); |
854 // TestIOThread isn't thread-safe and must be stopped on the |main_runner|. | 854 // TestIOThread isn't thread-safe and must be stopped on the |main_runner|. |
855 scoped_refptr<SequencedTaskRunner> main_runner = | 855 scoped_refptr<SequencedTaskRunner> main_runner = |
856 SequencedTaskRunnerHandle::Get(); | 856 SequencedTaskRunnerHandle::Get(); |
857 auto on_dump = [other_thread, main_runner, &on_memory_dump_call_count]( | 857 auto on_dump = [other_thread, main_runner, &on_memory_dump_call_count]( |
858 const MemoryDumpArgs& args, ProcessMemoryDump* pmd) { | 858 const MemoryDumpArgs& args, ProcessMemoryDump* pmd) { |
859 PostTaskAndWait( | 859 PostTaskAndWait( |
860 FROM_HERE, main_runner.get(), | 860 FROM_HERE, main_runner.get(), |
861 base::Bind(&TestIOThread::Stop, base::Unretained(other_thread))); | 861 base::BindOnce(&TestIOThread::Stop, base::Unretained(other_thread))); |
862 on_memory_dump_call_count++; | 862 on_memory_dump_call_count++; |
863 return true; | 863 return true; |
864 }; | 864 }; |
865 | 865 |
866 // OnMemoryDump is called once for the provider that dumps first, and zero | 866 // OnMemoryDump is called once for the provider that dumps first, and zero |
867 // times for the other provider. | 867 // times for the other provider. |
868 EXPECT_CALL(*mdp, OnMemoryDump(_, _)) | 868 EXPECT_CALL(*mdp, OnMemoryDump(_, _)) |
869 .Times(AtMost(1)) | 869 .Times(AtMost(1)) |
870 .WillOnce(Invoke(on_dump)); | 870 .WillOnce(Invoke(on_dump)); |
871 } | 871 } |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1203 mdp->enable_mock_destructor = true; | 1203 mdp->enable_mock_destructor = true; |
1204 RegisterDumpProvider(mdp.get(), nullptr, kDefaultOptions); | 1204 RegisterDumpProvider(mdp.get(), nullptr, kDefaultOptions); |
1205 | 1205 |
1206 base::PlatformThreadRef thread_ref; | 1206 base::PlatformThreadRef thread_ref; |
1207 auto self_unregister_from_another_thread = [&mdp, &thread_ref]( | 1207 auto self_unregister_from_another_thread = [&mdp, &thread_ref]( |
1208 const MemoryDumpArgs&, ProcessMemoryDump*) -> bool { | 1208 const MemoryDumpArgs&, ProcessMemoryDump*) -> bool { |
1209 thread_ref = PlatformThread::CurrentRef(); | 1209 thread_ref = PlatformThread::CurrentRef(); |
1210 TestIOThread thread_for_unregistration(TestIOThread::kAutoStart); | 1210 TestIOThread thread_for_unregistration(TestIOThread::kAutoStart); |
1211 PostTaskAndWait( | 1211 PostTaskAndWait( |
1212 FROM_HERE, thread_for_unregistration.task_runner().get(), | 1212 FROM_HERE, thread_for_unregistration.task_runner().get(), |
1213 base::Bind( | 1213 base::BindOnce( |
1214 &MemoryDumpManager::UnregisterAndDeleteDumpProviderSoon, | 1214 &MemoryDumpManager::UnregisterAndDeleteDumpProviderSoon, |
1215 base::Unretained(MemoryDumpManager::GetInstance()), | 1215 base::Unretained(MemoryDumpManager::GetInstance()), |
1216 base::Passed(std::unique_ptr<MemoryDumpProvider>(std::move(mdp))))); | 1216 base::Passed(std::unique_ptr<MemoryDumpProvider>(std::move(mdp))))); |
1217 thread_for_unregistration.Stop(); | 1217 thread_for_unregistration.Stop(); |
1218 return true; | 1218 return true; |
1219 }; | 1219 }; |
1220 EXPECT_CALL(*mdp, OnMemoryDump(_, _)) | 1220 EXPECT_CALL(*mdp, OnMemoryDump(_, _)) |
1221 .Times(1) | 1221 .Times(1) |
1222 .WillOnce(Invoke(self_unregister_from_another_thread)); | 1222 .WillOnce(Invoke(self_unregister_from_another_thread)); |
1223 EXPECT_CALL(*mdp, Destructor()) | 1223 EXPECT_CALL(*mdp, Destructor()) |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1302 thread.Start(); | 1302 thread.Start(); |
1303 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, | 1303 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, |
1304 "BlacklistTestDumpProvider"); | 1304 "BlacklistTestDumpProvider"); |
1305 // Unregistering on wrong thread should not crash. | 1305 // Unregistering on wrong thread should not crash. |
1306 mdm_->UnregisterDumpProvider(&mdp1); | 1306 mdm_->UnregisterDumpProvider(&mdp1); |
1307 thread.Stop(); | 1307 thread.Stop(); |
1308 } | 1308 } |
1309 | 1309 |
1310 } // namespace trace_event | 1310 } // namespace trace_event |
1311 } // namespace base | 1311 } // namespace base |
OLD | NEW |