blob: 7cbce908e0aa1afe908a01c129852fdaa8ffd391 [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);
31 }
jar@chromium.org666ef9c2011-10-25 03:55:16 +090032
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +090033 virtual ~TrackedObjectsTest() {
jar@chromium.org4be2cb02011-11-01 07:36:21 +090034 // We should not need to leak any structures we create, since we are
35 // single threaded, and carefully accounting for items.
36 ThreadData::ShutdownSingleThreadedCleanup(false);
37 }
jar@chromium.org7ad4a622011-11-07 04:14:48 +090038
isherman@chromium.org98c10d22012-04-13 09:39:26 +090039 // Reset the profiler state.
40 void Reset() {
41 ThreadData::ShutdownSingleThreadedCleanup(false);
42 }
43
44 // Simulate a birth on the thread named |thread_name|, at the given
45 // |location|.
46 void TallyABirth(const Location& location, const std::string& thread_name) {
47 // If the |thread_name| is empty, we don't initialize system with a thread
48 // name, so we're viewed as a worker thread.
49 if (!thread_name.empty())
50 ThreadData::InitializeThreadContext(kMainThreadName);
51
52 // Do not delete |birth|. We don't own it.
53 Births* birth = ThreadData::TallyABirthIfActive(location);
54
55 if (ThreadData::status() == ThreadData::DEACTIVATED)
56 EXPECT_EQ(reinterpret_cast<Births*>(NULL), birth);
57 else
58 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth);
59 }
60
61 // Helper function to verify the most common test expectations.
62 void ExpectSimpleProcessData(const ProcessDataSnapshot& process_data,
63 const std::string& function_name,
64 const std::string& birth_thread,
65 const std::string& death_thread,
66 int count,
67 int run_ms,
68 int queue_ms) {
69 ASSERT_EQ(1u, process_data.tasks.size());
70
71 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name);
72 EXPECT_EQ(function_name,
73 process_data.tasks[0].birth.location.function_name);
74 EXPECT_EQ(kLineNumber, process_data.tasks[0].birth.location.line_number);
75
76 EXPECT_EQ(birth_thread, process_data.tasks[0].birth.thread_name);
77
78 EXPECT_EQ(count, process_data.tasks[0].death_data.count);
79 EXPECT_EQ(count * run_ms,
80 process_data.tasks[0].death_data.run_duration_sum);
81 EXPECT_EQ(run_ms, process_data.tasks[0].death_data.run_duration_max);
82 EXPECT_EQ(run_ms, process_data.tasks[0].death_data.run_duration_sample);
83 EXPECT_EQ(count * queue_ms,
84 process_data.tasks[0].death_data.queue_duration_sum);
85 EXPECT_EQ(queue_ms, process_data.tasks[0].death_data.queue_duration_max);
86 EXPECT_EQ(queue_ms, process_data.tasks[0].death_data.queue_duration_sample);
87
88 EXPECT_EQ(death_thread, process_data.tasks[0].death_thread_name);
89
90 EXPECT_EQ(0u, process_data.descendants.size());
91
92 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
jar@chromium.org7ad4a622011-11-07 04:14:48 +090093 }
initial.commit3f4a7322008-07-27 06:49:38 +090094};
95
darin@google.comaca48f52008-08-26 15:44:38 +090096TEST_F(TrackedObjectsTest, MinimalStartupShutdown) {
initial.commit3f4a7322008-07-27 06:49:38 +090097 // Minimal test doesn't even create any tasks.
jar@chromium.org23b00722012-02-11 04:43:42 +090098 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +090099 ThreadData::PROFILING_CHILDREN_ACTIVE))
initial.commit3f4a7322008-07-27 06:49:38 +0900100 return;
101
102 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
jar@chromium.org79a58c32011-10-16 08:52:45 +0900103 ThreadData* data = ThreadData::Get();
initial.commit3f4a7322008-07-27 06:49:38 +0900104 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900105 ASSERT_TRUE(data);
106 EXPECT_FALSE(data->next());
jar@chromium.org79a58c32011-10-16 08:52:45 +0900107 EXPECT_EQ(data, ThreadData::Get());
initial.commit3f4a7322008-07-27 06:49:38 +0900108 ThreadData::BirthMap birth_map;
initial.commit3f4a7322008-07-27 06:49:38 +0900109 ThreadData::DeathMap death_map;
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900110 ThreadData::ParentChildSet parent_child_set;
111 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set);
jar@chromium.orga0260412011-12-04 16:19:10 +0900112 EXPECT_EQ(0u, birth_map.size());
initial.commit3f4a7322008-07-27 06:49:38 +0900113 EXPECT_EQ(0u, death_map.size());
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900114 EXPECT_EQ(0u, parent_child_set.size());
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900115
116 // Clean up with no leaking.
117 Reset();
initial.commit3f4a7322008-07-27 06:49:38 +0900118
119 // Do it again, just to be sure we reset state completely.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900120 EXPECT_TRUE(ThreadData::InitializeAndSetTrackingStatus(
121 ThreadData::PROFILING_CHILDREN_ACTIVE));
initial.commit3f4a7322008-07-27 06:49:38 +0900122 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
jar@chromium.org79a58c32011-10-16 08:52:45 +0900123 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 birth_map.clear();
initial.commit3f4a7322008-07-27 06:49:38 +0900129 death_map.clear();
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900130 parent_child_set.clear();
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900131 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());
initial.commit3f4a7322008-07-27 06:49:38 +0900135}
136
darin@google.comaca48f52008-08-26 15:44:38 +0900137TEST_F(TrackedObjectsTest, TinyStartupShutdown) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900138 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900139 ThreadData::PROFILING_CHILDREN_ACTIVE))
initial.commit3f4a7322008-07-27 06:49:38 +0900140 return;
141
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900142 // Instigate tracking on a single tracked object, on our thread.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900143 const char kFunction[] = "TinyStartupShutdown";
144 Location location(kFunction, kFile, kLineNumber, NULL);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900145 Births* first_birth = ThreadData::TallyABirthIfActive(location);
initial.commit3f4a7322008-07-27 06:49:38 +0900146
jar@chromium.orga0260412011-12-04 16:19:10 +0900147 ThreadData* data = ThreadData::first();
ajwong@chromium.org7211f7d2011-06-24 08:20:39 +0900148 ASSERT_TRUE(data);
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900149 EXPECT_FALSE(data->next());
jar@chromium.org79a58c32011-10-16 08:52:45 +0900150 EXPECT_EQ(data, ThreadData::Get());
initial.commit3f4a7322008-07-27 06:49:38 +0900151 ThreadData::BirthMap birth_map;
jar@chromium.orga0260412011-12-04 16:19:10 +0900152 ThreadData::DeathMap death_map;
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900153 ThreadData::ParentChildSet parent_child_set;
154 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set);
initial.commit3f4a7322008-07-27 06:49:38 +0900155 EXPECT_EQ(1u, birth_map.size()); // 1 birth location.
156 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth.
initial.commit3f4a7322008-07-27 06:49:38 +0900157 EXPECT_EQ(0u, death_map.size()); // No deaths.
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900158 EXPECT_EQ(0u, parent_child_set.size()); // No children.
initial.commit3f4a7322008-07-27 06:49:38 +0900159
160
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900161 // Now instigate another birth, while we are timing the run of the first
162 // execution.
isherman@chromium.org514b6342012-04-13 10:48:35 +0900163 ThreadData::NowForStartOfRun(first_birth);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900164 // Create a child (using the same birth location).
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900165 // TrackingInfo will call TallyABirth() during construction.
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900166 base::TimeTicks kBogusBirthTime;
167 base::TrackingInfo pending_task(location, kBogusBirthTime);
isherman@chromium.org514b6342012-04-13 10:48:35 +0900168 TrackedTime start_time(pending_task.time_posted);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900169 // Finally conclude the outer run.
170 TrackedTime end_time = ThreadData::NowForEndOfRun();
171 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, start_time,
172 end_time);
initial.commit3f4a7322008-07-27 06:49:38 +0900173
174 birth_map.clear();
jar@chromium.orga0260412011-12-04 16:19:10 +0900175 death_map.clear();
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900176 parent_child_set.clear();
177 data->SnapshotMaps(false, &birth_map, &death_map, &parent_child_set);
initial.commit3f4a7322008-07-27 06:49:38 +0900178 EXPECT_EQ(1u, birth_map.size()); // 1 birth location.
179 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births.
initial.commit3f4a7322008-07-27 06:49:38 +0900180 EXPECT_EQ(1u, death_map.size()); // 1 location.
181 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death.
jar@chromium.org23b00722012-02-11 04:43:42 +0900182 if (ThreadData::TrackingParentChildStatus()) {
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900183 EXPECT_EQ(1u, parent_child_set.size()); // 1 child.
184 EXPECT_EQ(parent_child_set.begin()->first,
185 parent_child_set.begin()->second);
186 } else {
187 EXPECT_EQ(0u, parent_child_set.size()); // no stats.
188 }
initial.commit3f4a7322008-07-27 06:49:38 +0900189
190 // The births were at the same location as the one known death.
191 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first);
192
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900193 ProcessDataSnapshot process_data;
194 ThreadData::Snapshot(false, &process_data);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900195
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900196 const int32 time_elapsed = (end_time - start_time).InMilliseconds();
197 ASSERT_EQ(1u, process_data.tasks.size());
198 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name);
199 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name);
200 EXPECT_EQ(kLineNumber, process_data.tasks[0].birth.location.line_number);
201 EXPECT_EQ(kWorkerThreadName, process_data.tasks[0].birth.thread_name);
202 EXPECT_EQ(1, process_data.tasks[0].death_data.count);
203 EXPECT_EQ(time_elapsed, process_data.tasks[0].death_data.run_duration_sum);
204 EXPECT_EQ(time_elapsed, process_data.tasks[0].death_data.run_duration_max);
205 EXPECT_EQ(time_elapsed, process_data.tasks[0].death_data.run_duration_sample);
206 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sum);
207 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_max);
208 EXPECT_EQ(0, process_data.tasks[0].death_data.queue_duration_sample);
209 EXPECT_EQ(kWorkerThreadName, process_data.tasks[0].death_thread_name);
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900210
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900211 if (ThreadData::TrackingParentChildStatus()) {
212 ASSERT_EQ(1u, process_data.descendants.size());
213 EXPECT_EQ(kFile, process_data.descendants[0].parent.location.file_name);
214 EXPECT_EQ(kFunction,
215 process_data.descendants[0].parent.location.function_name);
216 EXPECT_EQ(kLineNumber,
217 process_data.descendants[0].parent.location.line_number);
218 EXPECT_EQ(kWorkerThreadName,
219 process_data.descendants[0].parent.thread_name);
220 EXPECT_EQ(kFile, process_data.descendants[0].child.location.file_name);
221 EXPECT_EQ(kFunction,
222 process_data.descendants[0].child.location.function_name);
223 EXPECT_EQ(kLineNumber,
224 process_data.descendants[0].child.location.line_number);
225 EXPECT_EQ(kWorkerThreadName, process_data.descendants[0].child.thread_name);
226 } else {
227 EXPECT_EQ(0u, process_data.descendants.size());
228 }
jar@chromium.orgb5c974b2011-12-14 10:36:48 +0900229}
230
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900231TEST_F(TrackedObjectsTest, DeathDataTest) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900232 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900233 ThreadData::PROFILING_CHILDREN_ACTIVE))
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900234 return;
235
236 scoped_ptr<DeathData> data(new DeathData());
237 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL));
jar@chromium.orga0260412011-12-04 16:19:10 +0900238 EXPECT_EQ(data->run_duration_sum(), 0);
239 EXPECT_EQ(data->run_duration_sample(), 0);
240 EXPECT_EQ(data->queue_duration_sum(), 0);
241 EXPECT_EQ(data->queue_duration_sample(), 0);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900242 EXPECT_EQ(data->count(), 0);
243
isherman@chromium.org3306fc02012-03-25 07:17:18 +0900244 int32 run_ms = 42;
245 int32 queue_ms = 8;
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900246
jar@chromium.orga0260412011-12-04 16:19:10 +0900247 const int kUnrandomInt = 0; // Fake random int that ensure we sample data.
248 data->RecordDeath(queue_ms, run_ms, kUnrandomInt);
249 EXPECT_EQ(data->run_duration_sum(), run_ms);
250 EXPECT_EQ(data->run_duration_sample(), run_ms);
251 EXPECT_EQ(data->queue_duration_sum(), queue_ms);
252 EXPECT_EQ(data->queue_duration_sample(), queue_ms);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900253 EXPECT_EQ(data->count(), 1);
254
jar@chromium.orga0260412011-12-04 16:19:10 +0900255 data->RecordDeath(queue_ms, run_ms, kUnrandomInt);
256 EXPECT_EQ(data->run_duration_sum(), run_ms + run_ms);
257 EXPECT_EQ(data->run_duration_sample(), run_ms);
258 EXPECT_EQ(data->queue_duration_sum(), queue_ms + queue_ms);
259 EXPECT_EQ(data->queue_duration_sample(), queue_ms);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900260 EXPECT_EQ(data->count(), 2);
261
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900262 DeathDataSnapshot snapshot(*data);
263 EXPECT_EQ(2, snapshot.count);
264 EXPECT_EQ(2 * run_ms, snapshot.run_duration_sum);
265 EXPECT_EQ(run_ms, snapshot.run_duration_max);
266 EXPECT_EQ(run_ms, snapshot.run_duration_sample);
267 EXPECT_EQ(2 * queue_ms, snapshot.queue_duration_sum);
268 EXPECT_EQ(queue_ms, snapshot.queue_duration_max);
269 EXPECT_EQ(queue_ms, snapshot.queue_duration_sample);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900270}
271
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900272TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotWorkerThread) {
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900273 // Start in the deactivated state.
jar@chromium.org23b00722012-02-11 04:43:42 +0900274 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED))
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900275 return;
276
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900277 const char kFunction[] = "DeactivatedBirthOnlyToSnapshotWorkerThread";
278 Location location(kFunction, kFile, kLineNumber, NULL);
279 TallyABirth(location, std::string());
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900280
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900281 ProcessDataSnapshot process_data;
282 ThreadData::Snapshot(false, &process_data);
283 EXPECT_EQ(0u, process_data.tasks.size());
284 EXPECT_EQ(0u, process_data.descendants.size());
285 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900286}
287
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900288TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotMainThread) {
289 // Start in the deactivated state.
290 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED))
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900291 return;
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900292
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900293 const char kFunction[] = "DeactivatedBirthOnlyToSnapshotMainThread";
294 Location location(kFunction, kFile, kLineNumber, NULL);
295 TallyABirth(location, kMainThreadName);
296
297 ProcessDataSnapshot process_data;
298 ThreadData::Snapshot(false, &process_data);
299 EXPECT_EQ(0u, process_data.tasks.size());
300 EXPECT_EQ(0u, process_data.descendants.size());
301 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900302}
303
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900304TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotWorkerThread) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900305 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900306 ThreadData::PROFILING_CHILDREN_ACTIVE))
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900307 return;
308
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900309 const char kFunction[] = "BirthOnlyToSnapshotWorkerThread";
310 Location location(kFunction, kFile, kLineNumber, NULL);
311 TallyABirth(location, std::string());
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900312
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900313 ProcessDataSnapshot process_data;
314 ThreadData::Snapshot(false, &process_data);
315 ExpectSimpleProcessData(process_data, kFunction, kWorkerThreadName,
316 kStillAlive, 1, 0, 0);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900317}
318
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900319TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotMainThread) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900320 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900321 ThreadData::PROFILING_CHILDREN_ACTIVE))
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900322 return;
323
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900324 const char kFunction[] = "BirthOnlyToSnapshotMainThread";
325 Location location(kFunction, kFile, kLineNumber, NULL);
326 TallyABirth(location, kMainThreadName);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900327
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900328 ProcessDataSnapshot process_data;
329 ThreadData::Snapshot(false, &process_data);
330 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, kStillAlive,
331 1, 0, 0);
332}
333
334TEST_F(TrackedObjectsTest, LifeCycleToSnapshotMainThread) {
335 if (!ThreadData::InitializeAndSetTrackingStatus(
336 ThreadData::PROFILING_CHILDREN_ACTIVE))
337 return;
338
339 const char kFunction[] = "LifeCycleToSnapshotMainThread";
340 Location location(kFunction, kFile, kLineNumber, NULL);
341 TallyABirth(location, kMainThreadName);
342
343 const base::TimeTicks kTimePosted = base::TimeTicks() +
344 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900345 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
346 // TrackingInfo will call TallyABirth() during construction.
347 base::TrackingInfo pending_task(location, kDelayedStartTime);
348 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900349
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900350 const TrackedTime kStartOfRun = TrackedTime() +
351 Duration::FromMilliseconds(5);
352 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7);
353 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task,
354 kStartOfRun, kEndOfRun);
355
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900356 ProcessDataSnapshot process_data;
357 ThreadData::Snapshot(false, &process_data);
358 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
359 kMainThreadName, 1, 2, 4);
jar@chromium.org173e3862011-10-30 12:44:25 +0900360}
361
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900362// We will deactivate tracking after the birth, and before the death, and
363// demonstrate that the lifecycle is completely tallied. This ensures that
364// our tallied births are matched by tallied deaths (except for when the
365// task is still running, or is queued).
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900366TEST_F(TrackedObjectsTest, LifeCycleMidDeactivatedToSnapshotMainThread) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900367 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900368 ThreadData::PROFILING_CHILDREN_ACTIVE))
jar@chromium.org173e3862011-10-30 12:44:25 +0900369 return;
370
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900371 const char kFunction[] = "LifeCycleMidDeactivatedToSnapshotMainThread";
372 Location location(kFunction, kFile, kLineNumber, NULL);
373 TallyABirth(location, kMainThreadName);
jar@chromium.org173e3862011-10-30 12:44:25 +0900374
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900375 const base::TimeTicks kTimePosted = base::TimeTicks() +
376 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900377 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
378 // TrackingInfo will call TallyABirth() during construction.
379 base::TrackingInfo pending_task(location, kDelayedStartTime);
380 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
jar@chromium.org173e3862011-10-30 12:44:25 +0900381
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900382 // Turn off tracking now that we have births.
jar@chromium.org23b00722012-02-11 04:43:42 +0900383 EXPECT_TRUE(ThreadData::InitializeAndSetTrackingStatus(
384 ThreadData::DEACTIVATED));
jar@chromium.org173e3862011-10-30 12:44:25 +0900385
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900386 const TrackedTime kStartOfRun = TrackedTime() +
387 Duration::FromMilliseconds(5);
388 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7);
389 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task,
390 kStartOfRun, kEndOfRun);
391
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900392 ProcessDataSnapshot process_data;
393 ThreadData::Snapshot(false, &process_data);
394 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
395 kMainThreadName, 1, 2, 4);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900396}
397
398// We will deactivate tracking before starting a life cycle, and neither
399// the birth nor the death will be recorded.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900400TEST_F(TrackedObjectsTest, LifeCyclePreDeactivatedToSnapshotMainThread) {
401 // Start in the deactivated state.
jar@chromium.org23b00722012-02-11 04:43:42 +0900402 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED))
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900403 return;
404
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900405 const char kFunction[] = "LifeCyclePreDeactivatedToSnapshotMainThread";
406 Location location(kFunction, kFile, kLineNumber, NULL);
407 TallyABirth(location, kMainThreadName);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900408
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900409 const base::TimeTicks kTimePosted = base::TimeTicks() +
410 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900411 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
412 // TrackingInfo will call TallyABirth() during construction.
413 base::TrackingInfo pending_task(location, kDelayedStartTime);
414 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
415
416 const TrackedTime kStartOfRun = TrackedTime() +
417 Duration::FromMilliseconds(5);
418 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7);
419 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task,
420 kStartOfRun, kEndOfRun);
421
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900422 ProcessDataSnapshot process_data;
423 ThreadData::Snapshot(false, &process_data);
424 EXPECT_EQ(0u, process_data.tasks.size());
425 EXPECT_EQ(0u, process_data.descendants.size());
426 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900427}
428
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900429TEST_F(TrackedObjectsTest, LifeCycleToSnapshotWorkerThread) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900430 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900431 ThreadData::PROFILING_CHILDREN_ACTIVE))
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900432 return;
433
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900434 const char kFunction[] = "LifeCycleToSnapshotWorkerThread";
435 Location location(kFunction, kFile, kLineNumber, NULL);
436 // Do not delete |birth|. We don't own it.
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900437 Births* birth = ThreadData::TallyABirthIfActive(location);
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900438 EXPECT_NE(reinterpret_cast<Births*>(NULL), birth);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900439
440 const TrackedTime kTimePosted = TrackedTime() + Duration::FromMilliseconds(1);
441 const TrackedTime kStartOfRun = TrackedTime() +
442 Duration::FromMilliseconds(5);
443 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7);
444 ThreadData::TallyRunOnWorkerThreadIfTracking(birth, kTimePosted,
445 kStartOfRun, kEndOfRun);
446
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900447 // Call for the ToSnapshot, but tell it to not reset the maxes after scanning.
448 ProcessDataSnapshot process_data;
449 ThreadData::Snapshot(false, &process_data);
450 ExpectSimpleProcessData(process_data, kFunction, kWorkerThreadName,
451 kWorkerThreadName, 1, 2, 4);
jar@chromium.orga0260412011-12-04 16:19:10 +0900452
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900453 // Call for the ToSnapshot, but tell it to reset the maxes after scanning.
jar@chromium.orga0260412011-12-04 16:19:10 +0900454 // We'll still get the same values, but the data will be reset (which we'll
455 // see in a moment).
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900456 ProcessDataSnapshot process_data_pre_reset;
457 ThreadData::Snapshot(true, &process_data_pre_reset);
458 ExpectSimpleProcessData(process_data, kFunction, kWorkerThreadName,
459 kWorkerThreadName, 1, 2, 4);
jar@chromium.orga0260412011-12-04 16:19:10 +0900460
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900461 // Call for the ToSnapshot, and now we'll see the result of the last
462 // translation, as the max will have been pushed back to zero.
463 ProcessDataSnapshot process_data_post_reset;
464 ThreadData::Snapshot(true, &process_data_post_reset);
465 ASSERT_EQ(1u, process_data_post_reset.tasks.size());
466 EXPECT_EQ(kFile, process_data_post_reset.tasks[0].birth.location.file_name);
467 EXPECT_EQ(kFunction,
468 process_data_post_reset.tasks[0].birth.location.function_name);
469 EXPECT_EQ(kLineNumber,
470 process_data_post_reset.tasks[0].birth.location.line_number);
471 EXPECT_EQ(kWorkerThreadName,
472 process_data_post_reset.tasks[0].birth.thread_name);
473 EXPECT_EQ(1, process_data_post_reset.tasks[0].death_data.count);
474 EXPECT_EQ(2, process_data_post_reset.tasks[0].death_data.run_duration_sum);
475 EXPECT_EQ(0, process_data_post_reset.tasks[0].death_data.run_duration_max);
476 EXPECT_EQ(2, process_data_post_reset.tasks[0].death_data.run_duration_sample);
477 EXPECT_EQ(4, process_data_post_reset.tasks[0].death_data.queue_duration_sum);
478 EXPECT_EQ(0, process_data_post_reset.tasks[0].death_data.queue_duration_max);
479 EXPECT_EQ(4,
480 process_data_post_reset.tasks[0].death_data.queue_duration_sample);
481 EXPECT_EQ(kWorkerThreadName,
482 process_data_post_reset.tasks[0].death_thread_name);
483 EXPECT_EQ(0u, process_data_post_reset.descendants.size());
484 EXPECT_EQ(base::GetCurrentProcId(), process_data_post_reset.process_id);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900485}
486
487TEST_F(TrackedObjectsTest, TwoLives) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900488 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900489 ThreadData::PROFILING_CHILDREN_ACTIVE))
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900490 return;
491
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900492 const char kFunction[] = "TwoLives";
493 Location location(kFunction, kFile, kLineNumber, NULL);
494 TallyABirth(location, kMainThreadName);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900495
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900496 const base::TimeTicks kTimePosted = base::TimeTicks() +
497 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900498 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
499 // TrackingInfo will call TallyABirth() during construction.
500 base::TrackingInfo pending_task(location, kDelayedStartTime);
501 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
502
503 const TrackedTime kStartOfRun = TrackedTime() +
504 Duration::FromMilliseconds(5);
505 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7);
506 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task,
507 kStartOfRun, kEndOfRun);
508
509 // TrackingInfo will call TallyABirth() during construction.
510 base::TrackingInfo pending_task2(location, kDelayedStartTime);
511 pending_task2.time_posted = kTimePosted; // Overwrite implied Now().
512
513 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task2,
514 kStartOfRun, kEndOfRun);
515
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900516 ProcessDataSnapshot process_data;
517 ThreadData::Snapshot(false, &process_data);
518 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
519 kMainThreadName, 2, 2, 4);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900520}
521
522TEST_F(TrackedObjectsTest, DifferentLives) {
jar@chromium.org23b00722012-02-11 04:43:42 +0900523 if (!ThreadData::InitializeAndSetTrackingStatus(
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900524 ThreadData::PROFILING_CHILDREN_ACTIVE))
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900525 return;
526
527 // Use a well named thread.
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900528 ThreadData::InitializeThreadContext(kMainThreadName);
529 const char kFunction[] = "DifferentLives";
530 Location location(kFunction, kFile, kLineNumber, NULL);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900531
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900532 const base::TimeTicks kTimePosted = base::TimeTicks() +
533 base::TimeDelta::FromMilliseconds(1);
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900534 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
535 // TrackingInfo will call TallyABirth() during construction.
536 base::TrackingInfo pending_task(location, kDelayedStartTime);
537 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900538
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900539 const TrackedTime kStartOfRun = TrackedTime() +
540 Duration::FromMilliseconds(5);
541 const TrackedTime kEndOfRun = TrackedTime() + Duration::FromMilliseconds(7);
542 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task,
543 kStartOfRun, kEndOfRun);
jar@chromium.org666ef9c2011-10-25 03:55:16 +0900544
jar@chromium.org4be2cb02011-11-01 07:36:21 +0900545 const int kSecondFakeLineNumber = 999;
546 Location second_location(kFunction, kFile, kSecondFakeLineNumber, NULL);
547
548 // TrackingInfo will call TallyABirth() during construction.
549 base::TrackingInfo pending_task2(second_location, kDelayedStartTime);
550 pending_task2.time_posted = kTimePosted; // Overwrite implied Now().
551
isherman@chromium.org98c10d22012-04-13 09:39:26 +0900552 ProcessDataSnapshot process_data;
553 ThreadData::Snapshot(false, &process_data);
554 ASSERT_EQ(2u, process_data.tasks.size());
555 EXPECT_EQ(kFile, process_data.tasks[0].birth.location.file_name);
556 EXPECT_EQ(kFunction, process_data.tasks[0].birth.location.function_name);
557 EXPECT_EQ(kLineNumber, process_data.tasks[0].birth.location.line_number);
558 EXPECT_EQ(kMainThreadName, process_data.tasks[0].birth.thread_name);
559 EXPECT_EQ(1, process_data.tasks[0].death_data.count);
560 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sum);
561 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_max);
562 EXPECT_EQ(2, process_data.tasks[0].death_data.run_duration_sample);
563 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sum);
564 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_max);
565 EXPECT_EQ(4, process_data.tasks[0].death_data.queue_duration_sample);
566 EXPECT_EQ(kMainThreadName, process_data.tasks[0].death_thread_name);
567 EXPECT_EQ(kFile, process_data.tasks[1].birth.location.file_name);
568 EXPECT_EQ(kFunction, process_data.tasks[1].birth.location.function_name);
569 EXPECT_EQ(kSecondFakeLineNumber,
570 process_data.tasks[1].birth.location.line_number);
571 EXPECT_EQ(kMainThreadName, process_data.tasks[1].birth.thread_name);
572 EXPECT_EQ(1, process_data.tasks[1].death_data.count);
573 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_sum);
574 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_max);
575 EXPECT_EQ(0, process_data.tasks[1].death_data.run_duration_sample);
576 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_sum);
577 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_max);
578 EXPECT_EQ(0, process_data.tasks[1].death_data.queue_duration_sample);
579 EXPECT_EQ(kStillAlive, process_data.tasks[1].death_thread_name);
580 EXPECT_EQ(0u, process_data.descendants.size());
581 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
initial.commit3f4a7322008-07-27 06:49:38 +0900582}
583
deanm@google.com11f192a2008-08-26 20:29:01 +0900584} // namespace tracked_objects