summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/instrumentation/memory_pressure_listener.cc
blob: 074c7272786a291b290e02503525b0530e5a37d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2015 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 "third_party/blink/renderer/platform/instrumentation/memory_pressure_listener.h"

#include "base/allocator/partition_allocator/memory_reclaimer.h"
#include "base/feature_list.h"
#include "base/system/sys_info.h"
#include "build/build_config.h"
#include "third_party/blink/public/common/device_memory/approximated_device_memory.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/renderer/platform/fonts/font_global_context.h"
#include "third_party/blink/renderer/platform/graphics/image_decoding_store.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/wtf/allocator/partitions.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"

#if defined(OS_ANDROID)
#include "base/android/sys_utils.h"
#endif

namespace blink {

// Wrapper function defined in WebKit.h
void DecommitFreeableMemory() {
  CHECK(IsMainThread());
  base::PartitionAllocMemoryReclaimer::Instance()->Reclaim();
}

// static
bool MemoryPressureListenerRegistry::is_low_end_device_ = false;

// static
bool MemoryPressureListenerRegistry::IsLowEndDevice() {
  return is_low_end_device_;
}

// static
bool MemoryPressureListenerRegistry::IsCurrentlyLowMemory() {
#if defined(OS_ANDROID)
  return base::android::SysUtils::IsCurrentlyLowMemory();
#else
  return false;
#endif
}

// static
void MemoryPressureListenerRegistry::Initialize() {
  is_low_end_device_ = ::base::SysInfo::IsLowEndDevice();
  ApproximatedDeviceMemory::Initialize();
  // Make sure the instance of MemoryPressureListenerRegistry is created on
  // the main thread. Otherwise we might try to create the instance on a
  // thread which doesn't have ThreadState (e.g., the IO thread).
  MemoryPressureListenerRegistry::Instance();
}

// static
void MemoryPressureListenerRegistry::SetIsLowEndDeviceForTesting(
    bool is_low_end_device) {
  is_low_end_device_ = is_low_end_device;
}

// static
MemoryPressureListenerRegistry& MemoryPressureListenerRegistry::Instance() {
  DEFINE_THREAD_SAFE_STATIC_LOCAL(
      CrossThreadPersistent<MemoryPressureListenerRegistry>, external,
      (MakeGarbageCollected<MemoryPressureListenerRegistry>()));
  return *external.Get();
}

void MemoryPressureListenerRegistry::RegisterThread(Thread* thread) {
  MutexLocker lock(threads_mutex_);
  threads_.insert(thread);
}

void MemoryPressureListenerRegistry::UnregisterThread(Thread* thread) {
  MutexLocker lock(threads_mutex_);
  threads_.erase(thread);
}

MemoryPressureListenerRegistry::MemoryPressureListenerRegistry() = default;

void MemoryPressureListenerRegistry::RegisterClient(
    MemoryPressureListener* client) {
  DCHECK(IsMainThread());
  DCHECK(client);
  DCHECK(!clients_.Contains(client));
  clients_.insert(client);
}

void MemoryPressureListenerRegistry::UnregisterClient(
    MemoryPressureListener* client) {
  DCHECK(IsMainThread());
  clients_.erase(client);
}

void MemoryPressureListenerRegistry::OnMemoryPressure(
    WebMemoryPressureLevel level) {
  TRACE_EVENT0("blink", "MemoryPressureListenerRegistry::onMemoryPressure");
  CHECK(IsMainThread());
  for (auto& client : clients_)
    client->OnMemoryPressure(level);
  base::PartitionAllocMemoryReclaimer::Instance()->Reclaim();
}

void MemoryPressureListenerRegistry::OnPurgeMemory() {
  CHECK(IsMainThread());
  for (auto& client : clients_)
    client->OnPurgeMemory();
  ImageDecodingStore::Instance().Clear();
  base::PartitionAllocMemoryReclaimer::Instance()->Reclaim();

  // Thread-specific data never issues a layout, so we are safe here.
  MutexLocker lock(threads_mutex_);
  for (auto* thread : threads_) {
    if (!thread->GetTaskRunner())
      continue;

    PostCrossThreadTask(
        *thread->GetTaskRunner(), FROM_HERE,
        CrossThreadBindOnce(
            MemoryPressureListenerRegistry::ClearThreadSpecificMemory));
  }
}

void MemoryPressureListenerRegistry::ClearThreadSpecificMemory() {
  FontGlobalContext::ClearMemory();
}

void MemoryPressureListenerRegistry::Trace(blink::Visitor* visitor) {
  visitor->Trace(clients_);
}

}  // namespace blink