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
|
/* -*- 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 <thread>
#include "gtest/gtest.h"
#include "mozilla/Atomics.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsMemoryPressure.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
using namespace mozilla;
namespace {
enum class MemoryPressureEventType : int {
LowMemory,
LowMemoryOngoing,
Stop,
};
class MemoryPressureObserver final : public nsIObserver {
nsCOMPtr<nsIObserverService> mObserverSvc;
Vector<MemoryPressureEventType> mEvents;
~MemoryPressureObserver() {
EXPECT_TRUE(
NS_SUCCEEDED(mObserverSvc->RemoveObserver(this, kTopicMemoryPressure)));
EXPECT_TRUE(NS_SUCCEEDED(
mObserverSvc->RemoveObserver(this, kTopicMemoryPressureStop)));
}
public:
NS_DECL_ISUPPORTS
MemoryPressureObserver()
: mObserverSvc(do_GetService(NS_OBSERVERSERVICE_CONTRACTID)) {
EXPECT_TRUE(NS_SUCCEEDED(mObserverSvc->AddObserver(
this, kTopicMemoryPressure, /* ownsWeak */ false)));
EXPECT_TRUE(NS_SUCCEEDED(mObserverSvc->AddObserver(
this, kTopicMemoryPressureStop, /* ownsWeak */ false)));
}
NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) override {
Maybe<MemoryPressureEventType> event;
if (strcmp(aTopic, kTopicMemoryPressure) == 0) {
if (nsDependentString(aData) == kSubTopicLowMemoryNew) {
event = Some(MemoryPressureEventType::LowMemory);
} else if (nsDependentString(aData) == kSubTopicLowMemoryOngoing) {
event = Some(MemoryPressureEventType::LowMemoryOngoing);
} else {
fprintf(stderr, "Unexpected subtopic: %S\n",
reinterpret_cast<const wchar_t*>(aData));
EXPECT_TRUE(false);
}
} else if (strcmp(aTopic, kTopicMemoryPressureStop) == 0) {
event = Some(MemoryPressureEventType::Stop);
} else {
fprintf(stderr, "Unexpected topic: %s\n", aTopic);
EXPECT_TRUE(false);
}
if (event) {
Unused << mEvents.emplaceBack(event.value());
}
return NS_OK;
}
uint32_t GetCount() const { return mEvents.length(); }
void Reset() { mEvents.clear(); }
MemoryPressureEventType Top() const { return mEvents[0]; }
bool ValidateTransitions() const {
if (mEvents.length() == 0) {
return true;
}
for (size_t i = 1; i < mEvents.length(); ++i) {
MemoryPressureEventType eventFrom = mEvents[i - 1];
MemoryPressureEventType eventTo = mEvents[i];
if ((eventFrom == MemoryPressureEventType::LowMemory &&
eventTo == MemoryPressureEventType::LowMemoryOngoing) ||
(eventFrom == MemoryPressureEventType::LowMemoryOngoing &&
eventTo == MemoryPressureEventType::LowMemoryOngoing) ||
(eventFrom == MemoryPressureEventType::Stop &&
eventTo == MemoryPressureEventType::LowMemory) ||
(eventFrom == MemoryPressureEventType::LowMemoryOngoing &&
eventTo == MemoryPressureEventType::Stop) ||
(eventFrom == MemoryPressureEventType::LowMemory &&
eventTo == MemoryPressureEventType::Stop)) {
// Only these transitions are valid.
continue;
}
fprintf(stderr, "Invalid transition: %d -> %d\n",
static_cast<int>(eventFrom), static_cast<int>(eventTo));
return false;
}
return true;
}
};
NS_IMPL_ISUPPORTS(MemoryPressureObserver, nsIObserver)
template <MemoryPressureState State>
void PressureSender(Atomic<bool>& aContinue) {
while (aContinue) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
NS_NotifyOfEventualMemoryPressure(State);
}
}
template <MemoryPressureState State>
void PressureSenderQuick(Atomic<bool>& aContinue) {
while (aContinue) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
NS_NotifyOfMemoryPressure(State);
}
}
} // anonymous namespace
TEST(MemoryPressure, Singlethread)
{
RefPtr observer(new MemoryPressureObserver);
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::LowMemory);
SpinEventLoopUntil("xpcom:TEST(MemoryPressure, Singlethread) 1"_ns,
[&observer]() { return observer->GetCount() == 1; });
EXPECT_EQ(observer->Top(), MemoryPressureEventType::LowMemory);
observer->Reset();
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::LowMemory);
SpinEventLoopUntil("xpcom:TEST(MemoryPressure, Singlethread) 2"_ns,
[&observer]() { return observer->GetCount() == 1; });
EXPECT_EQ(observer->Top(), MemoryPressureEventType::LowMemoryOngoing);
observer->Reset();
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::LowMemory);
SpinEventLoopUntil("xpcom:TEST(MemoryPressure, Singlethread) 3"_ns,
[&observer]() { return observer->GetCount() == 1; });
EXPECT_EQ(observer->Top(), MemoryPressureEventType::LowMemoryOngoing);
observer->Reset();
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::NoPressure);
SpinEventLoopUntil("xpcom:TEST(MemoryPressure, Singlethread) 4"_ns,
[&observer]() { return observer->GetCount() == 1; });
EXPECT_EQ(observer->Top(), MemoryPressureEventType::Stop);
}
TEST(MemoryPressure, Multithread)
{
// Start |kNumThreads| threads each for the following thread type:
// - LowMemory via NS_NotifyOfEventualMemoryPressure
// - LowMemory via NS_NotifyOfMemoryPressure
// - LowMemoryOngoing via NS_NotifyOfEventualMemoryPressure
// - LowMemoryOngoing via NS_NotifyOfMemoryPressure
// and keep them running until |kNumEventsToValidate| memory-pressure events
// are received.
constexpr int kNumThreads = 5;
constexpr int kNumEventsToValidate = 200;
Atomic<bool> shouldContinue(true);
Vector<std::thread> threads;
for (int i = 0; i < kNumThreads; ++i) {
Unused << threads.emplaceBack(
PressureSender<MemoryPressureState::LowMemory>,
std::ref(shouldContinue));
Unused << threads.emplaceBack(
PressureSender<MemoryPressureState::NoPressure>,
std::ref(shouldContinue));
Unused << threads.emplaceBack(
PressureSenderQuick<MemoryPressureState::LowMemory>,
std::ref(shouldContinue));
Unused << threads.emplaceBack(
PressureSenderQuick<MemoryPressureState::NoPressure>,
std::ref(shouldContinue));
}
RefPtr observer(new MemoryPressureObserver);
// We cannot sleep here because the main thread needs to keep running.
SpinEventLoopUntil(
"xpcom:TEST(MemoryPressure, Multithread)"_ns,
[&observer]() { return observer->GetCount() >= kNumEventsToValidate; });
shouldContinue = false;
for (auto& thread : threads) {
thread.join();
}
EXPECT_TRUE(observer->ValidateTransitions());
}
|