OLD | NEW |
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 #ifndef BASE_TRACKED_OBJECTS_H_ | 5 #ifndef BASE_TRACKED_OBJECTS_H_ |
6 #define BASE_TRACKED_OBJECTS_H_ | 6 #define BASE_TRACKED_OBJECTS_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <stack> | 10 #include <stack> |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 233 |
234 class BASE_EXPORT Births: public BirthOnThread { | 234 class BASE_EXPORT Births: public BirthOnThread { |
235 public: | 235 public: |
236 Births(const Location& location, const ThreadData& current); | 236 Births(const Location& location, const ThreadData& current); |
237 | 237 |
238 int birth_count() const; | 238 int birth_count() const; |
239 | 239 |
240 // When we have a birth we update the count for this birthplace. | 240 // When we have a birth we update the count for this birthplace. |
241 void RecordBirth(); | 241 void RecordBirth(); |
242 | 242 |
243 // When a birthplace is changed (updated), we need to decrement the counter | |
244 // for the old instance. | |
245 void ForgetBirth(); | |
246 | |
247 // Hack to quickly reset all counts to zero. | |
248 void Clear(); | |
249 | |
250 private: | 243 private: |
251 // The number of births on this thread for our location_. | 244 // The number of births on this thread for our location_. |
252 int birth_count_; | 245 int birth_count_; |
253 | 246 |
254 DISALLOW_COPY_AND_ASSIGN(Births); | 247 DISALLOW_COPY_AND_ASSIGN(Births); |
255 }; | 248 }; |
256 | 249 |
257 //------------------------------------------------------------------------------ | 250 //------------------------------------------------------------------------------ |
258 // Basic info summarizing multiple destructions of a tracked object with a | 251 // Basic info summarizing multiple destructions of a tracked object with a |
259 // single birthplace (fixed Location). Used both on specific threads, and also | 252 // single birthplace (fixed Location). Used both on specific threads, and also |
(...skipping 17 matching lines...) Expand all Loading... |
277 | 270 |
278 // Metrics accessors, used only for serialization and in tests. | 271 // Metrics accessors, used only for serialization and in tests. |
279 int count() const; | 272 int count() const; |
280 int32 run_duration_sum() const; | 273 int32 run_duration_sum() const; |
281 int32 run_duration_max() const; | 274 int32 run_duration_max() const; |
282 int32 run_duration_sample() const; | 275 int32 run_duration_sample() const; |
283 int32 queue_duration_sum() const; | 276 int32 queue_duration_sum() const; |
284 int32 queue_duration_max() const; | 277 int32 queue_duration_max() const; |
285 int32 queue_duration_sample() const; | 278 int32 queue_duration_sample() const; |
286 | 279 |
287 // Reset the max values to zero. | |
288 void ResetMax(); | |
289 | |
290 // Reset all tallies to zero. This is used as a hack on realtime data. | 280 // Reset all tallies to zero. This is used as a hack on realtime data. |
291 void Clear(); | 281 void Clear(); |
292 | 282 |
293 private: | 283 private: |
294 // Members are ordered from most regularly read and updated, to least | 284 // Members are ordered from most regularly read and updated, to least |
295 // frequently used. This might help a bit with cache lines. | 285 // frequently used. This might help a bit with cache lines. |
296 // Number of runs seen (divisor for calculating averages). | 286 // Number of runs seen (divisor for calculating averages). |
297 int count_; | 287 int count_; |
298 // Basic tallies, used to compute averages. | 288 // Basic tallies, used to compute averages. |
299 int32 run_duration_sum_; | 289 int32 run_duration_sum_; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 // only used by the message loop, which has a well defined thread name. | 369 // only used by the message loop, which has a well defined thread name. |
380 static void InitializeThreadContext(const std::string& suggested_name); | 370 static void InitializeThreadContext(const std::string& suggested_name); |
381 | 371 |
382 // Using Thread Local Store, find the current instance for collecting data. | 372 // Using Thread Local Store, find the current instance for collecting data. |
383 // If an instance does not exist, construct one (and remember it for use on | 373 // If an instance does not exist, construct one (and remember it for use on |
384 // this thread. | 374 // this thread. |
385 // This may return NULL if the system is disabled for any reason. | 375 // This may return NULL if the system is disabled for any reason. |
386 static ThreadData* Get(); | 376 static ThreadData* Get(); |
387 | 377 |
388 // Fills |process_data| with all the recursive results in our process. | 378 // Fills |process_data| with all the recursive results in our process. |
389 // During the scavenging, if |reset_max| is true, then the DeathData instances | 379 static void Snapshot(ProcessDataSnapshot* process_data); |
390 // max-values are reset to zero during this scan. | |
391 static void Snapshot(bool reset_max, ProcessDataSnapshot* process_data); | |
392 | 380 |
393 // Finds (or creates) a place to count births from the given location in this | 381 // Finds (or creates) a place to count births from the given location in this |
394 // thread, and increment that tally. | 382 // thread, and increment that tally. |
395 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. | 383 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. |
396 static Births* TallyABirthIfActive(const Location& location); | 384 static Births* TallyABirthIfActive(const Location& location); |
397 | 385 |
398 // Records the end of a timed run of an object. The |completed_task| contains | 386 // Records the end of a timed run of an object. The |completed_task| contains |
399 // a pointer to a Births, the time_posted, and a delayed_start_time if any. | 387 // a pointer to a Births, the time_posted, and a delayed_start_time if any. |
400 // The |start_of_run| indicates when we started to perform the run of the | 388 // The |start_of_run| indicates when we started to perform the run of the |
401 // task. The delayed_start_time is non-null for tasks that were posted as | 389 // task. The delayed_start_time is non-null for tasks that were posted as |
(...skipping 18 matching lines...) Expand all Loading... |
420 const TaskStopwatch& stopwatch); | 408 const TaskStopwatch& stopwatch); |
421 | 409 |
422 // Record the end of execution in region, generally corresponding to a scope | 410 // Record the end of execution in region, generally corresponding to a scope |
423 // being exited. | 411 // being exited. |
424 static void TallyRunInAScopedRegionIfTracking( | 412 static void TallyRunInAScopedRegionIfTracking( |
425 const Births* birth, | 413 const Births* birth, |
426 const TaskStopwatch& stopwatch); | 414 const TaskStopwatch& stopwatch); |
427 | 415 |
428 const std::string& thread_name() const { return thread_name_; } | 416 const std::string& thread_name() const { return thread_name_; } |
429 | 417 |
430 // Hack: asynchronously clear all birth counts and death tallies data values | |
431 // in all ThreadData instances. The numerical (zeroing) part is done without | |
432 // use of a locks or atomics exchanges, and may (for int64 values) produce | |
433 // bogus counts VERY rarely. | |
434 static void ResetAllThreadData(); | |
435 | |
436 // Initializes all statics if needed (this initialization call should be made | 418 // Initializes all statics if needed (this initialization call should be made |
437 // while we are single threaded). Returns false if unable to initialize. | 419 // while we are single threaded). Returns false if unable to initialize. |
438 static bool Initialize(); | 420 static bool Initialize(); |
439 | 421 |
440 // Sets internal status_. | 422 // Sets internal status_. |
441 // If |status| is false, then status_ is set to DEACTIVATED. | 423 // If |status| is false, then status_ is set to DEACTIVATED. |
442 // If |status| is true, then status_ is set to, PROFILING_ACTIVE, or | 424 // If |status| is true, then status_ is set to, PROFILING_ACTIVE, or |
443 // PROFILING_CHILDREN_ACTIVE. | 425 // PROFILING_CHILDREN_ACTIVE. |
444 // If tracking is not compiled in, this function will return false. | 426 // If tracking is not compiled in, this function will return false. |
445 // If parent-child tracking is not compiled in, then an attempt to set the | 427 // If parent-child tracking is not compiled in, then an attempt to set the |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 // In this thread's data, record a new birth. | 505 // In this thread's data, record a new birth. |
524 Births* TallyABirth(const Location& location); | 506 Births* TallyABirth(const Location& location); |
525 | 507 |
526 // Find a place to record a death on this thread. | 508 // Find a place to record a death on this thread. |
527 void TallyADeath(const Births& birth, | 509 void TallyADeath(const Births& birth, |
528 int32 queue_duration, | 510 int32 queue_duration, |
529 const TaskStopwatch& stopwatch); | 511 const TaskStopwatch& stopwatch); |
530 | 512 |
531 // Snapshot (under a lock) the profiled data for the tasks in each ThreadData | 513 // Snapshot (under a lock) the profiled data for the tasks in each ThreadData |
532 // instance. Also updates the |birth_counts| tally for each task to keep | 514 // instance. Also updates the |birth_counts| tally for each task to keep |
533 // track of the number of living instances of the task. If |reset_max| is | 515 // track of the number of living instances of the task. |
534 // true, then the max values in each DeathData instance are reset during the | 516 static void SnapshotAllExecutedTasks(ProcessDataSnapshot* process_data, |
535 // scan. | |
536 static void SnapshotAllExecutedTasks(bool reset_max, | |
537 ProcessDataSnapshot* process_data, | |
538 BirthCountMap* birth_counts); | 517 BirthCountMap* birth_counts); |
539 | 518 |
540 // Snapshots (under a lock) the profiled data for the tasks for this thread | 519 // Snapshots (under a lock) the profiled data for the tasks for this thread |
541 // and writes all of the executed tasks' data -- i.e. the data for the tasks | 520 // and writes all of the executed tasks' data -- i.e. the data for the tasks |
542 // with with entries in the death_map_ -- into |process_data|. Also updates | 521 // with with entries in the death_map_ -- into |process_data|. Also updates |
543 // the |birth_counts| tally for each task to keep track of the number of | 522 // the |birth_counts| tally for each task to keep track of the number of |
544 // living instances of the task -- that is, each task maps to the number of | 523 // living instances of the task -- that is, each task maps to the number of |
545 // births for the task that have not yet been balanced by a death. If | 524 // births for the task that have not yet been balanced by a death. |
546 // |reset_max| is true, then the max values in each DeathData instance are | 525 void SnapshotExecutedTasks(ProcessDataSnapshot* process_data, |
547 // reset during the scan. | |
548 void SnapshotExecutedTasks(bool reset_max, | |
549 ProcessDataSnapshot* process_data, | |
550 BirthCountMap* birth_counts); | 526 BirthCountMap* birth_counts); |
551 | 527 |
552 // Using our lock, make a copy of the specified maps. This call may be made | 528 // Using our lock, make a copy of the specified maps. This call may be made |
553 // on non-local threads, which necessitate the use of the lock to prevent | 529 // on non-local threads, which necessitate the use of the lock to prevent |
554 // the map(s) from being reallocated while they are copied. If |reset_max| is | 530 // the map(s) from being reallocated while they are copied. |
555 // true, then, just after we copy the DeathMap, we will set the max values to | 531 void SnapshotMaps(BirthMap* birth_map, |
556 // zero in the active DeathMap (not the snapshot). | |
557 void SnapshotMaps(bool reset_max, | |
558 BirthMap* birth_map, | |
559 DeathMap* death_map, | 532 DeathMap* death_map, |
560 ParentChildSet* parent_child_set); | 533 ParentChildSet* parent_child_set); |
561 | 534 |
562 // Using our lock to protect the iteration, Clear all birth and death data. | |
563 void Reset(); | |
564 | |
565 // This method is called by the TLS system when a thread terminates. | 535 // This method is called by the TLS system when a thread terminates. |
566 // The argument may be NULL if this thread has never tracked a birth or death. | 536 // The argument may be NULL if this thread has never tracked a birth or death. |
567 static void OnThreadTermination(void* thread_data); | 537 static void OnThreadTermination(void* thread_data); |
568 | 538 |
569 // This method should be called when a worker thread terminates, so that we | 539 // This method should be called when a worker thread terminates, so that we |
570 // can save all the thread data into a cache of reusable ThreadData instances. | 540 // can save all the thread data into a cache of reusable ThreadData instances. |
571 void OnThreadTerminationCleanup(); | 541 void OnThreadTerminationCleanup(); |
572 | 542 |
573 // Cleans up data structures, and returns statics to near pristine (mostly | 543 // Cleans up data structures, and returns statics to near pristine (mostly |
574 // uninitialized) state. If there is any chance that other threads are still | 544 // uninitialized) state. If there is any chance that other threads are still |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 ~ProcessDataSnapshot(); | 754 ~ProcessDataSnapshot(); |
785 | 755 |
786 std::vector<TaskSnapshot> tasks; | 756 std::vector<TaskSnapshot> tasks; |
787 std::vector<ParentChildPairSnapshot> descendants; | 757 std::vector<ParentChildPairSnapshot> descendants; |
788 int process_id; | 758 int process_id; |
789 }; | 759 }; |
790 | 760 |
791 } // namespace tracked_objects | 761 } // namespace tracked_objects |
792 | 762 |
793 #endif // BASE_TRACKED_OBJECTS_H_ | 763 #endif // BASE_TRACKED_OBJECTS_H_ |
OLD | NEW |