commit 086ee410071f6c330f54f61f8c621f2c0ca3d54d
Author: Szymon Czynszak <szymon.czynszak@sky.uk>
Date:   Tue Feb 19 16:51:05 2019 +0000

    skydev-5.12.0-NGDEV-134823_Propose_porting_approach_for_Memory_Pressure_Support
    
    integrated memory pressure monitor and memory constraints

diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/base/BUILD.gn b/qtwebengine5.9.7/src/3rdparty/chromium/base/BUILD.gn
index 97f62fd699..05ade8426a 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/base/BUILD.gn
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/base/BUILD.gn
@@ -486,6 +486,8 @@ component("base") {
     "memory/memory_pressure_listener.h",
     "memory/memory_pressure_monitor.cc",
     "memory/memory_pressure_monitor.h",
+    "memory/memory_pressure_monitor_cgroup.cc",
+    "memory/memory_pressure_monitor_cgroup.h",
     "memory/memory_pressure_monitor_chromeos.cc",
     "memory/memory_pressure_monitor_chromeos.h",
     "memory/memory_pressure_monitor_mac.cc",
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/base/environment.cc b/qtwebengine5.9.7/src/3rdparty/chromium/base/environment.cc
index 534a7a8812..e8b2dde7a1 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/base/environment.cc
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/base/environment.cc
@@ -12,6 +12,7 @@
 #include "base/strings/string_piece.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
+#include "base/strings/string_number_conversions.h"
 #include "build/build_config.h"
 
 #if defined(OS_POSIX)
@@ -235,6 +236,47 @@ std::unique_ptr<char* []> AlterEnvironment(const char* const* const env,
   return result;
 }
 
+bool Environment::WRT_GetVar(const char * key, std::string * val) {
+  std::unique_ptr<base::Environment> env(base::Environment::Create());
+
+  if(env->GetVar(key, val)) {
+    VLOG(1) << "Found env var for key " << key << ": " << *val;
+    return true;
+  }
+  return false;
+}
+
+int Environment::WRT_GetVarInt(const char * key, int defaultInt) {
+  std::string val;
+  if(WRT_GetVar(key, &val)) {
+    int valInt;
+    if(base::StringToInt(val, &valInt)) {
+      return valInt;
+    }
+    LOG(ERROR) << "Don't recognise int env var for " << key << ": " << val;
+  }
+  return defaultInt;
+}
+
+bool Environment::WRT_GetVarBool(const char * key, bool defaultBool) {
+  std::string val;
+  if(WRT_GetVar(key, &val)) {
+    if(val == "true") return true;
+    if(val == "false") return false;
+    LOG(ERROR) << "Don't recognise bool env var for " << key << ": " << val;
+  }
+  return defaultBool;
+}
+
+std::string Environment::WRT_GetVarString(const char * key, const std::string& defaultString) {
+  std::string val;
+  if(WRT_GetVar(key, &val)) {
+
+    return val;
+  }
+  return defaultString;
+}
+
 #endif  // OS_POSIX
 
 }  // namespace base
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/base/environment.h b/qtwebengine5.9.7/src/3rdparty/chromium/base/environment.h
index 3a4ed04e4b..8c3d5da799 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/base/environment.h
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/base/environment.h
@@ -44,6 +44,11 @@ class BASE_EXPORT Environment {
 
   // Returns true on success, otherwise returns false.
   virtual bool UnSetVar(StringPiece variable_name) = 0;
+
+  static bool WRT_GetVarBool(const char * key, bool defaultBool);
+  static std::string WRT_GetVarString(const char * key, const std::string& defaultString);
+  static int WRT_GetVarInt(const char * key, int defaultInt);
+  static bool WRT_GetVar(const char * key, std::string * val);
 };
 
 
@@ -83,6 +88,17 @@ BASE_EXPORT std::unique_ptr<char* []> AlterEnvironment(
     const char* const* env,
     const EnvironmentMap& changes);
 
