shared_memory_tracker.h 2.7 KB

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