Skip to content

Commit 226b583

Browse files
schuayCommit Bot
authored andcommitted
[perf] Refactor the Memory benchmark to use d8
Until this CL, the Memory benchmark was the only one to be based on a cctest runner; all others use d8. Besides being a tedious exception to the rule, this caused issues such as described in the linked bug (summary: refbuilds are built with v8_static_library, and neither cctests nor unittests support this configuration). Here, we move the Memory benchmark into a d8 runner. Bug: v8:9189, chromium:957029 Change-Id: I9b45ff36f4842cb0bdef2c1c4b0184c5509d3385 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1588464 Commit-Queue: Jakob Gruber <[email protected]> Reviewed-by: Sigurd Schneider <[email protected]> Reviewed-by: Michael Achenbach <[email protected]> Reviewed-by: Sergiy Belozorov <[email protected]> Cr-Commit-Position: refs/heads/master@{#61245}
1 parent ec379ea commit 226b583

File tree

7 files changed

+116
-53
lines changed

7 files changed

+116
-53
lines changed

src/runtime/runtime-debug.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,5 +823,35 @@ RUNTIME_FUNCTION(Runtime_PerformSideEffectCheckForObject) {
823823
return ReadOnlyRoots(isolate).undefined_value();
824824
}
825825

826+
RUNTIME_FUNCTION(Runtime_ProfileCreateSnapshotDataBlob) {
827+
HandleScope scope(isolate);
828+
DCHECK_EQ(0, args.length());
829+
830+
// Used only by the test/memory/Memory.json benchmark. This creates a snapshot
831+
// blob and outputs various statistics around it.
832+
833+
DCHECK(FLAG_profile_deserialization);
834+
835+
DisableEmbeddedBlobRefcounting();
836+
837+
v8::StartupData blob = CreateSnapshotDataBlobInternal(
838+
v8::SnapshotCreator::FunctionCodeHandling::kClear, nullptr);
839+
delete[] blob.data;
840+
841+
// Track the embedded blob size as well.
842+
{
843+
int embedded_blob_size = 0;
844+
if (FLAG_embedded_builtins) {
845+
i::EmbeddedData d = i::EmbeddedData::FromBlob();
846+
embedded_blob_size = static_cast<int>(d.size());
847+
}
848+
PrintF("Embedded blob is %d bytes\n", embedded_blob_size);
849+
}
850+
851+
FreeCurrentEmbeddedBlob();
852+
853+
return ReadOnlyRoots(isolate).undefined_value();
854+
}
855+
826856
} // namespace internal
827857
} // namespace v8

