• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 android.adpf.atom.app;
18 
19 import static android.adpf.atom.common.ADPFAtomTestConstants.ACTION_CREATE_DEAD_TIDS_THEN_GO_BACKGROUND;
20 import static android.adpf.atom.common.ADPFAtomTestConstants.ACTION_CREATE_REGULAR_HINT_SESSIONS;
21 import static android.adpf.atom.common.ADPFAtomTestConstants.ACTION_CREATE_REGULAR_HINT_SESSIONS_MULTIPLE;
22 import static android.adpf.atom.common.ADPFAtomTestConstants.CONTENT_KEY_RESULT_TIDS;
23 import static android.adpf.atom.common.ADPFAtomTestConstants.CONTENT_KEY_UID;
24 import static android.adpf.atom.common.ADPFAtomTestConstants.CONTENT_URI_STRING;
25 import static android.adpf.atom.common.ADPFAtomTestConstants.INTENT_ACTION_KEY;
26 
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assume.assumeNotNull;
29 
30 import android.app.Activity;
31 import android.content.ContentValues;
32 import android.content.Intent;
33 import android.net.Uri;
34 import android.os.Build;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.os.Looper;
38 import android.os.PerformanceHintManager;
39 import android.os.Process;
40 import android.util.ArrayMap;
41 import android.util.Log;
42 import android.widget.RelativeLayout;
43 
44 import com.android.compatibility.common.util.PropertyUtil;
45 
46 import java.util.Map;
47 import java.util.StringJoiner;
48 import java.util.concurrent.CountDownLatch;
49 import java.util.concurrent.atomic.AtomicInteger;
50 
51 /** An activity which performs ADPF actions. */
52 public class ADPFAtomTestActivity extends Activity {
53     private static final String TAG = ADPFAtomTestActivity.class.getSimpleName();
54 
55 
56     private final Map<String, Bundle> mResult = new ArrayMap<>();
57 
58     private static final int FIRST_API_LEVEL = PropertyUtil.getFirstApiLevel();
59 
60     private RelativeLayout mRelativeLayout;
61 
62     @Override
onCreate(Bundle bundle)63     public void onCreate(Bundle bundle) {
64         super.onCreate(bundle);
65 
66         final Intent intent = this.getIntent();
67         assertNotNull(intent);
68         final String action = intent.getStringExtra(INTENT_ACTION_KEY);
69         assertNotNull(action);
70         switch (action) {
71             case ACTION_CREATE_DEAD_TIDS_THEN_GO_BACKGROUND:
72                 try {
73                     final int[] tids = createHintSessionWithExitedThreads();
74                     final StringJoiner sb = new StringJoiner(",");
75                     for (int tid : tids) {
76                         sb.add(String.valueOf(tid));
77                     }
78                     ContentValues values = new ContentValues();
79                     values.put(CONTENT_KEY_RESULT_TIDS, sb.toString());
80                     values.put(CONTENT_KEY_UID, String.valueOf(Process.myUid()));
81                     assertNotNull(
82                             getContentResolver().insert(Uri.parse(CONTENT_URI_STRING), values));
83                 } catch (InterruptedException e) {
84                     throw new RuntimeException(e);
85                 }
86                 Handler handler = new Handler(Looper.getMainLooper());
87                 handler.postDelayed(
88                         () -> {
89                             moveTaskToBack(true);
90                             Log.i(TAG, "Moved task ADPFHintSessionDeviceActivity to back after 1s");
91                         },
92                         1000);
93                 break;
94             case ACTION_CREATE_REGULAR_HINT_SESSIONS:
95                 PerformanceHintManager.Session session = createPerformanceHintSession();
96                 if (FIRST_API_LEVEL < Build.VERSION_CODES.S) {
97                     assumeNotNull(session);
98                 } else {
99                     assertNotNull(session);
100                 }
101                 drawText();
102                 Log.i(TAG, "Created hint session.");
103                 break;
104             case ACTION_CREATE_REGULAR_HINT_SESSIONS_MULTIPLE:
105                 PerformanceHintManager.Session session1 = createPerformanceHintSession();
106                 PerformanceHintManager.Session session2 = createPerformanceHintSession();
107                 PerformanceHintManager.Session session3 = createPerformanceHintSession();
108                 if (FIRST_API_LEVEL < Build.VERSION_CODES.S) {
109                     assumeNotNull(session1);
110                     assumeNotNull(session2);
111                     assumeNotNull(session3);
112                 } else {
113                     assertNotNull(session1);
114                     assertNotNull(session2);
115                     assertNotNull(session3);
116                 }
117                 drawText();
118                 Log.i(TAG, "Created multiple hint sessions.");
119                 break;
120         }
121     }
122 
drawText()123     private void drawText() {
124         setContentView(R.layout.activity_main);
125 
126         mRelativeLayout = findViewById(R.id.idRLView);
127 
128         ADPFAtomTestPaintView paintView = new ADPFAtomTestPaintView(this);
129         mRelativeLayout.addView(paintView);
130     }
131 
132     /**
133      * Read the run result of a specific action
134      */
getRunResult(String actionKey)135     public Bundle getRunResult(String actionKey) {
136         synchronized (mResult) {
137             return mResult.get(actionKey);
138         }
139     }
140 
createHintSessionWithExitedThreads()141     private int[] createHintSessionWithExitedThreads() throws InterruptedException {
142         PerformanceHintManager hintManager = getApplicationContext().getSystemService(
143                 PerformanceHintManager.class);
144         assertNotNull(hintManager);
145         CountDownLatch stopLatch = new CountDownLatch(1);
146         int[] tids = createThreads(5, stopLatch);
147         hintManager.createHintSession(tids, 100);
148         stopLatch.countDown();
149         return tids;
150     }
151 
createThreads(int tidCnt, CountDownLatch stopLatch)152     private int[] createThreads(int tidCnt, CountDownLatch stopLatch) throws InterruptedException {
153         int[] tids = new int[tidCnt];
154         CountDownLatch latch = new CountDownLatch(tidCnt);
155         AtomicInteger k = new AtomicInteger(0);
156         for (int i = 0; i < tidCnt; i++) {
157             final Thread t = new Thread(() -> {
158                 tids[k.getAndIncrement()] = android.os.Process.myTid();
159                 try {
160                     latch.countDown();
161                     stopLatch.await();
162                 } catch (InterruptedException e) {
163                     throw new RuntimeException(e);
164                 }
165             });
166             t.start();
167         }
168         latch.await();
169         return tids;
170     }
171 
createPerformanceHintSession()172     private PerformanceHintManager.Session createPerformanceHintSession() {
173         final long testTargetDuration = 12345678L;
174         PerformanceHintManager hintManager = getApplicationContext().getSystemService(
175                 PerformanceHintManager.class);
176         assertNotNull(hintManager);
177         return hintManager.createHintSession(
178                 new int[]{android.os.Process.myTid()}, testTargetDuration);
179     }
180 }
181