1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_COMPILER_ZONE_STATS_H_ 6 #define V8_COMPILER_ZONE_STATS_H_ 7 8 #include <map> 9 #include <set> 10 #include <vector> 11 12 #include "src/common/globals.h" 13 #include "src/zone/zone.h" 14 15 namespace v8 { 16 namespace internal { 17 namespace compiler { 18 19 class V8_EXPORT_PRIVATE ZoneStats final { 20 public: 21 class Scope final { 22 public: 23 explicit Scope(ZoneStats* zone_stats, const char* zone_name, 24 bool support_zone_compression = false) zone_name_(zone_name)25 : zone_name_(zone_name), 26 zone_stats_(zone_stats), 27 zone_(nullptr), 28 support_zone_compression_(support_zone_compression) {} ~Scope()29 ~Scope() { Destroy(); } 30 31 Scope(const Scope&) = delete; 32 Scope& operator=(const Scope&) = delete; 33 zone()34 Zone* zone() { 35 if (zone_ == nullptr) 36 zone_ = 37 zone_stats_->NewEmptyZone(zone_name_, support_zone_compression_); 38 return zone_; 39 } Destroy()40 void Destroy() { 41 if (zone_ != nullptr) zone_stats_->ReturnZone(zone_); 42 zone_ = nullptr; 43 } 44 zone_stats()45 ZoneStats* zone_stats() const { return zone_stats_; } 46 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 StatsScope final { 55 public: 56 explicit StatsScope(ZoneStats* zone_stats); 57 ~StatsScope(); 58 StatsScope(const StatsScope&) = delete; 59 StatsScope& operator=(const StatsScope&) = delete; 60 61 size_t GetMaxAllocatedBytes(); 62 size_t GetCurrentAllocatedBytes(); 63 size_t GetTotalAllocatedBytes(); 64 65 private: 66 friend class ZoneStats; 67 void ZoneReturned(Zone* zone); 68 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 size_t max_allocated_bytes_; 75 }; 76 77 explicit ZoneStats(AccountingAllocator* allocator); 78 ~ZoneStats(); 79 ZoneStats(const ZoneStats&) = delete; 80 ZoneStats& operator=(const ZoneStats&) = delete; 81 82 size_t GetMaxAllocatedBytes() const; 83 size_t GetTotalAllocatedBytes() const; 84 size_t GetCurrentAllocatedBytes() const; 85 86 private: 87 Zone* NewEmptyZone(const char* zone_name, bool support_zone_compression); 88 void ReturnZone(Zone* zone); 89 90 static const size_t kMaxUnusedSize = 3; 91 using Zones = std::vector<Zone*>; 92 using Stats = std::vector<StatsScope*>; 93 94 Zones zones_; 95 Stats stats_; 96 size_t max_allocated_bytes_; 97 size_t total_deleted_bytes_; 98 AccountingAllocator* allocator_; 99 }; 100 101 } // namespace compiler 102 } // namespace internal 103 } // namespace v8 104 105 #endif // V8_COMPILER_ZONE_STATS_H_ 106