blob: ebf1363800e8560183cdeb6bf94112a2600c92e5 [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"
Alexei Filippove48985e2019-02-01 00:27:4114#include "components/services/heap_profiling/public/cpp/settings.h"
Erik Chen74cba8c2018-04-16 18:49:4915#include "components/services/heap_profiling/public/mojom/heap_profiling_client.mojom.h"
Erik Chen9d81094d2017-11-20 17:40:0916
17namespace base {
18class Value;
19} // namespace base
20
erikchen102fe212018-04-06 13:02:1021namespace heap_profiling {
Erik Chen9d81094d2017-11-20 17:40:0922
Erik Chen74cba8c2018-04-16 18:49:4923// This class runs tests for the Heap Profiling Service, a cross-platform,
24// multi-process component.
25//
26// Chrome on Android does not support browser_tests. It does support
27// content_browsertests, but those are not multi-process tests. On Android,
28// processes have to be started via the Activity mechanism, and the test
29// infrastructure does not support this.
Erik Chen9d81094d2017-11-20 17:40:0930//
31// To avoid test-code duplication, all tests are pulled into this class.
32// browser_tests will directly call this class. The android
33// chrome_public_test_apk will invoke this class via a JNI shim. Since the
34// latter is not running within the gtest framework, this class cannot use
35// EXPECT* and ASSERT* macros. Instead, this class will return a bool indicating
36// success of the entire test. On failure, errors will be output via LOG(ERROR).
37// These will show up in the browser_tests output stream, and will be captured
38// by logcat [the Android logging facility]. The latter is already the canonical
39// mechanism for investigating test failures.
40//
41// Note: Outputting to stderr will not have the desired effect, since that is
42// not captured by logcat.
Erik Chen74cba8c2018-04-16 18:49:4943class TestDriver {
Erik Chen9d81094d2017-11-20 17:40:0944 public:
45 struct Options {
46 // The profiling mode to test.
Alexei Filippove48985e2019-02-01 00:27:4147 Mode mode = Mode::kBrowser;
Erik Chen9d81094d2017-11-20 17:40:0948
Erik Chen3303fd0232018-01-11 20:29:0549 // The stack profiling mode to test.
Alexei Filippove48985e2019-02-01 00:27:4150 mojom::StackMode stack_mode = mojom::StackMode::NATIVE_WITHOUT_THREAD_NAMES;
51
Erik Chen9d81094d2017-11-20 17:40:0952 // Whether the caller has already started profiling with the given mode.
erikchen011ad3f2018-01-26 17:54:5553 // When false, the test driver is responsible for starting profiling.
Alexei Filippove48985e2019-02-01 00:27:4154 bool profiling_already_started = false;
erikchen8bc20d82018-02-14 03:21:5155
56 // Whether to test sampling.
Alexei Filippove48985e2019-02-01 00:27:4157 bool should_sample = true;
erikchen8bc20d82018-02-14 03:21:5158
59 // When set to true, the internal sampling_rate is set to 2. While this
60 // doesn't record all allocations, it should record all test allocations
61 // made in this file with exponentially high probability.
62 // When set to false, the internal sampling rate is set to 10000.
Alexei Filippove48985e2019-02-01 00:27:4163 bool sample_everything = false;
Erik Chen9d81094d2017-11-20 17:40:0964 };
65
Erik Chen74cba8c2018-04-16 18:49:4966 TestDriver();
67 ~TestDriver();
Erik Chen9d81094d2017-11-20 17:40:0968
69 // If this is called on the content::BrowserThread::UI thread, then the
70 // platform must support nested message loops. [This is currently not
71 // supported on Android].
72 //
73 // Returns whether the test run was successful. Expectation/Assertion failures
74 // will be printed via LOG(ERROR).
75 bool RunTest(const Options& options);
76
77 private:
Erik Chen74cba8c2018-04-16 18:49:4978 // Populates |has_started_| and then signals |wait_for_ui_thread_|.
79 void GetHasStartedOnUIThread();
80
Erik Chen9d81094d2017-11-20 17:40:0981 // Populates |initialization_success_| with the result of
82 // |RunInitializationOnUIThread|, and then signals |wait_for_ui_thread_|.
Erik Chen3303fd0232018-01-11 20:29:0583 void CheckOrStartProfilingOnUIThreadAndSignal();
Erik Chen9d81094d2017-11-20 17:40:0984
85 // If profiling is expected to already be started, confirm it.
86 // Otherwise, start profiling with the given mode.
Erik Chen06a20b52018-04-17 19:05:3287 // This method must only be called on platforms that supported nested run
88 // loops on the UI thread.
89 bool CheckOrStartProfilingOnUIThreadWithNestedRunLoops();
90
91 // If profiling is expected to already be started, confirm it.
92 // Otherwise, start profiling with the given mode.
93 // This method must only be called on platforms that are running the
94 // TestDriver from a non-UI thread, which allows for async signalling.
95 bool CheckOrStartProfilingOnUIThreadWithAsyncSignalling();
Erik Chen9d81094d2017-11-20 17:40:0996
97 // Performs allocations. These are expected to be profiled.
98 void MakeTestAllocations();
99
100 // Collects a trace that contains a heap dump. The result is stored in
101 // |serialized_trace_|.
102 //
103 // When |synchronous| is true, this method spins a nested message loop. When
104 // |synchronous| is false, this method posts some tasks that will eventually
105 // signal |wait_for_ui_thread_|.
106 void CollectResults(bool synchronous);
107
erikchen872446d2017-11-29 00:34:40108 void TraceFinished(base::Closure closure,
109 bool success,
110 std::string trace_json);
111
Erik Chen9d81094d2017-11-20 17:40:09112 bool ValidateBrowserAllocations(base::Value* dump_json);
113 bool ValidateRendererAllocations(base::Value* dump_json);
114
Erik Chen3303fd0232018-01-11 20:29:05115 bool ShouldProfileBrowser();
erikchenee0a0072018-01-19 21:52:04116 bool ShouldProfileRenderer();
erikchen011ad3f2018-01-26 17:54:55117 bool ShouldIncludeNativeThreadNames();
Erik Chen3303fd0232018-01-11 20:29:05118 bool HasPseudoFrames();
erikchen080ebb12018-03-21 22:21:01119 bool HasNativeFrames();
erikchen8bc20d82018-02-14 03:21:51120 bool IsRecordingAllAllocations();
Erik Chen3303fd0232018-01-11 20:29:05121
erikchenee0a0072018-01-19 21:52:04122 void WaitForProfilingToStartForAllRenderersUIThread();
123
124 // Android does not support nested RunLoops. Instead, it signals
125 // |wait_for_ui_thread_| when finished.
126 void WaitForProfilingToStartForAllRenderersUIThreadAndSignal();
127 void WaitForProfilingToStartForAllRenderersUIThreadCallback(
128 std::vector<base::ProcessId> results);
129
Erik Chen9d81094d2017-11-20 17:40:09130 Options options_;
131
132 // Allocations made by this class. Intentionally leaked, since deallocating
133 // them would trigger a large number of IPCs, which is slow.
134 std::vector<char*> leaks_;
135
136 // Sum of size of all variadic allocations.
137 size_t total_variadic_allocations_ = 0;
138
139 // Use to make PA allocations, which should also be shimmed.
140 base::PartitionAllocatorGeneric partition_allocator_;
141
142 // Contains nothing until |CollectResults| has been called.
erikchen872446d2017-11-29 00:34:40143 std::string serialized_trace_;
Erik Chen9d81094d2017-11-20 17:40:09144
145 // Whether the test was invoked on the ui thread.
146 bool running_on_ui_thread_ = true;
147
Erik Chen74cba8c2018-04-16 18:49:49148 // Whether the supervisor has started.
149 bool has_started_ = false;
150
Erik Chen9d81094d2017-11-20 17:40:09151 // Whether an error has occurred.
152 bool initialization_success_ = false;
153
Erik Chen3303fd0232018-01-11 20:29:05154 // When |true|, initialization will wait for the allocator shim to enable
155 // before continuing.
156 bool wait_for_profiling_to_start_ = false;
157
Erik Chen9d81094d2017-11-20 17:40:09158 base::WaitableEvent wait_for_ui_thread_;
159
Erik Chen74cba8c2018-04-16 18:49:49160 DISALLOW_COPY_AND_ASSIGN(TestDriver);
Erik Chen9d81094d2017-11-20 17:40:09161};
162
erikchen102fe212018-04-06 13:02:10163} // namespace heap_profiling
Erik Chen9d81094d2017-11-20 17:40:09164
Erik Chen74cba8c2018-04-16 18:49:49165#endif // COMPONENTS_HEAP_PROFILING_TEST_DRIVER_H_