Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeviceSanitizer] Memory overhead statistics #1869

Merged
merged 24 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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: 4 additions & 0 deletions source/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,16 @@ if(UR_ENABLE_SANITIZER)
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_interceptor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_interceptor.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_libdevice.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_options.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_options.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_quarantine.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_quarantine.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_report.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_report.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_shadow_setup.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_shadow_setup.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_statistics.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_statistics.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_validator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_validator.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/common.hpp
Expand Down
1 change: 1 addition & 0 deletions source/loader/layers/sanitizer/asan_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct AllocInfo {
StackTrace ReleaseStack;

void print();
size_t getRedzoneSize() { return AllocSize - (UserEnd - UserBegin); }
};

using AllocationMap = std::map<uptr, std::shared_ptr<AllocInfo>>;
Expand Down
111 changes: 73 additions & 38 deletions source/loader/layers/sanitizer/asan_interceptor.cpp

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions source/loader/layers/sanitizer/asan_interceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "asan_allocator.hpp"
#include "asan_buffer.hpp"
#include "asan_libdevice.hpp"
#include "asan_options.hpp"
#include "asan_statistics.hpp"
#include "common.hpp"
#include "ur_sanitizer_layer.hpp"

Expand Down Expand Up @@ -111,6 +113,8 @@ struct ContextInfo {
std::vector<ur_device_handle_t> DeviceList;
std::unordered_map<ur_device_handle_t, AllocInfoList> AllocInfosMap;

AsanStatsWrapper Stats;

explicit ContextInfo(ur_context_handle_t Context) : Handle(Context) {
[[maybe_unused]] auto Result =
getContext()->urDdiTable.Context.pfnRetain(Context);
Expand Down Expand Up @@ -163,7 +167,7 @@ struct DeviceGlobalInfo {

class SanitizerInterceptor {
public:
explicit SanitizerInterceptor(logger::Logger &logger);
explicit SanitizerInterceptor();

~SanitizerInterceptor();

Expand Down Expand Up @@ -233,17 +237,19 @@ class SanitizerInterceptor {
return m_KernelMap[Kernel];
}

const AsanOptions &getOptions() { return m_Options; }

private:
ur_result_t updateShadowMemory(std::shared_ptr<ContextInfo> &ContextInfo,
std::shared_ptr<DeviceInfo> &DeviceInfo,
ur_queue_handle_t Queue);
ur_result_t enqueueAllocInfo(ur_context_handle_t Context,
ur_result_t enqueueAllocInfo(std::shared_ptr<ContextInfo> &ContextInfo,
std::shared_ptr<DeviceInfo> &DeviceInfo,
ur_queue_handle_t Queue,
std::shared_ptr<AllocInfo> &AI);

/// Initialize Global Variables & Kernel Name at first Launch
ur_result_t prepareLaunch(ur_context_handle_t Context,
ur_result_t prepareLaunch(std::shared_ptr<ContextInfo> &ContextInfo,
std::shared_ptr<DeviceInfo> &DeviceInfo,
ur_queue_handle_t Queue,
ur_kernel_handle_t Kernel,
Expand Down Expand Up @@ -273,7 +279,8 @@ class SanitizerInterceptor {
ur_shared_mutex m_AllocationMapMutex;

std::unique_ptr<Quarantine> m_Quarantine;
logger::Logger &logger;

AsanOptions m_Options;

std::unordered_set<ur_adapter_handle_t> m_Adapters;
ur_shared_mutex m_AdaptersMutex;
Expand Down
142 changes: 142 additions & 0 deletions source/loader/layers/sanitizer/asan_options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
*
* Copyright (C) 2024 Intel Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
* See LICENSE.TXT
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
* @file asan_options.cpp
*
*/

#include "asan_options.hpp"
#include "ur_sanitizer_layer.hpp"

#include <algorithm>
#include <cstring>
#include <stdexcept>

namespace ur_sanitizer_layer {

AsanOptions::AsanOptions() {
std::optional<EnvVarMap> OptionsEnvMap;
try {
OptionsEnvMap = getenv_to_map("UR_LAYER_ASAN_OPTIONS");
} catch (const std::invalid_argument &e) {
std::stringstream SS;
SS << "<SANITIZER>[ERROR]: ";
SS << e.what();
getContext()->logger.always(SS.str().c_str());
die("Sanitizer failed to parse options.\n");
}

if (!OptionsEnvMap.has_value()) {
return;
}

const char *TrueStrings[] = {"1", "true"};
const char *FalseStrings[] = {"0", "false"};

auto InplaceToLower = [](std::string &S) {
std::transform(S.begin(), S.end(), S.begin(),
[](unsigned char C) { return std::tolower(C); });
};
auto IsTrue = [&](const std::string &S) {
return std::any_of(std::begin(TrueStrings), std::end(TrueStrings),
[&](const char *CS) { return S == CS; });
};
auto IsFalse = [&](const std::string &S) {
return std::any_of(std::begin(FalseStrings), std::end(FalseStrings),
[&](const char *CS) { return S == CS; });
};

auto SetBoolOption = [&](const std::string &Name, bool &Opt) {
auto KV = OptionsEnvMap->find(Name);
if (KV != OptionsEnvMap->end()) {
auto Value = KV->second.front();
InplaceToLower(Value);
if (IsTrue(Value)) {
Opt = true;
} else if (IsFalse(Value)) {
Opt = false;
} else {
std::stringstream SS;
SS << "\"" << Name << "\" is set to \"" << Value
<< "\", which is not an valid setting. ";
SS << "Acceptable input are: for enable, use:";
for (auto &S : TrueStrings) {
SS << " \"" << S << "\"";
}
SS << "; ";
SS << "for disable, use:";
for (auto &S : FalseStrings) {
SS << " \"" << S << "\"";
}
SS << ".";
getContext()->logger.error(SS.str().c_str());
die("Sanitizer failed to parse options.\n");
}
}
};

SetBoolOption("debug", Debug);
SetBoolOption("detect_kernel_arguments", DetectKernelArguments);
SetBoolOption("detect_locals", DetectLocals);
SetBoolOption("detect_privates", DetectPrivates);
SetBoolOption("print_stats", PrintStats);

auto KV = OptionsEnvMap->find("quarantine_size_mb");
if (KV != OptionsEnvMap->end()) {
const auto &Value = KV->second.front();
try {
auto temp_long = std::stoul(Value);
if (temp_long > UINT32_MAX) {
throw std::out_of_range("");
}
MaxQuarantineSizeMB = temp_long;
} catch (...) {
getContext()->logger.error("\"quarantine_size_mb\" should be "
"an integer in range[0, {}].",
UINT32_MAX);
die("Sanitizer failed to parse options.\n");
}
}

KV = OptionsEnvMap->find("redzone");
if (KV != OptionsEnvMap->end()) {
const auto &Value = KV->second.front();
try {
MinRZSize = std::stoul(Value);
if (MinRZSize < 16) {
MinRZSize = 16;
getContext()->logger.warning("Trying to set redzone size to a "
AllanZyne marked this conversation as resolved.
Show resolved Hide resolved
"value less than 16 is ignored.");
}
} catch (...) {
getContext()->logger.error(
"\"redzone\" should be an integer in range[0, 16].");
die("Sanitizer failed to parse options.\n");
}
}

KV = OptionsEnvMap->find("max_redzone");
if (KV != OptionsEnvMap->end()) {
const auto &Value = KV->second.front();
try {
MaxRZSize = std::stoul(Value);
if (MaxRZSize > 2048) {
MaxRZSize = 2048;
getContext()->logger.warning(
"Trying to set max redzone size to a "
"value greater than 2048 is ignored.");
}
} catch (...) {
getContext()->logger.error(
"\"max_redzone\" should be an integer in range[0, 2048].");
die("Sanitizer failed to parse options.\n");
}
}
}

} // namespace ur_sanitizer_layer
141 changes: 3 additions & 138 deletions source/loader/layers/sanitizer/asan_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,156 +12,21 @@

#pragma once

#include "common/ur_util.hpp"
#include "ur/ur.hpp"
#include "ur_sanitizer_layer.hpp"

#include <algorithm>
#include <cstring>
#include <stdexcept>
#include "common.hpp"

namespace ur_sanitizer_layer {

struct AsanOptions {
public:
AsanOptions(AsanOptions &other) = delete;
void operator=(const AsanOptions &) = delete;

static AsanOptions &getInstance(logger::Logger &logger) {
static AsanOptions instance(logger);
return instance;
}

bool Debug = false;
uint64_t MinRZSize = 16;
uint64_t MaxRZSize = 2048;
uint32_t MaxQuarantineSizeMB = 0;
bool DetectLocals = true;
bool DetectPrivates = true;
bool PrintStats = false;
bool DetectKernelArguments = true;

private:
AsanOptions(logger::Logger &logger) {
std::optional<EnvVarMap> OptionsEnvMap;
try {
OptionsEnvMap = getenv_to_map("UR_LAYER_ASAN_OPTIONS");
} catch (const std::invalid_argument &e) {
std::stringstream SS;
SS << "<SANITIZER>[ERROR]: ";
SS << e.what();
logger.always(SS.str().c_str());
die("Sanitizer failed to parse options.\n");
}

if (!OptionsEnvMap.has_value()) {
return;
}

const char *TrueStrings[] = {"1", "true"};
const char *FalseStrings[] = {"0", "false"};

auto InplaceToLower = [](std::string &S) {
std::transform(S.begin(), S.end(), S.begin(),
[](unsigned char C) { return std::tolower(C); });
};
auto IsTrue = [&](const std::string &S) {
return std::any_of(std::begin(TrueStrings), std::end(TrueStrings),
[&](const char *CS) { return S == CS; });
};
auto IsFalse = [&](const std::string &S) {
return std::any_of(std::begin(FalseStrings), std::end(FalseStrings),
[&](const char *CS) { return S == CS; });
};

auto SetBoolOption = [&](const std::string &Name, bool &Opt) {
auto KV = OptionsEnvMap->find(Name);
if (KV != OptionsEnvMap->end()) {
auto Value = KV->second.front();
InplaceToLower(Value);
if (IsTrue(Value)) {
Opt = true;
} else if (IsFalse(Value)) {
Opt = false;
} else {
std::stringstream SS;
SS << "\"" << Name << "\" is set to \"" << Value
<< "\", which is not an valid setting. ";
SS << "Acceptable input are: for enable, use:";
for (auto &S : TrueStrings) {
SS << " \"" << S << "\"";
}
SS << "; ";
SS << "for disable, use:";
for (auto &S : FalseStrings) {
SS << " \"" << S << "\"";
}
SS << ".";
logger.error(SS.str().c_str());
die("Sanitizer failed to parse options.\n");
}
}
};

SetBoolOption("debug", Debug);
SetBoolOption("detect_locals", DetectLocals);
SetBoolOption("detect_privates", DetectPrivates);
SetBoolOption("detect_kernel_arguments", DetectKernelArguments);

auto KV = OptionsEnvMap->find("quarantine_size_mb");
if (KV != OptionsEnvMap->end()) {
const auto &Value = KV->second.front();
try {
auto temp_long = std::stoul(Value);
if (temp_long > UINT32_MAX) {
throw std::out_of_range("");
}
MaxQuarantineSizeMB = temp_long;
} catch (...) {
logger.error("\"quarantine_size_mb\" should be "
"an integer in range[0, {}].",
UINT32_MAX);
die("Sanitizer failed to parse options.\n");
}
}

KV = OptionsEnvMap->find("redzone");
if (KV != OptionsEnvMap->end()) {
const auto &Value = KV->second.front();
try {
MinRZSize = std::stoul(Value);
if (MinRZSize < 16) {
MinRZSize = 16;
logger.warning("Trying to set redzone size to a "
"value less than 16 is ignored.");
}
} catch (...) {
logger.error(
"\"redzone\" should be an integer in range[0, 16].");
die("Sanitizer failed to parse options.\n");
}
}

KV = OptionsEnvMap->find("max_redzone");
if (KV != OptionsEnvMap->end()) {
const auto &Value = KV->second.front();
try {
MaxRZSize = std::stoul(Value);
if (MaxRZSize > 2048) {
MaxRZSize = 2048;
logger.warning("Trying to set max redzone size to a "
"value greater than 2048 is ignored.");
}
} catch (...) {
logger.error(
"\"max_redzone\" should be an integer in range[0, 2048].");
die("Sanitizer failed to parse options.\n");
}
}
}
explicit AsanOptions();
};

inline const AsanOptions &Options(logger::Logger &logger) {
return AsanOptions::getInstance(logger);
}

} // namespace ur_sanitizer_layer
Loading
Loading