OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // Activity tracking provides a low-overhead method of collecting information | 5 // Activity tracking provides a low-overhead method of collecting information |
6 // about the state of the application for analysis both while it is running | 6 // about the state of the application for analysis both while it is running |
7 // and after it has terminated unexpectedly. Its primary purpose is to help | 7 // and after it has terminated unexpectedly. Its primary purpose is to help |
8 // locate reasons the browser becomes unresponsive by providing insight into | 8 // locate reasons the browser becomes unresponsive by providing insight into |
9 // what all the various threads and processes are (or were) doing. | 9 // what all the various threads and processes are (or were) doing. |
10 | 10 |
11 #ifndef BASE_DEBUG_ACTIVITY_TRACKER_H_ | 11 #ifndef BASE_DEBUG_ACTIVITY_TRACKER_H_ |
12 #define BASE_DEBUG_ACTIVITY_TRACKER_H_ | 12 #define BASE_DEBUG_ACTIVITY_TRACKER_H_ |
13 | 13 |
14 // std::atomic is undesired due to performance issues when used as global | 14 // std::atomic is undesired due to performance issues when used as global |
15 // variables. There are no such instances here. This module uses the | 15 // variables. There are no such instances here. This module uses the |
16 // PersistentMemoryAllocator which also uses std::atomic and is written | 16 // PersistentMemoryAllocator which also uses std::atomic and is written |
17 // by the same author. | 17 // by the same author. |
18 #include <atomic> | 18 #include <atomic> |
19 #include <memory> | 19 #include <memory> |
20 #include <string> | 20 #include <string> |
21 #include <vector> | 21 #include <vector> |
22 | 22 |
23 #include "base/base_export.h" | 23 #include "base/base_export.h" |
24 #include "base/location.h" | 24 #include "base/location.h" |
25 #include "base/metrics/persistent_memory_allocator.h" | 25 #include "base/metrics/persistent_memory_allocator.h" |
| 26 #include "base/threading/platform_thread.h" |
26 #include "base/threading/thread_checker.h" | 27 #include "base/threading/thread_checker.h" |
27 #include "base/threading/thread_local_storage.h" | 28 #include "base/threading/thread_local_storage.h" |
28 | 29 |
29 namespace base { | 30 namespace base { |
30 | 31 |
31 struct PendingTask; | 32 struct PendingTask; |
32 | 33 |
33 class FilePath; | 34 class FilePath; |
34 class Lock; | 35 class Lock; |
35 class MemoryMappedFile; | 36 class MemoryMappedFile; |
36 class PlatformThreadHandle; | 37 class PlatformThreadHandle; |
37 class Process; | 38 class Process; |
38 class WaitableEvent; | 39 class WaitableEvent; |
39 | 40 |
40 namespace debug { | 41 namespace debug { |
41 | 42 |
42 class ThreadActivityTracker; | 43 class ThreadActivityTracker; |
43 | 44 |
| 45 |
44 enum : int { | 46 enum : int { |
45 // The maximum number of call-stack addresses stored per activity. This | 47 // The maximum number of call-stack addresses stored per activity. This |
46 // cannot be changed without also changing the version number of the | 48 // cannot be changed without also changing the version number of the |
47 // structure. See kTypeIdActivityTracker in GlobalActivityTracker. | 49 // structure. See kTypeIdActivityTracker in GlobalActivityTracker. |
48 kActivityCallStackSize = 10, | 50 kActivityCallStackSize = 10, |
49 }; | 51 }; |
50 | 52 |
51 // The data associated with an activity is dependent upon the activity type. | 53 // The data associated with an activity is dependent upon the activity type. |
52 // This union defines all of the various fields. All fields must be explicitly | 54 // This union defines all of the various fields. All fields must be explicitly |
53 // sized types to ensure no interoperability problems between 32-bit and | 55 // sized types to ensure no interoperability problems between 32-bit and |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 base::ThreadChecker thread_checker_; | 337 base::ThreadChecker thread_checker_; |
336 | 338 |
337 DISALLOW_COPY_AND_ASSIGN(ThreadActivityTracker); | 339 DISALLOW_COPY_AND_ASSIGN(ThreadActivityTracker); |
338 }; | 340 }; |
339 | 341 |
340 | 342 |
341 // The global tracker manages all the individual thread trackers. Memory for | 343 // The global tracker manages all the individual thread trackers. Memory for |
342 // the thread trackers is taken from a PersistentMemoryAllocator which allows | 344 // the thread trackers is taken from a PersistentMemoryAllocator which allows |
343 // for the data to be analyzed by a parallel process or even post-mortem. | 345 // for the data to be analyzed by a parallel process or even post-mortem. |
344 class BASE_EXPORT GlobalActivityTracker { | 346 class BASE_EXPORT GlobalActivityTracker { |
| 347 template <typename T> |
| 348 class ThreadSafeStack { |
| 349 public: |
| 350 ThreadSafeStack(size_t size) |
| 351 : size_(size), values_(new T[size]), used_(0) {} |
| 352 ~ThreadSafeStack() {} |
| 353 |
| 354 size_t size() { return size_; } |
| 355 size_t used() { |
| 356 base::AutoLock autolock(lock_); |
| 357 return used_; |
| 358 } |
| 359 |
| 360 bool push(T value) { |
| 361 base::AutoLock autolock(lock_); |
| 362 if (used_ == size_) |
| 363 return false; |
| 364 values_[used_++] = value; |
| 365 return true; |
| 366 } |
| 367 |
| 368 bool pop(T* out_value) { |
| 369 base::AutoLock autolock(lock_); |
| 370 if (used_ == 0) |
| 371 return false; |
| 372 *out_value = values_[--used_]; |
| 373 return true; |
| 374 } |
| 375 |
| 376 private: |
| 377 const size_t size_; |
| 378 |
| 379 std::unique_ptr<T[]> values_; |
| 380 size_t used_; |
| 381 base::Lock lock_; |
| 382 |
| 383 private: |
| 384 DISALLOW_COPY_AND_ASSIGN(ThreadSafeStack); |
| 385 }; |
| 386 |
345 public: | 387 public: |
346 // Type identifiers used when storing in persistent memory so they can be | 388 // Type identifiers used when storing in persistent memory so they can be |
347 // identified during extraction; the first 4 bytes of the SHA1 of the name | 389 // identified during extraction; the first 4 bytes of the SHA1 of the name |
348 // is used as a unique integer. A "version number" is added to the base | 390 // is used as a unique integer. A "version number" is added to the base |
349 // so that, if the structure of that object changes, stored older versions | 391 // so that, if the structure of that object changes, stored older versions |
350 // will be safely ignored. These are public so that an external process | 392 // will be safely ignored. These are public so that an external process |
351 // can recognize records of this type within an allocator. | 393 // can recognize records of this type within an allocator. |
352 enum : uint32_t { | 394 enum : uint32_t { |
353 kTypeIdActivityTracker = 0x5D7381AF + 1, // SHA1(ActivityTracker) v1 | 395 kTypeIdActivityTracker = 0x5D7381AF + 1, // SHA1(ActivityTracker) v1 |
354 kTypeIdActivityTrackerFree = 0x3F0272FB + 1, // SHA1(ActivityTrackerFree) | 396 kTypeIdActivityTrackerFree = 0x3F0272FB + 1, // SHA1(ActivityTrackerFree) |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 // is taken. | 540 // is taken. |
499 std::unique_ptr<PersistentMemoryAllocator> allocator_; | 541 std::unique_ptr<PersistentMemoryAllocator> allocator_; |
500 | 542 |
501 // The size (in bytes) of memory required by a ThreadActivityTracker to | 543 // The size (in bytes) of memory required by a ThreadActivityTracker to |
502 // provide the stack-depth requested during construction. | 544 // provide the stack-depth requested during construction. |
503 const size_t stack_memory_size_; | 545 const size_t stack_memory_size_; |
504 | 546 |
505 // The activity tracker for the currently executing thread. | 547 // The activity tracker for the currently executing thread. |
506 base::ThreadLocalStorage::Slot this_thread_tracker_; | 548 base::ThreadLocalStorage::Slot this_thread_tracker_; |
507 | 549 |
508 // These have to be lock-free because lock activity is tracked and causes | 550 // The number of thread trackers currently active. |
509 // re-entry problems. | |
510 std::atomic<int> thread_tracker_count_; | 551 std::atomic<int> thread_tracker_count_; |
511 std::atomic<int> available_memories_count_; | 552 |
512 std::atomic<PersistentMemoryAllocator::Reference> | 553 // A cache of thread-tracker memories that have been previously freed and |
513 available_memories_[kMaxThreadCount]; | 554 // thus can be re-used instead of allocating new ones. |
| 555 ThreadSafeStack<PersistentMemoryAllocator::Reference> available_memories_; |
514 | 556 |
515 // The active global activity tracker. | 557 // The active global activity tracker. |
516 static GlobalActivityTracker* g_tracker_; | 558 static GlobalActivityTracker* g_tracker_; |
517 | 559 |
518 DISALLOW_COPY_AND_ASSIGN(GlobalActivityTracker); | 560 DISALLOW_COPY_AND_ASSIGN(GlobalActivityTracker); |
519 }; | 561 }; |
520 | 562 |
521 | 563 |
522 // Record entry in to and out of an arbitrary block of code. | 564 // Record entry in to and out of an arbitrary block of code. |
523 class BASE_EXPORT ScopedActivity | 565 class BASE_EXPORT ScopedActivity |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 explicit ScopedProcessWaitActivity(const base::Process* process); | 649 explicit ScopedProcessWaitActivity(const base::Process* process); |
608 private: | 650 private: |
609 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity); | 651 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity); |
610 }; | 652 }; |
611 #endif | 653 #endif |
612 | 654 |
613 } // namespace debug | 655 } // namespace debug |
614 } // namespace base | 656 } // namespace base |
615 | 657 |
616 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_ | 658 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_ |
OLD | NEW |