zone-stats.h 2.21 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/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 Scope final {
22
   public:
23 24
    explicit Scope(ZoneStats* zone_stats, const char* zone_name)
        : zone_name_(zone_name), zone_stats_(zone_stats), zone_(nullptr) {}
25 26 27
    ~Scope() { Destroy(); }

    Zone* zone() {
28
      if (zone_ == nullptr) zone_ = zone_stats_->NewEmptyZone(zone_name_);
29 30 31
      return zone_;
    }
    void Destroy() {
32
      if (zone_ != nullptr) zone_stats_->ReturnZone(zone_);
33
      zone_ = nullptr;
34 35 36
    }

   private:
37
    const char* zone_name_;
38
    ZoneStats* const zone_stats_;
39 40 41 42
    Zone* zone_;
    DISALLOW_COPY_AND_ASSIGN(Scope);
  };

43
  class V8_EXPORT_PRIVATE StatsScope final {
44
   public:
45
    explicit StatsScope(ZoneStats* zone_stats);
46 47 48 49
    ~StatsScope();

    size_t GetMaxAllocatedBytes();
    size_t GetCurrentAllocatedBytes();
50
    size_t GetTotalAllocatedBytes();
51 52

   private:
53
    friend class ZoneStats;
54 55 56 57
    void ZoneReturned(Zone* zone);

    typedef std::map<Zone*, size_t> InitialValues;

58
    ZoneStats* const zone_stats_;
59
    InitialValues initial_values_;
60
    size_t total_allocated_bytes_at_start_;
61 62 63 64 65
    size_t max_allocated_bytes_;

    DISALLOW_COPY_AND_ASSIGN(StatsScope);
  };

66 67
  explicit ZoneStats(AccountingAllocator* allocator);
  ~ZoneStats();
68

69 70 71
  size_t GetMaxAllocatedBytes() const;
  size_t GetTotalAllocatedBytes() const;
  size_t GetCurrentAllocatedBytes() const;
72 73

 private:
74
  Zone* NewEmptyZone(const char* zone_name);
75 76 77
  void ReturnZone(Zone* zone);

  static const size_t kMaxUnusedSize = 3;
78
  typedef std::vector<Zone*> Zones;
79 80
  typedef std::vector<StatsScope*> Stats;

81
  Zones zones_;
82 83 84
  Stats stats_;
  size_t max_allocated_bytes_;
  size_t total_deleted_bytes_;
85
  AccountingAllocator* allocator_;
86

87
  DISALLOW_COPY_AND_ASSIGN(ZoneStats);
88 89 90 91 92 93
};

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

94
#endif  // V8_COMPILER_ZONE_STATS_H_