OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
8 #include "src/api.h" | 8 #include "src/api.h" |
9 #include "src/base/platform/platform.h" | 9 #include "src/base/platform/platform.h" |
10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
628 void Deserializer::FlushICacheForNewCodeObjects() { | 628 void Deserializer::FlushICacheForNewCodeObjects() { |
629 PageIterator it(isolate_->heap()->code_space()); | 629 PageIterator it(isolate_->heap()->code_space()); |
630 while (it.has_next()) { | 630 while (it.has_next()) { |
631 Page* p = it.next(); | 631 Page* p = it.next(); |
632 CpuFeatures::FlushICache(p->area_start(), p->area_end() - p->area_start()); | 632 CpuFeatures::FlushICache(p->area_start(), p->area_end() - p->area_start()); |
633 } | 633 } |
634 } | 634 } |
635 | 635 |
636 | 636 |
637 bool Deserializer::ReserveSpace() { | 637 bool Deserializer::ReserveSpace() { |
638 for (int i = NEW_SPACE; i < kNumberOfSpaces; i++) { | |
639 DCHECK(reservations_[i].length() > 0); | |
jochen (gone - plz use gerrit)
2015/01/12 19:57:10
wrap the entire loop in #if DEBUG?
Yang
2015/01/13 08:20:11
Done.
| |
640 } | |
638 if (!isolate_->heap()->ReserveSpace(reservations_)) return false; | 641 if (!isolate_->heap()->ReserveSpace(reservations_)) return false; |
639 for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) { | 642 for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) { |
640 high_water_[i] = reservations_[i][0].start; | 643 high_water_[i] = reservations_[i][0].start; |
641 } | 644 } |
642 return true; | 645 return true; |
643 } | 646 } |
644 | 647 |
645 | 648 |
649 void Deserializer::Initialize(Isolate* isolate) { | |
650 DCHECK_EQ(NULL, isolate_); | |
651 DCHECK_NE(NULL, isolate); | |
652 isolate_ = isolate; | |
653 DCHECK_EQ(NULL, external_reference_decoder_); | |
654 external_reference_decoder_ = new ExternalReferenceDecoder(isolate); | |
655 } | |
656 | |
657 | |
646 void Deserializer::Deserialize(Isolate* isolate) { | 658 void Deserializer::Deserialize(Isolate* isolate) { |
647 isolate_ = isolate; | 659 Initialize(isolate); |
648 DCHECK(isolate_ != NULL); | |
649 if (!ReserveSpace()) FatalProcessOutOfMemory("deserializing context"); | 660 if (!ReserveSpace()) FatalProcessOutOfMemory("deserializing context"); |
650 // No active threads. | 661 // No active threads. |
651 DCHECK_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse()); | 662 DCHECK_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse()); |
652 // No active handles. | 663 // No active handles. |
653 DCHECK(isolate_->handle_scope_implementer()->blocks()->is_empty()); | 664 DCHECK(isolate_->handle_scope_implementer()->blocks()->is_empty()); |
654 DCHECK_EQ(NULL, external_reference_decoder_); | |
655 external_reference_decoder_ = new ExternalReferenceDecoder(isolate); | |
656 isolate_->heap()->IterateSmiRoots(this); | 665 isolate_->heap()->IterateSmiRoots(this); |
657 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG); | 666 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG); |
658 isolate_->heap()->RepairFreeListsAfterBoot(); | 667 isolate_->heap()->RepairFreeListsAfterBoot(); |
659 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL); | 668 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL); |
660 | 669 |
661 isolate_->heap()->set_native_contexts_list( | 670 isolate_->heap()->set_native_contexts_list( |
662 isolate_->heap()->undefined_value()); | 671 isolate_->heap()->undefined_value()); |
663 isolate_->heap()->set_array_buffers_list( | 672 isolate_->heap()->set_array_buffers_list( |
664 isolate_->heap()->undefined_value()); | 673 isolate_->heap()->undefined_value()); |
665 | 674 |
(...skipping 15 matching lines...) Expand all Loading... | |
681 } | 690 } |
682 | 691 |
683 FlushICacheForNewCodeObjects(); | 692 FlushICacheForNewCodeObjects(); |
684 | 693 |
685 // Issue code events for newly deserialized code objects. | 694 // Issue code events for newly deserialized code objects. |
686 LOG_CODE_EVENT(isolate_, LogCodeObjects()); | 695 LOG_CODE_EVENT(isolate_, LogCodeObjects()); |
687 LOG_CODE_EVENT(isolate_, LogCompiledFunctions()); | 696 LOG_CODE_EVENT(isolate_, LogCompiledFunctions()); |
688 } | 697 } |
689 | 698 |
690 | 699 |
691 void Deserializer::DeserializePartial(Isolate* isolate, Object** root, | 700 MaybeHandle<Object> Deserializer::DeserializePartial( |
692 OnOOM on_oom) { | 701 Isolate* isolate, Handle<FixedArray>* outdated_contexts_out) { |
693 isolate_ = isolate; | 702 Initialize(isolate); |
694 for (int i = NEW_SPACE; i < kNumberOfSpaces; i++) { | |
695 DCHECK(reservations_[i].length() > 0); | |
696 } | |
697 if (!ReserveSpace()) { | 703 if (!ReserveSpace()) { |
698 if (on_oom == FATAL_ON_OOM) FatalProcessOutOfMemory("deserialize context"); | 704 FatalProcessOutOfMemory("deserialize context"); |
699 *root = NULL; | 705 return MaybeHandle<Object>(); |
700 return; | |
701 } | |
702 if (external_reference_decoder_ == NULL) { | |
703 external_reference_decoder_ = new ExternalReferenceDecoder(isolate); | |
704 } | 706 } |
705 | 707 |
706 DisallowHeapAllocation no_gc; | 708 DisallowHeapAllocation no_gc; |
707 | |
708 // Keep track of the code space start and end pointers in case new | 709 // Keep track of the code space start and end pointers in case new |
709 // code objects were unserialized | 710 // code objects were unserialized |
710 OldSpace* code_space = isolate_->heap()->code_space(); | 711 OldSpace* code_space = isolate_->heap()->code_space(); |
711 Address start_address = code_space->top(); | 712 Address start_address = code_space->top(); |
712 VisitPointer(root); | 713 Object* root; |
714 Object* outdated_contexts; | |
715 VisitPointer(&root); | |
716 VisitPointer(&outdated_contexts); | |
713 | 717 |
714 // There's no code deserialized here. If this assert fires | 718 // There's no code deserialized here. If this assert fires |
715 // then that's changed and logging should be added to notify | 719 // then that's changed and logging should be added to notify |
716 // the profiler et al of the new code. | 720 // the profiler et al of the new code. |
717 CHECK_EQ(start_address, code_space->top()); | 721 CHECK_EQ(start_address, code_space->top()); |
722 *outdated_contexts_out = | |
723 Handle<FixedArray>(FixedArray::cast(outdated_contexts), isolate); | |
724 return Handle<Object>(root, isolate); | |
718 } | 725 } |
719 | 726 |
720 | 727 |
728 MaybeHandle<SharedFunctionInfo> Deserializer::DeserializeCode( | |
729 Isolate* isolate) { | |
730 Initialize(isolate); | |
731 if (!ReserveSpace()) { | |
732 return Handle<SharedFunctionInfo>(); | |
733 } else { | |
734 DisallowHeapAllocation no_gc; | |
735 Object* root; | |
736 VisitPointer(&root); | |
737 return Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(root)); | |
738 } | |
739 } | |
740 | |
741 | |
721 Deserializer::~Deserializer() { | 742 Deserializer::~Deserializer() { |
722 // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed. | 743 // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed. |
723 // DCHECK(source_.AtEOF()); | 744 // DCHECK(source_.AtEOF()); |
724 if (external_reference_decoder_) { | 745 if (external_reference_decoder_) { |
725 delete external_reference_decoder_; | 746 delete external_reference_decoder_; |
726 external_reference_decoder_ = NULL; | 747 external_reference_decoder_ = NULL; |
727 } | 748 } |
728 if (attached_objects_) attached_objects_->Dispose(); | 749 if (attached_objects_) attached_objects_->Dispose(); |
729 } | 750 } |
730 | 751 |
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1364 for (int i = 0; i < kPointerSize; i++) { | 1385 for (int i = 0; i < kPointerSize; i++) { |
1365 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte"); | 1386 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte"); |
1366 } | 1387 } |
1367 } else { | 1388 } else { |
1368 SerializeObject(HeapObject::cast(*current), kPlain, kStartOfObject, 0); | 1389 SerializeObject(HeapObject::cast(*current), kPlain, kStartOfObject, 0); |
1369 } | 1390 } |
1370 } | 1391 } |
1371 } | 1392 } |
1372 | 1393 |
1373 | 1394 |
1374 void PartialSerializer::Serialize(Object** object) { | 1395 void PartialSerializer::Serialize(Object** o) { |
1375 this->VisitPointer(object); | 1396 if ((*o)->IsContext()) global_object_ = Context::cast(*o)->global_object(); |
1397 VisitPointer(o); | |
1398 SerializeOutdatedContextsAsFixedArray(); | |
1376 Pad(); | 1399 Pad(); |
1377 } | 1400 } |
1378 | 1401 |
1379 | 1402 |
1403 void PartialSerializer::SerializeOutdatedContextsAsFixedArray() { | |
1404 int length = outdated_contexts_.length(); | |
1405 if (length == 0) { | |
1406 FixedArray* empty = isolate_->heap()->empty_fixed_array(); | |
1407 SerializeObject(empty, kPlain, kStartOfObject, 0); | |
1408 } else { | |
1409 // Serialize an imaginary fixed array containing outdated contexts. | |
1410 int size = FixedArray::SizeFor(length); | |
1411 Allocate(NEW_SPACE, size); | |
1412 sink_->Put(kNewObject + NEW_SPACE, "emulated FixedArray"); | |
1413 sink_->PutInt(size >> kObjectAlignmentBits, "FixedArray size in words"); | |
1414 Map* map = isolate_->heap()->fixed_array_map(); | |
1415 SerializeObject(map, kPlain, kStartOfObject, 0); | |
1416 Smi* length_smi = Smi::FromInt(length); | |
1417 sink_->Put(kOnePointerRawData, "Smi"); | |
1418 for (int i = 0; i < kPointerSize; i++) { | |
1419 sink_->Put(reinterpret_cast<byte*>(&length_smi)[i], "Byte"); | |
1420 } | |
1421 for (int i = 0; i < length; i++) { | |
1422 BackReference back_ref = outdated_contexts_[i]; | |
1423 sink_->Put(kBackref + back_ref.space(), "BackRef"); | |
1424 sink_->PutInt(back_ref.reference(), "BackRefValue"); | |
1425 } | |
1426 } | |
1427 } | |
1428 | |
1429 | |
1380 bool Serializer::ShouldBeSkipped(Object** current) { | 1430 bool Serializer::ShouldBeSkipped(Object** current) { |
1381 Object** roots = isolate()->heap()->roots_array_start(); | 1431 Object** roots = isolate()->heap()->roots_array_start(); |
1382 return current == &roots[Heap::kStoreBufferTopRootIndex] | 1432 return current == &roots[Heap::kStoreBufferTopRootIndex] |
1383 || current == &roots[Heap::kStackLimitRootIndex] | 1433 || current == &roots[Heap::kStackLimitRootIndex] |
1384 || current == &roots[Heap::kRealStackLimitRootIndex]; | 1434 || current == &roots[Heap::kRealStackLimitRootIndex]; |
1385 } | 1435 } |
1386 | 1436 |
1387 | 1437 |
1388 void Serializer::VisitPointers(Object** start, Object** end) { | 1438 void Serializer::VisitPointers(Object** start, Object** end) { |
1389 for (Object** current = start; current < end; current++) { | 1439 for (Object** current = start; current < end; current++) { |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1626 // either in the root table or in the partial snapshot cache. | 1676 // either in the root table or in the partial snapshot cache. |
1627 DCHECK(!obj->IsInternalizedString()); | 1677 DCHECK(!obj->IsInternalizedString()); |
1628 | 1678 |
1629 if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return; | 1679 if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return; |
1630 | 1680 |
1631 FlushSkip(skip); | 1681 FlushSkip(skip); |
1632 | 1682 |
1633 // Object has not yet been serialized. Serialize it here. | 1683 // Object has not yet been serialized. Serialize it here. |
1634 ObjectSerializer serializer(this, obj, sink_, how_to_code, where_to_point); | 1684 ObjectSerializer serializer(this, obj, sink_, how_to_code, where_to_point); |
1635 serializer.Serialize(); | 1685 serializer.Serialize(); |
1686 | |
1687 if (obj->IsContext() && | |
1688 Context::cast(obj)->global_object() == global_object_) { | |
1689 // Context refers to the current global object. This reference will | |
1690 // become outdated after deserialization. | |
1691 BackReference back_reference = back_reference_map_.Lookup(obj); | |
1692 DCHECK(back_reference.is_valid()); | |
1693 outdated_contexts_.Add(back_reference); | |
1694 } | |
1636 } | 1695 } |
1637 | 1696 |
1638 | 1697 |
1639 void Serializer::ObjectSerializer::SerializePrologue(AllocationSpace space, | 1698 void Serializer::ObjectSerializer::SerializePrologue(AllocationSpace space, |
1640 int size, Map* map) { | 1699 int size, Map* map) { |
1641 if (serializer_->code_address_map_) { | 1700 if (serializer_->code_address_map_) { |
1642 const char* code_name = | 1701 const char* code_name = |
1643 serializer_->code_address_map_->Lookup(object_->address()); | 1702 serializer_->code_address_map_->Lookup(object_->address()); |
1644 LOG(serializer_->isolate_, | 1703 LOG(serializer_->isolate_, |
1645 CodeNameEvent(object_->address(), sink_->Position(), code_name)); | 1704 CodeNameEvent(object_->address(), sink_->Position(), code_name)); |
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2269 sink_->Put(kAttachedReference + how_to_code + where_to_point, "Source"); | 2328 sink_->Put(kAttachedReference + how_to_code + where_to_point, "Source"); |
2270 sink_->PutInt(kSourceObjectIndex, "kSourceObjectIndex"); | 2329 sink_->PutInt(kSourceObjectIndex, "kSourceObjectIndex"); |
2271 } | 2330 } |
2272 | 2331 |
2273 | 2332 |
2274 MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize( | 2333 MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize( |
2275 Isolate* isolate, ScriptData* cached_data, Handle<String> source) { | 2334 Isolate* isolate, ScriptData* cached_data, Handle<String> source) { |
2276 base::ElapsedTimer timer; | 2335 base::ElapsedTimer timer; |
2277 if (FLAG_profile_deserialization) timer.Start(); | 2336 if (FLAG_profile_deserialization) timer.Start(); |
2278 | 2337 |
2279 Object* root; | 2338 HandleScope scope(isolate); |
2280 | 2339 |
2281 { | 2340 SmartPointer<SerializedCodeData> scd( |
2282 HandleScope scope(isolate); | 2341 SerializedCodeData::FromCachedData(cached_data, *source)); |
2342 if (scd.is_empty()) { | |
2343 if (FLAG_profile_deserialization) PrintF("[Cached code failed check]\n"); | |
2344 DCHECK(cached_data->rejected()); | |
2345 return MaybeHandle<SharedFunctionInfo>(); | |
2346 } | |
2283 | 2347 |
2284 SmartPointer<SerializedCodeData> scd( | 2348 // Eagerly expand string table to avoid allocations during deserialization. |
2285 SerializedCodeData::FromCachedData(cached_data, *source)); | 2349 StringTable::EnsureCapacityForDeserialization(isolate, |
2286 if (scd.is_empty()) { | 2350 scd->NumInternalizedStrings()); |
2287 if (FLAG_profile_deserialization) PrintF("[Cached code failed check]\n"); | |
2288 DCHECK(cached_data->rejected()); | |
2289 return MaybeHandle<SharedFunctionInfo>(); | |
2290 } | |
2291 | 2351 |
2292 // Eagerly expand string table to avoid allocations during deserialization. | 2352 // Prepare and register list of attached objects. |
2293 StringTable::EnsureCapacityForDeserialization( | 2353 Vector<const uint32_t> code_stub_keys = scd->CodeStubKeys(); |
2294 isolate, scd->NumInternalizedStrings()); | 2354 Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::New( |
2355 code_stub_keys.length() + kCodeStubsBaseIndex); | |
2356 attached_objects[kSourceObjectIndex] = source; | |
2357 for (int i = 0; i < code_stub_keys.length(); i++) { | |
2358 attached_objects[i + kCodeStubsBaseIndex] = | |
2359 CodeStub::GetCode(isolate, code_stub_keys[i]).ToHandleChecked(); | |
2360 } | |
2295 | 2361 |
2296 // Prepare and register list of attached objects. | 2362 Deserializer deserializer(scd.get()); |
2297 Vector<const uint32_t> code_stub_keys = scd->CodeStubKeys(); | 2363 deserializer.SetAttachedObjects(&attached_objects); |
2298 Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::New( | |
2299 code_stub_keys.length() + kCodeStubsBaseIndex); | |
2300 attached_objects[kSourceObjectIndex] = source; | |
2301 for (int i = 0; i < code_stub_keys.length(); i++) { | |
2302 attached_objects[i + kCodeStubsBaseIndex] = | |
2303 CodeStub::GetCode(isolate, code_stub_keys[i]).ToHandleChecked(); | |
2304 } | |
2305 | 2364 |
2306 Deserializer deserializer(scd.get()); | 2365 // Deserialize. |
2307 deserializer.SetAttachedObjects(&attached_objects); | 2366 Handle<SharedFunctionInfo> result; |
2308 | 2367 if (!deserializer.DeserializeCode(isolate).ToHandle(&result)) { |
2309 // Deserialize. | 2368 // Deserializing may fail if the reservations cannot be fulfilled. |
2310 deserializer.DeserializePartial(isolate, &root, Deserializer::NULL_ON_OOM); | 2369 if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n"); |
2311 if (root == NULL) { | 2370 return MaybeHandle<SharedFunctionInfo>(); |
2312 // Deserializing may fail if the reservations cannot be fulfilled. | |
2313 if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n"); | |
2314 return MaybeHandle<SharedFunctionInfo>(); | |
2315 } | |
2316 deserializer.FlushICacheForNewCodeObjects(); | |
2317 } | 2371 } |
2372 deserializer.FlushICacheForNewCodeObjects(); | |
2318 | 2373 |
2319 if (FLAG_profile_deserialization) { | 2374 if (FLAG_profile_deserialization) { |
2320 double ms = timer.Elapsed().InMillisecondsF(); | 2375 double ms = timer.Elapsed().InMillisecondsF(); |
2321 int length = cached_data->length(); | 2376 int length = cached_data->length(); |
2322 PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms); | 2377 PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms); |
2323 } | 2378 } |
2324 Handle<SharedFunctionInfo> result(SharedFunctionInfo::cast(root), isolate); | |
2325 result->set_deserialized(true); | 2379 result->set_deserialized(true); |
2326 | 2380 |
2327 if (isolate->logger()->is_logging_code_events() || | 2381 if (isolate->logger()->is_logging_code_events() || |
2328 isolate->cpu_profiler()->is_profiling()) { | 2382 isolate->cpu_profiler()->is_profiling()) { |
2329 String* name = isolate->heap()->empty_string(); | 2383 String* name = isolate->heap()->empty_string(); |
2330 if (result->script()->IsScript()) { | 2384 if (result->script()->IsScript()) { |
2331 Script* script = Script::cast(result->script()); | 2385 Script* script = Script::cast(result->script()); |
2332 if (script->name()->IsString()) name = String::cast(script->name()); | 2386 if (script->name()->IsString()) name = String::cast(script->name()); |
2333 } | 2387 } |
2334 isolate->logger()->CodeCreateEvent(Logger::SCRIPT_TAG, result->code(), | 2388 isolate->logger()->CodeCreateEvent(Logger::SCRIPT_TAG, result->code(), |
2335 *result, NULL, name); | 2389 *result, NULL, name); |
2336 } | 2390 } |
2337 | 2391 |
2338 return result; | 2392 return scope.CloseAndEscape(result); |
2339 } | 2393 } |
2340 | 2394 |
2341 | 2395 |
2342 void SerializedData::AllocateData(int size) { | 2396 void SerializedData::AllocateData(int size) { |
2343 DCHECK(!owns_data_); | 2397 DCHECK(!owns_data_); |
2344 data_ = NewArray<byte>(size); | 2398 data_ = NewArray<byte>(size); |
2345 size_ = size; | 2399 size_ = size; |
2346 owns_data_ = true; | 2400 owns_data_ = true; |
2347 DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment)); | 2401 DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment)); |
2348 } | 2402 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2481 return GetHeaderValue(kNumInternalizedStringsOffset); | 2535 return GetHeaderValue(kNumInternalizedStringsOffset); |
2482 } | 2536 } |
2483 | 2537 |
2484 Vector<const uint32_t> SerializedCodeData::CodeStubKeys() const { | 2538 Vector<const uint32_t> SerializedCodeData::CodeStubKeys() const { |
2485 int reservations_size = GetHeaderValue(kReservationsOffset) * kInt32Size; | 2539 int reservations_size = GetHeaderValue(kReservationsOffset) * kInt32Size; |
2486 const byte* start = data_ + kHeaderSize + reservations_size; | 2540 const byte* start = data_ + kHeaderSize + reservations_size; |
2487 return Vector<const uint32_t>(reinterpret_cast<const uint32_t*>(start), | 2541 return Vector<const uint32_t>(reinterpret_cast<const uint32_t*>(start), |
2488 GetHeaderValue(kNumCodeStubKeysOffset)); | 2542 GetHeaderValue(kNumCodeStubKeysOffset)); |
2489 } | 2543 } |
2490 } } // namespace v8::internal | 2544 } } // namespace v8::internal |
OLD | NEW |