Chromium Code Reviews| 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 <map> | 19 #include <map> |
| 20 #include <memory> | 20 #include <memory> |
| 21 #include <set> | |
| 21 #include <string> | 22 #include <string> |
| 22 #include <vector> | 23 #include <vector> |
| 23 | 24 |
| 24 #include "base/atomicops.h" | 25 #include "base/atomicops.h" |
| 25 #include "base/base_export.h" | 26 #include "base/base_export.h" |
| 26 #include "base/compiler_specific.h" | 27 #include "base/compiler_specific.h" |
| 27 #include "base/gtest_prod_util.h" | 28 #include "base/gtest_prod_util.h" |
| 28 #include "base/location.h" | 29 #include "base/location.h" |
| 29 #include "base/metrics/persistent_memory_allocator.h" | 30 #include "base/metrics/persistent_memory_allocator.h" |
| 31 #include "base/process/process_handle.h" | |
| 30 #include "base/strings/string_piece.h" | 32 #include "base/strings/string_piece.h" |
| 31 #include "base/strings/utf_string_conversions.h" | 33 #include "base/strings/utf_string_conversions.h" |
| 34 #include "base/task_runner.h" | |
| 32 #include "base/threading/platform_thread.h" | 35 #include "base/threading/platform_thread.h" |
| 33 #include "base/threading/thread_checker.h" | 36 #include "base/threading/thread_checker.h" |
| 34 #include "base/threading/thread_local_storage.h" | 37 #include "base/threading/thread_local_storage.h" |
| 35 | 38 |
| 36 namespace base { | 39 namespace base { |
| 37 | 40 |
| 38 struct PendingTask; | 41 struct PendingTask; |
| 39 | 42 |
| 40 class FilePath; | 43 class FilePath; |
| 41 class Lock; | 44 class Lock; |
| 42 class PlatformThreadHandle; | 45 class PlatformThreadHandle; |
| 43 class Process; | 46 class Process; |
| 44 class StaticAtomicSequenceNumber; | |
| 45 class WaitableEvent; | 47 class WaitableEvent; |
| 46 | 48 |
| 47 namespace debug { | 49 namespace debug { |
| 48 | 50 |
| 49 class ThreadActivityTracker; | 51 class ThreadActivityTracker; |
| 50 | 52 |
| 51 | 53 |
| 52 enum : int { | 54 enum : int { |
| 53 // The maximum number of call-stack addresses stored per activity. This | 55 // The maximum number of call-stack addresses stored per activity. This |
| 54 // cannot be changed without also changing the version number of the | 56 // cannot be changed without also changing the version number of the |
| 55 // structure. See kTypeIdActivityTracker in GlobalActivityTracker. | 57 // structure. See kTypeIdActivityTracker in GlobalActivityTracker. |
| 56 kActivityCallStackSize = 10, | 58 kActivityCallStackSize = 10, |
| 57 }; | 59 }; |
| 58 | 60 |
| 61 struct ProcessInfo { | |
| 62 ProcessInfo(); | |
| 63 ~ProcessInfo(); | |
| 64 | |
| 65 // Initializes structure with the current process id and the current time. | |
| 66 // These can uniquely identify a process. A unique non-zero data_id will be | |
| 67 // set making it possible to tell using atomic reads if the data has changed. | |
| 68 void Release_Initialize(); | |
| 69 | |
| 70 // Explicitly sets the process ID. | |
| 71 void SetOwningProcessIdForTesting(ProcessId pid, int64_t stamp); | |
| 72 | |
| 73 // Gets the associated process ID, in native form, and the creation timestamp | |
| 74 // from tracker memory without loading the entire structure for analysis. This | |
| 75 // will return false if no valid process ID is available. | |
| 76 static bool OwningProcessId(const void* memory, | |
| 77 ProcessId* out_id, | |
| 78 int64_t* out_stamp); | |
| 79 | |
| 80 // SHA1(base::debug::ProcessInfo): Increment this if structure changes! | |
| 81 static constexpr uint32_t kPersistentTypeId = 0xD485C718 + 1; | |
| 82 | |
| 83 // Expected size for 32/64-bit check by PersistentMemoryAllocator. | |
| 84 static constexpr size_t kExpectedInstanceSize = 24; | |
| 85 | |
| 86 std::atomic<uint32_t> data_id; | |
| 87 uint32_t padding; | |
| 88 int64_t process_id; | |
| 89 int64_t create_stamp; | |
| 90 }; | |
| 91 | |
| 59 // The data associated with an activity is dependent upon the activity type. | 92 // The data associated with an activity is dependent upon the activity type. |
| 60 // This union defines all of the various fields. All fields must be explicitly | 93 // This union defines all of the various fields. All fields must be explicitly |
| 61 // sized types to ensure no interoperability problems between 32-bit and | 94 // sized types to ensure no interoperability problems between 32-bit and |
| 62 // 64-bit systems. | 95 // 64-bit systems. |
| 63 union ActivityData { | 96 union ActivityData { |
| 64 // Generic activities don't have any defined structure. | 97 // Generic activities don't have any defined structure. |
| 65 struct { | 98 struct { |
| 66 uint32_t id; // An arbitrary identifier used for association. | 99 uint32_t id; // An arbitrary identifier used for association. |
| 67 int32_t info; // An arbitrary value used for information purposes. | 100 int32_t info; // An arbitrary value used for information purposes. |
| 68 } generic; | 101 } generic; |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 | 375 |
| 343 using Snapshot = std::map<std::string, TypedValue>; | 376 using Snapshot = std::map<std::string, TypedValue>; |
| 344 | 377 |
| 345 ActivityUserData(void* memory, size_t size); | 378 ActivityUserData(void* memory, size_t size); |
| 346 virtual ~ActivityUserData(); | 379 virtual ~ActivityUserData(); |
| 347 | 380 |
| 348 // Gets the unique ID number for this user data. If this changes then the | 381 // Gets the unique ID number for this user data. If this changes then the |
| 349 // contents have been overwritten by another thread. The return value is | 382 // contents have been overwritten by another thread. The return value is |
| 350 // always non-zero unless it's actually just a data "sink". | 383 // always non-zero unless it's actually just a data "sink". |
| 351 uint32_t id() const { | 384 uint32_t id() const { |
| 352 return memory_ ? id_->load(std::memory_order_relaxed) : 0; | 385 return header_ |
| 386 ? header_->process_info.data_id.load(std::memory_order_relaxed) | |
| 387 : 0; | |
| 353 } | 388 } |
| 354 | 389 |
| 355 // Writes a |value| (as part of a key/value pair) that will be included with | 390 // Writes a |value| (as part of a key/value pair) that will be included with |
| 356 // the activity in any reports. The same |name| can be written multiple times | 391 // the activity in any reports. The same |name| can be written multiple times |
| 357 // with each successive call overwriting the previously stored |value|. For | 392 // with each successive call overwriting the previously stored |value|. For |
| 358 // raw and string values, the maximum size of successive writes is limited by | 393 // raw and string values, the maximum size of successive writes is limited by |
| 359 // the first call. The length of "name" is limited to 255 characters. | 394 // the first call. The length of "name" is limited to 255 characters. |
| 360 // | 395 // |
| 361 // This information is stored on a "best effort" basis. It may be dropped if | 396 // This information is stored on a "best effort" basis. It may be dropped if |
| 362 // the memory buffer is full or the associated activity is beyond the maximum | 397 // the memory buffer is full or the associated activity is beyond the maximum |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 // Creates a snapshot of the key/value pairs contained within. The returned | 433 // Creates a snapshot of the key/value pairs contained within. The returned |
| 399 // data will be fixed, independent of whatever changes afterward. There is | 434 // data will be fixed, independent of whatever changes afterward. There is |
| 400 // protection against concurrent modification of the values but no protection | 435 // protection against concurrent modification of the values but no protection |
| 401 // against a complete overwrite of the contents; the caller must ensure that | 436 // against a complete overwrite of the contents; the caller must ensure that |
| 402 // the memory segment is not going to be re-initialized while this runs. | 437 // the memory segment is not going to be re-initialized while this runs. |
| 403 bool CreateSnapshot(Snapshot* output_snapshot) const; | 438 bool CreateSnapshot(Snapshot* output_snapshot) const; |
| 404 | 439 |
| 405 // Gets the base memory address used for storing data. | 440 // Gets the base memory address used for storing data. |
| 406 const void* GetBaseAddress(); | 441 const void* GetBaseAddress(); |
| 407 | 442 |
| 443 // Explicitly sets the process ID. | |
| 444 void SetOwningProcessIdForTesting(ProcessId pid, int64_t stamp); | |
| 445 | |
| 446 // Gets the associated process ID, in native form, and the creation timestamp | |
| 447 // from tracker memory without loading the entire structure for analysis. This | |
| 448 // will return false if no valid process ID is available. | |
| 449 static bool OwningProcessId(const void* memory, | |
| 450 ProcessId* out_id, | |
| 451 int64_t* out_stamp); | |
| 452 | |
| 408 protected: | 453 protected: |
| 409 virtual void Set(StringPiece name, | 454 virtual void Set(StringPiece name, |
| 410 ValueType type, | 455 ValueType type, |
| 411 const void* memory, | 456 const void* memory, |
| 412 size_t size); | 457 size_t size); |
| 413 | 458 |
| 414 private: | 459 private: |
| 415 FRIEND_TEST_ALL_PREFIXES(ActivityTrackerTest, UserDataTest); | 460 FRIEND_TEST_ALL_PREFIXES(ActivityTrackerTest, UserDataTest); |
| 416 | 461 |
| 417 enum : size_t { kMemoryAlignment = sizeof(uint64_t) }; | 462 enum : size_t { kMemoryAlignment = sizeof(uint64_t) }; |
| 418 | 463 |
| 419 // A structure used to reference data held outside of persistent memory. | 464 // A structure used to reference data held outside of persistent memory. |
| 420 struct ReferenceRecord { | 465 struct ReferenceRecord { |
| 421 uint64_t address; | 466 uint64_t address; |
| 422 uint64_t size; | 467 uint64_t size; |
| 423 }; | 468 }; |
| 424 | 469 |
| 470 // A structure that defines the structure header in memory. | |
| 471 struct MemoryHeader { | |
| 472 MemoryHeader(); | |
| 473 ~MemoryHeader(); | |
| 474 | |
| 475 ProcessInfo process_info; // Information about the creating process. | |
| 476 }; | |
| 477 | |
| 425 // Header to a key/value record held in persistent memory. | 478 // Header to a key/value record held in persistent memory. |
| 426 struct Header { | 479 struct FieldHeader { |
| 480 FieldHeader(); | |
| 481 ~FieldHeader(); | |
| 482 | |
| 427 std::atomic<uint8_t> type; // Encoded ValueType | 483 std::atomic<uint8_t> type; // Encoded ValueType |
| 428 uint8_t name_size; // Length of "name" key. | 484 uint8_t name_size; // Length of "name" key. |
| 429 std::atomic<uint16_t> value_size; // Actual size of of the stored value. | 485 std::atomic<uint16_t> value_size; // Actual size of of the stored value. |
| 430 uint16_t record_size; // Total storage of name, value, header. | 486 uint16_t record_size; // Total storage of name, value, header. |
| 431 }; | 487 }; |
| 432 | 488 |
| 433 // This record is used to hold known value is a map so that they can be | 489 // This record is used to hold known value is a map so that they can be |
| 434 // found and overwritten later. | 490 // found and overwritten later. |
| 435 struct ValueInfo { | 491 struct ValueInfo { |
| 436 ValueInfo(); | 492 ValueInfo(); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 457 // updates of the values. This is "mutable" because it changes on "const" | 513 // updates of the values. This is "mutable" because it changes on "const" |
| 458 // objects even when the actual data values can't change. | 514 // objects even when the actual data values can't change. |
| 459 mutable std::map<StringPiece, ValueInfo> values_; | 515 mutable std::map<StringPiece, ValueInfo> values_; |
| 460 | 516 |
| 461 // Information about the memory block in which new data can be stored. These | 517 // Information about the memory block in which new data can be stored. These |
| 462 // are "mutable" because they change even on "const" objects that are just | 518 // are "mutable" because they change even on "const" objects that are just |
| 463 // skipping already set values. | 519 // skipping already set values. |
| 464 mutable char* memory_; | 520 mutable char* memory_; |
| 465 mutable size_t available_; | 521 mutable size_t available_; |
| 466 | 522 |
| 467 // A pointer to the unique ID for this instance. | 523 // A pointer to the memory header for this instance. |
| 468 std::atomic<uint32_t>* const id_; | 524 MemoryHeader* const header_; |
| 469 | |
| 470 // This ID is used to create unique indentifiers for user data so that it's | |
| 471 // possible to tell if the information has been overwritten. | |
| 472 static StaticAtomicSequenceNumber next_id_; | |
| 473 | 525 |
| 474 DISALLOW_COPY_AND_ASSIGN(ActivityUserData); | 526 DISALLOW_COPY_AND_ASSIGN(ActivityUserData); |
| 475 }; | 527 }; |
| 476 | 528 |
| 477 // This class manages tracking a stack of activities for a single thread in | 529 // This class manages tracking a stack of activities for a single thread in |
| 478 // a persistent manner, implementing a bounded-size stack in a fixed-size | 530 // a persistent manner, implementing a bounded-size stack in a fixed-size |
| 479 // memory allocation. In order to support an operational mode where another | 531 // memory allocation. In order to support an operational mode where another |
| 480 // thread is analyzing this data in real-time, atomic operations are used | 532 // thread is analyzing this data in real-time, atomic operations are used |
| 481 // where necessary to guarantee a consistent view from the outside. | 533 // where necessary to guarantee a consistent view from the outside. |
| 482 // | 534 // |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 605 // Returns whether the current data is valid or not. It is not valid if | 657 // Returns whether the current data is valid or not. It is not valid if |
| 606 // corruption has been detected in the header or other data structures. | 658 // corruption has been detected in the header or other data structures. |
| 607 bool IsValid() const; | 659 bool IsValid() const; |
| 608 | 660 |
| 609 // Gets a copy of the tracker contents for analysis. Returns false if a | 661 // Gets a copy of the tracker contents for analysis. Returns false if a |
| 610 // snapshot was not possible, perhaps because the data is not valid; the | 662 // snapshot was not possible, perhaps because the data is not valid; the |
| 611 // contents of |output_snapshot| are undefined in that case. The current | 663 // contents of |output_snapshot| are undefined in that case. The current |
| 612 // implementation does not support concurrent snapshot operations. | 664 // implementation does not support concurrent snapshot operations. |
| 613 bool CreateSnapshot(Snapshot* output_snapshot) const; | 665 bool CreateSnapshot(Snapshot* output_snapshot) const; |
| 614 | 666 |
| 667 // Gets the base memory address used for storing data. | |
| 668 const void* GetBaseAddress(); | |
| 669 | |
| 670 // Explicitly sets the process ID. | |
| 671 void SetOwningProcessIdForTesting(ProcessId pid, int64_t stamp); | |
| 672 | |
| 673 // Gets the associated process ID, in native form, and the creation timestamp | |
| 674 // from tracker memory without loading the entire structure for analysis. This | |
| 675 // will return false if no valid process ID is available. | |
| 676 static bool OwningProcessId(const void* memory, | |
| 677 ProcessId* out_id, | |
| 678 int64_t* out_stamp); | |
| 679 | |
| 615 // Calculates the memory size required for a given stack depth, including | 680 // Calculates the memory size required for a given stack depth, including |
| 616 // the internal header structure for the stack. | 681 // the internal header structure for the stack. |
| 617 static size_t SizeForStackDepth(int stack_depth); | 682 static size_t SizeForStackDepth(int stack_depth); |
| 618 | 683 |
| 619 private: | 684 private: |
| 620 friend class ActivityTrackerTest; | 685 friend class ActivityTrackerTest; |
| 621 | 686 |
| 622 Header* const header_; // Pointer to the Header structure. | 687 Header* const header_; // Pointer to the Header structure. |
| 623 Activity* const stack_; // The stack of activities. | 688 Activity* const stack_; // The stack of activities. |
| 624 const uint32_t stack_slots_; // The total number of stack slots. | 689 const uint32_t stack_slots_; // The total number of stack slots. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 636 // for the data to be analyzed by a parallel process or even post-mortem. | 701 // for the data to be analyzed by a parallel process or even post-mortem. |
| 637 class BASE_EXPORT GlobalActivityTracker { | 702 class BASE_EXPORT GlobalActivityTracker { |
| 638 public: | 703 public: |
| 639 // Type identifiers used when storing in persistent memory so they can be | 704 // Type identifiers used when storing in persistent memory so they can be |
| 640 // identified during extraction; the first 4 bytes of the SHA1 of the name | 705 // identified during extraction; the first 4 bytes of the SHA1 of the name |
| 641 // is used as a unique integer. A "version number" is added to the base | 706 // is used as a unique integer. A "version number" is added to the base |
| 642 // so that, if the structure of that object changes, stored older versions | 707 // so that, if the structure of that object changes, stored older versions |
| 643 // will be safely ignored. These are public so that an external process | 708 // will be safely ignored. These are public so that an external process |
| 644 // can recognize records of this type within an allocator. | 709 // can recognize records of this type within an allocator. |
| 645 enum : uint32_t { | 710 enum : uint32_t { |
| 646 kTypeIdActivityTracker = 0x5D7381AF + 3, // SHA1(ActivityTracker) v3 | 711 kTypeIdActivityTracker = 0x5D7381AF + 4, // SHA1(ActivityTracker) v4 |
| 647 kTypeIdUserDataRecord = 0x615EDDD7 + 2, // SHA1(UserDataRecord) v2 | 712 kTypeIdUserDataRecord = 0x615EDDD7 + 3, // SHA1(UserDataRecord) v3 |
| 648 kTypeIdGlobalLogMessage = 0x4CF434F9 + 1, // SHA1(GlobalLogMessage) v1 | 713 kTypeIdGlobalLogMessage = 0x4CF434F9 + 1, // SHA1(GlobalLogMessage) v1 |
| 649 kTypeIdGlobalDataRecord = kTypeIdUserDataRecord + 1000, | 714 kTypeIdProcessDataRecord = kTypeIdUserDataRecord + 0x100, |
| 715 kTypeIdGlobalDataRecord = kTypeIdUserDataRecord + 0x200, | |
| 650 | 716 |
| 651 kTypeIdActivityTrackerFree = ~kTypeIdActivityTracker, | 717 kTypeIdActivityTrackerFree = ~kTypeIdActivityTracker, |
| 652 kTypeIdUserDataRecordFree = ~kTypeIdUserDataRecord, | 718 kTypeIdUserDataRecordFree = ~kTypeIdUserDataRecord, |
| 719 kTypeIdProcessDataRecordFree = ~kTypeIdProcessDataRecord, | |
| 653 }; | 720 }; |
| 654 | 721 |
| 655 // This structure contains information about a loaded module, as shown to | 722 // This structure contains information about a loaded module, as shown to |
| 656 // users of the tracker. | 723 // users of the tracker. |
| 657 struct BASE_EXPORT ModuleInfo { | 724 struct BASE_EXPORT ModuleInfo { |
| 658 ModuleInfo(); | 725 ModuleInfo(); |
| 659 ModuleInfo(ModuleInfo&& rhs); | 726 ModuleInfo(ModuleInfo&& rhs); |
| 660 ModuleInfo(const ModuleInfo& rhs); | 727 ModuleInfo(const ModuleInfo& rhs); |
| 661 ~ModuleInfo(); | 728 ~ModuleInfo(); |
| 662 | 729 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 773 return tracker; | 840 return tracker; |
| 774 return CreateTrackerForCurrentThread(); | 841 return CreateTrackerForCurrentThread(); |
| 775 } | 842 } |
| 776 | 843 |
| 777 // Creates an activity-tracker for the current thread. | 844 // Creates an activity-tracker for the current thread. |
| 778 ThreadActivityTracker* CreateTrackerForCurrentThread(); | 845 ThreadActivityTracker* CreateTrackerForCurrentThread(); |
| 779 | 846 |
| 780 // Releases the activity-tracker for the current thread (for testing only). | 847 // Releases the activity-tracker for the current thread (for testing only). |
| 781 void ReleaseTrackerForCurrentThreadForTesting(); | 848 void ReleaseTrackerForCurrentThreadForTesting(); |
| 782 | 849 |
| 850 // Sets a task-runner that can be used for background work. | |
| 851 void SetBackgroundTaskRunner(const scoped_refptr<TaskRunner>& runner); | |
| 852 | |
| 853 // Manages process lifetimes. | |
| 854 void RecordProcessLaunch(ProcessId process_id); | |
| 855 void RecordProcessExit(ProcessId process_id, int exit_code); | |
| 856 static void RecordProcessLaunchIfEnabled(ProcessId process_id) { | |
| 857 GlobalActivityTracker* tracker = Get(); | |
| 858 if (tracker) | |
| 859 tracker->RecordProcessLaunch(process_id); | |
| 860 } | |
| 861 static void RecordProcessExitIfEnabled(ProcessId process_id, int exit_code) { | |
| 862 GlobalActivityTracker* tracker = Get(); | |
| 863 if (tracker) | |
| 864 tracker->RecordProcessExit(process_id, exit_code); | |
| 865 } | |
| 866 | |
| 783 // Records a log message. The current implementation does NOT recycle these | 867 // Records a log message. The current implementation does NOT recycle these |
| 784 // only store critical messages such as FATAL ones. | 868 // only store critical messages such as FATAL ones. |
| 785 void RecordLogMessage(StringPiece message); | 869 void RecordLogMessage(StringPiece message); |
| 786 | 870 |
| 787 // Records a module load/unload event. This is safe to call multiple times | 871 // Records a module load/unload event. This is safe to call multiple times |
| 788 // even with the same information. | 872 // even with the same information. |
| 789 void RecordModuleInfo(const ModuleInfo& info); | 873 void RecordModuleInfo(const ModuleInfo& info); |
| 790 | 874 |
| 791 // Record field trial information. This call is thread-safe. In addition to | 875 // Record field trial information. This call is thread-safe. In addition to |
| 792 // this, construction of a GlobalActivityTracker will cause all existing | 876 // this, construction of a GlobalActivityTracker will cause all existing |
| 793 // active field trials to be fetched and recorded. | 877 // active field trials to be fetched and recorded. |
| 794 void RecordFieldTrial(const std::string& trial_name, StringPiece group_name); | 878 void RecordFieldTrial(const std::string& trial_name, StringPiece group_name); |
| 795 | 879 |
| 880 // Accesses the process data record for storing arbitrary key/value pairs. | |
| 881 // Updates to this are thread-safe. | |
| 882 ActivityUserData& process_data() { return process_data_; } | |
| 883 | |
| 796 // Accesses the global data record for storing arbitrary key/value pairs. | 884 // Accesses the global data record for storing arbitrary key/value pairs. |
| 885 // Updates to this are thread-safe. | |
| 797 ActivityUserData& global_data() { return global_data_; } | 886 ActivityUserData& global_data() { return global_data_; } |
| 798 | 887 |
| 799 private: | 888 private: |
| 800 friend class GlobalActivityAnalyzer; | 889 friend class GlobalActivityAnalyzer; |
| 801 friend class ScopedThreadActivity; | 890 friend class ScopedThreadActivity; |
| 802 friend class ActivityTrackerTest; | 891 friend class ActivityTrackerTest; |
| 803 | 892 |
| 804 enum : int { | 893 enum : int { |
| 805 // The maximum number of threads that can be tracked within a process. If | 894 // The maximum number of threads that can be tracked within a process. If |
| 806 // more than this number run concurrently, tracking of new ones may cease. | 895 // more than this number run concurrently, tracking of new ones may cease. |
| 807 kMaxThreadCount = 100, | 896 kMaxThreadCount = 100, |
| 808 kCachedThreadMemories = 10, | 897 kCachedThreadMemories = 10, |
| 809 kCachedUserDataMemories = 10, | 898 kCachedUserDataMemories = 10, |
| 810 }; | 899 }; |
| 811 | 900 |
| 812 // A wrapper around ActivityUserData that is thread-safe and thus can be used | 901 // A wrapper around ActivityUserData that is thread-safe and thus can be used |
| 813 // in the global scope without the requirement of being called from only one | 902 // in the global scope without the requirement of being called from only one |
| 814 // thread. | 903 // thread. |
| 815 class GlobalUserData : public ActivityUserData { | 904 class ThreadSafeUserData : public ActivityUserData { |
| 816 public: | 905 public: |
| 817 GlobalUserData(void* memory, size_t size); | 906 ThreadSafeUserData(void* memory, size_t size); |
| 818 ~GlobalUserData() override; | 907 ~ThreadSafeUserData() override; |
| 819 | 908 |
| 820 private: | 909 private: |
| 821 void Set(StringPiece name, | 910 void Set(StringPiece name, |
| 822 ValueType type, | 911 ValueType type, |
| 823 const void* memory, | 912 const void* memory, |
| 824 size_t size) override; | 913 size_t size) override; |
| 825 | 914 |
| 826 Lock data_lock_; | 915 Lock data_lock_; |
| 827 | 916 |
| 828 DISALLOW_COPY_AND_ASSIGN(GlobalUserData); | 917 DISALLOW_COPY_AND_ASSIGN(ThreadSafeUserData); |
| 829 }; | 918 }; |
| 830 | 919 |
| 831 // State of a module as stored in persistent memory. This supports a single | 920 // State of a module as stored in persistent memory. This supports a single |
| 832 // loading of a module only. If modules are loaded multiple times at | 921 // loading of a module only. If modules are loaded multiple times at |
| 833 // different addresses, only the last will be recorded and an unload will | 922 // different addresses, only the last will be recorded and an unload will |
| 834 // not revert to the information of any other addresses. | 923 // not revert to the information of any other addresses. |
| 835 struct BASE_EXPORT ModuleInfoRecord { | 924 struct BASE_EXPORT ModuleInfoRecord { |
| 836 // SHA1(ModuleInfoRecord): Increment this if structure changes! | 925 // SHA1(ModuleInfoRecord): Increment this if structure changes! |
| 837 static constexpr uint32_t kPersistentTypeId = 0x05DB5F41 + 1; | 926 static constexpr uint32_t kPersistentTypeId = 0x05DB5F41 + 1; |
| 838 | 927 |
| 839 // Expected size for 32/64-bit check by PersistentMemoryAllocator. | 928 // Expected size for 32/64-bit check by PersistentMemoryAllocator. |
| 840 static constexpr size_t kExpectedInstanceSize = 56; | 929 static constexpr size_t kExpectedInstanceSize = |
| 930 ProcessInfo::kExpectedInstanceSize + 56; | |
| 841 | 931 |
| 842 // The atomic unfortunately makes this a "complex" class on some compilers | 932 // The atomic unfortunately makes this a "complex" class on some compilers |
| 843 // and thus requires an out-of-line constructor & destructor even though | 933 // and thus requires an out-of-line constructor & destructor even though |
| 844 // they do nothing. | 934 // they do nothing. |
| 845 ModuleInfoRecord(); | 935 ModuleInfoRecord(); |
| 846 ~ModuleInfoRecord(); | 936 ~ModuleInfoRecord(); |
| 847 | 937 |
| 938 ProcessInfo process_info; // The process that created this record. | |
| 848 uint64_t address; // The base address of the module. | 939 uint64_t address; // The base address of the module. |
| 849 uint64_t load_time; // Time of last load/unload. | 940 uint64_t load_time; // Time of last load/unload. |
| 850 uint64_t size; // The size of the module in bytes. | 941 uint64_t size; // The size of the module in bytes. |
| 851 uint32_t timestamp; // Opaque timestamp of the module. | 942 uint32_t timestamp; // Opaque timestamp of the module. |
| 852 uint32_t age; // Opaque "age" associated with the module. | 943 uint32_t age; // Opaque "age" associated with the module. |
| 853 uint8_t identifier[16]; // Opaque identifier for the module. | 944 uint8_t identifier[16]; // Opaque identifier for the module. |
| 854 std::atomic<uint32_t> changes; // Number load/unload actions. | 945 std::atomic<uint32_t> changes; // Number load/unload actions. |
| 855 uint16_t pickle_size; // The size of the following pickle. | 946 uint16_t pickle_size; // The size of the following pickle. |
| 856 uint8_t loaded; // Flag if module is loaded or not. | 947 uint8_t loaded; // Flag if module is loaded or not. |
| 857 char pickle[1]; // Other strings; may allocate larger. | 948 char pickle[1]; // Other strings; may allocate larger. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 901 | 992 |
| 902 // Returns the memory used by an activity-tracker managed by this class. | 993 // Returns the memory used by an activity-tracker managed by this class. |
| 903 // It is called during the destruction of a ManagedActivityTracker object. | 994 // It is called during the destruction of a ManagedActivityTracker object. |
| 904 void ReturnTrackerMemory(ManagedActivityTracker* tracker); | 995 void ReturnTrackerMemory(ManagedActivityTracker* tracker); |
| 905 | 996 |
| 906 // Releases the activity-tracker associcated with thread. It is called | 997 // Releases the activity-tracker associcated with thread. It is called |
| 907 // automatically when a thread is joined and thus there is nothing more to | 998 // automatically when a thread is joined and thus there is nothing more to |
| 908 // be tracked. |value| is a pointer to a ManagedActivityTracker. | 999 // be tracked. |value| is a pointer to a ManagedActivityTracker. |
| 909 static void OnTLSDestroy(void* value); | 1000 static void OnTLSDestroy(void* value); |
| 910 | 1001 |
| 1002 // Does process-exit work. This can be run on any thread. | |
| 1003 void CleanupAfterProcess(ProcessId process_id, int64_t exit_stamp); | |
|
manzagop (departed)
2017/02/22 20:44:15
I'm still not sure how we call CleanupAfterProcess
bcwhite
2017/02/22 22:13:02
Shouldn't. The base Process code deals with the c
| |
| 1004 | |
| 911 // The persistent-memory allocator from which the memory for all trackers | 1005 // The persistent-memory allocator from which the memory for all trackers |
| 912 // is taken. | 1006 // is taken. |
| 913 std::unique_ptr<PersistentMemoryAllocator> allocator_; | 1007 std::unique_ptr<PersistentMemoryAllocator> allocator_; |
| 914 | 1008 |
| 915 // The size (in bytes) of memory required by a ThreadActivityTracker to | 1009 // The size (in bytes) of memory required by a ThreadActivityTracker to |
| 916 // provide the stack-depth requested during construction. | 1010 // provide the stack-depth requested during construction. |
| 917 const size_t stack_memory_size_; | 1011 const size_t stack_memory_size_; |
| 918 | 1012 |
| 919 // The activity tracker for the currently executing thread. | 1013 // The activity tracker for the currently executing thread. |
| 920 base::ThreadLocalStorage::Slot this_thread_tracker_; | 1014 base::ThreadLocalStorage::Slot this_thread_tracker_; |
| 921 | 1015 |
| 922 // The number of thread trackers currently active. | 1016 // The number of thread trackers currently active. |
| 923 std::atomic<int> thread_tracker_count_; | 1017 std::atomic<int> thread_tracker_count_; |
| 924 | 1018 |
| 925 // A caching memory allocator for thread-tracker objects. | 1019 // A caching memory allocator for thread-tracker objects. |
| 926 ActivityTrackerMemoryAllocator thread_tracker_allocator_; | 1020 ActivityTrackerMemoryAllocator thread_tracker_allocator_; |
| 927 base::Lock thread_tracker_allocator_lock_; | 1021 base::Lock thread_tracker_allocator_lock_; |
| 928 | 1022 |
| 929 // A caching memory allocator for user data attached to activity data. | 1023 // A caching memory allocator for user data attached to activity data. |
| 930 ActivityTrackerMemoryAllocator user_data_allocator_; | 1024 ActivityTrackerMemoryAllocator user_data_allocator_; |
| 931 base::Lock user_data_allocator_lock_; | 1025 base::Lock user_data_allocator_lock_; |
| 932 | 1026 |
| 933 // An object for holding global arbitrary key value pairs. Values must always | 1027 // An object for holding arbitrary key value pairs with thread-safe access. |
| 934 // be written from the main UI thread. | 1028 ThreadSafeUserData process_data_; |
| 935 GlobalUserData global_data_; | 1029 ThreadSafeUserData global_data_; |
| 936 | 1030 |
| 937 // A map of global module information, keyed by module path. | 1031 // A map of global module information, keyed by module path. |
| 938 std::map<const std::string, ModuleInfoRecord*> modules_; | 1032 std::map<const std::string, ModuleInfoRecord*> modules_; |
| 939 base::Lock modules_lock_; | 1033 base::Lock modules_lock_; |
| 940 | 1034 |
| 941 // The active global activity tracker. | 1035 // The active global activity tracker. |
| 942 static subtle::AtomicWord g_tracker_; | 1036 static subtle::AtomicWord g_tracker_; |
| 943 | 1037 |
| 1038 // A lock that is used to procect access to the following fields. | |
| 1039 base::Lock global_tracker_lock_; | |
| 1040 | |
| 1041 // The collection of processes being tracked. | |
|
manzagop (departed)
2017/02/22 20:44:15
This is not persistent information. Should it be?
bcwhite
2017/02/22 22:13:02
That race would continue to exist because the proc
| |
| 1042 std::set<int64_t> known_processes_; | |
| 1043 | |
| 1044 // A task-runner that can be used for doing background processing. | |
| 1045 scoped_refptr<TaskRunner> background_task_runner_; | |
| 1046 | |
| 944 DISALLOW_COPY_AND_ASSIGN(GlobalActivityTracker); | 1047 DISALLOW_COPY_AND_ASSIGN(GlobalActivityTracker); |
| 945 }; | 1048 }; |
| 946 | 1049 |
| 947 | 1050 |
| 948 // Record entry in to and out of an arbitrary block of code. | 1051 // Record entry in to and out of an arbitrary block of code. |
| 949 class BASE_EXPORT ScopedActivity | 1052 class BASE_EXPORT ScopedActivity |
| 950 : public GlobalActivityTracker::ScopedThreadActivity { | 1053 : public GlobalActivityTracker::ScopedThreadActivity { |
| 951 public: | 1054 public: |
| 952 // Track activity at the specified FROM_HERE location for an arbitrary | 1055 // Track activity at the specified FROM_HERE location for an arbitrary |
| 953 // 4-bit |action|, an arbitrary 32-bit |id|, and 32-bits of arbitrary | 1056 // 4-bit |action|, an arbitrary 32-bit |id|, and 32-bits of arbitrary |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1068 ScopedProcessWaitActivity(const void* program_counter, | 1171 ScopedProcessWaitActivity(const void* program_counter, |
| 1069 const base::Process* process); | 1172 const base::Process* process); |
| 1070 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity); | 1173 DISALLOW_COPY_AND_ASSIGN(ScopedProcessWaitActivity); |
| 1071 }; | 1174 }; |
| 1072 #endif | 1175 #endif |
| 1073 | 1176 |
| 1074 } // namespace debug | 1177 } // namespace debug |
| 1075 } // namespace base | 1178 } // namespace base |
| 1076 | 1179 |
| 1077 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_ | 1180 #endif // BASE_DEBUG_ACTIVITY_TRACKER_H_ |
| OLD | NEW |