Bug 532039 - Improve perf of IsLowMemory. r=bsmedberg.
authorDoug Turner <dougt@mozilla.com>
Wed, 02 Dec 2009 00:24:19 -0800
changeset 35395 410468c50ca16de043a0558babd73fc6e26386c8
parent 35394 11a9c2e01f58e2537ef6ec60129505f0bb61e135
child 35396 e2f77caaa367f905ec5b3820289336da2eaea0d5
push idunknown
push userunknown
push dateunknown
reviewersbsmedberg
bugs532039
milestone1.9.3a1pre
Bug 532039 - Improve perf of IsLowMemory. r=bsmedberg.
xpcom/base/nsMemoryImpl.cpp
--- a/xpcom/base/nsMemoryImpl.cpp
+++ b/xpcom/base/nsMemoryImpl.cpp
@@ -56,17 +56,17 @@
 #if defined(XP_WIN)
 #include <windows.h>
 #endif
 
 #if defined (NS_OSSO)
 #include <osso-mem.h>
 #include <fcntl.h>
 #include <unistd.h>
-const char* kHighMark = "/sys/kernel/high_watermark";
+static const char kHighMark[] = "/sys/kernel/high_watermark";
 #endif
 
 // Some platforms notify you when system memory is low, others do not.
 // In the case of those that do not, we want to post low memory
 // notifications from IsLowMemory().  For those that can notify us, that
 // code usually lives in toolkit.
 #ifdef WINCE
 #define NOTIFY_LOW_MEMORY
@@ -124,24 +124,35 @@ nsMemoryImpl::IsLowMemory(PRBool *result
     *result = PR_FALSE;
 #elif defined(XP_WIN)
     MEMORYSTATUSEX stat;
     stat.dwLength = sizeof stat;
     GlobalMemoryStatusEx(&stat);
     *result = (stat.ullAvailPageFile < kRequiredMemory) &&
         ((float)stat.ullAvailPageFile / stat.ullTotalPageFile) < 0.1;
 #elif defined(NS_OSSO)
-    int fd = open (kHighMark, O_RDONLY);
-    if (fd == -1) {
-        *result = PR_FALSE;
-        return NS_OK;
+    // We see about 1/4 of the cost by caching the fd and using
+    // rewind.  XXX leaking this fd.
+    static int osso_highmark_fd = -1;
+    if (osso_highmark_fd == -1) {
+        osso_highmark_fd = open (kHighMark, O_RDONLY);
+
+        if (osso_highmark_fd == -1) {
+            NS_ERROR("can't find the osso highmark file");    
+            *result = PR_FALSE;
+            return NS_OK;
+        }
     }
+
+    // be kind, rewind.
+    lseek(osso_highmark_fd, 0L, SEEK_SET);
+
     int c = 0;
-    read (fd, &c, 1);
-    close(fd);
+    read (osso_highmark_fd, &c, 1);
+
     *result = (c == '1');
 #else
     *result = PR_FALSE;
 #endif
 
 #ifdef NOTIFY_LOW_MEMORY
     if (*result) {
         sGlobalMemory.FlushMemory(NS_LITERAL_STRING("low-memory").get(), PR_FALSE);