vadimt | 45564c8 | 2020-06-10 18:40:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
| 17 | package com.android.launcher3.testing; |
| 18 | |
| 19 | import static android.graphics.Bitmap.Config.ARGB_8888; |
| 20 | |
vadimt | 839fb7b | 2020-06-22 15:01:49 -0700 | [diff] [blame] | 21 | import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; |
| 22 | |
vadimt | 45564c8 | 2020-06-10 18:40:58 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Color; |
vadimt | efa3acd | 2020-06-23 13:59:11 -0700 | [diff] [blame] | 26 | import android.os.Binder; |
vadimt | 45564c8 | 2020-06-10 18:40:58 -0700 | [diff] [blame] | 27 | import android.os.Bundle; |
| 28 | import android.os.Debug; |
| 29 | import android.system.Os; |
| 30 | import android.view.View; |
| 31 | |
| 32 | import androidx.annotation.Keep; |
| 33 | |
vadimt | 839fb7b | 2020-06-22 15:01:49 -0700 | [diff] [blame] | 34 | import com.android.launcher3.LauncherAppState; |
| 35 | import com.android.launcher3.LauncherSettings; |
| 36 | |
vadimt | a17a10c | 2020-06-19 15:00:00 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | import java.util.Collection; |
vadimt | 45564c8 | 2020-06-10 18:40:58 -0700 | [diff] [blame] | 39 | import java.util.LinkedList; |
| 40 | import java.util.concurrent.CountDownLatch; |
| 41 | import java.util.concurrent.TimeUnit; |
| 42 | |
| 43 | /** |
| 44 | * Class to handle requests from tests, including debug ones. |
| 45 | */ |
| 46 | public class DebugTestInformationHandler extends TestInformationHandler { |
| 47 | private static LinkedList sLeaks; |
vadimt | a17a10c | 2020-06-19 15:00:00 -0700 | [diff] [blame] | 48 | private static Collection<String> sEvents; |
vadimt | 45564c8 | 2020-06-10 18:40:58 -0700 | [diff] [blame] | 49 | |
| 50 | public DebugTestInformationHandler(Context context) { |
| 51 | init(context); |
| 52 | } |
| 53 | |
| 54 | private static void runGcAndFinalizersSync() { |
| 55 | Runtime.getRuntime().gc(); |
| 56 | Runtime.getRuntime().runFinalization(); |
| 57 | |
| 58 | final CountDownLatch fence = new CountDownLatch(1); |
| 59 | createFinalizationObserver(fence); |
| 60 | try { |
| 61 | do { |
| 62 | Runtime.getRuntime().gc(); |
| 63 | Runtime.getRuntime().runFinalization(); |
| 64 | } while (!fence.await(100, TimeUnit.MILLISECONDS)); |
| 65 | } catch (InterruptedException ex) { |
| 66 | throw new RuntimeException(ex); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Create the observer in the scope of a method to minimize the chance that |
| 71 | // it remains live in a DEX/machine register at the point of the fence guard. |
| 72 | // This must be kept to avoid R8 inlining it. |
| 73 | @Keep |
| 74 | private static void createFinalizationObserver(CountDownLatch fence) { |
| 75 | new Object() { |
| 76 | @Override |
| 77 | protected void finalize() throws Throwable { |
| 78 | try { |
| 79 | fence.countDown(); |
| 80 | } finally { |
| 81 | super.finalize(); |
| 82 | } |
| 83 | } |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Bundle call(String method) { |
| 89 | final Bundle response = new Bundle(); |
| 90 | switch (method) { |
| 91 | case TestProtocol.REQUEST_APP_LIST_FREEZE_FLAGS: { |
| 92 | return getLauncherUIProperty(Bundle::putInt, |
| 93 | l -> l.getAppsView().getAppsStore().getDeferUpdatesFlags()); |
| 94 | } |
| 95 | |
| 96 | case TestProtocol.REQUEST_ENABLE_DEBUG_TRACING: |
| 97 | TestProtocol.sDebugTracing = true; |
| 98 | return response; |
| 99 | |
| 100 | case TestProtocol.REQUEST_DISABLE_DEBUG_TRACING: |
| 101 | TestProtocol.sDebugTracing = false; |
| 102 | return response; |
| 103 | |
| 104 | case TestProtocol.REQUEST_PID: { |
| 105 | response.putInt(TestProtocol.TEST_INFO_RESPONSE_FIELD, Os.getpid()); |
| 106 | return response; |
| 107 | } |
| 108 | |
| 109 | case TestProtocol.REQUEST_TOTAL_PSS_KB: { |
| 110 | runGcAndFinalizersSync(); |
| 111 | Debug.MemoryInfo mem = new Debug.MemoryInfo(); |
| 112 | Debug.getMemoryInfo(mem); |
| 113 | response.putInt(TestProtocol.TEST_INFO_RESPONSE_FIELD, mem.getTotalPss()); |
| 114 | return response; |
| 115 | } |
| 116 | |
| 117 | case TestProtocol.REQUEST_JAVA_LEAK: { |
| 118 | if (sLeaks == null) sLeaks = new LinkedList(); |
| 119 | |
| 120 | // Allocate and dirty the memory. |
| 121 | final int leakSize = 1024 * 1024; |
| 122 | final byte[] bytes = new byte[leakSize]; |
| 123 | for (int i = 0; i < leakSize; i += 239) { |
| 124 | bytes[i] = (byte) (i % 256); |
| 125 | } |
| 126 | sLeaks.add(bytes); |
| 127 | return response; |
| 128 | } |
| 129 | |
| 130 | case TestProtocol.REQUEST_NATIVE_LEAK: { |
| 131 | if (sLeaks == null) sLeaks = new LinkedList(); |
| 132 | |
| 133 | // Allocate and dirty a bitmap. |
| 134 | final Bitmap bitmap = Bitmap.createBitmap(512, 512, ARGB_8888); |
| 135 | bitmap.eraseColor(Color.RED); |
| 136 | sLeaks.add(bitmap); |
| 137 | return response; |
| 138 | } |
| 139 | |
| 140 | case TestProtocol.REQUEST_VIEW_LEAK: { |
| 141 | if (sLeaks == null) sLeaks = new LinkedList(); |
| 142 | sLeaks.add(new View(mContext)); |
| 143 | return response; |
| 144 | } |
| 145 | |
vadimt | a17a10c | 2020-06-19 15:00:00 -0700 | [diff] [blame] | 146 | case TestProtocol.REQUEST_START_EVENT_LOGGING: { |
| 147 | sEvents = new ArrayList<>(); |
| 148 | TestLogging.setEventConsumer( |
| 149 | (sequence, event) -> { |
| 150 | final Collection<String> events = sEvents; |
| 151 | if (events != null) { |
| 152 | synchronized (events) { |
| 153 | events.add(sequence + '/' + event); |
| 154 | } |
| 155 | } |
| 156 | }); |
| 157 | return response; |
| 158 | } |
| 159 | |
| 160 | case TestProtocol.REQUEST_STOP_EVENT_LOGGING: { |
| 161 | TestLogging.setEventConsumer(null); |
| 162 | sEvents = null; |
| 163 | return response; |
| 164 | } |
| 165 | |
| 166 | case TestProtocol.REQUEST_GET_TEST_EVENTS: { |
vadimt | 56fe915 | 2020-06-25 13:32:20 -0700 | [diff] [blame] | 167 | if (sEvents == null) { |
| 168 | // sEvents can be null if Launcher died and restarted after |
| 169 | // REQUEST_START_EVENT_LOGGING. |
| 170 | return response; |
| 171 | } |
| 172 | |
vadimt | a17a10c | 2020-06-19 15:00:00 -0700 | [diff] [blame] | 173 | synchronized (sEvents) { |
| 174 | response.putStringArrayList( |
| 175 | TestProtocol.TEST_INFO_RESPONSE_FIELD, new ArrayList<>(sEvents)); |
| 176 | } |
| 177 | return response; |
| 178 | } |
| 179 | |
vadimt | 839fb7b | 2020-06-22 15:01:49 -0700 | [diff] [blame] | 180 | case TestProtocol.REQUEST_CLEAR_DATA: { |
vadimt | efa3acd | 2020-06-23 13:59:11 -0700 | [diff] [blame] | 181 | final long identity = Binder.clearCallingIdentity(); |
| 182 | try { |
| 183 | LauncherSettings.Settings.call(mContext.getContentResolver(), |
| 184 | LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB); |
| 185 | MAIN_EXECUTOR.submit(() -> |
| 186 | LauncherAppState.getInstance(mContext).getModel().forceReload()); |
| 187 | return response; |
| 188 | } finally { |
| 189 | Binder.restoreCallingIdentity(identity); |
| 190 | } |
vadimt | 839fb7b | 2020-06-22 15:01:49 -0700 | [diff] [blame] | 191 | } |
| 192 | |
vadimt | 45564c8 | 2020-06-10 18:40:58 -0700 | [diff] [blame] | 193 | default: |
| 194 | return super.call(method); |
| 195 | } |
| 196 | } |
| 197 | } |