shared_memory_tracker.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2017 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_MEMORY_SHARED_MEMORY_TRACKER_H_
  5. #define BASE_MEMORY_SHARED_MEMORY_TRACKER_H_
  6. #include <map>
  7. #include <string>
  8. #include "base/memory/shared_memory_mapping.h"
  9. #include "base/synchronization/lock.h"
  10. #include "base/trace_event/base_tracing.h"
  11. namespace base {
  12. namespace trace_event {
  13. class MemoryAllocatorDump;
  14. class MemoryAllocatorDumpGuid;
  15. class ProcessMemoryDump;
  16. }
  17. // SharedMemoryTracker tracks shared memory usage.
  18. class BASE_EXPORT SharedMemoryTracker : public trace_event::MemoryDumpProvider {
  19. public:
  20. // Returns a singleton instance.
  21. static SharedMemoryTracker* GetInstance();
  22. static std::string GetDumpNameForTracing(const UnguessableToken& id);
  23. static trace_event::MemoryAllocatorDumpGuid GetGlobalDumpIdForTracing(
  24. const UnguessableToken& id);
  25. // Gets or creates if non-existant, a memory dump for the |shared_memory|
  26. // inside the given |pmd|. Also adds the necessary edges for the dump when
  27. // creating the dump.
  28. static const trace_event::MemoryAllocatorDump* GetOrCreateSharedMemoryDump(
  29. const SharedMemoryMapping& shared_memory,
  30. trace_event::ProcessMemoryDump* pmd);
  31. // Records shared memory usage on valid mapping.
  32. void IncrementMemoryUsage(const SharedMemoryMapping& mapping);
  33. // Records shared memory usage on unmapping.
  34. void DecrementMemoryUsage(const SharedMemoryMapping& mapping);
  35. // Root dump name for all shared memory dumps.
  36. static const char kDumpRootName[];
  37. private:
  38. SharedMemoryTracker();
  39. ~SharedMemoryTracker() override;
  40. // trace_event::MemoryDumpProvider implementation.
  41. bool OnMemoryDump(const trace_event::MemoryDumpArgs& args,
  42. trace_event::ProcessMemoryDump* pmd) override;
  43. static const trace_event::MemoryAllocatorDump*
  44. GetOrCreateSharedMemoryDumpInternal(void* mapped_memory,
  45. size_t mapped_size,
  46. const UnguessableToken& mapped_id,
  47. trace_event::ProcessMemoryDump* pmd);
  48. // Information associated with each mapped address.
  49. struct UsageInfo {
  50. UsageInfo(size_t size, const UnguessableToken& id)
  51. : mapped_size(size), mapped_id(id) {}
  52. size_t mapped_size;
  53. UnguessableToken mapped_id;
  54. };
  55. Lock usages_lock_;
  56. std::map<void*, UsageInfo> usages_ GUARDED_BY(usages_lock_);
  57. DISALLOW_COPY_AND_ASSIGN(SharedMemoryTracker);
  58. };
  59. } // namespace base
  60. #endif // BASE_MEMORY_SHARED_MEMORY_TRACKER_H_