Skip to content

Commit

Permalink
Merge pull request #434 from NativeScript/jasssonpet/memory-map-snapshot
Browse files Browse the repository at this point in the history
Memory map heap snapshot blob file
  • Loading branch information
jasssonpet committed Apr 27, 2016
2 parents db12fa8 + 1df6e9d commit e97c304
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
40 changes: 40 additions & 0 deletions src/jni/File.cpp
Expand Up @@ -8,6 +8,11 @@
#include "File.h"
#include <sstream>
#include <fstream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>

using namespace std;

Expand Down Expand Up @@ -98,6 +103,41 @@ namespace tns
return Buffer;
}

MemoryMappedFile MemoryMappedFile::Open(const char* filePath)
{
void* memory = nullptr;
int length = 0;
if (FILE* file = fopen(filePath, "r+"))
{
if (fseek(file, 0, SEEK_END) == 0)
{
length = ftell(file);
if (length >= 0)
{
memory = mmap(NULL, length, PROT_READ, MAP_SHARED, fileno(file), 0);
if (memory == MAP_FAILED)
{
memory = nullptr;
}
}
}
fclose(file);
}
return MemoryMappedFile(memory, length);
}

MemoryMappedFile::MemoryMappedFile(void* memory, size_t size)
:
memory(memory), size(size)
{
}

MemoryMappedFile::~MemoryMappedFile()
{
int result = munmap(this->memory, this->size);
assert(result == 0);
}

char* File::Buffer = new char[BUFFER_SIZE];

const char* File::WRITE_BINARY = "wb";
Expand Down
10 changes: 10 additions & 0 deletions src/jni/File.h
Expand Up @@ -12,6 +12,16 @@

namespace tns
{
struct MemoryMappedFile final
{
static MemoryMappedFile Open(const char* filePath);
MemoryMappedFile(void* memory, size_t size);
~MemoryMappedFile();

void* memory = nullptr;
size_t size = 0;
};

class File
{
public:
Expand Down
16 changes: 6 additions & 10 deletions src/jni/Runtime.cpp
Expand Up @@ -17,7 +17,6 @@
#include "JsDebugger.h"
#include "SimpleProfiler.h"
#include "SimpleAllocator.h"
#include "File.h"
#include "JType.h"
#include "Module.h"
#include "NativeScriptException.h"
Expand Down Expand Up @@ -59,7 +58,6 @@ Runtime::Runtime(JNIEnv *env, jobject runtime, int id)
{
m_runtime = m_env.NewGlobalRef(runtime);
m_objectManager = new ObjectManager(m_runtime);
m_startupData = nullptr;
s_id2RuntimeCache.insert(make_pair(id, this));
}

Expand Down Expand Up @@ -340,10 +338,8 @@ void Runtime::PassUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, jthrowab
}

void Runtime::ClearStartupData(JNIEnv *env, jobject obj) {
if (m_startupData) {
delete m_startupData->data;
delete m_startupData;
}
delete m_heapSnapshotBlob;
delete m_startupData;
}

static void InitializeV8() {
Expand Down Expand Up @@ -380,11 +376,11 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,

if (File::Exists(snapshotPath))
{
int length;
m_startupData->data = reinterpret_cast<char*>(File::ReadBinary(snapshotPath, length));
m_startupData->raw_size = length;
m_heapSnapshotBlob = new MemoryMappedFile(MemoryMappedFile::Open(snapshotPath.c_str()));
m_startupData->data = static_cast<const char*>(m_heapSnapshotBlob->memory);
m_startupData->raw_size = m_heapSnapshotBlob->size;

DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), length);
DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), m_heapSnapshotBlob->size);
}
else if(saveSnapshot)
{
Expand Down
4 changes: 3 additions & 1 deletion src/jni/Runtime.h
Expand Up @@ -8,6 +8,7 @@
#include "WeakRef.h"
#include "ArrayBufferHelper.h"
#include "Profiler.h"
#include "File.h"

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

Expand Down Expand Up @@ -58,7 +59,8 @@ namespace tns

Profiler m_profiler;

v8::StartupData *m_startupData;
v8::StartupData *m_startupData = nullptr;
MemoryMappedFile *m_heapSnapshotBlob = nullptr;

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

0 comments on commit e97c304

Please sign in to comment.