================
@@ -13,54 +13,109 @@
 #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 <sys/eventfd.h>
+#include <sys/poll.h>
 #include <unistd.h>
 #endif
 
 #if defined(_WIN32)
+#include <atomic>
 #include <windows.h>
 #endif
 
 using namespace lldb_private;
 
-class MemoryMonitorPoll : public MemoryMonitor {
+#if defined(__linux__)
+class MemoryMonitorLinux : 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)
+    constexpr size_t pressure_idx = 0;
+    constexpr size_t stop_idx = 1;
+    constexpr size_t fd_count = 2;
+    std::array<pollfd, fd_count> pfds{};
+
+    // Setup stop file descriptor.
+    m_stop_fd = ::eventfd(0, O_NONBLOCK);
+    if (m_stop_fd < 0)
+      return {};
+    pfds[stop_idx].fd = m_stop_fd;
+    pfds[stop_idx].events = POLLIN;
+
+    // Setup pressure file descriptor.
+    pfds[pressure_idx].fd =
+        ::open("/proc/pressure/memory", O_RDWR | O_NONBLOCK);
+    if (pfds[pressure_idx].fd < 0) {
+      ::close(m_stop_fd);
       return {};
-    fds.events = POLLPRI;
+    }
+    pfds[pressure_idx].events = POLLPRI;
 
-    llvm::scope_exit cleanup([&]() { close(fds.fd); });
+    llvm::scope_exit cleanup([&]() {
+      ::close(pfds[pressure_idx].fd);
+      ::close(m_stop_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)
+    constexpr llvm::StringRef trigger = "some 50000 2000000";
----------------
JDevlieghere wrote:

```suggestion
    constexpr llvm::StringLiteral trigger = "some 50000 2000000";
```

https://github.com/llvm/llvm-project/pull/178083
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to