Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: content/common/host_discardable_shared_memory_manager.h

Issue 1009203004: content: Add DeletedDiscardableSharedMemory IPC. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_t DiscardableSharedMemoryId;
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 AllocateLockedDiscardableSharedMemory( 92 void AllocateLockedDiscardableSharedMemory(
75 base::ProcessHandle process_handle, 93 base::ProcessHandle process_handle,
76 size_t size, 94 size_t size,
95 DiscardableSharedMemoryId id,
77 base::SharedMemoryHandle* shared_memory_handle); 96 base::SharedMemoryHandle* shared_memory_handle);
97 void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id,
98 base::ProcessHandle process_handle);
78 void OnMemoryPressure( 99 void OnMemoryPressure(
79 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); 100 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
80 void ReduceMemoryUsageUntilWithinMemoryLimit(); 101 void ReduceMemoryUsageUntilWithinMemoryLimit();
81 void ReduceMemoryUsageUntilWithinLimit(size_t limit); 102 void ReduceMemoryUsageUntilWithinLimit(size_t limit);
103 void ReleaseMemory(base::DiscardableSharedMemory* memory);
82 void BytesAllocatedChanged(size_t new_bytes_allocated) const; 104 void BytesAllocatedChanged(size_t new_bytes_allocated) const;
83 105
84 // Virtual for tests. 106 // Virtual for tests.
85 virtual base::Time Now() const; 107 virtual base::Time Now() const;
86 virtual void ScheduleEnforceMemoryPolicy(); 108 virtual void ScheduleEnforceMemoryPolicy();
87 109
88 base::Lock lock_; 110 base::Lock lock_;
111 typedef base::hash_map<DiscardableSharedMemoryId,
112 scoped_refptr<MemorySegment>> MemorySegmentMap;
113 typedef base::hash_map<base::ProcessHandle, MemorySegmentMap> ProcessMap;
114 ProcessMap processes_;
89 // Note: The elements in |segments_| are arranged in such a way that they form 115 // Note: The elements in |segments_| are arranged in such a way that they form
90 // a heap. The LRU memory segment always first. 116 // a heap. The LRU memory segment always first.
91 typedef std::vector<MemorySegment> MemorySegmentVector; 117 typedef std::vector<scoped_refptr<MemorySegment>> MemorySegmentVector;
92 MemorySegmentVector segments_; 118 MemorySegmentVector segments_;
93 size_t memory_limit_; 119 size_t memory_limit_;
94 size_t bytes_allocated_; 120 size_t bytes_allocated_;
95 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_; 121 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
96 bool enforce_memory_policy_pending_; 122 bool enforce_memory_policy_pending_;
97 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_; 123 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_;
98 124
99 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager); 125 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager);
100 }; 126 };
101 127
102 } // namespace content 128 } // namespace content
103 129
104 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ 130 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
OLDNEW
« no previous file with comments | « content/common/discardable_shared_memory_heap_unittest.cc ('k') | content/common/host_discardable_shared_memory_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698