| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAGER_
H_ | 5 #ifndef COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAGER_
H_ |
| 6 #define COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAGER_
H_ | 6 #define COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAGER_
H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
| 16 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/discardable_memory_allocator.h" | 18 #include "base/memory/discardable_memory_allocator.h" |
| 19 #include "base/memory/discardable_shared_memory.h" | 19 #include "base/memory/discardable_shared_memory.h" |
| 20 #include "base/memory/memory_coordinator_client.h" | 20 #include "base/memory/memory_coordinator_client.h" |
| 21 #include "base/memory/memory_pressure_listener.h" | 21 #include "base/memory/memory_pressure_listener.h" |
| 22 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
| 23 #include "base/memory/shared_memory.h" | 23 #include "base/memory/shared_memory.h" |
| 24 #include "base/memory/weak_ptr.h" | 24 #include "base/memory/weak_ptr.h" |
| 25 #include "base/process/process_handle.h" | 25 #include "base/process/process_handle.h" |
| 26 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 27 #include "base/threading/thread_task_runner_handle.h" | 27 #include "base/threading/thread_task_runner_handle.h" |
| 28 #include "base/trace_event/memory_dump_provider.h" | 28 #include "base/trace_event/memory_dump_provider.h" |
| 29 #include "components/discardable_memory/common/discardable_memory_export.h" | 29 #include "components/discardable_memory/common/discardable_memory_export.h" |
| 30 #include "components/discardable_memory/common/discardable_shared_memory_id.h" | |
| 31 | 30 |
| 32 namespace discardable_memory { | 31 namespace discardable_memory { |
| 33 | 32 |
| 34 // Implementation of DiscardableMemoryAllocator that allocates and manages | 33 // Implementation of DiscardableMemoryAllocator that allocates and manages |
| 35 // discardable memory segments for the process which hosts this class, and | 34 // discardable memory segments for the process which hosts this class, and |
| 36 // for remote processes which request discardable memory from this class via | 35 // for remote processes which request discardable memory from this class via |
| 37 // IPC. | 36 // IPC. |
| 38 // This class is thread-safe and instances can safely be used on any thread. | 37 // This class is thread-safe and instances can safely be used on any thread. |
| 39 class DISCARDABLE_MEMORY_EXPORT DiscardableSharedMemoryManager | 38 class DISCARDABLE_MEMORY_EXPORT DiscardableSharedMemoryManager |
| 40 : public base::DiscardableMemoryAllocator, | 39 : public base::DiscardableMemoryAllocator, |
| 41 public base::trace_event::MemoryDumpProvider, | 40 public base::trace_event::MemoryDumpProvider, |
| 42 public base::MemoryCoordinatorClient { | 41 public base::MemoryCoordinatorClient { |
| 43 public: | 42 public: |
| 44 DiscardableSharedMemoryManager(); | 43 DiscardableSharedMemoryManager(); |
| 45 ~DiscardableSharedMemoryManager() override; | 44 ~DiscardableSharedMemoryManager() override; |
| 46 | 45 |
| 47 // Returns a singleton instance. | 46 // Returns a singleton instance. |
| 48 static DiscardableSharedMemoryManager* current(); | 47 static DiscardableSharedMemoryManager* current(); |
| 49 | 48 |
| 50 // Overridden from base::DiscardableMemoryAllocator: | 49 // Overridden from base::DiscardableMemoryAllocator: |
| 51 std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( | 50 std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( |
| 52 size_t size) override; | 51 size_t size) override; |
| 53 | 52 |
| 54 // Overridden from base::trace_event::MemoryDumpProvider: | 53 // Overridden from base::trace_event::MemoryDumpProvider: |
| 55 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, | 54 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
| 56 base::trace_event::ProcessMemoryDump* pmd) override; | 55 base::trace_event::ProcessMemoryDump* pmd) override; |
| 57 | 56 |
| 58 // TODO(penghuang): Get ride of the |process_handle| when we switch to mojo. | |
| 59 // This allocates a discardable memory segment for |process_handle|. | 57 // This allocates a discardable memory segment for |process_handle|. |
| 60 // A valid shared memory handle is returned on success. | 58 // A valid shared memory handle is returned on success. |
| 61 void AllocateLockedDiscardableSharedMemoryForClient( | 59 void AllocateLockedDiscardableSharedMemoryForClient( |
| 62 base::ProcessHandle process_handle, | |
| 63 int client_id, | 60 int client_id, |
| 64 size_t size, | 61 size_t size, |
| 65 DiscardableSharedMemoryId id, | 62 int32_t id, |
| 66 base::SharedMemoryHandle* shared_memory_handle); | 63 base::SharedMemoryHandle* shared_memory_handle); |
| 67 | 64 |
| 68 // Call this to notify the manager that client process associated with | 65 // Call this to notify the manager that client process associated with |
| 69 // |client_id| has deleted discardable memory segment with |id|. | 66 // |client_id| has deleted discardable memory segment with |id|. |
| 70 void ClientDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id, | 67 void ClientDeletedDiscardableSharedMemory(int32_t id, int client_id); |
| 71 int client_id); | |
| 72 | 68 |
| 73 // Call this to notify the manager that client associated with |client_id| | 69 // Call this to notify the manager that client associated with |client_id| |
| 74 // has been removed. The manager will use this to release memory segments | 70 // has been removed. The manager will use this to release memory segments |
| 75 // allocated for client to the OS. | 71 // allocated for client to the OS. |
| 76 void ClientRemoved(int client_id); | 72 void ClientRemoved(int client_id); |
| 77 | 73 |
| 78 // The maximum number of bytes of memory that may be allocated. This will | 74 // The maximum number of bytes of memory that may be allocated. This will |
| 79 // cause memory usage to be reduced if currently above |limit|. | 75 // cause memory usage to be reduced if currently above |limit|. |
| 80 void SetMemoryLimit(size_t limit); | 76 void SetMemoryLimit(size_t limit); |
| 81 | 77 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 104 | 100 |
| 105 static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a, | 101 static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a, |
| 106 const scoped_refptr<MemorySegment>& b) { | 102 const scoped_refptr<MemorySegment>& b) { |
| 107 // In this system, LRU memory segment is evicted first. | 103 // In this system, LRU memory segment is evicted first. |
| 108 return a->memory()->last_known_usage() > b->memory()->last_known_usage(); | 104 return a->memory()->last_known_usage() > b->memory()->last_known_usage(); |
| 109 } | 105 } |
| 110 | 106 |
| 111 // base::MemoryCoordinatorClient implementation: | 107 // base::MemoryCoordinatorClient implementation: |
| 112 void OnMemoryStateChange(base::MemoryState state) override; | 108 void OnMemoryStateChange(base::MemoryState state) override; |
| 113 | 109 |
| 114 // TODO(penghuang): Get ride of the |process_handle| when we switch to mojo. | |
| 115 void AllocateLockedDiscardableSharedMemory( | 110 void AllocateLockedDiscardableSharedMemory( |
| 116 base::ProcessHandle process_handle, | |
| 117 int client_id, | 111 int client_id, |
| 118 size_t size, | 112 size_t size, |
| 119 DiscardableSharedMemoryId id, | 113 int32_t id, |
| 120 base::SharedMemoryHandle* shared_memory_handle); | 114 base::SharedMemoryHandle* shared_memory_handle); |
| 121 void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id, | 115 void DeletedDiscardableSharedMemory(int32_t id, int client_id); |
| 122 int client_id); | |
| 123 void OnMemoryPressure( | 116 void OnMemoryPressure( |
| 124 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | 117 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); |
| 125 void ReduceMemoryUsageUntilWithinMemoryLimit(); | 118 void ReduceMemoryUsageUntilWithinMemoryLimit(); |
| 126 void ReduceMemoryUsageUntilWithinLimit(size_t limit); | 119 void ReduceMemoryUsageUntilWithinLimit(size_t limit); |
| 127 void ReleaseMemory(base::DiscardableSharedMemory* memory); | 120 void ReleaseMemory(base::DiscardableSharedMemory* memory); |
| 128 void BytesAllocatedChanged(size_t new_bytes_allocated) const; | 121 void BytesAllocatedChanged(size_t new_bytes_allocated) const; |
| 129 | 122 |
| 130 // Virtual for tests. | 123 // Virtual for tests. |
| 131 virtual base::Time Now() const; | 124 virtual base::Time Now() const; |
| 132 virtual void ScheduleEnforceMemoryPolicy(); | 125 virtual void ScheduleEnforceMemoryPolicy(); |
| 133 | 126 |
| 134 base::Lock lock_; | 127 base::Lock lock_; |
| 135 typedef base::hash_map<DiscardableSharedMemoryId, | 128 typedef base::hash_map<int32_t, scoped_refptr<MemorySegment>> |
| 136 scoped_refptr<MemorySegment>> | |
| 137 MemorySegmentMap; | 129 MemorySegmentMap; |
| 138 typedef base::hash_map<int, MemorySegmentMap> ClientMap; | 130 typedef base::hash_map<int, MemorySegmentMap> ClientMap; |
| 139 ClientMap clients_; | 131 ClientMap clients_; |
| 140 // Note: The elements in |segments_| are arranged in such a way that they form | 132 // Note: The elements in |segments_| are arranged in such a way that they form |
| 141 // a heap. The LRU memory segment always first. | 133 // a heap. The LRU memory segment always first. |
| 142 typedef std::vector<scoped_refptr<MemorySegment>> MemorySegmentVector; | 134 typedef std::vector<scoped_refptr<MemorySegment>> MemorySegmentVector; |
| 143 MemorySegmentVector segments_; | 135 MemorySegmentVector segments_; |
| 144 size_t default_memory_limit_; | 136 size_t default_memory_limit_; |
| 145 size_t memory_limit_; | 137 size_t memory_limit_; |
| 146 size_t bytes_allocated_; | 138 size_t bytes_allocated_; |
| 147 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 139 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 148 scoped_refptr<base::SingleThreadTaskRunner> | 140 scoped_refptr<base::SingleThreadTaskRunner> |
| 149 enforce_memory_policy_task_runner_; | 141 enforce_memory_policy_task_runner_; |
| 150 base::Closure enforce_memory_policy_callback_; | 142 base::Closure enforce_memory_policy_callback_; |
| 151 bool enforce_memory_policy_pending_; | 143 bool enforce_memory_policy_pending_; |
| 152 base::WeakPtrFactory<DiscardableSharedMemoryManager> weak_ptr_factory_; | 144 base::WeakPtrFactory<DiscardableSharedMemoryManager> weak_ptr_factory_; |
| 153 | 145 |
| 154 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryManager); | 146 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryManager); |
| 155 }; | 147 }; |
| 156 | 148 |
| 157 } // namespace discardable_memory | 149 } // namespace discardable_memory |
| 158 | 150 |
| 159 #endif // COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAG
ER_H_ | 151 #endif // COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAG
ER_H_ |
| OLD | NEW |