Created
August 2, 2019 09:00
-
-
Save pmatos/ecdc73807a4064a1a6aaf4127ca3ba3e to your computer and use it in GitHub Desktop.
Enable memory footprint dump on JSC in Linux
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
diff --git a/Source/JavaScriptCore/jsc.cpp b/Source/JavaScriptCore/jsc.cpp | |
index ead01e20b7a..fa69e09e84e 100644 | |
--- a/Source/JavaScriptCore/jsc.cpp | |
+++ b/Source/JavaScriptCore/jsc.cpp | |
@@ -140,17 +140,46 @@ | |
#if __has_include(<WebKitAdditions/MemoryFootprint.h>) | |
#include <WebKitAdditions/MemoryFootprint.h> | |
#else | |
+#include <unistd.h> | |
+#include <sys/resource.h> | |
struct MemoryFootprint { | |
uint64_t current; | |
uint64_t peak; | |
static MemoryFootprint now() | |
{ | |
- return { 0L, 0L }; | |
+ struct rusage ru; | |
+ getrusage(RUSAGE_SELF, &ru); | |
+ return { getCurrentMemoryUsage(), static_cast<uint64_t>(ru.ru_maxrss)*1024 }; | |
+ } | |
+ | |
+ static uint64_t getCurrentMemoryUsage() | |
+ { | |
+ long rss = 0L; | |
+ FILE* fp = fopen("/proc/self/statm", "r"); | |
+ if (fp == NULL) | |
+ return 0L; | |
+ if (fscanf(fp, "%*s%ld", &rss) != 1 ) { | |
+ fclose(fp); | |
+ return 0L; | |
+ } | |
+ fclose(fp); | |
+ return static_cast<uint64_t>(rss) * static_cast<uint64_t>(sysconf(_SC_PAGESIZE)); | |
} | |
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 | |
+ FILE *f = fopen("/proc/self/clear_refs", "w"); | |
+ if (!f) { | |
+ fprintf (stderr, "failed to reset peak\n"); | |
+ return; | |
+ } | |
+ int written = write(fileno(f), "5", 1); | |
+ if(written != 1) | |
+ fprintf (stderr, "failed to reset peak\n"); | |
+ fclose(f); | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment