[Lldb-commits] [lldb] [lldb-dap] Implement a MemoryMonitor (PR #129332)
Walter Erquinigo via lldb-commits
lldb-commits at lists.llvm.org
Wed Mar 5 17:39:35 PST 2025
================
@@ -0,0 +1,110 @@
+//===-- 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 "lldb/Host/MemoryMonitor.h"
+#include "lldb/Host/HostThread.h"
+#include "lldb/Host/ThreadLauncher.h"
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+#include "llvm/ADT/ScopeExit.h"
+#include "llvm/Support/Error.h"
+#include <atomic>
+#include <cstddef>
+#include <cstdio>
+#include <cstring>
+
+#if defined(__linux__)
+#include <fcntl.h>
+#include <poll.h>
+#include <unistd.h>
+#endif
+
+#if defined(_WIN32)
+#include <memoryapi.h>
+#include <synchapi.h>
+#endif
+
+using namespace lldb_private;
+
+class MemoryMonitorPoll : public MemoryMonitor {
+public:
+ using MemoryMonitor::MemoryMonitor;
+
+ lldb::thread_result_t MonitorThread() {
+#if defined(__linux__)
+ 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 (!m_done) {
+ int n = poll(&fds, 1, g_timeout);
+ if (n > 0) {
+ if (fds.revents & POLLERR)
----------------
walter-erquinigo wrote:
if there's an error, is there a way to restart the loop?
https://github.com/llvm/llvm-project/pull/129332
More information about the lldb-commits
mailing list