OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef BASE_TRACKED_OBJECTS_H_ | 5 #ifndef BASE_TRACKED_OBJECTS_H_ |
6 #define BASE_TRACKED_OBJECTS_H_ | 6 #define BASE_TRACKED_OBJECTS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <stack> | 10 #include <stack> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "base/threading/thread_local_storage.h" | 18 #include "base/threading/thread_local_storage.h" |
| 19 #include "base/tracking_info.h" |
19 #include "base/values.h" | 20 #include "base/values.h" |
20 | 21 |
| 22 #if defined(OS_WIN) |
| 23 #include <mmsystem.h> // Declare timeGetTime(); |
| 24 #endif |
| 25 |
21 // TrackedObjects provides a database of stats about objects (generally Tasks) | 26 // TrackedObjects provides a database of stats about objects (generally Tasks) |
22 // that are tracked. Tracking means their birth, death, duration, birth thread, | 27 // that are tracked. Tracking means their birth, death, duration, birth thread, |
23 // death thread, and birth place are recorded. This data is carefully spread | 28 // death thread, and birth place are recorded. This data is carefully spread |
24 // across a series of objects so that the counts and times can be rapidly | 29 // across a series of objects so that the counts and times can be rapidly |
25 // updated without (usually) having to lock the data, and hence there is usually | 30 // updated without (usually) having to lock the data, and hence there is usually |
26 // very little contention caused by the tracking. The data can be viewed via | 31 // very little contention caused by the tracking. The data can be viewed via |
27 // the about:tracking URL, with a variety of sorting and filtering choices. | 32 // the about:tracking URL, with a variety of sorting and filtering choices. |
28 // | 33 // |
29 // These classes serve as the basis of a profiler of sorts for the Tasks system. | 34 // These classes serve as the basis of a profiler of sorts for the Tasks system. |
30 // As a result, design decisions were made to maximize speed, by minimizing | 35 // As a result, design decisions were made to maximize speed, by minimizing |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 // For Tasks, having now either constructed or found the Births instance | 67 // For Tasks, having now either constructed or found the Births instance |
63 // described above, a pointer to the Births instance is then recorded into the | 68 // described above, a pointer to the Births instance is then recorded into the |
64 // PendingTask structure in MessageLoop. This fact alone is very useful in | 69 // PendingTask structure in MessageLoop. This fact alone is very useful in |
65 // debugging, when there is a question of where an instance came from. In | 70 // debugging, when there is a question of where an instance came from. In |
66 // addition, the birth time is also recorded and used to later evaluate the | 71 // addition, the birth time is also recorded and used to later evaluate the |
67 // lifetime duration of the whole Task. As a result of the above embedding, we | 72 // lifetime duration of the whole Task. As a result of the above embedding, we |
68 // can find out a Task's location of birth, and thread of birth, without using | 73 // can find out a Task's location of birth, and thread of birth, without using |
69 // any locks, as all that data is constant across the life of the process. | 74 // any locks, as all that data is constant across the life of the process. |
70 // | 75 // |
71 // The above work *could* also be done for any other object as well by calling | 76 // The above work *could* also be done for any other object as well by calling |
72 // TallyABirthIfActive() and TallyADeathIfActive() as appropriate. | 77 // TallyABirthIfActive() and TallyRunOnNamedThreadIfTracking() as appropriate. |
73 // | 78 // |
74 // The amount of memory used in the above data structures depends on how many | 79 // The amount of memory used in the above data structures depends on how many |
75 // threads there are, and how many Locations of construction there are. | 80 // threads there are, and how many Locations of construction there are. |
76 // Fortunately, we don't use memory that is the product of those two counts, but | 81 // Fortunately, we don't use memory that is the product of those two counts, but |
77 // rather we only need one Births instance for each thread that constructs an | 82 // rather we only need one Births instance for each thread that constructs an |
78 // instance at a Location. In many cases, instances are only created on one | 83 // instance at a Location. In many cases, instances are only created on one |
79 // thread, so the memory utilization is actually fairly restrained. | 84 // thread, so the memory utilization is actually fairly restrained. |
80 // | 85 // |
81 // Lastly, when an instance is deleted, the final tallies of statistics are | 86 // Lastly, when an instance is deleted, the final tallies of statistics are |
82 // carefully accumulated. That tallying wrties into slots (members) in a | 87 // carefully accumulated. That tallying wrties into slots (members) in a |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 // asynchronously relative to ongoing updates, and worse yet, some data fields | 165 // asynchronously relative to ongoing updates, and worse yet, some data fields |
161 // are 64bit quantities, and are not atomicly accessed (reset or incremented | 166 // are 64bit quantities, and are not atomicly accessed (reset or incremented |
162 // etc.). For basic profiling, this will work "most of the time," and should be | 167 // etc.). For basic profiling, this will work "most of the time," and should be |
163 // sufficient... but storing away DataCollections is the "right way" to do this. | 168 // sufficient... but storing away DataCollections is the "right way" to do this. |
164 | 169 |
165 class MessageLoop; | 170 class MessageLoop; |
166 | 171 |
167 namespace tracked_objects { | 172 namespace tracked_objects { |
168 | 173 |
169 //------------------------------------------------------------------------------ | 174 //------------------------------------------------------------------------------ |
| 175 |
| 176 #define USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS |
| 177 |
| 178 #if defined(USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS) |
| 179 |
| 180 // TimeTicks maintains a wasteful 64 bits of data (we need less than 32), and on |
| 181 // windows, a 64 bit timer is expensive to even obtain. We use a simple |
| 182 // millisecond counter for most of our time values, as well as millisecond units |
| 183 // of duration between those values. This means we can only handle durations |
| 184 // up to 49 days (range), or 24 days (non-negative time durations). |
| 185 // We only define enough methods to service the needs of the tracking classes, |
| 186 // and our interfaces are modeled after what TimeTicks and TimeDelta use (so we |
| 187 // can swap them into place if we want to use the "real" classes). |
| 188 |
| 189 class BASE_EXPORT Duration { // Similar to base::TimeDelta. |
| 190 public: |
| 191 Duration() : ms_(0) {} |
| 192 |
| 193 Duration& operator+=(const Duration& other) { |
| 194 ms_ += other.ms_; |
| 195 return *this; |
| 196 } |
| 197 |
| 198 Duration operator+(const Duration& other) const { |
| 199 return Duration(ms_ + other.ms_); |
| 200 } |
| 201 |
| 202 bool operator==(const Duration& other) const { return ms_ == other.ms_; } |
| 203 bool operator!=(const Duration& other) const { return ms_ != other.ms_; } |
| 204 bool operator>(const Duration& other) const { return ms_ > other.ms_; } |
| 205 |
| 206 static Duration FromMilliseconds(int ms) { return Duration(ms); } |
| 207 |
| 208 int32 InMilliseconds() const { return ms_; } |
| 209 |
| 210 private: |
| 211 friend class TrackedTime; |
| 212 explicit Duration(int32 duration) : ms_(duration) {} |
| 213 |
| 214 // Internal time is stored directly in milliseconds. |
| 215 int32 ms_; |
| 216 }; |
| 217 |
| 218 class BASE_EXPORT TrackedTime { // Similar to base::TimeTicks. |
| 219 public: |
| 220 TrackedTime() : ms_(0) {} |
| 221 explicit TrackedTime(const base::TimeTicks& time) |
| 222 : ms_((time - base::TimeTicks()).InMilliseconds()) { |
| 223 } |
| 224 |
| 225 static TrackedTime Now() { |
| 226 #if defined(OS_WIN) |
| 227 // Use lock-free accessor to 32 bit time. |
| 228 // Note that TimeTicks::Now() is built on this, so we have "compatible" |
| 229 // times when we down-convert a TimeTicks sample. |
| 230 // TODO(jar): Surface this interface via something in base/time.h. |
| 231 return TrackedTime(static_cast<int32>(::timeGetTime())); |
| 232 #else |
| 233 // Posix has nice cheap 64 bit times, so we just down-convert it. |
| 234 return TrackedTime(base::TimeTicks::Now()); |
| 235 #endif // OS_WIN |
| 236 } |
| 237 |
| 238 Duration operator-(const TrackedTime& other) const { |
| 239 return Duration(ms_ - other.ms_); |
| 240 } |
| 241 |
| 242 TrackedTime operator+(const Duration& other) const { |
| 243 return TrackedTime(ms_ + other.ms_); |
| 244 } |
| 245 |
| 246 bool is_null() const { return ms_ == 0; } |
| 247 |
| 248 private: |
| 249 friend class Duration; |
| 250 explicit TrackedTime(int32 ms) : ms_(ms) {} |
| 251 |
| 252 // Internal duration is stored directly in milliseconds. |
| 253 uint32 ms_; |
| 254 }; |
| 255 |
| 256 #else |
| 257 |
| 258 // Just use full 64 bit time calculations, and the slower TimeTicks::Now(). |
| 259 typedef base::TimeTicks TrackedTime; |
| 260 typedef base::TimeDelta Duration; |
| 261 |
| 262 #endif // USE_FAST_TIME_CLASS_FOR_DURATION_CALCULATIONS |
| 263 |
| 264 //------------------------------------------------------------------------------ |
170 // For a specific thread, and a specific birth place, the collection of all | 265 // For a specific thread, and a specific birth place, the collection of all |
171 // death info (with tallies for each death thread, to prevent access conflicts). | 266 // death info (with tallies for each death thread, to prevent access conflicts). |
172 class ThreadData; | 267 class ThreadData; |
173 class BASE_EXPORT BirthOnThread { | 268 class BASE_EXPORT BirthOnThread { |
174 public: | 269 public: |
175 BirthOnThread(const Location& location, const ThreadData& current); | 270 BirthOnThread(const Location& location, const ThreadData& current); |
176 | 271 |
177 const Location location() const { return location_; } | 272 const Location location() const { return location_; } |
178 const ThreadData* birth_thread() const { return birth_thread_; } | 273 const ThreadData* birth_thread() const { return birth_thread_; } |
179 | 274 |
(...skipping 30 matching lines...) Expand all Loading... |
210 void Clear() { birth_count_ = 0; } | 305 void Clear() { birth_count_ = 0; } |
211 | 306 |
212 private: | 307 private: |
213 // The number of births on this thread for our location_. | 308 // The number of births on this thread for our location_. |
214 int birth_count_; | 309 int birth_count_; |
215 | 310 |
216 DISALLOW_COPY_AND_ASSIGN(Births); | 311 DISALLOW_COPY_AND_ASSIGN(Births); |
217 }; | 312 }; |
218 | 313 |
219 //------------------------------------------------------------------------------ | 314 //------------------------------------------------------------------------------ |
220 // Basic info summarizing multiple destructions of an object with a single | 315 // Basic info summarizing multiple destructions of a tracked object with a |
221 // birthplace (fixed Location). Used both on specific threads, and also used | 316 // single birthplace (fixed Location). Used both on specific threads, and also |
222 // in snapshots when integrating assembled data. | 317 // in snapshots when integrating assembled data. |
223 | 318 |
224 class BASE_EXPORT DeathData { | 319 class BASE_EXPORT DeathData { |
225 public: | 320 public: |
226 // Default initializer. | 321 // Default initializer. |
227 DeathData() : count_(0) {} | 322 DeathData() : count_(0) {} |
228 | 323 |
229 // When deaths have not yet taken place, and we gather data from all the | 324 // When deaths have not yet taken place, and we gather data from all the |
230 // threads, we create DeathData stats that tally the number of births without | 325 // threads, we create DeathData stats that tally the number of births without |
231 // a corrosponding death. | 326 // a corrosponding death. |
232 explicit DeathData(int count) : count_(count) {} | 327 explicit DeathData(int count) : count_(count) {} |
233 | 328 |
234 // Update stats for a task destruction (death) that had a Run() time of | 329 // Update stats for a task destruction (death) that had a Run() time of |
235 // |duration|, and has had a queueing delay of |queue_duration|. | 330 // |duration|, and has had a queueing delay of |queue_duration|. |
236 void RecordDeath(const base::TimeDelta& queue_duration, | 331 void RecordDeath(const Duration& queue_duration, |
237 const base::TimeDelta& run_duration); | 332 const Duration& run_duration); |
238 | 333 |
239 // Metrics accessors. | 334 // Metrics accessors. |
240 int count() const { return count_; } | 335 int count() const { return count_; } |
241 base::TimeDelta run_duration() const { return run_duration_; } | 336 Duration run_duration() const { return run_duration_; } |
242 int AverageMsRunDuration() const; | 337 int AverageMsRunDuration() const; |
243 base::TimeDelta queue_duration() const { return queue_duration_; } | 338 Duration queue_duration() const { return queue_duration_; } |
244 int AverageMsQueueDuration() const; | 339 int AverageMsQueueDuration() const; |
245 | 340 |
246 // Accumulate metrics from other into this. This method is never used on | 341 // Accumulate metrics from other into this. This method is never used on |
247 // realtime statistics, and only used in snapshots and aggregatinos. | 342 // realtime statistics, and only used in snapshots and aggregatinos. |
248 void AddDeathData(const DeathData& other); | 343 void AddDeathData(const DeathData& other); |
249 | 344 |
250 // Simple print of internal state for use in line of HTML. | 345 // Simple print of internal state for use in line of HTML. |
251 void WriteHTML(std::string* output) const; | 346 void WriteHTML(std::string* output) const; |
252 | 347 |
253 // Constructe a DictionaryValue instance containing all our stats. The caller | 348 // Construct a DictionaryValue instance containing all our stats. The caller |
254 // assumes ownership of the returned instance. | 349 // assumes ownership of the returned instance. |
255 base::DictionaryValue* ToValue() const; | 350 base::DictionaryValue* ToValue() const; |
256 | 351 |
257 // Reset all tallies to zero. This is used as a hack on realtime data. | 352 // Reset all tallies to zero. This is used as a hack on realtime data. |
258 void Clear(); | 353 void Clear(); |
259 | 354 |
260 private: | 355 private: |
261 int count_; // Number of destructions. | 356 int count_; // Number of destructions. |
262 base::TimeDelta run_duration_; // Sum of all Run()time durations. | 357 Duration run_duration_; // Sum of all Run()time durations. |
263 base::TimeDelta queue_duration_; // Sum of all queue time durations. | 358 Duration queue_duration_; // Sum of all queue time durations. |
264 }; | 359 }; |
265 | 360 |
266 //------------------------------------------------------------------------------ | 361 //------------------------------------------------------------------------------ |
267 // A temporary collection of data that can be sorted and summarized. It is | 362 // A temporary collection of data that can be sorted and summarized. It is |
268 // gathered (carefully) from many threads. Instances are held in arrays and | 363 // gathered (carefully) from many threads. Instances are held in arrays and |
269 // processed, filtered, and rendered. | 364 // processed, filtered, and rendered. |
270 // The source of this data was collected on many threads, and is asynchronously | 365 // The source of this data was collected on many threads, and is asynchronously |
271 // changing. The data in this instance is not asynchronously changing. | 366 // changing. The data in this instance is not asynchronously changing. |
272 | 367 |
273 class BASE_EXPORT Snapshot { | 368 class BASE_EXPORT Snapshot { |
274 public: | 369 public: |
275 // When snapshotting a full life cycle set (birth-to-death), use this: | 370 // When snapshotting a full life cycle set (birth-to-death), use this: |
276 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, | 371 Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, |
277 const DeathData& death_data); | 372 const DeathData& death_data); |
278 | 373 |
279 // When snapshotting a birth, with no death yet, use this: | 374 // When snapshotting a birth, with no death yet, use this: |
280 Snapshot(const BirthOnThread& birth_on_thread, int count); | 375 Snapshot(const BirthOnThread& birth_on_thread, int count); |
281 | 376 |
282 const ThreadData* birth_thread() const { return birth_->birth_thread(); } | 377 const ThreadData* birth_thread() const { return birth_->birth_thread(); } |
283 const Location location() const { return birth_->location(); } | 378 const Location location() const { return birth_->location(); } |
284 const BirthOnThread& birth() const { return *birth_; } | 379 const BirthOnThread& birth() const { return *birth_; } |
285 const ThreadData* death_thread() const {return death_thread_; } | 380 const ThreadData* death_thread() const {return death_thread_; } |
286 const DeathData& death_data() const { return death_data_; } | 381 const DeathData& death_data() const { return death_data_; } |
287 const std::string DeathThreadName() const; | 382 const std::string DeathThreadName() const; |
288 | 383 |
289 int count() const { return death_data_.count(); } | 384 int count() const { return death_data_.count(); } |
290 base::TimeDelta run_duration() const { return death_data_.run_duration(); } | 385 Duration run_duration() const { return death_data_.run_duration(); } |
| 386 Duration queue_duration() const { return death_data_.queue_duration(); } |
291 int AverageMsRunDuration() const { | 387 int AverageMsRunDuration() const { |
292 return death_data_.AverageMsRunDuration(); | 388 return death_data_.AverageMsRunDuration(); |
293 } | 389 } |
294 base::TimeDelta queue_duration() const { | |
295 return death_data_.queue_duration(); | |
296 } | |
297 int AverageMsQueueDuration() const { | 390 int AverageMsQueueDuration() const { |
298 return death_data_.AverageMsQueueDuration(); | 391 return death_data_.AverageMsQueueDuration(); |
299 } | 392 } |
300 | 393 |
301 // Emit contents for use in a line of HTML | 394 // Emit contents for use in a line of HTML |
302 void WriteHTML(std::string* output) const; | 395 void WriteHTML(std::string* output) const; |
303 | 396 |
304 // Construct a DictionaryValue instance containing all our data recursively. | 397 // Construct a DictionaryValue instance containing all our data recursively. |
305 // The caller assumes ownership of the memory in the returned instance. | 398 // The caller assumes ownership of the memory in the returned instance. |
306 base::DictionaryValue* ToValue() const; | 399 base::DictionaryValue* ToValue() const; |
307 | 400 |
308 void Add(const Snapshot& other); | |
309 | |
310 private: | 401 private: |
311 const BirthOnThread* birth_; // Includes Location and birth_thread. | 402 const BirthOnThread* birth_; // Includes Location and birth_thread. |
312 const ThreadData* death_thread_; | 403 const ThreadData* death_thread_; |
313 DeathData death_data_; | 404 DeathData death_data_; |
314 }; | 405 }; |
315 | 406 |
316 //------------------------------------------------------------------------------ | 407 //------------------------------------------------------------------------------ |
317 // DataCollector is a container class for Snapshot and BirthOnThread count | 408 // DataCollector is a container class for Snapshot and BirthOnThread count |
318 // items. | 409 // items. |
319 | 410 |
320 class BASE_EXPORT DataCollector { | 411 class BASE_EXPORT DataCollector { |
321 public: | 412 public: |
322 typedef std::vector<Snapshot> Collection; | 413 typedef std::vector<Snapshot> Collection; |
323 | 414 |
324 // Construct with a list of how many threads should contribute. This helps us | 415 // Construct with a list of how many threads should contribute. This helps us |
325 // determine (in the async case) when we are done with all contributions. | 416 // determine (in the async case) when we are done with all contributions. |
326 DataCollector(); | 417 DataCollector(); |
327 ~DataCollector(); | 418 ~DataCollector(); |
328 | 419 |
329 // Add all stats from the indicated thread into our arrays. This function is | 420 // Adds all stats from the indicated thread into our arrays. This function |
330 // mutex protected, and *could* be called from any threads (although current | 421 // uses locks at the lowest level (when accessing the underlying maps which |
331 // implementation serialized calls to Append). | 422 // could change when not locked), and can be called from any threads. |
332 void Append(const ThreadData& thread_data); | 423 void Append(const ThreadData& thread_data); |
333 | 424 |
334 // After the accumulation phase, the following accessor is used to process the | 425 // After the accumulation phase, the following accessor is used to process the |
335 // data. | 426 // data (i.e., sort it, filter it, etc.). |
336 Collection* collection(); | 427 Collection* collection(); |
337 | 428 |
338 // After collection of death data is complete, we can add entries for all the | 429 // Adds entries for all the remaining living objects (objects that have |
339 // remaining living objects. | 430 // tallied a birth, but have not yet tallied a matching death, and hence must |
| 431 // be either running, queued up, or being held in limbo for future posting). |
| 432 // This should be called after all known ThreadData instances have been |
| 433 // processed using Append(). |
340 void AddListOfLivingObjects(); | 434 void AddListOfLivingObjects(); |
341 | 435 |
342 // Generate a ListValue representation of the vector of snapshots. The caller | 436 // Generates a ListValue representation of the vector of snapshots. The caller |
343 // assumes ownership of the memory in the returned instance. | 437 // assumes ownership of the memory in the returned instance. |
344 base::ListValue* ToValue() const; | 438 base::ListValue* ToValue() const; |
345 | 439 |
346 private: | 440 private: |
347 typedef std::map<const BirthOnThread*, int> BirthCount; | 441 typedef std::map<const BirthOnThread*, int> BirthCount; |
348 | 442 |
349 // The array that we collect data into. | 443 // The array that we collect data into. |
350 Collection collection_; | 444 Collection collection_; |
351 | 445 |
352 // The total number of births recorded at each location for which we have not | 446 // The total number of births recorded at each location for which we have not |
353 // seen a death count. | 447 // seen a death count. This map changes as we do Append() calls, and is later |
| 448 // used by AddListOfLivingObjects() to gather up unaccounted for births. |
354 BirthCount global_birth_count_; | 449 BirthCount global_birth_count_; |
355 | 450 |
356 DISALLOW_COPY_AND_ASSIGN(DataCollector); | 451 DISALLOW_COPY_AND_ASSIGN(DataCollector); |
357 }; | 452 }; |
358 | 453 |
359 //------------------------------------------------------------------------------ | 454 //------------------------------------------------------------------------------ |
360 // Aggregation contains summaries (totals and subtotals) of groups of Snapshot | 455 // Aggregation contains summaries (totals and subtotals) of groups of Snapshot |
361 // instances to provide printing of these collections on a single line. | 456 // instances to provide printing of these collections on a single line. |
| 457 // We generally provide an aggregate total for the entire list, as well as |
| 458 // aggregate subtotals for groups of stats (example: group of all lives that |
| 459 // died on the specific thread). |
362 | 460 |
363 class BASE_EXPORT Aggregation: public DeathData { | 461 class BASE_EXPORT Aggregation: public DeathData { |
364 public: | 462 public: |
365 Aggregation(); | 463 Aggregation(); |
366 ~Aggregation(); | 464 ~Aggregation(); |
367 | 465 |
368 void AddDeathSnapshot(const Snapshot& snapshot); | 466 void AddDeathSnapshot(const Snapshot& snapshot); |
369 void AddBirths(const Births& births); | 467 void AddBirths(const Births& births); |
370 void AddBirth(const BirthOnThread& birth); | 468 void AddBirth(const BirthOnThread& birth); |
371 void AddBirthPlace(const Location& location); | 469 void AddBirthPlace(const Location& location); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 BIRTH_FUNCTION = 8, | 505 BIRTH_FUNCTION = 8, |
408 BIRTH_LINE = 16, | 506 BIRTH_LINE = 16, |
409 COUNT = 32, | 507 COUNT = 32, |
410 AVERAGE_RUN_DURATION = 64, | 508 AVERAGE_RUN_DURATION = 64, |
411 TOTAL_RUN_DURATION = 128, | 509 TOTAL_RUN_DURATION = 128, |
412 AVERAGE_QUEUE_DURATION = 256, | 510 AVERAGE_QUEUE_DURATION = 256, |
413 TOTAL_QUEUE_DURATION = 512, | 511 TOTAL_QUEUE_DURATION = 512, |
414 | 512 |
415 // Imediate action keywords. | 513 // Imediate action keywords. |
416 RESET_ALL_DATA = -1, | 514 RESET_ALL_DATA = -1, |
| 515 UNKNOWN_KEYWORD = -2, |
417 }; | 516 }; |
418 | 517 |
419 explicit Comparator(); | 518 explicit Comparator(); |
420 | 519 |
421 // Reset the comparator to a NIL selector. Clear() and recursively delete any | 520 // Reset the comparator to a NIL selector. Clear() and recursively delete any |
422 // tiebreaker_ entries. NOTE: We can't use a standard destructor, because | 521 // tiebreaker_ entries. NOTE: We can't use a standard destructor, because |
423 // the sort algorithm makes copies of this object, and then deletes them, | 522 // the sort algorithm makes copies of this object, and then deletes them, |
424 // which would cause problems (either we'd make expensive deep copies, or we'd | 523 // which would cause problems (either we'd make expensive deep copies, or we'd |
425 // do more thna one delete on a tiebreaker_. | 524 // do more thna one delete on a tiebreaker_. |
426 void Clear(); | 525 void Clear(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 bool WriteSortGrouping(const Snapshot& sample, std::string* output) const; | 562 bool WriteSortGrouping(const Snapshot& sample, std::string* output) const; |
464 | 563 |
465 // Output a sample, with SortGroup details not displayed. | 564 // Output a sample, with SortGroup details not displayed. |
466 void WriteSnapshotHTML(const Snapshot& sample, std::string* output) const; | 565 void WriteSnapshotHTML(const Snapshot& sample, std::string* output) const; |
467 | 566 |
468 private: | 567 private: |
469 // The selector directs this instance to compare based on the specified | 568 // The selector directs this instance to compare based on the specified |
470 // members of the tested elements. | 569 // members of the tested elements. |
471 enum Selector selector_; | 570 enum Selector selector_; |
472 | 571 |
| 572 // Translate a path keyword into a selector. This is a slow implementation, |
| 573 // but this is rarely done, and only for HTML presentations. |
| 574 static Selector FindSelector(const std::string& keyword); |
| 575 |
473 // For filtering into acceptable and unacceptable snapshot instance, the | 576 // For filtering into acceptable and unacceptable snapshot instance, the |
474 // following is required to be a substring of the selector_ field. | 577 // following is required to be a substring of the selector_ field. |
475 std::string required_; | 578 std::string required_; |
476 | 579 |
477 // If this instance can't decide on an ordering, we can consult a tie-breaker | 580 // If this instance can't decide on an ordering, we can consult a tie-breaker |
478 // which may have a different basis of comparison. | 581 // which may have a different basis of comparison. |
479 Comparator* tiebreaker_; | 582 Comparator* tiebreaker_; |
480 | 583 |
481 // We or together all the selectors we sort on (not counting sub-group | 584 // We or together all the selectors we sort on (not counting sub-group |
482 // selectors), so that we can tell if we've decided to group on any given | 585 // selectors), so that we can tell if we've decided to group on any given |
483 // criteria. | 586 // criteria. |
484 int combined_selectors_; | 587 int combined_selectors_; |
485 | 588 |
486 // Some tiebreakrs are for subgroup ordering, and not for basic ordering (in | 589 // Some tiebreakrs are for subgroup ordering, and not for basic ordering (in |
487 // preparation for aggregation). The subgroup tiebreakers are not consulted | 590 // preparation for aggregation). The subgroup tiebreakers are not consulted |
488 // when deciding if two items are in equivalent groups. This flag tells us | 591 // when deciding if two items are in equivalent groups. This flag tells us |
489 // to ignore the tiebreaker when doing Equivalent() testing. | 592 // to ignore the tiebreaker when doing Equivalent() testing. |
490 bool use_tiebreaker_for_sort_only_; | 593 bool use_tiebreaker_for_sort_only_; |
491 }; | 594 }; |
492 | 595 |
493 //------------------------------------------------------------------------------ | 596 //------------------------------------------------------------------------------ |
494 // For each thread, we have a ThreadData that stores all tracking info generated | 597 // For each thread, we have a ThreadData that stores all tracking info generated |
495 // on this thread. This prevents the need for locking as data accumulates. | 598 // on this thread. This prevents the need for locking as data accumulates. |
| 599 // We use ThreadLocalStorage to quickly identfy the current ThreadData context. |
| 600 // We also have a linked list of ThreadData instances, and that list is used to |
| 601 // harvest data from all existing instances. |
496 | 602 |
497 class BASE_EXPORT ThreadData { | 603 class BASE_EXPORT ThreadData { |
498 public: | 604 public: |
| 605 // Current allowable states of the tracking system. The states can vary |
| 606 // between ACTIVE and DEACTIVATED, but can never go back to UNINITIALIZED. |
| 607 enum Status { |
| 608 UNINITIALIZED, |
| 609 ACTIVE, |
| 610 DEACTIVATED, |
| 611 }; |
| 612 |
499 typedef std::map<Location, Births*> BirthMap; | 613 typedef std::map<Location, Births*> BirthMap; |
500 typedef std::map<const Births*, DeathData> DeathMap; | 614 typedef std::map<const Births*, DeathData> DeathMap; |
501 | 615 |
502 // Initialize the current thread context with a new instance of ThreadData. | 616 // Initialize the current thread context with a new instance of ThreadData. |
503 // This is used by all threads that have names, and can be explicitly | 617 // This is used by all threads that have names, and should be explicitly |
504 // set *before* any births are threads have taken place. It is generally | 618 // set *before* any births on the threads have taken place. It is generally |
505 // only used by the message loop, which has a well defined name. | 619 // only used by the message loop, which has a well defined thread name. |
506 static void InitializeThreadContext(const std::string& suggested_name); | 620 static void InitializeThreadContext(const std::string& suggested_name); |
507 | 621 |
508 // Using Thread Local Store, find the current instance for collecting data. | 622 // Using Thread Local Store, find the current instance for collecting data. |
509 // If an instance does not exist, construct one (and remember it for use on | 623 // If an instance does not exist, construct one (and remember it for use on |
510 // this thread. | 624 // this thread. |
511 // This may return NULL if the system is disabled for any reason. | 625 // This may return NULL if the system is disabled for any reason. |
512 static ThreadData* Get(); | 626 static ThreadData* Get(); |
513 | 627 |
514 // For a given (unescaped) about:tracking query, develop resulting HTML, and | 628 // For a given (unescaped) about:tracking query, develop resulting HTML, and |
515 // append to output. | 629 // append to output. |
516 static void WriteHTML(const std::string& query, std::string* output); | 630 static void WriteHTML(const std::string& query, std::string* output); |
517 | 631 |
518 // Constructe a ListValue instance containing all recursive results in our | |
519 // process. The caller assumes ownership of the memory in the returned | |
520 // instance. The |process_type| should become an enum, which corresponds | |
521 // to all possible process types. I'm using an int as a placeholder. | |
522 static base::Value* ToValue(int process_type); | |
523 | |
524 // For a given accumulated array of results, use the comparator to sort and | 632 // For a given accumulated array of results, use the comparator to sort and |
525 // subtotal, writing the results to the output. | 633 // subtotal, writing the results to the output. |
526 static void WriteHTMLTotalAndSubtotals( | 634 static void WriteHTMLTotalAndSubtotals( |
527 const DataCollector::Collection& match_array, | 635 const DataCollector::Collection& match_array, |
528 const Comparator& comparator, std::string* output); | 636 const Comparator& comparator, std::string* output); |
529 | 637 |
530 // Find (or create) a place to count births from the given location in this | 638 // Constructs a DictionaryValue instance containing all recursive results in |
| 639 // our process. The caller assumes ownership of the memory in the returned |
| 640 // instance. |
| 641 static base::DictionaryValue* ToValue(); |
| 642 |
| 643 // Finds (or creates) a place to count births from the given location in this |
531 // thread, and increment that tally. | 644 // thread, and increment that tally. |
532 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. | 645 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. |
533 static Births* TallyABirthIfActive(const Location& location); | 646 static Births* TallyABirthIfActive(const Location& location); |
534 | 647 |
| 648 // Records the end of a timed run of an object. The |completed_task| contains |
| 649 // a pointer to a Births, the time_posted, and a delayed_start_time if any. |
| 650 // The |start_of_run| indicates when we started to perform the run of the |
| 651 // task. The delayed_start_time is non-null for tasks that were posted as |
| 652 // delayed tasks, and it indicates when the task should have run (i.e., when |
| 653 // it should have posted out of the timer queue, and into the work queue. |
| 654 // The |end_of_run| was just obtained by a call to Now() (just after the task |
| 655 // finished). It is provided as an argument to help with testing. |
| 656 static void TallyRunOnNamedThreadIfTracking( |
| 657 const base::TrackingInfo& completed_task, |
| 658 const TrackedTime& start_of_run, |
| 659 const TrackedTime& end_of_run); |
| 660 |
535 // Record the end of a timed run of an object. The |birth| is the record for | 661 // Record the end of a timed run of an object. The |birth| is the record for |
536 // the instance, the |time_posted| and |start_of_run| are times of posting | 662 // the instance, the |time_posted| records that instant, which is presumed to |
537 // into a message loop queue, and of starting to perform the run of the task. | 663 // be when the task was posted into a queue to run on a worker thread. |
| 664 // The |start_of_run| is when the worker thread started to perform the run of |
| 665 // the task. |
538 // The |end_of_run| was just obtained by a call to Now() (just after the task | 666 // The |end_of_run| was just obtained by a call to Now() (just after the task |
539 // finished). | 667 // finished). |
540 static void TallyADeathIfActive(const Births* birth, | 668 static void TallyRunOnWorkerThreadIfTracking( |
541 const base::TimeTicks& time_posted, | 669 const Births* birth, |
542 const base::TimeTicks& delayed_start_time, | 670 const TrackedTime& time_posted, |
543 const base::TimeTicks& start_of_run, | 671 const TrackedTime& start_of_run, |
544 const base::TimeTicks& end_of_run); | 672 const TrackedTime& end_of_run); |
545 | 673 |
546 const std::string thread_name() const { return thread_name_; } | 674 const std::string thread_name() const { return thread_name_; } |
547 | 675 |
548 // --------------------- | 676 // --------------------- |
549 // The following functions should all be private, and are only public because | 677 // The following functions should all be private, and are only public because |
550 // the collection is done externally. We need to relocate that code from the | 678 // the collection is done externally. We need to relocate that code from the |
551 // collection class into this class, and then all these methods can be made | 679 // collection class into this class, and then all these methods can be made |
552 // private. | 680 // private. |
553 // (Thread safe) Get start of list of all ThreadData instances. | 681 // (Thread safe) Get start of list of all ThreadData instances. |
554 static ThreadData* first(); | 682 static ThreadData* first(); |
555 // Iterate through the null terminated list of ThreadData instances. | 683 // Iterate through the null terminated list of ThreadData instances. |
556 ThreadData* next() const { return next_; } | 684 ThreadData* next() const { return next_; } |
557 // Using our lock, make a copy of the specified maps. These calls may arrive | 685 // Using our lock, make a copy of the specified maps. These calls may arrive |
558 // from non-local threads, and are used to quickly scan data from all threads | 686 // from non-local threads, and are used to quickly scan data from all threads |
559 // in order to build an HTML page for about:tracking. | 687 // in order to build an HTML page for about:tracking. |
560 void SnapshotBirthMap(BirthMap *output) const; | 688 void SnapshotBirthMap(BirthMap *output) const; |
561 void SnapshotDeathMap(DeathMap *output) const; | 689 void SnapshotDeathMap(DeathMap *output) const; |
562 // -------- end of should be private methods. | 690 // -------- end of should be private methods. |
563 | 691 |
564 // Hack: asynchronously clear all birth counts and death tallies data values | 692 // Hack: asynchronously clear all birth counts and death tallies data values |
565 // in all ThreadData instances. The numerical (zeroing) part is done without | 693 // in all ThreadData instances. The numerical (zeroing) part is done without |
566 // use of a locks or atomics exchanges, and may (for int64 values) produce | 694 // use of a locks or atomics exchanges, and may (for int64 values) produce |
567 // bogus counts VERY rarely. | 695 // bogus counts VERY rarely. |
568 static void ResetAllThreadData(); | 696 static void ResetAllThreadData(); |
569 | 697 |
570 // Set internal status_ to either become ACTIVE, or later, to be SHUTDOWN, | 698 // Initializes all statics if needed (this initialization call should be made |
| 699 // while we are single threaded). Returns false if unable to initialize. |
| 700 static bool Initialize(); |
| 701 |
| 702 // Sets internal status_ to either become ACTIVE, or DEACTIVATED, |
571 // based on argument being true or false respectively. | 703 // based on argument being true or false respectively. |
572 // IF tracking is not compiled in, this function will return false. | 704 // If tracking is not compiled in, this function will return false. |
573 static bool StartTracking(bool status); | 705 static bool InitializeAndSetTrackingStatus(bool status); |
574 static bool IsActive(); | 706 static bool tracking_status(); |
575 | 707 |
576 // Provide a time function that does nothing (runs fast) when we don't have | 708 // Provide a time function that does nothing (runs fast) when we don't have |
577 // the profiler enabled. It will generally be optimized away when it is | 709 // the profiler enabled. It will generally be optimized away when it is |
578 // ifdef'ed to be small enough (allowing the profiler to be "compiled out" of | 710 // ifdef'ed to be small enough (allowing the profiler to be "compiled out" of |
579 // the code). | 711 // the code). |
580 static base::TimeTicks Now(); | 712 static TrackedTime Now(); |
581 | 713 |
582 // WARNING: ONLY call this function when you are running single threaded | 714 // Cleans up data structures, and returns statics to near pristine (mostly |
583 // (again) and all message loops and threads have terminated. Until that | 715 // uninitialized) state. If there is any chance that other threads are still |
584 // point some threads may still attempt to write into our data structures. | 716 // using the data structures, then the |leak| argument should be passed in as |
585 // Delete recursively all data structures, starting with the list of | 717 // true, and the data structures (birth maps, death maps, ThreadData |
| 718 // insntances, etc.) will be leaked and not deleted. If you have joined all |
| 719 // threads since the time that InitializeAndSetTrackingStatus() was called, |
| 720 // then you can pass in a |leak| value of false, and this function will |
| 721 // delete recursively all data structures, starting with the list of |
586 // ThreadData instances. | 722 // ThreadData instances. |
587 static void ShutdownSingleThreadedCleanup(); | 723 static void ShutdownSingleThreadedCleanup(bool leak); |
588 | 724 |
589 private: | 725 private: |
590 // Current allowable states of the tracking system. The states always | |
591 // proceed towards SHUTDOWN, and never go backwards. | |
592 enum Status { | |
593 UNINITIALIZED, | |
594 ACTIVE, | |
595 SHUTDOWN, | |
596 }; | |
597 | |
598 typedef std::stack<const ThreadData*> ThreadDataPool; | 726 typedef std::stack<const ThreadData*> ThreadDataPool; |
599 | 727 |
600 // Worker thread construction creates a name since there is none. | 728 // Worker thread construction creates a name since there is none. |
601 ThreadData(); | 729 ThreadData(); |
602 // Message loop based construction should provide a name. | 730 // Message loop based construction should provide a name. |
603 explicit ThreadData(const std::string& suggested_name); | 731 explicit ThreadData(const std::string& suggested_name); |
604 | 732 |
605 ~ThreadData(); | 733 ~ThreadData(); |
606 | 734 |
607 // Push this instance to the head of all_thread_data_list_head_, linking it to | 735 // Push this instance to the head of all_thread_data_list_head_, linking it to |
608 // the previous head. This is performed after each construction, and leaves | 736 // the previous head. This is performed after each construction, and leaves |
609 // the instance permanently on that list. | 737 // the instance permanently on that list. |
610 void PushToHeadOfList(); | 738 void PushToHeadOfList(); |
611 | 739 |
612 // In this thread's data, record a new birth. | 740 // In this thread's data, record a new birth. |
613 Births* TallyABirth(const Location& location); | 741 Births* TallyABirth(const Location& location); |
614 | 742 |
615 // Find a place to record a death on this thread. | 743 // Find a place to record a death on this thread. |
616 void TallyADeath(const Births& birth, | 744 void TallyADeath(const Births& birth, |
617 const base::TimeDelta& queue_duration, | 745 const Duration& queue_duration, |
618 const base::TimeDelta& duration); | 746 const Duration& duration); |
619 | 747 |
620 // Using our lock to protect the iteration, Clear all birth and death data. | 748 // Using our lock to protect the iteration, Clear all birth and death data. |
621 void Reset(); | 749 void Reset(); |
622 | 750 |
623 // This method is called by the TLS system when a thread terminates. | 751 // This method is called by the TLS system when a thread terminates. |
624 // The argument may be NULL if this thread has never tracked a birth or death. | 752 // The argument may be NULL if this thread has never tracked a birth or death. |
625 static void OnThreadTermination(void* thread_data); | 753 static void OnThreadTermination(void* thread_data); |
626 | 754 |
627 // This method should be called when a worker thread terminates, so that we | 755 // This method should be called when a worker thread terminates, so that we |
628 // can save all the thread data into a cache of reusable ThreadData instances. | 756 // can save all the thread data into a cache of reusable ThreadData instances. |
629 void OnThreadTerminationCleanup() const; | 757 void OnThreadTerminationCleanup() const; |
630 | 758 |
631 // We use thread local store to identify which ThreadData to interact with. | 759 // We use thread local store to identify which ThreadData to interact with. |
632 static base::ThreadLocalStorage::Slot tls_index_; | 760 static base::ThreadLocalStorage::Slot tls_index_; |
633 | 761 |
634 // Link to the most recently created instance (starts a null terminated list). | 762 // Link to the most recently created instance (starts a null terminated list). |
635 // The list is traversed by about:tracking when it needs to snapshot data. | 763 // The list is traversed by about:tracking when it needs to snapshot data. |
| 764 // This is only accessed while list_lock_ is held. |
636 static ThreadData* all_thread_data_list_head_; | 765 static ThreadData* all_thread_data_list_head_; |
637 // Set of ThreadData instances for use with worker threads. When a worker | 766 // Set of ThreadData instances for use with worker threads. When a worker |
638 // thread is done (terminating), we push it into this pool. When a new worker | 767 // thread is done (terminating), we push it into this pool. When a new worker |
639 // thread is created, we first try to re-use a ThreadData instance from the | 768 // thread is created, we first try to re-use a ThreadData instance from the |
640 // pool, and if none are available, construct a new one. | 769 // pool, and if none are available, construct a new one. |
| 770 // This is only accessed while list_lock_ is held. |
641 static ThreadDataPool* unregistered_thread_data_pool_; | 771 static ThreadDataPool* unregistered_thread_data_pool_; |
| 772 // The next available thread number. This should only be accessed when the |
| 773 // list_lock_ is held. |
| 774 static int thread_number_counter_; |
| 775 // Incarnation sequence number, indicating how many times (during unittests) |
| 776 // we've either transitioned out of UNINITIALIZED, or into that state. This |
| 777 // value is only accessed while the list_lock_ is held. |
| 778 static int incarnation_counter_; |
642 // Protection for access to all_thread_data_list_head_, and to | 779 // Protection for access to all_thread_data_list_head_, and to |
643 // unregistered_thread_data_pool_. | 780 // unregistered_thread_data_pool_. This lock is leaked at shutdown. |
644 static base::Lock list_lock_; | 781 static base::Lock* list_lock_; |
| 782 |
| 783 // Record of what the incarnation_counter_ was when this instance was created. |
| 784 // If the incarnation_counter_ has changed, then we avoid pushing into the |
| 785 // pool (this is only critical in tests which go through multiple |
| 786 // incarations). |
| 787 int incarnation_count_for_pool_; |
645 | 788 |
646 // We set status_ to SHUTDOWN when we shut down the tracking service. | 789 // We set status_ to SHUTDOWN when we shut down the tracking service. |
647 static Status status_; | 790 static Status status_; |
648 | 791 |
649 // Link to next instance (null terminated list). Used to globally track all | 792 // Link to next instance (null terminated list). Used to globally track all |
650 // registered instances (corresponds to all registered threads where we keep | 793 // registered instances (corresponds to all registered threads where we keep |
651 // data). | 794 // data). |
652 ThreadData* next_; | 795 ThreadData* next_; |
653 | 796 |
654 // The name of the thread that is being recorded. If this thread has no | 797 // The name of the thread that is being recorded. If this thread has no |
(...skipping 17 matching lines...) Expand all Loading... |
672 DeathMap death_map_; | 815 DeathMap death_map_; |
673 | 816 |
674 // Lock to protect *some* access to BirthMap and DeathMap. The maps are | 817 // Lock to protect *some* access to BirthMap and DeathMap. The maps are |
675 // regularly read and written on this thread, but may only be read from other | 818 // regularly read and written on this thread, but may only be read from other |
676 // threads. To support this, we acquire this lock if we are writing from this | 819 // threads. To support this, we acquire this lock if we are writing from this |
677 // thread, or reading from another thread. For reading from this thread we | 820 // thread, or reading from another thread. For reading from this thread we |
678 // don't need a lock, as there is no potential for a conflict since the | 821 // don't need a lock, as there is no potential for a conflict since the |
679 // writing is only done from this thread. | 822 // writing is only done from this thread. |
680 mutable base::Lock lock_; | 823 mutable base::Lock lock_; |
681 | 824 |
682 // The next available thread number. This should only be accessed when the | |
683 // list_lock_ is held. | |
684 static int thread_number_counter_; | |
685 | |
686 DISALLOW_COPY_AND_ASSIGN(ThreadData); | 825 DISALLOW_COPY_AND_ASSIGN(ThreadData); |
687 }; | 826 }; |
688 | 827 |
689 //------------------------------------------------------------------------------ | 828 //------------------------------------------------------------------------------ |
690 // Provide simple way to to start global tracking, and to tear down tracking | 829 // Provide simple way to to start global tracking, and to tear down tracking |
691 // when done. The design has evolved to *not* do any teardown (and just leak | 830 // when done. The design has evolved to *not* do any teardown (and just leak |
692 // all allocated data structures). As a result, we don't have any code in this | 831 // all allocated data structures). As a result, we don't have any code in this |
693 // destructor, and perhaps this whole class should go away. | 832 // destructor, and perhaps this whole class should go away. |
694 | 833 |
695 class BASE_EXPORT AutoTracking { | 834 class BASE_EXPORT AutoTracking { |
696 public: | 835 public: |
697 AutoTracking() { | 836 AutoTracking() { |
698 if (state_ != kNeverBeenRun) | 837 ThreadData::Initialize(); |
699 return; | |
700 ThreadData::StartTracking(true); | |
701 state_ = kRunning; | |
702 } | 838 } |
703 | 839 |
704 ~AutoTracking() { | 840 ~AutoTracking() { |
| 841 // TODO(jar): Consider emitting a CSV dump of the data at this point. This |
| 842 // should be called after the message loops have all terminated (or at least |
| 843 // the main message loop is gone), so there is little chance for additional |
| 844 // tasks to be Run. |
705 } | 845 } |
706 | 846 |
707 private: | 847 private: |
708 enum State { | |
709 kNeverBeenRun, | |
710 kRunning, | |
711 }; | |
712 static State state_; | |
713 | 848 |
714 DISALLOW_COPY_AND_ASSIGN(AutoTracking); | 849 DISALLOW_COPY_AND_ASSIGN(AutoTracking); |
715 }; | 850 }; |
716 | 851 |
717 } // namespace tracked_objects | 852 } // namespace tracked_objects |
718 | 853 |
719 #endif // BASE_TRACKED_OBJECTS_H_ | 854 #endif // BASE_TRACKED_OBJECTS_H_ |
OLD | NEW |