Skip to content

Commit eaebd1e

Browse files
committed
Merge pull request #341 from NativeScript/atanasovg/snapshot-addons
Some heap snapshot related addons
2 parents e7e88f8 + 996b02e commit eaebd1e

12 files changed

+80
-54
lines changed

src/jni/Constants.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ std::string Constants::APP_ROOT_FOLDER_PATH = "";
1111
bool Constants::V8_CACHE_COMPILED_CODE = false;
1212
bool Constants::V8_HEAP_SNAPSHOT = false;
1313
std::string Constants::V8_STARTUP_FLAGS = "";
14+
std::string Constants::V8_HEAP_SNAPSHOT_SCRIPT = "";

src/jni/Constants.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
class Constants
77
{
8-
public:
9-
const static int PRIMITIVE_TYPE_OFFSET = 1;
8+
public:
9+
const static int PRIMITIVE_TYPE_OFFSET = 1;
1010

1111
const static char CLASS_NAME_LOCATION_SEPARATOR = '_';
1212

13-
static std::string APP_ROOT_FOLDER_PATH;
14-
static std::string V8_STARTUP_FLAGS;
15-
static bool V8_CACHE_COMPILED_CODE;
16-
static bool V8_HEAP_SNAPSHOT;
13+
static std::string APP_ROOT_FOLDER_PATH;
14+
static std::string V8_STARTUP_FLAGS;
15+
static std::string V8_HEAP_SNAPSHOT_SCRIPT;
16+
static bool V8_CACHE_COMPILED_CODE;
17+
static bool V8_HEAP_SNAPSHOT;
1718

1819
private:
1920
Constants()

src/jni/MetadataNode.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,15 +1472,25 @@ bool MetadataNode::GetExtendLocation(string& extendLocation)
14721472
}
14731473

14741474
string srcFileName = ConvertToString(scriptName);
1475+
string fullPathToFile;
1476+
if(srcFileName == "<embedded script>")
1477+
{
1478+
// Corner case, extend call is coming from the heap snapshot script
1479+
// This is possible for lazily compiled code - e.g. from the body of a function
1480+
fullPathToFile = "_embedded_script_";
1481+
}
1482+
else
1483+
{
1484+
string hardcodedPathToSkip = Constants::APP_ROOT_FOLDER_PATH;
14751485

1476-
string hardcodedPathToSkip = Constants::APP_ROOT_FOLDER_PATH;
1486+
int startIndex = hardcodedPathToSkip.length();
1487+
int strToTakeLen = (srcFileName.length() - startIndex - 3); // 3 refers to .js at the end of file name
1488+
fullPathToFile = srcFileName.substr(startIndex, strToTakeLen);
14771489

1478-
int startIndex = hardcodedPathToSkip.length();
1479-
int strToTakeLen = (srcFileName.length() - startIndex - 3); // 3 refers to .js at the end of file name
1480-
string fullPathToFile = srcFileName.substr(startIndex, strToTakeLen);
1490+
std::replace(fullPathToFile.begin(), fullPathToFile.end(), '/', '_');
1491+
std::replace(fullPathToFile.begin(), fullPathToFile.end(), '.', '_');
1492+
}
14811493

1482-
std::replace(fullPathToFile.begin(), fullPathToFile.end(), '/', '_');
1483-
std::replace(fullPathToFile.begin(), fullPathToFile.end(), '.', '_');
14841494
int lineNumber = frame->GetLineNumber();
14851495
if (lineNumber < 0)
14861496
{

src/jni/Module.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Constants.h"
1414
#include "NativeScriptException.h"
1515
#include "Util.h"
16+
#include "SimpleProfiler.h"
1617

1718
#include <sstream>
1819
#include <assert.h>
@@ -34,14 +35,20 @@ void Module::Init(Isolate *isolate)
3435
assert(RESOLVE_PATH_METHOD_ID != nullptr);
3536

3637
string requireFactoryScript =
37-
"(function () { "
38-
" function require_factory(requireInternal, dirName) { "
39-
" return function require(modulePath) { "
40-
" return requireInternal(modulePath, dirName); "
41-
" } "
42-
" } "
43-
" return require_factory; "
44-
"})()";
38+
"(function () { "
39+
" function require_factory(requireInternal, dirName) { "
40+
" return function require(modulePath) { "
41+
" if(global.__requireOverride) { "
42+
" var result = global.__requireOverride(modulePath, dirName); "
43+
" if(result) { "
44+
" return result; "
45+
" } "
46+
" } "
47+
" return requireInternal(modulePath, dirName); "
48+
" } "
49+
" } "
50+
" return require_factory; "
51+
"})()";
4552

4653
auto source = ConvertToV8String(requireFactoryScript);
4754
auto context = isolate->GetCurrentContext();
@@ -272,6 +279,8 @@ Local<Object> Module::LoadModule(Isolate *isolate, const string& modulePath)
272279
throw NativeScriptException(errMsg);
273280
}
274281

