dart-sdk/runtime/vm/benchmark_test.cc

615 lines
19 KiB
C++
Raw Normal View History

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/benchmark_test.h"
#include "bin/builtin.h"
#include "bin/file.h"
#include "bin/isolate_data.h"
#include "bin/process.h"
#include "bin/reference_counting.h"
#include "bin/vmservice_impl.h"
#include "platform/assert.h"
#include "platform/globals.h"
#include "platform/utils.h"
#include "vm/app_snapshot.h"
#include "vm/dart_api_impl.h"
[vm] Consolidate the *WriteStream hierarchy. All *WriteStream classes are now part of the same hierarchy: class BaseWriteStream : ValueObject; Base class for all *WriteStreams. Provides all the methods from the old WriteStream except for buffer() and SetPosition() (relegated to NonStreamingWriteStreams). Has one pure virtual method Realloc that must be overridden by concrete subclasses. class NonStreamingWriteStream : BaseWriteStream; Base class for all *WriteStreams where the entire stream is available at all times (i.e., no flushing to an external sink). Extends the public BaseWriteStream API with buffer() (for accessing the stream contents) and SetPosition() (for changing the current stream pointer to the given absolute position in the stream). class MallocWriteStream : NonStreamingWriteStream; Uses realloc to reallocate the internal buffer. Almost the same as the old WriteStream, except that it only takes an initial size. Adds one public method Steal() for taking ownership of the current buffer contents (after which the buffer is reset to an empty state). Instead of passing a pointer to a buffer, the internal buffer must be accessed via either Steal() or buffer(), which allows access to the current stream contents without changing ownership or resetting the stream. The internal buffer is freed on stream destruction. class ZoneWriteStream: NonStreamingWriteStream; Takes a zone and reallocates the internal buffer in that zone. No additional public methods beyond those available from NonStreamingWriteStream. class StreamingWriteStream : BaseWriteStream; Uses realloc to reallocate the internal buffer. Generally same as before, where the contents of the stream are periodically flushed using Dart_StreamingWriteCallback. Since it extends BaseWriteStream, there are now more methods available for writing data to the stream than just Print/VPrint/WriteBytes. Since portions of the stream may be flushed and thus no longer in the internal buffer, does not provide access to the contents of the stream or a way to reposition the current stream pointer. Flushes any unflushed data and frees the internal buffer on stream destruction. Also refactor things so that write streams are passed to appropriate recipients, instead of the recipients taking arguments they only used to create a WriteStream internally. Thus, recipients now can just specify the appropriate base class for the public API used: * BaseWriteStream for just writing to the stream, or * NonStreamingWriteStream if re-positioning or the stream contents are needed. Change-Id: I419096ecd9331483d168b079fca55b69ef397f15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164080 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
2020-09-23 21:05:59 +00:00
#include "vm/datastream.h"
#include "vm/message_snapshot.h"
#include "vm/stack_frame.h"
#include "vm/timer.h"
using dart::bin::File;
namespace dart {
Benchmark* Benchmark::first_ = nullptr;
Benchmark* Benchmark::tail_ = nullptr;
const char* Benchmark::executable_ = nullptr;
void Benchmark::RunAll(const char* executable) {
SetExecutable(executable);
Benchmark* benchmark = first_;
while (benchmark != nullptr) {
benchmark->RunBenchmark();
benchmark = benchmark->next_;
}
}
//
// Measure compile of all functions in dart core lib classes.
//
BENCHMARK(CorelibCompileAll) {
bin::Builtin::SetNativeResolver(bin::Builtin::kBuiltinLibrary);
bin::Builtin::SetNativeResolver(bin::Builtin::kIOLibrary);
bin::Builtin::SetNativeResolver(bin::Builtin::kCLILibrary);
TransitionNativeToVM transition(thread);
StackZone zone(thread);
Timer timer;
timer.Start();
const Error& error =
Error::Handle(Library::CompileAll(/*ignore_error=*/true));
if (!error.IsNull()) {
OS::PrintErr("Unexpected error in CorelibCompileAll benchmark:\n%s",
error.ToErrorCString());
}
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
// This file is created by the target //runtime/bin:dart_kernel_platform_cc
// which is depended on by run_vm_tests.
static char* ComputeKernelServicePath(const char* arg) {
char buffer[2048];
char* kernel_service_path =
Utils::StrDup(File::GetCanonicalPath(nullptr, arg));
EXPECT(kernel_service_path != nullptr);
const char* compiler_path = "%s%sgen%skernel_service.dill";
const char* path_separator = File::PathSeparator();
ASSERT(path_separator != nullptr && strlen(path_separator) == 1);
char* ptr = strrchr(kernel_service_path, *path_separator);
while (ptr != nullptr) {
*ptr = '\0';
Utils::SNPrint(buffer, ARRAY_SIZE(buffer), compiler_path,
kernel_service_path, path_separator, path_separator);
if (File::Exists(nullptr, buffer)) {
break;
}
ptr = strrchr(kernel_service_path, *path_separator);
}
free(kernel_service_path);
if (ptr == nullptr) {
return nullptr;
}
return Utils::StrDup(buffer);
}
//
// Measure creation of core isolate from a snapshot.
//
BENCHMARK(CorelibIsolateStartup) {
const int kNumIterations = 1000;
Timer timer;
Isolate* isolate = thread->isolate();
Dart_ExitIsolate();
for (int i = 0; i < kNumIterations; i++) {
timer.Start();
TestCase::CreateTestIsolate();
timer.Stop();
Dart_ShutdownIsolate();
}
benchmark->set_score(timer.TotalElapsedTime() / kNumIterations);
Dart_EnterIsolate(reinterpret_cast<Dart_Isolate>(isolate));
}
//
// Measure invocation of Dart API functions.
//
static void InitNativeFields(Dart_NativeArguments args) {
int count = Dart_GetNativeArgumentCount(args);
EXPECT_EQ(1, count);
Dart_Handle recv = Dart_GetNativeArgument(args, 0);
EXPECT_VALID(recv);
Dart_Handle result = Dart_SetNativeInstanceField(recv, 0, 7);
EXPECT_VALID(result);
}
// The specific api functions called here are a bit arbitrary. We are
// trying to get a sense of the overhead for using the dart api.
static void UseDartApi(Dart_NativeArguments args) {
int count = Dart_GetNativeArgumentCount(args);
EXPECT_EQ(3, count);
// Get native field from receiver.
intptr_t receiver_value;
Dart_Handle result = Dart_GetNativeReceiver(args, &receiver_value);
EXPECT_VALID(result);
EXPECT_EQ(7, receiver_value);
// Get param1.
Dart_Handle param1 = Dart_GetNativeArgument(args, 1);
EXPECT_VALID(param1);
EXPECT(Dart_IsInteger(param1));
bool fits = false;
result = Dart_IntegerFitsIntoInt64(param1, &fits);
EXPECT_VALID(result);
EXPECT(fits);
int64_t value1;
result = Dart_IntegerToInt64(param1, &value1);
EXPECT_VALID(result);
EXPECT_LE(0, value1);
EXPECT_LE(value1, 1000000);
// Return param + receiver.field.
Dart_SetReturnValue(args, Dart_NewInteger(value1 * receiver_value));
}
static Dart_NativeFunction bm_uda_lookup(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
ASSERT(auto_setup_scope != nullptr);
*auto_setup_scope = true;
const char* cstr = nullptr;
Dart_Handle result = Dart_StringToCString(name, &cstr);
EXPECT_VALID(result);
if (strcmp(cstr, "init") == 0) {
return InitNativeFields;
} else {
return UseDartApi;
}
}
BENCHMARK(UseDartApi) {
const int kNumIterations = 1000000;
const char* kScriptChars = R"(
import 'dart:nativewrappers';
base class Class extends NativeFieldWrapperClass1 {
@pragma("vm:external-name", "init")
external void init();
@pragma("vm:external-name", "method")
external int method(int param1, int param2);
}
void benchmark(int count) {
Class c = Class();
c.init();
for (int i = 0; i < count; i++) {
c.method(i,7);
}
})";
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, bm_uda_lookup,
RESOLVED_USER_TEST_URI, false);
Dart_Handle result = Dart_FinalizeLoading(false);
EXPECT_VALID(result);
Dart_Handle args[1];
args[0] = Dart_NewInteger(kNumIterations);
Represent tokens as a compressed stream instead of an array. On x86: Original sizes: Size of isolate snapshot = 925655 New space (0k of 32768k) Old space (1700k of 1792k) Code space (0k of 0k) out/ReleaseIA32/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 80944 CorelibIsolateStartup(RunTime): 5636 UseDartApi(RunTime): 404620 Dart2JSCompileAll(RunTime): 1379683 FrameLookup(RunTime): 45 Size with the new TokenStream implementation: Size of isolate snapshot = 851352 New space (0k of 32768k) Old space (1368k of 1536k) Code space (0k of 0k) out/ReleaseIA32/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 76918 CorelibIsolateStartup(RunTime): 4056 UseDartApi(RunTime): 502111 Dart2JSCompileAll(RunTime): 1251646 FrameLookup(RunTime): 45 (saves about 332k out of the original TokenStream size of 410k) On x64: Original sizes: Size of isolate snapshot = 924865 New space (0k of 32768k) Old space (3234k of 3444k) Code space (0k of 0k) out/ReleaseX64/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 109660 CorelibIsolateStartup(RunTime): 3985 UseDartApi(RunTime): 463009 Dart2JSCompileAll(RunTime): 2170309 FrameLookup(RunTime): 35 Size with the new TokenStream implementation: Size of isolate snapshot = 849675 New space (0k of 32768k) Old space (2436k of 2560k) Code space (0k of 0k) out/ReleaseX64/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 98716 CorelibIsolateStartup(RunTime): 2938 UseDartApi(RunTime): 509149 Dart2JSCompileAll(RunTime): 2042273 FrameLookup(RunTime): 39 (saves about 798k out of the original TokenStream size of 810k) Token stats: Number of tokens: 101470 Literal tokens: 4550 Ident tokens: 33020 Review URL: https://chromiumcodereview.appspot.com//10697055 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@9539 260f80e4-7a28-3924-810f-c04153c831b5
2012-07-11 00:11:58 +00:00
// Warmup first to avoid compilation jitters.
result = Dart_Invoke(lib, NewString("benchmark"), 1, args);
EXPECT_VALID(result);
Represent tokens as a compressed stream instead of an array. On x86: Original sizes: Size of isolate snapshot = 925655 New space (0k of 32768k) Old space (1700k of 1792k) Code space (0k of 0k) out/ReleaseIA32/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 80944 CorelibIsolateStartup(RunTime): 5636 UseDartApi(RunTime): 404620 Dart2JSCompileAll(RunTime): 1379683 FrameLookup(RunTime): 45 Size with the new TokenStream implementation: Size of isolate snapshot = 851352 New space (0k of 32768k) Old space (1368k of 1536k) Code space (0k of 0k) out/ReleaseIA32/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 76918 CorelibIsolateStartup(RunTime): 4056 UseDartApi(RunTime): 502111 Dart2JSCompileAll(RunTime): 1251646 FrameLookup(RunTime): 45 (saves about 332k out of the original TokenStream size of 410k) On x64: Original sizes: Size of isolate snapshot = 924865 New space (0k of 32768k) Old space (3234k of 3444k) Code space (0k of 0k) out/ReleaseX64/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 109660 CorelibIsolateStartup(RunTime): 3985 UseDartApi(RunTime): 463009 Dart2JSCompileAll(RunTime): 2170309 FrameLookup(RunTime): 35 Size with the new TokenStream implementation: Size of isolate snapshot = 849675 New space (0k of 32768k) Old space (2436k of 2560k) Code space (0k of 0k) out/ReleaseX64/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 98716 CorelibIsolateStartup(RunTime): 2938 UseDartApi(RunTime): 509149 Dart2JSCompileAll(RunTime): 2042273 FrameLookup(RunTime): 39 (saves about 798k out of the original TokenStream size of 810k) Token stats: Number of tokens: 101470 Literal tokens: 4550 Ident tokens: 33020 Review URL: https://chromiumcodereview.appspot.com//10697055 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@9539 260f80e4-7a28-3924-810f-c04153c831b5
2012-07-11 00:11:58 +00:00
Timer timer;
Represent tokens as a compressed stream instead of an array. On x86: Original sizes: Size of isolate snapshot = 925655 New space (0k of 32768k) Old space (1700k of 1792k) Code space (0k of 0k) out/ReleaseIA32/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 80944 CorelibIsolateStartup(RunTime): 5636 UseDartApi(RunTime): 404620 Dart2JSCompileAll(RunTime): 1379683 FrameLookup(RunTime): 45 Size with the new TokenStream implementation: Size of isolate snapshot = 851352 New space (0k of 32768k) Old space (1368k of 1536k) Code space (0k of 0k) out/ReleaseIA32/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 76918 CorelibIsolateStartup(RunTime): 4056 UseDartApi(RunTime): 502111 Dart2JSCompileAll(RunTime): 1251646 FrameLookup(RunTime): 45 (saves about 332k out of the original TokenStream size of 410k) On x64: Original sizes: Size of isolate snapshot = 924865 New space (0k of 32768k) Old space (3234k of 3444k) Code space (0k of 0k) out/ReleaseX64/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 109660 CorelibIsolateStartup(RunTime): 3985 UseDartApi(RunTime): 463009 Dart2JSCompileAll(RunTime): 2170309 FrameLookup(RunTime): 35 Size with the new TokenStream implementation: Size of isolate snapshot = 849675 New space (0k of 32768k) Old space (2436k of 2560k) Code space (0k of 0k) out/ReleaseX64/run_vm_tests --benchmarks CorelibCompileAll(RunTime): 98716 CorelibIsolateStartup(RunTime): 2938 UseDartApi(RunTime): 509149 Dart2JSCompileAll(RunTime): 2042273 FrameLookup(RunTime): 39 (saves about 798k out of the original TokenStream size of 810k) Token stats: Number of tokens: 101470 Literal tokens: 4550 Ident tokens: 33020 Review URL: https://chromiumcodereview.appspot.com//10697055 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@9539 260f80e4-7a28-3924-810f-c04153c831b5
2012-07-11 00:11:58 +00:00
timer.Start();
result = Dart_Invoke(lib, NewString("benchmark"), 1, args);
EXPECT_VALID(result);
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
[vm] Native API: Make Dart_NewWeakPersistentHandle not auto delete Changes Dart_NewWeakPersistentHandle to no longer auto delete the weak persistent handle. Changes the signatures of WeakPersistentHandleFinalizers to no longer have access to the handle. Flutter PR: https://github.com/flutter/engine/pull/19843 g3 presubmit: cl/318028238 Issue: https://github.com/dart-lang/sdk/issues/42312 TEST=runtime/vm/dart_api_impl_test.cc Change-Id: I3f77db9954d9486759f903b78c03a494f73c68ba Cq-Include-Trybots:dart/try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-mac-debug-x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-nnbd-linux-debug-x64-try,analyzer-nnbd-linux-release-try,front-end-nnbd-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151525 Commit-Queue: Daco Harkes <dacoharkes@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
2020-11-03 10:27:44 +00:00
static void NoopFinalizer(void* isolate_callback_data, void* peer) {}
//
// Measure time accessing internal and external strings.
//
BENCHMARK(DartStringAccess) {
const int kNumIterations = 10000000;
Timer timer;
timer.Start();
Dart_EnterScope();
// Create strings.
uint8_t data8[] = {'o', 'n', 'e', 0xFF};
int external_peer_data = 123;
intptr_t char_size;
intptr_t str_len;
Dart_Handle external_string = Dart_NewExternalLatin1String(
data8, ARRAY_SIZE(data8), &external_peer_data, sizeof(data8),
NoopFinalizer);
Dart_Handle internal_string = NewString("two");
// Run benchmark.
for (int64_t i = 0; i < kNumIterations; i++) {
EXPECT(Dart_IsString(internal_string));
EXPECT(!Dart_IsExternalString(internal_string));
EXPECT_VALID(external_string);
EXPECT(Dart_IsExternalString(external_string));
void* external_peer = nullptr;
EXPECT_VALID(Dart_StringGetProperties(external_string, &char_size, &str_len,
&external_peer));
EXPECT_EQ(1, char_size);
EXPECT_EQ(4, str_len);
EXPECT_EQ(&external_peer_data, external_peer);
}
Dart_ExitScope();
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
static void vmservice_resolver(Dart_NativeArguments args) {}
static Dart_NativeFunction NativeResolver(Dart_Handle name,
int arg_count,
bool* auto_setup_scope) {
ASSERT(auto_setup_scope != nullptr);
*auto_setup_scope = false;
return &vmservice_resolver;
}
//
// Measure compile of all kernel Service(CFE) functions.
//
BENCHMARK(KernelServiceCompileAll) {
// kernel_service.dill is built with sound null safety.
if (!FLAG_sound_null_safety) {
return;
}
bin::Builtin::SetNativeResolver(bin::Builtin::kBuiltinLibrary);
bin::Builtin::SetNativeResolver(bin::Builtin::kIOLibrary);
bin::Builtin::SetNativeResolver(bin::Builtin::kCLILibrary);
char* dill_path = ComputeKernelServicePath(Benchmark::Executable());
File* file = File::Open(nullptr, dill_path, File::kRead);
EXPECT(file != nullptr);
bin::RefCntReleaseScope<File> rs(file);
intptr_t kernel_buffer_size = file->Length();
uint8_t* kernel_buffer =
reinterpret_cast<uint8_t*>(malloc(kernel_buffer_size));
bool read_fully = file->ReadFully(kernel_buffer, kernel_buffer_size);
EXPECT(read_fully);
Dart_Handle result =
Dart_LoadScriptFromKernel(kernel_buffer, kernel_buffer_size);
EXPECT_VALID(result);
Dart_Handle service_lib = Dart_LookupLibrary(NewString("dart:vmservice_io"));
ASSERT(!Dart_IsError(service_lib));
Dart_SetNativeResolver(service_lib, NativeResolver, nullptr);
result = Dart_FinalizeLoading(false);
EXPECT_VALID(result);
Timer timer;
timer.Start();
#if !defined(PRODUCT)
const bool old_flag = FLAG_background_compilation;
FLAG_background_compilation = false;
#endif
result = Dart_CompileAll();
#if !defined(PRODUCT)
FLAG_background_compilation = old_flag;
#endif
EXPECT_VALID(result);
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
free(dill_path);
free(kernel_buffer);
}
//
// Measure frame lookup during stack traversal.
//
static void StackFrame_accessFrame(Dart_NativeArguments args) {
Timer timer;
timer.Start();
{
Thread* thread = Thread::Current();
TransitionNativeToVM transition(thread);
const int kNumIterations = 100;
Code& code = Code::Handle(thread->zone());
for (int i = 0; i < kNumIterations; i++) {
StackFrameIterator frames(ValidationPolicy::kDontValidateFrames, thread,
StackFrameIterator::kNoCrossThreadIteration);
StackFrame* frame = frames.NextFrame();
while (frame != nullptr) {
if (frame->IsStubFrame()) {
code = frame->LookupDartCode();
EXPECT(code.function() == Function::null());
} else if (frame->IsDartFrame()) {
code = frame->LookupDartCode();
EXPECT(code.function() != Function::null());
}
frame = frames.NextFrame();
}
}
}
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
Dart_SetReturnValue(args, Dart_NewInteger(elapsed_time));
}
static Dart_NativeFunction StackFrameNativeResolver(Dart_Handle name,
int arg_count,
bool* auto_setup_scope) {
ASSERT(auto_setup_scope != nullptr);
*auto_setup_scope = false;
return &StackFrame_accessFrame;
}
// Unit test case to verify stack frame iteration.
BENCHMARK(FrameLookup) {
const char* kScriptChars =
R"(
class StackFrame {
@pragma('vm:external-name', 'StackFrame_accessFrame')
external static int accessFrame();
}
class First {
First() { }
int method1(int param) {
if (param == 1) {
param = method2(200);
} else {
param = method2(100);
}
return param;
}
int method2(int param) {
if (param == 200) {
return First.staticmethod(this, param);
} else {
return First.staticmethod(this, 10);
}
}
static int staticmethod(First obj, int param) {
if (param == 10) {
return obj.method3(10);
} else {
return obj.method3(200);
}
}
int method3(int param) {
return StackFrame.accessFrame();
}
}
class StackFrameTest {
static int testMain() {
First obj = new First();
return obj.method1(1);
}
}
)";
Dart_Handle lib =
TestCase::LoadTestScript(kScriptChars, StackFrameNativeResolver);
Dart_Handle cls = Dart_GetClass(lib, NewString("StackFrameTest"));
Dart_Handle result = Dart_Invoke(cls, NewString("testMain"), 0, nullptr);
EXPECT_VALID(result);
int64_t elapsed_time = 0;
result = Dart_IntegerToInt64(result, &elapsed_time);
EXPECT_VALID(result);
benchmark->set_score(elapsed_time);
}
BENCHMARK_SIZE(CoreSnapshotSize) {
const char* kScriptChars =
"import 'dart:async';\n"
"import 'dart:core';\n"
"import 'dart:collection';\n"
"import 'dart:_internal';\n"
"import 'dart:math';\n"
"import 'dart:isolate';\n"
"import 'dart:mirrors';\n"
"import 'dart:typed_data';\n"
"\n";
// Start an Isolate, load a script and create a full snapshot.
// Need to load the script into the dart: core library due to
// the import of dart:_internal.
TestCase::LoadCoreTestScript(kScriptChars, nullptr);
TransitionNativeToVM transition(thread);
StackZone zone(thread);
Api::CheckAndFinalizePendingClasses(thread);
// Write snapshot with object content.
[vm] Consolidate the *WriteStream hierarchy. All *WriteStream classes are now part of the same hierarchy: class BaseWriteStream : ValueObject; Base class for all *WriteStreams. Provides all the methods from the old WriteStream except for buffer() and SetPosition() (relegated to NonStreamingWriteStreams). Has one pure virtual method Realloc that must be overridden by concrete subclasses. class NonStreamingWriteStream : BaseWriteStream; Base class for all *WriteStreams where the entire stream is available at all times (i.e., no flushing to an external sink). Extends the public BaseWriteStream API with buffer() (for accessing the stream contents) and SetPosition() (for changing the current stream pointer to the given absolute position in the stream). class MallocWriteStream : NonStreamingWriteStream; Uses realloc to reallocate the internal buffer. Almost the same as the old WriteStream, except that it only takes an initial size. Adds one public method Steal() for taking ownership of the current buffer contents (after which the buffer is reset to an empty state). Instead of passing a pointer to a buffer, the internal buffer must be accessed via either Steal() or buffer(), which allows access to the current stream contents without changing ownership or resetting the stream. The internal buffer is freed on stream destruction. class ZoneWriteStream: NonStreamingWriteStream; Takes a zone and reallocates the internal buffer in that zone. No additional public methods beyond those available from NonStreamingWriteStream. class StreamingWriteStream : BaseWriteStream; Uses realloc to reallocate the internal buffer. Generally same as before, where the contents of the stream are periodically flushed using Dart_StreamingWriteCallback. Since it extends BaseWriteStream, there are now more methods available for writing data to the stream than just Print/VPrint/WriteBytes. Since portions of the stream may be flushed and thus no longer in the internal buffer, does not provide access to the contents of the stream or a way to reposition the current stream pointer. Flushes any unflushed data and frees the internal buffer on stream destruction. Also refactor things so that write streams are passed to appropriate recipients, instead of the recipients taking arguments they only used to create a WriteStream internally. Thus, recipients now can just specify the appropriate base class for the public API used: * BaseWriteStream for just writing to the stream, or * NonStreamingWriteStream if re-positioning or the stream contents are needed. Change-Id: I419096ecd9331483d168b079fca55b69ef397f15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164080 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
2020-09-23 21:05:59 +00:00
MallocWriteStream vm_snapshot_data(FullSnapshotWriter::kInitialSize);
MallocWriteStream isolate_snapshot_data(FullSnapshotWriter::kInitialSize);
FullSnapshotWriter writer(
Snapshot::kFullCore, &vm_snapshot_data, &isolate_snapshot_data,
[vm] Consolidate the *WriteStream hierarchy. All *WriteStream classes are now part of the same hierarchy: class BaseWriteStream : ValueObject; Base class for all *WriteStreams. Provides all the methods from the old WriteStream except for buffer() and SetPosition() (relegated to NonStreamingWriteStreams). Has one pure virtual method Realloc that must be overridden by concrete subclasses. class NonStreamingWriteStream : BaseWriteStream; Base class for all *WriteStreams where the entire stream is available at all times (i.e., no flushing to an external sink). Extends the public BaseWriteStream API with buffer() (for accessing the stream contents) and SetPosition() (for changing the current stream pointer to the given absolute position in the stream). class MallocWriteStream : NonStreamingWriteStream; Uses realloc to reallocate the internal buffer. Almost the same as the old WriteStream, except that it only takes an initial size. Adds one public method Steal() for taking ownership of the current buffer contents (after which the buffer is reset to an empty state). Instead of passing a pointer to a buffer, the internal buffer must be accessed via either Steal() or buffer(), which allows access to the current stream contents without changing ownership or resetting the stream. The internal buffer is freed on stream destruction. class ZoneWriteStream: NonStreamingWriteStream; Takes a zone and reallocates the internal buffer in that zone. No additional public methods beyond those available from NonStreamingWriteStream. class StreamingWriteStream : BaseWriteStream; Uses realloc to reallocate the internal buffer. Generally same as before, where the contents of the stream are periodically flushed using Dart_StreamingWriteCallback. Since it extends BaseWriteStream, there are now more methods available for writing data to the stream than just Print/VPrint/WriteBytes. Since portions of the stream may be flushed and thus no longer in the internal buffer, does not provide access to the contents of the stream or a way to reposition the current stream pointer. Flushes any unflushed data and frees the internal buffer on stream destruction. Also refactor things so that write streams are passed to appropriate recipients, instead of the recipients taking arguments they only used to create a WriteStream internally. Thus, recipients now can just specify the appropriate base class for the public API used: * BaseWriteStream for just writing to the stream, or * NonStreamingWriteStream if re-positioning or the stream contents are needed. Change-Id: I419096ecd9331483d168b079fca55b69ef397f15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164080 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
2020-09-23 21:05:59 +00:00
/*vm_image_writer=*/nullptr, /*iso_image_writer=*/nullptr);
writer.WriteFullSnapshot();
const Snapshot* snapshot =
[vm] Consolidate the *WriteStream hierarchy. All *WriteStream classes are now part of the same hierarchy: class BaseWriteStream : ValueObject; Base class for all *WriteStreams. Provides all the methods from the old WriteStream except for buffer() and SetPosition() (relegated to NonStreamingWriteStreams). Has one pure virtual method Realloc that must be overridden by concrete subclasses. class NonStreamingWriteStream : BaseWriteStream; Base class for all *WriteStreams where the entire stream is available at all times (i.e., no flushing to an external sink). Extends the public BaseWriteStream API with buffer() (for accessing the stream contents) and SetPosition() (for changing the current stream pointer to the given absolute position in the stream). class MallocWriteStream : NonStreamingWriteStream; Uses realloc to reallocate the internal buffer. Almost the same as the old WriteStream, except that it only takes an initial size. Adds one public method Steal() for taking ownership of the current buffer contents (after which the buffer is reset to an empty state). Instead of passing a pointer to a buffer, the internal buffer must be accessed via either Steal() or buffer(), which allows access to the current stream contents without changing ownership or resetting the stream. The internal buffer is freed on stream destruction. class ZoneWriteStream: NonStreamingWriteStream; Takes a zone and reallocates the internal buffer in that zone. No additional public methods beyond those available from NonStreamingWriteStream. class StreamingWriteStream : BaseWriteStream; Uses realloc to reallocate the internal buffer. Generally same as before, where the contents of the stream are periodically flushed using Dart_StreamingWriteCallback. Since it extends BaseWriteStream, there are now more methods available for writing data to the stream than just Print/VPrint/WriteBytes. Since portions of the stream may be flushed and thus no longer in the internal buffer, does not provide access to the contents of the stream or a way to reposition the current stream pointer. Flushes any unflushed data and frees the internal buffer on stream destruction. Also refactor things so that write streams are passed to appropriate recipients, instead of the recipients taking arguments they only used to create a WriteStream internally. Thus, recipients now can just specify the appropriate base class for the public API used: * BaseWriteStream for just writing to the stream, or * NonStreamingWriteStream if re-positioning or the stream contents are needed. Change-Id: I419096ecd9331483d168b079fca55b69ef397f15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164080 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
2020-09-23 21:05:59 +00:00
Snapshot::SetupFromBuffer(isolate_snapshot_data.buffer());
ASSERT(snapshot->kind() == Snapshot::kFullCore);
benchmark->set_score(snapshot->length());
}
BENCHMARK_SIZE(StandaloneSnapshotSize) {
const char* kScriptChars =
"import 'dart:async';\n"
"import 'dart:core';\n"
"import 'dart:collection';\n"
"import 'dart:convert';\n"
"import 'dart:math';\n"
"import 'dart:isolate';\n"
"import 'dart:mirrors';\n"
"import 'dart:typed_data';\n"
"import 'dart:io';\n"
"import 'dart:cli';\n"
"\n";
// Start an Isolate, load a script and create a full snapshot.
// Need to load the script into the dart: core library due to
// the import of dart:_internal.
TestCase::LoadCoreTestScript(kScriptChars, nullptr);
TransitionNativeToVM transition(thread);
StackZone zone(thread);
Api::CheckAndFinalizePendingClasses(thread);
// Write snapshot with object content.
[vm] Consolidate the *WriteStream hierarchy. All *WriteStream classes are now part of the same hierarchy: class BaseWriteStream : ValueObject; Base class for all *WriteStreams. Provides all the methods from the old WriteStream except for buffer() and SetPosition() (relegated to NonStreamingWriteStreams). Has one pure virtual method Realloc that must be overridden by concrete subclasses. class NonStreamingWriteStream : BaseWriteStream; Base class for all *WriteStreams where the entire stream is available at all times (i.e., no flushing to an external sink). Extends the public BaseWriteStream API with buffer() (for accessing the stream contents) and SetPosition() (for changing the current stream pointer to the given absolute position in the stream). class MallocWriteStream : NonStreamingWriteStream; Uses realloc to reallocate the internal buffer. Almost the same as the old WriteStream, except that it only takes an initial size. Adds one public method Steal() for taking ownership of the current buffer contents (after which the buffer is reset to an empty state). Instead of passing a pointer to a buffer, the internal buffer must be accessed via either Steal() or buffer(), which allows access to the current stream contents without changing ownership or resetting the stream. The internal buffer is freed on stream destruction. class ZoneWriteStream: NonStreamingWriteStream; Takes a zone and reallocates the internal buffer in that zone. No additional public methods beyond those available from NonStreamingWriteStream. class StreamingWriteStream : BaseWriteStream; Uses realloc to reallocate the internal buffer. Generally same as before, where the contents of the stream are periodically flushed using Dart_StreamingWriteCallback. Since it extends BaseWriteStream, there are now more methods available for writing data to the stream than just Print/VPrint/WriteBytes. Since portions of the stream may be flushed and thus no longer in the internal buffer, does not provide access to the contents of the stream or a way to reposition the current stream pointer. Flushes any unflushed data and frees the internal buffer on stream destruction. Also refactor things so that write streams are passed to appropriate recipients, instead of the recipients taking arguments they only used to create a WriteStream internally. Thus, recipients now can just specify the appropriate base class for the public API used: * BaseWriteStream for just writing to the stream, or * NonStreamingWriteStream if re-positioning or the stream contents are needed. Change-Id: I419096ecd9331483d168b079fca55b69ef397f15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164080 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
2020-09-23 21:05:59 +00:00
MallocWriteStream vm_snapshot_data(FullSnapshotWriter::kInitialSize);
MallocWriteStream isolate_snapshot_data(FullSnapshotWriter::kInitialSize);
FullSnapshotWriter writer(
Snapshot::kFullCore, &vm_snapshot_data, &isolate_snapshot_data,
[vm] Consolidate the *WriteStream hierarchy. All *WriteStream classes are now part of the same hierarchy: class BaseWriteStream : ValueObject; Base class for all *WriteStreams. Provides all the methods from the old WriteStream except for buffer() and SetPosition() (relegated to NonStreamingWriteStreams). Has one pure virtual method Realloc that must be overridden by concrete subclasses. class NonStreamingWriteStream : BaseWriteStream; Base class for all *WriteStreams where the entire stream is available at all times (i.e., no flushing to an external sink). Extends the public BaseWriteStream API with buffer() (for accessing the stream contents) and SetPosition() (for changing the current stream pointer to the given absolute position in the stream). class MallocWriteStream : NonStreamingWriteStream; Uses realloc to reallocate the internal buffer. Almost the same as the old WriteStream, except that it only takes an initial size. Adds one public method Steal() for taking ownership of the current buffer contents (after which the buffer is reset to an empty state). Instead of passing a pointer to a buffer, the internal buffer must be accessed via either Steal() or buffer(), which allows access to the current stream contents without changing ownership or resetting the stream. The internal buffer is freed on stream destruction. class ZoneWriteStream: NonStreamingWriteStream; Takes a zone and reallocates the internal buffer in that zone. No additional public methods beyond those available from NonStreamingWriteStream. class StreamingWriteStream : BaseWriteStream; Uses realloc to reallocate the internal buffer. Generally same as before, where the contents of the stream are periodically flushed using Dart_StreamingWriteCallback. Since it extends BaseWriteStream, there are now more methods available for writing data to the stream than just Print/VPrint/WriteBytes. Since portions of the stream may be flushed and thus no longer in the internal buffer, does not provide access to the contents of the stream or a way to reposition the current stream pointer. Flushes any unflushed data and frees the internal buffer on stream destruction. Also refactor things so that write streams are passed to appropriate recipients, instead of the recipients taking arguments they only used to create a WriteStream internally. Thus, recipients now can just specify the appropriate base class for the public API used: * BaseWriteStream for just writing to the stream, or * NonStreamingWriteStream if re-positioning or the stream contents are needed. Change-Id: I419096ecd9331483d168b079fca55b69ef397f15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164080 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
2020-09-23 21:05:59 +00:00
/*vm_image_writer=*/nullptr, /*iso_image_writer=*/nullptr);
writer.WriteFullSnapshot();
const Snapshot* snapshot =
[vm] Consolidate the *WriteStream hierarchy. All *WriteStream classes are now part of the same hierarchy: class BaseWriteStream : ValueObject; Base class for all *WriteStreams. Provides all the methods from the old WriteStream except for buffer() and SetPosition() (relegated to NonStreamingWriteStreams). Has one pure virtual method Realloc that must be overridden by concrete subclasses. class NonStreamingWriteStream : BaseWriteStream; Base class for all *WriteStreams where the entire stream is available at all times (i.e., no flushing to an external sink). Extends the public BaseWriteStream API with buffer() (for accessing the stream contents) and SetPosition() (for changing the current stream pointer to the given absolute position in the stream). class MallocWriteStream : NonStreamingWriteStream; Uses realloc to reallocate the internal buffer. Almost the same as the old WriteStream, except that it only takes an initial size. Adds one public method Steal() for taking ownership of the current buffer contents (after which the buffer is reset to an empty state). Instead of passing a pointer to a buffer, the internal buffer must be accessed via either Steal() or buffer(), which allows access to the current stream contents without changing ownership or resetting the stream. The internal buffer is freed on stream destruction. class ZoneWriteStream: NonStreamingWriteStream; Takes a zone and reallocates the internal buffer in that zone. No additional public methods beyond those available from NonStreamingWriteStream. class StreamingWriteStream : BaseWriteStream; Uses realloc to reallocate the internal buffer. Generally same as before, where the contents of the stream are periodically flushed using Dart_StreamingWriteCallback. Since it extends BaseWriteStream, there are now more methods available for writing data to the stream than just Print/VPrint/WriteBytes. Since portions of the stream may be flushed and thus no longer in the internal buffer, does not provide access to the contents of the stream or a way to reposition the current stream pointer. Flushes any unflushed data and frees the internal buffer on stream destruction. Also refactor things so that write streams are passed to appropriate recipients, instead of the recipients taking arguments they only used to create a WriteStream internally. Thus, recipients now can just specify the appropriate base class for the public API used: * BaseWriteStream for just writing to the stream, or * NonStreamingWriteStream if re-positioning or the stream contents are needed. Change-Id: I419096ecd9331483d168b079fca55b69ef397f15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164080 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
2020-09-23 21:05:59 +00:00
Snapshot::SetupFromBuffer(isolate_snapshot_data.buffer());
ASSERT(snapshot->kind() == Snapshot::kFullCore);
benchmark->set_score(snapshot->length());
}
BENCHMARK(CreateMirrorSystem) {
const char* kScriptChars =
"import 'dart:mirrors';\n"
"\n"
"void benchmark() {\n"
" currentMirrorSystem();\n"
"}\n";
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, nullptr);
Timer timer;
timer.Start();
Dart_Handle result = Dart_Invoke(lib, NewString("benchmark"), 0, nullptr);
EXPECT_VALID(result);
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
BENCHMARK(EnterExitIsolate) {
const char* kScriptChars =
"import 'dart:core';\n"
"\n";
const intptr_t kLoopCount = 1000000;
TestCase::LoadTestScript(kScriptChars, nullptr);
{
TransitionNativeToVM transition(thread);
StackZone zone(thread);
Api::CheckAndFinalizePendingClasses(thread);
}
Dart_Isolate isolate = Dart_CurrentIsolate();
Timer timer;
timer.Start();
for (intptr_t i = 0; i < kLoopCount; i++) {
Dart_ExitIsolate();
Dart_EnterIsolate(isolate);
}
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
BENCHMARK(SerializeNull) {
TransitionNativeToVM transition(thread);
StackZone zone(thread);
const Object& null_object = Object::Handle();
const intptr_t kLoopCount = 1000000;
Timer timer;
timer.Start();
for (intptr_t i = 0; i < kLoopCount; i++) {
StackZone zone(thread);
std::unique_ptr<Message> message =
WriteMessage(/* same_group */ false, null_object, ILLEGAL_PORT,
Message::kNormalPriority);
// Read object back from the snapshot.
ReadMessage(thread, message.get());
}
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
BENCHMARK(SerializeSmi) {
TransitionNativeToVM transition(thread);
StackZone zone(thread);
const Integer& smi_object = Integer::Handle(Smi::New(42));
const intptr_t kLoopCount = 1000000;
Timer timer;
timer.Start();
for (intptr_t i = 0; i < kLoopCount; i++) {
StackZone zone(thread);
std::unique_ptr<Message> message =
WriteMessage(/* same_group */ false, smi_object, ILLEGAL_PORT,
Message::kNormalPriority);
// Read object back from the snapshot.
ReadMessage(thread, message.get());
}
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
BENCHMARK(SimpleMessage) {
TransitionNativeToVM transition(thread);
StackZone zone(thread);
const Array& array_object = Array::Handle(Array::New(2));
array_object.SetAt(0, Integer::Handle(Smi::New(42)));
array_object.SetAt(1, Object::Handle());
const intptr_t kLoopCount = 1000000;
Timer timer;
timer.Start();
for (intptr_t i = 0; i < kLoopCount; i++) {
StackZone zone(thread);
std::unique_ptr<Message> message =
WriteMessage(/* same_group */ false, array_object, ILLEGAL_PORT,
Message::kNormalPriority);
// Read object back from the snapshot.
ReadMessage(thread, message.get());
}
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
BENCHMARK(LargeMap) {
const char* kScript =
"makeMap() {\n"
" Map m = {};\n"
" for (int i = 0; i < 100000; ++i) m[i*13+i*(i>>7)] = i;\n"
" return m;\n"
"}";
Dart_Handle h_lib = TestCase::LoadTestScript(kScript, nullptr);
EXPECT_VALID(h_lib);
Dart_Handle h_result = Dart_Invoke(h_lib, NewString("makeMap"), 0, nullptr);
EXPECT_VALID(h_result);
TransitionNativeToVM transition(thread);
StackZone zone(thread);
Instance& map = Instance::Handle();
map ^= Api::UnwrapHandle(h_result);
const intptr_t kLoopCount = 100;
Timer timer;
timer.Start();
for (intptr_t i = 0; i < kLoopCount; i++) {
StackZone zone(thread);
std::unique_ptr<Message> message = WriteMessage(
/* same_group */ false, map, ILLEGAL_PORT, Message::kNormalPriority);
// Read object back from the snapshot.
ReadMessage(thread, message.get());
}
timer.Stop();
int64_t elapsed_time = timer.TotalElapsedTime();
benchmark->set_score(elapsed_time);
}
BENCHMARK_MEMORY(InitialRSS) {
benchmark->set_score(bin::Process::MaxRSS());
}
} // namespace dart