discardable_shared_memory.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2014 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_DISCARDABLE_SHARED_MEMORY_H_
  5. #define BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_
  6. #include <stddef.h>
  7. #include "base/base_export.h"
  8. #include "base/dcheck_is_on.h"
  9. #include "base/memory/shared_memory_mapping.h"
  10. #include "base/memory/unsafe_shared_memory_region.h"
  11. #include "base/threading/thread_collision_warner.h"
  12. #include "base/time/time.h"
  13. #include "build/build_config.h"
  14. #if DCHECK_IS_ON()
  15. #include <set>
  16. #endif
  17. // Linux (including Android) support the MADV_REMOVE argument with madvise()
  18. // which has the behavior of reliably causing zero-fill-on-demand pages to
  19. // be returned after a call. Here we define
  20. // DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE on Linux
  21. // and Android to indicate that this type of behavior can be expected on
  22. // those platforms. Note that madvise() will still be used on other POSIX
  23. // platforms but doesn't provide the zero-fill-on-demand pages guarantee.
  24. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  25. #define DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE
  26. #endif
  27. namespace base {
  28. namespace trace_event {
  29. class MemoryAllocatorDump;
  30. class ProcessMemoryDump;
  31. } // namespace trace_event
  32. // Platform abstraction for discardable shared memory.
  33. //
  34. // This class is not thread-safe. Clients are responsible for synchronizing
  35. // access to an instance of this class.
  36. class BASE_EXPORT DiscardableSharedMemory {
  37. public:
  38. enum LockResult { SUCCESS, PURGED, FAILED };
  39. DiscardableSharedMemory();
  40. // Create a new DiscardableSharedMemory object from an existing, open shared
  41. // memory file. Memory must be locked.
  42. explicit DiscardableSharedMemory(UnsafeSharedMemoryRegion region);
  43. DiscardableSharedMemory(const DiscardableSharedMemory&) = delete;
  44. DiscardableSharedMemory& operator=(const DiscardableSharedMemory&) = delete;
  45. // Closes any open files.
  46. virtual ~DiscardableSharedMemory();
  47. // Creates and maps a locked DiscardableSharedMemory object with |size|.
  48. // Returns true on success and false on failure.
  49. bool CreateAndMap(size_t size);
  50. // Maps the locked discardable memory into the caller's address space.
  51. // Returns true on success, false otherwise.
  52. bool Map(size_t size);
  53. // Unmaps the discardable shared memory from the caller's address space.
  54. // Unmapping won't unlock previously locked range.
  55. // Returns true if successful; returns false on error or if the memory is
  56. // not mapped.
  57. bool Unmap();
  58. // The actual size of the mapped memory (may be larger than requested).
  59. size_t mapped_size() const { return mapped_size_; }
  60. // Returns a duplicated shared memory region for this DiscardableSharedMemory
  61. // object.
  62. UnsafeSharedMemoryRegion DuplicateRegion() const {
  63. return shared_memory_region_.Duplicate();
  64. }
  65. // Returns an ID for the shared memory region. This is ID of the mapped region
  66. // consistent across all processes and is valid as long as the region is not
  67. // unmapped.
  68. const UnguessableToken& mapped_id() const {
  69. return shared_memory_mapping_.guid();
  70. }
  71. // Locks a range of memory so that it will not be purged by the system.
  72. // The range of memory must be unlocked. The result of trying to lock an
  73. // already locked range is undefined. |offset| and |length| must both be
  74. // a multiple of the page size as returned by GetPageSize().
  75. // Passing 0 for |length| means "everything onward".
  76. // Returns SUCCESS if range was successfully locked and the memory is still
  77. // resident, PURGED if range was successfully locked but has been purged
  78. // since last time it was locked and FAILED if range could not be locked.
  79. // Locking can fail for two reasons; object might have been purged, our
  80. // last known usage timestamp might be out of date. Last known usage time
  81. // is updated to the actual last usage timestamp if memory is still resident
  82. // or 0 if not.
  83. LockResult Lock(size_t offset, size_t length);
  84. // Unlock a previously successfully locked range of memory. The range of
  85. // memory must be locked. The result of trying to unlock a not
  86. // previously locked range is undefined.
  87. // |offset| and |length| must both be a multiple of the page size as returned
  88. // by GetPageSize().
  89. // Passing 0 for |length| means "everything onward".
  90. void Unlock(size_t offset, size_t length);
  91. // Gets a pointer to the opened discardable memory space. Discardable memory
  92. // must have been mapped via Map().
  93. void* memory() const;
  94. // Returns the last known usage time for DiscardableSharedMemory object. This
  95. // may be earlier than the "true" usage time when memory has been used by a
  96. // different process. Returns NULL time if purged.
  97. Time last_known_usage() const { return last_known_usage_; }
  98. // Releases any allocated pages in the specified range, if supported by the
  99. // platform. Address space in the specified range continues to be reserved.
  100. // The memory is not guaranteed to be released immediately.
  101. // |offset| and |length| are both in bytes. |offset| and |length| must both be
  102. // page aligned.
  103. void ReleaseMemoryIfPossible(size_t offset, size_t length);
  104. // This returns true and sets |last_known_usage_| to 0 if
  105. // DiscardableSharedMemory object was successfully purged. Purging can fail
  106. // for two reasons; object might be locked or our last known usage timestamp
  107. // might be out of date. Last known usage time is updated to |current_time|
  108. // if locked or the actual last usage timestamp if unlocked. It is often
  109. // necessary to call this function twice for the object to successfully be
  110. // purged. First call, updates |last_known_usage_|. Second call, successfully
  111. // purges the object using the updated |last_known_usage_|.
  112. // Note: there is no guarantee that multiple calls to this function will
  113. // successfully purge object. DiscardableSharedMemory object might be locked
  114. // or another thread/process might be able to lock and unlock it in between
  115. // each call.
  116. bool Purge(Time current_time);
  117. // Returns true if memory is still resident.
  118. bool IsMemoryResident() const;
  119. // Returns true if memory is locked.
  120. bool IsMemoryLocked() const;
  121. // Closes the open discardable memory segment.
  122. // It is safe to call Close repeatedly.
  123. void Close();
  124. // For tracing: Creates ownership edge to the underlying shared memory dump
  125. // which is cross process in the given |pmd|. |local_segment_dump| is the dump
  126. // associated with the local discardable shared memory segment and |is_owned|
  127. // is true when the current process owns the segment and the effective memory
  128. // is assigned to the current process.
  129. void CreateSharedMemoryOwnershipEdge(
  130. trace_event::MemoryAllocatorDump* local_segment_dump,
  131. trace_event::ProcessMemoryDump* pmd,
  132. bool is_owned) const;
  133. #if BUILDFLAG(IS_ANDROID)
  134. // Returns true if the Ashmem device is supported on this system.
  135. // Only use this for unit-testing.
  136. static bool IsAshmemDeviceSupportedForTesting();
  137. #endif
  138. private:
  139. // LockPages/UnlockPages are platform-native discardable page management
  140. // helper functions. Both expect |offset| to be specified relative to the
  141. // base address at which |memory| is mapped, and that |offset| and |length|
  142. // are page-aligned by the caller.
  143. // Returns SUCCESS on platforms which do not support discardable pages.
  144. static LockResult LockPages(const UnsafeSharedMemoryRegion& region,
  145. size_t offset,
  146. size_t length);
  147. // UnlockPages() is a no-op on platforms not supporting discardable pages.
  148. static void UnlockPages(const UnsafeSharedMemoryRegion& region,
  149. size_t offset,
  150. size_t length);
  151. // Virtual for tests.
  152. virtual Time Now() const;
  153. UnsafeSharedMemoryRegion shared_memory_region_;
  154. WritableSharedMemoryMapping shared_memory_mapping_;
  155. size_t mapped_size_;
  156. size_t locked_page_count_;
  157. #if DCHECK_IS_ON()
  158. std::set<size_t> locked_pages_;
  159. #endif
  160. // Implementation is not thread-safe but still usable if clients are
  161. // synchronized somehow. Use a collision warner to detect incorrect usage.
  162. DFAKE_MUTEX(thread_collision_warner_);
  163. Time last_known_usage_;
  164. };
  165. } // namespace base
  166. #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_