282+
SET_PROFILER_FRAME();
283+
275284
auto fileName = ConvertToV8String(modulePath);
276285
char pathcopy[1024];
277286
strcpy(pathcopy, modulePath.c_str());
@@ -280,7 +289,8 @@ Local<Object> Module::LoadModule(Isolate *isolate, const string& modulePath)
280289
auto require = GetRequireFunction(isolate, strDirName);
281290
Local<Value> requireArgs[5]
282291
{
283-
moduleObj, exportsObj, require, fileName, dirName };
292+
moduleObj, exportsObj, require, fileName, dirName
293+
};
284294

285295
moduleObj->Set(ConvertToV8String("require"), require);
286296

src/jni/Module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace tns
2323

2424
static v8::Local<v8::Object> Load(const std::string& path, bool& isData);
2525

26+
static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
27+
2628
private:
2729
Module() = default;
2830

@@ -34,8 +36,6 @@ namespace tns
3436

3537
static v8::Local<v8::Script> LoadScript(v8::Isolate *isolate, const std::string& modulePath, const v8::Local<v8::String>& fullRequiredModulePath);
3638

37-
static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
38-
3939
static void RequireNativeCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
4040

4141
static v8::Local<v8::Function> GetRequireFunction(v8::Isolate *isolate, const std::string& dirName);

