| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #include "gpu/ipc/service/gpu_memory_manager.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/process/process_handle.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/trace_event/trace_event.h" | |
| 15 #include "gpu/command_buffer/common/gpu_memory_allocation.h" | |
| 16 #include "gpu/command_buffer/service/gpu_switches.h" | |
| 17 #include "gpu/ipc/common/gpu_memory_uma_stats.h" | |
| 18 #include "gpu/ipc/common/memory_stats.h" | |
| 19 #include "gpu/ipc/service/gpu_channel_manager.h" | |
| 20 #include "gpu/ipc/service/gpu_channel_manager_delegate.h" | |
| 21 #include "gpu/ipc/service/gpu_memory_tracking.h" | |
| 22 | |
| 23 namespace gpu { | |
| 24 namespace { | |
| 25 | |
| 26 const uint64_t kBytesAllocatedStep = 16 * 1024 * 1024; | |
| 27 | |
| 28 void TrackValueChanged(uint64_t old_size, | |
| 29 uint64_t new_size, | |
| 30 uint64_t* total_size) { | |
| 31 DCHECK(new_size > old_size || *total_size >= (old_size - new_size)); | |
| 32 *total_size += (new_size - old_size); | |
| 33 } | |
| 34 | |
| 35 } | |
| 36 | |
| 37 GpuMemoryManager::GpuMemoryManager(GpuChannelManager* channel_manager) | |
| 38 : channel_manager_(channel_manager), | |
| 39 bytes_allocated_current_(0), | |
| 40 bytes_allocated_historical_max_(0) {} | |
| 41 | |
| 42 GpuMemoryManager::~GpuMemoryManager() { | |
| 43 DCHECK(tracking_groups_.empty()); | |
| 44 DCHECK(!bytes_allocated_current_); | |
| 45 } | |
| 46 | |
| 47 void GpuMemoryManager::TrackMemoryAllocatedChange( | |
| 48 GpuMemoryTrackingGroup* tracking_group, | |
| 49 uint64_t old_size, | |
| 50 uint64_t new_size) { | |
| 51 TrackValueChanged(old_size, new_size, &tracking_group->size_); | |
| 52 TrackValueChanged(old_size, new_size, &bytes_allocated_current_); | |
| 53 | |
| 54 if (GetCurrentUsage() > bytes_allocated_historical_max_ + | |
| 55 kBytesAllocatedStep) { | |
| 56 bytes_allocated_historical_max_ = GetCurrentUsage(); | |
| 57 // If we're blowing into new memory usage territory, spam the browser | |
| 58 // process with the most up-to-date information about our memory usage. | |
| 59 SendUmaStatsToHost(); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 bool GpuMemoryManager::EnsureGPUMemoryAvailable(uint64_t /* size_needed */) { | |
| 64 // TODO: Check if there is enough space. Lose contexts until there is. | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 uint64_t GpuMemoryManager::GetTrackerMemoryUsage( | |
| 69 gles2::MemoryTracker* tracker) const { | |
| 70 TrackingGroupMap::const_iterator tracking_group_it = | |
| 71 tracking_groups_.find(tracker); | |
| 72 DCHECK(tracking_group_it != tracking_groups_.end()); | |
| 73 return tracking_group_it->second->GetSize(); | |
| 74 } | |
| 75 | |
| 76 GpuMemoryTrackingGroup* GpuMemoryManager::CreateTrackingGroup( | |
| 77 base::ProcessId pid, gles2::MemoryTracker* memory_tracker) { | |
| 78 GpuMemoryTrackingGroup* tracking_group = new GpuMemoryTrackingGroup( | |
| 79 pid, memory_tracker, this); | |
| 80 DCHECK(!tracking_groups_.count(tracking_group->GetMemoryTracker())); | |
| 81 tracking_groups_.insert(std::make_pair(tracking_group->GetMemoryTracker(), | |
| 82 tracking_group)); | |
| 83 return tracking_group; | |
| 84 } | |
| 85 | |
| 86 void GpuMemoryManager::OnDestroyTrackingGroup( | |
| 87 GpuMemoryTrackingGroup* tracking_group) { | |
| 88 DCHECK(tracking_groups_.count(tracking_group->GetMemoryTracker())); | |
| 89 tracking_groups_.erase(tracking_group->GetMemoryTracker()); | |
| 90 } | |
| 91 | |
| 92 void GpuMemoryManager::GetVideoMemoryUsageStats( | |
| 93 VideoMemoryUsageStats* video_memory_usage_stats) const { | |
| 94 // For each context group, assign its memory usage to its PID | |
| 95 video_memory_usage_stats->process_map.clear(); | |
| 96 for (TrackingGroupMap::const_iterator i = | |
| 97 tracking_groups_.begin(); i != tracking_groups_.end(); ++i) { | |
| 98 const GpuMemoryTrackingGroup* tracking_group = i->second; | |
| 99 video_memory_usage_stats->process_map[ | |
| 100 tracking_group->GetPid()].video_memory += tracking_group->GetSize(); | |
| 101 } | |
| 102 | |
| 103 // Assign the total across all processes in the GPU process | |
| 104 video_memory_usage_stats->process_map[ | |
| 105 base::GetCurrentProcId()].video_memory = GetCurrentUsage(); | |
| 106 video_memory_usage_stats->process_map[ | |
| 107 base::GetCurrentProcId()].has_duplicates = true; | |
| 108 | |
| 109 video_memory_usage_stats->bytes_allocated = GetCurrentUsage(); | |
| 110 video_memory_usage_stats->bytes_allocated_historical_max = | |
| 111 bytes_allocated_historical_max_; | |
| 112 } | |
| 113 | |
| 114 void GpuMemoryManager::SendUmaStatsToHost() { | |
| 115 if (!channel_manager_) | |
| 116 return; | |
| 117 GPUMemoryUmaStats params; | |
| 118 params.bytes_allocated_current = GetCurrentUsage(); | |
| 119 params.bytes_allocated_max = bytes_allocated_historical_max_; | |
| 120 params.context_group_count = static_cast<uint32_t>(tracking_groups_.size()); | |
| 121 channel_manager_->delegate()->GpuMemoryUmaStats(params); | |
| 122 } | |
| 123 } // namespace gpu | |
| OLD | NEW |