Implement memory monitoring functions for Linux OS
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Sep 2019 15:07:19 +0000 (15:07 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Sep 2019 15:07:19 +0000 (15:07 +0000)
https://bugs.webkit.org/show_bug.cgi?id=200391

Patch by Paulo Matos <pmatos@igalia.com> on 2019-09-20
Reviewed by Žan Doberšek.

Source/JavaScriptCore:

* jsc.cpp:

Source/WTF:

* wtf/PlatformGTK.cmake:
* wtf/PlatformJSCOnly.cmake:
* wtf/PlatformWPE.cmake:
* wtf/linux/ProcessMemoryFootprint.h: Added.
(ProcessMemoryFootprint::now):
(ProcessMemoryFootprint::resetPeak):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@250129 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/jsc.cpp
Source/WTF/ChangeLog
Source/WTF/wtf/PlatformGTK.cmake
Source/WTF/wtf/PlatformJSCOnly.cmake
Source/WTF/wtf/PlatformWPE.cmake
Source/WTF/wtf/linux/ProcessMemoryFootprint.h [new file with mode: 0644]

index 0bef602..5759efa 100644 (file)
@@ -1,3 +1,12 @@
+2019-09-20  Paulo Matos  <pmatos@igalia.com>
+
+        Implement memory monitoring functions for Linux OS
+        https://bugs.webkit.org/show_bug.cgi?id=200391
+
+        Reviewed by Žan Doberšek.
+
+        * jsc.cpp:
+
 2019-09-20  Devin Rousso  <drousso@apple.com>
 
         ASSERT NOT REACHED in Inspector::InjectedScriptModule::ensureInjected() seen with inspector/heap/getRemoteObject.html
index 5baf5c6..71777d7 100644 (file)
 
 #if OS(DARWIN)
 #include <wtf/spi/darwin/ProcessMemoryFootprint.h>
+#elif OS(LINUX)
+#include <wtf/linux/ProcessMemoryFootprint.h>
+#endif
+
+#if OS(DARWIN) || OS(LINUX)
 struct MemoryFootprint : ProcessMemoryFootprint {
     MemoryFootprint(const ProcessMemoryFootprint& src)
         : ProcessMemoryFootprint(src)
index 0c5d361..5c9402a 100644 (file)
@@ -1,3 +1,17 @@
+2019-09-20  Paulo Matos  <pmatos@igalia.com>
+
+        Implement memory monitoring functions for Linux OS
+        https://bugs.webkit.org/show_bug.cgi?id=200391
+
+        Reviewed by Žan Doberšek.
+
+        * wtf/PlatformGTK.cmake:
+        * wtf/PlatformJSCOnly.cmake:
+        * wtf/PlatformWPE.cmake:
+        * wtf/linux/ProcessMemoryFootprint.h: Added.
+        (ProcessMemoryFootprint::now):
+        (ProcessMemoryFootprint::resetPeak):
+
 2019-09-20  Libor Bukata  <libor.bukata@oracle.com>
 
         UI process crash when using callOnMainThread() after the main thread dispatcher has been destroyed
index 60b37bd..9dc4552 100644 (file)
@@ -12,6 +12,7 @@ list(APPEND WTF_PUBLIC_HEADERS
 
 if (CMAKE_SYSTEM_NAME MATCHES "Linux")
     list(APPEND WTF_PUBLIC_HEADERS
+        linux/ProcessMemoryFootprint.h
         linux/CurrentProcessMemoryStatus.h
     )
 endif ()
index a3cd8e7..fd36edb 100644 (file)
@@ -85,6 +85,7 @@ elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
         linux/MemoryPressureHandlerLinux.cpp
     )
     list(APPEND WTF_PUBLIC_HEADERS
+        linux/ProcessMemoryFootprint.h
         linux/CurrentProcessMemoryStatus.h
     )
 else ()
index ab273e7..d80be2a 100644 (file)
@@ -7,6 +7,7 @@ list(APPEND WTF_PUBLIC_HEADERS
     glib/RunLoopSourcePriority.h
     glib/WTFGType.h
 
+    linux/ProcessMemoryFootprint.h
     linux/CurrentProcessMemoryStatus.h
 )
 
diff --git a/Source/WTF/wtf/linux/ProcessMemoryFootprint.h b/Source/WTF/wtf/linux/ProcessMemoryFootprint.h
new file mode 100644 (file)
index 0000000..a1893e9
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2019 Metrological Group B.V.
+ * Copyright (C) 2019 Igalia S.L.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if OS(LINUX)
+
+#include <sys/resource.h>
+#include <wtf/linux/CurrentProcessMemoryStatus.h>
+
+struct ProcessMemoryFootprint {
+    uint64_t current;
+    uint64_t peak;
+
+    static ProcessMemoryFootprint now()
+    {
+        struct rusage ru;
+        getrusage(RUSAGE_SELF, &ru);
+
+        ProcessMemoryStatus ps;
+        currentProcessMemoryStatus(ps);
+
+        return { ps.resident, static_cast<uint64_t>(ru.ru_maxrss) * 1024 };
+    }
+
+    static void resetPeak()
+    {
+        // To reset the peak size, we need to write 5 to /proc/self/clear_refs
+        // as described in `man -s5 proc`, in the clear_refs section.
+        // Only available since 4.0.
+        FILE* f = fopen("/proc/self/clear_refs", "w");
+        if (!f)
+            return;
+        fwrite("5", 1, 1, f);
+        fclose(f);
+    }
+};
+
+#endif