| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_ZONE_ACCOUNTING_ALLOCATOR_H_ | 5 #ifndef V8_ZONE_ACCOUNTING_ALLOCATOR_H_ |
| 6 #define V8_ZONE_ACCOUNTING_ALLOCATOR_H_ | 6 #define V8_ZONE_ACCOUNTING_ALLOCATOR_H_ |
| 7 | 7 |
| 8 #include "include/v8-platform.h" | 8 #include "include/v8-platform.h" |
| 9 #include "src/base/atomic-utils.h" | 9 #include "src/base/atomic-utils.h" |
| 10 #include "src/base/atomicops.h" | 10 #include "src/base/atomicops.h" |
| 11 #include "src/base/macros.h" | 11 #include "src/base/macros.h" |
| 12 #include "src/base/platform/mutex.h" | 12 #include "src/base/platform/mutex.h" |
| 13 #include "src/base/platform/semaphore.h" | 13 #include "src/base/platform/semaphore.h" |
| 14 #include "src/base/platform/time.h" | 14 #include "src/base/platform/time.h" |
| 15 #include "src/zone/zone-segment.h" | 15 #include "src/zone/zone-segment.h" |
| 16 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 16 | 17 |
| 17 namespace v8 { | 18 namespace v8 { |
| 18 namespace internal { | 19 namespace internal { |
| 19 | 20 |
| 20 class V8_EXPORT_PRIVATE AccountingAllocator { | 21 class V8_EXPORT_PRIVATE AccountingAllocator { |
| 21 public: | 22 public: |
| 23 static const size_t kMaxPoolSizeLowMemoryDevice = 8ul * KB; |
| 24 static const size_t kMaxPoolSizeMediumMemoryDevice = 1ul * MB; |
| 25 static const size_t kMaxPoolSizeHighMemoryDevice = 2ul * MB; |
| 26 static const size_t kMaxPoolSizeHugeMemoryDevice = 3ul * MB; |
| 27 |
| 22 AccountingAllocator(); | 28 AccountingAllocator(); |
| 23 virtual ~AccountingAllocator(); | 29 virtual ~AccountingAllocator(); |
| 24 | 30 |
| 25 // Gets an empty segment from the pool or creates a new one. | 31 // Gets an empty segment from the pool or creates a new one. |
| 26 virtual Segment* GetSegment(size_t bytes); | 32 virtual Segment* GetSegment(size_t bytes); |
| 27 // Return unneeded segments to either insert them into the pool or release | 33 // Return unneeded segments to either insert them into the pool or release |
| 28 // them if the pool is already full or memory pressure is high. | 34 // them if the pool is already full or memory pressure is high. |
| 29 virtual void ReturnSegment(Segment* memory); | 35 virtual void ReturnSegment(Segment* memory); |
| 30 | 36 |
| 31 size_t GetCurrentMemoryUsage() const; | 37 size_t GetCurrentMemoryUsage() const; |
| 32 size_t GetMaxMemoryUsage() const; | 38 size_t GetMaxMemoryUsage() const; |
| 33 | 39 |
| 34 size_t GetCurrentPoolSize() const; | 40 size_t GetCurrentPoolSize() const; |
| 35 | 41 |
| 36 void MemoryPressureNotification(MemoryPressureLevel level); | 42 void MemoryPressureNotification(MemoryPressureLevel level); |
| 43 // Configures the zone segment pool size limits so the pool does not |
| 44 // grow bigger than max_pool_size. |
| 45 // TODO(heimbuef): Do not accept segments to pool that are larger than |
| 46 // their size class requires. Sometimes the zones generate weird segments. |
| 47 void ConfigureSegmentPool(const size_t max_pool_size); |
| 37 | 48 |
| 38 virtual void ZoneCreation(const Zone* zone) {} | 49 virtual void ZoneCreation(const Zone* zone) {} |
| 39 virtual void ZoneDestruction(const Zone* zone) {} | 50 virtual void ZoneDestruction(const Zone* zone) {} |
| 40 | 51 |
| 41 private: | 52 private: |
| 42 static const uint8_t kMinSegmentSizePower = 13; | 53 FRIEND_TEST(Zone, SegmentPoolConstraints); |
| 43 static const uint8_t kMaxSegmentSizePower = 18; | 54 |
| 44 static const uint8_t kMaxSegmentsPerBucket = 5; | 55 static const size_t kMinSegmentSizePower = 13; |
| 56 static const size_t kMaxSegmentSizePower = 18; |
| 45 | 57 |
| 46 STATIC_ASSERT(kMinSegmentSizePower <= kMaxSegmentSizePower); | 58 STATIC_ASSERT(kMinSegmentSizePower <= kMaxSegmentSizePower); |
| 47 | 59 |
| 60 static const size_t kNumberBuckets = |
| 61 1 + kMaxSegmentSizePower - kMinSegmentSizePower; |
| 62 |
| 48 // Allocates a new segment. Returns nullptr on failed allocation. | 63 // Allocates a new segment. Returns nullptr on failed allocation. |
| 49 Segment* AllocateSegment(size_t bytes); | 64 Segment* AllocateSegment(size_t bytes); |
| 50 void FreeSegment(Segment* memory); | 65 void FreeSegment(Segment* memory); |
| 51 | 66 |
| 52 // Returns a segment from the pool of at least the requested size. | 67 // Returns a segment from the pool of at least the requested size. |
| 53 Segment* GetSegmentFromPool(size_t requested_size); | 68 Segment* GetSegmentFromPool(size_t requested_size); |
| 54 // Trys to add a segment to the pool. Returns false if the pool is full. | 69 // Trys to add a segment to the pool. Returns false if the pool is full. |
| 55 bool AddSegmentToPool(Segment* segment); | 70 bool AddSegmentToPool(Segment* segment); |
| 56 | 71 |
| 57 // Empties the pool and puts all its contents onto the garbage stack. | 72 // Empties the pool and puts all its contents onto the garbage stack. |
| 58 void ClearPool(); | 73 void ClearPool(); |
| 59 | 74 |
| 60 Segment* | 75 Segment* unused_segments_heads_[kNumberBuckets]; |
| 61 unused_segments_heads_[1 + kMaxSegmentSizePower - kMinSegmentSizePower]; | |
| 62 | 76 |
| 63 size_t unused_segments_sizes[1 + kMaxSegmentSizePower - kMinSegmentSizePower]; | 77 size_t unused_segments_sizes_[kNumberBuckets]; |
| 78 size_t unused_segments_max_sizes_[kNumberBuckets]; |
| 64 | 79 |
| 65 base::Mutex unused_segments_mutex_; | 80 base::Mutex unused_segments_mutex_; |
| 66 | 81 |
| 67 base::AtomicWord current_memory_usage_ = 0; | 82 base::AtomicWord current_memory_usage_ = 0; |
| 68 base::AtomicWord max_memory_usage_ = 0; | 83 base::AtomicWord max_memory_usage_ = 0; |
| 69 base::AtomicWord current_pool_size_ = 0; | 84 base::AtomicWord current_pool_size_ = 0; |
| 70 | 85 |
| 71 base::AtomicValue<MemoryPressureLevel> memory_pressure_level_; | 86 base::AtomicValue<MemoryPressureLevel> memory_pressure_level_; |
| 72 | 87 |
| 73 DISALLOW_COPY_AND_ASSIGN(AccountingAllocator); | 88 DISALLOW_COPY_AND_ASSIGN(AccountingAllocator); |
| 74 }; | 89 }; |
| 75 | 90 |
| 76 } // namespace internal | 91 } // namespace internal |
| 77 } // namespace v8 | 92 } // namespace v8 |
| 78 | 93 |
| 79 #endif // V8_ZONE_ACCOUNTING_ALLOCATOR_H_ | 94 #endif // V8_ZONE_ACCOUNTING_ALLOCATOR_H_ |
| OLD | NEW |