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" |
| 13 | #include "base/synchronization/waitable_event.h" |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 14 | #include "components/services/heap_profiling/public/mojom/heap_profiling_client.mojom.h" |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | class Value; |
| 18 | } // namespace base |
| 19 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 20 | namespace heap_profiling { |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 21 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 22 | enum class Mode; |
| 23 | |
| 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. |
erikchen | 6192373 | 2018-04-06 13:14:51 | [diff] [blame] | 48 | Mode mode; |
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. |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 51 | mojom::StackMode stack_mode; |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 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. |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 55 | bool profiling_already_started; |
erikchen | 8bc20d8 | 2018-02-14 03:21:51 | [diff] [blame] | 56 | |
| 57 | // Whether to test sampling. |
| 58 | bool should_sample; |
| 59 | |
| 60 | // When set to true, the internal sampling_rate is set to 2. While this |
| 61 | // doesn't record all allocations, it should record all test allocations |
| 62 | // made in this file with exponentially high probability. |
| 63 | // When set to false, the internal sampling rate is set to 10000. |
| 64 | bool sample_everything; |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 65 | }; |
| 66 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 67 | TestDriver(); |
| 68 | ~TestDriver(); |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 69 | |
| 70 | // If this is called on the content::BrowserThread::UI thread, then the |
| 71 | // platform must support nested message loops. [This is currently not |
| 72 | // supported on Android]. |
| 73 | // |
| 74 | // Returns whether the test run was successful. Expectation/Assertion failures |
| 75 | // will be printed via LOG(ERROR). |
| 76 | bool RunTest(const Options& options); |
| 77 | |
| 78 | private: |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 79 | // Populates |has_started_| and then signals |wait_for_ui_thread_|. |
| 80 | void GetHasStartedOnUIThread(); |
| 81 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 82 | // Populates |initialization_success_| with the result of |
| 83 | // |RunInitializationOnUIThread|, and then signals |wait_for_ui_thread_|. |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 84 | void CheckOrStartProfilingOnUIThreadAndSignal(); |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 85 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 86 | // Calls Supervisor::SetKeepSmallAllocations() and then signals |
| 87 | // |wait_for_ui_thread_|. |
| 88 | void SetKeepSmallAllocationsOnUIThreadAndSignal(); |
| 89 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 90 | // If profiling is expected to already be started, confirm it. |
| 91 | // Otherwise, start profiling with the given mode. |
Erik Chen | 06a20b5 | 2018-04-17 19:05:32 | [diff] [blame^] | 92 | // This method must only be called on platforms that supported nested run |
| 93 | // loops on the UI thread. |
| 94 | bool CheckOrStartProfilingOnUIThreadWithNestedRunLoops(); |
| 95 | |
| 96 | // If profiling is expected to already be started, confirm it. |
| 97 | // Otherwise, start profiling with the given mode. |
| 98 | // This method must only be called on platforms that are running the |
| 99 | // TestDriver from a non-UI thread, which allows for async signalling. |
| 100 | bool CheckOrStartProfilingOnUIThreadWithAsyncSignalling(); |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 101 | |
| 102 | // Performs allocations. These are expected to be profiled. |
| 103 | void MakeTestAllocations(); |
| 104 | |
| 105 | // Collects a trace that contains a heap dump. The result is stored in |
| 106 | // |serialized_trace_|. |
| 107 | // |
| 108 | // When |synchronous| is true, this method spins a nested message loop. When |
| 109 | // |synchronous| is false, this method posts some tasks that will eventually |
| 110 | // signal |wait_for_ui_thread_|. |
| 111 | void CollectResults(bool synchronous); |
| 112 | |
erikchen | 872446d | 2017-11-29 00:34:40 | [diff] [blame] | 113 | void TraceFinished(base::Closure closure, |
| 114 | bool success, |
| 115 | std::string trace_json); |
| 116 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 117 | bool ValidateBrowserAllocations(base::Value* dump_json); |
| 118 | bool ValidateRendererAllocations(base::Value* dump_json); |
| 119 | |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 120 | bool ShouldProfileBrowser(); |
erikchen | ee0a007 | 2018-01-19 21:52:04 | [diff] [blame] | 121 | bool ShouldProfileRenderer(); |
erikchen | 011ad3f | 2018-01-26 17:54:55 | [diff] [blame] | 122 | bool ShouldIncludeNativeThreadNames(); |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 123 | bool HasPseudoFrames(); |
erikchen | 080ebb1 | 2018-03-21 22:21:01 | [diff] [blame] | 124 | bool HasNativeFrames(); |
erikchen | 8bc20d8 | 2018-02-14 03:21:51 | [diff] [blame] | 125 | bool IsRecordingAllAllocations(); |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 126 | |
erikchen | ee0a007 | 2018-01-19 21:52:04 | [diff] [blame] | 127 | void WaitForProfilingToStartForAllRenderersUIThread(); |
| 128 | |
| 129 | // Android does not support nested RunLoops. Instead, it signals |
| 130 | // |wait_for_ui_thread_| when finished. |
| 131 | void WaitForProfilingToStartForAllRenderersUIThreadAndSignal(); |
| 132 | void WaitForProfilingToStartForAllRenderersUIThreadCallback( |
| 133 | std::vector<base::ProcessId> results); |
| 134 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 135 | Options options_; |
| 136 | |
| 137 | // Allocations made by this class. Intentionally leaked, since deallocating |
| 138 | // them would trigger a large number of IPCs, which is slow. |
| 139 | std::vector<char*> leaks_; |
| 140 | |
| 141 | // Sum of size of all variadic allocations. |
| 142 | size_t total_variadic_allocations_ = 0; |
| 143 | |
| 144 | // Use to make PA allocations, which should also be shimmed. |
| 145 | base::PartitionAllocatorGeneric partition_allocator_; |
| 146 | |
| 147 | // Contains nothing until |CollectResults| has been called. |
erikchen | 872446d | 2017-11-29 00:34:40 | [diff] [blame] | 148 | std::string serialized_trace_; |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 149 | |
| 150 | // Whether the test was invoked on the ui thread. |
| 151 | bool running_on_ui_thread_ = true; |
| 152 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 153 | // Whether the supervisor has started. |
| 154 | bool has_started_ = false; |
| 155 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 156 | // Whether an error has occurred. |
| 157 | bool initialization_success_ = false; |
| 158 | |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 159 | // When |true|, initialization will wait for the allocator shim to enable |
| 160 | // before continuing. |
| 161 | bool wait_for_profiling_to_start_ = false; |
| 162 | |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 163 | base::WaitableEvent wait_for_ui_thread_; |
| 164 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 165 | DISALLOW_COPY_AND_ASSIGN(TestDriver); |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 166 | }; |
| 167 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 168 | } // namespace heap_profiling |
Erik Chen | 9d81094d | 2017-11-20 17:40:09 | [diff] [blame] | 169 | |
Erik Chen | 74cba8c | 2018-04-16 18:49:49 | [diff] [blame] | 170 | #endif // COMPONENTS_HEAP_PROFILING_TEST_DRIVER_H_ |