zone-stats.cc 3.22 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include <algorithm>

7
#include "src/compiler/zone-stats.h"
8 9 10 11 12

namespace v8 {
namespace internal {
namespace compiler {

13 14 15
ZoneStats::StatsScope::StatsScope(ZoneStats* zone_stats)
    : zone_stats_(zone_stats),
      total_allocated_bytes_at_start_(zone_stats->GetTotalAllocatedBytes()),
16
      max_allocated_bytes_(0) {
17 18
  zone_stats_->stats_.push_back(this);
  for (Zone* zone : zone_stats_->zones_) {
19 20 21 22 23 24 25 26
    size_t size = static_cast<size_t>(zone->allocation_size());
    std::pair<InitialValues::iterator, bool> res =
        initial_values_.insert(std::make_pair(zone, size));
    USE(res);
    DCHECK(res.second);
  }
}

27 28 29
ZoneStats::StatsScope::~StatsScope() {
  DCHECK_EQ(zone_stats_->stats_.back(), this);
  zone_stats_->stats_.pop_back();
30 31
}

32
size_t ZoneStats::StatsScope::GetMaxAllocatedBytes() {
33 34 35
  return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes());
}

36
size_t ZoneStats::StatsScope::GetCurrentAllocatedBytes() {
37
  size_t total = 0;
38
  for (Zone* zone : zone_stats_->zones_) {
39 40 41 42 43 44 45 46 47 48
    total += static_cast<size_t>(zone->allocation_size());
    // Adjust for initial values.
    InitialValues::iterator it = initial_values_.find(zone);
    if (it != initial_values_.end()) {
      total -= it->second;
    }
  }
  return total;
}

49 50 51
size_t ZoneStats::StatsScope::GetTotalAllocatedBytes() {
  return zone_stats_->GetTotalAllocatedBytes() -
         total_allocated_bytes_at_start_;
52 53
}

54
void ZoneStats::StatsScope::ZoneReturned(Zone* zone) {
55 56 57 58 59 60 61 62 63 64
  size_t current_total = GetCurrentAllocatedBytes();
  // Update max.
  max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total);
  // Drop zone from initial value map.
  InitialValues::iterator it = initial_values_.find(zone);
  if (it != initial_values_.end()) {
    initial_values_.erase(it);
  }
}

65
ZoneStats::ZoneStats(AccountingAllocator* allocator)
66
    : max_allocated_bytes_(0), total_deleted_bytes_(0), allocator_(allocator) {}
67

68 69
ZoneStats::~ZoneStats() {
  DCHECK(zones_.empty());
70 71 72
  DCHECK(stats_.empty());
}

73
size_t ZoneStats::GetMaxAllocatedBytes() const {
74 75 76
  return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes());
}

77
size_t ZoneStats::GetCurrentAllocatedBytes() const {
78
  size_t total = 0;
79
  for (Zone* zone : zones_) {
80 81 82 83 84
    total += static_cast<size_t>(zone->allocation_size());
  }
  return total;
}

85
size_t ZoneStats::GetTotalAllocatedBytes() const {
86 87 88
  return total_deleted_bytes_ + GetCurrentAllocatedBytes();
}

89 90
Zone* ZoneStats::NewEmptyZone(const char* zone_name) {
  Zone* zone = new Zone(allocator_, zone_name);
91
  zones_.push_back(zone);
92 93 94
  return zone;
}

95
void ZoneStats::ReturnZone(Zone* zone) {
96 97 98 99
  size_t current_total = GetCurrentAllocatedBytes();
  // Update max.
  max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total);
  // Update stats.
100
  for (StatsScope* stat_scope : stats_) {
101 102 103
    stat_scope->ZoneReturned(zone);
  }
  // Remove from used.
104 105 106
  Zones::iterator it = std::find(zones_.begin(), zones_.end(), zone);
  DCHECK(it != zones_.end());
  zones_.erase(it);
107
  total_deleted_bytes_ += static_cast<size_t>(zone->allocation_size());
108
  delete zone;
109 110 111 112 113
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8