2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// The common functionality when building with or without snapshots.
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/api.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2015-02-20 09:34:00 +00:00
|
|
|
#include "src/full-codegen.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/snapshot.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-12-10 11:46:27 +00:00
|
|
|
bool Snapshot::HaveASnapshotToStartFrom() {
|
|
|
|
return SnapshotBlob().data != NULL;
|
|
|
|
}
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2014-12-10 11:46:27 +00:00
|
|
|
|
2015-01-14 11:06:38 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) {
|
|
|
|
return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() &&
|
|
|
|
!Snapshot::ExtractContextData(snapshot_blob).is_empty();
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
|
|
|
|
bool Snapshot::EmbedsScript() {
|
|
|
|
if (!HaveASnapshotToStartFrom()) return false;
|
|
|
|
const v8::StartupData blob = SnapshotBlob();
|
|
|
|
return ExtractMetadata(&blob).embeds_script();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 09:34:00 +00:00
|
|
|
uint32_t Snapshot::SizeOfFirstPage(AllocationSpace space) {
|
|
|
|
DCHECK(space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE);
|
|
|
|
if (!HaveASnapshotToStartFrom()) {
|
|
|
|
return static_cast<uint32_t>(MemoryAllocator::PageAreaSize(space));
|
|
|
|
}
|
|
|
|
uint32_t size;
|
|
|
|
int offset = kFirstPageSizesOffset + (space - FIRST_PAGED_SPACE) * kInt32Size;
|
|
|
|
memcpy(&size, SnapshotBlob().data + offset, kInt32Size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-10 11:46:27 +00:00
|
|
|
bool Snapshot::Initialize(Isolate* isolate) {
|
|
|
|
if (!HaveASnapshotToStartFrom()) return false;
|
|
|
|
base::ElapsedTimer timer;
|
|
|
|
if (FLAG_profile_deserialization) timer.Start();
|
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
const v8::StartupData blob = SnapshotBlob();
|
2015-01-12 15:26:20 +00:00
|
|
|
Vector<const byte> startup_data = ExtractStartupData(&blob);
|
|
|
|
SnapshotData snapshot_data(startup_data);
|
2014-12-10 11:46:27 +00:00
|
|
|
Deserializer deserializer(&snapshot_data);
|
|
|
|
bool success = isolate->Init(&deserializer);
|
|
|
|
if (FLAG_profile_deserialization) {
|
|
|
|
double ms = timer.Elapsed().InMillisecondsF();
|
2015-01-12 15:26:20 +00:00
|
|
|
int bytes = startup_data.length();
|
|
|
|
PrintF("[Deserializing isolate (%d bytes) took %0.3f ms]\n", bytes, ms);
|
2014-12-10 11:46:27 +00:00
|
|
|
}
|
|
|
|
return success;
|
2012-06-19 18:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-13 08:48:00 +00:00
|
|
|
MaybeHandle<Context> Snapshot::NewContextFromSnapshot(
|
2015-01-14 16:42:15 +00:00
|
|
|
Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
|
|
|
|
Handle<FixedArray>* outdated_contexts_out) {
|
2014-12-10 11:46:27 +00:00
|
|
|
if (!HaveASnapshotToStartFrom()) return Handle<Context>();
|
2015-01-12 15:26:20 +00:00
|
|
|
base::ElapsedTimer timer;
|
|
|
|
if (FLAG_profile_deserialization) timer.Start();
|
2014-12-05 13:03:10 +00:00
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
const v8::StartupData blob = SnapshotBlob();
|
2015-01-12 15:26:20 +00:00
|
|
|
Vector<const byte> context_data = ExtractContextData(&blob);
|
|
|
|
SnapshotData snapshot_data(context_data);
|
2014-12-05 13:03:10 +00:00
|
|
|
Deserializer deserializer(&snapshot_data);
|
2015-01-13 08:48:00 +00:00
|
|
|
|
2015-01-14 16:42:15 +00:00
|
|
|
MaybeHandle<Object> maybe_context = deserializer.DeserializePartial(
|
|
|
|
isolate, global_proxy, outdated_contexts_out);
|
2015-01-13 08:48:00 +00:00
|
|
|
Handle<Object> result;
|
|
|
|
if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>();
|
|
|
|
CHECK(result->IsContext());
|
2015-01-14 11:06:38 +00:00
|
|
|
// If the snapshot does not contain a custom script, we need to update
|
|
|
|
// the global object for exactly one context.
|
|
|
|
CHECK(EmbedsScript() || (*outdated_contexts_out)->length() == 1);
|
2015-01-12 15:26:20 +00:00
|
|
|
if (FLAG_profile_deserialization) {
|
|
|
|
double ms = timer.Elapsed().InMillisecondsF();
|
|
|
|
int bytes = context_data.length();
|
|
|
|
PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms);
|
|
|
|
}
|
2015-01-13 08:48:00 +00:00
|
|
|
return Handle<Context>::cast(result);
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
|
2015-02-20 09:34:00 +00:00
|
|
|
void CalculateFirstPageSizes(bool is_default_snapshot,
|
|
|
|
const SnapshotData& startup_snapshot,
|
|
|
|
const SnapshotData& context_snapshot,
|
|
|
|
uint32_t* sizes_out) {
|
|
|
|
Vector<const SerializedData::Reservation> startup_reservations =
|
|
|
|
startup_snapshot.Reservations();
|
|
|
|
Vector<const SerializedData::Reservation> context_reservations =
|
|
|
|
context_snapshot.Reservations();
|
|
|
|
int startup_index = 0;
|
|
|
|
int context_index = 0;
|
|
|
|
for (int space = 0; space < i::Serializer::kNumberOfSpaces; space++) {
|
|
|
|
bool single_chunk = true;
|
|
|
|
while (!startup_reservations[startup_index].is_last()) {
|
|
|
|
single_chunk = false;
|
|
|
|
startup_index++;
|
|
|
|
}
|
|
|
|
while (!context_reservations[context_index].is_last()) {
|
|
|
|
single_chunk = false;
|
|
|
|
context_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t required = kMaxUInt32;
|
|
|
|
if (single_chunk) {
|
|
|
|
// If both the startup snapshot data and the context snapshot data on
|
|
|
|
// this space fit in a single page, then we consider limiting the size
|
|
|
|
// of the first page. For this, we add the chunk sizes and some extra
|
|
|
|
// allowance. This way we achieve a smaller startup memory footprint.
|
|
|
|
required = (startup_reservations[startup_index].chunk_size() +
|
|
|
|
2 * context_reservations[context_index].chunk_size()) +
|
|
|
|
Page::kObjectStartOffset;
|
|
|
|
} else {
|
|
|
|
// We expect the vanilla snapshot to only require on page per space.
|
|
|
|
DCHECK(!is_default_snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) {
|
|
|
|
uint32_t max_size =
|
|
|
|
MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space));
|
|
|
|
sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size);
|
|
|
|
} else {
|
|
|
|
DCHECK(single_chunk);
|
|
|
|
}
|
|
|
|
startup_index++;
|
|
|
|
context_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK_EQ(startup_reservations.length(), startup_index);
|
|
|
|
DCHECK_EQ(context_reservations.length(), context_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
v8::StartupData Snapshot::CreateSnapshotBlob(
|
2015-02-20 09:34:00 +00:00
|
|
|
const i::StartupSerializer& startup_ser,
|
|
|
|
const i::PartialSerializer& context_ser, Snapshot::Metadata metadata) {
|
|
|
|
SnapshotData startup_snapshot(startup_ser);
|
|
|
|
SnapshotData context_snapshot(context_ser);
|
|
|
|
Vector<const byte> startup_data = startup_snapshot.RawData();
|
|
|
|
Vector<const byte> context_data = context_snapshot.RawData();
|
|
|
|
|
|
|
|
uint32_t first_page_sizes[kNumPagedSpaces];
|
|
|
|
|
|
|
|
CalculateFirstPageSizes(metadata.embeds_script(), startup_snapshot,
|
|
|
|
context_snapshot, first_page_sizes);
|
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
int startup_length = startup_data.length();
|
|
|
|
int context_length = context_data.length();
|
2015-01-14 11:06:38 +00:00
|
|
|
int context_offset = ContextOffset(startup_length);
|
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
int length = context_offset + context_length;
|
|
|
|
char* data = new char[length];
|
|
|
|
|
2015-01-14 11:06:38 +00:00
|
|
|
memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size);
|
2015-02-20 09:34:00 +00:00
|
|
|
memcpy(data + kFirstPageSizesOffset, first_page_sizes,
|
|
|
|
kNumPagedSpaces * kInt32Size);
|
2015-01-14 11:06:38 +00:00
|
|
|
memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size);
|
|
|
|
memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length);
|
2014-12-10 14:20:12 +00:00
|
|
|
memcpy(data + context_offset, context_data.begin(), context_length);
|
|
|
|
v8::StartupData result = {data, length};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-14 11:06:38 +00:00
|
|
|
Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) {
|
|
|
|
uint32_t raw;
|
|
|
|
memcpy(&raw, data->data + kMetadataOffset, kInt32Size);
|
|
|
|
return Metadata(raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-10 14:20:12 +00:00
|
|
|
Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) {
|
|
|
|
DCHECK_LT(kIntSize, data->raw_size);
|
|
|
|
int startup_length;
|
2015-01-14 11:06:38 +00:00
|
|
|
memcpy(&startup_length, data->data + kStartupLengthOffset, kInt32Size);
|
2014-12-10 14:20:12 +00:00
|
|
|
DCHECK_LT(startup_length, data->raw_size);
|
|
|
|
const byte* startup_data =
|
2015-01-14 11:06:38 +00:00
|
|
|
reinterpret_cast<const byte*>(data->data + kStartupDataOffset);
|
2014-12-10 14:20:12 +00:00
|
|
|
return Vector<const byte>(startup_data, startup_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) {
|
|
|
|
DCHECK_LT(kIntSize, data->raw_size);
|
|
|
|
int startup_length;
|
2015-01-14 11:06:38 +00:00
|
|
|
memcpy(&startup_length, data->data + kStartupLengthOffset, kIntSize);
|
|
|
|
int context_offset = ContextOffset(startup_length);
|
2014-12-10 14:20:12 +00:00
|
|
|
const byte* context_data =
|
|
|
|
reinterpret_cast<const byte*>(data->data + context_offset);
|
|
|
|
DCHECK_LT(context_offset, data->raw_size);
|
|
|
|
int context_length = data->raw_size - context_offset;
|
|
|
|
return Vector<const byte>(context_data, context_length);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|