Bug 1584721 - P3. Add ability to make ShmemPool dynamic in size. r=mjf
authorJean-Yves Avenard <jyavenard@mozilla.com>
Tue, 01 Oct 2019 11:24:49 +0000
changeset 495722 68be28f880f3fbb8874698e541165ffe02e6ffdb
parent 495721 b4de320b347971aa9a0e3b7ba3519c72b656b6f8
child 495723 3f6d096c53580132cef7a76884954bfc4efebed4
push id36638
push usershindli@mozilla.com
push dateWed, 02 Oct 2019 03:38:52 +0000
treeherdermozilla-central@cb9bbf38fa45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmjf
bugs1584721
milestone71.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 1584721 - P3. Add ability to make ShmemPool dynamic in size. r=mjf Differential Revision: https://phabricator.services.mozilla.com/D47555
dom/media/systemservices/ShmemPool.cpp
dom/media/systemservices/ShmemPool.h
--- a/dom/media/systemservices/ShmemPool.cpp
+++ b/dom/media/systemservices/ShmemPool.cpp
@@ -1,28 +1,29 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set sw=2 ts=8 et ft=cpp : */
 /* 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/ShmemPool.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/Logging.h"
-#include "mozilla/ShmemPool.h"
 #include "mozilla/Move.h"
 
 mozilla::LazyLogModule sShmemPoolLog("ShmemPool");
 
 #define SHMEMPOOL_LOG_VERBOSE(args) \
   MOZ_LOG(sShmemPoolLog, mozilla::LogLevel::Verbose, args)
 
 namespace mozilla {
 
-ShmemPool::ShmemPool(size_t aPoolSize)
-    : mMutex("mozilla::ShmemPool"),
+ShmemPool::ShmemPool(size_t aPoolSize, PoolType aPoolType)
+    : mPoolType(aPoolType),
+      mMutex("mozilla::ShmemPool"),
       mPoolFree(aPoolSize),
       mErrorLogged(false)
 #ifdef DEBUG
       ,
       mMaxPoolUse(0)
 #endif
 {
   mShmemPool.SetLength(aPoolSize);
--- a/dom/media/systemservices/ShmemPool.h
+++ b/dom/media/systemservices/ShmemPool.h
@@ -54,19 +54,21 @@ class ShmemBuffer {
 
  private:
   friend class ShmemPool;
 
   bool mInitialized;
   mozilla::ipc::Shmem mShmem;
 };
 
-class ShmemPool {
+class ShmemPool final {
  public:
-  explicit ShmemPool(size_t aPoolSize);
+  enum class PoolType { StaticPool, DynamicPool };
+  explicit ShmemPool(size_t aPoolSize,
+                     PoolType aPoolType = PoolType::StaticPool);
   ~ShmemPool();
   // Get/GetIfAvailable differ in what thread they can run on. GetIfAvailable
   // can run anywhere but won't allocate if the right size isn't available.
   ShmemBuffer GetIfAvailable(size_t aSize);
   void Put(ShmemBuffer&& aShmem);
 
   // We need to use the allocation/deallocation functions
   // of a specific IPC child/parent instance.
@@ -84,29 +86,36 @@ class ShmemPool {
   enum class AllocationPolicy { Default, Unsafe };
 
   template <class T>
   ShmemBuffer Get(T* aInstance, size_t aSize,
                   AllocationPolicy aPolicy = AllocationPolicy::Default) {
     MutexAutoLock lock(mMutex);
 
     // Pool is empty, don't block caller.
-    if (mPoolFree == 0) {
+    if (mPoolFree == 0 && mPoolType == PoolType::StaticPool) {
       if (!mErrorLogged) {
         // log "out of pool" once as error to avoid log spam
         mErrorLogged = true;
         SHMEMPOOL_LOG_ERROR(
             ("ShmemPool is empty, future occurrences "
              "will be logged as warnings"));
       } else {
         SHMEMPOOL_LOG_WARN(("ShmemPool is empty"));
       }
       // This isn't initialized, so will be understood as an error.
       return ShmemBuffer();
     }
+    if (mPoolFree == 0) {
+      MOZ_ASSERT(mPoolType == PoolType::DynamicPool);
+      SHMEMPOOL_LOG(("Dynamic ShmemPool empty, allocating extra Shmem buffer"));
+      ShmemBuffer newBuffer;
+      mShmemPool.InsertElementAt(0, std::move(newBuffer));
+      mPoolFree++;
+    }
 
     ShmemBuffer& res = mShmemPool[mPoolFree - 1];
 
     if (!res.mInitialized) {
       SHMEMPOOL_LOG(("Initializing new Shmem in pool"));
       if (!AllocateShmem(aInstance, aSize, res, aPolicy)) {
         SHMEMPOOL_LOG(("Failure allocating new Shmem buffer"));
         return ShmemBuffer();
@@ -153,16 +162,17 @@ class ShmemPool {
                      AllocationPolicy aPolicy) {
     return (aPolicy == AllocationPolicy::Default &&
             aInstance->AllocShmem(aSize, ipc::SharedMemory::TYPE_BASIC,
                                   &aRes.mShmem)) ||
            (aPolicy == AllocationPolicy::Unsafe &&
             aInstance->AllocUnsafeShmem(aSize, ipc::SharedMemory::TYPE_BASIC,
                                         &aRes.mShmem));
   }
+  const PoolType mPoolType;
   Mutex mMutex;
   size_t mPoolFree;
   bool mErrorLogged;
 #ifdef DEBUG
   size_t mMaxPoolUse;
 #endif
   nsTArray<ShmemBuffer> mShmemPool;
 };