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/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: Scope(ZoneStats * zone_stats,const char * zone_name)23 explicit Scope(ZoneStats* zone_stats, const char* zone_name) 24 : zone_name_(zone_name), zone_stats_(zone_stats), zone_(nullptr) {} ~Scope()25 ~Scope() { Destroy(); } 26 zone()27 Zone* zone() { 28 if (zone_ == nullptr) zone_ = zone_stats_->NewEmptyZone(zone_name_); 29 return zone_; 30 } Destroy()31 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 Zone* zone_; 40 DISALLOW_COPY_AND_ASSIGN(Scope); 41 }; 42 43 class V8_EXPORT_PRIVATE StatsScope final { 44 public: 45 explicit StatsScope(ZoneStats* zone_stats); 46 ~StatsScope(); 47 48 size_t GetMaxAllocatedBytes(); 49 size_t GetCurrentAllocatedBytes(); 50 size_t GetTotalAllocatedBytes(); 51 52 private: 53 friend class ZoneStats; 54 void ZoneReturned(Zone* zone); 55 56 typedef std::map<Zone*, size_t> InitialValues; 57 58 ZoneStats* const zone_stats_; 59 InitialValues initial_values_; 60 size_t total_allocated_bytes_at_start_; 61 size_t max_allocated_bytes_; 62 63 DISALLOW_COPY_AND_ASSIGN(StatsScope); 64 }; 65 66 explicit ZoneStats(AccountingAllocator* allocator); 67 ~ZoneStats(); 68 69 size_t GetMaxAllocatedBytes(); 70 size_t GetTotalAllocatedBytes(); 71 size_t GetCurrentAllocatedBytes(); 72 73 private: 74 Zone* NewEmptyZone(const char* zone_name); 75 void ReturnZone(Zone* zone); 76 77 static const size_t kMaxUnusedSize = 3; 78 typedef std::vector<Zone*> Zones; 79 typedef std::vector<StatsScope*> Stats; 80 81 Zones zones_; 82 Stats stats_; 83 size_t max_allocated_bytes_; 84 size_t total_deleted_bytes_; 85 AccountingAllocator* allocator_; 86 87 DISALLOW_COPY_AND_ASSIGN(ZoneStats); 88 }; 89 90 } // namespace compiler 91 } // namespace internal 92 } // namespace v8 93 94 #endif // V8_COMPILER_ZONE_STATS_H_ 95