Bug 1538279 - Extract nsZipArchive's madvise and similar to a helper r=glandium,froydnj
authorDoug Thayer <dothayer@mozilla.com>
Sat, 13 Apr 2019 18:46:04 +0000
changeset 469415 9f73549db05e66e2af402cb6aefe53fc360365e9
parent 469414 247f4004f26458433122642fdef01fe5a91f9e0a
child 469416 05fe1e4224558caf7483c6f9d5f1d9e0a9f5912d
push id35865
push userapavel@mozilla.com
push dateSat, 13 Apr 2019 21:44:49 +0000
treeherdermozilla-central@2c3837b46068 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium, froydnj
bugs1538279
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1538279 - Extract nsZipArchive's madvise and similar to a helper r=glandium,froydnj This is just to make it simpler to use PrefetchVirtualMemory in subsequent patches. Differential Revision: https://phabricator.services.mozilla.com/D26016
modules/libjar/nsZipArchive.cpp
xpcom/glue/MemUtils.cpp
xpcom/glue/MemUtils.h
xpcom/glue/moz.build
xpcom/glue/objs.mozbuild
--- a/modules/libjar/nsZipArchive.cpp
+++ b/modules/libjar/nsZipArchive.cpp
@@ -15,16 +15,17 @@
 #ifdef MOZ_JAR_BROTLI
 #  include "brotli/decode.h"  // brotli
 #endif
 #include "nsISupportsUtils.h"
 #include "prio.h"
 #include "plstr.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/Logging.h"
+#include "mozilla/MemUtils.h"
 #include "mozilla/UniquePtrExtensions.h"
 #include "stdlib.h"
 #include "nsDirectoryService.h"
 #include "nsWildCard.h"
 #include "nsXULAppAPI.h"
 #include "nsZipArchive.h"
 #include "nsString.h"
 #include "prenv.h"
@@ -645,42 +646,18 @@ nsresult nsZipArchive::BuildFileList(PRF
   // Only perform readahead in the parent process. Children processes
   // don't need readahead when the file has already been readahead by
   // the parent process, and readahead only really happens for omni.ja,
   // which is used in the parent process.
   if (XRE_IsParentProcess() && mFd->mLen > ZIPCENTRAL_SIZE &&
       xtolong(startp + centralOffset) == CENTRALSIG) {
     // Success means optimized jar layout from bug 559961 is in effect
     uint32_t readaheadLength = xtolong(startp);
-    if (readaheadLength) {
-#if defined(XP_SOLARIS)
-      posix_madvise(const_cast<uint8_t *>(startp), readaheadLength,
-                    POSIX_MADV_WILLNEED);
-#elif defined(XP_UNIX)
-      madvise(const_cast<uint8_t *>(startp), readaheadLength, MADV_WILLNEED);
-#elif defined(XP_WIN)
-      static auto prefetchVirtualMemory =
-          reinterpret_cast<BOOL(WINAPI *)(HANDLE, ULONG_PTR, PVOID, ULONG)>(
-              GetProcAddress(GetModuleHandle(L"kernel32.dll"),
-                             "PrefetchVirtualMemory"));
-      if (prefetchVirtualMemory) {
-        // Normally, we'd use WIN32_MEMORY_RANGE_ENTRY, but that requires
-        // a different _WIN32_WINNT value before including windows.h, but
-        // that causes complications with unified sources. It's a simple
-        // enough struct anyways.
-        struct {
-          PVOID VirtualAddress;
-          SIZE_T NumberOfBytes;
-        } entry;
-        entry.VirtualAddress = const_cast<uint8_t *>(startp);
-        entry.NumberOfBytes = readaheadLength;
-        prefetchVirtualMemory(GetCurrentProcess(), 1, &entry, 0);
-      }
-#endif
-    }
+    mozilla::MaybePrefetchMemory(const_cast<uint8_t *>(startp),
+                                 readaheadLength);
   } else {
     for (buf = endp - ZIPEND_SIZE; buf > startp; buf--) {
       if (xtolong(buf) == ENDSIG) {
         centralOffset = xtolong(((ZipEnd *)buf)->offset_central_dir);
         break;
       }
     }
   }
new file mode 100644
--- /dev/null
+++ b/xpcom/glue/MemUtils.cpp
@@ -0,0 +1,50 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/MemUtils.h"
+
+#if defined(XP_WIN)
+#  include <windows.h>
+#else
+#  include <sys/mman.h>
+#endif
+
+bool mozilla::MaybePrefetchMemory(uint8_t* aStart, size_t aNumBytes) {
+  if (aNumBytes == 0) {
+    return true;
+  }
+
+#if defined(XP_SOLARIS)
+  posix_madvise(aStart, aNumBytes, POSIX_MADV_WILLNEED);
+  return true;
+#elif defined(XP_UNIX)
+  madvise(aStart, aNumBytes, MADV_WILLNEED);
+  return true;
+#elif defined(XP_WIN)
+  static auto prefetchVirtualMemory =
+      reinterpret_cast<BOOL(WINAPI*)(HANDLE, ULONG_PTR, PVOID, ULONG)>(
+          GetProcAddress(GetModuleHandleW(L"kernel32.dll"),
+                         "PrefetchVirtualMemory"));
+
+  if (prefetchVirtualMemory) {
+    // Normally, we'd use WIN32_MEMORY_RANGE_ENTRY, but that requires
+    // a different _WIN32_WINNT value before including windows.h, but
+    // that causes complications with unified sources. It's a simple
+    // enough struct anyways.
+    struct {
+      PVOID VirtualAddress;
+      SIZE_T NumberOfBytes;
+    } entry;
+    entry.VirtualAddress = aStart;
+    entry.NumberOfBytes = aNumBytes;
+    prefetchVirtualMemory(GetCurrentProcess(), 1, &entry, 0);
+    return true;
+  }
+  return false;
+#else
+  return false;
+#endif
+}
new file mode 100644
--- /dev/null
+++ b/xpcom/glue/MemUtils.h
@@ -0,0 +1,19 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_MemUtils_h
+#define mozilla_MemUtils_h
+
+#include <stddef.h>
+#include <stdint.h>
+
+namespace mozilla {
+
+bool MaybePrefetchMemory(uint8_t* aStart, size_t aNumBytes);
+
+}  // namespace mozilla
+
+#endif
--- a/xpcom/glue/moz.build
+++ b/xpcom/glue/moz.build
@@ -3,9 +3,10 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 DIRS += ['standalone']
 
 EXPORTS.mozilla += [
     'FileUtils.h',
+    'MemUtils.h',
 ]
--- a/xpcom/glue/objs.mozbuild
+++ b/xpcom/glue/objs.mozbuild
@@ -1,16 +1,17 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 xpcom_glue_src_lcppsrcs = [
     'FileUtils.cpp',
+    'MemUtils.cpp',
     'XREAppData.cpp',
 ]
 
 xpcom_glue_src_cppsrcs = [
     '/xpcom/glue/%s' % s for s in xpcom_glue_src_lcppsrcs
 ]
 
 xpcom_gluens_src_lcppsrcs = [