src/runtime/runtime.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ namespace internal {
119119
#define FOR_EACH_INTRINSIC_DEBUG(F, I) \
120120
F(ClearStepping, 0, 1) \
121121
F(CollectGarbage, 1, 1) \
122+
F(DebugAsyncFunctionEntered, 1, 1) \
123+
F(DebugAsyncFunctionFinished, 2, 1) \
124+
F(DebugAsyncFunctionSuspended, 1, 1) \
122125
F(DebugBreakAtEntry, 1, 1) \
123126
F(DebugCollectCoverage, 0, 1) \
124127
F(DebugGetLoadedScriptIds, 0, 1) \
125128
F(DebugOnFunctionCall, 2, 1) \
126129
F(DebugPopPromise, 0, 1) \
127130
F(DebugPrepareStepInSuspendedGenerator, 0, 1) \
128131
F(DebugPushPromise, 1, 1) \
129-
F(DebugAsyncFunctionEntered, 1, 1) \
130-
F(DebugAsyncFunctionFinished, 2, 1) \
131-
F(DebugAsyncFunctionSuspended, 1, 1) \
132132
F(DebugToggleBlockCoverage, 1, 1) \
133133
F(DebugTogglePreciseCoverage, 1, 1) \
134134
F(FunctionGetInferredName, 1, 1) \
@@ -137,12 +137,13 @@ namespace internal {
137137
F(GetGeneratorScopeDetails, 2, 1) \
138138
F(GetHeapUsage, 0, 1) \
139139
F(HandleDebuggerStatement, 0, 1) \
140-
I(IncBlockCounter, 2, 1) \
141140
F(IsBreakOnException, 1, 1) \
141+
F(LiveEditPatchScript, 2, 1) \
142+
F(ProfileCreateSnapshotDataBlob, 0, 1) \
142143
F(ScheduleBreak, 0, 1) \
143144
F(ScriptLocationFromLine2, 4, 1) \
144145
F(SetGeneratorScopeVariableValue, 4, 1) \
145-
F(LiveEditPatchScript, 2, 1)
146+
I(IncBlockCounter, 2, 1)
146147

147148
#define FOR_EACH_INTRINSIC_FORIN(F, I) \
148149
F(ForInEnumerate, 1, 1) \

src/snapshot/snapshot-common.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,5 +354,56 @@ Vector<const byte> SnapshotData::Payload() const {
354354
return Vector<const byte>(payload, length);
355355
}
356356

357+
namespace {
358+
359+
bool RunExtraCode(v8::Isolate* isolate, v8::Local<v8::Context> context,
360+
const char* utf8_source, const char* name) {
361+
v8::Context::Scope context_scope(context);
362+
v8::TryCatch try_catch(isolate);
363+
v8::Local<v8::String> source_string;
364+
if (!v8::String::NewFromUtf8(isolate, utf8_source, v8::NewStringType::kNormal)
365+
.ToLocal(&source_string)) {
366+
return false;
367+
}
368+
v8::Local<v8::String> resource_name =
369+
v8::String::NewFromUtf8(isolate, name, v8::NewStringType::kNormal)
370+
.ToLocalChecked();
371+
v8::ScriptOrigin origin(resource_name);
372+
v8::ScriptCompiler::Source source(source_string, origin);
373+
v8::Local<v8::Script> script;
374+
if (!v8::ScriptCompiler::Compile(context, &source).ToLocal(&script))
375+
return false;
376+
if (script->Run(context).IsEmpty()) return false;
377+
CHECK(!try_catch.HasCaught());
378+
return true;
379+
}
380+
381+
} // namespace
382+
383+
// TODO(jgruber): Merge with related code in mksnapshot.cc and
384+
// inspector-test.cc.
385+
v8::StartupData CreateSnapshotDataBlobInternal(
386+
v8::SnapshotCreator::FunctionCodeHandling function_code_handling,
387+
const char* embedded_source) {
388+
// Create a new isolate and a new context from scratch, optionally run
389+
// a script to embed, and serialize to create a snapshot blob.
390+
v8::StartupData result = {nullptr, 0};
391+
{
392+
v8::SnapshotCreator snapshot_creator;
393+
v8::Isolate* isolate = snapshot_creator.GetIsolate();
394+
{
395+
v8::HandleScope scope(isolate);
396+
v8::Local<v8::Context> context = v8::Context::New(isolate);
397+
if (embedded_source != nullptr &&
398+
!RunExtraCode(isolate, context, embedded_source, "<embedded>")) {
399+
return result;
400+
}
401+
snapshot_creator.SetDefaultContext(context);
402+
}
403+
result = snapshot_creator.CreateBlob(function_code_handling);
404+
}
405+
return result;
406+
}
407+
357408
} // namespace internal
358409
} // namespace v8

src/snapshot/snapshot.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ class Snapshot : public AllStatic {
158158
DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot);
159159
};
160160

161+
// Convenience wrapper around snapshot data blob creation used e.g. by tests and
162+
// mksnapshot.
163+
V8_EXPORT_PRIVATE v8::StartupData CreateSnapshotDataBlobInternal(
164+
v8::SnapshotCreator::FunctionCodeHandling function_code_handling,
165+
const char* embedded_source);
166+
161167
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
162168
void SetSnapshotFromFile(StartupData* snapshot_blob);
163169
#endif

test/cctest/test-serialize.cc

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -169,32 +169,8 @@ bool RunExtraCode(v8::Isolate* isolate, v8::Local<v8::Context> context,
169169
return true;
170170
}
171171

