| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project 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 "src/compiler/zone-stats.h" | 5 #include "src/compiler/zone-pool.h" |
| 6 | 6 |
| 7 namespace v8 { | 7 namespace v8 { |
| 8 namespace internal { | 8 namespace internal { |
| 9 namespace compiler { | 9 namespace compiler { |
| 10 | 10 |
| 11 ZoneStats::StatsScope::StatsScope(ZoneStats* zone_stats) | 11 ZonePool::StatsScope::StatsScope(ZonePool* zone_pool) |
| 12 : zone_stats_(zone_stats), | 12 : zone_pool_(zone_pool), |
| 13 total_allocated_bytes_at_start_(zone_stats->GetTotalAllocatedBytes()), | 13 total_allocated_bytes_at_start_(zone_pool->GetTotalAllocatedBytes()), |
| 14 max_allocated_bytes_(0) { | 14 max_allocated_bytes_(0) { |
| 15 zone_stats_->stats_.push_back(this); | 15 zone_pool_->stats_.push_back(this); |
| 16 for (Zone* zone : zone_stats_->zones_) { | 16 for (Zone* zone : zone_pool_->used_) { |
| 17 size_t size = static_cast<size_t>(zone->allocation_size()); | 17 size_t size = static_cast<size_t>(zone->allocation_size()); |
| 18 std::pair<InitialValues::iterator, bool> res = | 18 std::pair<InitialValues::iterator, bool> res = |
| 19 initial_values_.insert(std::make_pair(zone, size)); | 19 initial_values_.insert(std::make_pair(zone, size)); |
| 20 USE(res); | 20 USE(res); |
| 21 DCHECK(res.second); | 21 DCHECK(res.second); |
| 22 } | 22 } |
| 23 } | 23 } |
| 24 | 24 |
| 25 ZoneStats::StatsScope::~StatsScope() { | 25 |
| 26 DCHECK_EQ(zone_stats_->stats_.back(), this); | 26 ZonePool::StatsScope::~StatsScope() { |
| 27 zone_stats_->stats_.pop_back(); | 27 DCHECK_EQ(zone_pool_->stats_.back(), this); |
| 28 zone_pool_->stats_.pop_back(); |
| 28 } | 29 } |
| 29 | 30 |
| 30 size_t ZoneStats::StatsScope::GetMaxAllocatedBytes() { | 31 |
| 32 size_t ZonePool::StatsScope::GetMaxAllocatedBytes() { |
| 31 return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes()); | 33 return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes()); |
| 32 } | 34 } |
| 33 | 35 |
| 34 size_t ZoneStats::StatsScope::GetCurrentAllocatedBytes() { | 36 |
| 37 size_t ZonePool::StatsScope::GetCurrentAllocatedBytes() { |
| 35 size_t total = 0; | 38 size_t total = 0; |
| 36 for (Zone* zone : zone_stats_->zones_) { | 39 for (Zone* zone : zone_pool_->used_) { |
| 37 total += static_cast<size_t>(zone->allocation_size()); | 40 total += static_cast<size_t>(zone->allocation_size()); |
| 38 // Adjust for initial values. | 41 // Adjust for initial values. |
| 39 InitialValues::iterator it = initial_values_.find(zone); | 42 InitialValues::iterator it = initial_values_.find(zone); |
| 40 if (it != initial_values_.end()) { | 43 if (it != initial_values_.end()) { |
| 41 total -= it->second; | 44 total -= it->second; |
| 42 } | 45 } |
| 43 } | 46 } |
| 44 return total; | 47 return total; |
| 45 } | 48 } |
| 46 | 49 |
| 47 size_t ZoneStats::StatsScope::GetTotalAllocatedBytes() { | 50 |
| 48 return zone_stats_->GetTotalAllocatedBytes() - | 51 size_t ZonePool::StatsScope::GetTotalAllocatedBytes() { |
| 49 total_allocated_bytes_at_start_; | 52 return zone_pool_->GetTotalAllocatedBytes() - total_allocated_bytes_at_start_; |
| 50 } | 53 } |
| 51 | 54 |
| 52 void ZoneStats::StatsScope::ZoneReturned(Zone* zone) { | 55 |
| 56 void ZonePool::StatsScope::ZoneReturned(Zone* zone) { |
| 53 size_t current_total = GetCurrentAllocatedBytes(); | 57 size_t current_total = GetCurrentAllocatedBytes(); |
| 54 // Update max. | 58 // Update max. |
| 55 max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total); | 59 max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total); |
| 56 // Drop zone from initial value map. | 60 // Drop zone from initial value map. |
| 57 InitialValues::iterator it = initial_values_.find(zone); | 61 InitialValues::iterator it = initial_values_.find(zone); |
| 58 if (it != initial_values_.end()) { | 62 if (it != initial_values_.end()) { |
| 59 initial_values_.erase(it); | 63 initial_values_.erase(it); |
| 60 } | 64 } |
| 61 } | 65 } |
| 62 | 66 |
| 63 ZoneStats::ZoneStats(AccountingAllocator* allocator) | 67 ZonePool::ZonePool(AccountingAllocator* allocator) |
| 64 : max_allocated_bytes_(0), total_deleted_bytes_(0), allocator_(allocator) {} | 68 : max_allocated_bytes_(0), total_deleted_bytes_(0), allocator_(allocator) {} |
| 65 | 69 |
| 66 ZoneStats::~ZoneStats() { | 70 ZonePool::~ZonePool() { |
| 67 DCHECK(zones_.empty()); | 71 DCHECK(used_.empty()); |
| 68 DCHECK(stats_.empty()); | 72 DCHECK(stats_.empty()); |
| 73 for (Zone* zone : unused_) { |
| 74 delete zone; |
| 75 } |
| 69 } | 76 } |
| 70 | 77 |
| 71 size_t ZoneStats::GetMaxAllocatedBytes() { | 78 |
| 79 size_t ZonePool::GetMaxAllocatedBytes() { |
| 72 return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes()); | 80 return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes()); |
| 73 } | 81 } |
| 74 | 82 |
| 75 size_t ZoneStats::GetCurrentAllocatedBytes() { | 83 |
| 84 size_t ZonePool::GetCurrentAllocatedBytes() { |
| 76 size_t total = 0; | 85 size_t total = 0; |
| 77 for (Zone* zone : zones_) { | 86 for (Zone* zone : used_) { |
| 78 total += static_cast<size_t>(zone->allocation_size()); | 87 total += static_cast<size_t>(zone->allocation_size()); |
| 79 } | 88 } |
| 80 return total; | 89 return total; |
| 81 } | 90 } |
| 82 | 91 |
| 83 size_t ZoneStats::GetTotalAllocatedBytes() { | 92 |
| 93 size_t ZonePool::GetTotalAllocatedBytes() { |
| 84 return total_deleted_bytes_ + GetCurrentAllocatedBytes(); | 94 return total_deleted_bytes_ + GetCurrentAllocatedBytes(); |
| 85 } | 95 } |
| 86 | 96 |
| 87 Zone* ZoneStats::NewEmptyZone() { | 97 |
| 88 Zone* zone = new Zone(allocator_); | 98 Zone* ZonePool::NewEmptyZone() { |
| 89 zones_.push_back(zone); | 99 Zone* zone; |
| 100 // Grab a zone from pool if possible. |
| 101 if (!unused_.empty()) { |
| 102 zone = unused_.back(); |
| 103 unused_.pop_back(); |
| 104 } else { |
| 105 zone = new Zone(allocator_); |
| 106 } |
| 107 used_.push_back(zone); |
| 108 DCHECK_EQ(0u, zone->allocation_size()); |
| 90 return zone; | 109 return zone; |
| 91 } | 110 } |
| 92 | 111 |
| 93 void ZoneStats::ReturnZone(Zone* zone) { | 112 |
| 113 void ZonePool::ReturnZone(Zone* zone) { |
| 94 size_t current_total = GetCurrentAllocatedBytes(); | 114 size_t current_total = GetCurrentAllocatedBytes(); |
| 95 // Update max. | 115 // Update max. |
| 96 max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total); | 116 max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total); |
| 97 // Update stats. | 117 // Update stats. |
| 98 for (StatsScope* stat_scope : stats_) { | 118 for (StatsScope* stat_scope : stats_) { |
| 99 stat_scope->ZoneReturned(zone); | 119 stat_scope->ZoneReturned(zone); |
| 100 } | 120 } |
| 101 // Remove from used. | 121 // Remove from used. |
| 102 Zones::iterator it = std::find(zones_.begin(), zones_.end(), zone); | 122 Used::iterator it = std::find(used_.begin(), used_.end(), zone); |
| 103 DCHECK(it != zones_.end()); | 123 DCHECK(it != used_.end()); |
| 104 zones_.erase(it); | 124 used_.erase(it); |
| 105 total_deleted_bytes_ += static_cast<size_t>(zone->allocation_size()); | 125 total_deleted_bytes_ += static_cast<size_t>(zone->allocation_size()); |
| 106 delete zone; | 126 // Delete zone or clear and stash on unused_. |
| 127 if (unused_.size() >= kMaxUnusedSize) { |
| 128 delete zone; |
| 129 } else { |
| 130 zone->DeleteAll(); |
| 131 DCHECK_EQ(0u, zone->allocation_size()); |
| 132 unused_.push_back(zone); |
| 133 } |
| 107 } | 134 } |
| 108 | 135 |
| 109 } // namespace compiler | 136 } // namespace compiler |
| 110 } // namespace internal | 137 } // namespace internal |
| 111 } // namespace v8 | 138 } // namespace v8 |
| OLD | NEW |