Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ class LinuxMemoryChecker : public PeriodicMemoryChecker {

~LinuxMemoryChecker() override {}

int64_t getUsedMemory() {
return systemUsedMemoryBytes();
}

void setStatFile(std::string statFile) {
memStatFile_ = statFile;
LOG(INFO) << fmt::format(
Expand Down Expand Up @@ -180,7 +176,7 @@ class LinuxMemoryChecker : public PeriodicMemoryChecker {
// value. It may be better than what we currently use. For
// consistency we will match cgroup V1 and change if
// necessary.
int64_t systemUsedMemoryBytes() override {
void loadSystemMemoryUsage() override {
size_t memAvailable = 0;
size_t memTotal = 0;
size_t inactiveAnon = 0;
Expand All @@ -207,7 +203,6 @@ class LinuxMemoryChecker : public PeriodicMemoryChecker {
// Unit is in bytes.
const auto memBytes = inactiveAnon + activeAnon;
cachedSystemUsedMemoryBytes_ = memBytes;
return memBytes;
}

// Last resort use host machine info.
Expand All @@ -231,7 +226,6 @@ class LinuxMemoryChecker : public PeriodicMemoryChecker {
const auto memBytes =
(memAvailable && memTotal) ? memTotal - memAvailable : 0;
cachedSystemUsedMemoryBytes_ = memBytes;
return memBytes;
}

int64_t mallocBytes() const override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void PeriodicMemoryChecker::start() {
scheduler_->setThreadName("MemoryCheckerThread");
scheduler_->addFunction(
[&]() {
loadSystemMemoryUsage();
periodicCb();
if (config_.mallocMemHeapDumpEnabled) {
maybeDumpHeap();
Expand All @@ -92,6 +93,13 @@ void PeriodicMemoryChecker::stop() {
scheduler_.reset();
}

int64_t PeriodicMemoryChecker::systemUsedMemoryBytes(bool fetchFresh) {
if (fetchFresh) {
loadSystemMemoryUsage();
}
return cachedSystemUsedMemoryBytes_;
}

std::string PeriodicMemoryChecker::createHeapDumpFilePath() const {
const size_t now = velox::getCurrentTimeMs() / 1000;
// Format as follow:
Expand Down Expand Up @@ -210,7 +218,9 @@ void PeriodicMemoryChecker::pushbackMemory() {
RECORD_HISTOGRAM_METRIC_VALUE(
kCounterMemoryPushbackLatencyMs, latencyUs / 1000);
const auto actualFreedBytes = std::max<int64_t>(
0, static_cast<int64_t>(currentMemBytes) - systemUsedMemoryBytes());
0,
static_cast<int64_t>(currentMemBytes) -
systemUsedMemoryBytes(/*fetchFresh=*/true));
RECORD_HISTOGRAM_METRIC_VALUE(
kCounterMemoryPushbackExpectedReductionBytes, freedBytes);
RECORD_HISTOGRAM_METRIC_VALUE(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ class PeriodicMemoryChecker {
/// Stops the 'PeriodicMemoryChecker'.
virtual void stop();

/// Returns the last known cached 'current' system memory usage in bytes.
int64_t cachedSystemUsedMemoryBytes() const {
return cachedSystemUsedMemoryBytes_;
}
/// Returns the last known cached 'current' system memory usage in bytes. If
/// 'fetchFresh' is true, retrieves and returns the current system memory usage.
/// The returned value is used to compare with 'Config::systemMemLimitBytes'.
int64_t systemUsedMemoryBytes(bool fetchFresh = false);

protected:
/// Fetches and returns current system memory usage in bytes.
/// The returned value is used to compare with 'Config::systemMemLimitBytes'.
virtual int64_t systemUsedMemoryBytes() = 0;
/// Fetches current system memory usage in bytes and stores it in the cache.
virtual void loadSystemMemoryUsage() = 0;

/// Returns current bytes allocated by malloc. The returned value is used to
/// compare with 'Config::mallocBytesUsageDumpThreshold'
Expand Down
2 changes: 1 addition & 1 deletion presto-native-execution/presto_cpp/main/PrestoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ void PrestoServer::checkOverload() {
systemConfig->workerOverloadedThresholdMemGb() * 1024 * 1024 * 1024;
if (overloadedThresholdMemBytes > 0) {
const auto currentUsedMemoryBytes = (memoryChecker_ != nullptr)
? memoryChecker_->cachedSystemUsedMemoryBytes()
? memoryChecker_->systemUsedMemoryBytes()
: 0;
const bool memOverloaded =
(currentUsedMemoryBytes > overloadedThresholdMemBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ class PeriodicMemoryCheckerTest : public testing::Test {
std::function<void()>&& periodicCb = nullptr,
std::function<bool(const std::string&)>&& heapDumpCb = nullptr)
: PeriodicMemoryChecker(config),
systemUsedMemoryBytes_(systemUsedMemoryBytes),
mallocBytes_(mallocBytes),
periodicCb_(std::move(periodicCb)),
heapDumpCb_(std::move(heapDumpCb)) {}
heapDumpCb_(std::move(heapDumpCb)) {
cachedSystemUsedMemoryBytes_ = systemUsedMemoryBytes;
}

~TestPeriodicMemoryChecker() override {}

Expand All @@ -46,9 +47,7 @@ class PeriodicMemoryCheckerTest : public testing::Test {
}

protected:
int64_t systemUsedMemoryBytes() override {
return systemUsedMemoryBytes_;
}
void loadSystemMemoryUsage() override {}

int64_t mallocBytes() const override {
return mallocBytes_;
Expand All @@ -70,7 +69,6 @@ class PeriodicMemoryCheckerTest : public testing::Test {
void removeDumpFile(const std::string& filePath) const override {}

private:
int64_t systemUsedMemoryBytes_{0};
int64_t mallocBytes_{0};
std::function<void()> periodicCb_;
std::function<bool(const std::string&)> heapDumpCb_;
Expand Down
Loading