1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
content / browser / tracing / startup_tracing_browsertest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/dcheck_is_on.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "components/tracing/common/tracing_switches.h"
#include "content/browser/tracing/startup_tracing_controller.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "services/tracing/perfetto/privacy_filtering_check.h"
#include "services/tracing/public/cpp/perfetto/trace_event_data_source.h"
#include "services/tracing/public/cpp/trace_startup.h"
#include "services/tracing/public/cpp/trace_startup_config.h"
#include "services/tracing/public/cpp/tracing_features.h"
#include "third_party/perfetto/include/perfetto/tracing/tracing.h"
namespace content {
namespace {
void CheckForConditionAndWaitMoreIfNeeded(
base::RepeatingCallback<bool()> condition,
base::OnceClosure quit_closure) {
if (condition.Run()) {
std::move(quit_closure).Run();
return;
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&CheckForConditionAndWaitMoreIfNeeded,
std::move(condition), std::move(quit_closure)),
TestTimeouts::tiny_timeout());
}
// Wait until |condition| returns true.
void WaitForCondition(base::RepeatingCallback<bool()> condition,
const std::string& description) {
base::RunLoop run_loop;
CheckForConditionAndWaitMoreIfNeeded(condition, run_loop.QuitClosure());
run_loop.Run();
ASSERT_TRUE(condition.Run())
<< "Timeout waiting for condition: " << description;
}
} // namespace
class StartupTracingInProcessTest : public ContentBrowserTest {
public:
StartupTracingInProcessTest() {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{features::kTracingServiceInProcess},
/*disabled_features=*/{});
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
class LargeTraceEventData : public base::trace_event::ConvertableToTraceFormat {
public:
LargeTraceEventData() = default;
LargeTraceEventData(const LargeTraceEventData&) = delete;
LargeTraceEventData& operator=(const LargeTraceEventData&) = delete;
~LargeTraceEventData() override = default;
const size_t kLargeMessageSize = 100 * 1024;
void AppendAsTraceFormat(std::string* out) const override {
std::string large_string(kLargeMessageSize, '.');
out->append(large_string);
}
};
// This will fill a massive amount of startup tracing data into a
// StartupTraceWriter, which Perfetto will then have to sync copy into
// the SMB once the full tracing service starts up. This is to catch common
// deadlocks.
// TODO(crbug.com/330909115): Re-enable this test.
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_TestFilledStartupBuffer DISABLED_TestFilledStartupBuffer
#else
#define MAYBE_TestFilledStartupBuffer TestFilledStartupBuffer
#endif
IN_PROC_BROWSER_TEST_F(StartupTracingInProcessTest,
MAYBE_TestFilledStartupBuffer) {
auto config = tracing::TraceStartupConfig::GetInstance()
.GetDefaultBackgroundStartupConfig();
CHECK(tracing::EnableStartupTracingForProcess(config));
for (int i = 0; i < 1024; ++i) {
auto data = std::make_unique<LargeTraceEventData>();
TRACE_EVENT1("toplevel", "bar", "data", std::move(data));
}
base::RunLoop wait_for_tracing;
auto session =
perfetto::Tracing::NewTrace(perfetto::BackendType::kCustomBackend);
session->Setup(config);
session->SetOnStartCallback(
[&wait_for_tracing]() { wait_for_tracing.Quit(); });
session->Start();
wait_for_tracing.Run();
EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl("", "title1.html")));
base::RunLoop wait_for_stop;
session->SetOnStopCallback([&wait_for_stop]() { wait_for_stop.Quit(); });
session->Stop();
wait_for_stop.Run();
}
namespace {
enum class FinishType {
kWaitForTimeout,
kStopExplicitly,
};
std::ostream& operator<<(std::ostream& o, FinishType type) {
switch (type) {
case FinishType::kStopExplicitly:
o << "Stop";
return o;
case FinishType::kWaitForTimeout:
o << "Wait";
return o;
}
}
enum class OutputType {
kProto,
kJSON,
};
std::ostream& operator<<(std::ostream& o, OutputType type) {
switch (type) {
case OutputType::kJSON:
o << "json";
return o;
case OutputType::kProto:
o << "proto";
return o;
}
}
enum class OutputLocation {
// Write trace to a given file.
kGivenFile,
// Write trace into a given directory (basename will be set to trace1 before
// starting).
kDirectoryWithDefaultBasename,
// Write trace into a given directory (basename will be set to trace1 before
// starting, and updated to trace2 before calling Stop()).
kDirectoryWithBasenameUpdatedBeforeStop,
};
std::ostream& operator<<(std::ostream& o, OutputLocation type) {
switch (type) {
case OutputLocation::kGivenFile:
o << "file";
return o;
case OutputLocation::kDirectoryWithDefaultBasename:
o << "dir/trace1";
return o;
case OutputLocation::kDirectoryWithBasenameUpdatedBeforeStop:
o << "dir/trace2";
return o;
}
}
} // namespace
class StartupTracingTest
: public ContentBrowserTest,
public testing::WithParamInterface<
std::tuple<FinishType, OutputType, OutputLocation>> {
public:
StartupTracingTest() = default;
StartupTracingTest(const StartupTracingTest&) = delete;
StartupTracingTest& operator=(const StartupTracingTest&) = delete;
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kTraceStartup);
if (GetFinishType() == FinishType::kWaitForTimeout) {
command_line->AppendSwitchASCII(switches::kTraceStartupDuration, "3");
} else {
command_line->AppendSwitchASCII(switches::kTraceStartupDuration, "0");
}
command_line->AppendSwitchASCII(switches::kTraceStartupFormat,
GetOutputTypeAsString());
if (GetOutputLocation() == OutputLocation::kGivenFile) {
base::CreateTemporaryFile(&temp_file_path_);
} else {
base::CreateNewTempDirectory(base::FilePath::StringType(),
&temp_file_path_);
temp_file_path_ = temp_file_path_.AsEndingWithSeparator();
}
command_line->AppendSwitchASCII(switches::kEnableTracingOutput,
temp_file_path_.AsUTF8Unsafe());
if (GetOutputLocation() != OutputLocation::kGivenFile) {
// --enable-tracing-format switch should be initialised before
// calling SetDefaultBasenameForTest, which forces the creation of
// TraceStartupConfig, which queries the command line flags and
// stores the snapshot.
StartupTracingController::GetInstance().SetDefaultBasenameForTest(
"trace1",
StartupTracingController::ExtensionType::kAppendAppropriate);
}
}
FinishType GetFinishType() { return std::get<0>(GetParam()); }
OutputType GetOutputType() { return std::get<1>(GetParam()); }
std::string GetOutputTypeAsString() {
switch (GetOutputType()) {
case OutputType::kJSON:
return "json";
case OutputType::kProto:
return "pftrace";
}
}
OutputLocation GetOutputLocation() { return std::get<2>(GetParam()); }
base::FilePath GetExpectedPath() {
std::string filename;
switch (GetOutputLocation()) {
case OutputLocation::kGivenFile:
return temp_file_path_;
case OutputLocation::kDirectoryWithDefaultBasename:
filename = "trace1";
break;
case OutputLocation::kDirectoryWithBasenameUpdatedBeforeStop:
filename = "trace2";
break;
}
// Renames are not supported together with timeouts.
if (GetFinishType() == FinishType::kWaitForTimeout)
filename = "trace1";
return temp_file_path_.AppendASCII(filename + "." +
GetOutputTypeAsString());
}
static void CheckOutput(base::FilePath path, OutputType output_type) {
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
// Skip checks because the thread sanitizer is often too slow to flush trace
// data correctly within the timeouts. We still run the tests on TSAN to
// catch general threading issues.
#else // !(BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER))
std::string trace;
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(base::ReadFileToString(path, &trace))
<< "Failed to read file " << path;
if (output_type == OutputType::kJSON) {
EXPECT_TRUE(base::JSONReader::Read(trace));
}
// Both proto and json should have the trace event name recorded somewhere
// as a substring. We check for "ThreadControllerImpl::RunTask" because
// it's an example of event that happens early in the trace, but any other
// early event will do. The event has to happen early because in
// WaitForTimeout and in EmergencyStop tests we don't wait for
// TracingSession::StartBlocking() to complete.
EXPECT_TRUE(trace.find("ThreadControllerImpl::RunTask") !=
std::string::npos);
#endif // !(BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER))
}
void Wait() {
if (GetFinishType() == FinishType::kWaitForTimeout) {
WaitForCondition(base::BindRepeating([]() {
return StartupTracingController::GetInstance()
.is_finished_for_testing();
}),
"finish file write");
} else {
StartupTracingController::GetInstance().WaitUntilStopped();
}
}
protected:
base::FilePath temp_file_path_;
private:
base::test::ScopedRunLoopTimeout increased_timeout_{
FROM_HERE, TestTimeouts::test_launcher_timeout()};
};
INSTANTIATE_TEST_SUITE_P(
All,
StartupTracingTest,
testing::Combine(
testing::Values(FinishType::kStopExplicitly,
FinishType::kWaitForTimeout),
testing::Values(OutputType::kJSON, OutputType::kProto),
testing::Values(
OutputLocation::kGivenFile,
OutputLocation::kDirectoryWithDefaultBasename,
OutputLocation::kDirectoryWithBasenameUpdatedBeforeStop)));
// TODO(crbug.com/40900782): Re-enable this test.
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_TestEnableTracing DISABLED_TestEnableTracing
#else
#define MAYBE_TestEnableTracing TestEnableTracing
#endif
IN_PROC_BROWSER_TEST_P(StartupTracingTest, MAYBE_TestEnableTracing) {
EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl("", "title1.html")));
if (GetOutputLocation() ==
OutputLocation::kDirectoryWithBasenameUpdatedBeforeStop) {
StartupTracingController::GetInstance().SetDefaultBasenameForTest(
"trace2", StartupTracingController::ExtensionType::kAppendAppropriate);
}
Wait();
CheckOutput(GetExpectedPath(), GetOutputType());
}
// TODO(ssid): Fix the flaky tests, probably the same reason as
// crbug.com/1041392.
IN_PROC_BROWSER_TEST_P(StartupTracingTest, DISABLED_ContinueAtShutdown) {
EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl("", "title1.html")));
StartupTracingController::GetInstance()
.set_continue_on_shutdown_for_testing();
}
class EmergencyStopTracingTest : public StartupTracingTest {};
INSTANTIATE_TEST_SUITE_P(
All,
EmergencyStopTracingTest,
testing::Combine(
testing::Values(FinishType::kStopExplicitly),
testing::Values(OutputType::kJSON, OutputType::kProto),
testing::Values(OutputLocation::kDirectoryWithDefaultBasename)));
// TODO(crbug.com/40900782): Re-enable this test.
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_StopOnUIThread DISABLED_StopOnUIThread
#else
#define MAYBE_StopOnUIThread StopOnUIThread
#endif
IN_PROC_BROWSER_TEST_P(EmergencyStopTracingTest, MAYBE_StopOnUIThread) {
EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl("", "title1.html")));
StartupTracingController::EmergencyStop();
CheckOutput(GetExpectedPath(), GetOutputType());
}
// TODO(crbug.com/40900782): Re-enable this test.
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_StopOnThreadPool DISABLED_StopOnThreadPool
#else
#define MAYBE_StopOnThreadPool StopOnThreadPool
#endif
IN_PROC_BROWSER_TEST_P(EmergencyStopTracingTest, MAYBE_StopOnThreadPool) {
EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl("", "title1.html")));
auto expected_path = GetExpectedPath();
auto output_type = GetOutputType();
base::RunLoop run_loop;
base::ThreadPool::PostTask(FROM_HERE, base::BindLambdaForTesting([&]() {
StartupTracingController::EmergencyStop();
CheckOutput(expected_path, output_type);
run_loop.Quit();
}));
run_loop.Run();
}
// TODO(crbug.com/40900782): Re-enable this test.
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_StopOnThreadPoolTwice DISABLED_StopOnThreadPoolTwice
#else
#define MAYBE_StopOnThreadPoolTwice StopOnThreadPoolTwice
#endif
IN_PROC_BROWSER_TEST_P(EmergencyStopTracingTest, MAYBE_StopOnThreadPoolTwice) {
EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl("", "title1.html")));
auto expected_path = GetExpectedPath();
auto output_type = GetOutputType();
base::RunLoop run_loop1;
base::RunLoop run_loop2;
base::ThreadPool::PostTask(FROM_HERE, base::BindLambdaForTesting([&]() {
StartupTracingController::EmergencyStop();
CheckOutput(expected_path, output_type);
run_loop1.Quit();
}));
base::ThreadPool::PostTask(FROM_HERE, base::BindLambdaForTesting([&]() {
StartupTracingController::EmergencyStop();
CheckOutput(expected_path, output_type);
run_loop2.Quit();
}));
run_loop1.Run();
run_loop2.Run();
}
} // namespace content