zone-stats.h 2.65 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
#ifndef V8_COMPILER_ZONE_STATS_H_
#define V8_COMPILER_ZONE_STATS_H_
7 8 9 10

#include <map>
#include <vector>

11
#include "src/zone/zone.h"
12 13 14 15 16

namespace v8 {
namespace internal {
namespace compiler {

17
class V8_EXPORT_PRIVATE ZoneStats final {
18
 public:
19
  class V8_NODISCARD Scope final {
20
   public:
21 22 23 24 25 26
    explicit Scope(ZoneStats* zone_stats, const char* zone_name,
                   bool support_zone_compression = false)
        : zone_name_(zone_name),
          zone_stats_(zone_stats),
          zone_(nullptr),
          support_zone_compression_(support_zone_compression) {}
27 28
    ~Scope() { Destroy(); }

29 30 31
    Scope(const Scope&) = delete;
    Scope& operator=(const Scope&) = delete;

32
    Zone* zone() {
33 34 35
      if (zone_ == nullptr)
        zone_ =
            zone_stats_->NewEmptyZone(zone_name_, support_zone_compression_);
36 37 38
      return zone_;
    }
    void Destroy() {
39
      if (zone_ != nullptr) zone_stats_->ReturnZone(zone_);
40
      zone_ = nullptr;
41 42
    }

43 44
    ZoneStats* zone_stats() const { return zone_stats_; }

45
   private:
46
    const char* zone_name_;
47
    ZoneStats* const zone_stats_;
48
    Zone* zone_;
49
    const bool support_zone_compression_;
50 51
  };

52
  class V8_EXPORT_PRIVATE V8_NODISCARD StatsScope final {
53
   public:
54
    explicit StatsScope(ZoneStats* zone_stats);
55
    ~StatsScope();
56 57
    StatsScope(const StatsScope&) = delete;
    StatsScope& operator=(const StatsScope&) = delete;
58 59 60

    size_t GetMaxAllocatedBytes();
    size_t GetCurrentAllocatedBytes();
61
    size_t GetTotalAllocatedBytes();
62 63

   private:
64
    friend class ZoneStats;
65 66
    void ZoneReturned(Zone* zone);

67
    using InitialValues = std::map<Zone*, size_t>;
68

69
    ZoneStats* const zone_stats_;
70
    InitialValues initial_values_;
71
    size_t total_allocated_bytes_at_start_;
72 73 74
    size_t max_allocated_bytes_;
  };

75 76
  explicit ZoneStats(AccountingAllocator* allocator);
  ~ZoneStats();
77 78
  ZoneStats(const ZoneStats&) = delete;
  ZoneStats& operator=(const ZoneStats&) = delete;
79

80 81 82
  size_t GetMaxAllocatedBytes() const;
  size_t GetTotalAllocatedBytes() const;
  size_t GetCurrentAllocatedBytes() const;
83 84

 private:
85
  Zone* NewEmptyZone(const char* zone_name, bool support_zone_compression);
86 87 88
  void ReturnZone(Zone* zone);

  static const size_t kMaxUnusedSize = 3;
89 90
  using Zones = std::vector<Zone*>;
  using Stats = std::vector<StatsScope*>;
91

92
  Zones zones_;
93 94 95
  Stats stats_;
  size_t max_allocated_bytes_;
  size_t total_deleted_bytes_;
96
  AccountingAllocator* allocator_;
97 98 99 100 101 102
};

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

103
#endif  // V8_COMPILER_ZONE_STATS_H_