Skip to content

Commit

Permalink
Try support 16k page size
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Mar 25, 2024
1 parent 1e36d0a commit f5563d0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lsplant/src/main/jni/lsplant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <array>
#include <atomic>
#include <bits/sysconf.h>

#include "art/mirror/class.hpp"
#include "art/runtime/art_method.hpp"
Expand Down Expand Up @@ -509,7 +510,8 @@ static_assert(std::endian::native == std::endian::little, "Unsupported architect
union Trampoline {
public:
uintptr_t address;
unsigned count : 12;
unsigned count4k : 12;
unsigned count16k : 14;
};

static_assert(sizeof(Trampoline) == sizeof(uintptr_t), "Unsupported architecture");
Expand All @@ -518,16 +520,16 @@ static_assert(std::atomic_uintptr_t::is_always_lock_free, "Unsupported architect
std::atomic_uintptr_t trampoline_pool{0};
std::atomic_flag trampoline_lock{false};
constexpr size_t kTrampolineSize = RoundUpTo(sizeof(trampoline), kPointerSize);
constexpr size_t kPageSize = 4096; // assume
constexpr size_t kTrampolineNumPerPage = kPageSize / kTrampolineSize;
constexpr uintptr_t kAddressMask = 0xFFFU;

void *GenerateTrampolineFor(art::ArtMethod *hook) {
static const size_t kPageSize = sysconf(_SC_PAGESIZE); // assume
static const size_t kTrampolineNumPerPage = kPageSize / kTrampolineSize;
unsigned count;
uintptr_t address;
while (true) {
auto tl = Trampoline{.address = trampoline_pool.fetch_add(1, std::memory_order_release)};
count = tl.count;
count = kPageSize == 16384 ? tl.count16k : tl.count4k;
address = tl.address & ~kAddressMask;
if (address == 0 || count >= kTrampolineNumPerPage) {
if (trampoline_lock.test_and_set(std::memory_order_acq_rel)) {
Expand All @@ -545,7 +547,7 @@ void *GenerateTrampolineFor(art::ArtMethod *hook) {
}
count = 0;
tl.address = address;
tl.count = count + 1;
(kPageSize == 16384 ? tl.count16k : tl.count4k) = count + 1;
trampoline_pool.store(tl.address, std::memory_order_release);
trampoline_lock.clear(std::memory_order_release);
trampoline_lock.notify_all();
Expand Down

0 comments on commit f5563d0

Please sign in to comment.