v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
allocation-stats.h
Go to the documentation of this file.
1// Copyright 2020 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_HEAP_ALLOCATION_STATS_H_
6#define V8_HEAP_ALLOCATION_STATS_H_
7
8#include <atomic>
9#include <unordered_map>
10
11#include "src/base/hashing.h"
12#include "src/base/macros.h"
14
15namespace v8 {
16namespace internal {
17
18// An abstraction of the accounting statistics of a page-structured space.
19//
20// The stats are only set by functions that ensure they stay balanced. These
21// functions increase or decrease one of the non-capacity stats in conjunction
22// with capacity, or else they always balance increases and decreases to the
23// non-capacity stats.
25 public:
27
29 capacity_ = stats.capacity_.load();
30 max_capacity_ = stats.max_capacity_;
31 size_.store(stats.size_);
32#ifdef DEBUG
33 allocated_on_page_ = stats.allocated_on_page_;
34#endif
35 return *this;
36 }
37
38 // Zero out all the allocation statistics (i.e., no capacity).
39 void Clear() {
40 capacity_ = 0;
41 max_capacity_ = 0;
42 ClearSize();
43 }
44
45 void ClearSize() {
46 size_ = 0;
47#ifdef DEBUG
48 allocated_on_page_.clear();
49#endif
50 }
51
52 // Accessors for the allocation statistics.
53 size_t Capacity() const { return capacity_; }
54 size_t MaxCapacity() const { return max_capacity_; }
55 size_t Size() const { return size_; }
56#ifdef DEBUG
57 size_t AllocatedOnPage(const MemoryChunkMetadata* page) const {
58 return allocated_on_page_.at(page);
59 }
60#endif
61
62 void IncreaseAllocatedBytes(size_t bytes, const MemoryChunkMetadata* page) {
65#ifdef DEBUG
66 size_t size = size_;
67 DCHECK_GE(size + bytes, size);
68#endif
69 size_.fetch_add(bytes);
70#ifdef DEBUG
71 allocated_on_page_[page] += bytes;
72#endif
73 }
74
75 void DecreaseAllocatedBytes(size_t bytes, const MemoryChunkMetadata* page) {
76 DCHECK_GE(size_, bytes);
77 size_.fetch_sub(bytes);
78#ifdef DEBUG
79 DCHECK_GE(allocated_on_page_[page], bytes);
80 allocated_on_page_[page] -= bytes;
81#endif
82 }
83
84 void DecreaseCapacity(size_t bytes) {
85 DCHECK_GE(capacity_, bytes);
86 DCHECK_GE(capacity_ - bytes, size_);
87 capacity_ -= bytes;
88 }
89
90 void IncreaseCapacity(size_t bytes) {
92 capacity_ += bytes;
95 }
96 }
97
98 private:
99 // |capacity_|: The number of object-area bytes (i.e., not including page
100 // bookkeeping structures) currently in the space.
101 // During evacuation capacity of the main spaces is accessed from multiple
102 // threads to check the old generation hard limit.
103 std::atomic<size_t> capacity_;
104
105 // |max_capacity_|: The maximum capacity ever observed.
107
108 // |size_|: The number of allocated bytes.
109 std::atomic<size_t> size_;
110
111#ifdef DEBUG
112 std::unordered_map<const MemoryChunkMetadata*, size_t,
114 allocated_on_page_;
115#endif
116};
117
118} // namespace internal
119} // namespace v8
120
121#endif // V8_HEAP_ALLOCATION_STATS_H_
AllocationStats & operator=(const AllocationStats &stats) V8_NOEXCEPT
std::atomic< size_t > capacity_
void IncreaseCapacity(size_t bytes)
void DecreaseCapacity(size_t bytes)
void IncreaseAllocatedBytes(size_t bytes, const MemoryChunkMetadata *page)
void DecreaseAllocatedBytes(size_t bytes, const MemoryChunkMetadata *page)
#define V8_COMPRESS_POINTERS_8GB_BOOL
Definition globals.h:608
BasePage * page
Definition sweeper.cc:218
constexpr intptr_t kObjectAlignment8GbHeap
Definition globals.h:934
#define V8_NOEXCEPT
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK_GE(v1, v2)
Definition logging.h:488
constexpr bool IsAligned(T value, U alignment)
Definition macros.h:403