Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 5 | #ifndef COMPONENTS_HEAP_PROFILING_TEST_DRIVER_H_ |
| 6 | #define COMPONENTS_HEAP_PROFILING_TEST_DRIVER_H_ |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/allocator/partition_allocator/partition_alloc.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "base/memory/ref_counted_memory.h" |
James Cook | 500a245e | 2019-10-02 19:18:35 | [diff] [blame] | 13 | #include "base/process/process_handle.h" |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 14 | #include "base/synchronization/waitable_event.h" |
Alexei Filippov | e48985e | 2019-02-01 00:27:41 | [diff] [blame] | 15 | #include "components/services/heap_profiling/public/cpp/settings.h" |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 16 | #include "components/services/heap_profiling/public/mojom/heap_profiling_client.mojom.h" |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 17 | |
| 18 | namespace base { |
| 19 | class Value; |
| 20 | } // namespace base |
| 21 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 22 | namespace heap_profiling { |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 23 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 24 | // This class runs tests for the Heap Profiling Service, a cross-platform, |
| 25 | // multi-process component. |
| 26 | // |
| 27 | // Chrome on Android does not support browser_tests. It does support |
| 28 | // content_browsertests, but those are not multi-process tests. On Android, |
| 29 | // processes have to be started via the Activity mechanism, and the test |
| 30 | // infrastructure does not support this. |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 31 | // |
| 32 | // To avoid test-code duplication, all tests are pulled into this class. |
| 33 | // browser_tests will directly call this class. The android |
| 34 | // chrome_public_test_apk will invoke this class via a JNI shim. Since the |
| 35 | // latter is not running within the gtest framework, this class cannot use |
| 36 | // EXPECT* and ASSERT* macros. Instead, this class will return a bool indicating |
| 37 | // success of the entire test. On failure, errors will be output via LOG(ERROR). |
| 38 | // These will show up in the browser_tests output stream, and will be captured |
| 39 | // by logcat [the Android logging facility]. The latter is already the canonical |
| 40 | // mechanism for investigating test failures. |
| 41 | // |
| 42 | // Note: Outputting to stderr will not have the desired effect, since that is |
| 43 | // not captured by logcat. |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 44 | class TestDriver { |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 45 | public: |
| 46 | struct Options { |
| 47 | // The profiling mode to test. |
Alexei Filippov | e48985e | 2019-02-01 00:27:41 | [diff] [blame] | 48 | Mode mode = Mode::kBrowser; |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 49 | |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 50 | // The stack profiling mode to test. |
Alexei Filippov | e48985e | 2019-02-01 00:27:41 | [diff] [blame] | 51 | mojom::StackMode stack_mode = mojom::StackMode::NATIVE_WITHOUT_THREAD_NAMES; |
| 52 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 53 | // Whether the caller has already started profiling with the given mode. |
erikchen | 011ad3f | 2018-01-26 17:54:55 | [diff] [blame] | 54 | // When false, the test driver is responsible for starting profiling. |
Alexei Filippov | e48985e | 2019-02-01 00:27:41 | [diff] [blame] | 55 | bool profiling_already_started = false; |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 56 | }; |
| 57 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 58 | TestDriver(); |
| 59 | ~TestDriver(); |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 60 | |
| 61 | // If this is called on the content::BrowserThread::UI thread, then the |
| 62 | // platform must support nested message loops. [This is currently not |
| 63 | // supported on Android]. |
| 64 | // |
| 65 | // Returns whether the test run was successful. Expectation/Assertion failures |
| 66 | // will be printed via LOG(ERROR). |
| 67 | bool RunTest(const Options& options); |
| 68 | |
| 69 | private: |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 70 | // Populates |has_started_| and then signals |wait_for_ui_thread_|. |
| 71 | void GetHasStartedOnUIThread(); |
| 72 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 73 | // Populates |initialization_success_| with the result of |
| 74 | // |RunInitializationOnUIThread|, and then signals |wait_for_ui_thread_|. |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 75 | void CheckOrStartProfilingOnUIThreadAndSignal(); |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 76 | |
| 77 | // If profiling is expected to already be started, confirm it. |
| 78 | // Otherwise, start profiling with the given mode. |
Erik Chen | 06a20b5 | 2018-04-17 19:05:32 | [diff] [blame] | 79 | // This method must only be called on platforms that supported nested run |
| 80 | // loops on the UI thread. |
| 81 | bool CheckOrStartProfilingOnUIThreadWithNestedRunLoops(); |
| 82 | |
| 83 | // If profiling is expected to already be started, confirm it. |
| 84 | // Otherwise, start profiling with the given mode. |
| 85 | // This method must only be called on platforms that are running the |
| 86 | // TestDriver from a non-UI thread, which allows for async signalling. |
| 87 | bool CheckOrStartProfilingOnUIThreadWithAsyncSignalling(); |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 88 | |
| 89 | // Performs allocations. These are expected to be profiled. |
| 90 | void MakeTestAllocations(); |
| 91 | |
| 92 | // Collects a trace that contains a heap dump. The result is stored in |
| 93 | // |serialized_trace_|. |
| 94 | // |
| 95 | // When |synchronous| is true, this method spins a nested message loop. When |
| 96 | // |synchronous| is false, this method posts some tasks that will eventually |
| 97 | // signal |wait_for_ui_thread_|. |
| 98 | void CollectResults(bool synchronous); |
| 99 | |
Ken Rockot | 2ade11d7 | 2019-12-20 18:37:16 | [diff] [blame] | 100 | void TraceFinished(base::OnceClosure closure, |
erikchen | 872446d | 2017-11-29 00:34:40 | [diff] [blame] | 101 | bool success, |
| 102 | std::string trace_json); |
| 103 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 104 | bool ValidateBrowserAllocations(base::Value* dump_json); |
| 105 | bool ValidateRendererAllocations(base::Value* dump_json); |
| 106 | |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 107 | bool ShouldProfileBrowser(); |
erikchen | ee0a007 | 2018-01-19 21:52:04 | [diff] [blame] | 108 | bool ShouldProfileRenderer(); |
erikchen | 011ad3f | 2018-01-26 17:54:55 | [diff] [blame] | 109 | bool ShouldIncludeNativeThreadNames(); |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 110 | bool HasPseudoFrames(); |
erikchen | 080ebb1 | 2018-03-21 22:21:01 | [diff] [blame] | 111 | bool HasNativeFrames(); |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 112 | |
Erik Chen | 7e4cccd6 | 2019-12-05 23:58:58 | [diff] [blame] | 113 | void WaitForProfilingToStartForBrowserUIThread(); |
erikchen | ee0a007 | 2018-01-19 21:52:04 | [diff] [blame] | 114 | void WaitForProfilingToStartForAllRenderersUIThread(); |
| 115 | |
| 116 | // Android does not support nested RunLoops. Instead, it signals |
| 117 | // |wait_for_ui_thread_| when finished. |
| 118 | void WaitForProfilingToStartForAllRenderersUIThreadAndSignal(); |
| 119 | void WaitForProfilingToStartForAllRenderersUIThreadCallback( |
| 120 | std::vector<base::ProcessId> results); |
| 121 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 122 | Options options_; |
| 123 | |
| 124 | // Allocations made by this class. Intentionally leaked, since deallocating |
| 125 | // them would trigger a large number of IPCs, which is slow. |
| 126 | std::vector<char*> leaks_; |
| 127 | |
| 128 | // Sum of size of all variadic allocations. |
| 129 | size_t total_variadic_allocations_ = 0; |
| 130 | |
| 131 | // Use to make PA allocations, which should also be shimmed. |
| 132 | base::PartitionAllocatorGeneric partition_allocator_; |
| 133 | |
| 134 | // Contains nothing until |CollectResults| has been called. |
erikchen | 872446d | 2017-11-29 00:34:40 | [diff] [blame] | 135 | std::string serialized_trace_; |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 136 | |
| 137 | // Whether the test was invoked on the ui thread. |
| 138 | bool running_on_ui_thread_ = true; |
| 139 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 140 | // Whether the supervisor has started. |
| 141 | bool has_started_ = false; |
| 142 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 143 | // Whether an error has occurred. |
| 144 | bool initialization_success_ = false; |
| 145 | |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 146 | // When |true|, initialization will wait for the allocator shim to enable |
| 147 | // before continuing. |
| 148 | bool wait_for_profiling_to_start_ = false; |
| 149 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 150 | base::WaitableEvent wait_for_ui_thread_; |
| 151 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 152 | DISALLOW_COPY_AND_ASSIGN(TestDriver); |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 153 | }; |
| 154 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 155 | } // namespace heap_profiling |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 156 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 157 | #endif // COMPONENTS_HEAP_PROFILING_TEST_DRIVER_H_ |