| 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/public/interfaces/discardable_shared_mem
     ory_manager.mojom.h" |   30 #include "components/discardable_memory/common/discardable_shared_memory_id.h" | 
|   31  |   31  | 
|   32 namespace discardable_memory { |   32 namespace discardable_memory { | 
|   33  |   33  | 
|   34 // Implementation of DiscardableMemoryAllocator that allocates and manages |   34 // Implementation of DiscardableMemoryAllocator that allocates and manages | 
|   35 // discardable memory segments for the process which hosts this class, and |   35 // discardable memory segments for the process which hosts this class, and | 
|   36 // for remote processes which request discardable memory from this class via |   36 // for remote processes which request discardable memory from this class via | 
|   37 // IPC. |   37 // IPC. | 
|   38 // This class is thread-safe and instances can safely be used on any thread. |   38 // This class is thread-safe and instances can safely be used on any thread. | 
|   39 class DISCARDABLE_MEMORY_EXPORT DiscardableSharedMemoryManager |   39 class DISCARDABLE_MEMORY_EXPORT DiscardableSharedMemoryManager | 
|   40     : public base::DiscardableMemoryAllocator, |   40     : public base::DiscardableMemoryAllocator, | 
|   41       public base::trace_event::MemoryDumpProvider, |   41       public base::trace_event::MemoryDumpProvider, | 
|   42       public base::MemoryCoordinatorClient { |   42       public base::MemoryCoordinatorClient { | 
|   43  public: |   43  public: | 
|   44   DiscardableSharedMemoryManager(); |   44   DiscardableSharedMemoryManager(); | 
|   45   ~DiscardableSharedMemoryManager() override; |   45   ~DiscardableSharedMemoryManager() override; | 
|   46  |   46  | 
|   47   // Create a sigleton instance. |  | 
|   48   static DiscardableSharedMemoryManager* CreateInstance(); |  | 
|   49  |  | 
|   50   // Returns a singleton instance. |   47   // Returns a singleton instance. | 
|   51   static DiscardableSharedMemoryManager* GetInstance(); |   48   static DiscardableSharedMemoryManager* current(); | 
|   52  |  | 
|   53   // Bind the manager to a mojo interface request. |  | 
|   54   void Bind(mojom::DiscardableSharedMemoryManagerRequest request); |  | 
|   55  |   49  | 
|   56   // Overridden from base::DiscardableMemoryAllocator: |   50   // Overridden from base::DiscardableMemoryAllocator: | 
|   57   std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( |   51   std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( | 
|   58       size_t size) override; |   52       size_t size) override; | 
|   59  |   53  | 
|   60   // Overridden from base::trace_event::MemoryDumpProvider: |   54   // Overridden from base::trace_event::MemoryDumpProvider: | 
|   61   bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |   55   bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, | 
|   62                     base::trace_event::ProcessMemoryDump* pmd) override; |   56                     base::trace_event::ProcessMemoryDump* pmd) override; | 
|   63  |   57  | 
 |   58   // TODO(penghuang): Get ride of the |process_handle| when we switch to mojo. | 
|   64   // This allocates a discardable memory segment for |process_handle|. |   59   // This allocates a discardable memory segment for |process_handle|. | 
|   65   // A valid shared memory handle is returned on success. |   60   // A valid shared memory handle is returned on success. | 
|   66   void AllocateLockedDiscardableSharedMemoryForClient( |   61   void AllocateLockedDiscardableSharedMemoryForClient( | 
 |   62       base::ProcessHandle process_handle, | 
|   67       int client_id, |   63       int client_id, | 
|   68       size_t size, |   64       size_t size, | 
|   69       int32_t id, |   65       DiscardableSharedMemoryId id, | 
|   70       base::SharedMemoryHandle* shared_memory_handle); |   66       base::SharedMemoryHandle* shared_memory_handle); | 
|   71  |   67  | 
|   72   // Call this to notify the manager that client process associated with |   68   // Call this to notify the manager that client process associated with | 
|   73   // |client_id| has deleted discardable memory segment with |id|. |   69   // |client_id| has deleted discardable memory segment with |id|. | 
|   74   void ClientDeletedDiscardableSharedMemory(int32_t id, int client_id); |   70   void ClientDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id, | 
 |   71                                             int client_id); | 
|   75  |   72  | 
|   76   // Call this to notify the manager that client associated with |client_id| |   73   // Call this to notify the manager that client associated with |client_id| | 
|   77   // has been removed. The manager will use this to release memory segments |   74   // has been removed. The manager will use this to release memory segments | 
|   78   // allocated for client to the OS. |   75   // allocated for client to the OS. | 
|   79   void ClientRemoved(int client_id); |   76   void ClientRemoved(int client_id); | 
|   80  |   77  | 
|   81   // The maximum number of bytes of memory that may be allocated. This will |   78   // The maximum number of bytes of memory that may be allocated. This will | 
|   82   // cause memory usage to be reduced if currently above |limit|. |   79   // cause memory usage to be reduced if currently above |limit|. | 
|   83   void SetMemoryLimit(size_t limit); |   80   void SetMemoryLimit(size_t limit); | 
|   84  |   81  | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
|  107  |  104  | 
|  108   static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a, |  105   static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a, | 
|  109                                      const scoped_refptr<MemorySegment>& b) { |  106                                      const scoped_refptr<MemorySegment>& b) { | 
|  110     // In this system, LRU memory segment is evicted first. |  107     // In this system, LRU memory segment is evicted first. | 
|  111     return a->memory()->last_known_usage() > b->memory()->last_known_usage(); |  108     return a->memory()->last_known_usage() > b->memory()->last_known_usage(); | 
|  112   } |  109   } | 
|  113  |  110  | 
|  114   // base::MemoryCoordinatorClient implementation: |  111   // base::MemoryCoordinatorClient implementation: | 
|  115   void OnMemoryStateChange(base::MemoryState state) override; |  112   void OnMemoryStateChange(base::MemoryState state) override; | 
|  116  |  113  | 
 |  114   // TODO(penghuang): Get ride of the |process_handle| when we switch to mojo. | 
