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 CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ | 5 #ifndef CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ |
6 #define CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ | 6 #define CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | |
10 #include "base/memory/discardable_memory_shmem_allocator.h" | 11 #include "base/memory/discardable_memory_shmem_allocator.h" |
11 #include "base/memory/discardable_shared_memory.h" | 12 #include "base/memory/discardable_shared_memory.h" |
12 #include "base/memory/linked_ptr.h" | |
13 #include "base/memory/memory_pressure_listener.h" | 13 #include "base/memory/memory_pressure_listener.h" |
14 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
16 #include "base/process/process_handle.h" | 17 #include "base/process/process_handle.h" |
17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
18 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
22 typedef int32 DiscardableSharedMemoryId; | |
Avi (use Gerrit)
2015/03/16 16:58:21
First, int32_t, per the deprecation notice in base
reveman
2015/03/16 18:30:55
int32 -> int32_t, Done.
The reason I didn't make
| |
21 | 23 |
22 // Implementation of DiscardableMemoryShmemAllocator that allocates and | 24 // Implementation of DiscardableMemoryShmemAllocator that allocates and |
23 // manages discardable memory segments for the browser process and child | 25 // manages discardable memory segments for the browser process and child |
24 // processes. This class is thread-safe and instances can safely be used | 26 // processes. This class is thread-safe and instances can safely be used |
25 // on any thread. | 27 // on any thread. |
26 class CONTENT_EXPORT HostDiscardableSharedMemoryManager | 28 class CONTENT_EXPORT HostDiscardableSharedMemoryManager |
27 : public base::DiscardableMemoryShmemAllocator { | 29 : public base::DiscardableMemoryShmemAllocator { |
28 public: | 30 public: |
29 HostDiscardableSharedMemoryManager(); | 31 HostDiscardableSharedMemoryManager(); |
30 ~HostDiscardableSharedMemoryManager() override; | 32 ~HostDiscardableSharedMemoryManager() override; |
31 | 33 |
32 // Returns a singleton instance. | 34 // Returns a singleton instance. |
33 static HostDiscardableSharedMemoryManager* current(); | 35 static HostDiscardableSharedMemoryManager* current(); |
34 | 36 |
35 // Overridden from base::DiscardableMemoryShmemAllocator: | 37 // Overridden from base::DiscardableMemoryShmemAllocator: |
36 scoped_ptr<base::DiscardableMemoryShmemChunk> AllocateLockedDiscardableMemory( | 38 scoped_ptr<base::DiscardableMemoryShmemChunk> AllocateLockedDiscardableMemory( |
37 size_t size) override; | 39 size_t size) override; |
38 | 40 |
39 // This allocates a discardable memory segment for |process_handle|. | 41 // This allocates a discardable memory segment for |process_handle|. |
40 // A valid shared memory handle is returned on success. | 42 // A valid shared memory handle is returned on success. |
41 void AllocateLockedDiscardableSharedMemoryForChild( | 43 void AllocateLockedDiscardableSharedMemoryForChild( |
42 base::ProcessHandle process_handle, | 44 base::ProcessHandle process_handle, |
43 size_t size, | 45 size_t size, |
46 DiscardableSharedMemoryId id, | |
44 base::SharedMemoryHandle* shared_memory_handle); | 47 base::SharedMemoryHandle* shared_memory_handle); |
45 | 48 |
46 // Call this to notify the manager that child process associated with | 49 // Call this to notify the manager that child process associated with |
50 // |process_handle| has deleted discardable memory segment with |id|. | |
51 void ChildDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id, | |
52 base::ProcessHandle process_handle); | |
53 | |
54 // Call this to notify the manager that child process associated with | |
47 // |process_handle| has been removed. The manager will use this to release | 55 // |process_handle| has been removed. The manager will use this to release |
48 // memory segments allocated for child process to the OS. | 56 // memory segments allocated for child process to the OS. |
49 void ProcessRemoved(base::ProcessHandle process_handle); | 57 void ProcessRemoved(base::ProcessHandle process_handle); |
50 | 58 |
51 // The maximum number of bytes of memory that may be allocated. This will | 59 // The maximum number of bytes of memory that may be allocated. This will |
52 // cause memory usage to be reduced if currently above |limit|. | 60 // cause memory usage to be reduced if currently above |limit|. |
53 void SetMemoryLimit(size_t limit); | 61 void SetMemoryLimit(size_t limit); |
54 | 62 |
55 // Reduce memory usage if above current memory limit. | 63 // Reduce memory usage if above current memory limit. |
56 void EnforceMemoryPolicy(); | 64 void EnforceMemoryPolicy(); |
57 | 65 |
66 // Returns bytes of allocated discardable memory. | |
67 size_t GetBytesAllocated(); | |
68 | |
58 private: | 69 private: |
59 struct MemorySegment { | 70 class MemorySegment : public base::RefCountedThreadSafe<MemorySegment> { |
60 MemorySegment(linked_ptr<base::DiscardableSharedMemory> memory, | 71 public: |
61 base::ProcessHandle process_handle); | 72 MemorySegment(scoped_ptr<base::DiscardableSharedMemory> memory); |
73 | |
74 base::DiscardableSharedMemory* memory() const { return memory_.get(); } | |
75 | |
76 private: | |
77 friend class base::RefCountedThreadSafe<MemorySegment>; | |
78 | |
62 ~MemorySegment(); | 79 ~MemorySegment(); |
63 | 80 |
64 linked_ptr<base::DiscardableSharedMemory> memory; | 81 scoped_ptr<base::DiscardableSharedMemory> memory_; |
65 base::ProcessHandle process_handle; | 82 |
83 DISALLOW_COPY_AND_ASSIGN(MemorySegment); | |
66 }; | 84 }; |
67 | 85 |
68 static bool CompareMemoryUsageTime(const MemorySegment& a, | 86 static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a, |
69 const MemorySegment& b) { | 87 const scoped_refptr<MemorySegment>& b) { |
70 // In this system, LRU memory segment is evicted first. | 88 // In this system, LRU memory segment is evicted first. |
71 return a.memory->last_known_usage() > b.memory->last_known_usage(); | 89 return a->memory()->last_known_usage() > b->memory()->last_known_usage(); |
72 } | 90 } |
73 | 91 |
74 void OnMemoryPressure( | 92 void OnMemoryPressure( |
75 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | 93 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); |
76 void ReduceMemoryUsageUntilWithinMemoryLimit(); | 94 void ReduceMemoryUsageUntilWithinMemoryLimit(); |
77 void ReduceMemoryUsageUntilWithinLimit(size_t limit); | 95 void ReduceMemoryUsageUntilWithinLimit(size_t limit); |
96 void ReleaseMemory(base::DiscardableSharedMemory* memory); | |
78 void BytesAllocatedChanged(size_t new_bytes_allocated) const; | 97 void BytesAllocatedChanged(size_t new_bytes_allocated) const; |
79 | 98 |
80 // Virtual for tests. | 99 // Virtual for tests. |
81 virtual base::Time Now() const; | 100 virtual base::Time Now() const; |
82 virtual void ScheduleEnforceMemoryPolicy(); | 101 virtual void ScheduleEnforceMemoryPolicy(); |
83 | 102 |
84 base::Lock lock_; | 103 base::Lock lock_; |
104 typedef base::hash_map<DiscardableSharedMemoryId, | |
105 scoped_refptr<MemorySegment>> MemorySegmentMap; | |
106 typedef base::hash_map<base::ProcessHandle, MemorySegmentMap> ProcessMap; | |
107 ProcessMap processes_; | |
85 // Note: The elements in |segments_| are arranged in such a way that they form | 108 // Note: The elements in |segments_| are arranged in such a way that they form |
86 // a heap. The LRU memory segment always first. | 109 // a heap. The LRU memory segment always first. |
87 typedef std::vector<MemorySegment> MemorySegmentVector; | 110 typedef std::vector<scoped_refptr<MemorySegment>> MemorySegmentVector; |
88 MemorySegmentVector segments_; | 111 MemorySegmentVector segments_; |
89 size_t memory_limit_; | 112 size_t memory_limit_; |
90 size_t bytes_allocated_; | 113 size_t bytes_allocated_; |
91 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 114 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
92 bool enforce_memory_policy_pending_; | 115 bool enforce_memory_policy_pending_; |
93 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_; | 116 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_; |
94 | 117 |
95 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager); | 118 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager); |
96 }; | 119 }; |
97 | 120 |
98 } // namespace content | 121 } // namespace content |
99 | 122 |
100 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ | 123 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ |
OLD | NEW |