Title: [260592] trunk/Source/WebKit
Revision
260592
Author
[email protected]
Date
2020-04-23 13:06:25 -0700 (Thu, 23 Apr 2020)

Log Message

Regression after r260359 ([GTK][WPE] lowWatermarkPages() in MemoryPressureMonitor.cpp only searches the "low" value inside the first "Node" section)
https://bugs.webkit.org/show_bug.cgi?id=210916

Reviewed by Carlos Alberto Lopez Perez.

Switch over to using a FileHandle type based on std::unique_ptr with a custom deleter and an
utility function for opening files. This makes easier to follow the logic inside the polling
loop for opening files and retrying when needed. This also fixes exiting the thread when the
systemMemoryUsedAsPercentage() function would return -1, to loop restart instead to keep
trying.

Thanks to Pablo Saavedra for his help in making this patch.

No new tests needed.

* UIProcess/linux/MemoryPressureMonitor.cpp:
(WebKit::FileHandleDeleter::operator()): Add deleter to use with std::unique_ptr<>.
(WebKit::tryOpeningForUnbufferedReading): Add utility function to open a file handle if
needed and configuring its buffering upon opening.
(WebKit::MemoryPressureMonitor::start): Use FileHandle to ensure that handles are always
closed properly, and fix logic retry opening files on failure.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (260591 => 260592)


--- trunk/Source/WebKit/ChangeLog	2020-04-23 19:57:12 UTC (rev 260591)
+++ trunk/Source/WebKit/ChangeLog	2020-04-23 20:06:25 UTC (rev 260592)
@@ -1,3 +1,27 @@
+2020-04-23  Adrian Perez de Castro  <[email protected]>
+
+        Regression after r260359 ([GTK][WPE] lowWatermarkPages() in MemoryPressureMonitor.cpp only searches the "low" value inside the first "Node" section)
+        https://bugs.webkit.org/show_bug.cgi?id=210916
+
+        Reviewed by Carlos Alberto Lopez Perez.
+
+        Switch over to using a FileHandle type based on std::unique_ptr with a custom deleter and an
+        utility function for opening files. This makes easier to follow the logic inside the polling
+        loop for opening files and retrying when needed. This also fixes exiting the thread when the
+        systemMemoryUsedAsPercentage() function would return -1, to loop restart instead to keep
+        trying.
+
+        Thanks to Pablo Saavedra for his help in making this patch.
+
+        No new tests needed.
+
+        * UIProcess/linux/MemoryPressureMonitor.cpp:
+        (WebKit::FileHandleDeleter::operator()): Add deleter to use with std::unique_ptr<>.
+        (WebKit::tryOpeningForUnbufferedReading): Add utility function to open a file handle if
+        needed and configuring its buffering upon opening.
+        (WebKit::MemoryPressureMonitor::start): Use FileHandle to ensure that handles are always
+        closed properly, and fix logic retry opening files on failure.
+
 2020-04-23  Nikos Mouchtaris  <[email protected]>
 
         Soft link QuickLookThumbnailing framework

Modified: trunk/Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp (260591 => 260592)


--- trunk/Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp	2020-04-23 19:57:12 UTC (rev 260591)
+++ trunk/Source/WebKit/UIProcess/linux/MemoryPressureMonitor.cpp	2020-04-23 20:06:25 UTC (rev 260592)
@@ -297,6 +297,29 @@
     return memoryMonitor;
 }
 
+struct FileHandleDeleter {
+    void operator()(FILE* f) { fclose(f); }
+};
+
+using FileHandle = std::unique_ptr<FILE, FileHandleDeleter>;
+
+static bool tryOpeningForUnbufferedReading(FileHandle& handle, const char* filePath)
+{
+    // Check whether the file handle is already valid.
+    if (handle)
+        return true;
+
+    // Else, try opening it and disable buffering after opening.
+    if (auto* f = fopen(filePath, "r")) {
+        setbuf(f, nullptr);
+        handle.reset(f);
+        return true;
+    }
+
+    // Could not produce a valid handle.
+    return false;
+}
+
 void MemoryPressureMonitor::start()
 {
     if (m_started)
@@ -305,31 +328,27 @@
     m_started = true;
 
     Thread::create("MemoryPressureMonitor", [] {
-        FILE* memInfoFile;
-        FILE* zoneInfoFile;
-        FILE* cgroupControllerFile;
+        FileHandle memInfoFile, zoneInfoFile, cgroupControllerFile;
         CGroupMemoryController memoryController = CGroupMemoryController();
         Seconds pollInterval = s_maxPollingInterval;
         while (true) {
             sleep(pollInterval);
 
-            memInfoFile = fopen(s_procMeminfo, "r");
-            zoneInfoFile = fopen(s_procZoneinfo, "r");
-            if (!memInfoFile || !zoneInfoFile)
+            // Cannot operate without this one, retry opening on the next iteration after sleeping.
+            if (!tryOpeningForUnbufferedReading(memInfoFile, s_procMeminfo))
                 continue;
-            setbuf(memInfoFile, nullptr);
-            setbuf(zoneInfoFile, nullptr);
 
-            cgroupControllerFile = fopen(s_procSelfCgroup, "r");
-            if (cgroupControllerFile)
-                setbuf(cgroupControllerFile, nullptr);
+            // The monitor can work without these two, but it will be more precise if thy are eventually opened: keep trying.
+            tryOpeningForUnbufferedReading(zoneInfoFile, s_procZoneinfo);
+            tryOpeningForUnbufferedReading(cgroupControllerFile, s_procSelfCgroup);
 
-            CString cgroupMemoryControllerPath = getCgroupControllerPath(cgroupControllerFile, "memory");
+            CString cgroupMemoryControllerPath = getCgroupControllerPath(cgroupControllerFile.get(), "memory");
             memoryController.setMemoryControllerPath(cgroupMemoryControllerPath);
-            int usedPercentage = systemMemoryUsedAsPercentage(memInfoFile, zoneInfoFile, &memoryController);
+            int usedPercentage = systemMemoryUsedAsPercentage(memInfoFile.get(), zoneInfoFile.get(), &memoryController);
             if (usedPercentage == -1) {
                 WTFLogAlways("Failed to get the memory usage");
-                break;
+                pollInterval = s_maxPollingInterval;
+                continue;
             }
 
             if (usedPercentage >= s_memoryPresurePercentageThreshold) {
@@ -339,12 +358,6 @@
             }
             pollInterval = pollIntervalForUsedMemoryPercentage(usedPercentage);
         }
-        if (memInfoFile)
-            fclose(memInfoFile);
-        if (zoneInfoFile)
-            fclose(zoneInfoFile);
-        if (cgroupControllerFile)
-            fclose(cgroupControllerFile);
     })->detach();
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to