discardable_shared_memory.h 7.8 KB

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