Skip to content

Commit 1df6e9d

Browse files
committed
Memory map heap snapshot blob
1 parent 3d4448b commit 1df6e9d

4 files changed

Lines changed: 59 additions & 11 deletions

File tree

src/jni/File.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#include "File.h"
99
#include <sstream>
1010
#include <fstream>
11+
#include <sys/mman.h>
12+
#include <sys/stat.h>
13+
#include <fcntl.h>
14+
#include <unistd.h>
15+
#include <assert.h>
1116

1217
using namespace std;
1318

@@ -98,6 +103,41 @@ namespace tns
98103
return Buffer;
99104
}
100105

106+
MemoryMappedFile MemoryMappedFile::Open(const char* filePath)
107+
{
108+
void* memory = nullptr;
109+
int length = 0;
110+
if (FILE* file = fopen(filePath, "r+"))
111+
{
112+
if (fseek(file, 0, SEEK_END) == 0)
113+
{
114+
length = ftell(file);
115+
if (length >= 0)
116+
{
117+
memory = mmap(NULL, length, PROT_READ, MAP_SHARED, fileno(file), 0);
118+
if (memory == MAP_FAILED)
119+
{
120+
memory = nullptr;
121+
}
122+
}
123+
}
124+
fclose(file);
125+
}
126+
return MemoryMappedFile(memory, length);
127+
}
128+
129+
MemoryMappedFile::MemoryMappedFile(void* memory, size_t size)
130+
:
131+
memory(memory), size(size)
132+
{
133+
}
134+
135+
MemoryMappedFile::~MemoryMappedFile()
136+
{
137+
int result = munmap(this->memory, this->size);
138+
assert(result == 0);
139+
}
140+
101141
char* File::Buffer = new char[BUFFER_SIZE];
102142

103143
const char* File::WRITE_BINARY = "wb";

src/jni/File.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212

1313
namespace tns
1414
{
15+
struct MemoryMappedFile final
16+
{
17+
static MemoryMappedFile Open(const char* filePath);
18+
MemoryMappedFile(void* memory, size_t size);
19+
~MemoryMappedFile();
20+
21+
void* memory = nullptr;
22+
size_t size = 0;
23+
};
24+
1525
class File
1626
{
1727
public:

src/jni/Runtime.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "JsDebugger.h"
1818
#include "SimpleProfiler.h"
1919
#include "SimpleAllocator.h"
20-
#include "File.h"
2120
#include "JType.h"
2221
#include "Module.h"
2322
#include "NativeScriptException.h"
@@ -58,7 +57,6 @@ Runtime::Runtime(JNIEnv *env, jobject runtime, int id)
5857
{
5958
m_runtime = m_env.NewGlobalRef(runtime);
6059
m_objectManager = new ObjectManager(m_runtime);
61-
m_startupData = nullptr;
6260
s_id2RuntimeCache.insert(make_pair(id, this));
6361
}
6462

@@ -339,10 +337,8 @@ void Runtime::PassUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, jthrowab
339337
}
340338

341339
void Runtime::ClearStartupData(JNIEnv *env, jobject obj) {
342-
if (m_startupData) {
343-
delete m_startupData->data;
344-
delete m_startupData;
345-
}
340+
delete m_heapSnapshotBlob;
341+
delete m_startupData;
346342
}
347343

348344
Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, jobject jsDebugger, jstring profilerOutputDir)
@@ -377,11 +373,11 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
377373

378374
if (File::Exists(snapshotPath))
379375
{
380-
int length;
381-
m_startupData->data = reinterpret_cast<char*>(File::ReadBinary(snapshotPath, length));
382-
m_startupData->raw_size = length;
376+
m_heapSnapshotBlob = new MemoryMappedFile(MemoryMappedFile::Open(snapshotPath.c_str()));
377+
m_startupData->data = static_cast<const char*>(m_heapSnapshotBlob->memory);
378+
m_startupData->raw_size = m_heapSnapshotBlob->size;
383379

384-
DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), length);
380+
DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), m_heapSnapshotBlob->size);
385381
}
386382
else if(saveSnapshot)
387383
{

src/jni/Runtime.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "WeakRef.h"
99
#include "ArrayBufferHelper.h"
1010
#include "Profiler.h"
11+
#include "File.h"
1112

1213
jobject ConvertJsValueToJavaObject(tns::JEnv& env, const v8::Local<v8::Value>& value, int classReturnType);
1314

@@ -58,7 +59,8 @@ namespace tns
5859

5960
Profiler m_profiler;
6061

61-
v8::StartupData *m_startupData;
62+
v8::StartupData *m_startupData = nullptr;
63+
MemoryMappedFile *m_heapSnapshotBlob = nullptr;
6264

6365
static void PrepareExtendFunction(v8::Isolate *isolate, jstring filesPath);
6466
v8::Isolate* PrepareV8Runtime(const std::string& filesPath, jstring packageName, jobject jsDebugger, jstring profilerOutputDir);

0 commit comments

Comments
 (0)