From 4c7fc5950853b0c368e2148db77ced7c4d3c303c Mon Sep 17 00:00:00 2001 From: Mathieu Chartier Date: Wed, 3 Sep 2014 10:30:11 -0700 Subject: [PATCH] Fix native allocation watermark clamping. The main issue causing the test to fail is that native_footprint_gc_watermark_ becoming > growth_limit_ due to no clamping. Temporary runFinalization fix is calling runFinalization 2x. Bug: 17371542 Change-Id: I188cb530a44dd109e066a22091f12f8d2d4350c3 --- runtime/gc/heap.cc | 18 ++++++++++-------- runtime/gc/heap.h | 4 ++-- runtime/native/dalvik_system_VMRuntime.cc | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc index ce914e543..b2873e566 100644 --- a/runtime/gc/heap.cc +++ b/runtime/gc/heap.cc @@ -2831,7 +2831,7 @@ void Heap::UpdateMaxNativeFootprint() { } else if (target_size < native_size + min_free_) { target_size = native_size + min_free_; } - native_footprint_gc_watermark_ = target_size; + native_footprint_gc_watermark_ = std::min(growth_limit_, target_size); } collector::GarbageCollector* Heap::FindCollectorByGcType(collector::GcType gc_type) { @@ -3085,9 +3085,11 @@ void Heap::RunFinalization(JNIEnv* env) { } env->CallStaticVoidMethod(WellKnownClasses::java_lang_System, WellKnownClasses::java_lang_System_runFinalization); + env->CallStaticVoidMethod(WellKnownClasses::java_lang_System, + WellKnownClasses::java_lang_System_runFinalization); } -void Heap::RegisterNativeAllocation(JNIEnv* env, int bytes) { +void Heap::RegisterNativeAllocation(JNIEnv* env, size_t bytes) { Thread* self = ThreadForEnv(env); if (native_need_to_run_finalization_) { RunFinalization(env); @@ -3129,19 +3131,19 @@ void Heap::RegisterNativeAllocation(JNIEnv* env, int bytes) { } } -void Heap::RegisterNativeFree(JNIEnv* env, int bytes) { - int expected_size, new_size; +void Heap::RegisterNativeFree(JNIEnv* env, size_t bytes) { + size_t expected_size; do { expected_size = native_bytes_allocated_.LoadRelaxed(); - new_size = expected_size - bytes; - if (UNLIKELY(new_size < 0)) { + if (UNLIKELY(bytes > expected_size)) { ScopedObjectAccess soa(env); env->ThrowNew(WellKnownClasses::java_lang_RuntimeException, - StringPrintf("Attempted to free %d native bytes with only %d native bytes " + StringPrintf("Attempted to free %zd native bytes with only %zd native bytes " "registered as allocated", bytes, expected_size).c_str()); break; } - } while (!native_bytes_allocated_.CompareExchangeWeakRelaxed(expected_size, new_size)); + } while (!native_bytes_allocated_.CompareExchangeWeakRelaxed(expected_size, + expected_size - bytes)); } size_t Heap::GetTotalMemory() const { diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h index a0fcf53da..d3d613f56 100644 --- a/runtime/gc/heap.h +++ b/runtime/gc/heap.h @@ -211,8 +211,8 @@ class Heap { void CheckPreconditionsForAllocObject(mirror::Class* c, size_t byte_count) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); - void RegisterNativeAllocation(JNIEnv* env, int bytes); - void RegisterNativeFree(JNIEnv* env, int bytes); + void RegisterNativeAllocation(JNIEnv* env, size_t bytes); + void RegisterNativeFree(JNIEnv* env, size_t bytes); // Change the allocator, updates entrypoints. void ChangeAllocator(AllocatorType allocator) diff --git a/runtime/native/dalvik_system_VMRuntime.cc b/runtime/native/dalvik_system_VMRuntime.cc index b0b64aac1..19e655041 100644 --- a/runtime/native/dalvik_system_VMRuntime.cc +++ b/runtime/native/dalvik_system_VMRuntime.cc @@ -184,7 +184,7 @@ static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jint bytes) ThrowRuntimeException("allocation size negative %d", bytes); return; } - Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, bytes); + Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, static_cast(bytes)); } static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jint bytes) { @@ -193,7 +193,7 @@ static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jint bytes) { ThrowRuntimeException("allocation size negative %d", bytes); return; } - Runtime::Current()->GetHeap()->RegisterNativeFree(env, bytes); + Runtime::Current()->GetHeap()->RegisterNativeFree(env, static_cast(bytes)); } static void VMRuntime_updateProcessState(JNIEnv* env, jobject, jint process_state) { -- 2.11.0