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/bits.h" | 9 #include "src/base/bits.h" |
10 #include "src/base/once.h" | 10 #include "src/base/once.h" |
(...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 } | 768 } |
769 // We must not compact the weak fixed list here, as we may be in the middle | 769 // We must not compact the weak fixed list here, as we may be in the middle |
770 // of writing to it, when the GC triggered. Instead, we reset the root value. | 770 // of writing to it, when the GC triggered. Instead, we reset the root value. |
771 set_weak_stack_trace_list(Smi::FromInt(0)); | 771 set_weak_stack_trace_list(Smi::FromInt(0)); |
772 } | 772 } |
773 | 773 |
774 | 774 |
775 void Heap::HandleGCRequest() { | 775 void Heap::HandleGCRequest() { |
776 if (incremental_marking()->request_type() == | 776 if (incremental_marking()->request_type() == |
777 IncrementalMarking::COMPLETE_MARKING) { | 777 IncrementalMarking::COMPLETE_MARKING) { |
778 CollectAllGarbage(Heap::kNoGCFlags, "GC interrupt"); | 778 CollectAllGarbage(Heap::kNoGCFlags, "GC interrupt", |
| 779 incremental_marking()->CallbackFlags()); |
779 return; | 780 return; |
780 } | 781 } |
781 DCHECK(FLAG_overapproximate_weak_closure); | 782 DCHECK(FLAG_overapproximate_weak_closure); |
782 if (!incremental_marking()->weak_closure_was_overapproximated()) { | 783 if (!incremental_marking()->weak_closure_was_overapproximated()) { |
783 OverApproximateWeakClosure("GC interrupt"); | 784 OverApproximateWeakClosure("GC interrupt"); |
784 } | 785 } |
785 } | 786 } |
786 | 787 |
787 | 788 |
788 void Heap::OverApproximateWeakClosure(const char* gc_reason) { | 789 void Heap::OverApproximateWeakClosure(const char* gc_reason) { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
978 if (collector == MARK_COMPACTOR && | 979 if (collector == MARK_COMPACTOR && |
979 (gc_callback_flags & kGCCallbackFlagForced) != 0) { | 980 (gc_callback_flags & kGCCallbackFlagForced) != 0) { |
980 isolate()->CountUsage(v8::Isolate::kForcedGC); | 981 isolate()->CountUsage(v8::Isolate::kForcedGC); |
981 } | 982 } |
982 | 983 |
983 // Start incremental marking for the next cycle. The heap snapshot | 984 // Start incremental marking for the next cycle. The heap snapshot |
984 // generator needs incremental marking to stay off after it aborted. | 985 // generator needs incremental marking to stay off after it aborted. |
985 if (!mark_compact_collector()->abort_incremental_marking() && | 986 if (!mark_compact_collector()->abort_incremental_marking() && |
986 incremental_marking()->IsStopped() && | 987 incremental_marking()->IsStopped() && |
987 incremental_marking()->ShouldActivateEvenWithoutIdleNotification()) { | 988 incremental_marking()->ShouldActivateEvenWithoutIdleNotification()) { |
988 incremental_marking()->Start(kNoGCFlags); | 989 incremental_marking()->Start(kNoGCFlags, kNoGCCallbackFlags, "GC epilogue"); |
989 } | 990 } |
990 | 991 |
991 return next_gc_likely_to_collect_more; | 992 return next_gc_likely_to_collect_more; |
992 } | 993 } |
993 | 994 |
994 | 995 |
995 int Heap::NotifyContextDisposed(bool dependant_context) { | 996 int Heap::NotifyContextDisposed(bool dependant_context) { |
996 if (!dependant_context) { | 997 if (!dependant_context) { |
997 tracer()->ResetSurvivalEvents(); | 998 tracer()->ResetSurvivalEvents(); |
998 old_generation_size_configured_ = false; | 999 old_generation_size_configured_ = false; |
999 } | 1000 } |
1000 if (isolate()->concurrent_recompilation_enabled()) { | 1001 if (isolate()->concurrent_recompilation_enabled()) { |
1001 // Flush the queued recompilation tasks. | 1002 // Flush the queued recompilation tasks. |
1002 isolate()->optimizing_compile_dispatcher()->Flush(); | 1003 isolate()->optimizing_compile_dispatcher()->Flush(); |
1003 } | 1004 } |
1004 AgeInlineCaches(); | 1005 AgeInlineCaches(); |
1005 set_retained_maps(ArrayList::cast(empty_fixed_array())); | 1006 set_retained_maps(ArrayList::cast(empty_fixed_array())); |
1006 tracer()->AddContextDisposalTime(base::OS::TimeCurrentMillis()); | 1007 tracer()->AddContextDisposalTime(base::OS::TimeCurrentMillis()); |
1007 MemoryReducer::Event event; | 1008 MemoryReducer::Event event; |
1008 event.type = MemoryReducer::kContextDisposed; | 1009 event.type = MemoryReducer::kContextDisposed; |
1009 event.time_ms = MonotonicallyIncreasingTimeInMs(); | 1010 event.time_ms = MonotonicallyIncreasingTimeInMs(); |
1010 memory_reducer_.NotifyContextDisposed(event); | 1011 memory_reducer_.NotifyContextDisposed(event); |
1011 return ++contexts_disposed_; | 1012 return ++contexts_disposed_; |
1012 } | 1013 } |
1013 | 1014 |
1014 | 1015 |
| 1016 void Heap::StartIncrementalMarking(int gc_flags, |
| 1017 const GCCallbackFlags gc_callback_flags, |
| 1018 const char* reason) { |
| 1019 DCHECK(incremental_marking()->IsStopped()); |
| 1020 incremental_marking()->Start(gc_flags, gc_callback_flags, reason); |
| 1021 } |
| 1022 |
| 1023 |
1015 void Heap::StartIdleIncrementalMarking() { | 1024 void Heap::StartIdleIncrementalMarking() { |
1016 gc_idle_time_handler_.ResetNoProgressCounter(); | 1025 gc_idle_time_handler_.ResetNoProgressCounter(); |
1017 incremental_marking()->Start(kReduceMemoryFootprintMask); | 1026 StartIncrementalMarking(kReduceMemoryFootprintMask, kNoGCCallbackFlags, |
| 1027 "idle"); |
1018 } | 1028 } |
1019 | 1029 |
1020 | 1030 |
1021 void Heap::MoveElements(FixedArray* array, int dst_index, int src_index, | 1031 void Heap::MoveElements(FixedArray* array, int dst_index, int src_index, |
1022 int len) { | 1032 int len) { |
1023 if (len == 0) return; | 1033 if (len == 0) return; |
1024 | 1034 |
1025 DCHECK(array->map() != fixed_cow_array_map()); | 1035 DCHECK(array->map() != fixed_cow_array_map()); |
1026 Object** dst_objects = array->data_start() + dst_index; | 1036 Object** dst_objects = array->data_start() + dst_index; |
1027 MemMove(dst_objects, array->data_start() + src_index, len * kPointerSize); | 1037 MemMove(dst_objects, array->data_start() + src_index, len * kPointerSize); |
(...skipping 3727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4755 if (!IsHeapIterable()) { | 4765 if (!IsHeapIterable()) { |
4756 CollectAllGarbage(kMakeHeapIterableMask, "Heap::MakeHeapIterable"); | 4766 CollectAllGarbage(kMakeHeapIterableMask, "Heap::MakeHeapIterable"); |
4757 } | 4767 } |
4758 if (mark_compact_collector()->sweeping_in_progress()) { | 4768 if (mark_compact_collector()->sweeping_in_progress()) { |
4759 mark_compact_collector()->EnsureSweepingCompleted(); | 4769 mark_compact_collector()->EnsureSweepingCompleted(); |
4760 } | 4770 } |
4761 DCHECK(IsHeapIterable()); | 4771 DCHECK(IsHeapIterable()); |
4762 } | 4772 } |
4763 | 4773 |
4764 | 4774 |
| 4775 static double ComputeMutatorUtilization(double mutator_speed, double gc_speed) { |
| 4776 const double kMinMutatorUtilization = 0.0; |
| 4777 const double kConservativeGcSpeedInBytesPerMillisecond = 200000; |
| 4778 if (mutator_speed == 0) return kMinMutatorUtilization; |
| 4779 if (gc_speed == 0) gc_speed = kConservativeGcSpeedInBytesPerMillisecond; |
| 4780 // Derivation: |
| 4781 // mutator_utilization = mutator_time / (mutator_time + gc_time) |
| 4782 // mutator_time = 1 / mutator_speed |
| 4783 // gc_time = 1 / gc_speed |
| 4784 // mutator_utilization = (1 / mutator_speed) / |
| 4785 // (1 / mutator_speed + 1 / gc_speed) |
| 4786 // mutator_utilization = gc_speed / (mutator_speed + gc_speed) |
| 4787 return gc_speed / (mutator_speed + gc_speed); |
| 4788 } |
| 4789 |
| 4790 |
| 4791 double Heap::YoungGenerationMutatorUtilization() { |
| 4792 double mutator_speed = static_cast<double>( |
| 4793 tracer()->NewSpaceAllocationThroughputInBytesPerMillisecond()); |
| 4794 double gc_speed = static_cast<double>( |
| 4795 tracer()->ScavengeSpeedInBytesPerMillisecond(kForSurvivedObjects)); |
| 4796 double result = ComputeMutatorUtilization(mutator_speed, gc_speed); |
| 4797 if (FLAG_trace_mutator_utilization) { |
| 4798 PrintIsolate(isolate(), |
| 4799 "Young generation mutator utilization = %.3f (" |
| 4800 "mutator_speed=%.f, gc_speed=%.f)\n", |
| 4801 result, mutator_speed, gc_speed); |
| 4802 } |
| 4803 return result; |
| 4804 } |
| 4805 |
| 4806 |
| 4807 double Heap::OldGenerationMutatorUtilization() { |
| 4808 double mutator_speed = static_cast<double>( |
| 4809 tracer()->OldGenerationAllocationThroughputInBytesPerMillisecond()); |
| 4810 double gc_speed = static_cast<double>( |
| 4811 tracer()->CombinedMarkCompactSpeedInBytesPerMillisecond()); |
| 4812 double result = ComputeMutatorUtilization(mutator_speed, gc_speed); |
| 4813 if (FLAG_trace_mutator_utilization) { |
| 4814 PrintIsolate(isolate(), |
| 4815 "Old generation mutator utilization = %.3f (" |
| 4816 "mutator_speed=%.f, gc_speed=%.f)\n", |
| 4817 result, mutator_speed, gc_speed); |
| 4818 } |
| 4819 return result; |
| 4820 } |
| 4821 |
| 4822 |
4765 bool Heap::HasLowYoungGenerationAllocationRate() { | 4823 bool Heap::HasLowYoungGenerationAllocationRate() { |
4766 const double high_mutator_utilization = 0.993; | 4824 const double high_mutator_utilization = 0.993; |
4767 double mutator_speed = static_cast<double>( | 4825 return YoungGenerationMutatorUtilization() > high_mutator_utilization; |
4768 tracer()->NewSpaceAllocationThroughputInBytesPerMillisecond()); | |
4769 double gc_speed = static_cast<double>( | |
4770 tracer()->ScavengeSpeedInBytesPerMillisecond(kForSurvivedObjects)); | |
4771 if (mutator_speed == 0 || gc_speed == 0) return false; | |
4772 double mutator_utilization = gc_speed / (mutator_speed + gc_speed); | |
4773 return mutator_utilization > high_mutator_utilization; | |
4774 } | 4826 } |
4775 | 4827 |
4776 | 4828 |
4777 bool Heap::HasLowOldGenerationAllocationRate() { | 4829 bool Heap::HasLowOldGenerationAllocationRate() { |
4778 const double high_mutator_utilization = 0.993; | 4830 const double high_mutator_utilization = 0.993; |
4779 double mutator_speed = static_cast<double>( | 4831 return OldGenerationMutatorUtilization() > high_mutator_utilization; |
4780 tracer()->OldGenerationAllocationThroughputInBytesPerMillisecond()); | |
4781 double gc_speed = static_cast<double>( | |
4782 tracer()->CombinedMarkCompactSpeedInBytesPerMillisecond()); | |
4783 if (mutator_speed == 0 || gc_speed == 0) return false; | |
4784 double mutator_utilization = gc_speed / (mutator_speed + gc_speed); | |
4785 return mutator_utilization > high_mutator_utilization; | |
4786 } | 4832 } |
4787 | 4833 |
4788 | 4834 |
4789 bool Heap::HasLowAllocationRate() { | 4835 bool Heap::HasLowAllocationRate() { |
4790 return HasLowYoungGenerationAllocationRate() && | 4836 return HasLowYoungGenerationAllocationRate() && |
4791 HasLowOldGenerationAllocationRate(); | 4837 HasLowOldGenerationAllocationRate(); |
4792 } | 4838 } |
4793 | 4839 |
4794 | 4840 |
4795 bool Heap::HasHighFragmentation() { | 4841 bool Heap::HasHighFragmentation() { |
4796 intptr_t used = PromotedSpaceSizeOfObjects(); | 4842 intptr_t used = PromotedSpaceSizeOfObjects(); |
4797 intptr_t committed = CommittedOldGenerationMemory(); | 4843 intptr_t committed = CommittedOldGenerationMemory(); |
4798 return HasHighFragmentation(used, committed); | 4844 return HasHighFragmentation(used, committed); |
4799 } | 4845 } |
4800 | 4846 |
4801 | 4847 |
4802 bool Heap::HasHighFragmentation(intptr_t used, intptr_t committed) { | 4848 bool Heap::HasHighFragmentation(intptr_t used, intptr_t committed) { |
4803 const intptr_t kSlack = 16 * MB; | 4849 const intptr_t kSlack = 16 * MB; |
4804 // Fragmentation is high if committed > 2 * used + kSlack. | 4850 // Fragmentation is high if committed > 2 * used + kSlack. |
4805 // Rewrite the exression to avoid overflow. | 4851 // Rewrite the exression to avoid overflow. |
4806 return committed - used > used + kSlack; | 4852 return committed - used > used + kSlack; |
4807 } | 4853 } |
4808 | 4854 |
4809 | 4855 |
4810 void Heap::ReduceNewSpaceSize() { | 4856 void Heap::ReduceNewSpaceSize() { |
4811 if (!FLAG_predictable && HasLowAllocationRate()) { | 4857 // TODO(ulan): Unify this constant with the similar constant in |
| 4858 // GCIdleTimeHandler once the change is merged to 4.5. |
| 4859 static const size_t kLowAllocationThroughput = 1000; |
| 4860 size_t allocation_throughput = |
| 4861 tracer()->CurrentAllocationThroughputInBytesPerMillisecond(); |
| 4862 if (FLAG_predictable || allocation_throughput == 0) return; |
| 4863 if (allocation_throughput < kLowAllocationThroughput) { |
4812 new_space_.Shrink(); | 4864 new_space_.Shrink(); |
4813 UncommitFromSpace(); | 4865 UncommitFromSpace(); |
4814 } | 4866 } |
4815 } | 4867 } |
4816 | 4868 |
4817 | 4869 |
| 4870 void Heap::FinalizeIncrementalMarkingIfComplete(const char* comment) { |
| 4871 if (FLAG_overapproximate_weak_closure && |
| 4872 (incremental_marking()->IsReadyToOverApproximateWeakClosure() || |
| 4873 (!incremental_marking()->weak_closure_was_overapproximated() && |
| 4874 mark_compact_collector_.marking_deque()->IsEmpty()))) { |
| 4875 OverApproximateWeakClosure(comment); |
| 4876 } else if (incremental_marking()->IsComplete() || |
| 4877 (mark_compact_collector_.marking_deque()->IsEmpty())) { |
| 4878 CollectAllGarbage(kNoGCFlags, comment); |
| 4879 } |
| 4880 } |
| 4881 |
| 4882 |
4818 bool Heap::TryFinalizeIdleIncrementalMarking( | 4883 bool Heap::TryFinalizeIdleIncrementalMarking( |
4819 double idle_time_in_ms, size_t size_of_objects, | 4884 double idle_time_in_ms, size_t size_of_objects, |
4820 size_t final_incremental_mark_compact_speed_in_bytes_per_ms) { | 4885 size_t final_incremental_mark_compact_speed_in_bytes_per_ms) { |
4821 if (FLAG_overapproximate_weak_closure && | 4886 if (FLAG_overapproximate_weak_closure && |
4822 (incremental_marking()->IsReadyToOverApproximateWeakClosure() || | 4887 (incremental_marking()->IsReadyToOverApproximateWeakClosure() || |
4823 (!incremental_marking()->weak_closure_was_overapproximated() && | 4888 (!incremental_marking()->weak_closure_was_overapproximated() && |
4824 mark_compact_collector_.marking_deque()->IsEmpty() && | 4889 mark_compact_collector_.marking_deque()->IsEmpty() && |
4825 gc_idle_time_handler_.ShouldDoOverApproximateWeakClosure( | 4890 gc_idle_time_handler_.ShouldDoOverApproximateWeakClosure( |
4826 static_cast<size_t>(idle_time_in_ms))))) { | 4891 static_cast<size_t>(idle_time_in_ms))))) { |
4827 OverApproximateWeakClosure( | 4892 OverApproximateWeakClosure( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4860 heap_state.scavenge_speed_in_bytes_per_ms = | 4925 heap_state.scavenge_speed_in_bytes_per_ms = |
4861 static_cast<size_t>(tracer()->ScavengeSpeedInBytesPerMillisecond()); | 4926 static_cast<size_t>(tracer()->ScavengeSpeedInBytesPerMillisecond()); |
4862 heap_state.used_new_space_size = new_space_.Size(); | 4927 heap_state.used_new_space_size = new_space_.Size(); |
4863 heap_state.new_space_capacity = new_space_.Capacity(); | 4928 heap_state.new_space_capacity = new_space_.Capacity(); |
4864 heap_state.new_space_allocation_throughput_in_bytes_per_ms = | 4929 heap_state.new_space_allocation_throughput_in_bytes_per_ms = |
4865 tracer()->NewSpaceAllocationThroughputInBytesPerMillisecond(); | 4930 tracer()->NewSpaceAllocationThroughputInBytesPerMillisecond(); |
4866 return heap_state; | 4931 return heap_state; |
4867 } | 4932 } |
4868 | 4933 |
4869 | 4934 |
| 4935 double Heap::AdvanceIncrementalMarking( |
| 4936 intptr_t step_size_in_bytes, double deadline_in_ms, |
| 4937 IncrementalMarking::StepActions step_actions) { |
| 4938 DCHECK(!incremental_marking()->IsStopped()); |
| 4939 |
| 4940 if (step_size_in_bytes == 0) { |
| 4941 step_size_in_bytes = GCIdleTimeHandler::EstimateMarkingStepSize( |
| 4942 static_cast<size_t>(GCIdleTimeHandler::kIncrementalMarkingStepTimeInMs), |
| 4943 static_cast<size_t>( |
| 4944 tracer()->FinalIncrementalMarkCompactSpeedInBytesPerMillisecond())); |
| 4945 } |
| 4946 |
| 4947 double remaining_time_in_ms = 0.0; |
| 4948 do { |
| 4949 incremental_marking()->Step( |
| 4950 step_size_in_bytes, step_actions.completion_action, |
| 4951 step_actions.force_marking, step_actions.force_completion); |
| 4952 remaining_time_in_ms = deadline_in_ms - MonotonicallyIncreasingTimeInMs(); |
| 4953 } while (remaining_time_in_ms >= |
| 4954 2.0 * GCIdleTimeHandler::kIncrementalMarkingStepTimeInMs && |
| 4955 !incremental_marking()->IsComplete() && |
| 4956 !mark_compact_collector_.marking_deque()->IsEmpty()); |
| 4957 return remaining_time_in_ms; |
| 4958 } |
| 4959 |
| 4960 |
4870 bool Heap::PerformIdleTimeAction(GCIdleTimeAction action, | 4961 bool Heap::PerformIdleTimeAction(GCIdleTimeAction action, |
4871 GCIdleTimeHandler::HeapState heap_state, | 4962 GCIdleTimeHandler::HeapState heap_state, |
4872 double deadline_in_ms) { | 4963 double deadline_in_ms) { |
4873 bool result = false; | 4964 bool result = false; |
4874 switch (action.type) { | 4965 switch (action.type) { |
4875 case DONE: | 4966 case DONE: |
4876 result = true; | 4967 result = true; |
4877 break; | 4968 break; |
4878 case DO_INCREMENTAL_MARKING: { | 4969 case DO_INCREMENTAL_MARKING: { |
4879 DCHECK(!incremental_marking()->IsStopped()); | 4970 const double remaining_idle_time_in_ms = |
4880 double remaining_idle_time_in_ms = 0.0; | 4971 AdvanceIncrementalMarking(action.parameter, deadline_in_ms, |
4881 do { | 4972 IncrementalMarking::IdleStepActions()); |
4882 incremental_marking()->Step( | |
4883 action.parameter, IncrementalMarking::NO_GC_VIA_STACK_GUARD, | |
4884 IncrementalMarking::FORCE_MARKING, | |
4885 IncrementalMarking::DO_NOT_FORCE_COMPLETION); | |
4886 remaining_idle_time_in_ms = | |
4887 deadline_in_ms - MonotonicallyIncreasingTimeInMs(); | |
4888 } while (remaining_idle_time_in_ms >= | |
4889 2.0 * GCIdleTimeHandler::kIncrementalMarkingStepTimeInMs && | |
4890 !incremental_marking()->IsComplete() && | |
4891 !mark_compact_collector_.marking_deque()->IsEmpty()); | |
4892 if (remaining_idle_time_in_ms > 0.0) { | 4973 if (remaining_idle_time_in_ms > 0.0) { |
4893 action.additional_work = TryFinalizeIdleIncrementalMarking( | 4974 action.additional_work = TryFinalizeIdleIncrementalMarking( |
4894 remaining_idle_time_in_ms, heap_state.size_of_objects, | 4975 remaining_idle_time_in_ms, heap_state.size_of_objects, |
4895 heap_state.final_incremental_mark_compact_speed_in_bytes_per_ms); | 4976 heap_state.final_incremental_mark_compact_speed_in_bytes_per_ms); |
4896 } | 4977 } |
4897 break; | 4978 break; |
4898 } | 4979 } |
4899 case DO_FULL_GC: { | 4980 case DO_FULL_GC: { |
4900 DCHECK(contexts_disposed_ > 0); | 4981 DCHECK(contexts_disposed_ > 0); |
4901 HistogramTimerScope scope(isolate_->counters()->gc_context()); | 4982 HistogramTimerScope scope(isolate_->counters()->gc_context()); |
(...skipping 2004 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6906 *object_type = "CODE_TYPE"; \ | 6987 *object_type = "CODE_TYPE"; \ |
6907 *object_sub_type = "CODE_AGE/" #name; \ | 6988 *object_sub_type = "CODE_AGE/" #name; \ |
6908 return true; | 6989 return true; |
6909 CODE_AGE_LIST_COMPLETE(COMPARE_AND_RETURN_NAME) | 6990 CODE_AGE_LIST_COMPLETE(COMPARE_AND_RETURN_NAME) |
6910 #undef COMPARE_AND_RETURN_NAME | 6991 #undef COMPARE_AND_RETURN_NAME |
6911 } | 6992 } |
6912 return false; | 6993 return false; |
6913 } | 6994 } |
6914 } // namespace internal | 6995 } // namespace internal |
6915 } // namespace v8 | 6996 } // namespace v8 |
OLD | NEW |