blob: 3ca7d746fd595704a57b1215e4d7f1f265480e84 [file] [log] [blame]
fischman@chromium.org998561e2012-01-24 07:56:41 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5// Test of classes in the tracked_objects.h classes.
6
7#include "base/tracked_objects.h"
phajdan.jr@chromium.org6dce3c22009-06-04 00:01:29 +09008
mostynb@opera.comf29dd612013-09-04 08:29:12 +09009#include <stddef.h>
10
jar@chromium.org666ef9c2011-10-25 03:55:16 +090011#include "base/memory/scoped_ptr.h"
rsesek@chromium.org687756f2013-07-26 06:38:23 +090012#include "base/process/process_handle.h"
avi@chromium.orgb45ec932013-06-29 00:14:18 +090013#include "base/time/time.h"
mostynb@opera.comf29dd612013-09-04 08:29:12 +090014#include "base/tracking_info.h"
initial.commit3f4a7322008-07-27 06:49:38 +090015#include "testing/gtest/include/gtest/gtest.h"
16
isherman@chromium.org98c10d22012-04-13 09:39:26 +090017const int kLineNumber = 1776;
18const char kFile[] = "FixedUnitTestFileName";
19const char kWorkerThreadName[] = "WorkerThread-1";
20const char kMainThreadName[] = "SomeMainThreadName";
21const char kStillAlive[] = "Still_Alive";
22
initial.commit3f4a7322008-07-27 06:49:38 +090023namespace tracked_objects {
24
25class TrackedObjectsTest : public testing::Test {
jar@chromium.org7ad4a622011-11-07 04:14:48 +090026 protected:
jar@chromium.org4be2cb02011-11-01 07:36:21 +090027 TrackedObjectsTest() {
28 // On entry, leak any database structures in case they are still in use by
29 // prior threads.
30 ThreadData::ShutdownSingleThreadedCleanup(true);
vadimt29787df2014-09-16 04:19:38 +090031
32 test_time_ = 0;
33 ThreadData::SetAlternateTimeSource(&TrackedObjectsTest::GetTestTime);
34 ThreadData::now_function_is_time_ = true;
jar@chromium.org4be2cb02011-11-01 07:36:21 +090035 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +090036
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +090037 virtual ~TrackedObjectsTest() {
jar@chromium.org4be2cb02011-11-01 07:36:21 +090038 // 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);
41 }
jar@chromium.org7ad4a622011-11-07 04:14:48 +090042
isherman@chromium.org98c10d22012-04-13 09:39:26 +090043 // Reset the profiler state.
44 void Reset() {
45 ThreadData::ShutdownSingleThreadedCleanup(false);
vadimt29787df2014-09-16 04:19:38 +090046 test_time_ = 0;
isherman@chromium.org98c10d22012-04-13 09:39:26 +090047 }
48
49 // Simulate a birth on the thread named |thread_name|, at the given
50 // |location|.
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);
56
57 // Do not delete |birth|. We don't own it.
58 Births* birth = ThreadData::TallyABirthIfActive(location);
59
60 if (ThreadData::status() == ThreadData::DEACTIVATED)
61 EXPECT_EQ(reinterpret_cast<Births*>(NULL), birth);
62 else
63 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth);
64 }
65
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,
71 int count,
72 int run_ms,
73 int queue_ms) {
74 ASSERT_EQ(1u, process_data.tasks.size());
75
76 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name);
77 EXPECT_EQ(function_name,
78 process_data.tasks[0].birth.location.function_name);
79 EXPECT_EQ(kLineNumber, process_data.tasks[0].birth.location.line_number);
80
81 EXPECT_EQ(birth_thread, process_data.tasks[0].birth.thread_name);
82
83 EXPECT_EQ(count, process_data.tasks[0].death_data.count);
84 EXPECT_EQ(count * run_ms,
85 process_data.tasks[0].death_data.run_duration_sum);
86 EXPECT_EQ(run_ms, process_data.tasks[0].death_data.run_duration_max);
87 EXPECT_EQ(run_ms, process_data.tasks[0].death_data.run_duration_sample);
88 EXPECT_EQ(count * queue_ms,
89 process_data.tasks[0].death_data.queue_duration_sum);
90 EXPECT_EQ(queue_ms, process_data.tasks[0].death_data.queue_duration_max);
91 EXPECT_EQ(queue_ms, process_data.tasks[0].death_data.queue_duration_sample);
92
93 EXPECT_EQ(death_thread, process_data.tasks[0].death_thread_name);
94
95 EXPECT_EQ(0u, process_data.descendants.size());
96
97 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
jar@chromium.org7ad4a622011-11-07 04:14:48 +090098 }
vadimt29787df2014-09-16 04:19:38 +090099
100 // Sets time that will be returned by ThreadData::Now().
101 static void SetTestTime(unsigned int test_time) { test_time_ = test_time; }
102
103 private:
104 // Returns test time in milliseconds.
105 static unsigned int GetTestTime() { return test_time_; }
106
107 // Test time in milliseconds.
108 static unsigned int test_time_;
initial.commit3f4a7322008-07-27 06:49:38 +0900109};
110
vadimt29787df2014-09-16 04:19:38 +0900111// static
112unsigned int TrackedObjectsTest::test_time_;
113
114
darin@google.comaca48f52008-08-26 15:44:38 +0900115TEST_F(TrackedObjectsTest, MinimalStartupShutdown) {
initial.commit3f4a7322008-07-27 06:49:38 +0900116 // Minimal test doesn't even create any tasks.
jar@chromium.org23b00722012-02-11 04:43:42 +0900117 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900118 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
initial.commit3f4a7322008-07-27 06:49:38 +0900119 return;
vadimt29787df2014-09-16 04:19:38 +0900120 }
initial.commit3f4a7322008-07-27 06:49:38 +0900121
122 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
jar@chromium.org79a58c32011-10-16 08:52:45 +0900123 ThreadData* data = ThreadData::Get();
initial.commit3f4a7322008-07-27 06:49:38 +0900124 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900125 ASSERT_TRUE(data);
126 EXPECT_FALSE(data->next());
jar@chromium.org79a58c32011-10-16 08:52:45 +0900127 EXPECT_EQ(data, ThreadData::Get());
initial.commit3f4a7322008-07-27 06:49:38 +0900128 ThreadData::BirthMap birth_map;
initial.commit3f4a7322008-07-27 06:49:38 +0900129 ThreadData::DeathMap death_map;
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900130 ThreadData::ParentChildSet parent_child_set;
131 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set);
jar@chromium.orga0260412011-12-04 16:19:10 +0900132 EXPECT_EQ(0u, birth_map.size());
initial.commit3f4a7322008-07-27 06:49:38 +0900133 EXPECT_EQ(0u, death_map.size());
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900134 EXPECT_EQ(0u, parent_child_set.size());
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900135
136 // Clean up with no leaking.
137 Reset();
initial.commit3f4a7322008-07-27 06:49:38 +0900138
139 // Do it again, just to be sure we reset state completely.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900140 EXPECT_TRUE(ThreadData::InitializeAndSetTrackingStatus(
141 ThreadData::PROFILING_CHILDREN_ACTIVE));
initial.commit3f4a7322008-07-27 06:49:38 +0900142 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
jar@chromium.org79a58c32011-10-16 08:52:45 +0900143 data = ThreadData::Get();
initial.commit3f4a7322008-07-27 06:49:38 +0900144 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900145 ASSERT_TRUE(data);
146 EXPECT_FALSE(data->next());
jar@chromium.org79a58c32011-10-16 08:52:45 +0900147 EXPECT_EQ(data, ThreadData::Get());
initial.commit3f4a7322008-07-27 06:49:38 +0900148 birth_map.clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900149 death_map.clear();
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900150 parent_child_set.clear();
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900151 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set);
jar@chromium.orga0260412011-12-04 16:19:10 +0900152 EXPECT_EQ(0u, birth_map.size());
initial.commit3f4a7322008-07-27 06:49:38 +0900153 EXPECT_EQ(0u, death_map.size());
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900154 EXPECT_EQ(0u, parent_child_set.size());
initial.commit3f4a7322008-07-27 06:49:38 +0900155}
156
darin@google.comaca48f52008-08-26 15:44:38 +0900157TEST_F(TrackedObjectsTest, TinyStartupShutdown) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900158 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900159 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
initial.commit3f4a7322008-07-27 06:49:38 +0900160 return;
vadimt29787df2014-09-16 04:19:38 +0900161 }
initial.commit3f4a7322008-07-27 06:49:38 +0900162
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900163 // Instigate tracking on a single tracked object, on our thread.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900164 const char kFunction[] = "TinyStartupShutdown";
165 Location location(kFunction, kFile, kLineNumber, NULL);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900166 Births* first_birth = ThreadData::TallyABirthIfActive(location);
initial.commit3f4a7322008-07-27 06:49:38 +0900167
jar@chromium.orga0260412011-12-04 16:19:10 +0900168 ThreadData* data = ThreadData::first();
ajwong@chromium.org7211f7d2011-06-24 08:20:39 +0900169 ASSERT_TRUE(data);
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900170 EXPECT_FALSE(data->next());
jar@chromium.org79a58c32011-10-16 08:52:45 +0900171 EXPECT_EQ(data, ThreadData::Get());
initial.commit3f4a7322008-07-27 06:49:38 +0900172 ThreadData::BirthMap birth_map;
jar@chromium.orga0260412011-12-04 16:19:10 +0900173 ThreadData::DeathMap death_map;
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900174 ThreadData::ParentChildSet parent_child_set;
175 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set);
initial.commit3f4a7322008-07-27 06:49:38 +0900176 EXPECT_EQ(1u, birth_map.size()); // 1 birth location.
177 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth.
initial.commit3f4a7322008-07-27 06:49:38 +0900178 EXPECT_EQ(0u, death_map.size()); // No deaths.
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900179 EXPECT_EQ(0u, parent_child_set.size()); // No children.
initial.commit3f4a7322008-07-27 06:49:38 +0900180
181
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900182 // Now instigate another birth, while we are timing the run of the first
183 // execution.
vadimt29787df2014-09-16 04:19:38 +0900184 ThreadData::PrepareForStartOfRun(first_birth);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900185 // Create a child (using the same birth location).
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900186 // TrackingInfo will call TallyABirth() during construction.
vadimt29787df2014-09-16 04:19:38 +0900187 const int32 start_time = 1;
188 base::TimeTicks kBogusBirthTime = base::TimeTicks() +
189 base::TimeDelta::FromMilliseconds(start_time);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900190 base::TrackingInfo pending_task(location, kBogusBirthTime);
vadimt29787df2014-09-16 04:19:38 +0900191 SetTestTime(1);
192 TaskStopwatch stopwatch;
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900193 // Finally conclude the outer run.
vadimt29787df2014-09-16 04:19:38 +0900194 const int32 time_elapsed = 1000;
195 SetTestTime(start_time + time_elapsed);
196 stopwatch.Stop();
197
198 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
initial.commit3f4a7322008-07-27 06:49:38 +0900199
200 birth_map.clear();
jar@chromium.orga0260412011-12-04 16:19:10 +0900201 death_map.clear();
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900202 parent_child_set.clear();
203 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set);
initial.commit3f4a7322008-07-27 06:49:38 +0900204 EXPECT_EQ(1u, birth_map.size()); // 1 birth location.
205 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births.
initial.commit3f4a7322008-07-27 06:49:38 +0900206 EXPECT_EQ(1u, death_map.size()); // 1 location.
207 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death.
jar@chromium.org23b00722012-02-11 04:43:42 +0900208 if (ThreadData::TrackingParentChildStatus()) {
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900209 EXPECT_EQ(1u, parent_child_set.size()); // 1 child.
210 EXPECT_EQ(parent_child_set.begin()->first,
211 parent_child_set.begin()->second);
212 } else {
213 EXPECT_EQ(0u, parent_child_set.size()); // no stats.
214 }
initial.commit3f4a7322008-07-27 06:49:38 +0900215
216 // The births were at the same location as the one known death.
217 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first);
218
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900219 ProcessDataSnapshot process_data;
220 ThreadData::Snapshot(false, &process_data);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900221
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900222 ASSERT_EQ(1u, process_data.tasks.size());
223 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name);
224 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name);
225 EXPECT_EQ(kLineNumber, process_data.tasks[0].birth.location.line_number);
226 EXPECT_EQ(kWorkerThreadName, process_data.tasks[0].birth.thread_name);
227 EXPECT_EQ(1, process_data.tasks[0].death_data.count);
228 EXPECT_EQ(time_elapsed, process_data.tasks[0].death_data.run_duration_sum);
229 EXPECT_EQ(time_elapsed, process_data.tasks[0].death_data.run_duration_max);
230 EXPECT_EQ(time_elapsed, process_data.tasks[0].death_data.run_duration_sample);
231 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sum);
232 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_max);
233 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sample);
234 EXPECT_EQ(kWorkerThreadName, process_data.tasks[0].death_thread_name);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900235
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900236 if (ThreadData::TrackingParentChildStatus()) {
237 ASSERT_EQ(1u, process_data.descendants.size());
238 EXPECT_EQ(kFile, process_data.descendants[0].parent.location.file_name);
239 EXPECT_EQ(kFunction,
240 process_data.descendants[0].parent.location.function_name);
241 EXPECT_EQ(kLineNumber,
242 process_data.descendants[0].parent.location.line_number);
243 EXPECT_EQ(kWorkerThreadName,
244 process_data.descendants[0].parent.thread_name);
245 EXPECT_EQ(kFile, process_data.descendants[0].child.location.file_name);
246 EXPECT_EQ(kFunction,
247 process_data.descendants[0].child.location.function_name);
248 EXPECT_EQ(kLineNumber,
249 process_data.descendants[0].child.location.line_number);
250 EXPECT_EQ(kWorkerThreadName, process_data.descendants[0].child.thread_name);
251 } else {
252 EXPECT_EQ(0u, process_data.descendants.size());
253 }
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900254}
255
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900256TEST_F(TrackedObjectsTest, DeathDataTest) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900257 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900258 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900259 return;
vadimt29787df2014-09-16 04:19:38 +0900260 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900261
262 scoped_ptr<DeathData> data(new DeathData());
263 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL));
jar@chromium.orga0260412011-12-04 16:19:10 +0900264 EXPECT_EQ(data->run_duration_sum(), 0);
265 EXPECT_EQ(data->run_duration_sample(), 0);
266 EXPECT_EQ(data->queue_duration_sum(), 0);
267 EXPECT_EQ(data->queue_duration_sample(), 0);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900268 EXPECT_EQ(data->count(), 0);
269
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900270 int32 run_ms = 42;
271 int32 queue_ms = 8;
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900272
jar@chromium.orga0260412011-12-04 16:19:10 +0900273 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_sample(), run_ms);
277 EXPECT_EQ(data->queue_duration_sum(), queue_ms);
278 EXPECT_EQ(data->queue_duration_sample(), queue_ms);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900279 EXPECT_EQ(data->count(), 1);
280
jar@chromium.orga0260412011-12-04 16:19:10 +0900281 data->RecordDeath(queue_ms, run_ms, kUnrandomInt);
282 EXPECT_EQ(data->run_duration_sum(), run_ms + run_ms);
283 EXPECT_EQ(data->run_duration_sample(), run_ms);
284 EXPECT_EQ(data->queue_duration_sum(), queue_ms + queue_ms);
285 EXPECT_EQ(data->queue_duration_sample(), queue_ms);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900286 EXPECT_EQ(data->count(), 2);
287
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900288 DeathDataSnapshot snapshot(*data);
289 EXPECT_EQ(2, snapshot.count);
290 EXPECT_EQ(2 * run_ms, snapshot.run_duration_sum);
291 EXPECT_EQ(run_ms, snapshot.run_duration_max);
292 EXPECT_EQ(run_ms, snapshot.run_duration_sample);
293 EXPECT_EQ(2 * queue_ms, snapshot.queue_duration_sum);
294 EXPECT_EQ(queue_ms, snapshot.queue_duration_max);
295 EXPECT_EQ(queue_ms, snapshot.queue_duration_sample);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900296}
297
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900298TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotWorkerThread) {
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900299 // Start in the deactivated state.
vadimt29787df2014-09-16 04:19:38 +0900300 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) {
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900301 return;
vadimt29787df2014-09-16 04:19:38 +0900302 }
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900303
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900304 const char kFunction[] = "DeactivatedBirthOnlyToSnapshotWorkerThread";
305 Location location(kFunction, kFile, kLineNumber, NULL);
306 TallyABirth(location, std::string());
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900307
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900308 ProcessDataSnapshot process_data;
309 ThreadData::Snapshot(false, &process_data);
310 EXPECT_EQ(0u, process_data.tasks.size());
311 EXPECT_EQ(0u, process_data.descendants.size());
312 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900313}
314
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900315TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotMainThread) {
316 // Start in the deactivated state.
vadimt29787df2014-09-16 04:19:38 +0900317 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) {
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900318 return;
vadimt29787df2014-09-16 04:19:38 +0900319 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900320
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900321 const char kFunction[] = "DeactivatedBirthOnlyToSnapshotMainThread";
322 Location location(kFunction, kFile, kLineNumber, NULL);
323 TallyABirth(location, kMainThreadName);
324
325 ProcessDataSnapshot process_data;
326 ThreadData::Snapshot(false, &process_data);
327 EXPECT_EQ(0u, process_data.tasks.size());
328 EXPECT_EQ(0u, process_data.descendants.size());
329 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900330}
331
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900332TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotWorkerThread) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900333 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900334 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900335 return;
vadimt29787df2014-09-16 04:19:38 +0900336 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900337
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900338 const char kFunction[] = "BirthOnlyToSnapshotWorkerThread";
339 Location location(kFunction, kFile, kLineNumber, NULL);
340 TallyABirth(location, std::string());
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900341
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900342 ProcessDataSnapshot process_data;
343 ThreadData::Snapshot(false, &process_data);
344 ExpectSimpleProcessData(process_data, kFunction, kWorkerThreadName,
345 kStillAlive, 1, 0, 0);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900346}
347
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900348TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotMainThread) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900349 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900350 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900351 return;
vadimt29787df2014-09-16 04:19:38 +0900352 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900353
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900354 const char kFunction[] = "BirthOnlyToSnapshotMainThread";
355 Location location(kFunction, kFile, kLineNumber, NULL);
356 TallyABirth(location, kMainThreadName);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900357
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900358 ProcessDataSnapshot process_data;
359 ThreadData::Snapshot(false, &process_data);
360 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, kStillAlive,
361 1, 0, 0);
362}
363
364TEST_F(TrackedObjectsTest, LifeCycleToSnapshotMainThread) {
365 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900366 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900367 return;
vadimt29787df2014-09-16 04:19:38 +0900368 }
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900369
370 const char kFunction[] = "LifeCycleToSnapshotMainThread";
371 Location location(kFunction, kFile, kLineNumber, NULL);
372 TallyABirth(location, kMainThreadName);
373
374 const base::TimeTicks kTimePosted = base::TimeTicks() +
375 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900376 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
377 // TrackingInfo will call TallyABirth() during construction.
378 base::TrackingInfo pending_task(location, kDelayedStartTime);
379 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900380
vadimt29787df2014-09-16 04:19:38 +0900381 const unsigned int kStartOfRun = 5;
382 const unsigned int kEndOfRun = 7;
383 SetTestTime(kStartOfRun);
384 TaskStopwatch stopwatch;
385 SetTestTime(kEndOfRun);
386 stopwatch.Stop();
387
388 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900389
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900390 ProcessDataSnapshot process_data;
391 ThreadData::Snapshot(false, &process_data);
392 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
393 kMainThreadName, 1, 2, 4);
jar@chromium.org173e3862011-10-30 12:44:25 +0900394}
395
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900396// We will deactivate tracking after the birth, and before the death, and
397// demonstrate that the lifecycle is completely tallied. This ensures that
398// our tallied births are matched by tallied deaths (except for when the
399// task is still running, or is queued).
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900400TEST_F(TrackedObjectsTest, LifeCycleMidDeactivatedToSnapshotMainThread) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900401 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900402 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
jar@chromium.org173e3862011-10-30 12:44:25 +0900403 return;
vadimt29787df2014-09-16 04:19:38 +0900404 }
jar@chromium.org173e3862011-10-30 12:44:25 +0900405
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900406 const char kFunction[] = "LifeCycleMidDeactivatedToSnapshotMainThread";
407 Location location(kFunction, kFile, kLineNumber, NULL);
408 TallyABirth(location, kMainThreadName);
jar@chromium.org173e3862011-10-30 12:44:25 +0900409
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900410 const base::TimeTicks kTimePosted = base::TimeTicks() +
411 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900412 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
413 // TrackingInfo will call TallyABirth() during construction.
414 base::TrackingInfo pending_task(location, kDelayedStartTime);
415 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
jar@chromium.org173e3862011-10-30 12:44:25 +0900416
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900417 // Turn off tracking now that we have births.
jar@chromium.org23b00722012-02-11 04:43:42 +0900418 EXPECT_TRUE(ThreadData::InitializeAndSetTrackingStatus(
419 ThreadData::DEACTIVATED));
jar@chromium.org173e3862011-10-30 12:44:25 +0900420
vadimt29787df2014-09-16 04:19:38 +0900421 const unsigned int kStartOfRun = 5;
422 const unsigned int kEndOfRun = 7;
423 SetTestTime(kStartOfRun);
424 TaskStopwatch stopwatch;
425 SetTestTime(kEndOfRun);
426 stopwatch.Stop();
427
428 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900429
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900430 ProcessDataSnapshot process_data;
431 ThreadData::Snapshot(false, &process_data);
432 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
433 kMainThreadName, 1, 2, 4);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900434}
435
436// We will deactivate tracking before starting a life cycle, and neither
437// the birth nor the death will be recorded.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900438TEST_F(TrackedObjectsTest, LifeCyclePreDeactivatedToSnapshotMainThread) {
439 // Start in the deactivated state.
vadimt29787df2014-09-16 04:19:38 +0900440 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) {
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900441 return;
vadimt29787df2014-09-16 04:19:38 +0900442 }
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900443
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900444 const char kFunction[] = "LifeCyclePreDeactivatedToSnapshotMainThread";
445 Location location(kFunction, kFile, kLineNumber, NULL);
446 TallyABirth(location, kMainThreadName);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900447
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900448 const base::TimeTicks kTimePosted = base::TimeTicks() +
449 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900450 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
451 // TrackingInfo will call TallyABirth() during construction.
452 base::TrackingInfo pending_task(location, kDelayedStartTime);
453 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
454
vadimt29787df2014-09-16 04:19:38 +0900455 const unsigned int kStartOfRun = 5;
456 const unsigned int kEndOfRun = 7;
457 SetTestTime(kStartOfRun);
458 TaskStopwatch stopwatch;
459 SetTestTime(kEndOfRun);
460 stopwatch.Stop();
461
462 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900463
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900464 ProcessDataSnapshot process_data;
465 ThreadData::Snapshot(false, &process_data);
466 EXPECT_EQ(0u, process_data.tasks.size());
467 EXPECT_EQ(0u, process_data.descendants.size());
468 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900469}
470
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900471TEST_F(TrackedObjectsTest, LifeCycleToSnapshotWorkerThread) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900472 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900473 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900474 return;
vadimt29787df2014-09-16 04:19:38 +0900475 }
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900476
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900477 const char kFunction[] = "LifeCycleToSnapshotWorkerThread";
478 Location location(kFunction, kFile, kLineNumber, NULL);
479 // Do not delete |birth|. We don't own it.
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900480 Births* birth = ThreadData::TallyABirthIfActive(location);
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900481 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900482
vadimt29787df2014-09-16 04:19:38 +0900483 const unsigned int kTimePosted = 1;
484 const unsigned int kStartOfRun = 5;
485 const unsigned int kEndOfRun = 7;
486 SetTestTime(kStartOfRun);
487 TaskStopwatch stopwatch;
488 SetTestTime(kEndOfRun);
489 stopwatch.Stop();
490
491 ThreadData::TallyRunOnWorkerThreadIfTracking(
492 birth, TrackedTime() + Duration::FromMilliseconds(kTimePosted), stopwatch);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900493
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900494 // Call for the ToSnapshot, but tell it to not reset the maxes after scanning.
495 ProcessDataSnapshot process_data;
496 ThreadData::Snapshot(false, &process_data);
497 ExpectSimpleProcessData(process_data, kFunction, kWorkerThreadName,
498 kWorkerThreadName, 1, 2, 4);
jar@chromium.orga0260412011-12-04 16:19:10 +0900499
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900500 // Call for the ToSnapshot, but tell it to reset the maxes after scanning.
jar@chromium.orga0260412011-12-04 16:19:10 +0900501 // We'll still get the same values, but the data will be reset (which we'll
502 // see in a moment).
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900503 ProcessDataSnapshot process_data_pre_reset;
504 ThreadData::Snapshot(true, &process_data_pre_reset);
505 ExpectSimpleProcessData(process_data, kFunction, kWorkerThreadName,
506 kWorkerThreadName, 1, 2, 4);
jar@chromium.orga0260412011-12-04 16:19:10 +0900507
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900508 // Call for the ToSnapshot, and now we'll see the result of the last
509 // translation, as the max will have been pushed back to zero.
510 ProcessDataSnapshot process_data_post_reset;
511 ThreadData::Snapshot(true, &process_data_post_reset);
512 ASSERT_EQ(1u, process_data_post_reset.tasks.size());
513 EXPECT_EQ(kFile, process_data_post_reset.tasks[0].birth.location.file_name);
514 EXPECT_EQ(kFunction,
515 process_data_post_reset.tasks[0].birth.location.function_name);
516 EXPECT_EQ(kLineNumber,
517 process_data_post_reset.tasks[0].birth.location.line_number);
518 EXPECT_EQ(kWorkerThreadName,
519 process_data_post_reset.tasks[0].birth.thread_name);
520 EXPECT_EQ(1, process_data_post_reset.tasks[0].death_data.count);
521 EXPECT_EQ(2, process_data_post_reset.tasks[0].death_data.run_duration_sum);
522 EXPECT_EQ(0, process_data_post_reset.tasks[0].death_data.run_duration_max);
523 EXPECT_EQ(2, process_data_post_reset.tasks[0].death_data.run_duration_sample);
524 EXPECT_EQ(4, process_data_post_reset.tasks[0].death_data.queue_duration_sum);
525 EXPECT_EQ(0, process_data_post_reset.tasks[0].death_data.queue_duration_max);
526 EXPECT_EQ(4,
527 process_data_post_reset.tasks[0].death_data.queue_duration_sample);
528 EXPECT_EQ(kWorkerThreadName,
529 process_data_post_reset.tasks[0].death_thread_name);
530 EXPECT_EQ(0u, process_data_post_reset.descendants.size());
531 EXPECT_EQ(base::GetCurrentProcId(), process_data_post_reset.process_id);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900532}
533
534TEST_F(TrackedObjectsTest, TwoLives) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900535 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900536 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900537 return;
vadimt29787df2014-09-16 04:19:38 +0900538 }
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900539
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900540 const char kFunction[] = "TwoLives";
541 Location location(kFunction, kFile, kLineNumber, NULL);
542 TallyABirth(location, kMainThreadName);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900543
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900544 const base::TimeTicks kTimePosted = base::TimeTicks() +
545 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900546 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
547 // TrackingInfo will call TallyABirth() during construction.
548 base::TrackingInfo pending_task(location, kDelayedStartTime);
549 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
550
vadimt29787df2014-09-16 04:19:38 +0900551 const unsigned int kStartOfRun = 5;
552 const unsigned int kEndOfRun = 7;
553 SetTestTime(kStartOfRun);
554 TaskStopwatch stopwatch;
555 SetTestTime(kEndOfRun);
556 stopwatch.Stop();
557
558 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900559
560 // TrackingInfo will call TallyABirth() during construction.
561 base::TrackingInfo pending_task2(location, kDelayedStartTime);
562 pending_task2.time_posted = kTimePosted; // Overwrite implied Now().
vadimt29787df2014-09-16 04:19:38 +0900563 SetTestTime(kStartOfRun);
564 TaskStopwatch stopwatch2;
565 SetTestTime(kEndOfRun);
566 stopwatch2.Stop();
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900567
vadimt29787df2014-09-16 04:19:38 +0900568 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task2, stopwatch2);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900569
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900570 ProcessDataSnapshot process_data;
571 ThreadData::Snapshot(false, &process_data);
572 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
573 kMainThreadName, 2, 2, 4);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900574}
575
576TEST_F(TrackedObjectsTest, DifferentLives) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900577 if (!ThreadData::InitializeAndSetTrackingStatus(
vadimt29787df2014-09-16 04:19:38 +0900578 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900579 return;
vadimt29787df2014-09-16 04:19:38 +0900580 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900581
582 // Use a well named thread.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900583 ThreadData::InitializeThreadContext(kMainThreadName);
584 const char kFunction[] = "DifferentLives";
585 Location location(kFunction, kFile, kLineNumber, NULL);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900586
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900587 const base::TimeTicks kTimePosted = base::TimeTicks() +
588 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900589 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
590 // TrackingInfo will call TallyABirth() during construction.
591 base::TrackingInfo pending_task(location, kDelayedStartTime);
592 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900593
vadimt29787df2014-09-16 04:19:38 +0900594 const unsigned int kStartOfRun = 5;
595 const unsigned int kEndOfRun = 7;
596 SetTestTime(kStartOfRun);
597 TaskStopwatch stopwatch;
598 SetTestTime(kEndOfRun);
599 stopwatch.Stop();
600
601 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900602
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900603 const int kSecondFakeLineNumber = 999;
604 Location second_location(kFunction, kFile, kSecondFakeLineNumber, NULL);
605
606 // TrackingInfo will call TallyABirth() during construction.
607 base::TrackingInfo pending_task2(second_location, kDelayedStartTime);
608 pending_task2.time_posted = kTimePosted; // Overwrite implied Now().
609
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900610 ProcessDataSnapshot process_data;
611 ThreadData::Snapshot(false, &process_data);
612 ASSERT_EQ(2u, process_data.tasks.size());
vadimt29787df2014-09-16 04:19:38 +0900613
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900614 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name);
615 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name);
616 EXPECT_EQ(kLineNumber, process_data.tasks[0].birth.location.line_number);
617 EXPECT_EQ(kMainThreadName, process_data.tasks[0].birth.thread_name);
618 EXPECT_EQ(1, process_data.tasks[0].death_data.count);
619 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sum);
620 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_max);
621 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sample);
622 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sum);
623 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_max);
624 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sample);
625 EXPECT_EQ(kMainThreadName, process_data.tasks[0].death_thread_name);
626 EXPECT_EQ(kFile, process_data.tasks[1].birth.location.file_name);
627 EXPECT_EQ(kFunction, process_data.tasks[1].birth.location.function_name);
628 EXPECT_EQ(kSecondFakeLineNumber,
629 process_data.tasks[1].birth.location.line_number);
630 EXPECT_EQ(kMainThreadName, process_data.tasks[1].birth.thread_name);
631 EXPECT_EQ(1, process_data.tasks[1].death_data.count);
632 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_sum);
633 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_max);
634 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_sample);
635 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_sum);
636 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_max);
637 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_sample);
638 EXPECT_EQ(kStillAlive, process_data.tasks[1].death_thread_name);
639 EXPECT_EQ(0u, process_data.descendants.size());
640 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
initial.commit3f4a7322008-07-27 06:49:38 +0900641}
642
vadimt29787df2014-09-16 04:19:38 +0900643TEST_F(TrackedObjectsTest, TaskWithNestedExclusion) {
644 if (!ThreadData::InitializeAndSetTrackingStatus(
645 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
646 return;
647 }
648
649 const char kFunction[] = "TaskWithNestedExclusion";
650 Location location(kFunction, kFile, kLineNumber, NULL);
651 TallyABirth(location, kMainThreadName);
652
653 const base::TimeTicks kTimePosted = base::TimeTicks() +
654 base::TimeDelta::FromMilliseconds(1);
655 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
656 // TrackingInfo will call TallyABirth() during construction.
657 base::TrackingInfo pending_task(location, kDelayedStartTime);
658 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
659
660 SetTestTime(5);
661 TaskStopwatch task_stopwatch;
662 {
663 SetTestTime(8);
664 TaskStopwatch exclusion_stopwatch;
665 SetTestTime(12);
666 exclusion_stopwatch.Stop();
667 }
668 SetTestTime(15);
669 task_stopwatch.Stop();
670
671 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch);
672
673 ProcessDataSnapshot process_data;
674 ThreadData::Snapshot(false, &process_data);
675 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
676 kMainThreadName, 1, 6, 4);
677}
678
679TEST_F(TrackedObjectsTest, TaskWith2NestedExclusions) {
680 if (!ThreadData::InitializeAndSetTrackingStatus(
681 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
682 return;
683 }
684
685 const char kFunction[] = "TaskWith2NestedExclusions";
686 Location location(kFunction, kFile, kLineNumber, NULL);
687 TallyABirth(location, kMainThreadName);
688
689 const base::TimeTicks kTimePosted = base::TimeTicks() +
690 base::TimeDelta::FromMilliseconds(1);
691 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
692 // TrackingInfo will call TallyABirth() during construction.
693 base::TrackingInfo pending_task(location, kDelayedStartTime);
694 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
695
696 SetTestTime(5);
697 TaskStopwatch task_stopwatch;
698 {
699 SetTestTime(8);
700 TaskStopwatch exclusion_stopwatch;
701 SetTestTime(12);
702 exclusion_stopwatch.Stop();
703
704 SetTestTime(15);
705 TaskStopwatch exclusion_stopwatch2;
706 SetTestTime(18);
707 exclusion_stopwatch2.Stop();
708 }
709 SetTestTime(25);
710 task_stopwatch.Stop();
711
712 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch);
713
714 ProcessDataSnapshot process_data;
715 ThreadData::Snapshot(false, &process_data);
716 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
717 kMainThreadName, 1, 13, 4);
718}
719
720TEST_F(TrackedObjectsTest, TaskWithNestedExclusionWithNestedTask) {
721 if (!ThreadData::InitializeAndSetTrackingStatus(
722 ThreadData::PROFILING_CHILDREN_ACTIVE)) {
723 return;
724 }
725
726 const char kFunction[] = "TaskWithNestedExclusionWithNestedTask";
727 Location location(kFunction, kFile, kLineNumber, NULL);
728
729 const int kSecondFakeLineNumber = 999;
730
731 TallyABirth(location, kMainThreadName);
732
733 const base::TimeTicks kTimePosted = base::TimeTicks() +
734 base::TimeDelta::FromMilliseconds(1);
735 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
736 // TrackingInfo will call TallyABirth() during construction.
737 base::TrackingInfo pending_task(location, kDelayedStartTime);
738 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
739
740 SetTestTime(5);
741 TaskStopwatch task_stopwatch;
742 {
743 SetTestTime(8);
744 TaskStopwatch exclusion_stopwatch;
745 {
746 Location second_location(kFunction, kFile, kSecondFakeLineNumber, NULL);
747 base::TrackingInfo nested_task(second_location, kDelayedStartTime);
748 // Overwrite implied Now().
749 nested_task.time_posted =
750 base::TimeTicks() + base::TimeDelta::FromMilliseconds(8);
751 SetTestTime(9);
752 TaskStopwatch nested_task_stopwatch;
753 SetTestTime(11);
754 nested_task_stopwatch.Stop();
755 ThreadData::TallyRunOnNamedThreadIfTracking(
756 nested_task, nested_task_stopwatch);
757 }
758 SetTestTime(12);
759 exclusion_stopwatch.Stop();
760 }
761 SetTestTime(15);
762 task_stopwatch.Stop();
763
764 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch);
765
766 ProcessDataSnapshot process_data;
767 ThreadData::Snapshot(false, &process_data);
768
769 // The order in which the two task follow is platform-dependent.
770 int t0 = (process_data.tasks[0].birth.location.line_number == kLineNumber) ?
771 0 : 1;
772 int t1 = 1 - t0;
773
774 ASSERT_EQ(2u, process_data.tasks.size());
775 EXPECT_EQ(kFile, process_data.tasks[t0].birth.location.file_name);
776 EXPECT_EQ(kFunction, process_data.tasks[t0].birth.location.function_name);
777 EXPECT_EQ(kLineNumber, process_data.tasks[t0].birth.location.line_number);
778 EXPECT_EQ(kMainThreadName, process_data.tasks[t0].birth.thread_name);
779 EXPECT_EQ(1, process_data.tasks[t0].death_data.count);
780 EXPECT_EQ(6, process_data.tasks[t0].death_data.run_duration_sum);
781 EXPECT_EQ(6, process_data.tasks[t0].death_data.run_duration_max);
782 EXPECT_EQ(6, process_data.tasks[t0].death_data.run_duration_sample);
783 EXPECT_EQ(4, process_data.tasks[t0].death_data.queue_duration_sum);
784 EXPECT_EQ(4, process_data.tasks[t0].death_data.queue_duration_max);
785 EXPECT_EQ(4, process_data.tasks[t0].death_data.queue_duration_sample);
786 EXPECT_EQ(kMainThreadName, process_data.tasks[t0].death_thread_name);
787 EXPECT_EQ(kFile, process_data.tasks[t1].birth.location.file_name);
788 EXPECT_EQ(kFunction, process_data.tasks[t1].birth.location.function_name);
789 EXPECT_EQ(kSecondFakeLineNumber,
790 process_data.tasks[t1].birth.location.line_number);
791 EXPECT_EQ(kMainThreadName, process_data.tasks[t1].birth.thread_name);
792 EXPECT_EQ(1, process_data.tasks[t1].death_data.count);
793 EXPECT_EQ(2, process_data.tasks[t1].death_data.run_duration_sum);
794 EXPECT_EQ(2, process_data.tasks[t1].death_data.run_duration_max);
795 EXPECT_EQ(2, process_data.tasks[t1].death_data.run_duration_sample);
796 EXPECT_EQ(1, process_data.tasks[t1].death_data.queue_duration_sum);
797 EXPECT_EQ(1, process_data.tasks[t1].death_data.queue_duration_max);
798 EXPECT_EQ(1, process_data.tasks[t1].death_data.queue_duration_sample);
799 EXPECT_EQ(kMainThreadName, process_data.tasks[t1].death_thread_name);
800 EXPECT_EQ(0u, process_data.descendants.size());
801 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
802}
803
deanm@google.com11f192a2008-08-26 20:29:01 +0900804} // namespace tracked_objects