Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mjp41 committed Mar 23, 2022
1 parent 30fcc22 commit f6254d6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/backend/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ namespace snmalloc
#if defined(OPEN_ENCLAVE)
// Single global buddy allocator is used on open enclave due to
// the limited address space.
using GlobalR = GlobalRange<StatsR<SmallBuddyRange<
LargeBuddyRange<EmptyRange, bits::BITS - 1, bits::BITS - 1, Pagemap>>>>;
using StatsR = StatsR<SmallBuddyRange<
LargeBuddyRange<EmptyRange, bits::BITS - 1, bits::BITS - 1, Pagemap>>>;
using GlobalR = GlobalRange<StatsR>;
using ObjectRange = GlobalR;
using GlobalMetaRange = ObjectRange;
#else
Expand Down Expand Up @@ -280,16 +281,15 @@ namespace snmalloc
auto p = local_state.object_range->alloc_range(size);

#ifdef SNMALLOC_TRACING
std::cout << "Alloc chunk: " << p.unsafe_ptr() << " (" << size << ")"
<< std::endl;
message<1024>("Alloc chunk: {} ({})", p.unsafe_ptr(), size);
#endif
if (p == nullptr)
{
local_state.get_meta_range()->dealloc_range(
meta_cap, PAGEMAP_METADATA_STRUCT_SIZE);
errno = ENOMEM;
#ifdef SNMALLOC_TRACING
std::cout << "Out of memory" << std::endl;
message<1024>("Out of memory");
#endif
return {p, nullptr};
}
Expand Down
37 changes: 34 additions & 3 deletions src/backend/decayrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace snmalloc
auto curr = *this;
while (!curr.is_empty())
{
auto next = get_next();
auto next = curr.get_next();

f(curr.get_capability());

Expand Down Expand Up @@ -183,8 +183,7 @@ namespace snmalloc
ParentRange::ConcurrencySafe,
"Parent must be concurrency safe, as dealloc_range is called here on "
"potentially another thread's state.");
auto new_epoch =
(epoch + 1) % NUM_EPOCHS;
auto new_epoch = (epoch + 1) % NUM_EPOCHS;
// Flush old index for all threads.
auto curr = all_local.load(std::memory_order_acquire);
while (curr != nullptr)
Expand All @@ -211,6 +210,9 @@ namespace snmalloc
*/
static void process(PalTimerObject*)
{
#ifdef SNMALLOC_TRACING
message<1024>("DecayRange::handle_decay_tick timer");
#endif
handle_decay_tick();
}

Expand Down Expand Up @@ -256,7 +258,14 @@ namespace snmalloc
auto p = chunk_stack[slab_sizeclass][(epoch - e) % NUM_EPOCHS].pop();

if (p != nullptr)
{
#ifdef SNMALLOC_TRACING
message<1024>(
"DecayRange::alloc_range: returning from local cache: {} on {}",
address_cast(p), this);
#endif
return p;
}
}
}

Expand All @@ -272,14 +281,31 @@ namespace snmalloc
result = parent->alloc_range(size);
if (result != nullptr)
{
#ifdef SNMALLOC_TRACING
message<1024>(
"DecayRange::alloc_range: returning from parent: {} on {}",
address_cast(result), this);
#endif
return result;
}

// We have run out of memory.
// Try to free some memory to the parent.
#ifdef SNMALLOC_TRACING
message<1024>("DecayRange::handle_decay_tick OOM");
#endif
handle_decay_tick();
}

// Last try.
result = parent->alloc_range(size);

#ifdef SNMALLOC_TRACING
message<1024>(
"DecayRange::alloc_range: returning from parent last try: {} on {}",
address_cast(result), this);
#endif

return result;
}

Expand Down Expand Up @@ -310,6 +336,11 @@ namespace snmalloc

auto slab_sizeclass = bits::next_pow2_bits(size) - MIN_CHUNK_BITS;
// Add to local cache.
#ifdef SNMALLOC_TRACING
message<1024>(
"DecayRange::dealloc_range: returning to local cache: {} on {}",
address_cast(base), this);
#endif
chunk_stack[slab_sizeclass][epoch].push(base);
}
};
Expand Down
12 changes: 11 additions & 1 deletion src/pal/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,20 @@ namespace snmalloc
Pal::error(msg.get_message());
}

static inline thread_local size_t tid {0};
static inline std::atomic<size_t> tid_source {1};

template<size_t BufferSize, typename... Args>
inline void message(Args... args)
{
if (tid == 0)
{
tid = tid_source++;
}

MessageBuilder<BufferSize> msg{std::forward<Args>(args)...};
Pal::message(msg.get_message());
MessageBuilder<BufferSize> msg_tid{"{}: {}", tid, msg.get_message()};

Pal::message(msg_tid.get_message());
}
} // namespace snmalloc

0 comments on commit f6254d6

Please sign in to comment.