blob: 22a7de74cc382ce4332f495ddf1cbd2031a4b4bb [file] [log] [blame]
Dianne Hackbornd23e0d62015-05-15 16:36:12 -07001/*
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
17package com.android.server.am;
18
Sudheer Shankaf4923ea2017-03-27 15:36:35 -070019import android.Manifest;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -070020import android.app.ActivityManager;
Sudheer Shankaf4923ea2017-03-27 15:36:35 -070021import android.content.pm.PackageManager;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070022import android.os.SystemClock;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -070023import android.os.UserHandle;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070024import android.util.TimeUtils;
Yi Jin148d7f42017-11-28 14:23:56 -080025import android.util.proto.ProtoOutputStream;
26import android.util.proto.ProtoUtils;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -070027
Sudheer Shanka84a48952017-03-08 18:19:01 -080028import com.android.internal.annotations.GuardedBy;
Sudheer Shanka84a48952017-03-08 18:19:01 -080029
Dianne Hackbornd23e0d62015-05-15 16:36:12 -070030/**
31 * Overall information about a uid that has actively running processes.
32 */
33public final class UidRecord {
34 final int uid;
Wale Ogunwalebff2df42018-10-18 17:09:19 -070035 private int mCurProcState;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -070036 int setProcState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070037 long lastBackgroundTime;
Dianne Hackborne07641d2016-11-09 15:07:23 -080038 boolean ephemeral;
Dianne Hackbornbe68d492017-05-03 18:04:05 -070039 boolean foregroundServices;
Dianne Hackborn85e35642017-01-12 15:10:57 -080040 boolean curWhitelist;
41 boolean setWhitelist;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070042 boolean idle;
Dianne Hackborn3e99f652017-07-05 16:33:56 -070043 boolean setIdle;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -070044 int numProcs;
Sudheer Shanka51ab3ac2017-03-07 15:38:01 -080045
Sudheer Shanka88c4d1c2017-02-23 16:34:40 -080046 /**
Wale Ogunwalebff2df42018-10-18 17:09:19 -070047 * Sequence number associated with the {@link #mCurProcState}. This is incremented using
Sudheer Shanka88c4d1c2017-02-23 16:34:40 -080048 * {@link ActivityManagerService#mProcStateSeqCounter}
Wale Ogunwalebff2df42018-10-18 17:09:19 -070049 * when {@link #mCurProcState} changes from background to foreground or vice versa.
Sudheer Shanka88c4d1c2017-02-23 16:34:40 -080050 */
Sudheer Shankaf4923ea2017-03-27 15:36:35 -070051 @GuardedBy("networkStateUpdate")
Sudheer Shanka88c4d1c2017-02-23 16:34:40 -080052 long curProcStateSeq;
Sudheer Shanka51ab3ac2017-03-07 15:38:01 -080053
Sudheer Shankae7361852017-03-07 11:51:46 -080054 /**
55 * Last seq number for which NetworkPolicyManagerService notified ActivityManagerService that
56 * network policies rules were updated.
57 */
Sudheer Shankaf4923ea2017-03-27 15:36:35 -070058 @GuardedBy("networkStateUpdate")
Sudheer Shankae7361852017-03-07 11:51:46 -080059 long lastNetworkUpdatedProcStateSeq;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -070060
Sudheer Shanka51ab3ac2017-03-07 15:38:01 -080061 /**
62 * Last seq number for which AcitivityManagerService dispatched uid state change to
63 * NetworkPolicyManagerService.
64 */
Sudheer Shankaf4923ea2017-03-27 15:36:35 -070065 @GuardedBy("networkStateUpdate")
Sudheer Shanka51ab3ac2017-03-07 15:38:01 -080066 long lastDispatchedProcStateSeq;
67
Sudheer Shanka84a48952017-03-08 18:19:01 -080068 /**
69 * Indicates if any thread is waiting for network rules to get updated for {@link #uid}.
70 */
Sudheer Shankaf4923ea2017-03-27 15:36:35 -070071 volatile boolean waitingForNetwork;
Sudheer Shanka84a48952017-03-08 18:19:01 -080072
Sudheer Shankaf4923ea2017-03-27 15:36:35 -070073 /**
74 * Indicates whether this uid has internet permission or not.
75 */
76 volatile boolean hasInternetPermission;
77
78 /**
79 * This object is used for waiting for the network state to get updated.
80 */
81 final Object networkStateLock = new Object();
Sudheer Shanka84a48952017-03-08 18:19:01 -080082
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070083 static final int CHANGE_PROCSTATE = 0;
Dianne Hackborn3e99f652017-07-05 16:33:56 -070084 static final int CHANGE_GONE = 1<<0;
85 static final int CHANGE_IDLE = 1<<1;
86 static final int CHANGE_ACTIVE = 1<<2;
87 static final int CHANGE_CACHED = 1<<3;
88 static final int CHANGE_UNCACHED = 1<<4;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070089
Yi Jin148d7f42017-11-28 14:23:56 -080090 // Keep the enum lists in sync
91 private static int[] ORIG_ENUMS = new int[] {
92 CHANGE_GONE,
93 CHANGE_IDLE,
94 CHANGE_ACTIVE,
95 CHANGE_CACHED,
96 CHANGE_UNCACHED,
97 };
98 private static int[] PROTO_ENUMS = new int[] {
99 UidRecordProto.CHANGE_GONE,
100 UidRecordProto.CHANGE_IDLE,
101 UidRecordProto.CHANGE_ACTIVE,
102 UidRecordProto.CHANGE_CACHED,
103 UidRecordProto.CHANGE_UNCACHED,
104 };
105
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700106 static final class ChangeItem {
107 UidRecord uidRecord;
108 int uid;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700109 int change;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700110 int processState;
Dianne Hackborne07641d2016-11-09 15:07:23 -0800111 boolean ephemeral;
Sudheer Shanka80255802017-03-04 14:48:53 -0800112 long procStateSeq;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700113 }
114
115 ChangeItem pendingChange;
Dianne Hackborn3e99f652017-07-05 16:33:56 -0700116 int lastReportedChange;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700117
Riddle Hsud7088f82019-01-30 13:04:50 +0800118 public UidRecord(int _uid) {
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700119 uid = _uid;
Dianne Hackborn333394a2017-03-23 16:11:25 -0700120 idle = true;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700121 reset();
122 }
123
Wale Ogunwalebff2df42018-10-18 17:09:19 -0700124 public int getCurProcState() {
125 return mCurProcState;
126 }
127
128 public void setCurProcState(int curProcState) {
129 mCurProcState = curProcState;
Wale Ogunwalebff2df42018-10-18 17:09:19 -0700130 }
131
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700132 public void reset() {
Wale Ogunwalebff2df42018-10-18 17:09:19 -0700133 setCurProcState(ActivityManager.PROCESS_STATE_CACHED_EMPTY);
Dianne Hackbornbe68d492017-05-03 18:04:05 -0700134 foregroundServices = false;
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700135 }
136
Sudheer Shankaf4923ea2017-03-27 15:36:35 -0700137 public void updateHasInternetPermission() {
138 hasInternetPermission = ActivityManager.checkUidPermission(Manifest.permission.INTERNET,
139 uid) == PackageManager.PERMISSION_GRANTED;
140 }
141
Sudheer Shanka51ab3ac2017-03-07 15:38:01 -0800142 /**
Dianne Hackborn3e99f652017-07-05 16:33:56 -0700143 * If the change being dispatched is not CHANGE_GONE (not interested in
Sudheer Shanka51ab3ac2017-03-07 15:38:01 -0800144 * these changes), then update the {@link #lastDispatchedProcStateSeq} with
145 * {@link #curProcStateSeq}.
146 */
147 public void updateLastDispatchedProcStateSeq(int changeToDispatch) {
Dianne Hackborn3e99f652017-07-05 16:33:56 -0700148 if ((changeToDispatch & CHANGE_GONE) == 0) {
Sudheer Shanka51ab3ac2017-03-07 15:38:01 -0800149 lastDispatchedProcStateSeq = curProcStateSeq;
150 }
151 }
152
Yi Jin148d7f42017-11-28 14:23:56 -0800153
154 void writeToProto(ProtoOutputStream proto, long fieldId) {
155 long token = proto.start(fieldId);
Yi Jin148d7f42017-11-28 14:23:56 -0800156 proto.write(UidRecordProto.UID, uid);
Wale Ogunwalebff2df42018-10-18 17:09:19 -0700157 proto.write(UidRecordProto.CURRENT, ProcessList.makeProcStateProtoEnum(mCurProcState));
Yi Jin148d7f42017-11-28 14:23:56 -0800158 proto.write(UidRecordProto.EPHEMERAL, ephemeral);
159 proto.write(UidRecordProto.FG_SERVICES, foregroundServices);
160 proto.write(UidRecordProto.WHILELIST, curWhitelist);
161 ProtoUtils.toDuration(proto, UidRecordProto.LAST_BACKGROUND_TIME,
162 lastBackgroundTime, SystemClock.elapsedRealtime());
163 proto.write(UidRecordProto.IDLE, idle);
164 if (lastReportedChange != 0) {
165 ProtoUtils.writeBitWiseFlagsToProtoEnum(proto, UidRecordProto.LAST_REPORTED_CHANGES,
166 lastReportedChange, ORIG_ENUMS, PROTO_ENUMS);
167 }
168 proto.write(UidRecordProto.NUM_PROCS, numProcs);
169
170 long seqToken = proto.start(UidRecordProto.NETWORK_STATE_UPDATE);
171 proto.write(UidRecordProto.ProcStateSequence.CURURENT, curProcStateSeq);
172 proto.write(UidRecordProto.ProcStateSequence.LAST_NETWORK_UPDATED,
173 lastNetworkUpdatedProcStateSeq);
174 proto.write(UidRecordProto.ProcStateSequence.LAST_DISPATCHED, lastDispatchedProcStateSeq);
175 proto.end(seqToken);
176
177 proto.end(token);
178 }
179
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700180 public String toString() {
181 StringBuilder sb = new StringBuilder(128);
182 sb.append("UidRecord{");
183 sb.append(Integer.toHexString(System.identityHashCode(this)));
184 sb.append(' ');
185 UserHandle.formatUid(sb, uid);
186 sb.append(' ');
Wale Ogunwalebff2df42018-10-18 17:09:19 -0700187 sb.append(ProcessList.makeProcStateString(mCurProcState));
Dianne Hackborne07641d2016-11-09 15:07:23 -0800188 if (ephemeral) {
189 sb.append(" ephemeral");
190 }
Dianne Hackbornbe68d492017-05-03 18:04:05 -0700191 if (foregroundServices) {
192 sb.append(" fgServices");
193 }
Dianne Hackborn85e35642017-01-12 15:10:57 -0800194 if (curWhitelist) {
195 sb.append(" whitelist");
196 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700197 if (lastBackgroundTime > 0) {
198 sb.append(" bg:");
199 TimeUtils.formatDuration(SystemClock.elapsedRealtime()-lastBackgroundTime, sb);
200 }
201 if (idle) {
202 sb.append(" idle");
203 }
Dianne Hackborn3e99f652017-07-05 16:33:56 -0700204 if (lastReportedChange != 0) {
205 sb.append(" change:");
206 boolean printed = false;
207 if ((lastReportedChange & CHANGE_GONE) != 0) {
208 printed = true;
209 sb.append("gone");
210 }
211 if ((lastReportedChange & CHANGE_IDLE) != 0) {
212 if (printed) {
213 sb.append("|");
214 }
215 printed = true;
216 sb.append("idle");
217 }
218 if ((lastReportedChange & CHANGE_ACTIVE) != 0) {
219 if (printed) {
220 sb.append("|");
221 }
222 printed = true;
223 sb.append("active");
224 }
225 if ((lastReportedChange & CHANGE_CACHED) != 0) {
226 if (printed) {
227 sb.append("|");
228 }
229 printed = true;
230 sb.append("cached");
231 }
232 if ((lastReportedChange & CHANGE_UNCACHED) != 0) {
233 if (printed) {
234 sb.append("|");
235 }
236 sb.append("uncached");
237 }
238 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700239 sb.append(" procs:");
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700240 sb.append(numProcs);
Dianne Hackborn0aa43132017-03-22 11:42:03 -0700241 sb.append(" seq(");
Sudheer Shanka88c4d1c2017-02-23 16:34:40 -0800242 sb.append(curProcStateSeq);
Dianne Hackborn0aa43132017-03-22 11:42:03 -0700243 sb.append(",");
Sudheer Shankae7361852017-03-07 11:51:46 -0800244 sb.append(lastNetworkUpdatedProcStateSeq);
Dianne Hackborn0aa43132017-03-22 11:42:03 -0700245 sb.append(",");
Sudheer Shanka51ab3ac2017-03-07 15:38:01 -0800246 sb.append(lastDispatchedProcStateSeq);
Dianne Hackborn0aa43132017-03-22 11:42:03 -0700247 sb.append(")}");
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700248 return sb.toString();
249 }
250}