blob: 92f4e21f4015fc8a6149d56185115d659e797d51 [file] [log] [blame]
Mathieu Chartier987ccff2013-07-08 11:05:21 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.lang.reflect.*;
Mathieu Chartiere03df652014-09-02 17:36:08 -070018import java.lang.Runtime;
Mathieu Chartier987ccff2013-07-08 11:05:21 -070019
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070020public class Main {
Mathieu Chartier987ccff2013-07-08 11:05:21 -070021 static Object nativeLock = new Object();
Mathieu Chartier3b532d72015-06-05 13:21:05 -070022 static Object deadlockLock = new Object();
23 static boolean aboutToDeadlockLock = false;
Mathieu Chartier987ccff2013-07-08 11:05:21 -070024 static int nativeBytes = 0;
25 static Object runtime;
26 static Method register_native_allocation;
27 static Method register_native_free;
Mathieu Chartiere03df652014-09-02 17:36:08 -070028 static long maxMem = 0;
Mathieu Chartier987ccff2013-07-08 11:05:21 -070029
30 static class NativeAllocation {
31 private int bytes;
32
Mathieu Chartier3b532d72015-06-05 13:21:05 -070033 NativeAllocation(int bytes, boolean testingDeadlock) throws Exception {
Mathieu Chartier987ccff2013-07-08 11:05:21 -070034 this.bytes = bytes;
35 register_native_allocation.invoke(runtime, bytes);
36 synchronized (nativeLock) {
Mathieu Chartier3b532d72015-06-05 13:21:05 -070037 if (!testingDeadlock) {
38 nativeBytes += bytes;
39 if (nativeBytes > maxMem) {
40 throw new OutOfMemoryError();
41 }
Mathieu Chartier987ccff2013-07-08 11:05:21 -070042 }
43 }
44 }
45
46 protected void finalize() throws Exception {
47 synchronized (nativeLock) {
48 nativeBytes -= bytes;
49 }
50 register_native_free.invoke(runtime, bytes);
Mathieu Chartier3b532d72015-06-05 13:21:05 -070051 aboutToDeadlockLock = true;
52 synchronized (deadlockLock) {
53 }
Mathieu Chartier987ccff2013-07-08 11:05:21 -070054 }
55 }
56
57 public static void main(String[] args) throws Exception {
58 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
59 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
60 runtime = get_runtime.invoke(null);
61 register_native_allocation = vm_runtime.getDeclaredMethod("registerNativeAllocation", Integer.TYPE);
62 register_native_free = vm_runtime.getDeclaredMethod("registerNativeFree", Integer.TYPE);
Mathieu Chartiere03df652014-09-02 17:36:08 -070063 maxMem = Runtime.getRuntime().maxMemory();
Mathieu Chartier987ccff2013-07-08 11:05:21 -070064 int count = 16;
Mathieu Chartiere03df652014-09-02 17:36:08 -070065 int size = (int)(maxMem / 2 / count);
Mathieu Chartier987ccff2013-07-08 11:05:21 -070066 int allocation_count = 256;
67 NativeAllocation[] allocations = new NativeAllocation[count];
68 for (int i = 0; i < allocation_count; ++i) {
Mathieu Chartier3b532d72015-06-05 13:21:05 -070069 allocations[i % count] = new NativeAllocation(size, false);
70 }
71 // Test that we don't get a deadlock if we are holding nativeLock. If there is no timeout,
72 // then we will get a finalizer timeout exception.
73 aboutToDeadlockLock = false;
74 synchronized (deadlockLock) {
75 for (int i = 0; aboutToDeadlockLock != true; ++i) {
76 allocations[i % count] = new NativeAllocation(size, true);
77 }
78 // Do more allocations now that the finalizer thread is deadlocked so that we force
79 // finalization and timeout.
80 for (int i = 0; i < 10; ++i) {
81 allocations[i % count] = new NativeAllocation(size, true);
82 }
Mathieu Chartier987ccff2013-07-08 11:05:21 -070083 }
84 System.out.println("Test complete");
85 }
86}
87