+#define WRT_IMAGE_CACHE_COUNT_MAX_DEFAULT 15
+#define WRT_IMAGE_CACHE_SIZE_MAX_KB_DEFAULT (32*1024)
+#define WRT_MEMORY_MAX_MB_DEFAULT 200
+#define WRT_FONT_CACHE_MAX_KB_DEFAULT 1024
+#define WRT_CHUNK_DEMUX_AUDIO_MEMORY_MAX_KB_DEFAULT (3*512)
+#define WRT_CHUNK_DEMUX_VIDEO_MEMORY_MAX_KB_DEFAULT (12*1024)
+#define WRT_ENABLE_MEMORY_PRESSURE_MONITOR_DEFAULT false
+#define WRT_MEMORY_PRESSURE_MODERATE_PERCENT_DEFAULT 60
+#define WRT_MEMORY_PRESSURE_CRITICAL_PERCENT_DEFAULT 80
+#define WRT_DEFAULT_MEMORY_LIMIT_MB_DEFAULT 16
+
 #endif
 
 }  // namespace base
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/base/memory/memory_pressure_monitor_cgroup.cc b/qtwebengine5.9.7/src/3rdparty/chromium/base/memory/memory_pressure_monitor_cgroup.cc
new file mode 100644
index 0000000000..65ab55f879
--- /dev/null
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/base/memory/memory_pressure_monitor_cgroup.cc
@@ -0,0 +1,215 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/memory/memory_pressure_monitor_cgroup.h"
+
+#include <fcntl.h>
+#include <sys/select.h>
+#include <fstream>
+#include <iostream>
+#include <sys/statvfs.h>
+#include <sys/types.h>
+
+#include "base/environment.h"
+#include "base/metrics/histogram_macros.h"
+#include "base/posix/eintr_wrapper.h"
+#include "base/process/process_metrics.h"
+#include "base/single_thread_task_runner.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "base/time/time.h"
+
+#include <dirent.h>
+#include <cstdio>
+
+namespace base {
+namespace cgroup {
+
+namespace {
+
+// The time between memory pressure checks. While under critical pressure, this
+// is also the timer to repeat cleanup attempts.
+const int kMemoryPressureIntervalMs = 1000;
+
+// The time which should pass between two moderate memory pressure calls.
+const int kModerateMemoryPressureCooldownMs = 10000;
+
+// Number of event polls before the next moderate pressure event can be sent.
+const int kModerateMemoryPressureCooldown =
+    kModerateMemoryPressureCooldownMs / kMemoryPressureIntervalMs;
+
+// The possible state for memory pressure level. The values should be in line
+// with values in MemoryPressureListener::MemoryPressureLevel and should be
+// updated if more memory pressure levels are introduced.
+enum MemoryPressureLevelUMA {
+  MEMORY_PRESSURE_LEVEL_NONE = 0,
+  MEMORY_PRESSURE_LEVEL_MODERATE,
+  MEMORY_PRESSURE_LEVEL_CRITICAL,
+  NUM_MEMORY_PRESSURE_LEVELS
+};
+
+
+
+// Converts free percent of memory into a memory pressure value.
+MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevelFromFillLevel(
+    int actual_fill_level,
+    int moderate_threshold,
+    int critical_threshold) {
+  if (actual_fill_level < moderate_threshold)
+    return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
+  return actual_fill_level < critical_threshold
+             ? MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
+             : MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
+}
+
+
+}  // namespace
+
+MemoryPressureMonitor::MemoryPressureMonitor()
+    : current_memory_pressure_level_(
+          MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
+      moderate_pressure_repeat_count_(0),
+      moderate_pressure_threshold_percent_(
+          base::Environment::WRT_GetVarInt("WRT_MEMORY_PRESSURE_MODERATE_PERCENT",
+              WRT_MEMORY_PRESSURE_MODERATE_PERCENT_DEFAULT)),
+      critical_pressure_threshold_percent_(
+          base::Environment::WRT_GetVarInt("WRT_MEMORY_PRESSURE_CRITICAL_PERCENT",
+              WRT_MEMORY_PRESSURE_CRITICAL_PERCENT_DEFAULT)),
+      dispatch_callback_(
+          base::Bind(&MemoryPressureListener::NotifyMemoryPressure)),
+      weak_ptr_factory_(this) {
+  StartObserving();
+}
+
+MemoryPressureMonitor::~MemoryPressureMonitor() {
+  StopObserving();
+}
+
+void MemoryPressureMonitor::ScheduleEarlyCheck() {
+  ThreadTaskRunnerHandle::Get()->PostTask(
+      FROM_HERE, Bind(&MemoryPressureMonitor::CheckMemoryPressure,
+                      weak_ptr_factory_.GetWeakPtr()));
+}
+
+MemoryPressureListener::MemoryPressureLevel
+MemoryPressureMonitor::GetCurrentPressureLevel() const {
+  return current_memory_pressure_level_;
+}
+
+// static
+MemoryPressureMonitor* MemoryPressureMonitor::Get() {
+  return static_cast<MemoryPressureMonitor*>(
+      base::MemoryPressureMonitor::Get());
+}
+
+void MemoryPressureMonitor::StartObserving() {
+  timer_.Start(FROM_HERE,
+               TimeDelta::FromMilliseconds(kMemoryPressureIntervalMs),
+               Bind(&MemoryPressureMonitor::
+                        CheckMemoryPressureAndRecordStatistics,
+                    weak_ptr_factory_.GetWeakPtr()));
+}
+
+void MemoryPressureMonitor::StopObserving() {
+  // If StartObserving failed, StopObserving will still get called.
+  timer_.Stop();
+}
+
+void MemoryPressureMonitor::CheckMemoryPressureAndRecordStatistics() {
+  CheckMemoryPressure();
+
+  // Record UMA histogram statistics for the current memory pressure level.
+  MemoryPressureLevelUMA memory_pressure_level_uma(MEMORY_PRESSURE_LEVEL_NONE);
+  switch (current_memory_pressure_level_) {
+    case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
+      memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_NONE;
+      break;
+    case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
+      memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_MODERATE;
+      break;
+    case MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
+      memory_pressure_level_uma = MEMORY_PRESSURE_LEVEL_CRITICAL;
+      break;
+  }
+
+  UMA_HISTOGRAM_ENUMERATION("CGroup.MemoryPressureLevel",
+                            memory_pressure_level_uma,
+                            NUM_MEMORY_PRESSURE_LEVELS);
+}
+
+void MemoryPressureMonitor::CheckMemoryPressure() {
+  MemoryPressureListener::MemoryPressureLevel old_pressure =
+      current_memory_pressure_level_;
+
+  current_memory_pressure_level_ = GetMemoryPressureLevelFromFillLevel(
+      GetUsedMemoryInPercent(),
+      moderate_pressure_threshold_percent_,
+      critical_pressure_threshold_percent_);
+
+
+  // In case there is no memory pressure we do not notify.
+  if (current_memory_pressure_level_ ==
+      MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) {
+    return;
+  }
+  if (old_pressure == current_memory_pressure_level_) {
+    // If the memory pressure is still at the same level, we notify again for a
+    // critical level. In case of a moderate level repeat however, we only send
+    // a notification after a certain time has passed.
+    if (current_memory_pressure_level_ ==
+        MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE &&
+          ++moderate_pressure_repeat_count_ <
+              kModerateMemoryPressureCooldown) {
+      return;
+    }
+  } else if (current_memory_pressure_level_ ==
+               MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE &&
+             old_pressure ==
+               MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
+    // When we reducing the pressure level from critical to moderate, we
+    // restart the timeout and do not send another notification.
+    moderate_pressure_repeat_count_ = 0;
+    return;
+  }
+  moderate_pressure_repeat_count_ = 0;
+  LOG(INFO) << "Notifying Memory Pressure level " <<
+		  (current_memory_pressure_level_  == MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL ?
+				  "CRITICAL" : "MODERATE");
+  dispatch_callback_.Run(current_memory_pressure_level_);
+}
+
+
+long MemoryPressureMonitor::GetMemoryFromFile(const char * name) {
+    std::ifstream f(name);
+
+    if(!f) {
+        LOG(ERROR) << "Failed to open file " << name << ": " << strerror(errno);
+        return 0;
+    }
+    long n = 0;
+    f >> n;
+    f.close();
+    if(n < 0) n = 0;
+    return n / 1024;
+}
+
+// Gets the used CGroup memory in percent.
+int MemoryPressureMonitor::GetUsedMemoryInPercent() {
+  long used_memory = GetMemoryFromFile("/sys/fs/cgroup/memory/memory.usage_in_bytes");
+  long total_memory = GetMemoryFromFile("/sys/fs/cgroup/memory/memory.limit_in_bytes");
+  if(total_memory <= 0) {
+	  return 0;
+  }
+  int percentage = (used_memory * 100L) / total_memory;
+
+  if(percentage > 100) percentage = 100;
+  return percentage;
+}
+
+void MemoryPressureMonitor::SetDispatchCallback(
+    const DispatchCallback& callback) {
+  dispatch_callback_ = callback;
+}
+
+}  // namespace cgroup
+}  // namespace base
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/base/memory/memory_pressure_monitor_cgroup.h b/qtwebengine5.9.7/src/3rdparty/chromium/base/memory/memory_pressure_monitor_cgroup.h
new file mode 100644
index 0000000000..4c53734c81
--- /dev/null
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/base/memory/memory_pressure_monitor_cgroup.h
@@ -0,0 +1,104 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_CGROUP_H_
+#define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_CGROUP_H_
+
+#include "base/base_export.h"
+#include "base/files/scoped_file.h"
+#include "base/gtest_prod_util.h"
+#include "base/macros.h"
+#include "base/memory/memory_pressure_listener.h"
+#include "base/memory/memory_pressure_monitor.h"
+#include "base/memory/weak_ptr.h"
+#include "base/timer/timer.h"
+
+namespace base {
+namespace cgroup {
+
+class TestMemoryPressureMonitor;
+
+////////////////////////////////////////////////////////////////////////////////
+// MemoryPressureMonitor
+//
+// A class to handle the observation of our free memory. It notifies the
+// MemoryPressureListener of memory fill level changes, so that it can take
+// action to reduce memory resources accordingly.
+//
+class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
+ public:
+  using GetUsedMemoryInPercentCallback = int (*)();
+
+
+  explicit MemoryPressureMonitor();
+  ~MemoryPressureMonitor() override;
+
+  // Redo the memory pressure calculation soon and call again if a critical
+  // memory pressure prevails. Note that this call will trigger an asynchronous
+  // action which gives the system time to release memory back into the pool.
+  void ScheduleEarlyCheck();
+
+  // Get the current memory pressure level.
+  MemoryPressureListener::MemoryPressureLevel GetCurrentPressureLevel() const
+      override;
+
+  // Returns a type-casted version of the current memory pressure monitor. A
+  // simple wrapper to base::MemoryPressureMonitor::Get.
+  static MemoryPressureMonitor* Get();
+
+  void SetDispatchCallback(const DispatchCallback& callback) override;
+
+ private:
+  friend TestMemoryPressureMonitor;
+  // Starts observing the memory fill level.
+  // Calls to StartObserving should always be matched with calls to
+  // StopObserving.
+  void StartObserving();
+
+  // Stop observing the memory fill level.
+  // May be safely called if StartObserving has not been called.
+  void StopObserving();
+
+  // The function which gets periodically called to check any changes in the
+  // memory pressure. It will report pressure changes as well as continuous
+  // critical pressure levels.
+  void CheckMemoryPressure();
+
+  // The function periodically checks the memory pressure changes and records
+  // the UMA histogram statistics for the current memory pressure level.
+  void CheckMemoryPressureAndRecordStatistics();
+
+  // Get the memory pressure in percent (virtual for testing).
+  virtual int GetUsedMemoryInPercent();
+
+  // reads memory from cgroup file
+  long GetMemoryFromFile(const char * name);
+
+  // The current memory pressure.
+  base::MemoryPressureListener::MemoryPressureLevel
+      current_memory_pressure_level_;
+
+  // A periodic timer to check for resource pressure changes.
+  //base::RepeatingTimer<MemoryPressureMonitor> timer_;
+  base::RepeatingTimer timer_;
+
+  // To slow down the amount of moderate pressure event calls, this counter
+  // gets used to count the number of events since the last event occured.
+  int moderate_pressure_repeat_count_;
+
+  // The thresholds for moderate and critical pressure.
+  const int moderate_pressure_threshold_percent_;
+  const int critical_pressure_threshold_percent_;
+
+  DispatchCallback dispatch_callback_;
+
+  base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_;
+
+  DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor);
+};
+
+}  // namespace cgroup
+}  // namespace base
+
+#endif  // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_CGROUP_H_
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/base/sys_info_linux.cc b/qtwebengine5.9.7/src/3rdparty/chromium/base/sys_info_linux.cc
index 298d245ecf..edf09e6a71 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/base/sys_info_linux.cc
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/base/sys_info_linux.cc
@@ -16,17 +16,13 @@
 #include "base/strings/string_number_conversions.h"
 #include "base/sys_info_internal.h"
 #include "build/build_config.h"
