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

Side by Side Diff: gpu/command_buffer/client/mapped_memory.cc

Issue 23130004: Enforce a memory limit on MappedMemoryManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More code review feedback incorporated Created 7 years, 4 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "gpu/command_buffer/client/mapped_memory.h" 5 #include "gpu/command_buffer/client/mapped_memory.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 9
10 #include "gpu/command_buffer/client/cmd_buffer_helper.h" 10 #include "gpu/command_buffer/client/cmd_buffer_helper.h"
11 11
12 namespace gpu { 12 namespace gpu {
13 13
14 MemoryChunk::MemoryChunk( 14 MemoryChunk::MemoryChunk(
15 int32 shm_id, gpu::Buffer shm, CommandBufferHelper* helper) 15 int32 shm_id, gpu::Buffer shm, CommandBufferHelper* helper)
16 : shm_id_(shm_id), 16 : shm_id_(shm_id),
17 shm_(shm), 17 shm_(shm),
18 allocator_(shm.size, helper, shm.ptr) { 18 allocator_(shm.size, helper, shm.ptr) {
19 } 19 }
20 20
21 MappedMemoryManager::MappedMemoryManager(CommandBufferHelper* helper) 21 MappedMemoryManager::MappedMemoryManager(CommandBufferHelper* helper)
22 : chunk_size_multiple_(1), 22 : chunk_size_multiple_(1),
23 helper_(helper) { 23 helper_(helper),
24 allocated_memory_(0),
25 memory_limit_(kNoLimit) {
26 }
27
28 MappedMemoryManager::MappedMemoryManager(CommandBufferHelper* helper,
no sievers 2013/08/19 21:57:44 Let's not add a new constructor for that. It just
kaanb 2013/08/20 01:23:43 Done.
29 size_t memory_limit)
30 : chunk_size_multiple_(1),
31 helper_(helper),
32 allocated_memory_(0),
33 memory_limit_(memory_limit) {
24 } 34 }
25 35
26 MappedMemoryManager::~MappedMemoryManager() { 36 MappedMemoryManager::~MappedMemoryManager() {
27 CommandBuffer* cmd_buf = helper_->command_buffer(); 37 CommandBuffer* cmd_buf = helper_->command_buffer();
28 for (MemoryChunkVector::iterator iter = chunks_.begin(); 38 for (MemoryChunkVector::iterator iter = chunks_.begin();
29 iter != chunks_.end(); ++iter) { 39 iter != chunks_.end(); ++iter) {
30 MemoryChunk* chunk = *iter; 40 MemoryChunk* chunk = *iter;
31 cmd_buf->DestroyTransferBuffer(chunk->shm_id()); 41 cmd_buf->DestroyTransferBuffer(chunk->shm_id());
32 } 42 }
33 } 43 }
34 44
35 void* MappedMemoryManager::Alloc( 45 void* MappedMemoryManager::Alloc(
36 unsigned int size, int32* shm_id, unsigned int* shm_offset) { 46 unsigned int size, int32* shm_id, unsigned int* shm_offset) {
37 GPU_DCHECK(shm_id); 47 GPU_DCHECK(shm_id);
38 GPU_DCHECK(shm_offset); 48 GPU_DCHECK(shm_offset);
39 // See if any of the chucks can satisfy this request. 49 // See if any of the chunks can satisfy this request.
40 for (size_t ii = 0; ii < chunks_.size(); ++ii) { 50 for (size_t ii = 0; ii < chunks_.size(); ++ii) {
41 MemoryChunk* chunk = chunks_[ii]; 51 MemoryChunk* chunk = chunks_[ii];
42 chunk->FreeUnused(); 52 chunk->FreeUnused();
43 if (chunk->GetLargestFreeSizeWithoutWaiting() >= size) { 53 if (chunk->GetLargestFreeSizeWithoutWaiting() >= size) {
44 void* mem = chunk->Alloc(size); 54 void* mem = chunk->Alloc(size);
45 GPU_DCHECK(mem); 55 GPU_DCHECK(mem);
46 *shm_id = chunk->shm_id(); 56 *shm_id = chunk->shm_id();
47 *shm_offset = chunk->GetOffset(mem); 57 *shm_offset = chunk->GetOffset(mem);
48 return mem; 58 return mem;
49 } 59 }
50 } 60 }
51 61
62 // If there is a memory limit being enforced and the allocated memory
63 // is above the limit, try existing chunks again with waiting.
epenner 2013/08/19 22:05:52 This should be rare right? Should we add a trace a
kaanb 2013/08/20 01:23:43 See line 76
64 if (memory_limit_ != kNoLimit && allocated_memory_ >= memory_limit_) {
no sievers 2013/08/19 21:57:44 What do you all think about looking at the free+fr
kaanb 2013/08/20 01:23:43 Done.
65 for (size_t ii = 0; ii < chunks_.size(); ++ii) {
66 MemoryChunk* chunk = chunks_[ii];
67 chunk->FreeUnused();
68 if (chunk->GetLargestFreeSizeWithWaiting() >= size) {
69 void* mem = chunk->Alloc(size);
70 GPU_DCHECK(mem);
71 *shm_id = chunk->shm_id();
72 *shm_offset = chunk->GetOffset(mem);
73 return mem;
74 }
75 }
76 LOG(INFO) << "Exceeding memory limit: " << memory_limit_;
77 }
78
52 // Make a new chunk to satisfy the request. 79 // Make a new chunk to satisfy the request.
53 CommandBuffer* cmd_buf = helper_->command_buffer(); 80 CommandBuffer* cmd_buf = helper_->command_buffer();
54 unsigned int chunk_size = 81 unsigned int chunk_size =
55 ((size + chunk_size_multiple_ - 1) / chunk_size_multiple_) * 82 ((size + chunk_size_multiple_ - 1) / chunk_size_multiple_) *
56 chunk_size_multiple_; 83 chunk_size_multiple_;
57 int32 id = -1; 84 int32 id = -1;
58 gpu::Buffer shm = cmd_buf->CreateTransferBuffer(chunk_size, &id); 85 gpu::Buffer shm = cmd_buf->CreateTransferBuffer(chunk_size, &id);
59 if (id < 0) 86 if (id < 0)
60 return NULL; 87 return NULL;
61 MemoryChunk* mc = new MemoryChunk(id, shm, helper_); 88 MemoryChunk* mc = new MemoryChunk(id, shm, helper_);
89 allocated_memory_ += mc->GetSize();
62 chunks_.push_back(mc); 90 chunks_.push_back(mc);
63 void* mem = mc->Alloc(size); 91 void* mem = mc->Alloc(size);
64 GPU_DCHECK(mem); 92 GPU_DCHECK(mem);
65 *shm_id = mc->shm_id(); 93 *shm_id = mc->shm_id();
66 *shm_offset = mc->GetOffset(mem); 94 *shm_offset = mc->GetOffset(mem);
67 return mem; 95 return mem;
68 } 96 }
69 97
70 void MappedMemoryManager::Free(void* pointer) { 98 void MappedMemoryManager::Free(void* pointer) {
71 for (size_t ii = 0; ii < chunks_.size(); ++ii) { 99 for (size_t ii = 0; ii < chunks_.size(); ++ii) {
(...skipping 18 matching lines...) Expand all
90 } 118 }
91 119
92 void MappedMemoryManager::FreeUnused() { 120 void MappedMemoryManager::FreeUnused() {
93 CommandBuffer* cmd_buf = helper_->command_buffer(); 121 CommandBuffer* cmd_buf = helper_->command_buffer();
94 MemoryChunkVector::iterator iter = chunks_.begin(); 122 MemoryChunkVector::iterator iter = chunks_.begin();
95 while (iter != chunks_.end()) { 123 while (iter != chunks_.end()) {
96 MemoryChunk* chunk = *iter; 124 MemoryChunk* chunk = *iter;
97 chunk->FreeUnused(); 125 chunk->FreeUnused();
98 if (!chunk->InUse()) { 126 if (!chunk->InUse()) {
99 cmd_buf->DestroyTransferBuffer(chunk->shm_id()); 127 cmd_buf->DestroyTransferBuffer(chunk->shm_id());
128 allocated_memory_ -= chunk->GetSize();
100 iter = chunks_.erase(iter); 129 iter = chunks_.erase(iter);
101 } else { 130 } else {
102 ++iter; 131 ++iter;
103 } 132 }
104 } 133 }
105 } 134 }
106 135
107 } // namespace gpu 136 } // namespace gpu
108
109
110
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698