activity_analyzer.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_DEBUG_ACTIVITY_ANALYZER_H_
  5. #define BASE_DEBUG_ACTIVITY_ANALYZER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/base_export.h"
  12. #include "base/debug/activity_tracker.h"
  13. #include "base/memory/shared_memory_mapping.h"
  14. namespace base {
  15. namespace debug {
  16. class GlobalActivityAnalyzer;
  17. // This class provides analysis of data captured from a ThreadActivityTracker.
  18. // When created, it takes a snapshot of the data held by the tracker and
  19. // makes that information available to other code.
  20. class BASE_EXPORT ThreadActivityAnalyzer {
  21. public:
  22. struct BASE_EXPORT Snapshot : ThreadActivityTracker::Snapshot {
  23. Snapshot();
  24. ~Snapshot();
  25. // The user-data snapshot for an activity, matching the |activity_stack|
  26. // of ThreadActivityTracker::Snapshot, if any.
  27. std::vector<ActivityUserData::Snapshot> user_data_stack;
  28. };
  29. // This class provides keys that uniquely identify a thread, even across
  30. // multiple processes.
  31. class ThreadKey {
  32. public:
  33. ThreadKey(int64_t pid, int64_t tid) : pid_(pid), tid_(tid) {}
  34. bool operator<(const ThreadKey& rhs) const {
  35. if (pid_ != rhs.pid_)
  36. return pid_ < rhs.pid_;
  37. return tid_ < rhs.tid_;
  38. }
  39. bool operator==(const ThreadKey& rhs) const {
  40. return (pid_ == rhs.pid_ && tid_ == rhs.tid_);
  41. }
  42. private:
  43. int64_t pid_;
  44. int64_t tid_;
  45. };
  46. // Creates an analyzer for an existing activity |tracker|. A snapshot is taken
  47. // immediately and the tracker is not referenced again.
  48. explicit ThreadActivityAnalyzer(const ThreadActivityTracker& tracker);
  49. // Creates an analyzer for a block of memory currently or previously in-use
  50. // by an activity-tracker. A snapshot is taken immediately and the memory
  51. // is not referenced again.
  52. ThreadActivityAnalyzer(void* base, size_t size);
  53. // Creates an analyzer for a block of memory held within a persistent-memory
  54. // |allocator| at the given |reference|. A snapshot is taken immediately and
  55. // the memory is not referenced again.
  56. ThreadActivityAnalyzer(PersistentMemoryAllocator* allocator,
  57. PersistentMemoryAllocator::Reference reference);
  58. ~ThreadActivityAnalyzer();
  59. // Adds information from the global analyzer.
  60. void AddGlobalInformation(GlobalActivityAnalyzer* global);
  61. // Returns true iff the contained data is valid. Results from all other
  62. // methods are undefined if this returns false.
  63. bool IsValid() { return activity_snapshot_valid_; }
  64. // Gets the process id and its creation stamp.
  65. int64_t GetProcessId(int64_t* out_stamp = nullptr) {
  66. if (out_stamp)
  67. *out_stamp = activity_snapshot_.create_stamp;
  68. return activity_snapshot_.process_id;
  69. }
  70. // Gets the name of the thread.
  71. const std::string& GetThreadName() {
  72. return activity_snapshot_.thread_name;
  73. }
  74. // Gets the TheadKey for this thread.
  75. ThreadKey GetThreadKey() {
  76. return ThreadKey(activity_snapshot_.process_id,
  77. activity_snapshot_.thread_id);
  78. }
  79. const Snapshot& activity_snapshot() { return activity_snapshot_; }
  80. private:
  81. friend class GlobalActivityAnalyzer;
  82. // The snapshot of the activity tracker taken at the moment of construction.
  83. Snapshot activity_snapshot_;
  84. // Flag indicating if the snapshot data is valid.
  85. bool activity_snapshot_valid_;
  86. // A reference into a persistent memory allocator, used by the global
  87. // analyzer to know where this tracker came from.
  88. PersistentMemoryAllocator::Reference allocator_reference_ = 0;
  89. DISALLOW_COPY_AND_ASSIGN(ThreadActivityAnalyzer);
  90. };
  91. // This class manages analyzers for all known processes and threads as stored
  92. // in a persistent memory allocator. It supports retrieval of them through
  93. // iteration and directly using a ThreadKey, which allows for cross-references
  94. // to be resolved.
  95. // Note that though atomic snapshots are used and everything has its snapshot
  96. // taken at the same time, the multi-snapshot itself is not atomic and thus may
  97. // show small inconsistencies between threads if attempted on a live system.
  98. class BASE_EXPORT GlobalActivityAnalyzer {
  99. public:
  100. struct ProgramLocation {
  101. int module;
  102. uintptr_t offset;
  103. };
  104. using ThreadKey = ThreadActivityAnalyzer::ThreadKey;
  105. // Creates a global analyzer from a persistent memory allocator.
  106. explicit GlobalActivityAnalyzer(
  107. std::unique_ptr<PersistentMemoryAllocator> allocator);
  108. ~GlobalActivityAnalyzer();
  109. // Creates a global analyzer using a given persistent-memory |allocator|.
  110. static std::unique_ptr<GlobalActivityAnalyzer> CreateWithAllocator(
  111. std::unique_ptr<PersistentMemoryAllocator> allocator);
  112. #if !defined(OS_NACL)
  113. // Creates a global analyzer using the contents of a file given in
  114. // |file_path|.
  115. static std::unique_ptr<GlobalActivityAnalyzer> CreateWithFile(
  116. const FilePath& file_path);
  117. #endif // !defined(OS_NACL)
  118. // Like above but accesses an allocator in a mapped shared-memory segment.
  119. static std::unique_ptr<GlobalActivityAnalyzer> CreateWithSharedMemory(
  120. base::ReadOnlySharedMemoryMapping mapping);
  121. // Iterates over all known valid processes and returns their PIDs or zero
  122. // if there are no more. Calls to GetFirstProcess() will perform a global
  123. // snapshot in order to provide a relatively consistent state across the
  124. // future calls to GetNextProcess() and GetFirst/NextAnalyzer(). PIDs are
  125. // returned in the order they're found meaning that a first-launched
  126. // controlling process will be found first. Note, however, that space
  127. // freed by an exiting process may be re-used by a later process.
  128. int64_t GetFirstProcess();
  129. int64_t GetNextProcess();
  130. // Iterates over all known valid analyzers for the a given process or returns
  131. // null if there are no more.
  132. //
  133. // GetFirstProcess() must be called first in order to capture a global
  134. // snapshot! Ownership stays with the global analyzer object and all existing
  135. // analyzer pointers are invalidated when GetFirstProcess() is called.
  136. ThreadActivityAnalyzer* GetFirstAnalyzer(int64_t pid);
  137. ThreadActivityAnalyzer* GetNextAnalyzer();
  138. // Gets the analyzer for a specific thread or null if there is none.
  139. // Ownership stays with the global analyzer object.
  140. ThreadActivityAnalyzer* GetAnalyzerForThread(const ThreadKey& key);
  141. // Extract user data based on a reference and its identifier.
  142. ActivityUserData::Snapshot GetUserDataSnapshot(int64_t pid,
  143. uint32_t ref,
  144. uint32_t id);
  145. // Extract the data for a specific process. An empty snapshot will be
  146. // returned if the process is not known.
  147. const ActivityUserData::Snapshot& GetProcessDataSnapshot(int64_t pid);
  148. // Gets all log messages stored within.
  149. std::vector<std::string> GetLogMessages();
  150. // Gets modules corresponding to a pid. This pid must come from a call to
  151. // GetFirst/NextProcess. Only modules that were first registered prior to
  152. // GetFirstProcess's snapshot are returned.
  153. std::vector<GlobalActivityTracker::ModuleInfo> GetModules(int64_t pid);
  154. // Gets the corresponding "program location" for a given "program counter".
  155. // This will return {0,0} if no mapping could be found.
  156. ProgramLocation GetProgramLocationFromAddress(uint64_t address);
  157. // Returns whether the data is complete. Data can be incomplete if the
  158. // recording size quota is hit.
  159. bool IsDataComplete() const;
  160. private:
  161. using AnalyzerMap =
  162. std::map<ThreadKey, std::unique_ptr<ThreadActivityAnalyzer>>;
  163. struct UserDataSnapshot {
  164. // Complex class needs out-of-line ctor/dtor.
  165. UserDataSnapshot();
  166. UserDataSnapshot(const UserDataSnapshot& rhs);
  167. UserDataSnapshot(UserDataSnapshot&& rhs);
  168. ~UserDataSnapshot();
  169. int64_t process_id;
  170. int64_t create_stamp;
  171. ActivityUserData::Snapshot data;
  172. };
  173. // Finds, creates, and indexes analyzers for all known processes and threads.
  174. void PrepareAllAnalyzers();
  175. // The persistent memory allocator holding all tracking data.
  176. std::unique_ptr<PersistentMemoryAllocator> allocator_;
  177. // The time stamp when analysis began. This is used to prevent looking into
  178. // process IDs that get reused when analyzing a live system.
  179. int64_t analysis_stamp_;
  180. // The iterator for finding tracking information in the allocator.
  181. PersistentMemoryAllocator::Iterator allocator_iterator_;
  182. // A set of all interesting memory references found within the allocator.
  183. std::set<PersistentMemoryAllocator::Reference> memory_references_;
  184. // A set of all process-data memory references found within the allocator.
  185. std::map<int64_t, UserDataSnapshot> process_data_;
  186. // A set of all process IDs collected during PrepareAllAnalyzers. These are
  187. // popped and returned one-by-one with calls to GetFirst/NextProcess().
  188. std::vector<int64_t> process_ids_;
  189. // A map, keyed by ThreadKey, of all valid activity analyzers.
  190. AnalyzerMap analyzers_;
  191. // The iterator within the analyzers_ map for returning analyzers through
  192. // first/next iteration.
  193. AnalyzerMap::iterator analyzers_iterator_;
  194. int64_t analyzers_iterator_pid_;
  195. DISALLOW_COPY_AND_ASSIGN(GlobalActivityAnalyzer);
  196. };
  197. } // namespace debug
  198. } // namespace base
  199. #endif // BASE_DEBUG_ACTIVITY_ANALYZER_H_