Debian Codegraph @

apex/jobscheduler/service/java/com/android/server/tare/ProcessStateModifier.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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.server.tare;
18
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.app.ActivityManager;
22 import android.app.IUidObserver;
23 import android.os.RemoteException;
24 import android.util.IndentingPrintWriter;
25 import android.util.Slog;
26 import android.util.SparseArrayMap;
27 import android.util.SparseIntArray;
28
29 import com.android.internal.annotations.GuardedBy;
30
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33
34 /** Modifier that makes things more cheaper based on an app's process state. */
35 class ProcessStateModifier extends Modifier {
36 private static final String TAG = "TARE-" + ProcessStateModifier.class.getSimpleName();
37
38 private static final int PROC_STATE_BUCKET_NONE = 0;
39 private static final int PROC_STATE_BUCKET_TOP = 1;
40 private static final int PROC_STATE_BUCKET_FGS = 2;
41 private static final int PROC_STATE_BUCKET_BFGS = 3;
42 private static final int PROC_STATE_BUCKET_BG = 4;
43
44 @IntDef(prefix = {"PROC_STATE_BUCKET_"}, value = {
45 PROC_STATE_BUCKET_NONE,
46 PROC_STATE_BUCKET_TOP,
47 PROC_STATE_BUCKET_FGS,
48 PROC_STATE_BUCKET_BFGS,
49 PROC_STATE_BUCKET_BG
50 })
51 @Retention(RetentionPolicy.SOURCE)
52 public @interface ProcStateBucket {
53 }
54
55 private final Object mLock = new Object();
56 private final InternalResourceService mIrs;
57
58 /** Cached mapping of userId+package to their UIDs (for all users) */
59 private final SparseArrayMap<String, Integer> mPackageToUidCache = new SparseArrayMap<>();
60
61 @GuardedBy("mLock")
62 private final SparseIntArray mUidProcStateBucketCache = new SparseIntArray();
63
64 private final IUidObserver mUidObserver = new IUidObserver.Stub() {
65 @Override
66 public void onUidStateChanged(int uid, int procState, long procStateSeq, int capability) {
67 final int newBucket = getProcStateBucket(procState);
68 synchronized (mLock) {
69 final int curBucket = mUidProcStateBucketCache.get(uid);
70 if (curBucket != newBucket) {
71 mUidProcStateBucketCache.put(uid, newBucket);
72 }
73 notifyStateChangedLocked(uid);
74 }
75 }
76
77 @Override
78 public void onUidGone(int uid, boolean disabled) {
79 synchronized (mLock) {
80 if (mUidProcStateBucketCache.indexOfKey(uid) < 0) {
81 Slog.e(TAG, "UID " + uid + " marked gone but wasn't in cache.");
82 return;
83 }
84 mUidProcStateBucketCache.delete(uid);
85 notifyStateChangedLocked(uid);
86 }
87 }
88
89 @Override
90 public void onUidActive(int uid) {
91 }
92
93 @Override
94 public void onUidIdle(int uid, boolean disabled) {
95 }
96
97 @Override
98 public void onUidCachedChanged(int uid, boolean cached) {
99 }
100
101 @Override
102 public void onUidProcAdjChanged(int uid) {
103 }
104 };
105
106 ProcessStateModifier(@NonNull InternalResourceService irs) {
107 super();
108 mIrs = irs;
109 }
110
111 @Override
112 @GuardedBy("mLock")
113 void setup() {
114 try {
115 ActivityManager.getService().registerUidObserver(mUidObserver,
116 ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE,
117 ActivityManager.PROCESS_STATE_UNKNOWN, null);
118 } catch (RemoteException e) {
119 // ignored; both services live in system_server
120 }
121 }
122
123 @Override
124 @GuardedBy("mLock")
125 void tearDown() {
126 try {
127 ActivityManager.getService().unregisterUidObserver(mUidObserver);
128 } catch (RemoteException e) {
129 // ignored; both services live in system_server
130 }
131 mPackageToUidCache.clear();
132 mUidProcStateBucketCache.clear();
133 }
134
135 /**
136 * Get the final modified price based on an app's process state.
137 *
138 * @param ctp Cost to produce. @see EconomicPolicy.Action#costToProduce
139 * @param price Current price
140 */
141 long getModifiedPrice(final int userId, @NonNull final String pkgName,
142 final long ctp, final long price) {
143 final int procState;
144 synchronized (mLock) {
145 procState = mUidProcStateBucketCache.get(
146 mIrs.getUid(userId, pkgName), PROC_STATE_BUCKET_NONE);
147 }
148 switch (procState) {
149 case PROC_STATE_BUCKET_TOP:
150 return 0;
151 case PROC_STATE_BUCKET_FGS:
152 // Can't get notification priority. Just use CTP for now.
153 return Math.min(ctp, price);
154 case PROC_STATE_BUCKET_BFGS:
155 if (price <= ctp) {
156 return price;
157 }
158 return (long) (ctp + .5 * (price - ctp));
159 case PROC_STATE_BUCKET_BG:
160 default:
161 return price;
162 }
163 }
164
165 @Override
166 @GuardedBy("mLock")
167 void dump(IndentingPrintWriter pw) {
168 pw.print("Proc state bucket cache = ");
169 pw.println(mUidProcStateBucketCache);
170 }
171
172 @ProcStateBucket
173 private int getProcStateBucket(int procState) {
174 if (procState <= ActivityManager.PROCESS_STATE_TOP) {
175 return PROC_STATE_BUCKET_TOP;
176 }
177 if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
178 return PROC_STATE_BUCKET_FGS;
179 }
180 if (procState <= ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
181 return PROC_STATE_BUCKET_BFGS;
182 }
183 return PROC_STATE_BUCKET_BG;
184 }
185
186 @GuardedBy("mLock")
187 private void notifyStateChangedLocked(final int uid) {
188 // Never call out to the IRS with the local lock held.
189 TareHandlerThread.getHandler().post(() -> mIrs.onUidStateChanged(uid));
190 }
191 }