1 // Copyright (c) 2012 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.
5 // Test of classes in the tracked_objects.h classes.
7 #include "base/tracked_objects.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/process/process_handle.h"
13 #include "base/time/time.h"
14 #include "base/tracking_info.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 const int kLineNumber
= 1776;
18 const char kFile
[] = "FixedUnitTestFileName";
19 const char kWorkerThreadName
[] = "WorkerThread-1";
20 const char kMainThreadName
[] = "SomeMainThreadName";
21 const char kStillAlive
[] = "Still_Alive";
23 namespace tracked_objects
{
25 class TrackedObjectsTest
: public testing::Test
{
27 TrackedObjectsTest() {
28 // On entry, leak any database structures in case they are still in use by
30 ThreadData::ShutdownSingleThreadedCleanup(true);
33 ThreadData::SetAlternateTimeSource(&TrackedObjectsTest::GetTestTime
);
34 ThreadData::now_function_is_time_
= true;
37 ~TrackedObjectsTest() override
{
38 // We should not need to leak any structures we create, since we are
39 // single threaded, and carefully accounting for items.
40 ThreadData::ShutdownSingleThreadedCleanup(false);
43 // Reset the profiler state.
45 ThreadData::ShutdownSingleThreadedCleanup(false);
49 // Simulate a birth on the thread named |thread_name|, at the given
51 void TallyABirth(const Location
& location
, const std::string
& thread_name
) {
52 // If the |thread_name| is empty, we don't initialize system with a thread
53 // name, so we're viewed as a worker thread.
54 if (!thread_name
.empty())
55 ThreadData::InitializeThreadContext(kMainThreadName
);
57 // Do not delete |birth|. We don't own it.
58 Births
* birth
= ThreadData::TallyABirthIfActive(location
);
60 if (ThreadData::status() == ThreadData::DEACTIVATED
)
61 EXPECT_EQ(reinterpret_cast<Births
*>(NULL
), birth
);
63 EXPECT_NE(reinterpret_cast<Births
*>(NULL
), birth
);
66 // Helper function to verify the most common test expectations.
67 void ExpectSimpleProcessData(const ProcessDataSnapshot
& process_data
,
68 const std::string
& function_name
,
69 const std::string
& birth_thread
,
70 const std::string
& death_thread
,
74 ASSERT_EQ(1u, process_data
.phased_snapshots
.size());
75 auto it
= process_data
.phased_snapshots
.find(0);
76 ASSERT_TRUE(it
!= process_data
.phased_snapshots
.end());
77 const ProcessDataPhaseSnapshot
& process_data_phase
= it
->second
;
79 ASSERT_EQ(1u, process_data_phase
.tasks
.size());
81 EXPECT_EQ(kFile
, process_data_phase
.tasks
[0].birth
.location
.file_name
);
82 EXPECT_EQ(function_name
,
83 process_data_phase
.tasks
[0].birth
.location
.function_name
);
84 EXPECT_EQ(kLineNumber
,
85 process_data_phase
.tasks
[0].birth
.location
.line_number
);
87 EXPECT_EQ(birth_thread
, process_data_phase
.tasks
[0].birth
.thread_name
);
89 EXPECT_EQ(count
, process_data_phase
.tasks
[0].death_data
.count
);
90 EXPECT_EQ(count
* run_ms
,
91 process_data_phase
.tasks
[0].death_data
.run_duration_sum
);
92 EXPECT_EQ(run_ms
, process_data_phase
.tasks
[0].death_data
.run_duration_max
);
94 process_data_phase
.tasks
[0].death_data
.run_duration_sample
);
95 EXPECT_EQ(count
* queue_ms
,
96 process_data_phase
.tasks
[0].death_data
.queue_duration_sum
);
98 process_data_phase
.tasks
[0].death_data
.queue_duration_max
);
100 process_data_phase
.tasks
[0].death_data
.queue_duration_sample
);
102 EXPECT_EQ(death_thread
, process_data_phase
.tasks
[0].death_thread_name
);
104 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
107 // Sets time that will be returned by ThreadData::Now().
108 static void SetTestTime(unsigned int test_time
) { test_time_
= test_time
; }
111 // Returns test time in milliseconds.
112 static unsigned int GetTestTime() { return test_time_
; }
114 // Test time in milliseconds.
115 static unsigned int test_time_
;
119 unsigned int TrackedObjectsTest::test_time_
;
121 TEST_F(TrackedObjectsTest
, TaskStopwatchNoStartStop
) {
122 if (!ThreadData::InitializeAndSetTrackingStatus(
123 ThreadData::PROFILING_ACTIVE
)) {
124 // Don't run the test if task tracking is not compiled in.
128 // Check that creating and destroying a stopwatch without starting it doesn't
130 TaskStopwatch stopwatch
;
133 TEST_F(TrackedObjectsTest
, MinimalStartupShutdown
) {
134 // Minimal test doesn't even create any tasks.
135 if (!ThreadData::InitializeAndSetTrackingStatus(
136 ThreadData::PROFILING_ACTIVE
)) {
137 // Don't run the test if task tracking is not compiled in.
141 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
142 ThreadData
* data
= ThreadData::Get();
143 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
145 EXPECT_FALSE(data
->next());
146 EXPECT_EQ(data
, ThreadData::Get());
147 ThreadData::BirthMap birth_map
;
148 ThreadData::DeathsSnapshot deaths
;
149 data
->SnapshotMaps(0, &birth_map
, &deaths
);
150 EXPECT_EQ(0u, birth_map
.size());
151 EXPECT_EQ(0u, deaths
.size());
153 // Clean up with no leaking.
156 // Do it again, just to be sure we reset state completely.
158 ThreadData::InitializeAndSetTrackingStatus(ThreadData::PROFILING_ACTIVE
));
159 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
160 data
= ThreadData::Get();
161 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
163 EXPECT_FALSE(data
->next());
164 EXPECT_EQ(data
, ThreadData::Get());
167 data
->SnapshotMaps(0, &birth_map
, &deaths
);
168 EXPECT_EQ(0u, birth_map
.size());
169 EXPECT_EQ(0u, deaths
.size());
172 TEST_F(TrackedObjectsTest
, TinyStartupShutdown
) {
173 if (!ThreadData::InitializeAndSetTrackingStatus(
174 ThreadData::PROFILING_ACTIVE
)) {
175 // Don't run the test if task tracking is not compiled in.
179 // Instigate tracking on a single tracked object, on our thread.
180 const char kFunction
[] = "TinyStartupShutdown";
181 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
182 ThreadData::TallyABirthIfActive(location
);
184 ThreadData
* data
= ThreadData::first();
186 EXPECT_FALSE(data
->next());
187 EXPECT_EQ(data
, ThreadData::Get());
188 ThreadData::BirthMap birth_map
;
189 ThreadData::DeathsSnapshot deaths
;
190 data
->SnapshotMaps(0, &birth_map
, &deaths
);
191 EXPECT_EQ(1u, birth_map
.size()); // 1 birth location.
192 EXPECT_EQ(1, birth_map
.begin()->second
->birth_count()); // 1 birth.
193 EXPECT_EQ(0u, deaths
.size()); // No deaths.
196 // Now instigate another birth, while we are timing the run of the first
198 // Create a child (using the same birth location).
199 // TrackingInfo will call TallyABirth() during construction.
200 const int32 start_time
= 1;
201 base::TimeTicks kBogusBirthTime
= base::TimeTicks() +
202 base::TimeDelta::FromMilliseconds(start_time
);
203 base::TrackingInfo
pending_task(location
, kBogusBirthTime
);
205 TaskStopwatch stopwatch
;
207 // Finally conclude the outer run.
208 const int32 time_elapsed
= 1000;
209 SetTestTime(start_time
+ time_elapsed
);
212 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
216 data
->SnapshotMaps(0, &birth_map
, &deaths
);
217 EXPECT_EQ(1u, birth_map
.size()); // 1 birth location.
218 EXPECT_EQ(2, birth_map
.begin()->second
->birth_count()); // 2 births.
219 EXPECT_EQ(1u, deaths
.size()); // 1 location.
220 EXPECT_EQ(1, deaths
.begin()->second
.death_data
.count
); // 1 death.
222 // The births were at the same location as the one known death.
223 EXPECT_EQ(birth_map
.begin()->second
, deaths
.begin()->first
);
225 ProcessDataSnapshot process_data
;
226 ThreadData::Snapshot(0, &process_data
);
228 ASSERT_EQ(1u, process_data
.phased_snapshots
.size());
229 auto it
= process_data
.phased_snapshots
.find(0);
230 ASSERT_TRUE(it
!= process_data
.phased_snapshots
.end());
231 const ProcessDataPhaseSnapshot
& process_data_phase
= it
->second
;
232 ASSERT_EQ(1u, process_data_phase
.tasks
.size());
233 EXPECT_EQ(kFile
, process_data_phase
.tasks
[0].birth
.location
.file_name
);
235 process_data_phase
.tasks
[0].birth
.location
.function_name
);
236 EXPECT_EQ(kLineNumber
,
237 process_data_phase
.tasks
[0].birth
.location
.line_number
);
238 EXPECT_EQ(kWorkerThreadName
, process_data_phase
.tasks
[0].birth
.thread_name
);
239 EXPECT_EQ(1, process_data_phase
.tasks
[0].death_data
.count
);
240 EXPECT_EQ(time_elapsed
,
241 process_data_phase
.tasks
[0].death_data
.run_duration_sum
);
242 EXPECT_EQ(time_elapsed
,
243 process_data_phase
.tasks
[0].death_data
.run_duration_max
);
244 EXPECT_EQ(time_elapsed
,
245 process_data_phase
.tasks
[0].death_data
.run_duration_sample
);
246 EXPECT_EQ(0, process_data_phase
.tasks
[0].death_data
.queue_duration_sum
);
247 EXPECT_EQ(0, process_data_phase
.tasks
[0].death_data
.queue_duration_max
);
248 EXPECT_EQ(0, process_data_phase
.tasks
[0].death_data
.queue_duration_sample
);
249 EXPECT_EQ(kWorkerThreadName
, process_data_phase
.tasks
[0].death_thread_name
);
252 TEST_F(TrackedObjectsTest
, DeathDataTestRecordDeath
) {
253 if (!ThreadData::InitializeAndSetTrackingStatus(
254 ThreadData::PROFILING_ACTIVE
)) {
255 // Don't run the test if task tracking is not compiled in.
259 scoped_ptr
<DeathData
> data(new DeathData());
260 ASSERT_NE(data
, reinterpret_cast<DeathData
*>(NULL
));
261 EXPECT_EQ(data
->run_duration_sum(), 0);
262 EXPECT_EQ(data
->run_duration_max(), 0);
263 EXPECT_EQ(data
->run_duration_sample(), 0);
264 EXPECT_EQ(data
->queue_duration_sum(), 0);
265 EXPECT_EQ(data
->queue_duration_max(), 0);
266 EXPECT_EQ(data
->queue_duration_sample(), 0);
267 EXPECT_EQ(data
->count(), 0);
268 EXPECT_EQ(nullptr, data
->last_phase_snapshot());
273 const int kUnrandomInt
= 0; // Fake random int that ensure we sample data.
274 data
->RecordDeath(queue_ms
, run_ms
, kUnrandomInt
);
275 EXPECT_EQ(data
->run_duration_sum(), run_ms
);
276 EXPECT_EQ(data
->run_duration_max(), run_ms
);
277 EXPECT_EQ(data
->run_duration_sample(), run_ms
);
278 EXPECT_EQ(data
->queue_duration_sum(), queue_ms
);
279 EXPECT_EQ(data
->queue_duration_max(), queue_ms
);
280 EXPECT_EQ(data
->queue_duration_sample(), queue_ms
);
281 EXPECT_EQ(data
->count(), 1);
282 EXPECT_EQ(nullptr, data
->last_phase_snapshot());
284 data
->RecordDeath(queue_ms
, run_ms
, kUnrandomInt
);
285 EXPECT_EQ(data
->run_duration_sum(), run_ms
+ run_ms
);
286 EXPECT_EQ(data
->run_duration_max(), run_ms
);
287 EXPECT_EQ(data
->run_duration_sample(), run_ms
);
288 EXPECT_EQ(data
->queue_duration_sum(), queue_ms
+ queue_ms
);
289 EXPECT_EQ(data
->queue_duration_max(), queue_ms
);
290 EXPECT_EQ(data
->queue_duration_sample(), queue_ms
);
291 EXPECT_EQ(data
->count(), 2);
292 EXPECT_EQ(nullptr, data
->last_phase_snapshot());
295 TEST_F(TrackedObjectsTest
, DeathDataTest2Phases
) {
296 if (!ThreadData::InitializeAndSetTrackingStatus(
297 ThreadData::PROFILING_ACTIVE
)) {
298 // Don't run the test if task tracking is not compiled in.
302 scoped_ptr
<DeathData
> data(new DeathData());
303 ASSERT_NE(data
, reinterpret_cast<DeathData
*>(NULL
));
308 const int kUnrandomInt
= 0; // Fake random int that ensure we sample data.
309 data
->RecordDeath(queue_ms
, run_ms
, kUnrandomInt
);
310 data
->RecordDeath(queue_ms
, run_ms
, kUnrandomInt
);
312 data
->OnProfilingPhaseCompleted(123);
313 EXPECT_EQ(data
->run_duration_sum(), run_ms
+ run_ms
);
314 EXPECT_EQ(data
->run_duration_max(), 0);
315 EXPECT_EQ(data
->run_duration_sample(), run_ms
);
316 EXPECT_EQ(data
->queue_duration_sum(), queue_ms
+ queue_ms
);
317 EXPECT_EQ(data
->queue_duration_max(), 0);
318 EXPECT_EQ(data
->queue_duration_sample(), queue_ms
);
319 EXPECT_EQ(data
->count(), 2);
320 ASSERT_NE(nullptr, data
->last_phase_snapshot());
321 EXPECT_EQ(123, data
->last_phase_snapshot()->profiling_phase
);
322 EXPECT_EQ(2, data
->last_phase_snapshot()->death_data
.count
);
323 EXPECT_EQ(2 * run_ms
,
324 data
->last_phase_snapshot()->death_data
.run_duration_sum
);
325 EXPECT_EQ(run_ms
, data
->last_phase_snapshot()->death_data
.run_duration_max
);
327 data
->last_phase_snapshot()->death_data
.run_duration_sample
);
328 EXPECT_EQ(2 * queue_ms
,
329 data
->last_phase_snapshot()->death_data
.queue_duration_sum
);
331 data
->last_phase_snapshot()->death_data
.queue_duration_max
);
333 data
->last_phase_snapshot()->death_data
.queue_duration_sample
);
334 EXPECT_EQ(nullptr, data
->last_phase_snapshot()->prev
);
339 data
->RecordDeath(queue_ms1
, run_ms1
, kUnrandomInt
);
340 EXPECT_EQ(data
->run_duration_sum(), run_ms
+ run_ms
+ run_ms1
);
341 EXPECT_EQ(data
->run_duration_max(), run_ms1
);
342 EXPECT_EQ(data
->run_duration_sample(), run_ms1
);
343 EXPECT_EQ(data
->queue_duration_sum(), queue_ms
+ queue_ms
+ queue_ms1
);
344 EXPECT_EQ(data
->queue_duration_max(), queue_ms1
);
345 EXPECT_EQ(data
->queue_duration_sample(), queue_ms1
);
346 EXPECT_EQ(data
->count(), 3);
347 ASSERT_NE(nullptr, data
->last_phase_snapshot());
348 EXPECT_EQ(123, data
->last_phase_snapshot()->profiling_phase
);
349 EXPECT_EQ(2, data
->last_phase_snapshot()->death_data
.count
);
350 EXPECT_EQ(2 * run_ms
,
351 data
->last_phase_snapshot()->death_data
.run_duration_sum
);
352 EXPECT_EQ(run_ms
, data
->last_phase_snapshot()->death_data
.run_duration_max
);
354 data
->last_phase_snapshot()->death_data
.run_duration_sample
);
355 EXPECT_EQ(2 * queue_ms
,
356 data
->last_phase_snapshot()->death_data
.queue_duration_sum
);
358 data
->last_phase_snapshot()->death_data
.queue_duration_max
);
360 data
->last_phase_snapshot()->death_data
.queue_duration_sample
);
361 EXPECT_EQ(nullptr, data
->last_phase_snapshot()->prev
);
364 TEST_F(TrackedObjectsTest
, Delta
) {
365 if (!ThreadData::InitializeAndSetTrackingStatus(
366 ThreadData::PROFILING_ACTIVE
)) {
367 // Don't run the test if task tracking is not compiled in.
371 DeathDataSnapshot snapshot
;
373 snapshot
.run_duration_sum
= 100;
374 snapshot
.run_duration_max
= 50;
375 snapshot
.run_duration_sample
= 25;
376 snapshot
.queue_duration_sum
= 200;
377 snapshot
.queue_duration_max
= 101;
378 snapshot
.queue_duration_sample
= 26;
380 DeathDataSnapshot older_snapshot
;
381 older_snapshot
.count
= 2;
382 older_snapshot
.run_duration_sum
= 95;
383 older_snapshot
.run_duration_max
= 48;
384 older_snapshot
.run_duration_sample
= 22;
385 older_snapshot
.queue_duration_sum
= 190;
386 older_snapshot
.queue_duration_max
= 99;
387 older_snapshot
.queue_duration_sample
= 21;
389 const DeathDataSnapshot
& delta
= snapshot
.Delta(older_snapshot
);
390 EXPECT_EQ(8, delta
.count
);
391 EXPECT_EQ(5, delta
.run_duration_sum
);
392 EXPECT_EQ(50, delta
.run_duration_max
);
393 EXPECT_EQ(25, delta
.run_duration_sample
);
394 EXPECT_EQ(10, delta
.queue_duration_sum
);
395 EXPECT_EQ(101, delta
.queue_duration_max
);
396 EXPECT_EQ(26, delta
.queue_duration_sample
);
399 TEST_F(TrackedObjectsTest
, DeactivatedBirthOnlyToSnapshotWorkerThread
) {
400 // Start in the deactivated state.
401 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED
)) {
402 // Don't run the test if task tracking is not compiled in.
406 const char kFunction
[] = "DeactivatedBirthOnlyToSnapshotWorkerThread";
407 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
408 TallyABirth(location
, std::string());
410 ProcessDataSnapshot process_data
;
411 ThreadData::Snapshot(0, &process_data
);
413 ASSERT_EQ(1u, process_data
.phased_snapshots
.size());
415 auto it
= process_data
.phased_snapshots
.find(0);
416 ASSERT_TRUE(it
!= process_data
.phased_snapshots
.end());
417 const ProcessDataPhaseSnapshot
& process_data_phase
= it
->second
;
419 ASSERT_EQ(0u, process_data_phase
.tasks
.size());
421 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
424 TEST_F(TrackedObjectsTest
, DeactivatedBirthOnlyToSnapshotMainThread
) {
425 // Start in the deactivated state.
426 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED
)) {
427 // Don't run the test if task tracking is not compiled in.
431 const char kFunction
[] = "DeactivatedBirthOnlyToSnapshotMainThread";
432 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
433 TallyABirth(location
, kMainThreadName
);
435 ProcessDataSnapshot process_data
;
436 ThreadData::Snapshot(0, &process_data
);
438 ASSERT_EQ(1u, process_data
.phased_snapshots
.size());
440 auto it
= process_data
.phased_snapshots
.find(0);
441 ASSERT_TRUE(it
!= process_data
.phased_snapshots
.end());
442 const ProcessDataPhaseSnapshot
& process_data_phase
= it
->second
;
444 ASSERT_EQ(0u, process_data_phase
.tasks
.size());
446 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
449 TEST_F(TrackedObjectsTest
, BirthOnlyToSnapshotWorkerThread
) {
450 if (!ThreadData::InitializeAndSetTrackingStatus(
451 ThreadData::PROFILING_ACTIVE
)) {
452 // Don't run the test if task tracking is not compiled in.
456 const char kFunction
[] = "BirthOnlyToSnapshotWorkerThread";
457 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
458 TallyABirth(location
, std::string());
460 ProcessDataSnapshot process_data
;
461 ThreadData::Snapshot(0, &process_data
);
462 ExpectSimpleProcessData(process_data
, kFunction
, kWorkerThreadName
,
463 kStillAlive
, 1, 0, 0);
466 TEST_F(TrackedObjectsTest
, BirthOnlyToSnapshotMainThread
) {
467 if (!ThreadData::InitializeAndSetTrackingStatus(
468 ThreadData::PROFILING_ACTIVE
)) {
469 // Don't run the test if task tracking is not compiled in.
473 const char kFunction
[] = "BirthOnlyToSnapshotMainThread";
474 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
475 TallyABirth(location
, kMainThreadName
);
477 ProcessDataSnapshot process_data
;
478 ThreadData::Snapshot(0, &process_data
);
479 ExpectSimpleProcessData(process_data
, kFunction
, kMainThreadName
, kStillAlive
,
483 TEST_F(TrackedObjectsTest
, LifeCycleToSnapshotMainThread
) {
484 if (!ThreadData::InitializeAndSetTrackingStatus(
485 ThreadData::PROFILING_ACTIVE
)) {
486 // Don't run the test if task tracking is not compiled in.
490 const char kFunction
[] = "LifeCycleToSnapshotMainThread";
491 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
492 TallyABirth(location
, kMainThreadName
);
494 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
495 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
496 // TrackingInfo will call TallyABirth() during construction.
497 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
498 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
500 const unsigned int kStartOfRun
= 5;
501 const unsigned int kEndOfRun
= 7;
502 SetTestTime(kStartOfRun
);
503 TaskStopwatch stopwatch
;
505 SetTestTime(kEndOfRun
);
508 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
510 ProcessDataSnapshot process_data
;
511 ThreadData::Snapshot(0, &process_data
);
512 ExpectSimpleProcessData(process_data
, kFunction
, kMainThreadName
,
513 kMainThreadName
, 1, 2, 4);
516 TEST_F(TrackedObjectsTest
, TwoPhases
) {
517 if (!ThreadData::InitializeAndSetTrackingStatus(
518 ThreadData::PROFILING_ACTIVE
)) {
519 // Don't run the test if task tracking is not compiled in.
523 const char kFunction
[] = "TwoPhases";
524 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
525 TallyABirth(location
, kMainThreadName
);
527 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
528 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
529 // TrackingInfo will call TallyABirth() during construction.
530 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
531 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
533 const unsigned int kStartOfRun
= 5;
534 const unsigned int kEndOfRun
= 7;
535 SetTestTime(kStartOfRun
);
536 TaskStopwatch stopwatch
;
538 SetTestTime(kEndOfRun
);
541 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
543 ThreadData::OnProfilingPhaseCompleted(0);
545 TallyABirth(location
, kMainThreadName
);
547 const TrackedTime kTimePosted1
= TrackedTime::FromMilliseconds(9);
548 const base::TimeTicks kDelayedStartTime1
= base::TimeTicks();
549 // TrackingInfo will call TallyABirth() during construction.
550 base::TrackingInfo
pending_task1(location
, kDelayedStartTime1
);
551 pending_task1
.time_posted
= kTimePosted1
; // Overwrite implied Now().
553 const unsigned int kStartOfRun1
= 11;
554 const unsigned int kEndOfRun1
= 21;
555 SetTestTime(kStartOfRun1
);
556 TaskStopwatch stopwatch1
;
558 SetTestTime(kEndOfRun1
);
561 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task1
, stopwatch1
);
563 ProcessDataSnapshot process_data
;
564 ThreadData::Snapshot(1, &process_data
);
566 ASSERT_EQ(2u, process_data
.phased_snapshots
.size());
568 auto it0
= process_data
.phased_snapshots
.find(0);
569 ASSERT_TRUE(it0
!= process_data
.phased_snapshots
.end());
570 const ProcessDataPhaseSnapshot
& process_data_phase0
= it0
->second
;
572 ASSERT_EQ(1u, process_data_phase0
.tasks
.size());
574 EXPECT_EQ(kFile
, process_data_phase0
.tasks
[0].birth
.location
.file_name
);
576 process_data_phase0
.tasks
[0].birth
.location
.function_name
);
577 EXPECT_EQ(kLineNumber
,
578 process_data_phase0
.tasks
[0].birth
.location
.line_number
);
580 EXPECT_EQ(kMainThreadName
, process_data_phase0
.tasks
[0].birth
.thread_name
);
582 EXPECT_EQ(1, process_data_phase0
.tasks
[0].death_data
.count
);
583 EXPECT_EQ(2, process_data_phase0
.tasks
[0].death_data
.run_duration_sum
);
584 EXPECT_EQ(2, process_data_phase0
.tasks
[0].death_data
.run_duration_max
);
585 EXPECT_EQ(2, process_data_phase0
.tasks
[0].death_data
.run_duration_sample
);
586 EXPECT_EQ(4, process_data_phase0
.tasks
[0].death_data
.queue_duration_sum
);
587 EXPECT_EQ(4, process_data_phase0
.tasks
[0].death_data
.queue_duration_max
);
588 EXPECT_EQ(4, process_data_phase0
.tasks
[0].death_data
.queue_duration_sample
);
590 EXPECT_EQ(kMainThreadName
, process_data_phase0
.tasks
[0].death_thread_name
);
592 auto it1
= process_data
.phased_snapshots
.find(1);
593 ASSERT_TRUE(it1
!= process_data
.phased_snapshots
.end());
594 const ProcessDataPhaseSnapshot
& process_data_phase1
= it1
->second
;
596 ASSERT_EQ(1u, process_data_phase1
.tasks
.size());
598 EXPECT_EQ(kFile
, process_data_phase1
.tasks
[0].birth
.location
.file_name
);
600 process_data_phase1
.tasks
[0].birth
.location
.function_name
);
601 EXPECT_EQ(kLineNumber
,
602 process_data_phase1
.tasks
[0].birth
.location
.line_number
);
604 EXPECT_EQ(kMainThreadName
, process_data_phase1
.tasks
[0].birth
.thread_name
);
606 EXPECT_EQ(1, process_data_phase1
.tasks
[0].death_data
.count
);
607 EXPECT_EQ(10, process_data_phase1
.tasks
[0].death_data
.run_duration_sum
);
608 EXPECT_EQ(10, process_data_phase1
.tasks
[0].death_data
.run_duration_max
);
609 EXPECT_EQ(10, process_data_phase1
.tasks
[0].death_data
.run_duration_sample
);
610 EXPECT_EQ(2, process_data_phase1
.tasks
[0].death_data
.queue_duration_sum
);
611 EXPECT_EQ(2, process_data_phase1
.tasks
[0].death_data
.queue_duration_max
);
612 EXPECT_EQ(2, process_data_phase1
.tasks
[0].death_data
.queue_duration_sample
);
614 EXPECT_EQ(kMainThreadName
, process_data_phase1
.tasks
[0].death_thread_name
);
616 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
619 TEST_F(TrackedObjectsTest
, ThreePhases
) {
620 if (!ThreadData::InitializeAndSetTrackingStatus(
621 ThreadData::PROFILING_ACTIVE
)) {
622 // Don't run the test if task tracking is not compiled in.
626 const char kFunction
[] = "ThreePhases";
627 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
631 TallyABirth(location
, kMainThreadName
);
633 // TrackingInfo will call TallyABirth() during construction.
635 base::TrackingInfo
pending_task(location
, base::TimeTicks());
638 TaskStopwatch stopwatch
;
643 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
646 ThreadData::OnProfilingPhaseCompleted(0);
650 TallyABirth(location
, kMainThreadName
);
653 base::TrackingInfo
pending_task(location
, base::TimeTicks());
656 TaskStopwatch stopwatch
;
661 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
664 ThreadData::OnProfilingPhaseCompleted(1);
668 TallyABirth(location
, kMainThreadName
);
670 // TrackingInfo will call TallyABirth() during construction.
672 base::TrackingInfo
pending_task(location
, base::TimeTicks());
675 TaskStopwatch stopwatch
;
680 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
683 // Snapshot and check results.
684 ProcessDataSnapshot process_data
;
685 ThreadData::Snapshot(2, &process_data
);
687 ASSERT_EQ(3u, process_data
.phased_snapshots
.size());
689 auto it0
= process_data
.phased_snapshots
.find(0);
690 ASSERT_TRUE(it0
!= process_data
.phased_snapshots
.end());
691 const ProcessDataPhaseSnapshot
& process_data_phase0
= it0
->second
;
693 ASSERT_EQ(1u, process_data_phase0
.tasks
.size());
695 EXPECT_EQ(kFile
, process_data_phase0
.tasks
[0].birth
.location
.file_name
);
697 process_data_phase0
.tasks
[0].birth
.location
.function_name
);
698 EXPECT_EQ(kLineNumber
,
699 process_data_phase0
.tasks
[0].birth
.location
.line_number
);
701 EXPECT_EQ(kMainThreadName
, process_data_phase0
.tasks
[0].birth
.thread_name
);
703 EXPECT_EQ(1, process_data_phase0
.tasks
[0].death_data
.count
);
704 EXPECT_EQ(6, process_data_phase0
.tasks
[0].death_data
.run_duration_sum
);
705 EXPECT_EQ(6, process_data_phase0
.tasks
[0].death_data
.run_duration_max
);
706 EXPECT_EQ(6, process_data_phase0
.tasks
[0].death_data
.run_duration_sample
);
707 EXPECT_EQ(7, process_data_phase0
.tasks
[0].death_data
.queue_duration_sum
);
708 EXPECT_EQ(7, process_data_phase0
.tasks
[0].death_data
.queue_duration_max
);
709 EXPECT_EQ(7, process_data_phase0
.tasks
[0].death_data
.queue_duration_sample
);
711 EXPECT_EQ(kMainThreadName
, process_data_phase0
.tasks
[0].death_thread_name
);
713 auto it1
= process_data
.phased_snapshots
.find(1);
714 ASSERT_TRUE(it1
!= process_data
.phased_snapshots
.end());
715 const ProcessDataPhaseSnapshot
& process_data_phase1
= it1
->second
;
717 ASSERT_EQ(1u, process_data_phase1
.tasks
.size());
719 EXPECT_EQ(kFile
, process_data_phase1
.tasks
[0].birth
.location
.file_name
);
721 process_data_phase1
.tasks
[0].birth
.location
.function_name
);
722 EXPECT_EQ(kLineNumber
,
723 process_data_phase1
.tasks
[0].birth
.location
.line_number
);
725 EXPECT_EQ(kMainThreadName
, process_data_phase1
.tasks
[0].birth
.thread_name
);
727 EXPECT_EQ(1, process_data_phase1
.tasks
[0].death_data
.count
);
728 EXPECT_EQ(4, process_data_phase1
.tasks
[0].death_data
.run_duration_sum
);
729 EXPECT_EQ(4, process_data_phase1
.tasks
[0].death_data
.run_duration_max
);
730 EXPECT_EQ(4, process_data_phase1
.tasks
[0].death_data
.run_duration_sample
);
731 EXPECT_EQ(5, process_data_phase1
.tasks
[0].death_data
.queue_duration_sum
);
732 EXPECT_EQ(5, process_data_phase1
.tasks
[0].death_data
.queue_duration_max
);
733 EXPECT_EQ(5, process_data_phase1
.tasks
[0].death_data
.queue_duration_sample
);
735 EXPECT_EQ(kMainThreadName
, process_data_phase1
.tasks
[0].death_thread_name
);
737 auto it2
= process_data
.phased_snapshots
.find(2);
738 ASSERT_TRUE(it2
!= process_data
.phased_snapshots
.end());
739 const ProcessDataPhaseSnapshot
& process_data_phase2
= it2
->second
;
741 ASSERT_EQ(1u, process_data_phase2
.tasks
.size());
743 EXPECT_EQ(kFile
, process_data_phase2
.tasks
[0].birth
.location
.file_name
);
745 process_data_phase2
.tasks
[0].birth
.location
.function_name
);
746 EXPECT_EQ(kLineNumber
,
747 process_data_phase2
.tasks
[0].birth
.location
.line_number
);
749 EXPECT_EQ(kMainThreadName
, process_data_phase2
.tasks
[0].birth
.thread_name
);
751 EXPECT_EQ(1, process_data_phase2
.tasks
[0].death_data
.count
);
752 EXPECT_EQ(2, process_data_phase2
.tasks
[0].death_data
.run_duration_sum
);
753 EXPECT_EQ(2, process_data_phase2
.tasks
[0].death_data
.run_duration_max
);
754 EXPECT_EQ(2, process_data_phase2
.tasks
[0].death_data
.run_duration_sample
);
755 EXPECT_EQ(3, process_data_phase2
.tasks
[0].death_data
.queue_duration_sum
);
756 EXPECT_EQ(3, process_data_phase2
.tasks
[0].death_data
.queue_duration_max
);
757 EXPECT_EQ(3, process_data_phase2
.tasks
[0].death_data
.queue_duration_sample
);
759 EXPECT_EQ(kMainThreadName
, process_data_phase2
.tasks
[0].death_thread_name
);
761 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
764 TEST_F(TrackedObjectsTest
, TwoPhasesSecondEmpty
) {
765 if (!ThreadData::InitializeAndSetTrackingStatus(
766 ThreadData::PROFILING_ACTIVE
)) {
767 // Don't run the test if task tracking is not compiled in.
771 const char kFunction
[] = "TwoPhasesSecondEmpty";
772 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
773 ThreadData::InitializeThreadContext(kMainThreadName
);
775 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
776 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
777 // TrackingInfo will call TallyABirth() during construction.
778 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
779 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
781 const unsigned int kStartOfRun
= 5;
782 const unsigned int kEndOfRun
= 7;
783 SetTestTime(kStartOfRun
);
784 TaskStopwatch stopwatch
;
786 SetTestTime(kEndOfRun
);
789 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
791 ThreadData::OnProfilingPhaseCompleted(0);
793 ProcessDataSnapshot process_data
;
794 ThreadData::Snapshot(1, &process_data
);
796 ASSERT_EQ(2u, process_data
.phased_snapshots
.size());
798 auto it0
= process_data
.phased_snapshots
.find(0);
799 ASSERT_TRUE(it0
!= process_data
.phased_snapshots
.end());
800 const ProcessDataPhaseSnapshot
& process_data_phase0
= it0
->second
;
802 ASSERT_EQ(1u, process_data_phase0
.tasks
.size());
804 EXPECT_EQ(kFile
, process_data_phase0
.tasks
[0].birth
.location
.file_name
);
806 process_data_phase0
.tasks
[0].birth
.location
.function_name
);
807 EXPECT_EQ(kLineNumber
,
808 process_data_phase0
.tasks
[0].birth
.location
.line_number
);
810 EXPECT_EQ(kMainThreadName
, process_data_phase0
.tasks
[0].birth
.thread_name
);
812 EXPECT_EQ(1, process_data_phase0
.tasks
[0].death_data
.count
);
813 EXPECT_EQ(2, process_data_phase0
.tasks
[0].death_data
.run_duration_sum
);
814 EXPECT_EQ(2, process_data_phase0
.tasks
[0].death_data
.run_duration_max
);
815 EXPECT_EQ(2, process_data_phase0
.tasks
[0].death_data
.run_duration_sample
);
816 EXPECT_EQ(4, process_data_phase0
.tasks
[0].death_data
.queue_duration_sum
);
817 EXPECT_EQ(4, process_data_phase0
.tasks
[0].death_data
.queue_duration_max
);
818 EXPECT_EQ(4, process_data_phase0
.tasks
[0].death_data
.queue_duration_sample
);
820 EXPECT_EQ(kMainThreadName
, process_data_phase0
.tasks
[0].death_thread_name
);
822 auto it1
= process_data
.phased_snapshots
.find(1);
823 ASSERT_TRUE(it1
!= process_data
.phased_snapshots
.end());
824 const ProcessDataPhaseSnapshot
& process_data_phase1
= it1
->second
;
826 ASSERT_EQ(0u, process_data_phase1
.tasks
.size());
828 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
831 TEST_F(TrackedObjectsTest
, TwoPhasesFirstEmpty
) {
832 if (!ThreadData::InitializeAndSetTrackingStatus(
833 ThreadData::PROFILING_ACTIVE
)) {
834 // Don't run the test if task tracking is not compiled in.
838 ThreadData::OnProfilingPhaseCompleted(0);
840 const char kFunction
[] = "TwoPhasesSecondEmpty";
841 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
842 ThreadData::InitializeThreadContext(kMainThreadName
);
844 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
845 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
846 // TrackingInfo will call TallyABirth() during construction.
847 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
848 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
850 const unsigned int kStartOfRun
= 5;
851 const unsigned int kEndOfRun
= 7;
852 SetTestTime(kStartOfRun
);
853 TaskStopwatch stopwatch
;
855 SetTestTime(kEndOfRun
);
858 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
860 ProcessDataSnapshot process_data
;
861 ThreadData::Snapshot(1, &process_data
);
863 ASSERT_EQ(1u, process_data
.phased_snapshots
.size());
865 auto it1
= process_data
.phased_snapshots
.find(1);
866 ASSERT_TRUE(it1
!= process_data
.phased_snapshots
.end());
867 const ProcessDataPhaseSnapshot
& process_data_phase1
= it1
->second
;
869 ASSERT_EQ(1u, process_data_phase1
.tasks
.size());
871 EXPECT_EQ(kFile
, process_data_phase1
.tasks
[0].birth
.location
.file_name
);
873 process_data_phase1
.tasks
[0].birth
.location
.function_name
);
874 EXPECT_EQ(kLineNumber
,
875 process_data_phase1
.tasks
[0].birth
.location
.line_number
);
877 EXPECT_EQ(kMainThreadName
, process_data_phase1
.tasks
[0].birth
.thread_name
);
879 EXPECT_EQ(1, process_data_phase1
.tasks
[0].death_data
.count
);
880 EXPECT_EQ(2, process_data_phase1
.tasks
[0].death_data
.run_duration_sum
);
881 EXPECT_EQ(2, process_data_phase1
.tasks
[0].death_data
.run_duration_max
);
882 EXPECT_EQ(2, process_data_phase1
.tasks
[0].death_data
.run_duration_sample
);
883 EXPECT_EQ(4, process_data_phase1
.tasks
[0].death_data
.queue_duration_sum
);
884 EXPECT_EQ(4, process_data_phase1
.tasks
[0].death_data
.queue_duration_max
);
885 EXPECT_EQ(4, process_data_phase1
.tasks
[0].death_data
.queue_duration_sample
);
887 EXPECT_EQ(kMainThreadName
, process_data_phase1
.tasks
[0].death_thread_name
);
889 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
892 // We will deactivate tracking after the birth, and before the death, and
893 // demonstrate that the lifecycle is completely tallied. This ensures that
894 // our tallied births are matched by tallied deaths (except for when the
895 // task is still running, or is queued).
896 TEST_F(TrackedObjectsTest
, LifeCycleMidDeactivatedToSnapshotMainThread
) {
897 if (!ThreadData::InitializeAndSetTrackingStatus(
898 ThreadData::PROFILING_ACTIVE
)) {
899 // Don't run the test if task tracking is not compiled in.
903 const char kFunction
[] = "LifeCycleMidDeactivatedToSnapshotMainThread";
904 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
905 TallyABirth(location
, kMainThreadName
);
907 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
908 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
909 // TrackingInfo will call TallyABirth() during construction.
910 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
911 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
913 // Turn off tracking now that we have births.
915 ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED
));
917 const unsigned int kStartOfRun
= 5;
918 const unsigned int kEndOfRun
= 7;
919 SetTestTime(kStartOfRun
);
920 TaskStopwatch stopwatch
;
922 SetTestTime(kEndOfRun
);
925 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
927 ProcessDataSnapshot process_data
;
928 ThreadData::Snapshot(0, &process_data
);
929 ExpectSimpleProcessData(process_data
, kFunction
, kMainThreadName
,
930 kMainThreadName
, 1, 2, 4);
933 // We will deactivate tracking before starting a life cycle, and neither
934 // the birth nor the death will be recorded.
935 TEST_F(TrackedObjectsTest
, LifeCyclePreDeactivatedToSnapshotMainThread
) {
936 // Start in the deactivated state.
937 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED
)) {
938 // Don't run the test if task tracking is not compiled in.
942 const char kFunction
[] = "LifeCyclePreDeactivatedToSnapshotMainThread";
943 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
944 TallyABirth(location
, kMainThreadName
);
946 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
947 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
948 // TrackingInfo will call TallyABirth() during construction.
949 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
950 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
952 const unsigned int kStartOfRun
= 5;
953 const unsigned int kEndOfRun
= 7;
954 SetTestTime(kStartOfRun
);
955 TaskStopwatch stopwatch
;
957 SetTestTime(kEndOfRun
);
960 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
962 ProcessDataSnapshot process_data
;
963 ThreadData::Snapshot(0, &process_data
);
965 ASSERT_EQ(1u, process_data
.phased_snapshots
.size());
967 auto it
= process_data
.phased_snapshots
.find(0);
968 ASSERT_TRUE(it
!= process_data
.phased_snapshots
.end());
969 const ProcessDataPhaseSnapshot
& process_data_phase
= it
->second
;
971 ASSERT_EQ(0u, process_data_phase
.tasks
.size());
973 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
976 TEST_F(TrackedObjectsTest
, TwoLives
) {
977 if (!ThreadData::InitializeAndSetTrackingStatus(
978 ThreadData::PROFILING_ACTIVE
)) {
979 // Don't run the test if task tracking is not compiled in.
983 const char kFunction
[] = "TwoLives";
984 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
985 TallyABirth(location
, kMainThreadName
);
987 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
988 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
989 // TrackingInfo will call TallyABirth() during construction.
990 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
991 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
993 const unsigned int kStartOfRun
= 5;
994 const unsigned int kEndOfRun
= 7;
995 SetTestTime(kStartOfRun
);
996 TaskStopwatch stopwatch
;
998 SetTestTime(kEndOfRun
);
1001 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
1003 // TrackingInfo will call TallyABirth() during construction.
1004 base::TrackingInfo
pending_task2(location
, kDelayedStartTime
);
1005 pending_task2
.time_posted
= kTimePosted
; // Overwrite implied Now().
1006 SetTestTime(kStartOfRun
);
1007 TaskStopwatch stopwatch2
;
1009 SetTestTime(kEndOfRun
);
1012 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task2
, stopwatch2
);
1014 ProcessDataSnapshot process_data
;
1015 ThreadData::Snapshot(0, &process_data
);
1016 ExpectSimpleProcessData(process_data
, kFunction
, kMainThreadName
,
1017 kMainThreadName
, 2, 2, 4);
1020 TEST_F(TrackedObjectsTest
, DifferentLives
) {
1021 if (!ThreadData::InitializeAndSetTrackingStatus(
1022 ThreadData::PROFILING_ACTIVE
)) {
1023 // Don't run the test if task tracking is not compiled in.
1027 // Use a well named thread.
1028 ThreadData::InitializeThreadContext(kMainThreadName
);
1029 const char kFunction
[] = "DifferentLives";
1030 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
1032 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
1033 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
1034 // TrackingInfo will call TallyABirth() during construction.
1035 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
1036 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
1038 const unsigned int kStartOfRun
= 5;
1039 const unsigned int kEndOfRun
= 7;
1040 SetTestTime(kStartOfRun
);
1041 TaskStopwatch stopwatch
;
1043 SetTestTime(kEndOfRun
);
1046 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, stopwatch
);
1048 const int kSecondFakeLineNumber
= 999;
1049 Location
second_location(kFunction
, kFile
, kSecondFakeLineNumber
, NULL
);
1051 // TrackingInfo will call TallyABirth() during construction.
1052 base::TrackingInfo
pending_task2(second_location
, kDelayedStartTime
);
1053 pending_task2
.time_posted
= kTimePosted
; // Overwrite implied Now().
1055 ProcessDataSnapshot process_data
;
1056 ThreadData::Snapshot(0, &process_data
);
1058 ASSERT_EQ(1u, process_data
.phased_snapshots
.size());
1059 auto it
= process_data
.phased_snapshots
.find(0);
1060 ASSERT_TRUE(it
!= process_data
.phased_snapshots
.end());
1061 const ProcessDataPhaseSnapshot
& process_data_phase
= it
->second
;
1063 ASSERT_EQ(2u, process_data_phase
.tasks
.size());
1065 EXPECT_EQ(kFile
, process_data_phase
.tasks
[0].birth
.location
.file_name
);
1066 EXPECT_EQ(kFunction
,
1067 process_data_phase
.tasks
[0].birth
.location
.function_name
);
1068 EXPECT_EQ(kLineNumber
,
1069 process_data_phase
.tasks
[0].birth
.location
.line_number
);
1070 EXPECT_EQ(kMainThreadName
, process_data_phase
.tasks
[0].birth
.thread_name
);
1071 EXPECT_EQ(1, process_data_phase
.tasks
[0].death_data
.count
);
1072 EXPECT_EQ(2, process_data_phase
.tasks
[0].death_data
.run_duration_sum
);
1073 EXPECT_EQ(2, process_data_phase
.tasks
[0].death_data
.run_duration_max
);
1074 EXPECT_EQ(2, process_data_phase
.tasks
[0].death_data
.run_duration_sample
);
1075 EXPECT_EQ(4, process_data_phase
.tasks
[0].death_data
.queue_duration_sum
);
1076 EXPECT_EQ(4, process_data_phase
.tasks
[0].death_data
.queue_duration_max
);
1077 EXPECT_EQ(4, process_data_phase
.tasks
[0].death_data
.queue_duration_sample
);
1078 EXPECT_EQ(kMainThreadName
, process_data_phase
.tasks
[0].death_thread_name
);
1079 EXPECT_EQ(kFile
, process_data_phase
.tasks
[1].birth
.location
.file_name
);
1080 EXPECT_EQ(kFunction
,
1081 process_data_phase
.tasks
[1].birth
.location
.function_name
);
1082 EXPECT_EQ(kSecondFakeLineNumber
,
1083 process_data_phase
.tasks
[1].birth
.location
.line_number
);
1084 EXPECT_EQ(kMainThreadName
, process_data_phase
.tasks
[1].birth
.thread_name
);
1085 EXPECT_EQ(1, process_data_phase
.tasks
[1].death_data
.count
);
1086 EXPECT_EQ(0, process_data_phase
.tasks
[1].death_data
.run_duration_sum
);
1087 EXPECT_EQ(0, process_data_phase
.tasks
[1].death_data
.run_duration_max
);
1088 EXPECT_EQ(0, process_data_phase
.tasks
[1].death_data
.run_duration_sample
);
1089 EXPECT_EQ(0, process_data_phase
.tasks
[1].death_data
.queue_duration_sum
);
1090 EXPECT_EQ(0, process_data_phase
.tasks
[1].death_data
.queue_duration_max
);
1091 EXPECT_EQ(0, process_data_phase
.tasks
[1].death_data
.queue_duration_sample
);
1092 EXPECT_EQ(kStillAlive
, process_data_phase
.tasks
[1].death_thread_name
);
1093 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
1096 TEST_F(TrackedObjectsTest
, TaskWithNestedExclusion
) {
1097 if (!ThreadData::InitializeAndSetTrackingStatus(
1098 ThreadData::PROFILING_ACTIVE
)) {
1099 // Don't run the test if task tracking is not compiled in.
1103 const char kFunction
[] = "TaskWithNestedExclusion";
1104 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
1105 TallyABirth(location
, kMainThreadName
);
1107 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
1108 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
1109 // TrackingInfo will call TallyABirth() during construction.
1110 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
1111 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
1114 TaskStopwatch task_stopwatch
;
1115 task_stopwatch
.Start();
1118 TaskStopwatch exclusion_stopwatch
;
1119 exclusion_stopwatch
.Start();
1121 exclusion_stopwatch
.Stop();
1124 task_stopwatch
.Stop();
1126 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, task_stopwatch
);
1128 ProcessDataSnapshot process_data
;
1129 ThreadData::Snapshot(0, &process_data
);
1130 ExpectSimpleProcessData(process_data
, kFunction
, kMainThreadName
,
1131 kMainThreadName
, 1, 6, 4);
1134 TEST_F(TrackedObjectsTest
, TaskWith2NestedExclusions
) {
1135 if (!ThreadData::InitializeAndSetTrackingStatus(
1136 ThreadData::PROFILING_ACTIVE
)) {
1137 // Don't run the test if task tracking is not compiled in.
1141 const char kFunction
[] = "TaskWith2NestedExclusions";
1142 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
1143 TallyABirth(location
, kMainThreadName
);
1145 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
1146 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
1147 // TrackingInfo will call TallyABirth() during construction.
1148 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
1149 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
1152 TaskStopwatch task_stopwatch
;
1153 task_stopwatch
.Start();
1156 TaskStopwatch exclusion_stopwatch
;
1157 exclusion_stopwatch
.Start();
1159 exclusion_stopwatch
.Stop();
1162 TaskStopwatch exclusion_stopwatch2
;
1163 exclusion_stopwatch2
.Start();
1165 exclusion_stopwatch2
.Stop();
1168 task_stopwatch
.Stop();
1170 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, task_stopwatch
);
1172 ProcessDataSnapshot process_data
;
1173 ThreadData::Snapshot(0, &process_data
);
1174 ExpectSimpleProcessData(process_data
, kFunction
, kMainThreadName
,
1175 kMainThreadName
, 1, 13, 4);
1178 TEST_F(TrackedObjectsTest
, TaskWithNestedExclusionWithNestedTask
) {
1179 if (!ThreadData::InitializeAndSetTrackingStatus(
1180 ThreadData::PROFILING_ACTIVE
)) {
1181 // Don't run the test if task tracking is not compiled in.
1185 const char kFunction
[] = "TaskWithNestedExclusionWithNestedTask";
1186 Location
location(kFunction
, kFile
, kLineNumber
, NULL
);
1188 const int kSecondFakeLineNumber
= 999;
1190 TallyABirth(location
, kMainThreadName
);
1192 const TrackedTime kTimePosted
= TrackedTime::FromMilliseconds(1);
1193 const base::TimeTicks kDelayedStartTime
= base::TimeTicks();
1194 // TrackingInfo will call TallyABirth() during construction.
1195 base::TrackingInfo
pending_task(location
, kDelayedStartTime
);
1196 pending_task
.time_posted
= kTimePosted
; // Overwrite implied Now().
1199 TaskStopwatch task_stopwatch
;
1200 task_stopwatch
.Start();
1203 TaskStopwatch exclusion_stopwatch
;
1204 exclusion_stopwatch
.Start();
1206 Location
second_location(kFunction
, kFile
, kSecondFakeLineNumber
, NULL
);
1207 base::TrackingInfo
nested_task(second_location
, kDelayedStartTime
);
1208 // Overwrite implied Now().
1209 nested_task
.time_posted
= TrackedTime::FromMilliseconds(8);
1211 TaskStopwatch nested_task_stopwatch
;
1212 nested_task_stopwatch
.Start();
1214 nested_task_stopwatch
.Stop();
1215 ThreadData::TallyRunOnNamedThreadIfTracking(
1216 nested_task
, nested_task_stopwatch
);
1219 exclusion_stopwatch
.Stop();
1222 task_stopwatch
.Stop();
1224 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task
, task_stopwatch
);
1226 ProcessDataSnapshot process_data
;
1227 ThreadData::Snapshot(0, &process_data
);
1229 ASSERT_EQ(1u, process_data
.phased_snapshots
.size());
1230 auto it
= process_data
.phased_snapshots
.find(0);
1231 ASSERT_TRUE(it
!= process_data
.phased_snapshots
.end());
1232 const ProcessDataPhaseSnapshot
& process_data_phase
= it
->second
;
1234 // The order in which the two task follow is platform-dependent.
1236 (process_data_phase
.tasks
[0].birth
.location
.line_number
== kLineNumber
)
1241 ASSERT_EQ(2u, process_data_phase
.tasks
.size());
1242 EXPECT_EQ(kFile
, process_data_phase
.tasks
[t0
].birth
.location
.file_name
);
1243 EXPECT_EQ(kFunction
,
1244 process_data_phase
.tasks
[t0
].birth
.location
.function_name
);
1245 EXPECT_EQ(kLineNumber
,
1246 process_data_phase
.tasks
[t0
].birth
.location
.line_number
);
1247 EXPECT_EQ(kMainThreadName
, process_data_phase
.tasks
[t0
].birth
.thread_name
);
1248 EXPECT_EQ(1, process_data_phase
.tasks
[t0
].death_data
.count
);
1249 EXPECT_EQ(6, process_data_phase
.tasks
[t0
].death_data
.run_duration_sum
);
1250 EXPECT_EQ(6, process_data_phase
.tasks
[t0
].death_data
.run_duration_max
);
1251 EXPECT_EQ(6, process_data_phase
.tasks
[t0
].death_data
.run_duration_sample
);
1252 EXPECT_EQ(4, process_data_phase
.tasks
[t0
].death_data
.queue_duration_sum
);
1253 EXPECT_EQ(4, process_data_phase
.tasks
[t0
].death_data
.queue_duration_max
);
1254 EXPECT_EQ(4, process_data_phase
.tasks
[t0
].death_data
.queue_duration_sample
);
1255 EXPECT_EQ(kMainThreadName
, process_data_phase
.tasks
[t0
].death_thread_name
);
1256 EXPECT_EQ(kFile
, process_data_phase
.tasks
[t1
].birth
.location
.file_name
);
1257 EXPECT_EQ(kFunction
,
1258 process_data_phase
.tasks
[t1
].birth
.location
.function_name
);
1259 EXPECT_EQ(kSecondFakeLineNumber
,
1260 process_data_phase
.tasks
[t1
].birth
.location
.line_number
);
1261 EXPECT_EQ(kMainThreadName
, process_data_phase
.tasks
[t1
].birth
.thread_name
);
1262 EXPECT_EQ(1, process_data_phase
.tasks
[t1
].death_data
.count
);
1263 EXPECT_EQ(2, process_data_phase
.tasks
[t1
].death_data
.run_duration_sum
);
1264 EXPECT_EQ(2, process_data_phase
.tasks
[t1
].death_data
.run_duration_max
);
1265 EXPECT_EQ(2, process_data_phase
.tasks
[t1
].death_data
.run_duration_sample
);
1266 EXPECT_EQ(1, process_data_phase
.tasks
[t1
].death_data
.queue_duration_sum
);
1267 EXPECT_EQ(1, process_data_phase
.tasks
[t1
].death_data
.queue_duration_max
);
1268 EXPECT_EQ(1, process_data_phase
.tasks
[t1
].death_data
.queue_duration_sample
);
1269 EXPECT_EQ(kMainThreadName
, process_data_phase
.tasks
[t1
].death_thread_name
);
1270 EXPECT_EQ(base::GetCurrentProcId(), process_data
.process_id
);
1273 } // namespace tracked_objects