blob: f897c67204e7024412d8074def241c70c9b98512 [file] [log] [blame]
Erik Chen9d81094d2017-11-20 17:40:091// 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 Chen74cba8c2018-04-16 18:49:495#ifndef COMPONENTS_HEAP_PROFILING_TEST_DRIVER_H_
6#define COMPONENTS_HEAP_PROFILING_TEST_DRIVER_H_
Erik Chen9d81094d2017-11-20 17:40:097
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 Chen74cba8c2018-04-16 18:49:4914#include "components/services/heap_profiling/public/mojom/heap_profiling_client.mojom.h"
Erik Chen9d81094d2017-11-20 17:40:0915
16namespace base {
17class Value;
18} // namespace base
19
erikchen102fe212018-04-06 13:02:1020namespace heap_profiling {
Erik Chen9d81094d2017-11-20 17:40:0921
Erik Chen74cba8c2018-04-16 18:49:4922enum 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 Chen9d81094d2017-11-20 17:40:0931//
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 Chen74cba8c2018-04-16 18:49:4944class TestDriver {
Erik Chen9d81094d2017-11-20 17:40:0945 public:
46 struct Options {
47 // The profiling mode to test.
erikchen61923732018-04-06 13:14:5148 Mode mode;
Erik Chen9d81094d2017-11-20 17:40:0949
Erik Chen3303fd0232018-01-11 20:29:0550 // The stack profiling mode to test.
erikchen102fe212018-04-06 13:02:1051 mojom::StackMode stack_mode;
Erik Chen3303fd0232018-01-11 20:29:0552
Erik Chen9d81094d2017-11-20 17:40:0953 // Whether the caller has already started profiling with the given mode.
erikchen011ad3f2018-01-26 17:54:5554 // When false, the test driver is responsible for starting profiling.
Erik Chen9d81094d2017-11-20 17:40:0955 bool profiling_already_started;
erikchen8bc20d82018-02-14 03:21:5156
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 Chen9d81094d2017-11-20 17:40:0965 };
66
Erik Chen74cba8c2018-04-16 18:49:4967 TestDriver();
68 ~TestDriver();
Erik Chen9d81094d2017-11-20 17:40:0969
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 Chen74cba8c2018-04-16 18:49:4979 // Populates |has_started_| and then signals |wait_for_ui_thread_|.
80 void GetHasStartedOnUIThread();
81
Erik Chen9d81094d2017-11-20 17:40:0982 // Populates |initialization_success_| with the result of
83 // |RunInitializationOnUIThread|, and then signals |wait_for_ui_thread_|.
Erik Chen3303fd0232018-01-11 20:29:0584 void CheckOrStartProfilingOnUIThreadAndSignal();
Erik Chen9d81094d2017-11-20 17:40:0985
Erik Chen74cba8c2018-04-16 18:49:4986 // Calls Supervisor::SetKeepSmallAllocations() and then signals
87 // |wait_for_ui_thread_|.
88 void SetKeepSmallAllocationsOnUIThreadAndSignal();
89
Erik Chen9d81094d2017-11-20 17:40:0990 // If profiling is expected to already be started, confirm it.
91 // Otherwise, start profiling with the given mode.
Erik Chen06a20b52018-04-17 19:05:3292 // 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 Chen9d81094d2017-11-20 17:40:09101
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
erikchen872446d2017-11-29 00:34:40113 void TraceFinished(base::Closure closure,
114 bool success,
115 std::string trace_json);
116
Erik Chen9d81094d2017-11-20 17:40:09117 bool ValidateBrowserAllocations(base::Value* dump_json);
118 bool ValidateRendererAllocations(base::Value* dump_json);
119
Erik Chen3303fd0232018-01-11 20:29:05120 bool ShouldProfileBrowser();
erikchenee0a0072018-01-19 21:52:04121 bool ShouldProfileRenderer();
erikchen011ad3f2018-01-26 17:54:55122 bool ShouldIncludeNativeThreadNames();
Erik Chen3303fd0232018-01-11 20:29:05123 bool HasPseudoFrames();
erikchen080ebb12018-03-21 22:21:01124 bool HasNativeFrames();
erikchen8bc20d82018-02-14 03:21:51125 bool IsRecordingAllAllocations();
Erik Chen3303fd0232018-01-11 20:29:05126
erikchenee0a0072018-01-19 21:52:04127 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 Chen9d81094d2017-11-20 17:40:09135 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.
erikchen872446d2017-11-29 00:34:40148 std::string serialized_trace_;
Erik Chen9d81094d2017-11-20 17:40:09149
150 // Whether the test was invoked on the ui thread.
151 bool running_on_ui_thread_ = true;
152
Erik Chen74cba8c2018-04-16 18:49:49153 // Whether the supervisor has started.
154 bool has_started_ = false;
155
Erik Chen9d81094d2017-11-20 17:40:09156 // Whether an error has occurred.
157 bool initialization_success_ = false;
158
Erik Chen3303fd0232018-01-11 20:29:05159 // When |true|, initialization will wait for the allocator shim to enable
160 // before continuing.
161 bool wait_for_profiling_to_start_ = false;
162
Erik Chen9d81094d2017-11-20 17:40:09163 base::WaitableEvent wait_for_ui_thread_;
164
Erik Chen74cba8c2018-04-16 18:49:49165 DISALLOW_COPY_AND_ASSIGN(TestDriver);
Erik Chen9d81094d2017-11-20 17:40:09166};
167
erikchen102fe212018-04-06 13:02:10168} // namespace heap_profiling
Erik Chen9d81094d2017-11-20 17:40:09169
Erik Chen74cba8c2018-04-16 18:49:49170#endif // COMPONENTS_HEAP_PROFILING_TEST_DRIVER_H_