summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/gtest/TestAvailableMemoryWatcherLinux.cpp
blob: 56d6f03ff87345f6625705cf03fbc8dfd9c13592 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* -*- 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 <sys/mman.h>  // For memory-locking.

#include "gtest/gtest.h"

#include "AvailableMemoryWatcher.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/StaticPrefs_browser.h"
#include "nsIObserverService.h"
#include "nsISupports.h"
#include "nsITimer.h"
#include "nsMemoryPressure.h"

using namespace mozilla;

namespace {

// Dummy tab unloader whose one job is to dispatch a low memory event.
class MockTabUnloader final : public nsITabUnloader {
  NS_DECL_THREADSAFE_ISUPPORTS
 public:
  MockTabUnloader() = default;

  NS_IMETHOD UnloadTabAsync() override {
    // We want to issue a memory pressure event for
    NS_NotifyOfEventualMemoryPressure(MemoryPressureState::LowMemory);
    return NS_OK;
  }

 private:
  ~MockTabUnloader() = default;
};

NS_IMPL_ISUPPORTS(MockTabUnloader, nsITabUnloader)

// Class that gradually increases the percent memory threshold
// until it reaches 100%, which should guarantee a memory pressure
// notification.
class AvailableMemoryChecker final : public nsITimerCallback, public nsINamed {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSITIMERCALLBACK
  NS_DECL_NSINAMED

  AvailableMemoryChecker();
  void Init();
  void Shutdown();

 private:
  ~AvailableMemoryChecker() = default;

  bool mResolved;
  nsCOMPtr<nsITimer> mTimer;
  RefPtr<nsAvailableMemoryWatcherBase> mWatcher;
  RefPtr<MockTabUnloader> mTabUnloader;

  const uint32_t kPollingInterval = 50;
  const uint32_t kPrefIncrement = 5;
};

AvailableMemoryChecker::AvailableMemoryChecker() : mResolved(false) {}

NS_IMPL_ISUPPORTS(AvailableMemoryChecker, nsITimerCallback, nsINamed);

void AvailableMemoryChecker::Init() {
  mTabUnloader = new MockTabUnloader;

  mWatcher = nsAvailableMemoryWatcherBase::GetSingleton();
  mWatcher->RegisterTabUnloader(mTabUnloader);

  mTimer = NS_NewTimer();
  mTimer->InitWithCallback(this, kPollingInterval,
                           nsITimer::TYPE_REPEATING_SLACK);
}

void AvailableMemoryChecker::Shutdown() {
  if (mTimer) {
    mTimer->Cancel();
  }
  Preferences::ClearUser("browser.low_commit_space_threshold_percent");
}

// Timer callback to increase the pref threshold.
NS_IMETHODIMP
AvailableMemoryChecker::Notify(nsITimer* aTimer) {
  uint32_t threshold =
      StaticPrefs::browser_low_commit_space_threshold_percent();
  if (threshold >= 100) {
    mResolved = true;
    return NS_OK;
  }
  threshold += kPrefIncrement;
  Preferences::SetUint("browser.low_commit_space_threshold_percent", threshold);
  return NS_OK;
}

NS_IMETHODIMP AvailableMemoryChecker::GetName(nsACString& aName) {
  aName.AssignLiteral("AvailableMemoryChecker");
  return NS_OK;
}

// Class that listens for a given notification, then records
// if it was received.
class Spinner final : public nsIObserver {
  nsCOMPtr<nsIObserverService> mObserverSvc;
  nsDependentCString mTopic;
  bool mTopicObserved;

  ~Spinner() = default;

 public:
  NS_DECL_ISUPPORTS

  Spinner(nsIObserverService* aObserverSvc, const char* aTopic)
      : mObserverSvc(aObserverSvc), mTopic(aTopic), mTopicObserved(false) {}

  NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
                     const char16_t* aData) override {
    if (mTopic == aTopic) {
      mTopicObserved = true;
      mObserverSvc->RemoveObserver(this, aTopic);

      // Force the loop to move in case there is no event in the queue.
      nsCOMPtr<nsIRunnable> dummyEvent = new Runnable(__func__);
      NS_DispatchToMainThread(dummyEvent);
    }
    return NS_OK;
  }
  void StartListening() {
    mObserverSvc->AddObserver(this, mTopic.get(), false);
  }
  bool TopicObserved() { return mTopicObserved; }
  bool WaitForNotification();
};
NS_IMPL_ISUPPORTS(Spinner, nsIObserver);

bool Spinner::WaitForNotification() {
  bool isTimeout = false;

  nsCOMPtr<nsITimer> timer;

  // This timer should time us out if we never observe our notification.
  // Set to 5000 since the memory checker should finish incrementing the
  // pref by then, and if it hasn't then it is probably stuck somehow.
  NS_NewTimerWithFuncCallback(
      getter_AddRefs(timer),
      [](nsITimer*, void* isTimeout) {
        *reinterpret_cast<bool*>(isTimeout) = true;
      },
      &isTimeout, 5000, nsITimer::TYPE_ONE_SHOT, __func__);

  SpinEventLoopUntil("Spinner:WaitForNotification"_ns, [&]() -> bool {
    if (isTimeout) {
      return true;
    }
    return mTopicObserved;
  });
  return !isTimeout;
}

void StartUserInteraction(const nsCOMPtr<nsIObserverService>& aObserverSvc) {
  aObserverSvc->NotifyObservers(nullptr, "user-interaction-active", nullptr);
}

TEST(AvailableMemoryWatcher, BasicTest)
{
  nsCOMPtr<nsIObserverService> observerSvc = services::GetObserverService();
  RefPtr<Spinner> aSpinner = new Spinner(observerSvc, "memory-pressure");
  aSpinner->StartListening();

  // Start polling for low memory.
  StartUserInteraction(observerSvc);

  RefPtr<AvailableMemoryChecker> checker = new AvailableMemoryChecker();
  checker->Init();

  aSpinner->WaitForNotification();

  // The checker should have dispatched a low memory event before reaching 100%
  // memory pressure threshold, so the topic should be observed by the spinner.
  EXPECT_TRUE(aSpinner->TopicObserved());
  checker->Shutdown();
}

TEST(AvailableMemoryWatcher, MemoryLowToHigh)
{
  // Setting this pref to 100 ensures we start in a low memory scenario.
  Preferences::SetUint("browser.low_commit_space_threshold_percent", 100);

  nsCOMPtr<nsIObserverService> observerSvc = services::GetObserverService();
  RefPtr<Spinner> lowMemorySpinner =
      new Spinner(observerSvc, "memory-pressure");
  lowMemorySpinner->StartListening();

  StartUserInteraction(observerSvc);

  // Start polling for low memory. We should start with low memory when we start
  // the checker.
  RefPtr<AvailableMemoryChecker> checker = new AvailableMemoryChecker();
  checker->Init();

  lowMemorySpinner->WaitForNotification();

  EXPECT_TRUE(lowMemorySpinner->TopicObserved());

  RefPtr<Spinner> highMemorySpinner =
      new Spinner(observerSvc, "memory-pressure-stop");
  highMemorySpinner->StartListening();

  // Now that we are definitely low on memory, let's reset the pref to 0 to
  // exit low memory.
  Preferences::SetUint("browser.low_commit_space_threshold_percent", 0);

  highMemorySpinner->WaitForNotification();

  EXPECT_TRUE(highMemorySpinner->TopicObserved());

  checker->Shutdown();
}
}  // namespace