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.
5 #ifndef CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
6 #define CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
10 #include "base/containers/hash_tables.h"
11 #include "base/format_macros.h"
12 #include "base/memory/discardable_memory_allocator.h"
13 #include "base/memory/discardable_shared_memory.h"
14 #include "base/memory/memory_pressure_listener.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/shared_memory.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/process/process_handle.h"
19 #include "base/synchronization/lock.h"
20 #include "base/trace_event/memory_dump_provider.h"
21 #include "content/common/content_export.h"
24 typedef int32_t DiscardableSharedMemoryId
;
26 // Implementation of DiscardableMemoryAllocator that allocates and manages
27 // discardable memory segments for the browser process and child processes.
28 // This class is thread-safe and instances can safely be used on any thread.
29 class CONTENT_EXPORT HostDiscardableSharedMemoryManager
30 : public base::DiscardableMemoryAllocator
,
31 public base::trace_event::MemoryDumpProvider
{
33 HostDiscardableSharedMemoryManager();
34 ~HostDiscardableSharedMemoryManager() override
;
36 // Returns a singleton instance.
37 static HostDiscardableSharedMemoryManager
* current();
39 // Overridden from base::DiscardableMemoryAllocator:
40 scoped_ptr
<base::DiscardableMemory
> AllocateLockedDiscardableMemory(
41 size_t size
) override
;
43 // Overridden from base::trace_event::MemoryDumpProvider:
44 bool OnMemoryDump(base::trace_event::ProcessMemoryDump
* pmd
) override
;
46 // This allocates a discardable memory segment for |process_handle|.
47 // A valid shared memory handle is returned on success.
48 void AllocateLockedDiscardableSharedMemoryForChild(
49 base::ProcessHandle process_handle
,
52 DiscardableSharedMemoryId id
,
53 base::SharedMemoryHandle
* shared_memory_handle
);
55 // Call this to notify the manager that child process associated with
56 // |child_process_id| has deleted discardable memory segment with |id|.
57 void ChildDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id
,
58 int child_process_id
);
60 // Call this to notify the manager that child process associated with
61 // |child_process_id| has been removed. The manager will use this to release
62 // memory segments allocated for child process to the OS.
63 void ProcessRemoved(int child_process_id
);
65 // The maximum number of bytes of memory that may be allocated. This will
66 // cause memory usage to be reduced if currently above |limit|.
67 void SetMemoryLimit(size_t limit
);
69 // Reduce memory usage if above current memory limit.
70 void EnforceMemoryPolicy();
72 // Returns bytes of allocated discardable memory.
73 size_t GetBytesAllocated();
76 class MemorySegment
: public base::RefCountedThreadSafe
<MemorySegment
> {
78 MemorySegment(scoped_ptr
<base::DiscardableSharedMemory
> memory
);
80 base::DiscardableSharedMemory
* memory() const { return memory_
.get(); }
83 friend class base::RefCountedThreadSafe
<MemorySegment
>;
87 scoped_ptr
<base::DiscardableSharedMemory
> memory_
;
89 DISALLOW_COPY_AND_ASSIGN(MemorySegment
);
92 static bool CompareMemoryUsageTime(const scoped_refptr
<MemorySegment
>& a
,
93 const scoped_refptr
<MemorySegment
>& b
) {
94 // In this system, LRU memory segment is evicted first.
95 return a
->memory()->last_known_usage() > b
->memory()->last_known_usage();
98 void AllocateLockedDiscardableSharedMemory(
99 base::ProcessHandle process_handle
,
100 int client_process_id
,
102 DiscardableSharedMemoryId id
,
103 base::SharedMemoryHandle
* shared_memory_handle
);
104 void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id
,
105 int client_process_id
);
106 void OnMemoryPressure(
107 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level
);
108 void ReduceMemoryUsageUntilWithinMemoryLimit();
109 void ReduceMemoryUsageUntilWithinLimit(size_t limit
);
110 void ReleaseMemory(base::DiscardableSharedMemory
* memory
);
111 void BytesAllocatedChanged(size_t new_bytes_allocated
) const;
113 // Virtual for tests.
114 virtual base::Time
Now() const;
115 virtual void ScheduleEnforceMemoryPolicy();
118 typedef base::hash_map
<DiscardableSharedMemoryId
,
119 scoped_refptr
<MemorySegment
>> MemorySegmentMap
;
120 typedef base::hash_map
<int, MemorySegmentMap
> ProcessMap
;
121 ProcessMap processes_
;
122 // Note: The elements in |segments_| are arranged in such a way that they form
123 // a heap. The LRU memory segment always first.
124 typedef std::vector
<scoped_refptr
<MemorySegment
>> MemorySegmentVector
;
125 MemorySegmentVector segments_
;
126 size_t memory_limit_
;
127 size_t bytes_allocated_
;
128 scoped_ptr
<base::MemoryPressureListener
> memory_pressure_listener_
;
129 bool enforce_memory_policy_pending_
;
130 base::WeakPtrFactory
<HostDiscardableSharedMemoryManager
> weak_ptr_factory_
;
132 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager
);
135 } // namespace content
137 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_