172-
v8::StartupData CreateSnapshotDataBlob(
173-
v8::SnapshotCreator::FunctionCodeHandling function_code_handling,
174-
const char* embedded_source) {
175-
// Create a new isolate and a new context from scratch, optionally run
176-
// a script to embed, and serialize to create a snapshot blob.
177-
DisableEmbeddedBlobRefcounting();
178-
v8::StartupData result = {nullptr, 0};
179-
{
180-
v8::SnapshotCreator snapshot_creator;
181-
v8::Isolate* isolate = snapshot_creator.GetIsolate();
182-
{
183-
v8::HandleScope scope(isolate);
184-
v8::Local<v8::Context> context = v8::Context::New(isolate);
185-
if (embedded_source != nullptr &&
186-
!RunExtraCode(isolate, context, embedded_source, "<embedded>")) {
187-
return result;
188-
}
189-
snapshot_creator.SetDefaultContext(context);
190-
}
191-
result = snapshot_creator.CreateBlob(function_code_handling);
192-
}
193-
return result;
194-
}
195-
196172
v8::StartupData CreateSnapshotDataBlob(const char* embedded_source = nullptr) {
197-
return CreateSnapshotDataBlob(
173+
return CreateSnapshotDataBlobInternal(
198174
v8::SnapshotCreator::FunctionCodeHandling::kClear, embedded_source);
199175
}
200176

@@ -770,6 +746,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlob1) {
770746
DisableAlwaysOpt();
771747
const char* source1 = "function f() { return 42; }";
772748

749+
DisableEmbeddedBlobRefcounting();
773750
v8::StartupData data1 = CreateSnapshotDataBlob(source1);
774751

775752
v8::Isolate::CreateParams params1;
@@ -801,6 +778,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlobOverwriteGlobal) {
801778
DisableAlwaysOpt();
802779
const char* source1 = "function f() { return 42; }";
803780

781+
DisableEmbeddedBlobRefcounting();
804782
v8::StartupData data1 = CreateSnapshotDataBlob(source1);
805783

806784
v8::Isolate::CreateParams params1;
@@ -839,6 +817,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlobStringNotInternalized) {
839817
function f() { return global; }
840818
)javascript";
841819

820+
DisableEmbeddedBlobRefcounting();
842821
v8::StartupData data1 = CreateSnapshotDataBlob(source1);
843822

