Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZyne committed Sep 23, 2024
1 parent c7fc52d commit 5b014c5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
4 changes: 1 addition & 3 deletions source/loader/layers/sanitizer/asan_interceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ ur_result_t enqueueMemSetShadow(std::shared_ptr<ContextInfo> &ContextInfo,
} // namespace

ContextInfo::~ContextInfo() {
if (getContext()->interceptor->getOptions().PrintStats) {
Stats.Print(Handle);
}
Stats.Print(Handle);

[[maybe_unused]] auto Result =
getContext()->urDdiTable.Context.pfnRelease(Handle);
Expand Down
2 changes: 1 addition & 1 deletion source/loader/layers/sanitizer/asan_interceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct ContextInfo {
std::vector<ur_device_handle_t> DeviceList;
std::unordered_map<ur_device_handle_t, AllocInfoList> AllocInfosMap;

AsanStats Stats;
AsanStatsWrapper Stats;

explicit ContextInfo(ur_context_handle_t Context) : Handle(Context) {
[[maybe_unused]] auto Result =
Expand Down
76 changes: 76 additions & 0 deletions source/loader/layers/sanitizer/asan_statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@

namespace ur_sanitizer_layer {

struct AsanStats {
void UpdateUSMMalloced(uptr MallocedSize, uptr RedzoneSize);
void UpdateUSMFreed(uptr FreedSize);
void UpdateUSMRealFreed(uptr FreedSize, uptr RedzoneSize);

void UpdateShadowMmaped(uptr ShadowSize);
void UpdateShadowMalloced(uptr ShadowSize);
void UpdateShadowFreed(uptr ShadowSize);

void Print(ur_context_handle_t Context);

private:
std::atomic<uptr> UsmMalloced;
std::atomic<uptr> UsmMallocedRedzones;

// Quarantined memory
std::atomic<uptr> UsmFreed;

std::atomic<uptr> ShadowMmaped;
std::atomic<uptr> ShadowMalloced;

double Overhead = 0.0;

void UpdateOverhead();
};

void AsanStats::Print(ur_context_handle_t Context) {
getContext()->logger.always("Stats: Context {}", (void *)Context);
getContext()->logger.always("Stats: peak memory overhead: {}%",
Expand Down Expand Up @@ -79,4 +105,54 @@ void AsanStats::UpdateOverhead() {
Overhead = std::max(Overhead, NewOverhead);
}

void AsanStatsWrapper::UpdateUSMMalloced(uptr MallocedSize, uptr RedzoneSize) {
if (Stat) {
Stat->UpdateUSMMalloced(MallocedSize, RedzoneSize);
}
}

void AsanStatsWrapper::UpdateUSMFreed(uptr FreedSize) {
if (Stat) {
Stat->UpdateUSMFreed(FreedSize);
}
}

void AsanStatsWrapper::UpdateUSMRealFreed(uptr FreedSize, uptr RedzoneSize) {
if (Stat) {
Stat->UpdateUSMRealFreed(FreedSize, RedzoneSize);
}
}

void AsanStatsWrapper::UpdateShadowMmaped(uptr ShadowSize) {
if (Stat) {
Stat->UpdateShadowMmaped(ShadowSize);
}
}

void AsanStatsWrapper::UpdateShadowMalloced(uptr ShadowSize) {
if (Stat) {
Stat->UpdateShadowMalloced(ShadowSize);
}
}

void AsanStatsWrapper::UpdateShadowFreed(uptr ShadowSize) {
if (Stat) {
Stat->UpdateShadowFreed(ShadowSize);
}
}

void AsanStatsWrapper::Print(ur_context_handle_t Context) {
if (Stat) {
Stat->Print(Context);
}
}

AsanStatsWrapper::AsanStatsWrapper() : Stat(nullptr) {
if (getContext()->interceptor->getOptions().PrintStats) {
Stat = new AsanStats;
}
}

AsanStatsWrapper::~AsanStatsWrapper() { delete Stat; }

} // namespace ur_sanitizer_layer
21 changes: 8 additions & 13 deletions source/loader/layers/sanitizer/asan_statistics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

namespace ur_sanitizer_layer {

struct AsanStats {
struct AsanStats;

struct AsanStatsWrapper {

AsanStatsWrapper();
~AsanStatsWrapper();

void UpdateUSMMalloced(uptr MallocedSize, uptr RedzoneSize);
void UpdateUSMFreed(uptr FreedSize);
void UpdateUSMRealFreed(uptr FreedSize, uptr RedzoneSize);
Expand All @@ -29,18 +35,7 @@ struct AsanStats {
void Print(ur_context_handle_t Context);

private:
std::atomic<uptr> UsmMalloced;
std::atomic<uptr> UsmMallocedRedzones;

// Quarantined memory
std::atomic<uptr> UsmFreed;

std::atomic<uptr> ShadowMmaped;
std::atomic<uptr> ShadowMalloced;

double Overhead = 0.0;

void UpdateOverhead();
AsanStats *Stat;
};

} // namespace ur_sanitizer_layer

0 comments on commit 5b014c5

Please sign in to comment.