zone-stats.h 2.7 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 11

#include <map>
#include <set>
#include <vector>

12
#include "src/common/globals.h"
13
#include "src/zone/zone.h"
14 15 16 17 18

namespace v8 {
namespace internal {
namespace compiler {

19
class V8_EXPORT_PRIVATE ZoneStats final {
20
 public:
21
  class V8_NODISCARD Scope final {
22
   public:
23 24 25 26 27 28
    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) {}
29 30
    ~Scope() { Destroy(); }

31 32 33
    Scope(const Scope&) = delete;
    Scope& operator=(const Scope&) = delete;

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

45 46
    ZoneStats* zone_stats() const { return zone_stats_; }

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

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

    size_t GetMaxAllocatedBytes();
    size_t GetCurrentAllocatedBytes();
63
    size_t GetTotalAllocatedBytes();
64 65

   private:
66
    friend class ZoneStats;
67 68
    void ZoneReturned(Zone* zone);

69
    using InitialValues = std::map<Zone*, size_t>;
70

71
    ZoneStats* const zone_stats_;
72
    InitialValues initial_values_;
73
    size_t total_allocated_bytes_at_start_;
74 75 76
    size_t max_allocated_bytes_;
  };

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

82 83 84
  size_t GetMaxAllocatedBytes() const;
  size_t GetTotalAllocatedBytes() const;
  size_t GetCurrentAllocatedBytes() const;
85 86

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

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

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

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

105
#endif  // V8_COMPILER_ZONE_STATS_H_