forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocation-stats.h
More file actions
121 lines (102 loc) Β· 3.24 KB
/
allocation-stats.h
File metadata and controls
121 lines (102 loc) Β· 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2020 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.
#ifndef V8_HEAP_ALLOCATION_STATS_H_
#define V8_HEAP_ALLOCATION_STATS_H_
#include <atomic>
#include <unordered_map>
#include "src/base/hashing.h"
#include "src/base/macros.h"
#include "src/heap/memory-chunk-metadata.h"
namespace v8 {
namespace internal {
// An abstraction of the accounting statistics of a page-structured space.
//
// The stats are only set by functions that ensure they stay balanced. These
// functions increase or decrease one of the non-capacity stats in conjunction
// with capacity, or else they always balance increases and decreases to the
// non-capacity stats.
class AllocationStats {
public:
AllocationStats() { Clear(); }
AllocationStats& operator=(const AllocationStats& stats) V8_NOEXCEPT {
capacity_ = stats.capacity_.load();
max_capacity_ = stats.max_capacity_;
size_.store(stats.size_);
#ifdef DEBUG
allocated_on_page_ = stats.allocated_on_page_;
#endif
return *this;
}
// Zero out all the allocation statistics (i.e., no capacity).
void Clear() {
capacity_ = 0;
max_capacity_ = 0;
ClearSize();
}
void ClearSize() {
size_ = 0;
#ifdef DEBUG
allocated_on_page_.clear();
#endif
}
// Accessors for the allocation statistics.
size_t Capacity() const { return capacity_; }
size_t MaxCapacity() const { return max_capacity_; }
size_t Size() const { return size_; }
#ifdef DEBUG
size_t AllocatedOnPage(const MemoryChunkMetadata* page) const {
return allocated_on_page_.at(page);
}
#endif
void IncreaseAllocatedBytes(size_t bytes, const MemoryChunkMetadata* page) {
DCHECK_IMPLIES(V8_COMPRESS_POINTERS_8GB_BOOL,
IsAligned(bytes, kObjectAlignment8GbHeap));
#ifdef DEBUG
size_t size = size_;
DCHECK_GE(size + bytes, size);
#endif
size_.fetch_add(bytes);
#ifdef DEBUG
allocated_on_page_[page] += bytes;
#endif
}
void DecreaseAllocatedBytes(size_t bytes, const MemoryChunkMetadata* page) {
DCHECK_GE(size_, bytes);
size_.fetch_sub(bytes);
#ifdef DEBUG
DCHECK_GE(allocated_on_page_[page], bytes);
allocated_on_page_[page] -= bytes;
#endif
}
void DecreaseCapacity(size_t bytes) {
DCHECK_GE(capacity_, bytes);
DCHECK_GE(capacity_ - bytes, size_);
capacity_ -= bytes;
}
void IncreaseCapacity(size_t bytes) {
DCHECK_GE(capacity_ + bytes, capacity_);
capacity_ += bytes;
if (capacity_ > max_capacity_) {
max_capacity_ = capacity_;
}
}
private:
// |capacity_|: The number of object-area bytes (i.e., not including page
// bookkeeping structures) currently in the space.
// During evacuation capacity of the main spaces is accessed from multiple
// threads to check the old generation hard limit.
std::atomic<size_t> capacity_;
// |max_capacity_|: The maximum capacity ever observed.
size_t max_capacity_;
// |size_|: The number of allocated bytes.
std::atomic<size_t> size_;
#ifdef DEBUG
std::unordered_map<const MemoryChunkMetadata*, size_t,
base::hash<const MemoryChunkMetadata*>>
allocated_on_page_;
#endif
};
} // namespace internal
} // namespace v8
#endif // V8_HEAP_ALLOCATION_STATS_H_