zone-stats.cc 3.2 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
#include "src/compiler/zone-stats.h"
6 7 8 9 10

namespace v8 {
namespace internal {
namespace compiler {

11 12 13
ZoneStats::StatsScope::StatsScope(ZoneStats* zone_stats)
    : zone_stats_(zone_stats),
      total_allocated_bytes_at_start_(zone_stats->GetTotalAllocatedBytes()),
14
      max_allocated_bytes_(0) {
15 16
  zone_stats_->stats_.push_back(this);
  for (Zone* zone : zone_stats_->zones_) {
17 18 19 20 21 22 23 24
    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);
  }
}

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

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

34
size_t ZoneStats::StatsScope::GetCurrentAllocatedBytes() {
35
  size_t total = 0;
36
  for (Zone* zone : zone_stats_->zones_) {
37 38 39 40 41 42 43 44 45 46
    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;
}

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

52
void ZoneStats::StatsScope::ZoneReturned(Zone* zone) {
53 54 55 56 57 58 59 60 61 62
  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);
  }
}

63
ZoneStats::ZoneStats(AccountingAllocator* allocator)
64
    : max_allocated_bytes_(0), total_deleted_bytes_(0), allocator_(allocator) {}
65

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

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

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

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

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

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

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