Repository navigation
Expand file tree
/
Copy pathExternalMemory.mm
More file actions
214 lines (191 loc) · 6.46 KB
/
Copy pathExternalMemory.mm
File metadata and controls
214 lines (191 loc) · 6.46 KB
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
#include "ExternalMemory.h"
#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>
#include <dispatch/dispatch.h>
#include <dlfcn.h>
#include <objc/message.h>
#include <objc/runtime.h>
#include "Caches.h"
#include "DataWrapper.h"
#include "Helpers.h"
#include "IsolateWrapper.h"
#include "NSDataAdapter.h"
#include "Runtime.h"
using namespace v8;
namespace tns {
ExternalMemoryCharge::~ExternalMemoryCharge() {
if (bytes_ == 0) {
return;
}
// A closed gate means the isolate is being disposed, which drops its
// external memory total with it.
if (IsolateGates::TryPin(gateId_)) {
accounter_.Decrease(isolate_, bytes_);
IsolateGates::Unpin(gateId_);
}
}
void ExternalMemoryCharge::Set(size_t bytes) {
if (bytes > bytes_) {
accounter_.Increase(isolate_, bytes - bytes_);
} else if (bytes < bytes_) {
accounter_.Decrease(isolate_, bytes_ - bytes);
}
bytes_ = bytes;
}
namespace ExternalMemory {
namespace {
// Values of Caches::ExternalSizeEstimators.
enum Estimator : uint8_t {
kNone,
kNSData,
kUIImage,
// Toll-free bridged CF instances all share one class, so the estimator is
// picked per object from its CFTypeID.
kCFType,
};
typedef CFTypeID (*TypeIDFn)(void);
typedef size_t (*SizeOfFn)(CFTypeRef);
// Resolved at runtime so the runtime does not link CoreGraphics or CoreVideo
// itself; apps that never load them simply get no estimate.
struct CFEstimators {
TypeIDFn cgImageTypeID = nullptr;
SizeOfFn cgImageBytesPerRow = nullptr;
SizeOfFn cgImageHeight = nullptr;
TypeIDFn pixelBufferTypeID = nullptr;
SizeOfFn pixelBufferDataSize = nullptr;
};
const CFEstimators& GetCFEstimators() {
static CFEstimators estimators = [] {
CFEstimators e;
e.cgImageTypeID = (TypeIDFn)dlsym(RTLD_DEFAULT, "CGImageGetTypeID");
e.cgImageBytesPerRow = (SizeOfFn)dlsym(RTLD_DEFAULT, "CGImageGetBytesPerRow");
e.cgImageHeight = (SizeOfFn)dlsym(RTLD_DEFAULT, "CGImageGetHeight");
e.pixelBufferTypeID = (TypeIDFn)dlsym(RTLD_DEFAULT, "CVPixelBufferGetTypeID");
e.pixelBufferDataSize = (SizeOfFn)dlsym(RTLD_DEFAULT, "CVPixelBufferGetDataSize");
return e;
}();
return estimators;
}
size_t CGImageSize(CFTypeRef image) {
const CFEstimators& e = GetCFEstimators();
if (image == nullptr || e.cgImageBytesPerRow == nullptr || e.cgImageHeight == nullptr) {
return 0;
}
return e.cgImageBytesPerRow(image) * e.cgImageHeight(image);
}
Estimator ResolveEstimator(Class klass) {
// NSDataAdapter exposes bytes of a JS buffer that V8 already accounts for.
if ([klass isSubclassOfClass:[NSDataAdapter class]]) {
return kNone;
}
if ([klass isSubclassOfClass:[NSData class]]) {
return kNSData;
}
static Class uiImageClass = objc_getClass("UIImage");
if (uiImageClass != nil && [klass isSubclassOfClass:uiImageClass]) {
return kUIImage;
}
static Class cfTypeClass = objc_getClass("__NSCFType");
if (cfTypeClass != nil && klass == cfTypeClass) {
return kCFType;
}
return kNone;
}
size_t Estimate(Estimator estimator, id obj) {
switch (estimator) {
case kNSData:
return [(NSData*)obj length];
case kUIImage: {
// UIKit is not linked by the runtime, so its selectors are registered
// rather than declared. A symbol image rasterizes on -CGImage, and one
// backed by a CIImage has no bitmap until it is rendered (NULL here).
static SEL isSymbolImage = sel_registerName("isSymbolImage");
static SEL cgImage = sel_registerName("CGImage");
if ([obj respondsToSelector:isSymbolImage] &&
((BOOL (*)(id, SEL))objc_msgSend)(obj, isSymbolImage)) {
return 0;
}
return CGImageSize(((CFTypeRef (*)(id, SEL))objc_msgSend)(obj, cgImage));
}
case kCFType: {
const CFEstimators& e = GetCFEstimators();
CFTypeID type = CFGetTypeID((CFTypeRef)obj);
if (e.cgImageTypeID != nullptr && type == e.cgImageTypeID()) {
return CGImageSize((CFTypeRef)obj);
}
if (e.pixelBufferTypeID != nullptr && e.pixelBufferDataSize != nullptr &&
type == e.pixelBufferTypeID()) {
return e.pixelBufferDataSize((CFTypeRef)obj);
}
return 0;
}
case kNone:
return 0;
}
return 0;
}
} // namespace
void SetSize(Isolate* isolate, BaseDataWrapper* wrapper, size_t bytes) {
ExternalMemoryCharge* charge = wrapper->ExternalCharge();
if (charge == nullptr) {
if (bytes == 0) {
return;
}
auto created =
std::make_unique<ExternalMemoryCharge>(isolate, Caches::Get(isolate)->getGateId());
charge = created.get();
wrapper->SetExternalCharge(std::move(created));
} else if (charge->Isolate() != isolate) {
return;
}
charge->Set(bytes);
}
void ChargeEstimatedSize(Isolate* isolate, Local<Value> value) {
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
if (wrapper == nullptr || wrapper->Type() != WrapperType::ObjCObject ||
wrapper->ExternalCharge() != nullptr) {
return;
}
ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper);
id obj = objcWrapper->Data();
if (obj == nil) {
return;
}
auto& estimators = Caches::Get(isolate)->ExternalSizeEstimators;
Class klass = objcWrapper->Klass();
auto it = estimators.find(klass);
Estimator estimator;
if (it != estimators.end()) {
estimator = static_cast<Estimator>(it->second);
} else {
estimator = ResolveEstimator(klass);
estimators.emplace(klass, estimator);
}
if (estimator == kNone) {
return;
}
SetSize(isolate, wrapper, Estimate(estimator, obj));
}
void StartMemoryPressureMonitoring() {
static dispatch_source_t source;
static dispatch_once_t once;
dispatch_once(&once, ^{
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0,
DISPATCH_MEMORYPRESSURE_NORMAL | DISPATCH_MEMORYPRESSURE_WARN |
DISPATCH_MEMORYPRESSURE_CRITICAL,
dispatch_get_global_queue(QOS_CLASS_UTILITY, 0));
dispatch_source_set_event_handler(source, ^{
unsigned long status = dispatch_source_get_data(source);
MemoryPressureLevel level = MemoryPressureLevel::kNone;
if (status & DISPATCH_MEMORYPRESSURE_CRITICAL) {
level = MemoryPressureLevel::kCritical;
} else if (status & DISPATCH_MEMORYPRESSURE_WARN) {
level = MemoryPressureLevel::kModerate;
}
Runtime::NotifyMemoryPressure(level);
});
dispatch_resume(source);
});
}
} // namespace ExternalMemory
} // namespace tns