Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Side by Side Diff: base/tracked_objects_unittest.cc

Issue 1128733002: Update from https://crrev.com/328418 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/tracked_objects.cc ('k') | base/vlog.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Test of classes in the tracked_objects.h classes. 5 // Test of classes in the tracked_objects.h classes.
6 6
7 #include "base/tracked_objects.h" 7 #include "base/tracked_objects.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 10
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 } 64 }
65 65
66 // Helper function to verify the most common test expectations. 66 // Helper function to verify the most common test expectations.
67 void ExpectSimpleProcessData(const ProcessDataSnapshot& process_data, 67 void ExpectSimpleProcessData(const ProcessDataSnapshot& process_data,
68 const std::string& function_name, 68 const std::string& function_name,
69 const std::string& birth_thread, 69 const std::string& birth_thread,
70 const std::string& death_thread, 70 const std::string& death_thread,
71 int count, 71 int count,
72 int run_ms, 72 int run_ms,
73 int queue_ms) { 73 int queue_ms) {
74 ASSERT_EQ(1u, process_data.phased_process_data_snapshots.size()); 74 ASSERT_EQ(1u, process_data.phased_snapshots.size());
75 auto it = process_data.phased_process_data_snapshots.find(0); 75 auto it = process_data.phased_snapshots.find(0);
76 ASSERT_TRUE(it != process_data.phased_process_data_snapshots.end()); 76 ASSERT_TRUE(it != process_data.phased_snapshots.end());
77 const ProcessDataPhaseSnapshot& process_data_phase = it->second; 77 const ProcessDataPhaseSnapshot& process_data_phase = it->second;
78 78
79 ASSERT_EQ(1u, process_data_phase.tasks.size()); 79 ASSERT_EQ(1u, process_data_phase.tasks.size());
80 80
81 EXPECT_EQ(kFile, process_data_phase.tasks[0].birth.location.file_name); 81 EXPECT_EQ(kFile, process_data_phase.tasks[0].birth.location.file_name);
82 EXPECT_EQ(function_name, 82 EXPECT_EQ(function_name,
83 process_data_phase.tasks[0].birth.location.function_name); 83 process_data_phase.tasks[0].birth.location.function_name);
84 EXPECT_EQ(kLineNumber, 84 EXPECT_EQ(kLineNumber,
85 process_data_phase.tasks[0].birth.location.line_number); 85 process_data_phase.tasks[0].birth.location.line_number);
86 86
87 EXPECT_EQ(birth_thread, process_data_phase.tasks[0].birth.thread_name); 87 EXPECT_EQ(birth_thread, process_data_phase.tasks[0].birth.thread_name);
88 88
89 EXPECT_EQ(count, process_data_phase.tasks[0].death_data.count); 89 EXPECT_EQ(count, process_data_phase.tasks[0].death_data.count);
90 EXPECT_EQ(count * run_ms, 90 EXPECT_EQ(count * run_ms,
91 process_data_phase.tasks[0].death_data.run_duration_sum); 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); 92 EXPECT_EQ(run_ms, process_data_phase.tasks[0].death_data.run_duration_max);
93 EXPECT_EQ(run_ms, 93 EXPECT_EQ(run_ms,
94 process_data_phase.tasks[0].death_data.run_duration_sample); 94 process_data_phase.tasks[0].death_data.run_duration_sample);
95 EXPECT_EQ(count * queue_ms, 95 EXPECT_EQ(count * queue_ms,
96 process_data_phase.tasks[0].death_data.queue_duration_sum); 96 process_data_phase.tasks[0].death_data.queue_duration_sum);
97 EXPECT_EQ(queue_ms, 97 EXPECT_EQ(queue_ms,
98 process_data_phase.tasks[0].death_data.queue_duration_max); 98 process_data_phase.tasks[0].death_data.queue_duration_max);
99 EXPECT_EQ(queue_ms, 99 EXPECT_EQ(queue_ms,
100 process_data_phase.tasks[0].death_data.queue_duration_sample); 100 process_data_phase.tasks[0].death_data.queue_duration_sample);
101 101
102 EXPECT_EQ(death_thread, process_data_phase.tasks[0].death_thread_name); 102 EXPECT_EQ(death_thread, process_data_phase.tasks[0].death_thread_name);
103 103
104 EXPECT_EQ(0u, process_data_phase.descendants.size());
105
106 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); 104 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
107 } 105 }
108 106
109 // Sets time that will be returned by ThreadData::Now(). 107 // Sets time that will be returned by ThreadData::Now().
110 static void SetTestTime(unsigned int test_time) { test_time_ = test_time; } 108 static void SetTestTime(unsigned int test_time) { test_time_ = test_time; }
111 109
112 private: 110 private:
113 // Returns test time in milliseconds. 111 // Returns test time in milliseconds.
114 static unsigned int GetTestTime() { return test_time_; } 112 static unsigned int GetTestTime() { return test_time_; }
115 113
116 // Test time in milliseconds. 114 // Test time in milliseconds.
117 static unsigned int test_time_; 115 static unsigned int test_time_;
118 }; 116 };
119 117
120 // static 118 // static
121 unsigned int TrackedObjectsTest::test_time_; 119 unsigned int TrackedObjectsTest::test_time_;
122 120
123 TEST_F(TrackedObjectsTest, TaskStopwatchNoStartStop) { 121 TEST_F(TrackedObjectsTest, TaskStopwatchNoStartStop) {
124 if (!ThreadData::InitializeAndSetTrackingStatus( 122 if (!ThreadData::InitializeAndSetTrackingStatus(
125 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 123 ThreadData::PROFILING_ACTIVE)) {
124 // Don't run the test if task tracking is not compiled in.
126 return; 125 return;
127 } 126 }
128 127
129 // Check that creating and destroying a stopwatch without starting it doesn't 128 // Check that creating and destroying a stopwatch without starting it doesn't
130 // crash. 129 // crash.
131 TaskStopwatch stopwatch; 130 TaskStopwatch stopwatch;
132 } 131 }
133 132
134 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) { 133 TEST_F(TrackedObjectsTest, MinimalStartupShutdown) {
135 // Minimal test doesn't even create any tasks. 134 // Minimal test doesn't even create any tasks.
136 if (!ThreadData::InitializeAndSetTrackingStatus( 135 if (!ThreadData::InitializeAndSetTrackingStatus(
137 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 136 ThreadData::PROFILING_ACTIVE)) {
137 // Don't run the test if task tracking is not compiled in.
138 return; 138 return;
139 } 139 }
140 140
141 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. 141 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
142 ThreadData* data = ThreadData::Get(); 142 ThreadData* data = ThreadData::Get();
143 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. 143 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
144 ASSERT_TRUE(data); 144 ASSERT_TRUE(data);
145 EXPECT_FALSE(data->next()); 145 EXPECT_FALSE(data->next());
146 EXPECT_EQ(data, ThreadData::Get()); 146 EXPECT_EQ(data, ThreadData::Get());
147 ThreadData::BirthMap birth_map; 147 ThreadData::BirthMap birth_map;
148 ThreadData::DeathMap death_map; 148 ThreadData::DeathsSnapshot deaths;
149 ThreadData::ParentChildSet parent_child_set; 149 data->SnapshotMaps(0, &birth_map, &deaths);
150 data->SnapshotMaps(&birth_map, &death_map, &parent_child_set);
151 EXPECT_EQ(0u, birth_map.size()); 150 EXPECT_EQ(0u, birth_map.size());
152 EXPECT_EQ(0u, death_map.size()); 151 EXPECT_EQ(0u, deaths.size());
153 EXPECT_EQ(0u, parent_child_set.size());
154 152
155 // Clean up with no leaking. 153 // Clean up with no leaking.
156 Reset(); 154 Reset();
157 155
158 // Do it again, just to be sure we reset state completely. 156 // Do it again, just to be sure we reset state completely.
159 EXPECT_TRUE(ThreadData::InitializeAndSetTrackingStatus( 157 EXPECT_TRUE(
160 ThreadData::PROFILING_CHILDREN_ACTIVE)); 158 ThreadData::InitializeAndSetTrackingStatus(ThreadData::PROFILING_ACTIVE));
161 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread. 159 EXPECT_FALSE(ThreadData::first()); // No activity even on this thread.
162 data = ThreadData::Get(); 160 data = ThreadData::Get();
163 EXPECT_TRUE(ThreadData::first()); // Now class was constructed. 161 EXPECT_TRUE(ThreadData::first()); // Now class was constructed.
164 ASSERT_TRUE(data); 162 ASSERT_TRUE(data);
165 EXPECT_FALSE(data->next()); 163 EXPECT_FALSE(data->next());
166 EXPECT_EQ(data, ThreadData::Get()); 164 EXPECT_EQ(data, ThreadData::Get());
167 birth_map.clear(); 165 birth_map.clear();
168 death_map.clear(); 166 deaths.clear();
169 parent_child_set.clear(); 167 data->SnapshotMaps(0, &birth_map, &deaths);
170 data->SnapshotMaps(&birth_map, &death_map, &parent_child_set);
171 EXPECT_EQ(0u, birth_map.size()); 168 EXPECT_EQ(0u, birth_map.size());
172 EXPECT_EQ(0u, death_map.size()); 169 EXPECT_EQ(0u, deaths.size());
173 EXPECT_EQ(0u, parent_child_set.size());
174 } 170 }
175 171
176 TEST_F(TrackedObjectsTest, TinyStartupShutdown) { 172 TEST_F(TrackedObjectsTest, TinyStartupShutdown) {
177 if (!ThreadData::InitializeAndSetTrackingStatus( 173 if (!ThreadData::InitializeAndSetTrackingStatus(
178 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 174 ThreadData::PROFILING_ACTIVE)) {
175 // Don't run the test if task tracking is not compiled in.
179 return; 176 return;
180 } 177 }
181 178
182 // Instigate tracking on a single tracked object, on our thread. 179 // Instigate tracking on a single tracked object, on our thread.
183 const char kFunction[] = "TinyStartupShutdown"; 180 const char kFunction[] = "TinyStartupShutdown";
184 Location location(kFunction, kFile, kLineNumber, NULL); 181 Location location(kFunction, kFile, kLineNumber, NULL);
185 Births* first_birth = ThreadData::TallyABirthIfActive(location); 182 ThreadData::TallyABirthIfActive(location);
186 183
187 ThreadData* data = ThreadData::first(); 184 ThreadData* data = ThreadData::first();
188 ASSERT_TRUE(data); 185 ASSERT_TRUE(data);
189 EXPECT_FALSE(data->next()); 186 EXPECT_FALSE(data->next());
190 EXPECT_EQ(data, ThreadData::Get()); 187 EXPECT_EQ(data, ThreadData::Get());
191 ThreadData::BirthMap birth_map; 188 ThreadData::BirthMap birth_map;
192 ThreadData::DeathMap death_map; 189 ThreadData::DeathsSnapshot deaths;
193 ThreadData::ParentChildSet parent_child_set; 190 data->SnapshotMaps(0, &birth_map, &deaths);
194 data->SnapshotMaps(&birth_map, &death_map, &parent_child_set);
195 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. 191 EXPECT_EQ(1u, birth_map.size()); // 1 birth location.
196 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth. 192 EXPECT_EQ(1, birth_map.begin()->second->birth_count()); // 1 birth.
197 EXPECT_EQ(0u, death_map.size()); // No deaths. 193 EXPECT_EQ(0u, deaths.size()); // No deaths.
198 EXPECT_EQ(0u, parent_child_set.size()); // No children.
199 194
200 195
201 // Now instigate another birth, while we are timing the run of the first 196 // Now instigate another birth, while we are timing the run of the first
202 // execution. 197 // execution.
203 ThreadData::PrepareForStartOfRun(first_birth);
204 // Create a child (using the same birth location). 198 // Create a child (using the same birth location).
205 // TrackingInfo will call TallyABirth() during construction. 199 // TrackingInfo will call TallyABirth() during construction.
206 const int32 start_time = 1; 200 const int32 start_time = 1;
207 base::TimeTicks kBogusBirthTime = base::TimeTicks() + 201 base::TimeTicks kBogusBirthTime = base::TimeTicks() +
208 base::TimeDelta::FromMilliseconds(start_time); 202 base::TimeDelta::FromMilliseconds(start_time);
209 base::TrackingInfo pending_task(location, kBogusBirthTime); 203 base::TrackingInfo pending_task(location, kBogusBirthTime);
210 SetTestTime(1); 204 SetTestTime(1);
211 TaskStopwatch stopwatch; 205 TaskStopwatch stopwatch;
212 stopwatch.Start(); 206 stopwatch.Start();
213 // Finally conclude the outer run. 207 // Finally conclude the outer run.
214 const int32 time_elapsed = 1000; 208 const int32 time_elapsed = 1000;
215 SetTestTime(start_time + time_elapsed); 209 SetTestTime(start_time + time_elapsed);
216 stopwatch.Stop(); 210 stopwatch.Stop();
217 211
218 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch); 212 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
219 213
220 birth_map.clear(); 214 birth_map.clear();
221 death_map.clear(); 215 deaths.clear();
222 parent_child_set.clear(); 216 data->SnapshotMaps(0, &birth_map, &deaths);
223 data->SnapshotMaps(&birth_map, &death_map, &parent_child_set);
224 EXPECT_EQ(1u, birth_map.size()); // 1 birth location. 217 EXPECT_EQ(1u, birth_map.size()); // 1 birth location.
225 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births. 218 EXPECT_EQ(2, birth_map.begin()->second->birth_count()); // 2 births.
226 EXPECT_EQ(1u, death_map.size()); // 1 location. 219 EXPECT_EQ(1u, deaths.size()); // 1 location.
227 EXPECT_EQ(1, death_map.begin()->second.count()); // 1 death. 220 EXPECT_EQ(1, deaths.begin()->second.death_data.count); // 1 death.
228 if (ThreadData::TrackingParentChildStatus()) {
229 EXPECT_EQ(1u, parent_child_set.size()); // 1 child.
230 EXPECT_EQ(parent_child_set.begin()->first,
231 parent_child_set.begin()->second);
232 } else {
233 EXPECT_EQ(0u, parent_child_set.size()); // no stats.
234 }
235 221
236 // The births were at the same location as the one known death. 222 // The births were at the same location as the one known death.
237 EXPECT_EQ(birth_map.begin()->second, death_map.begin()->first); 223 EXPECT_EQ(birth_map.begin()->second, deaths.begin()->first);
238 224
239 ProcessDataSnapshot process_data; 225 ProcessDataSnapshot process_data;
240 ThreadData::Snapshot(&process_data); 226 ThreadData::Snapshot(0, &process_data);
241 227
242 ASSERT_EQ(1u, process_data.phased_process_data_snapshots.size()); 228 ASSERT_EQ(1u, process_data.phased_snapshots.size());
243 auto it = process_data.phased_process_data_snapshots.find(0); 229 auto it = process_data.phased_snapshots.find(0);
244 ASSERT_TRUE(it != process_data.phased_process_data_snapshots.end()); 230 ASSERT_TRUE(it != process_data.phased_snapshots.end());
245 const ProcessDataPhaseSnapshot& process_data_phase = it->second; 231 const ProcessDataPhaseSnapshot& process_data_phase = it->second;
246 ASSERT_EQ(1u, process_data_phase.tasks.size()); 232 ASSERT_EQ(1u, process_data_phase.tasks.size());
247 EXPECT_EQ(kFile, process_data_phase.tasks[0].birth.location.file_name); 233 EXPECT_EQ(kFile, process_data_phase.tasks[0].birth.location.file_name);
248 EXPECT_EQ(kFunction, 234 EXPECT_EQ(kFunction,
249 process_data_phase.tasks[0].birth.location.function_name); 235 process_data_phase.tasks[0].birth.location.function_name);
250 EXPECT_EQ(kLineNumber, 236 EXPECT_EQ(kLineNumber,
251 process_data_phase.tasks[0].birth.location.line_number); 237 process_data_phase.tasks[0].birth.location.line_number);
252 EXPECT_EQ(kWorkerThreadName, process_data_phase.tasks[0].birth.thread_name); 238 EXPECT_EQ(kWorkerThreadName, process_data_phase.tasks[0].birth.thread_name);
253 EXPECT_EQ(1, process_data_phase.tasks[0].death_data.count); 239 EXPECT_EQ(1, process_data_phase.tasks[0].death_data.count);
254 EXPECT_EQ(time_elapsed, 240 EXPECT_EQ(time_elapsed,
255 process_data_phase.tasks[0].death_data.run_duration_sum); 241 process_data_phase.tasks[0].death_data.run_duration_sum);
256 EXPECT_EQ(time_elapsed, 242 EXPECT_EQ(time_elapsed,
257 process_data_phase.tasks[0].death_data.run_duration_max); 243 process_data_phase.tasks[0].death_data.run_duration_max);
258 EXPECT_EQ(time_elapsed, 244 EXPECT_EQ(time_elapsed,
259 process_data_phase.tasks[0].death_data.run_duration_sample); 245 process_data_phase.tasks[0].death_data.run_duration_sample);
260 EXPECT_EQ(0, process_data_phase.tasks[0].death_data.queue_duration_sum); 246 EXPECT_EQ(0, process_data_phase.tasks[0].death_data.queue_duration_sum);
261 EXPECT_EQ(0, process_data_phase.tasks[0].death_data.queue_duration_max); 247 EXPECT_EQ(0, process_data_phase.tasks[0].death_data.queue_duration_max);
262 EXPECT_EQ(0, process_data_phase.tasks[0].death_data.queue_duration_sample); 248 EXPECT_EQ(0, process_data_phase.tasks[0].death_data.queue_duration_sample);
263 EXPECT_EQ(kWorkerThreadName, process_data_phase.tasks[0].death_thread_name); 249 EXPECT_EQ(kWorkerThreadName, process_data_phase.tasks[0].death_thread_name);
264
265 if (ThreadData::TrackingParentChildStatus()) {
266 ASSERT_EQ(1u, process_data_phase.descendants.size());
267 EXPECT_EQ(kFile,
268 process_data_phase.descendants[0].parent.location.file_name);
269 EXPECT_EQ(kFunction,
270 process_data_phase.descendants[0].parent.location.function_name);
271 EXPECT_EQ(kLineNumber,
272 process_data_phase.descendants[0].parent.location.line_number);
273 EXPECT_EQ(kWorkerThreadName,
274 process_data_phase.descendants[0].parent.thread_name);
275 EXPECT_EQ(kFile,
276 process_data_phase.descendants[0].child.location.file_name);
277 EXPECT_EQ(kFunction,
278 process_data_phase.descendants[0].child.location.function_name);
279 EXPECT_EQ(kLineNumber,
280 process_data_phase.descendants[0].child.location.line_number);
281 EXPECT_EQ(kWorkerThreadName,
282 process_data_phase.descendants[0].child.thread_name);
283 } else {
284 EXPECT_EQ(0u, process_data_phase.descendants.size());
285 }
286 } 250 }
287 251
288 TEST_F(TrackedObjectsTest, DeathDataTest) { 252 TEST_F(TrackedObjectsTest, DeathDataTestRecordDeath) {
289 if (!ThreadData::InitializeAndSetTrackingStatus( 253 if (!ThreadData::InitializeAndSetTrackingStatus(
290 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 254 ThreadData::PROFILING_ACTIVE)) {
255 // Don't run the test if task tracking is not compiled in.
291 return; 256 return;
292 } 257 }
293 258
294 scoped_ptr<DeathData> data(new DeathData()); 259 scoped_ptr<DeathData> data(new DeathData());
295 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL)); 260 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL));
296 EXPECT_EQ(data->run_duration_sum(), 0); 261 EXPECT_EQ(data->run_duration_sum(), 0);
262 EXPECT_EQ(data->run_duration_max(), 0);
297 EXPECT_EQ(data->run_duration_sample(), 0); 263 EXPECT_EQ(data->run_duration_sample(), 0);
298 EXPECT_EQ(data->queue_duration_sum(), 0); 264 EXPECT_EQ(data->queue_duration_sum(), 0);
265 EXPECT_EQ(data->queue_duration_max(), 0);
299 EXPECT_EQ(data->queue_duration_sample(), 0); 266 EXPECT_EQ(data->queue_duration_sample(), 0);
300 EXPECT_EQ(data->count(), 0); 267 EXPECT_EQ(data->count(), 0);
268 EXPECT_EQ(nullptr, data->last_phase_snapshot());
301 269
302 int32 run_ms = 42; 270 int32 run_ms = 42;
303 int32 queue_ms = 8; 271 int32 queue_ms = 8;
304 272
305 const int kUnrandomInt = 0; // Fake random int that ensure we sample data. 273 const int kUnrandomInt = 0; // Fake random int that ensure we sample data.
306 data->RecordDeath(queue_ms, run_ms, kUnrandomInt); 274 data->RecordDeath(queue_ms, run_ms, kUnrandomInt);
307 EXPECT_EQ(data->run_duration_sum(), run_ms); 275 EXPECT_EQ(data->run_duration_sum(), run_ms);
276 EXPECT_EQ(data->run_duration_max(), run_ms);
308 EXPECT_EQ(data->run_duration_sample(), run_ms); 277 EXPECT_EQ(data->run_duration_sample(), run_ms);
309 EXPECT_EQ(data->queue_duration_sum(), queue_ms); 278 EXPECT_EQ(data->queue_duration_sum(), queue_ms);
279 EXPECT_EQ(data->queue_duration_max(), queue_ms);
310 EXPECT_EQ(data->queue_duration_sample(), queue_ms); 280 EXPECT_EQ(data->queue_duration_sample(), queue_ms);
311 EXPECT_EQ(data->count(), 1); 281 EXPECT_EQ(data->count(), 1);
282 EXPECT_EQ(nullptr, data->last_phase_snapshot());
312 283
313 data->RecordDeath(queue_ms, run_ms, kUnrandomInt); 284 data->RecordDeath(queue_ms, run_ms, kUnrandomInt);
314 EXPECT_EQ(data->run_duration_sum(), run_ms + run_ms); 285 EXPECT_EQ(data->run_duration_sum(), run_ms + run_ms);
286 EXPECT_EQ(data->run_duration_max(), run_ms);
315 EXPECT_EQ(data->run_duration_sample(), run_ms); 287 EXPECT_EQ(data->run_duration_sample(), run_ms);
316 EXPECT_EQ(data->queue_duration_sum(), queue_ms + queue_ms); 288 EXPECT_EQ(data->queue_duration_sum(), queue_ms + queue_ms);
289 EXPECT_EQ(data->queue_duration_max(), queue_ms);
317 EXPECT_EQ(data->queue_duration_sample(), queue_ms); 290 EXPECT_EQ(data->queue_duration_sample(), queue_ms);
318 EXPECT_EQ(data->count(), 2); 291 EXPECT_EQ(data->count(), 2);
292 EXPECT_EQ(nullptr, data->last_phase_snapshot());
293 }
319 294
320 DeathDataSnapshot snapshot(*data); 295 TEST_F(TrackedObjectsTest, DeathDataTest2Phases) {
321 EXPECT_EQ(2, snapshot.count); 296 if (!ThreadData::InitializeAndSetTrackingStatus(
322 EXPECT_EQ(2 * run_ms, snapshot.run_duration_sum); 297 ThreadData::PROFILING_ACTIVE)) {
323 EXPECT_EQ(run_ms, snapshot.run_duration_max); 298 // Don't run the test if task tracking is not compiled in.
324 EXPECT_EQ(run_ms, snapshot.run_duration_sample); 299 return;
325 EXPECT_EQ(2 * queue_ms, snapshot.queue_duration_sum); 300 }
326 EXPECT_EQ(queue_ms, snapshot.queue_duration_max); 301
327 EXPECT_EQ(queue_ms, snapshot.queue_duration_sample); 302 scoped_ptr<DeathData> data(new DeathData());
303 ASSERT_NE(data, reinterpret_cast<DeathData*>(NULL));
304
305 int32 run_ms = 42;
306 int32 queue_ms = 8;
307
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);
311
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);
326 EXPECT_EQ(run_ms,
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);
330 EXPECT_EQ(queue_ms,
331 data->last_phase_snapshot()->death_data.queue_duration_max);
332 EXPECT_EQ(queue_ms,
333 data->last_phase_snapshot()->death_data.queue_duration_sample);
334 EXPECT_EQ(nullptr, data->last_phase_snapshot()->prev);
335
336 int32 run_ms1 = 21;
337 int32 queue_ms1 = 4;
338
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);
353 EXPECT_EQ(run_ms,
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);
357 EXPECT_EQ(queue_ms,
358 data->last_phase_snapshot()->death_data.queue_duration_max);
359 EXPECT_EQ(queue_ms,
360 data->last_phase_snapshot()->death_data.queue_duration_sample);
361 EXPECT_EQ(nullptr, data->last_phase_snapshot()->prev);
362 }
363
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.
368 return;
369 }
370
371 DeathDataSnapshot snapshot;
372 snapshot.count = 10;
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;
379
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;
388
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);
328 } 397 }
329 398
330 TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotWorkerThread) { 399 TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotWorkerThread) {
331 // Start in the deactivated state. 400 // Start in the deactivated state.
332 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) { 401 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) {
402 // Don't run the test if task tracking is not compiled in.
333 return; 403 return;
334 } 404 }
335 405
336 const char kFunction[] = "DeactivatedBirthOnlyToSnapshotWorkerThread"; 406 const char kFunction[] = "DeactivatedBirthOnlyToSnapshotWorkerThread";
337 Location location(kFunction, kFile, kLineNumber, NULL); 407 Location location(kFunction, kFile, kLineNumber, NULL);
338 TallyABirth(location, std::string()); 408 TallyABirth(location, std::string());
339 409
340 ProcessDataSnapshot process_data; 410 ProcessDataSnapshot process_data;
341 ThreadData::Snapshot(&process_data); 411 ThreadData::Snapshot(0, &process_data);
342 412
343 ASSERT_EQ(1u, process_data.phased_process_data_snapshots.size()); 413 ASSERT_EQ(1u, process_data.phased_snapshots.size());
344 auto it = process_data.phased_process_data_snapshots.find(0); 414
345 ASSERT_TRUE(it != process_data.phased_process_data_snapshots.end()); 415 auto it = process_data.phased_snapshots.find(0);
416 ASSERT_TRUE(it != process_data.phased_snapshots.end());
346 const ProcessDataPhaseSnapshot& process_data_phase = it->second; 417 const ProcessDataPhaseSnapshot& process_data_phase = it->second;
347 418
348 EXPECT_EQ(0u, process_data_phase.tasks.size()); 419 ASSERT_EQ(0u, process_data_phase.tasks.size());
349 EXPECT_EQ(0u, process_data_phase.descendants.size()); 420
350 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); 421 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
351 } 422 }
352 423
353 TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotMainThread) { 424 TEST_F(TrackedObjectsTest, DeactivatedBirthOnlyToSnapshotMainThread) {
354 // Start in the deactivated state. 425 // Start in the deactivated state.
355 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) { 426 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) {
427 // Don't run the test if task tracking is not compiled in.
356 return; 428 return;
357 } 429 }
358 430
359 const char kFunction[] = "DeactivatedBirthOnlyToSnapshotMainThread"; 431 const char kFunction[] = "DeactivatedBirthOnlyToSnapshotMainThread";
360 Location location(kFunction, kFile, kLineNumber, NULL); 432 Location location(kFunction, kFile, kLineNumber, NULL);
361 TallyABirth(location, kMainThreadName); 433 TallyABirth(location, kMainThreadName);
362 434
363 ProcessDataSnapshot process_data; 435 ProcessDataSnapshot process_data;
364 ThreadData::Snapshot(&process_data); 436 ThreadData::Snapshot(0, &process_data);
365 437
366 ASSERT_EQ(1u, process_data.phased_process_data_snapshots.size()); 438 ASSERT_EQ(1u, process_data.phased_snapshots.size());
367 auto it = process_data.phased_process_data_snapshots.find(0); 439
368 ASSERT_TRUE(it != process_data.phased_process_data_snapshots.end()); 440 auto it = process_data.phased_snapshots.find(0);
441 ASSERT_TRUE(it != process_data.phased_snapshots.end());
369 const ProcessDataPhaseSnapshot& process_data_phase = it->second; 442 const ProcessDataPhaseSnapshot& process_data_phase = it->second;
370 443
371 EXPECT_EQ(0u, process_data_phase.tasks.size()); 444 ASSERT_EQ(0u, process_data_phase.tasks.size());
372 EXPECT_EQ(0u, process_data_phase.descendants.size()); 445
373 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); 446 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
374 } 447 }
375 448
376 TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotWorkerThread) { 449 TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotWorkerThread) {
377 if (!ThreadData::InitializeAndSetTrackingStatus( 450 if (!ThreadData::InitializeAndSetTrackingStatus(
378 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 451 ThreadData::PROFILING_ACTIVE)) {
452 // Don't run the test if task tracking is not compiled in.
379 return; 453 return;
380 } 454 }
381 455
382 const char kFunction[] = "BirthOnlyToSnapshotWorkerThread"; 456 const char kFunction[] = "BirthOnlyToSnapshotWorkerThread";
383 Location location(kFunction, kFile, kLineNumber, NULL); 457 Location location(kFunction, kFile, kLineNumber, NULL);
384 TallyABirth(location, std::string()); 458 TallyABirth(location, std::string());
385 459
386 ProcessDataSnapshot process_data; 460 ProcessDataSnapshot process_data;
387 ThreadData::Snapshot(&process_data); 461 ThreadData::Snapshot(0, &process_data);
388 ExpectSimpleProcessData(process_data, kFunction, kWorkerThreadName, 462 ExpectSimpleProcessData(process_data, kFunction, kWorkerThreadName,
389 kStillAlive, 1, 0, 0); 463 kStillAlive, 1, 0, 0);
390 } 464 }
391 465
392 TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotMainThread) { 466 TEST_F(TrackedObjectsTest, BirthOnlyToSnapshotMainThread) {
393 if (!ThreadData::InitializeAndSetTrackingStatus( 467 if (!ThreadData::InitializeAndSetTrackingStatus(
394 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 468 ThreadData::PROFILING_ACTIVE)) {
469 // Don't run the test if task tracking is not compiled in.
395 return; 470 return;
396 } 471 }
397 472
398 const char kFunction[] = "BirthOnlyToSnapshotMainThread"; 473 const char kFunction[] = "BirthOnlyToSnapshotMainThread";
399 Location location(kFunction, kFile, kLineNumber, NULL); 474 Location location(kFunction, kFile, kLineNumber, NULL);
400 TallyABirth(location, kMainThreadName); 475 TallyABirth(location, kMainThreadName);
401 476
402 ProcessDataSnapshot process_data; 477 ProcessDataSnapshot process_data;
403 ThreadData::Snapshot(&process_data); 478 ThreadData::Snapshot(0, &process_data);
404 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, kStillAlive, 479 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, kStillAlive,
405 1, 0, 0); 480 1, 0, 0);
406 } 481 }
407 482
408 TEST_F(TrackedObjectsTest, LifeCycleToSnapshotMainThread) { 483 TEST_F(TrackedObjectsTest, LifeCycleToSnapshotMainThread) {
409 if (!ThreadData::InitializeAndSetTrackingStatus( 484 if (!ThreadData::InitializeAndSetTrackingStatus(
410 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 485 ThreadData::PROFILING_ACTIVE)) {
486 // Don't run the test if task tracking is not compiled in.
411 return; 487 return;
412 } 488 }
413 489
414 const char kFunction[] = "LifeCycleToSnapshotMainThread"; 490 const char kFunction[] = "LifeCycleToSnapshotMainThread";
415 Location location(kFunction, kFile, kLineNumber, NULL); 491 Location location(kFunction, kFile, kLineNumber, NULL);
416 TallyABirth(location, kMainThreadName); 492 TallyABirth(location, kMainThreadName);
417 493
418 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1); 494 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1);
419 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); 495 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
420 // TrackingInfo will call TallyABirth() during construction. 496 // TrackingInfo will call TallyABirth() during construction.
421 base::TrackingInfo pending_task(location, kDelayedStartTime); 497 base::TrackingInfo pending_task(location, kDelayedStartTime);
422 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). 498 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
423 499
424 const unsigned int kStartOfRun = 5; 500 const unsigned int kStartOfRun = 5;
425 const unsigned int kEndOfRun = 7; 501 const unsigned int kEndOfRun = 7;
426 SetTestTime(kStartOfRun); 502 SetTestTime(kStartOfRun);
427 TaskStopwatch stopwatch; 503 TaskStopwatch stopwatch;
428 stopwatch.Start(); 504 stopwatch.Start();
429 SetTestTime(kEndOfRun); 505 SetTestTime(kEndOfRun);
430 stopwatch.Stop(); 506 stopwatch.Stop();
431 507
432 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch); 508 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
433 509
434 ProcessDataSnapshot process_data; 510 ProcessDataSnapshot process_data;
435 ThreadData::Snapshot(&process_data); 511 ThreadData::Snapshot(0, &process_data);
436 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, 512 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
437 kMainThreadName, 1, 2, 4); 513 kMainThreadName, 1, 2, 4);
438 } 514 }
439 515
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.
520 return;
521 }
522
523 const char kFunction[] = "TwoPhases";
524 Location location(kFunction, kFile, kLineNumber, NULL);
525 TallyABirth(location, kMainThreadName);
526
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().
532
533 const unsigned int kStartOfRun = 5;
534 const unsigned int kEndOfRun = 7;
535 SetTestTime(kStartOfRun);
536 TaskStopwatch stopwatch;
537 stopwatch.Start();
538 SetTestTime(kEndOfRun);
539 stopwatch.Stop();
540
541 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
542
543 ThreadData::OnProfilingPhaseCompleted(0);
544
545 TallyABirth(location, kMainThreadName);
546
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().
552
553 const unsigned int kStartOfRun1 = 11;
554 const unsigned int kEndOfRun1 = 21;
555 SetTestTime(kStartOfRun1);
556 TaskStopwatch stopwatch1;
557 stopwatch1.Start();
558 SetTestTime(kEndOfRun1);
559 stopwatch1.Stop();
560
561 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task1, stopwatch1);
562
563 ProcessDataSnapshot process_data;
564 ThreadData::Snapshot(1, &process_data);
565
566 ASSERT_EQ(2u, process_data.phased_snapshots.size());
567
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;
571
572 ASSERT_EQ(1u, process_data_phase0.tasks.size());
573
574 EXPECT_EQ(kFile, process_data_phase0.tasks[0].birth.location.file_name);
575 EXPECT_EQ(kFunction,
576 process_data_phase0.tasks[0].birth.location.function_name);
577 EXPECT_EQ(kLineNumber,
578 process_data_phase0.tasks[0].birth.location.line_number);
579
580 EXPECT_EQ(kMainThreadName, process_data_phase0.tasks[0].birth.thread_name);
581
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);
589
590 EXPECT_EQ(kMainThreadName, process_data_phase0.tasks[0].death_thread_name);
591
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;
595
596 ASSERT_EQ(1u, process_data_phase1.tasks.size());
597
598 EXPECT_EQ(kFile, process_data_phase1.tasks[0].birth.location.file_name);
599 EXPECT_EQ(kFunction,
600 process_data_phase1.tasks[0].birth.location.function_name);
601 EXPECT_EQ(kLineNumber,
602 process_data_phase1.tasks[0].birth.location.line_number);
603
604 EXPECT_EQ(kMainThreadName, process_data_phase1.tasks[0].birth.thread_name);
605
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);
613
614 EXPECT_EQ(kMainThreadName, process_data_phase1.tasks[0].death_thread_name);
615
616 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
617 }
618
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.
623 return;
624 }
625
626 const char kFunction[] = "ThreePhases";
627 Location location(kFunction, kFile, kLineNumber, NULL);
628
629 // Phase 0
630 {
631 TallyABirth(location, kMainThreadName);
632
633 // TrackingInfo will call TallyABirth() during construction.
634 SetTestTime(10);
635 base::TrackingInfo pending_task(location, base::TimeTicks());
636
637 SetTestTime(17);
638 TaskStopwatch stopwatch;
639 stopwatch.Start();
640 SetTestTime(23);
641 stopwatch.Stop();
642
643 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
644 }
645
646 ThreadData::OnProfilingPhaseCompleted(0);
647
648 // Phase 1
649 {
650 TallyABirth(location, kMainThreadName);
651
652 SetTestTime(30);
653 base::TrackingInfo pending_task(location, base::TimeTicks());
654
655 SetTestTime(35);
656 TaskStopwatch stopwatch;
657 stopwatch.Start();
658 SetTestTime(39);
659 stopwatch.Stop();
660
661 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
662 }
663
664 ThreadData::OnProfilingPhaseCompleted(1);
665
666 // Phase 2
667 {
668 TallyABirth(location, kMainThreadName);
669
670 // TrackingInfo will call TallyABirth() during construction.
671 SetTestTime(40);
672 base::TrackingInfo pending_task(location, base::TimeTicks());
673
674 SetTestTime(43);
675 TaskStopwatch stopwatch;
676 stopwatch.Start();
677 SetTestTime(45);
678 stopwatch.Stop();
679
680 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
681 }
682
683 // Snapshot and check results.
684 ProcessDataSnapshot process_data;
685 ThreadData::Snapshot(2, &process_data);
686
687 ASSERT_EQ(3u, process_data.phased_snapshots.size());
688
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;
692
693 ASSERT_EQ(1u, process_data_phase0.tasks.size());
694
695 EXPECT_EQ(kFile, process_data_phase0.tasks[0].birth.location.file_name);
696 EXPECT_EQ(kFunction,
697 process_data_phase0.tasks[0].birth.location.function_name);
698 EXPECT_EQ(kLineNumber,
699 process_data_phase0.tasks[0].birth.location.line_number);
700
701 EXPECT_EQ(kMainThreadName, process_data_phase0.tasks[0].birth.thread_name);
702
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);
710
711 EXPECT_EQ(kMainThreadName, process_data_phase0.tasks[0].death_thread_name);
712
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;
716
717 ASSERT_EQ(1u, process_data_phase1.tasks.size());
718
719 EXPECT_EQ(kFile, process_data_phase1.tasks[0].birth.location.file_name);
720 EXPECT_EQ(kFunction,
721 process_data_phase1.tasks[0].birth.location.function_name);
722 EXPECT_EQ(kLineNumber,
723 process_data_phase1.tasks[0].birth.location.line_number);
724
725 EXPECT_EQ(kMainThreadName, process_data_phase1.tasks[0].birth.thread_name);
726
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);
734
735 EXPECT_EQ(kMainThreadName, process_data_phase1.tasks[0].death_thread_name);
736
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;
740
741 ASSERT_EQ(1u, process_data_phase2.tasks.size());
742
743 EXPECT_EQ(kFile, process_data_phase2.tasks[0].birth.location.file_name);
744 EXPECT_EQ(kFunction,
745 process_data_phase2.tasks[0].birth.location.function_name);
746 EXPECT_EQ(kLineNumber,
747 process_data_phase2.tasks[0].birth.location.line_number);
748
749 EXPECT_EQ(kMainThreadName, process_data_phase2.tasks[0].birth.thread_name);
750
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);
758
759 EXPECT_EQ(kMainThreadName, process_data_phase2.tasks[0].death_thread_name);
760
761 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
762 }
763
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.
768 return;
769 }
770
771 const char kFunction[] = "TwoPhasesSecondEmpty";
772 Location location(kFunction, kFile, kLineNumber, NULL);
773 ThreadData::InitializeThreadContext(kMainThreadName);
774
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().
780
781 const unsigned int kStartOfRun = 5;
782 const unsigned int kEndOfRun = 7;
783 SetTestTime(kStartOfRun);
784 TaskStopwatch stopwatch;
785 stopwatch.Start();
786 SetTestTime(kEndOfRun);
787 stopwatch.Stop();
788
789 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
790
791 ThreadData::OnProfilingPhaseCompleted(0);
792
793 ProcessDataSnapshot process_data;
794 ThreadData::Snapshot(1, &process_data);
795
796 ASSERT_EQ(2u, process_data.phased_snapshots.size());
797
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;
801
802 ASSERT_EQ(1u, process_data_phase0.tasks.size());
803
804 EXPECT_EQ(kFile, process_data_phase0.tasks[0].birth.location.file_name);
805 EXPECT_EQ(kFunction,
806 process_data_phase0.tasks[0].birth.location.function_name);
807 EXPECT_EQ(kLineNumber,
808 process_data_phase0.tasks[0].birth.location.line_number);
809
810 EXPECT_EQ(kMainThreadName, process_data_phase0.tasks[0].birth.thread_name);
811
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);
819
820 EXPECT_EQ(kMainThreadName, process_data_phase0.tasks[0].death_thread_name);
821
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;
825
826 ASSERT_EQ(0u, process_data_phase1.tasks.size());
827
828 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
829 }
830
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.
835 return;
836 }
837
838 ThreadData::OnProfilingPhaseCompleted(0);
839
840 const char kFunction[] = "TwoPhasesSecondEmpty";
841 Location location(kFunction, kFile, kLineNumber, NULL);
842 ThreadData::InitializeThreadContext(kMainThreadName);
843
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().
849
850 const unsigned int kStartOfRun = 5;
851 const unsigned int kEndOfRun = 7;
852 SetTestTime(kStartOfRun);
853 TaskStopwatch stopwatch;
854 stopwatch.Start();
855 SetTestTime(kEndOfRun);
856 stopwatch.Stop();
857
858 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
859
860 ProcessDataSnapshot process_data;
861 ThreadData::Snapshot(1, &process_data);
862
863 ASSERT_EQ(1u, process_data.phased_snapshots.size());
864
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;
868
869 ASSERT_EQ(1u, process_data_phase1.tasks.size());
870
871 EXPECT_EQ(kFile, process_data_phase1.tasks[0].birth.location.file_name);
872 EXPECT_EQ(kFunction,
873 process_data_phase1.tasks[0].birth.location.function_name);
874 EXPECT_EQ(kLineNumber,
875 process_data_phase1.tasks[0].birth.location.line_number);
876
877 EXPECT_EQ(kMainThreadName, process_data_phase1.tasks[0].birth.thread_name);
878
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);
886
887 EXPECT_EQ(kMainThreadName, process_data_phase1.tasks[0].death_thread_name);
888
889 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
890 }
891
440 // We will deactivate tracking after the birth, and before the death, and 892 // We will deactivate tracking after the birth, and before the death, and
441 // demonstrate that the lifecycle is completely tallied. This ensures that 893 // demonstrate that the lifecycle is completely tallied. This ensures that
442 // our tallied births are matched by tallied deaths (except for when the 894 // our tallied births are matched by tallied deaths (except for when the
443 // task is still running, or is queued). 895 // task is still running, or is queued).
444 TEST_F(TrackedObjectsTest, LifeCycleMidDeactivatedToSnapshotMainThread) { 896 TEST_F(TrackedObjectsTest, LifeCycleMidDeactivatedToSnapshotMainThread) {
445 if (!ThreadData::InitializeAndSetTrackingStatus( 897 if (!ThreadData::InitializeAndSetTrackingStatus(
446 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 898 ThreadData::PROFILING_ACTIVE)) {
447 return; 899 // Don't run the test if task tracking is not compiled in.
448 } 900 return;
449 901 }
902
450 const char kFunction[] = "LifeCycleMidDeactivatedToSnapshotMainThread"; 903 const char kFunction[] = "LifeCycleMidDeactivatedToSnapshotMainThread";
451 Location location(kFunction, kFile, kLineNumber, NULL); 904 Location location(kFunction, kFile, kLineNumber, NULL);
452 TallyABirth(location, kMainThreadName); 905 TallyABirth(location, kMainThreadName);
453 906
454 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1); 907 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1);
455 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); 908 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
456 // TrackingInfo will call TallyABirth() during construction. 909 // TrackingInfo will call TallyABirth() during construction.
457 base::TrackingInfo pending_task(location, kDelayedStartTime); 910 base::TrackingInfo pending_task(location, kDelayedStartTime);
458 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). 911 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
459 912
460 // Turn off tracking now that we have births. 913 // Turn off tracking now that we have births.
461 EXPECT_TRUE( 914 EXPECT_TRUE(
462 ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)); 915 ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED));
463 916
464 const unsigned int kStartOfRun = 5; 917 const unsigned int kStartOfRun = 5;
465 const unsigned int kEndOfRun = 7; 918 const unsigned int kEndOfRun = 7;
466 SetTestTime(kStartOfRun); 919 SetTestTime(kStartOfRun);
467 TaskStopwatch stopwatch; 920 TaskStopwatch stopwatch;
468 stopwatch.Start(); 921 stopwatch.Start();
469 SetTestTime(kEndOfRun); 922 SetTestTime(kEndOfRun);
470 stopwatch.Stop(); 923 stopwatch.Stop();
471 924
472 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch); 925 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
473 926
474 ProcessDataSnapshot process_data; 927 ProcessDataSnapshot process_data;
475 ThreadData::Snapshot(&process_data); 928 ThreadData::Snapshot(0, &process_data);
476 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, 929 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
477 kMainThreadName, 1, 2, 4); 930 kMainThreadName, 1, 2, 4);
478 } 931 }
479 932
480 // We will deactivate tracking before starting a life cycle, and neither 933 // We will deactivate tracking before starting a life cycle, and neither
481 // the birth nor the death will be recorded. 934 // the birth nor the death will be recorded.
482 TEST_F(TrackedObjectsTest, LifeCyclePreDeactivatedToSnapshotMainThread) { 935 TEST_F(TrackedObjectsTest, LifeCyclePreDeactivatedToSnapshotMainThread) {
483 // Start in the deactivated state. 936 // Start in the deactivated state.
484 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) { 937 if (!ThreadData::InitializeAndSetTrackingStatus(ThreadData::DEACTIVATED)) {
938 // Don't run the test if task tracking is not compiled in.
485 return; 939 return;
486 } 940 }
487 941
488 const char kFunction[] = "LifeCyclePreDeactivatedToSnapshotMainThread"; 942 const char kFunction[] = "LifeCyclePreDeactivatedToSnapshotMainThread";
489 Location location(kFunction, kFile, kLineNumber, NULL); 943 Location location(kFunction, kFile, kLineNumber, NULL);
490 TallyABirth(location, kMainThreadName); 944 TallyABirth(location, kMainThreadName);
491 945
492 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1); 946 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1);
493 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); 947 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
494 // TrackingInfo will call TallyABirth() during construction. 948 // TrackingInfo will call TallyABirth() during construction.
495 base::TrackingInfo pending_task(location, kDelayedStartTime); 949 base::TrackingInfo pending_task(location, kDelayedStartTime);
496 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). 950 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
497 951
498 const unsigned int kStartOfRun = 5; 952 const unsigned int kStartOfRun = 5;
499 const unsigned int kEndOfRun = 7; 953 const unsigned int kEndOfRun = 7;
500 SetTestTime(kStartOfRun); 954 SetTestTime(kStartOfRun);
501 TaskStopwatch stopwatch; 955 TaskStopwatch stopwatch;
502 stopwatch.Start(); 956 stopwatch.Start();
503 SetTestTime(kEndOfRun); 957 SetTestTime(kEndOfRun);
504 stopwatch.Stop(); 958 stopwatch.Stop();
505 959
506 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch); 960 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
507 961
508 ProcessDataSnapshot process_data; 962 ProcessDataSnapshot process_data;
509 ThreadData::Snapshot(&process_data); 963 ThreadData::Snapshot(0, &process_data);
510 964
511 ASSERT_EQ(1u, process_data.phased_process_data_snapshots.size()); 965 ASSERT_EQ(1u, process_data.phased_snapshots.size());
512 auto it = process_data.phased_process_data_snapshots.find(0); 966
513 ASSERT_TRUE(it != process_data.phased_process_data_snapshots.end()); 967 auto it = process_data.phased_snapshots.find(0);
968 ASSERT_TRUE(it != process_data.phased_snapshots.end());
514 const ProcessDataPhaseSnapshot& process_data_phase = it->second; 969 const ProcessDataPhaseSnapshot& process_data_phase = it->second;
515 970
516 EXPECT_EQ(0u, process_data_phase.tasks.size()); 971 ASSERT_EQ(0u, process_data_phase.tasks.size());
517 EXPECT_EQ(0u, process_data_phase.descendants.size()); 972
518 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); 973 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
519 } 974 }
520 975
521 TEST_F(TrackedObjectsTest, TwoLives) { 976 TEST_F(TrackedObjectsTest, TwoLives) {
522 if (!ThreadData::InitializeAndSetTrackingStatus( 977 if (!ThreadData::InitializeAndSetTrackingStatus(
523 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 978 ThreadData::PROFILING_ACTIVE)) {
979 // Don't run the test if task tracking is not compiled in.
524 return; 980 return;
525 } 981 }
526 982
527 const char kFunction[] = "TwoLives"; 983 const char kFunction[] = "TwoLives";
528 Location location(kFunction, kFile, kLineNumber, NULL); 984 Location location(kFunction, kFile, kLineNumber, NULL);
529 TallyABirth(location, kMainThreadName); 985 TallyABirth(location, kMainThreadName);
530 986
531 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1); 987 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1);
532 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); 988 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
533 // TrackingInfo will call TallyABirth() during construction. 989 // TrackingInfo will call TallyABirth() during construction.
(...skipping 15 matching lines...) Expand all
549 pending_task2.time_posted = kTimePosted; // Overwrite implied Now(). 1005 pending_task2.time_posted = kTimePosted; // Overwrite implied Now().
550 SetTestTime(kStartOfRun); 1006 SetTestTime(kStartOfRun);
551 TaskStopwatch stopwatch2; 1007 TaskStopwatch stopwatch2;
552 stopwatch2.Start(); 1008 stopwatch2.Start();
553 SetTestTime(kEndOfRun); 1009 SetTestTime(kEndOfRun);
554 stopwatch2.Stop(); 1010 stopwatch2.Stop();
555 1011
556 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task2, stopwatch2); 1012 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task2, stopwatch2);
557 1013
558 ProcessDataSnapshot process_data; 1014 ProcessDataSnapshot process_data;
559 ThreadData::Snapshot(&process_data); 1015 ThreadData::Snapshot(0, &process_data);
560 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, 1016 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
561 kMainThreadName, 2, 2, 4); 1017 kMainThreadName, 2, 2, 4);
562 } 1018 }
563 1019
564 TEST_F(TrackedObjectsTest, DifferentLives) { 1020 TEST_F(TrackedObjectsTest, DifferentLives) {
565 if (!ThreadData::InitializeAndSetTrackingStatus( 1021 if (!ThreadData::InitializeAndSetTrackingStatus(
566 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 1022 ThreadData::PROFILING_ACTIVE)) {
1023 // Don't run the test if task tracking is not compiled in.
567 return; 1024 return;
568 } 1025 }
569 1026
570 // Use a well named thread. 1027 // Use a well named thread.
571 ThreadData::InitializeThreadContext(kMainThreadName); 1028 ThreadData::InitializeThreadContext(kMainThreadName);
572 const char kFunction[] = "DifferentLives"; 1029 const char kFunction[] = "DifferentLives";
573 Location location(kFunction, kFile, kLineNumber, NULL); 1030 Location location(kFunction, kFile, kLineNumber, NULL);
574 1031
575 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1); 1032 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1);
576 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); 1033 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
(...skipping 12 matching lines...) Expand all
589 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch); 1046 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, stopwatch);
590 1047
591 const int kSecondFakeLineNumber = 999; 1048 const int kSecondFakeLineNumber = 999;
592 Location second_location(kFunction, kFile, kSecondFakeLineNumber, NULL); 1049 Location second_location(kFunction, kFile, kSecondFakeLineNumber, NULL);
593 1050
594 // TrackingInfo will call TallyABirth() during construction. 1051 // TrackingInfo will call TallyABirth() during construction.
595 base::TrackingInfo pending_task2(second_location, kDelayedStartTime); 1052 base::TrackingInfo pending_task2(second_location, kDelayedStartTime);
596 pending_task2.time_posted = kTimePosted; // Overwrite implied Now(). 1053 pending_task2.time_posted = kTimePosted; // Overwrite implied Now().
597 1054
598 ProcessDataSnapshot process_data; 1055 ProcessDataSnapshot process_data;
599 ThreadData::Snapshot(&process_data); 1056 ThreadData::Snapshot(0, &process_data);
600 1057
601 ASSERT_EQ(1u, process_data.phased_process_data_snapshots.size()); 1058 ASSERT_EQ(1u, process_data.phased_snapshots.size());
602 auto it = process_data.phased_process_data_snapshots.find(0); 1059 auto it = process_data.phased_snapshots.find(0);
603 ASSERT_TRUE(it != process_data.phased_process_data_snapshots.end()); 1060 ASSERT_TRUE(it != process_data.phased_snapshots.end());
604 const ProcessDataPhaseSnapshot& process_data_phase = it->second; 1061 const ProcessDataPhaseSnapshot& process_data_phase = it->second;
605 1062
606 ASSERT_EQ(2u, process_data_phase.tasks.size()); 1063 ASSERT_EQ(2u, process_data_phase.tasks.size());
607 1064
608 EXPECT_EQ(kFile, process_data_phase.tasks[0].birth.location.file_name); 1065 EXPECT_EQ(kFile, process_data_phase.tasks[0].birth.location.file_name);
609 EXPECT_EQ(kFunction, 1066 EXPECT_EQ(kFunction,
610 process_data_phase.tasks[0].birth.location.function_name); 1067 process_data_phase.tasks[0].birth.location.function_name);
611 EXPECT_EQ(kLineNumber, 1068 EXPECT_EQ(kLineNumber,
612 process_data_phase.tasks[0].birth.location.line_number); 1069 process_data_phase.tasks[0].birth.location.line_number);
613 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[0].birth.thread_name); 1070 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[0].birth.thread_name);
(...skipping 12 matching lines...) Expand all
626 process_data_phase.tasks[1].birth.location.line_number); 1083 process_data_phase.tasks[1].birth.location.line_number);
627 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[1].birth.thread_name); 1084 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[1].birth.thread_name);
628 EXPECT_EQ(1, process_data_phase.tasks[1].death_data.count); 1085 EXPECT_EQ(1, process_data_phase.tasks[1].death_data.count);
629 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.run_duration_sum); 1086 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.run_duration_sum);
630 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.run_duration_max); 1087 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.run_duration_max);
631 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.run_duration_sample); 1088 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.run_duration_sample);
632 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.queue_duration_sum); 1089 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.queue_duration_sum);
633 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.queue_duration_max); 1090 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.queue_duration_max);
634 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.queue_duration_sample); 1091 EXPECT_EQ(0, process_data_phase.tasks[1].death_data.queue_duration_sample);
635 EXPECT_EQ(kStillAlive, process_data_phase.tasks[1].death_thread_name); 1092 EXPECT_EQ(kStillAlive, process_data_phase.tasks[1].death_thread_name);
636 EXPECT_EQ(0u, process_data_phase.descendants.size());
637 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); 1093 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
638 } 1094 }
639 1095
640 TEST_F(TrackedObjectsTest, TaskWithNestedExclusion) { 1096 TEST_F(TrackedObjectsTest, TaskWithNestedExclusion) {
641 if (!ThreadData::InitializeAndSetTrackingStatus( 1097 if (!ThreadData::InitializeAndSetTrackingStatus(
642 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 1098 ThreadData::PROFILING_ACTIVE)) {
1099 // Don't run the test if task tracking is not compiled in.
643 return; 1100 return;
644 } 1101 }
645 1102
646 const char kFunction[] = "TaskWithNestedExclusion"; 1103 const char kFunction[] = "TaskWithNestedExclusion";
647 Location location(kFunction, kFile, kLineNumber, NULL); 1104 Location location(kFunction, kFile, kLineNumber, NULL);
648 TallyABirth(location, kMainThreadName); 1105 TallyABirth(location, kMainThreadName);
649 1106
650 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1); 1107 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1);
651 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); 1108 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
652 // TrackingInfo will call TallyABirth() during construction. 1109 // TrackingInfo will call TallyABirth() during construction.
653 base::TrackingInfo pending_task(location, kDelayedStartTime); 1110 base::TrackingInfo pending_task(location, kDelayedStartTime);
654 pending_task.time_posted = kTimePosted; // Overwrite implied Now(). 1111 pending_task.time_posted = kTimePosted; // Overwrite implied Now().
655 1112
656 SetTestTime(5); 1113 SetTestTime(5);
657 TaskStopwatch task_stopwatch; 1114 TaskStopwatch task_stopwatch;
658 task_stopwatch.Start(); 1115 task_stopwatch.Start();
659 { 1116 {
660 SetTestTime(8); 1117 SetTestTime(8);
661 TaskStopwatch exclusion_stopwatch; 1118 TaskStopwatch exclusion_stopwatch;
662 exclusion_stopwatch.Start(); 1119 exclusion_stopwatch.Start();
663 SetTestTime(12); 1120 SetTestTime(12);
664 exclusion_stopwatch.Stop(); 1121 exclusion_stopwatch.Stop();
665 } 1122 }
666 SetTestTime(15); 1123 SetTestTime(15);
667 task_stopwatch.Stop(); 1124 task_stopwatch.Stop();
668 1125
669 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch); 1126 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch);
670 1127
671 ProcessDataSnapshot process_data; 1128 ProcessDataSnapshot process_data;
672 ThreadData::Snapshot(&process_data); 1129 ThreadData::Snapshot(0, &process_data);
673 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, 1130 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
674 kMainThreadName, 1, 6, 4); 1131 kMainThreadName, 1, 6, 4);
675 } 1132 }
676 1133
677 TEST_F(TrackedObjectsTest, TaskWith2NestedExclusions) { 1134 TEST_F(TrackedObjectsTest, TaskWith2NestedExclusions) {
678 if (!ThreadData::InitializeAndSetTrackingStatus( 1135 if (!ThreadData::InitializeAndSetTrackingStatus(
679 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 1136 ThreadData::PROFILING_ACTIVE)) {
1137 // Don't run the test if task tracking is not compiled in.
680 return; 1138 return;
681 } 1139 }
682 1140
683 const char kFunction[] = "TaskWith2NestedExclusions"; 1141 const char kFunction[] = "TaskWith2NestedExclusions";
684 Location location(kFunction, kFile, kLineNumber, NULL); 1142 Location location(kFunction, kFile, kLineNumber, NULL);
685 TallyABirth(location, kMainThreadName); 1143 TallyABirth(location, kMainThreadName);
686 1144
687 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1); 1145 const TrackedTime kTimePosted = TrackedTime::FromMilliseconds(1);
688 const base::TimeTicks kDelayedStartTime = base::TimeTicks(); 1146 const base::TimeTicks kDelayedStartTime = base::TimeTicks();
689 // TrackingInfo will call TallyABirth() during construction. 1147 // TrackingInfo will call TallyABirth() during construction.
(...skipping 15 matching lines...) Expand all
705 exclusion_stopwatch2.Start(); 1163 exclusion_stopwatch2.Start();
706 SetTestTime(18); 1164 SetTestTime(18);
707 exclusion_stopwatch2.Stop(); 1165 exclusion_stopwatch2.Stop();
708 } 1166 }
709 SetTestTime(25); 1167 SetTestTime(25);
710 task_stopwatch.Stop(); 1168 task_stopwatch.Stop();
711 1169
712 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch); 1170 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch);
713 1171
714 ProcessDataSnapshot process_data; 1172 ProcessDataSnapshot process_data;
715 ThreadData::Snapshot(&process_data); 1173 ThreadData::Snapshot(0, &process_data);
716 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName, 1174 ExpectSimpleProcessData(process_data, kFunction, kMainThreadName,
717 kMainThreadName, 1, 13, 4); 1175 kMainThreadName, 1, 13, 4);
718 } 1176 }
719 1177
720 TEST_F(TrackedObjectsTest, TaskWithNestedExclusionWithNestedTask) { 1178 TEST_F(TrackedObjectsTest, TaskWithNestedExclusionWithNestedTask) {
721 if (!ThreadData::InitializeAndSetTrackingStatus( 1179 if (!ThreadData::InitializeAndSetTrackingStatus(
722 ThreadData::PROFILING_CHILDREN_ACTIVE)) { 1180 ThreadData::PROFILING_ACTIVE)) {
1181 // Don't run the test if task tracking is not compiled in.
723 return; 1182 return;
724 } 1183 }
725 1184
726 const char kFunction[] = "TaskWithNestedExclusionWithNestedTask"; 1185 const char kFunction[] = "TaskWithNestedExclusionWithNestedTask";
727 Location location(kFunction, kFile, kLineNumber, NULL); 1186 Location location(kFunction, kFile, kLineNumber, NULL);
728 1187
729 const int kSecondFakeLineNumber = 999; 1188 const int kSecondFakeLineNumber = 999;
730 1189
731 TallyABirth(location, kMainThreadName); 1190 TallyABirth(location, kMainThreadName);
732 1191
(...skipping 25 matching lines...) Expand all
758 } 1217 }
759 SetTestTime(12); 1218 SetTestTime(12);
760 exclusion_stopwatch.Stop(); 1219 exclusion_stopwatch.Stop();
761 } 1220 }
762 SetTestTime(15); 1221 SetTestTime(15);
763 task_stopwatch.Stop(); 1222 task_stopwatch.Stop();
764 1223
765 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch); 1224 ThreadData::TallyRunOnNamedThreadIfTracking(pending_task, task_stopwatch);
766 1225
767 ProcessDataSnapshot process_data; 1226 ProcessDataSnapshot process_data;
768 ThreadData::Snapshot(&process_data); 1227 ThreadData::Snapshot(0, &process_data);
769 1228
770 ASSERT_EQ(1u, process_data.phased_process_data_snapshots.size()); 1229 ASSERT_EQ(1u, process_data.phased_snapshots.size());
771 auto it = process_data.phased_process_data_snapshots.find(0); 1230 auto it = process_data.phased_snapshots.find(0);
772 ASSERT_TRUE(it != process_data.phased_process_data_snapshots.end()); 1231 ASSERT_TRUE(it != process_data.phased_snapshots.end());
773 const ProcessDataPhaseSnapshot& process_data_phase = it->second; 1232 const ProcessDataPhaseSnapshot& process_data_phase = it->second;
774 1233
775 // The order in which the two task follow is platform-dependent. 1234 // The order in which the two task follow is platform-dependent.
776 int t0 = 1235 int t0 =
777 (process_data_phase.tasks[0].birth.location.line_number == kLineNumber) 1236 (process_data_phase.tasks[0].birth.location.line_number == kLineNumber)
778 ? 0 1237 ? 0
779 : 1; 1238 : 1;
780 int t1 = 1 - t0; 1239 int t1 = 1 - t0;
781 1240
782 ASSERT_EQ(2u, process_data_phase.tasks.size()); 1241 ASSERT_EQ(2u, process_data_phase.tasks.size());
(...skipping 18 matching lines...) Expand all
801 process_data_phase.tasks[t1].birth.location.line_number); 1260 process_data_phase.tasks[t1].birth.location.line_number);
802 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[t1].birth.thread_name); 1261 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[t1].birth.thread_name);
803 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.count); 1262 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.count);
804 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_sum); 1263 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_sum);
805 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_max); 1264 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_max);
806 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_sample); 1265 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_sample);
807 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_sum); 1266 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_sum);
808 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_max); 1267 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_max);
809 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_sample); 1268 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_sample);
810 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[t1].death_thread_name); 1269 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[t1].death_thread_name);
811 EXPECT_EQ(0u, process_data_phase.descendants.size());
812 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); 1270 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
813 } 1271 }
814 1272
815 } // namespace tracked_objects 1273 } // namespace tracked_objects
OLDNEW
« no previous file with comments | « base/tracked_objects.cc ('k') | base/vlog.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698