Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.am; |
| 18 | |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 22 | import android.os.SystemClock; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 23 | import android.os.UserHandle; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 24 | import android.util.TimeUtils; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 25 | import android.util.proto.ProtoOutputStream; |
| 26 | import android.util.proto.ProtoUtils; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 27 | |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 28 | import com.android.internal.annotations.GuardedBy; |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 29 | import com.android.server.wm.ActivityTaskManagerInternal; |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 30 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Overall information about a uid that has actively running processes. |
| 33 | */ |
| 34 | public final class UidRecord { |
| 35 | final int uid; |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 36 | private int mCurProcState; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 37 | int setProcState = ActivityManager.PROCESS_STATE_NONEXISTENT; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 38 | long lastBackgroundTime; |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 39 | boolean ephemeral; |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 40 | boolean foregroundServices; |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 41 | boolean curWhitelist; |
| 42 | boolean setWhitelist; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 43 | boolean idle; |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 44 | boolean setIdle; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 45 | int numProcs; |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 46 | final ActivityTaskManagerInternal mAtmInternal; |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 47 | |
Sudheer Shanka | 88c4d1c | 2017-02-23 16:34:40 -0800 | [diff] [blame] | 48 | /** |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 49 | * Sequence number associated with the {@link #mCurProcState}. This is incremented using |
Sudheer Shanka | 88c4d1c | 2017-02-23 16:34:40 -0800 | [diff] [blame] | 50 | * {@link ActivityManagerService#mProcStateSeqCounter} |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 51 | * when {@link #mCurProcState} changes from background to foreground or vice versa. |
Sudheer Shanka | 88c4d1c | 2017-02-23 16:34:40 -0800 | [diff] [blame] | 52 | */ |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 53 | @GuardedBy("networkStateUpdate") |
Sudheer Shanka | 88c4d1c | 2017-02-23 16:34:40 -0800 | [diff] [blame] | 54 | long curProcStateSeq; |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 55 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Last seq number for which NetworkPolicyManagerService notified ActivityManagerService that |
| 58 | * network policies rules were updated. |
| 59 | */ |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 60 | @GuardedBy("networkStateUpdate") |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 61 | long lastNetworkUpdatedProcStateSeq; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 62 | |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Last seq number for which AcitivityManagerService dispatched uid state change to |
| 65 | * NetworkPolicyManagerService. |
| 66 | */ |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 67 | @GuardedBy("networkStateUpdate") |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 68 | long lastDispatchedProcStateSeq; |
| 69 | |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Indicates if any thread is waiting for network rules to get updated for {@link #uid}. |
| 72 | */ |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 73 | volatile boolean waitingForNetwork; |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 74 | |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Indicates whether this uid has internet permission or not. |
| 77 | */ |
| 78 | volatile boolean hasInternetPermission; |
| 79 | |
| 80 | /** |
| 81 | * This object is used for waiting for the network state to get updated. |
| 82 | */ |
| 83 | final Object networkStateLock = new Object(); |
Sudheer Shanka | 84a4895 | 2017-03-08 18:19:01 -0800 | [diff] [blame] | 84 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 85 | static final int CHANGE_PROCSTATE = 0; |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 86 | static final int CHANGE_GONE = 1<<0; |
| 87 | static final int CHANGE_IDLE = 1<<1; |
| 88 | static final int CHANGE_ACTIVE = 1<<2; |
| 89 | static final int CHANGE_CACHED = 1<<3; |
| 90 | static final int CHANGE_UNCACHED = 1<<4; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 91 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 92 | // Keep the enum lists in sync |
| 93 | private static int[] ORIG_ENUMS = new int[] { |
| 94 | CHANGE_GONE, |
| 95 | CHANGE_IDLE, |
| 96 | CHANGE_ACTIVE, |
| 97 | CHANGE_CACHED, |
| 98 | CHANGE_UNCACHED, |
| 99 | }; |
| 100 | private static int[] PROTO_ENUMS = new int[] { |
| 101 | UidRecordProto.CHANGE_GONE, |
| 102 | UidRecordProto.CHANGE_IDLE, |
| 103 | UidRecordProto.CHANGE_ACTIVE, |
| 104 | UidRecordProto.CHANGE_CACHED, |
| 105 | UidRecordProto.CHANGE_UNCACHED, |
| 106 | }; |
| 107 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 108 | static final class ChangeItem { |
| 109 | UidRecord uidRecord; |
| 110 | int uid; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 111 | int change; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 112 | int processState; |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 113 | boolean ephemeral; |
Sudheer Shanka | 8025580 | 2017-03-04 14:48:53 -0800 | [diff] [blame] | 114 | long procStateSeq; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | ChangeItem pendingChange; |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 118 | int lastReportedChange; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 119 | |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 120 | public UidRecord(int _uid, ActivityTaskManagerInternal atmInternal) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 121 | uid = _uid; |
Dianne Hackborn | 333394a | 2017-03-23 16:11:25 -0700 | [diff] [blame] | 122 | idle = true; |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 123 | mAtmInternal = atmInternal; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 124 | reset(); |
| 125 | } |
| 126 | |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 127 | public int getCurProcState() { |
| 128 | return mCurProcState; |
| 129 | } |
| 130 | |
| 131 | public void setCurProcState(int curProcState) { |
| 132 | mCurProcState = curProcState; |
| 133 | if (mAtmInternal != null) { |
| 134 | mAtmInternal.onUidProcStateChanged(uid, curProcState); |
| 135 | } |
| 136 | } |
| 137 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 138 | public void reset() { |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 139 | setCurProcState(ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 140 | foregroundServices = false; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Sudheer Shanka | f4923ea | 2017-03-27 15:36:35 -0700 | [diff] [blame] | 143 | public void updateHasInternetPermission() { |
| 144 | hasInternetPermission = ActivityManager.checkUidPermission(Manifest.permission.INTERNET, |
| 145 | uid) == PackageManager.PERMISSION_GRANTED; |
| 146 | } |
| 147 | |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 148 | /** |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 149 | * If the change being dispatched is not CHANGE_GONE (not interested in |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 150 | * these changes), then update the {@link #lastDispatchedProcStateSeq} with |
| 151 | * {@link #curProcStateSeq}. |
| 152 | */ |
| 153 | public void updateLastDispatchedProcStateSeq(int changeToDispatch) { |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 154 | if ((changeToDispatch & CHANGE_GONE) == 0) { |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 155 | lastDispatchedProcStateSeq = curProcStateSeq; |
| 156 | } |
| 157 | } |
| 158 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 159 | |
| 160 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 161 | long token = proto.start(fieldId); |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 162 | proto.write(UidRecordProto.UID, uid); |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 163 | proto.write(UidRecordProto.CURRENT, ProcessList.makeProcStateProtoEnum(mCurProcState)); |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 164 | proto.write(UidRecordProto.EPHEMERAL, ephemeral); |
| 165 | proto.write(UidRecordProto.FG_SERVICES, foregroundServices); |
| 166 | proto.write(UidRecordProto.WHILELIST, curWhitelist); |
| 167 | ProtoUtils.toDuration(proto, UidRecordProto.LAST_BACKGROUND_TIME, |
| 168 | lastBackgroundTime, SystemClock.elapsedRealtime()); |
| 169 | proto.write(UidRecordProto.IDLE, idle); |
| 170 | if (lastReportedChange != 0) { |
| 171 | ProtoUtils.writeBitWiseFlagsToProtoEnum(proto, UidRecordProto.LAST_REPORTED_CHANGES, |
| 172 | lastReportedChange, ORIG_ENUMS, PROTO_ENUMS); |
| 173 | } |
| 174 | proto.write(UidRecordProto.NUM_PROCS, numProcs); |
| 175 | |
| 176 | long seqToken = proto.start(UidRecordProto.NETWORK_STATE_UPDATE); |
| 177 | proto.write(UidRecordProto.ProcStateSequence.CURURENT, curProcStateSeq); |
| 178 | proto.write(UidRecordProto.ProcStateSequence.LAST_NETWORK_UPDATED, |
| 179 | lastNetworkUpdatedProcStateSeq); |
| 180 | proto.write(UidRecordProto.ProcStateSequence.LAST_DISPATCHED, lastDispatchedProcStateSeq); |
| 181 | proto.end(seqToken); |
| 182 | |
| 183 | proto.end(token); |
| 184 | } |
| 185 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 186 | public String toString() { |
| 187 | StringBuilder sb = new StringBuilder(128); |
| 188 | sb.append("UidRecord{"); |
| 189 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 190 | sb.append(' '); |
| 191 | UserHandle.formatUid(sb, uid); |
| 192 | sb.append(' '); |
Wale Ogunwale | bff2df4 | 2018-10-18 17:09:19 -0700 | [diff] [blame] | 193 | sb.append(ProcessList.makeProcStateString(mCurProcState)); |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 194 | if (ephemeral) { |
| 195 | sb.append(" ephemeral"); |
| 196 | } |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 197 | if (foregroundServices) { |
| 198 | sb.append(" fgServices"); |
| 199 | } |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 200 | if (curWhitelist) { |
| 201 | sb.append(" whitelist"); |
| 202 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 203 | if (lastBackgroundTime > 0) { |
| 204 | sb.append(" bg:"); |
| 205 | TimeUtils.formatDuration(SystemClock.elapsedRealtime()-lastBackgroundTime, sb); |
| 206 | } |
| 207 | if (idle) { |
| 208 | sb.append(" idle"); |
| 209 | } |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 210 | if (lastReportedChange != 0) { |
| 211 | sb.append(" change:"); |
| 212 | boolean printed = false; |
| 213 | if ((lastReportedChange & CHANGE_GONE) != 0) { |
| 214 | printed = true; |
| 215 | sb.append("gone"); |
| 216 | } |
| 217 | if ((lastReportedChange & CHANGE_IDLE) != 0) { |
| 218 | if (printed) { |
| 219 | sb.append("|"); |
| 220 | } |
| 221 | printed = true; |
| 222 | sb.append("idle"); |
| 223 | } |
| 224 | if ((lastReportedChange & CHANGE_ACTIVE) != 0) { |
| 225 | if (printed) { |
| 226 | sb.append("|"); |
| 227 | } |
| 228 | printed = true; |
| 229 | sb.append("active"); |
| 230 | } |
| 231 | if ((lastReportedChange & CHANGE_CACHED) != 0) { |
| 232 | if (printed) { |
| 233 | sb.append("|"); |
| 234 | } |
| 235 | printed = true; |
| 236 | sb.append("cached"); |
| 237 | } |
| 238 | if ((lastReportedChange & CHANGE_UNCACHED) != 0) { |
| 239 | if (printed) { |
| 240 | sb.append("|"); |
| 241 | } |
| 242 | sb.append("uncached"); |
| 243 | } |
| 244 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 245 | sb.append(" procs:"); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 246 | sb.append(numProcs); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 247 | sb.append(" seq("); |
Sudheer Shanka | 88c4d1c | 2017-02-23 16:34:40 -0800 | [diff] [blame] | 248 | sb.append(curProcStateSeq); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 249 | sb.append(","); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 250 | sb.append(lastNetworkUpdatedProcStateSeq); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 251 | sb.append(","); |
Sudheer Shanka | 51ab3ac | 2017-03-07 15:38:01 -0800 | [diff] [blame] | 252 | sb.append(lastDispatchedProcStateSeq); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 253 | sb.append(")}"); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 254 | return sb.toString(); |
| 255 | } |
| 256 | } |