Convert events_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / content / common / host_discardable_shared_memory_manager.h
blobe197cf9072fc948604246e998f07fb9ad07d1bf1
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_
8 #include <vector>
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/discardable_memory_allocator.h"
12 #include "base/memory/discardable_shared_memory.h"
13 #include "base/memory/memory_pressure_listener.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/process/process_handle.h"
18 #include "base/synchronization/lock.h"
19 #include "content/common/content_export.h"
21 namespace content {
22 typedef int32_t DiscardableSharedMemoryId;
24 // Implementation of DiscardableMemoryAllocator that allocates and manages
25 // discardable memory segments for the browser process and child processes.
26 // This class is thread-safe and instances can safely be used on any thread.
27 class CONTENT_EXPORT HostDiscardableSharedMemoryManager
28 : public base::DiscardableMemoryAllocator {
29 public:
30 HostDiscardableSharedMemoryManager();
31 ~HostDiscardableSharedMemoryManager() override;
33 // Returns a singleton instance.
34 static HostDiscardableSharedMemoryManager* current();
36 // Overridden from base::DiscardableMemoryAllocator:
37 scoped_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory(
38 size_t size) override;
40 // This allocates a discardable memory segment for |process_handle|.
41 // A valid shared memory handle is returned on success.
42 void AllocateLockedDiscardableSharedMemoryForChild(
43 base::ProcessHandle process_handle,
44 int child_process_id,
45 size_t size,
46 DiscardableSharedMemoryId id,
47 base::SharedMemoryHandle* shared_memory_handle);
49 // Call this to notify the manager that child process associated with
50 // |child_process_id| has deleted discardable memory segment with |id|.
51 void ChildDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id,
52 int child_process_id);
54 // Call this to notify the manager that child process associated with
55 // |child_process_id| has been removed. The manager will use this to release
56 // memory segments allocated for child process to the OS.
57 void ProcessRemoved(int child_process_id);
59 // The maximum number of bytes of memory that may be allocated. This will
60 // cause memory usage to be reduced if currently above |limit|.
61 void SetMemoryLimit(size_t limit);
63 // Reduce memory usage if above current memory limit.
64 void EnforceMemoryPolicy();
66 // Returns bytes of allocated discardable memory.
67 size_t GetBytesAllocated();
69 private:
70 class MemorySegment : public base::RefCountedThreadSafe<MemorySegment> {
71 public:
72 MemorySegment(scoped_ptr<base::DiscardableSharedMemory> memory);
74 base::DiscardableSharedMemory* memory() const { return memory_.get(); }
76 private:
77 friend class base::RefCountedThreadSafe<MemorySegment>;
79 ~MemorySegment();
81 scoped_ptr<base::DiscardableSharedMemory> memory_;
83 DISALLOW_COPY_AND_ASSIGN(MemorySegment);
86 static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a,
87 const scoped_refptr<MemorySegment>& b) {
88 // In this system, LRU memory segment is evicted first.
89 return a->memory()->last_known_usage() > b->memory()->last_known_usage();
92 void AllocateLockedDiscardableSharedMemory(
93 base::ProcessHandle process_handle,
94 int client_process_id,
95 size_t size,
96 DiscardableSharedMemoryId id,
97 base::SharedMemoryHandle* shared_memory_handle);
98 void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id,
99 int client_process_id);
100 void OnMemoryPressure(
101 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
102 void ReduceMemoryUsageUntilWithinMemoryLimit();
103 void ReduceMemoryUsageUntilWithinLimit(size_t limit);
104 void ReleaseMemory(base::DiscardableSharedMemory* memory);
105 void BytesAllocatedChanged(size_t new_bytes_allocated) const;
107 // Virtual for tests.
108 virtual base::Time Now() const;
109 virtual void ScheduleEnforceMemoryPolicy();
111 base::Lock lock_;
112 typedef base::hash_map<DiscardableSharedMemoryId,
113 scoped_refptr<MemorySegment>> MemorySegmentMap;
114 typedef base::hash_map<int, MemorySegmentMap> ProcessMap;
115 ProcessMap processes_;
116 // Note: The elements in |segments_| are arranged in such a way that they form
117 // a heap. The LRU memory segment always first.
118 typedef std::vector<scoped_refptr<MemorySegment>> MemorySegmentVector;
119 MemorySegmentVector segments_;
120 size_t memory_limit_;
121 size_t bytes_allocated_;
122 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
123 bool enforce_memory_policy_pending_;
124 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_;
126 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager);
129 } // namespace content
131 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_