844823
v8::Isolate::CreateParams params1;
@@ -878,8 +857,9 @@ void TestCustomSnapshotDataBlobWithIrregexpCode(
878857
"function i() { return '/* a comment */'.search(re2); }\n"
879858
"f(); f(); g(); g(); h(); h(); i(); i();\n";
880859

860+
DisableEmbeddedBlobRefcounting();
881861
v8::StartupData data1 =
882-
CreateSnapshotDataBlob(function_code_handling, source);
862+
CreateSnapshotDataBlobInternal(function_code_handling, source);
883863

884864
v8::Isolate::CreateParams params1;
885865
params1.snapshot_blob = &data1;
@@ -945,6 +925,7 @@ UNINITIALIZED_TEST(SnapshotChecksum) {
945925
DisableAlwaysOpt();
946926
const char* source1 = "function f() { return 42; }";
947927

928+
DisableEmbeddedBlobRefcounting();
948929
v8::StartupData data1 = CreateSnapshotDataBlob(source1);
949930
CHECK(i::Snapshot::VerifyChecksum(&data1));
950931
const_cast<char*>(data1.data)[142] = data1.data[142] ^ 4; // Flip a bit.
@@ -1302,6 +1283,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlob2) {
13021283
"function g() { return 43; }"
13031284
"/./.test('a')";
13041285

1286+
DisableEmbeddedBlobRefcounting();
13051287
v8::StartupData data2 = CreateSnapshotDataBlob(source2);
13061288

13071289
v8::Isolate::CreateParams params2;
@@ -1346,6 +1328,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlobOutdatedContextWithOverflow) {
13461328

13471329
const char* source2 = "o.a(42)";
13481330

1331+
DisableEmbeddedBlobRefcounting();
13491332
v8::StartupData data = CreateSnapshotDataBlob(source1);
13501333

13511334
v8::Isolate::CreateParams params;
@@ -1397,6 +1380,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlobWithLocker) {
13971380

13981381
const char* source1 = "function f() { return 42; }";
13991382

1383+
DisableEmbeddedBlobRefcounting();
14001384
v8::StartupData data1 = CreateSnapshotDataBlob(source1);
14011385

14021386
v8::Isolate::CreateParams params1;
@@ -1430,6 +1414,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlobStackOverflow) {
14301414
" b = c;"
14311415
"}";
14321416

1417+
DisableEmbeddedBlobRefcounting();
14331418
v8::StartupData data = CreateSnapshotDataBlob(source);
14341419

14351420
v8::Isolate::CreateParams params;
@@ -1470,6 +1455,7 @@ UNINITIALIZED_TEST(SnapshotDataBlobWithWarmup) {
14701455
DisableAlwaysOpt();
14711456
const char* warmup = "Math.abs(1); Math.random = 1;";
14721457

1458+
DisableEmbeddedBlobRefcounting();
14731459
v8::StartupData cold = CreateSnapshotDataBlob();
14741460
v8::StartupData warm = WarmUpSnapshotDataBlob(cold, warmup);
14751461
delete[] cold.data;
@@ -1505,6 +1491,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlobWithWarmup) {
15051491
"var a = 5";
15061492
const char* warmup = "a = f()";
15071493

1494+
DisableEmbeddedBlobRefcounting();
15081495
v8::StartupData cold = CreateSnapshotDataBlob(source);
15091496
v8::StartupData warm = WarmUpSnapshotDataBlob(cold, warmup);
15101497
delete[] cold.data;
@@ -1544,6 +1531,7 @@ UNINITIALIZED_TEST(CustomSnapshotDataBlobImmortalImmovableRoots) {
15441531
StaticCharVector("a.push(function() {return 7});"),
15451532
StaticCharVector("\0"), 10000);
15461533

1534+
DisableEmbeddedBlobRefcounting();
15471535
v8::StartupData data =
15481536
CreateSnapshotDataBlob(reinterpret_cast<const char*>(source.begin()));
15491537

@@ -3818,25 +3806,6 @@ UNINITIALIZED_TEST(ReinitializeHashSeedRehashable) {
38183806
FreeCurrentEmbeddedBlob();
38193807
}
38203808

3821-
UNINITIALIZED_TEST(SerializationStats) {
3822-
FLAG_profile_deserialization = true;
3823-
FLAG_always_opt = false;
3824-
v8::StartupData blob = CreateSnapshotDataBlob();
3825-
delete[] blob.data;
3826-
3827-
// Track the embedded blob size as well.
3828-
{
3829-
int embedded_blob_size = 0;
3830-
if (FLAG_embedded_builtins) {
3831-
i::EmbeddedData d = i::EmbeddedData::FromBlob();
3832-
embedded_blob_size = static_cast<int>(d.size());
3833-
}
3834-
PrintF("Embedded blob is %d bytes\n", embedded_blob_size);
3835-
}
3836-
3837-
FreeCurrentEmbeddedBlob();
3838-
}
3839-
38403809
void CheckSFIsAreWeak(WeakFixedArray sfis, Isolate* isolate) {
38413810
CHECK_GT(sfis->length(), 0);
38423811
int no_of_weak = 0;

test/memory/Memory.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"name": "Memory",
44
"run_count": 5,
55
"units": "bytes",
6-
"path" : ["."],
7-
"binary": "cctest",
8-
"main": "test-serialize/SerializationStats",
6+
"resources": ["run.js"],
7+
"main": "run.js",
8+
"path": ["."],
9+
"flags": ["--allow-natives-syntax", "--profile-deserialization"],
910
"tests": [
1011
{
1112
"name": "ReservedMemoryIsolate",

test/memory/run.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright 2019 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
%ProfileCreateSnapshotDataBlob();

0 commit comments

Comments
 (0)