Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler-rt/lib/scudo/standalone/combined.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "size_class_allocator.h"
#include "stack_depot.h"
#include "string_utils.h"
#include "tracing.h"
#include "tsd.h"

#include "scudo/interface.h"
Expand Down Expand Up @@ -671,10 +672,11 @@ class Allocator {

void releaseToOS(ReleaseToOS ReleaseType) {
initThreadMaybe();
SCUDO_SCOPED_TRACE(GetReleaseToOSTraceName(ReleaseType));
if (ReleaseType == ReleaseToOS::ForceAll)
drainCaches();
Primary.releaseToOS(ReleaseType);
Secondary.releaseToOS();
Secondary.releaseToOS(ReleaseType);
}

// Iterate over all chunks and call a callback for all busy chunks located
Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/scudo/standalone/primary32.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ uptr SizeClassAllocator32<Config>::tryReleaseToOS(uptr ClassId,

template <typename Config>
uptr SizeClassAllocator32<Config>::releaseToOS(ReleaseToOS ReleaseType) {
SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSTraceName(ReleaseType));

uptr TotalReleasedBytes = 0;
for (uptr I = 0; I < NumClasses; I++) {
if (I == SizeClassMap::BatchClassId)
Expand Down Expand Up @@ -1056,6 +1058,8 @@ uptr SizeClassAllocator32<Config>::releaseToOSMaybe(SizeClassInfo *Sci,
uptr ClassId,
ReleaseToOS ReleaseType)
REQUIRES(Sci->Mutex) {
SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSMaybeTraceName(ReleaseType));

const uptr BlockSize = getSizeByClassId(ClassId);

DCHECK_GE(Sci->FreeListInfo.PoppedBlocks, Sci->FreeListInfo.PushedBlocks);
Expand Down
5 changes: 5 additions & 0 deletions compiler-rt/lib/scudo/standalone/primary64.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "stats.h"
#include "string_utils.h"
#include "thread_annotations.h"
#include "tracing.h"

namespace scudo {

Expand Down Expand Up @@ -1307,6 +1308,8 @@ uptr SizeClassAllocator64<Config>::tryReleaseToOS(uptr ClassId,

template <typename Config>
uptr SizeClassAllocator64<Config>::releaseToOS(ReleaseToOS ReleaseType) {
SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSTraceName(ReleaseType));

uptr TotalReleasedBytes = 0;
for (uptr I = 0; I < NumClasses; I++) {
if (I == SizeClassMap::BatchClassId)
Expand Down Expand Up @@ -1376,6 +1379,8 @@ uptr SizeClassAllocator64<Config>::releaseToOSMaybe(RegionInfo *Region,
uptr ClassId,
ReleaseToOS ReleaseType)
REQUIRES(Region->MMLock) EXCLUDES(Region->FLLock) {
SCUDO_SCOPED_TRACE(GetPrimaryReleaseToOSMaybeTraceName(ReleaseType));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq, are we able to log the ClassId in trace?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that helps as an inspiration, Fuchsia's tracing system lets you attach arbitrary arguments (including integers) to trace events. But it relies on sophisticated C preprocessing (example and implementation).

Maybe we could expose individual trace points directly as macros to give maximum flexibility? For instance

#if defined(SCUDO_ENABLE_TRACING)
#include "custom_scudo_tracing.h"
#else
#define TRACE_PRIMARY_RELEASE_TO_OS_MAYBE(ClassId)
... // and so on, one empty macro (instead of a NULL-returning function) per trace point
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can work on getting better trace data after this. I would like a better way to generate the names that can happen at compile time. I'll definitely look at that for a follow-on cl.


const uptr BlockSize = getSizeByClassId(ClassId);
uptr BytesInFreeList;
const uptr AllocatedUserEnd =
Expand Down
14 changes: 11 additions & 3 deletions compiler-rt/lib/scudo/standalone/secondary.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "stats.h"
#include "string_utils.h"
#include "thread_annotations.h"
#include "tracing.h"
#include "vector.h"

namespace scudo {
Expand Down Expand Up @@ -118,7 +119,7 @@ template <typename Config> class MapAllocatorNoCache {
bool canCache(UNUSED uptr Size) { return false; }
void disable() {}
void enable() {}
void releaseToOS() {}
void releaseToOS(ReleaseToOS) {}
void disableMemoryTagging() {}
void unmapTestOnly() {}
bool setOption(Option O, UNUSED sptr Value) {
Expand Down Expand Up @@ -351,6 +352,9 @@ class MapAllocatorCache {
// same time will not actually release any extra elements. Therefore,
// let any other thread continue, skipping the release.
if (Mutex.tryLock()) {
SCUDO_SCOPED_TRACE(
GetSecondaryReleaseToOSTraceName(ReleaseToOS::Normal));

// TODO: Add ReleaseToOS logic to LRU algorithm
releaseOlderThan(Time - static_cast<u64>(Interval) * 1000000);
Mutex.unlock();
Expand Down Expand Up @@ -499,7 +503,9 @@ class MapAllocatorCache {
return true;
}

void releaseToOS() EXCLUDES(Mutex) {
void releaseToOS([[maybe_unused]] ReleaseToOS ReleaseType) EXCLUDES(Mutex) {
SCUDO_SCOPED_TRACE(GetSecondaryReleaseToOSTraceName(ReleaseType));

// Since this is a request to release everything, always wait for the
// lock so that we guarantee all entries are released after this call.
ScopedLock L(Mutex);
Expand Down Expand Up @@ -574,6 +580,8 @@ class MapAllocatorCache {
}

void releaseOlderThan(u64 Time) REQUIRES(Mutex) {
SCUDO_SCOPED_TRACE(GetSecondaryReleaseOlderThan());

if (!LRUEntries.size() || OldestTime == 0 || OldestTime > Time)
return;
OldestTime = 0;
Expand Down Expand Up @@ -669,7 +677,7 @@ template <typename Config> class MapAllocator {

bool setOption(Option O, sptr Value) { return Cache.setOption(O, Value); }

void releaseToOS() { Cache.releaseToOS(); }
void releaseToOS(ReleaseToOS ReleaseType) { Cache.releaseToOS(ReleaseType); }

void disableMemoryTagging() { Cache.disableMemoryTagging(); }

Expand Down
48 changes: 48 additions & 0 deletions compiler-rt/lib/scudo/standalone/tracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//===-- tracing.h -----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef SCUDO_TRACING_H_
#define SCUDO_TRACING_H_

#if defined(SCUDO_ENABLE_TRACING)

// This file must include definitions for all of the functions below.
#include "custom_scudo_tracing.h"

#else

// Should start a trace in the given scope, and end the trace when going out of
// scope.
#define SCUDO_SCOPED_TRACE(Name)

// Create a trace name for the call to releaseToOS.
static inline const char *GetReleaseToOSTraceName(scudo::ReleaseToOS) {
return nullptr;
}

// Create a trace name for the call to releaseToOSMaybe in the primary.
static inline const char *
GetPrimaryReleaseToOSMaybeTraceName(scudo::ReleaseToOS) {
return nullptr;
}

static inline const char *GetPrimaryReleaseToOSTraceName(scudo::ReleaseToOS) {
return nullptr;
}

// Create a trace name for the call to releaseToOS in the secondary.
static inline const char *GetSecondaryReleaseToOSTraceName(scudo::ReleaseToOS) {
return nullptr;
}

// Create a trace name for the call to releaseOlderThan in the secondary.
static inline const char *GetSecondaryReleaseOlderThan() { return nullptr; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think you meant GetSecondaryReleaseOlderThanTraceName (should we put all trace names in a dedicated namespace?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this name and the place it was used.


#endif

#endif // SCUDO_TRACING_H_
Loading