src/jni/NativePlatform.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ Isolate* NativePlatform::InitNativeScript(JNIEnv *_env, jobject obj, jstring fil
7474
JniLocalRef cacheCode(env.GetObjectArrayElement(args, 1));
7575
Constants::V8_CACHE_COMPILED_CODE = (bool) cacheCode;
7676
JniLocalRef snapshot(env.GetObjectArrayElement(args, 2));
77-
Constants::V8_HEAP_SNAPSHOT = (bool) snapshot;
77+
Constants::V8_HEAP_SNAPSHOT = (bool)snapshot;
78+
JniLocalRef snapshotScript(env.GetObjectArrayElement(args, 3));
79+
Constants::V8_HEAP_SNAPSHOT_SCRIPT = ArgConverter::jstringToString(snapshotScript);
7880

7981
DEBUG_WRITE("Initializing Telerik NativeScript: app instance id:%d", appJavaObjectId);
8082

@@ -329,22 +331,29 @@ Isolate* NativePlatform::PrepareV8Runtime(JEnv& env, const string& filesPath, js
329331
V8::Initialize();
330332

331333
Isolate::CreateParams create_params;
332-
create_params.array_buffer_allocator = &g_allocator;
334+
StartupData startup_data;
335+
string customScript;
333336

337+
create_params.array_buffer_allocator = &g_allocator;
334338
// prepare the snapshot blob
335339
if (Constants::V8_HEAP_SNAPSHOT)
336340
{
337-
auto snapshotPath = filesPath + "/internal/snapshot.dat";
338-
StartupData startup_data;
339-
if (File::Exists(snapshotPath))
341+
auto snapshotPath = filesPath + "/internal/snapshot.blob";
342+
if( File::Exists(snapshotPath))
340343
{
341344
int length;
342345
startup_data.data = reinterpret_cast<char*>(File::ReadBinary(snapshotPath, length));
343346
startup_data.raw_size = length;
344347
}
345348
else
346349
{
347-
startup_data = V8::CreateSnapshotDataBlob();
350+
// check for custom script to include in the snapshot
351+
if(Constants::V8_HEAP_SNAPSHOT_SCRIPT.size() > 0 && File::Exists(Constants::V8_HEAP_SNAPSHOT_SCRIPT))
352+
{
353+
customScript = File::ReadText(Constants::V8_HEAP_SNAPSHOT_SCRIPT);
354+
}
355+
356+
startup_data = V8::CreateSnapshotDataBlob(customScript.c_str());
348357
File::WriteBinary(snapshotPath, startup_data.data, startup_data.raw_size);
349358
}
350359

@@ -375,6 +384,7 @@ Isolate* NativePlatform::PrepareV8Runtime(JEnv& env, const string& filesPath, js
375384
globalTemplate->Set(ConvertToV8String("__enableVerboseLogging"), FunctionTemplate::New(isolate, NativeScriptRuntime::EnableVerboseLoggingMethodCallback));
376385
globalTemplate->Set(ConvertToV8String("__disableVerboseLogging"), FunctionTemplate::New(isolate, NativeScriptRuntime::DisableVerboseLoggingMethodCallback));
377386
globalTemplate->Set(ConvertToV8String("__exit"), FunctionTemplate::New(isolate, NativeScriptRuntime::ExitMethodCallback));
387+
globalTemplate->Set(ConvertToV8String("__nativeRequire"), FunctionTemplate::New(isolate, Module::RequireCallback));
378388

379389
WeakRef::Init(isolate, globalTemplate, g_objectManager);
380390

@@ -388,7 +398,7 @@ Isolate* NativePlatform::PrepareV8Runtime(JEnv& env, const string& filesPath, js
388398
if (Constants::V8_HEAP_SNAPSHOT)
389399
{
390400
// we own the snapshot buffer, delete it
391-
delete[] create_params.snapshot_blob->data;
401+
delete[] startup_data.data;
392402
}
393403

394404
context_scope = new Context::Scope(context);

src/jni/SimpleProfiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ SimpleProfiler::~SimpleProfiler()
4646
struct timespec nowt;
4747
clock_gettime(CLOCK_MONOTONIC, &nowt);
4848
auto time = (int64_t) nowt.tv_sec * 1000000000LL + nowt.tv_nsec;
49-
m_frame->time += (time - m_time);
49+
m_frame->time += (time - m_time) / 1000000;
5050
}
5151
}
5252

@@ -85,7 +85,7 @@ void SimpleProfiler::PrintProfilerData()
8585
std::sort(s_frames.begin(), s_frames.end());
8686
for (auto& f : s_frames)
8787
{
88-
__android_log_print(ANDROID_LOG_DEBUG, "TNS.Native", "Time: %lld, File: %s, Line: %d", f.time, f.fileName, f.lineNumber);
88+
__android_log_print(ANDROID_LOG_DEBUG, "TNS.Native.Profiler", "Time: %lld, File: %s, Line: %d", f.time, f.fileName, f.lineNumber);
8989
}
9090
}
9191

src/src/com/tns/JsDebugger.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@
33
import java.io.BufferedReader;
44
import java.io.Closeable;
55
import java.io.File;
6-
import java.io.FileOutputStream;
7-
import java.io.FileReader;
86
import java.io.IOException;
9-
import java.io.InputStream;
107
import java.io.InputStreamReader;
118
import java.io.OutputStream;
12-
import java.io.OutputStreamWriter;
139
import java.io.UnsupportedEncodingException;
14-
import java.net.ServerSocket;
15-
import java.net.Socket;
1610
import java.util.ArrayList;
1711
import java.util.NoSuchElementException;
1812
import java.util.Scanner;

src/src/com/tns/LogcatLogger.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.tns;
22

3-
import java.io.BufferedReader;
4-
import java.io.Closeable;
5-
import java.io.IOException;
6-
import java.io.InputStreamReader;
7-
83
import android.content.Context;
94
import android.util.Log;
105

src/src/com/tns/Module.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.File;
44
import java.io.IOException;
5-
import java.security.InvalidParameterException;
65
import java.util.HashMap;
76

87
import org.json.JSONException;

0 commit comments

Comments
 (0)