-
Notifications
You must be signed in to change notification settings - Fork 830
/
Copy pathExternalMemTracking.cc
100 lines (86 loc) · 3.58 KB
/
ExternalMemTracking.cc
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
#include "ExternalMemTracking.h"
#include <iostream>
#ifdef OPENCV4NODEJS_ENABLE_EXTERNALMEMTRACKING
CustomMatAllocator *ExternalMemTracking::custommatallocator = NULL;
#endif
NAN_MODULE_INIT(ExternalMemTracking::Init) {
#ifdef OPENCV4NODEJS_ENABLE_EXTERNALMEMTRACKING
try {
char* env = std::getenv("OPENCV4NODEJS_DISABLE_EXTERNAL_MEM_TRACKING");
if (env == NULL && custommatallocator == NULL) {
custommatallocator = new CustomMatAllocator();
cv::Mat::setDefaultAllocator(custommatallocator);
}
}
catch (...) {
printf("ExternalMemTracking::Init - fatal exception while trying to read env: OPENCV4NODEJS_DISABLE_EXTERNAL_MEM_TRACKING");
}
#endif
Nan::SetMethod(target, "isCustomMatAllocatorEnabled", IsCustomMatAllocatorEnabled);
Nan::SetMethod(target, "dangerousEnableCustomMatAllocator", DangerousEnableCustomMatAllocator);
Nan::SetMethod(target, "dangerousDisableCustomMatAllocator", DangerousDisableCustomMatAllocator);
Nan::SetMethod(target, "getMemMetrics", GetMemMetrics);
};
NAN_METHOD(ExternalMemTracking::GetMemMetrics) {
int64_t TotalAlloc = -1;
int64_t TotalKnownByJS = -1;
int64_t NumAllocations = -1;
int64_t NumDeAllocations = -1;
#ifdef OPENCV4NODEJS_ENABLE_EXTERNALMEMTRACKING
if (ExternalMemTracking::custommatallocator != NULL){
TotalAlloc = ExternalMemTracking::custommatallocator->readtotalmem();
TotalKnownByJS = ExternalMemTracking::custommatallocator->readmeminformed();
NumAllocations = ExternalMemTracking::custommatallocator->readnumallocated();
NumDeAllocations = ExternalMemTracking::custommatallocator->readnumdeallocated();
}
#endif
v8::Local<v8::Object> result = Nan::New<v8::Object>();
Nan::Set(result, FF::newString("TotalAlloc"), Nan::New((double)TotalAlloc));
Nan::Set(result, FF::newString("TotalKnownByJS"), Nan::New((double)TotalKnownByJS));
Nan::Set(result, FF::newString("NumAllocations"), Nan::New((double)NumAllocations));
Nan::Set(result, FF::newString("NumDeAllocations"), Nan::New((double)NumDeAllocations));
info.GetReturnValue().Set(result);
return;
}
NAN_METHOD(ExternalMemTracking::IsCustomMatAllocatorEnabled) {
bool allocatorOn = false;
#ifdef OPENCV4NODEJS_ENABLE_EXTERNALMEMTRACKING
if (ExternalMemTracking::custommatallocator != NULL){
allocatorOn = true;
}
#endif
info.GetReturnValue().Set(allocatorOn);
}
NAN_METHOD(ExternalMemTracking::DangerousEnableCustomMatAllocator) {
bool success = false;
#ifdef OPENCV4NODEJS_ENABLE_EXTERNALMEMTRACKING
if (ExternalMemTracking::custommatallocator == NULL) {
ExternalMemTracking::custommatallocator = new CustomMatAllocator();
cv::Mat::setDefaultAllocator(ExternalMemTracking::custommatallocator);
}
success = ExternalMemTracking::custommatallocator != NULL;
#endif
info.GetReturnValue().Set(success);
}
NAN_METHOD(ExternalMemTracking::DangerousDisableCustomMatAllocator) {
bool success = false;
#ifdef OPENCV4NODEJS_ENABLE_EXTERNALMEMTRACKING
if (ExternalMemTracking::custommatallocator != NULL) {
CustomMatAllocator *allocator = ExternalMemTracking::custommatallocator;
// return default allocator
if (allocator->variables) {
allocator->variables->MemTotalChangeMutex.lock();
}
cv::Mat::setDefaultAllocator(NULL);
ExternalMemTracking::custommatallocator = NULL;
if (allocator->variables) {
allocator->variables->MemTotalChangeMutex.unlock();
}
// sorry, can't delete it, since it may be references by a number of outstanding Mats -> memory leak, but it's small
// and should not happen often, or ever!.
//delete allocator;
}
success = ExternalMemTracking::custommatallocator == NULL;
#endif
info.GetReturnValue().Set(success);
}