-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[lldb-dap] Implement a MemoryMonitor #129332
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c63350d
[lldb-dap] Implement a MemoryMonitor for macOS & Linux
JDevlieghere 5959487
Use the dispatch_get_global_queue and call dispatch_release
JDevlieghere 0563235
Move MemoryMonitor into Host
JDevlieghere 9209e10
Use NativeThread and attempt a Windows implementation
JDevlieghere f427808
Include windows.h instead of memoryapi.h and synchapi.h
JDevlieghere ad1e211
Log when memory pressure is detected
JDevlieghere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//===-- MemoryMonitor.cpp -------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "MemoryMonitor.h" | ||
#include "llvm/ADT/ScopeExit.h" | ||
#include <atomic> | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <thread> | ||
|
||
#if defined(__APPLE__) | ||
#include <dispatch/dispatch.h> | ||
#endif | ||
|
||
#if defined(__linux__) | ||
#include <fcntl.h> | ||
#include <poll.h> | ||
#include <unistd.h> | ||
#endif | ||
|
||
using namespace lldb_dap; | ||
|
||
#if defined(__APPLE__) | ||
class MemoryMonitorDarwin : public MemoryMonitor { | ||
using MemoryMonitor::MemoryMonitor; | ||
void Start() override { | ||
m_memory_pressure_source = dispatch_source_create( | ||
DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, | ||
0, // system-wide monitoring | ||
DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL, | ||
dispatch_get_main_queue()); | ||
|
||
dispatch_source_set_event_handler(m_memory_pressure_source, ^{ | ||
dispatch_source_memorypressure_flags_t pressureLevel = | ||
dispatch_source_get_data(m_memory_pressure_source); | ||
if (pressureLevel & | ||
(DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL)) { | ||
m_callback(); | ||
} | ||
}); | ||
} | ||
|
||
void Stop() override { dispatch_source_cancel(m_memory_pressure_source); } | ||
JDevlieghere marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private: | ||
dispatch_source_t m_memory_pressure_source; | ||
}; | ||
#endif | ||
|
||
#if defined(__linux__) | ||
static void MonitorThread(std::atomic<bool> &done, | ||
MemoryMonitor::Callback callback) { | ||
struct pollfd fds; | ||
fds.fd = open("/proc/pressure/memory", O_RDWR | O_NONBLOCK); | ||
if (fds.fd < 0) | ||
return; | ||
fds.events = POLLPRI; | ||
|
||
auto cleanup = llvm::make_scope_exit([&]() { close(fds.fd); }); | ||
|
||
// Detect a 50ms stall in a 2 second time window. | ||
const char trig[] = "some 50000 2000000"; | ||
if (write(fds.fd, trig, strlen(trig) + 1) < 0) | ||
return; | ||
|
||
while (!done) { | ||
int n = poll(&fds, 1, 1000); | ||
if (n > 0) { | ||
if (fds.revents & POLLERR) | ||
return; | ||
if (fds.revents & POLLPRI) | ||
callback(); | ||
} | ||
} | ||
} | ||
|
||
class MemoryMonitorLinux : public MemoryMonitor { | ||
public: | ||
using MemoryMonitor::MemoryMonitor; | ||
|
||
void Start() override { | ||
m_memory_pressure_thread = | ||
std::thread(MonitorThread, std::ref(m_done), m_callback); | ||
} | ||
|
||
void Stop() override { | ||
if (m_memory_pressure_thread.joinable()) { | ||
m_done = true; | ||
m_memory_pressure_thread.join(); | ||
} | ||
} | ||
|
||
private: | ||
std::atomic<bool> m_done = false; | ||
std::thread m_memory_pressure_thread; | ||
}; | ||
#endif | ||
|
||
std::unique_ptr<MemoryMonitor> MemoryMonitor::Create(Callback callback) { | ||
#if defined(__APPLE__) | ||
return std::make_unique<MemoryMonitorDarwin>(callback); | ||
#endif | ||
|
||
#if defined(__linux__) | ||
return std::make_unique<MemoryMonitorLinux>(callback); | ||
#endif | ||
|
||
return nullptr; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//===-- MemoryMonitor.h ---------------------------------------------------===// | ||
// | ||
// 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 LLDB_TOOLS_LLDB_DAP_WATCHPOINT_H | ||
#define LLDB_TOOLS_LLDB_DAP_WATCHPOINT_H | ||
|
||
#include <functional> | ||
#include <memory> | ||
|
||
namespace lldb_dap { | ||
|
||
class MemoryMonitor { | ||
public: | ||
using Callback = std::function<void()>; | ||
|
||
MemoryMonitor(Callback callback) : m_callback(callback) {} | ||
virtual ~MemoryMonitor() = default; | ||
|
||
/// MemoryMonitor is not copyable. | ||
/// @{ | ||
MemoryMonitor(const MemoryMonitor &) = delete; | ||
MemoryMonitor &operator=(const MemoryMonitor &) = delete; | ||
/// @} | ||
|
||
static std::unique_ptr<MemoryMonitor> Create(Callback callback); | ||
|
||
virtual void Start() = 0; | ||
virtual void Stop() = 0; | ||
|
||
protected: | ||
Callback m_callback; | ||
}; | ||
|
||
} // namespace lldb_dap | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.