Skip to content

Commit cd97ef9

Browse files
yoneymeta-codesync[bot]
authored andcommitted
Make CodeAllocatorCinder stats fields atomic
Summary: Convert `lost_bytes_`, `huge_allocs_`, and `fragmented_allocs_` to `std::atomic` so their getters can be called without holding the allocator mutex. Also switch `used_bytes_` accesses in subclasses to explicit `fetch_add` with relaxed ordering. Reviewed By: alexmalyshev Differential Revision: D97004723 fbshipit-source-id: 7c52abfd9a8c806963acfcea5dcc845fe624829b
1 parent 04859c2 commit cd97ef9

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

cinderx/Jit/code_allocator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ AllocateResult CodeAllocatorCinder::addCode(asmjit::CodeHolder* code) {
147147
size_t max_code_size = code->codeSize();
148148
size_t alloc_size = ((max_code_size / kAllocSize) + 1) * kAllocSize;
149149
if (current_alloc_free_ < max_code_size) {
150-
lost_bytes_ += current_alloc_free_;
150+
lost_bytes_.fetch_add(current_alloc_free_, std::memory_order_relaxed);
151151

152152
uint8_t* res = allocPages(alloc_size);
153153
if (!setHugePages(res, alloc_size)) {
154-
fragmented_allocs_++;
154+
fragmented_allocs_.fetch_add(1, std::memory_order_relaxed);
155155
} else {
156-
huge_allocs_++;
156+
huge_allocs_.fetch_add(1, std::memory_order_relaxed);
157157
}
158158
current_alloc_ = static_cast<uint8_t*>(res);
159159
allocations_.emplace_back(res, alloc_size);
@@ -186,7 +186,7 @@ AllocateResult CodeAllocatorCinder::addCode(asmjit::CodeHolder* code) {
186186

187187
current_alloc_ += actual_code_size;
188188
current_alloc_free_ -= actual_code_size;
189-
used_bytes_ += actual_code_size;
189+
used_bytes_.fetch_add(actual_code_size, std::memory_order_relaxed);
190190

191191
return AllocateResult{addr, asmjit::kErrorOk};
192192
}
@@ -264,7 +264,7 @@ AllocateResult MultipleSectionCodeAllocator::addCode(asmjit::CodeHolder* code) {
264264
}
265265

266266
size_t potential_code_size = code->codeSize();
267-
used_bytes_ += potential_code_size;
267+
used_bytes_.fetch_add(potential_code_size, std::memory_order_relaxed);
268268
// We fall back to the default size of code allocation if the
269269
// code doesn't fit into either section, and we can make this check more
270270
// granular by comparing sizes section-by-section.

cinderx/Jit/code_allocator.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ class CodeAllocatorCinder : public CodeAllocator {
5353
~CodeAllocatorCinder() override;
5454

5555
size_t lostBytes() const {
56-
return lost_bytes_;
56+
return lost_bytes_.load(std::memory_order_relaxed);
5757
}
5858

5959
size_t fragmentedAllocs() const {
60-
return fragmented_allocs_;
60+
return fragmented_allocs_.load(std::memory_order_relaxed);
6161
}
6262

6363
size_t hugeAllocs() const {
64-
return huge_allocs_;
64+
return huge_allocs_.load(std::memory_order_relaxed);
6565
}
6666

6767
AllocateResult addCode(asmjit::CodeHolder* code) override;
@@ -84,11 +84,11 @@ class CodeAllocatorCinder : public CodeAllocator {
8484

8585
// Number of bytes in total lost when allocations didn't fit neatly into
8686
// the bytes remaining in a chunk so a new one was allocated.
87-
size_t lost_bytes_{0};
87+
std::atomic<size_t> lost_bytes_{0};
8888
// Number of chunks allocated (= to number of huge pages used)
89-
size_t huge_allocs_{0};
89+
std::atomic<size_t> huge_allocs_{0};
9090
// Number of chunks allocated which did not use huge pages.
91-
size_t fragmented_allocs_{0};
91+
std::atomic<size_t> fragmented_allocs_{0};
9292
};
9393

9494
class MultipleSectionCodeAllocator : public CodeAllocator {

0 commit comments

Comments
 (0)