|  117   void AllocateLockedDiscardableSharedMemory( |  115   void AllocateLockedDiscardableSharedMemory( | 
 |  116       base::ProcessHandle process_handle, | 
|  118       int client_id, |  117       int client_id, | 
|  119       size_t size, |  118       size_t size, | 
|  120       int32_t id, |  119       DiscardableSharedMemoryId id, | 
|  121       base::SharedMemoryHandle* shared_memory_handle); |  120       base::SharedMemoryHandle* shared_memory_handle); | 
|  122   void DeletedDiscardableSharedMemory(int32_t id, int client_id); |  121   void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id, | 
 |  122                                       int client_id); | 
|  123   void OnMemoryPressure( |  123   void OnMemoryPressure( | 
|  124       base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); |  124       base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | 
|  125   void ReduceMemoryUsageUntilWithinMemoryLimit(); |  125   void ReduceMemoryUsageUntilWithinMemoryLimit(); | 
|  126   void ReduceMemoryUsageUntilWithinLimit(size_t limit); |  126   void ReduceMemoryUsageUntilWithinLimit(size_t limit); | 
|  127   void ReleaseMemory(base::DiscardableSharedMemory* memory); |  127   void ReleaseMemory(base::DiscardableSharedMemory* memory); | 
|  128   void BytesAllocatedChanged(size_t new_bytes_allocated) const; |  128   void BytesAllocatedChanged(size_t new_bytes_allocated) const; | 
|  129  |  129  | 
|  130   // Virtual for tests. |  130   // Virtual for tests. | 
|  131   virtual base::Time Now() const; |  131   virtual base::Time Now() const; | 
|  132   virtual void ScheduleEnforceMemoryPolicy(); |  132   virtual void ScheduleEnforceMemoryPolicy(); | 
|  133  |  133  | 
|  134   int32_t next_client_id_; |  | 
|  135  |  | 
|  136   base::Lock lock_; |  134   base::Lock lock_; | 
|  137   using MemorySegmentMap = |  135   typedef base::hash_map<DiscardableSharedMemoryId, | 
|  138       base::hash_map<int32_t, scoped_refptr<MemorySegment>>; |  136                          scoped_refptr<MemorySegment>> | 
|  139   using ClientMap = base::hash_map<int, MemorySegmentMap>; |  137       MemorySegmentMap; | 
 |  138   typedef base::hash_map<int, MemorySegmentMap> ClientMap; | 
|  140   ClientMap clients_; |  139   ClientMap clients_; | 
|  141   // Note: The elements in |segments_| are arranged in such a way that they form |  140   // Note: The elements in |segments_| are arranged in such a way that they form | 
|  142   // a heap. The LRU memory segment always first. |  141   // a heap. The LRU memory segment always first. | 
|  143   using MemorySegmentVector = std::vector<scoped_refptr<MemorySegment>>; |  142   typedef std::vector<scoped_refptr<MemorySegment>> MemorySegmentVector; | 
|  144   MemorySegmentVector segments_; |  143   MemorySegmentVector segments_; | 
|  145   size_t default_memory_limit_; |  144   size_t default_memory_limit_; | 
|  146   size_t memory_limit_; |  145   size_t memory_limit_; | 
|  147   size_t bytes_allocated_; |  146   size_t bytes_allocated_; | 
|  148   std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |  147   std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 
|  149   scoped_refptr<base::SingleThreadTaskRunner> |  148   scoped_refptr<base::SingleThreadTaskRunner> | 
|  150       enforce_memory_policy_task_runner_; |  149       enforce_memory_policy_task_runner_; | 
|  151   base::Closure enforce_memory_policy_callback_; |  150   base::Closure enforce_memory_policy_callback_; | 
|  152   bool enforce_memory_policy_pending_; |  151   bool enforce_memory_policy_pending_; | 
|  153   base::WeakPtrFactory<DiscardableSharedMemoryManager> weak_ptr_factory_; |  152   base::WeakPtrFactory<DiscardableSharedMemoryManager> weak_ptr_factory_; | 
|  154  |  153  | 
|  155   DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryManager); |  154   DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryManager); | 
|  156 }; |  155 }; | 
|  157  |  156  | 
|  158 }  // namespace discardable_memory |  157 }  // namespace discardable_memory | 
|  159  |  158  | 
|  160 #endif  // COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAG
     ER_H_ |  159 #endif  // COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAG
     ER_H_ | 
| OLD | NEW |