+#include "base/environment.h"
 
 namespace {
 
 int64_t AmountOfMemory(int pages_name) {
-  long pages = sysconf(pages_name);
-  long page_size = sysconf(_SC_PAGESIZE);
-  if (pages == -1 || page_size == -1) {
-    NOTREACHED();
-    return 0;
-  }
-  return static_cast<int64_t>(pages) * page_size;
+  int size = base::Environment::WRT_GetVarInt("WRT_MEMORY_MAX_MB", WRT_MEMORY_MAX_MB_DEFAULT);
+  return size*1024*1024;
 }
 
 int64_t AmountOfPhysicalMemory() {
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/components/discardable_memory/service/discardable_shared_memory_manager.cc b/qtwebengine5.9.7/src/3rdparty/chromium/components/discardable_memory/service/discardable_shared_memory_manager.cc
index 1036dc84d6..80ddb005c2 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/components/discardable_memory/service/discardable_shared_memory_manager.cc
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/components/discardable_memory/service/discardable_shared_memory_manager.cc
@@ -29,7 +29,7 @@
 #include "base/trace_event/trace_event.h"
 #include "build/build_config.h"
 #include "components/discardable_memory/common/discardable_shared_memory_heap.h"
-
+#include "base/environment.h"
 #if defined(OS_LINUX)
 #include "base/files/file_path.h"
 #include "base/files/file_util.h"
@@ -125,7 +125,9 @@ int64_t GetDefaultMemoryLimit() {
   // Limits the number of FDs used to 32, assuming a 4MB allocation size.
   int64_t max_default_memory_limit = 128 * kMegabyte;
 #else
-  int64_t max_default_memory_limit = 512 * kMegabyte;
+  int defaultMemoryLimit = base::Environment::WRT_GetVarInt("WRT_DEFAULT_MEMORY_LIMIT_MB",
+                                       WRT_DEFAULT_MEMORY_LIMIT_MB_DEFAULT);
+  int64_t max_default_memory_limit = defaultMemoryLimit * kMegabyte;
 #endif
 
   // Use 1/8th of discardable memory on low-end devices.
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/media/nova/NoVAMediaSourceManager.cc b/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/media/nova/NoVAMediaSourceManager.cc
index 800d8087a9..4eca610d1b 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/media/nova/NoVAMediaSourceManager.cc
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/media/nova/NoVAMediaSourceManager.cc
@@ -141,16 +141,16 @@ void NoVAMediaSourceManager::onDemuxerInitialised(PipelineStatus status) {
 
         // TODO, DC WE NEED TO SET MSE MEMORY BUFFER SIZES HERE!
 	// ensure memory usage is kept within container limits
-        #ifdef SORT_OUT_SET_MEMORY_LIMITS
+
         // We need WRT env vars, and SetMemoryLimits support
         //  void SetMemoryLimitsForTest(DemuxerStream::Type type, size_t memory_limit); ????
-	m_chunkDemuxer->SetMemoryLimits(DemuxerStream::AUDIO,
+
+        m_chunkDemuxer->SetMemoryLimitsForTest(DemuxerStream::AUDIO,
 		base::Environment::WRT_GetVarInt("WRT_CHUNK_DEMUX_AUDIO_MEMORY_MAX_KB",
 			WRT_CHUNK_DEMUX_AUDIO_MEMORY_MAX_KB_DEFAULT) * 1024);
-	m_chunkDemuxer->SetMemoryLimits(DemuxerStream::VIDEO,
+        m_chunkDemuxer->SetMemoryLimitsForTest(DemuxerStream::VIDEO,
 		base::Environment::WRT_GetVarInt("WRT_CHUNK_DEMUX_VIDEO_MEMORY_MAX_KB",
 			WRT_CHUNK_DEMUX_VIDEO_MEMORY_MAX_KB_DEFAULT) * 1024);
-        #endif
         
 	std::string videoMime("video/");
 	VideoCodec videoCodec = VideoCodec::kUnknownVideoCodec;
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/render_thread_impl.cc b/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/render_thread_impl.cc
index 2934f9438a..5224dcfd62 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/render_thread_impl.cc
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/render_thread_impl.cc
@@ -165,6 +165,8 @@
 #include "ui/base/ui_base_switches.h"
 #include "ui/gl/gl_switches.h"
 
+#include "base/memory/memory_pressure_monitor_cgroup.h"
+
 #if defined(OS_ANDROID)
 #include <cpu-features.h>
 #include "content/renderer/android/synchronous_compositor_filter.h"
@@ -841,6 +843,11 @@ void RenderThreadImpl::Init(
       base::Bind(&RenderThreadImpl::OnSyncMemoryPressure,
                  base::Unretained(this))));
 
+  if (base::Environment::WRT_GetVarBool("WRT_ENABLE_MEMORY_PRESSURE_MONITOR",
+                 WRT_ENABLE_MEMORY_PRESSURE_MONITOR_DEFAULT)) {
+    memory_pressure_monitor_.reset(new base::cgroup::MemoryPressureMonitor());
+  }
+
   if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) {
     // Currently it is not possible to enable both PurgeAndSuspend and
     // MemoryCoordinator at the same time.
@@ -1005,6 +1012,8 @@ void RenderThreadImpl::Shutdown() {
 
   ChildThreadImpl::Shutdown();
 
+  memory_pressure_monitor_.reset();
+
   // Shut down the message loop (if provided when the RenderThreadImpl was
   // constructed) and the renderer scheduler before shutting down Blink. This
   // prevents a scenario where a pending task in the message loop accesses Blink
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/render_thread_impl.h b/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/render_thread_impl.h
index 0e9192501d..37affa96c3 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/render_thread_impl.h
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/content/renderer/render_thread_impl.h
@@ -64,6 +64,7 @@ class WebMediaStreamCenterClient;
 namespace base {
 class SingleThreadTaskRunner;
 class Thread;
+class MemoryPressureMonitor;
 }
 
 namespace cc {
@@ -664,6 +665,7 @@ class CONTENT_EXPORT RenderThreadImpl
   HistogramCustomizer histogram_customizer_;
 
   std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_;
+  std::unique_ptr<base::MemoryPressureMonitor> memory_pressure_monitor_;
 
   std::unique_ptr<MemoryObserver> memory_observer_;
   std::unique_ptr<ChildMemoryCoordinatorImpl> memory_coordinator_;
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkGlyphCache_Globals.h b/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkGlyphCache_Globals.h
index 4d7fe22d80..477424bd9d 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkGlyphCache_Globals.h
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkGlyphCache_Globals.h
@@ -12,6 +12,7 @@
 #include "SkMutex.h"
 #include "SkSpinlock.h"
 #include "SkTLS.h"
+#include "base/environment.h"
 
 #ifndef SK_DEFAULT_FONT_CACHE_COUNT_LIMIT
     #define SK_DEFAULT_FONT_CACHE_COUNT_LIMIT   2048
@@ -28,7 +29,8 @@ public:
     SkGlyphCache_Globals() {
         fHead = nullptr;
         fTotalMemoryUsed = 0;
-        fCacheSizeLimit = SK_DEFAULT_FONT_CACHE_LIMIT;
+        fCacheSizeLimit = base::Environment::WRT_GetVarInt("WRT_FONT_CACHE_MAX_KB",
+                WRT_FONT_CACHE_MAX_KB_DEFAULT) * 1024;
         fCacheCount = 0;
         fCacheCountLimit = SK_DEFAULT_FONT_CACHE_COUNT_LIMIT;
     }
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkResourceCache.cpp b/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkResourceCache.cpp
index 4bdc8dd9e0..c3ba6d85e1 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkResourceCache.cpp
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkResourceCache.cpp
@@ -16,6 +16,8 @@
 #include <stddef.h>
 #include <stdlib.h>
 
+#include "base/environment.h"
+
 DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage)
 
 // This can be defined by the caller's build system
@@ -71,6 +73,11 @@ void SkResourceCache::init() {
     // One of these should be explicit set by the caller after we return.
     fTotalByteLimit = 0;
     fDiscardableFactory = nullptr;
+
+    fImageCacheCountLimit = base::Environment::WRT_GetVarInt("WRT_IMAGE_CACHE_COUNT_MAX",
+            WRT_IMAGE_CACHE_COUNT_MAX_DEFAULT);
+    fImageCacheSizeLimit = 1024 * base::Environment::WRT_GetVarInt("WRT_IMAGE_CACHE_SIZE_MAX_KB",
+            WRT_IMAGE_CACHE_SIZE_MAX_KB_DEFAULT);
 }
 
 #include "SkDiscardableMemory.h"
@@ -301,8 +308,8 @@ void SkResourceCache::purgeAsNeeded(bool forcePurge) {
     int    countLimit;
 
     if (fDiscardableFactory) {
-        countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT;
-        byteLimit = SK_MaxU32;  // no limit based on bytes
+        countLimit = fImageCacheCountLimit;
+        byteLimit = fImageCacheSizeLimit;
     } else {
         countLimit = SK_MaxS32; // no limit based on count
         byteLimit = fTotalByteLimit;
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkResourceCache.h b/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkResourceCache.h
index 5919336312..82a91818f6 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkResourceCache.h
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/third_party/skia/src/core/SkResourceCache.h
@@ -267,6 +267,8 @@ private:
     size_t  fTotalByteLimit;
     size_t  fSingleAllocationByteLimit;
     int     fCount;
+    int     fImageCacheCountLimit;
+    int     fImageCacheSizeLimit;
 
     SkMessageBus<PurgeSharedIDMessage>::Inbox fPurgeSharedIDInbox;
 
diff --git a/qtwebengine5.9.7/src/3rdparty/chromium/v8/src/base/platform/platform-linux.cc b/qtwebengine5.9.7/src/3rdparty/chromium/v8/src/base/platform/platform-linux.cc
index a35d423210..a1d830e5ba 100644
--- a/qtwebengine5.9.7/src/3rdparty/chromium/v8/src/base/platform/platform-linux.cc
+++ b/qtwebengine5.9.7/src/3rdparty/chromium/v8/src/base/platform/platform-linux.cc
@@ -365,7 +365,10 @@ bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
 
 
 bool VirtualMemory::HasLazyCommits() {
-  return true;
+    if(const char * str = getenv("WRT_VIRTUAL_MEMORY_LAZY_COMMITS")) {
+        if(strcmp(str, "true") == 0) return true;
+        if(strcmp(str, "false") == 0) return false;
+    }
 }
 
 }  // namespace base
