gpu: Add memory tracing of GPU transfer buffers.
[chromium-blink-merge.git] / content / common / host_discardable_shared_memory_manager.h
blob00ba1b51efa87144b3c2aa2aa766ed392cd9b607
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/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"
23 namespace content {
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 {
32 public:
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(const base::trace_event::MemoryDumpArgs& args,
45 base::trace_event::ProcessMemoryDump* pmd) override;
47 // This allocates a discardable memory segment for |process_handle|.
48 // A valid shared memory handle is returned on success.
49 void AllocateLockedDiscardableSharedMemoryForChild(
50 base::ProcessHandle process_handle,
51 int child_process_id,
52 size_t size,
53 DiscardableSharedMemoryId id,
54 base::SharedMemoryHandle* shared_memory_handle);
56 // Call this to notify the manager that child process associated with
57 // |child_process_id| has deleted discardable memory segment with |id|.
58 void ChildDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id,
59 int child_process_id);
61 // Call this to notify the manager that child process associated with
62 // |child_process_id| has been removed. The manager will use this to release
63 // memory segments allocated for child process to the OS.
64 void ProcessRemoved(int child_process_id);
66 // The maximum number of bytes of memory that may be allocated. This will
67 // cause memory usage to be reduced if currently above |limit|.
68 void SetMemoryLimit(size_t limit);
70 // Reduce memory usage if above current memory limit.
71 void EnforceMemoryPolicy();
73 // Returns bytes of allocated discardable memory.
74 size_t GetBytesAllocated();
76 private:
77 class MemorySegment : public base::RefCountedThreadSafe<MemorySegment> {
78 public:
79 MemorySegment(scoped_ptr<base::DiscardableSharedMemory> memory);
81 base::DiscardableSharedMemory* memory() const { return memory_.get(); }
83 private:
84 friend class base::RefCountedThreadSafe<MemorySegment>;
86 ~MemorySegment();
88 scoped_ptr<base::DiscardableSharedMemory> memory_;
90 DISALLOW_COPY_AND_ASSIGN(MemorySegment);
93 static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a,
94 const scoped_refptr<MemorySegment>& b) {
95 // In this system, LRU memory segment is evicted first.
96 return a->memory()->last_known_usage() > b->memory()->last_known_usage();
99 void AllocateLockedDiscardableSharedMemory(
100 base::ProcessHandle process_handle,
101 int client_process_id,
102 size_t size,
103 DiscardableSharedMemoryId id,
104 base::SharedMemoryHandle* shared_memory_handle);
105 void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id,
106 int client_process_id);
107 void OnMemoryPressure(
108 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
109 void ReduceMemoryUsageUntilWithinMemoryLimit();
110 void ReduceMemoryUsageUntilWithinLimit(size_t limit);
111 void ReleaseMemory(base::DiscardableSharedMemory* memory);
112 void BytesAllocatedChanged(size_t new_bytes_allocated) const;
114 // Virtual for tests.
115 virtual base::Time Now() const;
116 virtual void ScheduleEnforceMemoryPolicy();
118 base::Lock lock_;
119 typedef base::hash_map<DiscardableSharedMemoryId,
120 scoped_refptr<MemorySegment>> MemorySegmentMap;
121 typedef base::hash_map<int, MemorySegmentMap> ProcessMap;
122 ProcessMap processes_;
123 // Note: The elements in |segments_| are arranged in such a way that they form
124 // a heap. The LRU memory segment always first.
125 typedef std::vector<scoped_refptr<MemorySegment>> MemorySegmentVector;
126 MemorySegmentVector segments_;
127 size_t memory_limit_;
128 size_t bytes_allocated_;
129 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
130 bool enforce_memory_policy_pending_;
131 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_;
133 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager);
136 } // namespace content
138 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_