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

Side by Side Diff: content/common/host_shared_bitmap_manager.cc

Issue 2535213002: [WIP] Add SharedMemoryTracker to dump base::SharedMemory usage
Patch Set: Implement buckets Created 3 years, 11 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/common/host_shared_bitmap_manager.h" 5 #include "content/common/host_shared_bitmap_manager.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory_tracker.h"
15 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
16 #include "base/trace_event/process_memory_dump.h" 17 #include "base/trace_event/process_memory_dump.h"
17 #include "build/build_config.h" 18 #include "build/build_config.h"
18 #include "content/common/view_messages.h" 19 #include "content/common/view_messages.h"
19 #include "ui/gfx/geometry/size.h" 20 #include "ui/gfx/geometry/size.h"
20 21
21 namespace content { 22 namespace content {
22 23
23 class BitmapData : public base::RefCountedThreadSafe<BitmapData> { 24 class BitmapData : public base::RefCountedThreadSafe<BitmapData> {
24 public: 25 public:
25 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {} 26 explicit BitmapData(size_t buffer_size) : buffer_size(buffer_size) {}
26 std::unique_ptr<base::SharedMemory> memory; 27 std::unique_ptr<base::SharedMemory> memory;
27 std::unique_ptr<uint8_t[]> pixels; 28 std::unique_ptr<uint8_t[]> pixels;
28 size_t buffer_size; 29 size_t buffer_size;
29 30
30 private: 31 private:
31 friend class base::RefCountedThreadSafe<BitmapData>; 32 friend class base::RefCountedThreadSafe<BitmapData>;
32 ~BitmapData() {} 33 ~BitmapData() {}
33 DISALLOW_COPY_AND_ASSIGN(BitmapData); 34 DISALLOW_COPY_AND_ASSIGN(BitmapData);
34 }; 35 };
35 36
36 namespace { 37 namespace {
37 38
38 class HostSharedBitmap : public cc::SharedBitmap { 39 class HostSharedBitmap : public cc::SharedBitmap {
39 public: 40 public:
40 HostSharedBitmap(uint8_t* pixels, 41 HostSharedBitmap(uint8_t* pixels,
41 scoped_refptr<BitmapData> bitmap_data, 42 scoped_refptr<BitmapData> bitmap_data,
42 const cc::SharedBitmapId& id, 43 const cc::SharedBitmapId& id,
43 HostSharedBitmapManager* manager) 44 HostSharedBitmapManager* manager)
44 : SharedBitmap(pixels, id), 45 : SharedBitmap(pixels, nullptr, id),
45 bitmap_data_(bitmap_data), 46 bitmap_data_(bitmap_data),
46 manager_(manager) {} 47 manager_(manager) {}
47 48
48 ~HostSharedBitmap() override { 49 ~HostSharedBitmap() override {
49 if (manager_) 50 if (manager_)
50 manager_->FreeSharedMemoryFromMap(id()); 51 manager_->FreeSharedMemoryFromMap(id());
51 } 52 }
52 53
53 private: 54 private:
54 scoped_refptr<BitmapData> bitmap_data_; 55 scoped_refptr<BitmapData> bitmap_data_;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 172
172 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, 173 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
173 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 174 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
174 bitmap.second->buffer_size); 175 bitmap.second->buffer_size);
175 176
176 // Generate a global GUID used to share this allocation with renderer 177 // Generate a global GUID used to share this allocation with renderer
177 // processes. 178 // processes.
178 auto guid = cc::GetSharedBitmapGUIDForTracing(bitmap.first); 179 auto guid = cc::GetSharedBitmapGUIDForTracing(bitmap.first);
179 pmd->CreateSharedGlobalAllocatorDump(guid); 180 pmd->CreateSharedGlobalAllocatorDump(guid);
180 pmd->AddOwnershipEdge(dump->guid(), guid); 181 pmd->AddOwnershipEdge(dump->guid(), guid);
182 base::SharedMemoryTracker::AddOwnershipEdgeToSharedGlobalDump(
183 pmd, guid,
184 bitmap.second->memory->handle(), bitmap.second->memory->mapped_size());
181 } 185 }
182 186
183 return true; 187 return true;
184 } 188 }
185 189
186 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap( 190 bool HostSharedBitmapManager::ChildAllocatedSharedBitmap(
187 size_t buffer_size, 191 size_t buffer_size,
188 const base::SharedMemoryHandle& handle, 192 const base::SharedMemoryHandle& handle,
189 const cc::SharedBitmapId& id) { 193 const cc::SharedBitmapId& id) {
190 base::AutoLock lock(lock_); 194 base::AutoLock lock(lock_);
(...skipping 27 matching lines...) Expand all
218 222
219 scoped_refptr<BitmapData> data(new BitmapData(buffer_size)); 223 scoped_refptr<BitmapData> data(new BitmapData(buffer_size));
220 data->memory = std::move(shared_memory); 224 data->memory = std::move(shared_memory);
221 225
222 handle_map_[id] = data; 226 handle_map_[id] = data;
223 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) { 227 if (!data->memory->ShareToProcess(process_handle, shared_memory_handle)) {
224 LOG(ERROR) << "Cannot share shared memory buffer"; 228 LOG(ERROR) << "Cannot share shared memory buffer";
225 *shared_memory_handle = base::SharedMemory::NULLHandle(); 229 *shared_memory_handle = base::SharedMemory::NULLHandle();
226 return; 230 return;
227 } 231 }
228 data->memory->Close(); 232 data->memory->Close();
229 } 233 }
230 234
231 void HostSharedBitmapManager::ChildDeletedSharedBitmap( 235 void HostSharedBitmapManager::ChildDeletedSharedBitmap(
232 const cc::SharedBitmapId& id) { 236 const cc::SharedBitmapId& id) {
233 base::AutoLock lock(lock_); 237 base::AutoLock lock(lock_);
234 handle_map_.erase(id); 238 handle_map_.erase(id);
235 } 239 }
236 240
237 size_t HostSharedBitmapManager::AllocatedBitmapCount() const { 241 size_t HostSharedBitmapManager::AllocatedBitmapCount() const {
238 base::AutoLock lock(lock_); 242 base::AutoLock lock(lock_);
239 return handle_map_.size(); 243 return handle_map_.size();
240 } 244 }
241 245
242 void HostSharedBitmapManager::FreeSharedMemoryFromMap( 246 void HostSharedBitmapManager::FreeSharedMemoryFromMap(
243 const cc::SharedBitmapId& id) { 247 const cc::SharedBitmapId& id) {
244 base::AutoLock lock(lock_); 248 base::AutoLock lock(lock_);
245 handle_map_.erase(id); 249 handle_map_.erase(id);
246 } 250 }
247 251
248 } // namespace content 252 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698