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.server.am;
18 
19 import static android.app.ActivityManager.PROCESS_CAPABILITY_NONE;
20 import static android.app.ActivityManager.PROCESS_STATE_CACHED_EMPTY;
21 import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
22 import static android.app.ActivityManagerInternal.OOM_ADJ_REASON_UI_VISIBILITY;
23 import static android.app.ProcessMemoryState.HOSTING_COMPONENT_TYPE_ACTIVITY;
24 import static android.app.ProcessMemoryState.HOSTING_COMPONENT_TYPE_BROADCAST_RECEIVER;
25 import static android.app.ProcessMemoryState.HOSTING_COMPONENT_TYPE_STARTED_SERVICE;
26 
27 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_OOM_ADJ;
28 import static com.android.server.am.ProcessList.CACHED_APP_MIN_ADJ;
29 import static com.android.server.am.ProcessRecord.TAG;
30 
31 import android.annotation.ElapsedRealtimeLong;
32 import android.app.ActivityManager;
33 import android.content.ComponentName;
34 import android.os.SystemClock;
35 import android.os.Trace;
36 import android.util.Slog;
37 import android.util.TimeUtils;
38 
39 import com.android.internal.annotations.CompositeRWLock;
40 import com.android.internal.annotations.GuardedBy;
41 import com.android.server.am.PlatformCompatCache.CachedCompatChangeId;
42 
43 import java.io.PrintWriter;
44 
45 /**
46  * The state info of the process, including proc state, oom adj score, et al.
47  */
48 final class ProcessStateRecord {
49     // Enable this to trace all OomAdjuster state transitions
50     private static final boolean TRACE_OOM_ADJ = false;
51 
52     private final ProcessRecord mApp;
53     private final ActivityManagerService mService;
54     private final ActivityManagerGlobalLock mProcLock;
55 
56     /**
57      * Maximum OOM adjustment for this process.
58      */
59     @GuardedBy("mService")
60     private int mMaxAdj = ProcessList.UNKNOWN_ADJ;
61 
62     /**
63      *  Current OOM unlimited adjustment for this process.
64      */
65     @CompositeRWLock({"mService", "mProcLock"})
66     private int mCurRawAdj = ProcessList.INVALID_ADJ;
67 
68     /**
69      * Last set OOM unlimited adjustment for this process.
70      */
71     @CompositeRWLock({"mService", "mProcLock"})
72     private int mSetRawAdj = ProcessList.INVALID_ADJ;
73 
74     /**
75      * Current OOM adjustment for this process.
76      */
77     @CompositeRWLock({"mService", "mProcLock"})
78     private int mCurAdj = ProcessList.INVALID_ADJ;
79 
80     /**
81      * Last set OOM adjustment for this process.
82      */
83     @CompositeRWLock({"mService", "mProcLock"})
84     private int mSetAdj = ProcessList.INVALID_ADJ;
85 
86     /**
87      * The last adjustment that was verified as actually being set.
88      */
89     @GuardedBy("mService")
90     private int mVerifiedAdj = ProcessList.INVALID_ADJ;
91 
92     /**
93      * Current capability flags of this process.
94      * For example, PROCESS_CAPABILITY_FOREGROUND_LOCATION is one capability.
95      */
96     @CompositeRWLock({"mService", "mProcLock"})
97     private int mCurCapability = PROCESS_CAPABILITY_NONE;
98 
99     /**
100      * Last set capability flags.
101      */
102     @CompositeRWLock({"mService", "mProcLock"})
103     private int mSetCapability = PROCESS_CAPABILITY_NONE;
104 
105     /**
106      * Currently desired scheduling class.
107      */
108     @CompositeRWLock({"mService", "mProcLock"})
109     private int mCurSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
110 
111     /**
112      * Last set to background scheduling class.
113      */
114     @CompositeRWLock({"mService", "mProcLock"})
115     private int mSetSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
116 
117     /**
118      * Currently computed process state.
119      */
120     @CompositeRWLock({"mService", "mProcLock"})
121     private int mCurProcState = PROCESS_STATE_NONEXISTENT;
122 
123     /**
124      * Last reported process state.
125      */
126     @CompositeRWLock({"mService", "mProcLock"})
127     private int mRepProcState = PROCESS_STATE_NONEXISTENT;
128 
129     /**
130      * Temp state during computation.
131      */
132     @CompositeRWLock({"mService", "mProcLock"})
133     private int mCurRawProcState = PROCESS_STATE_NONEXISTENT;
134 
135     /**
136      * Last set process state in process tracker.
137      */
138     @CompositeRWLock({"mService", "mProcLock"})
139     private int mSetProcState = PROCESS_STATE_NONEXISTENT;
140 
141     /**
142      * Last time mSetProcState changed.
143      */
144     @CompositeRWLock({"mService", "mProcLock"})
145     private long mLastStateTime;
146 
147     /**
148      * Previous priority value if we're switching to non-SCHED_OTHER.
149      */
150     @CompositeRWLock({"mService", "mProcLock"})
151     private int mSavedPriority;
152 
153     /**
154      * Process currently is on the service B list.
155      */
156     @CompositeRWLock({"mService", "mProcLock"})
157     private boolean mServiceB;
158 
159     /**
160      * We are forcing to service B list due to its RAM use.
161      */
162     @CompositeRWLock({"mService", "mProcLock"})
163     private boolean mServiceHighRam;
164 
165     /**
166      * Has this process not been in a cached state since last idle?
167      */
168     @GuardedBy("mProcLock")
169     private boolean mNotCachedSinceIdle;
170 
171     /**
172      * Are there any started services running in this process?
173      */
174     @CompositeRWLock({"mService", "mProcLock"})
175     private boolean mHasStartedServices;
176 
177     /**
178      * Running any activities that are foreground?
179      */
180     @CompositeRWLock({"mService", "mProcLock"})
181     private boolean mHasForegroundActivities;
182 
183     /**
184      * Last reported foreground activities.
185      */
186     @CompositeRWLock({"mService", "mProcLock"})
187     private boolean mRepForegroundActivities;
188 
189     /**
190      * Has UI been shown in this process since it was started?
191      */
192     @GuardedBy("mService")
193     private boolean mHasShownUi;
194 
195     /**
196      * Is this process currently showing a non-activity UI that the user
197      * is interacting with? E.g. The status bar when it is expanded, but
198      * not when it is minimized. When true the
199      * process will be set to use the ProcessList#SCHED_GROUP_TOP_APP
200      * scheduling group to boost performance.
201      */
202     @GuardedBy("mService")
203     private boolean mHasTopUi;
204 
205     /**
206      * Is the process currently showing a non-activity UI that
207      * overlays on-top of activity UIs on screen. E.g. display a window
208      * of type android.view.WindowManager.LayoutParams#TYPE_APPLICATION_OVERLAY
209      * When true the process will oom adj score will be set to
210      * ProcessList#PERCEPTIBLE_APP_ADJ at minimum to reduce the chance
211      * of the process getting killed.
212      */
213     @GuardedBy("mService")
214     private boolean mHasOverlayUi;
215 
216     /**
217      * Is the process currently running a RemoteAnimation? When true
218      * the process will be set to use the
219      * ProcessList#SCHED_GROUP_TOP_APP scheduling group to boost
220      * performance, as well as oom adj score will be set to
221      * ProcessList#VISIBLE_APP_ADJ at minimum to reduce the chance
222      * of the process getting killed.
223      */
224     @GuardedBy("mService")
225     private boolean mRunningRemoteAnimation;
226 
227     /**
228      * Keep track of whether we changed 'mSetAdj'.
229      */
230     @CompositeRWLock({"mService", "mProcLock"})
231     private boolean mProcStateChanged;
232 
233     /**
234      * Whether we have told usage stats about it being an interaction.
235      */
236     @CompositeRWLock({"mService", "mProcLock"})
237     private boolean mReportedInteraction;
238 
239     /**
240      * The time we sent the last interaction event.
241      */
242     @CompositeRWLock({"mService", "mProcLock"})
243     private long mInteractionEventTime;
244 
245     /**
246      * When we became foreground for interaction purposes.
247      */
248     @CompositeRWLock({"mService", "mProcLock"})
249     private long mFgInteractionTime;
250 
251     /**
252      * Token that is forcing this process to be important.
253      */
254     @GuardedBy("mService")
255     private Object mForcingToImportant;
256 
257     /**
258      * Sequence id for identifying oom_adj assignment cycles.
259      */
260     @GuardedBy("mService")
261     private int mAdjSeq;
262 
263     /**
264      * Sequence id for identifying oom_adj assignment cycles.
265      */
266     @GuardedBy("mService")
267     private int mCompletedAdjSeq;
268 
269     /**
270      * Whether this app has encountered a cycle in the most recent update.
271      */
272     @GuardedBy("mService")
273     private boolean mContainsCycle;
274 
275     /**
276      * When (uptime) the process last became unimportant.
277      */
278     @CompositeRWLock({"mService", "mProcLock"})
279     private long mWhenUnimportant;
280 
281     /**
282      * The last time the process was in the TOP state or greater.
283      */
284     @GuardedBy("mService")
285     private long mLastTopTime = Long.MIN_VALUE;
286 
287     /**
288      * This is a system process, but not currently showing UI.
289      */
290     @GuardedBy("mService")
291     private boolean mSystemNoUi;
292 
293     /**
294      * Whether or not the app is background restricted (OP_RUN_ANY_IN_BACKGROUND is NOT allowed).
295      */
296     @GuardedBy("mService")
297     private boolean mBackgroundRestricted = false;
298 
299     /**
300      * Whether or not this process is being bound by a non-background restricted app.
301      */
302     @GuardedBy("mService")
303     private boolean mCurBoundByNonBgRestrictedApp = false;
304 
305     /**
306      * Last set state of {@link #mCurBoundByNonBgRestrictedApp}.
307      */
308     private boolean mSetBoundByNonBgRestrictedApp = false;
309 
310     /**
311      * Debugging: primary thing impacting oom_adj.
312      */
313     @GuardedBy("mService")
314     private String mAdjType;
315 
316     /**
317      * Debugging: adj code to report to app.
318      */
319     @CompositeRWLock({"mService", "mProcLock"})
320     private int mAdjTypeCode;
321 
322     /**
323      * Debugging: option dependent object.
324      */
325     @CompositeRWLock({"mService", "mProcLock"})
326     private Object mAdjSource;
327 
328     /**
329      * Debugging: proc state of mAdjSource's process.
330      */
331     @CompositeRWLock({"mService", "mProcLock"})
332     private int mAdjSourceProcState;
333 
334     /**
335      * Debugging: target component impacting oom_adj.
336      */
337     @CompositeRWLock({"mService", "mProcLock"})
338     private Object mAdjTarget;
339 
340     /**
341      * Approximates the usage count of the app, used for cache re-ranking by CacheOomRanker.
342      *
343      * Counts the number of times the process is re-added to the cache (i.e. setCached(false);
344      * setCached(true)). This over counts, as setCached is sometimes reset while remaining in the
345      * cache. However, this happens uniformly across processes, so ranking is not affected.
346      */
347     @GuardedBy("mService")
348     private int mCacheOomRankerUseCount;
349 
350     /**
351      * Process memory usage (RSS).
352      *
353      * Periodically populated by {@code CacheOomRanker}, stored in this object to cache the values.
354      */
355     @GuardedBy("mService")
356     private long mCacheOomRankerRss;
357 
358     /**
359      * The last time, in milliseconds since boot, since {@link #mCacheOomRankerRss} was updated.
360      */
361     @GuardedBy("mService")
362     private long mCacheOomRankerRssTimeMs;
363 
364     /**
365      * Whether or not this process is reachable from given process.
366      */
367     @GuardedBy("mService")
368     private boolean mReachable;
369 
370     /**
371      * The most recent time when the last visible activity within this process became invisible.
372      *
373      * <p> It'll be set to 0 if there is never a visible activity, or Long.MAX_VALUE if there is
374      * any visible activities within this process at this moment.</p>
375      */
376     @GuardedBy("mService")
377     @ElapsedRealtimeLong
378     private long mLastInvisibleTime;
379 
380     /**
381      * Whether or not this process could be killed when it's in background restricted mode
382      * and cached &amp; idle state.
383      */
384     @GuardedBy("mService")
385     private boolean mNoKillOnBgRestrictedAndIdle;
386 
387     /**
388      * Last set value of {@link #isCached()}.
389      */
390     @GuardedBy("mService")
391     private boolean mSetCached;
392 
393     /**
394      * Last set value of {@link #mNoKillOnBgRestrictedAndIdle}.
395      */
396     @GuardedBy("mService")
397     private boolean mSetNoKillOnBgRestrictedAndIdle;
398 
399     /**
400      * The last time when the {@link #mNoKillOnBgRestrictedAndIdle} is false and the
401      * {@link #isCached()} is true, and either the former state is flipping from true to false
402      * when latter state is true, or the latter state is flipping from false to true when the
403      * former state is false.
404      */
405     @GuardedBy("mService")
406     private @ElapsedRealtimeLong long mLastCanKillOnBgRestrictedAndIdleTime;
407 
408     // Below are the cached task info for OomAdjuster only
409     private static final int VALUE_INVALID = -1;
410     private static final int VALUE_FALSE = 0;
411     private static final int VALUE_TRUE = 1;
412 
413     @GuardedBy("mService")
414     private int mCachedHasActivities = VALUE_INVALID;
415     @GuardedBy("mService")
416     private int mCachedIsHeavyWeight = VALUE_INVALID;
417     @GuardedBy("mService")
418     private int mCachedHasVisibleActivities = VALUE_INVALID;
419     @GuardedBy("mService")
420     private int mCachedIsHomeProcess = VALUE_INVALID;
421     @GuardedBy("mService")
422     private int mCachedIsPreviousProcess = VALUE_INVALID;
423     @GuardedBy("mService")
424     private int mCachedHasRecentTasks = VALUE_INVALID;
425     @GuardedBy("mService")
426     private int mCachedIsReceivingBroadcast = VALUE_INVALID;
427 
428     /**
429      * Cache the return value of PlatformCompat.isChangeEnabled().
430      */
431     @GuardedBy("mService")
432     private int[] mCachedCompatChanges = new int[] {
433         VALUE_INVALID, // CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY
434         VALUE_INVALID, // CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY
435         VALUE_INVALID, // CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME
436     };
437 
438     @GuardedBy("mService")
439     private String mCachedAdjType = null;
440     @GuardedBy("mService")
441     private int mCachedAdj = ProcessList.INVALID_ADJ;
442     @GuardedBy("mService")
443     private boolean mCachedForegroundActivities = false;
444     @GuardedBy("mService")
445     private int mCachedProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
446     @GuardedBy("mService")
447     private int mCachedSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
448 
449     @GuardedBy("mService")
450     private boolean mScheduleLikeTopApp = false;
451 
452     @GuardedBy("mService")
453     private long mFollowupUpdateUptimeMs = Long.MAX_VALUE;
454 
ProcessStateRecord(ProcessRecord app)455     ProcessStateRecord(ProcessRecord app) {
456         mApp = app;
457         mService = app.mService;
458         mProcLock = mService.mProcLock;
459     }
460 
init(long now)461     void init(long now) {
462         mLastStateTime = now;
463     }
464 
465     @GuardedBy("mService")
setMaxAdj(int maxAdj)466     void setMaxAdj(int maxAdj) {
467         mMaxAdj = maxAdj;
468     }
469 
470     @GuardedBy("mService")
getMaxAdj()471     int getMaxAdj() {
472         return mMaxAdj;
473     }
474 
475     @GuardedBy({"mService", "mProcLock"})
setCurRawAdj(int curRawAdj)476     void setCurRawAdj(int curRawAdj) {
477         setCurRawAdj(curRawAdj, false);
478     }
479 
480     /**
481      * @return {@code true} if it's a dry run and it's going to bump the adj score of the process
482      * if it was a real run.
483      */
484     @GuardedBy({"mService", "mProcLock"})
setCurRawAdj(int curRawAdj, boolean dryRun)485     boolean setCurRawAdj(int curRawAdj, boolean dryRun) {
486         if (dryRun) {
487             return mCurRawAdj > curRawAdj;
488         }
489         mCurRawAdj = curRawAdj;
490         mApp.getWindowProcessController().setPerceptible(
491                 curRawAdj <= ProcessList.PERCEPTIBLE_APP_ADJ);
492         return false;
493     }
494 
495     @GuardedBy(anyOf = {"mService", "mProcLock"})
getCurRawAdj()496     int getCurRawAdj() {
497         return mCurRawAdj;
498     }
499 
500     @GuardedBy({"mService", "mProcLock"})
setSetRawAdj(int setRawAdj)501     void setSetRawAdj(int setRawAdj) {
502         mSetRawAdj = setRawAdj;
503     }
504 
505     @GuardedBy(anyOf = {"mService", "mProcLock"})
getSetRawAdj()506     int getSetRawAdj() {
507         return mSetRawAdj;
508     }
509 
510     @GuardedBy({"mService", "mProcLock"})
setCurAdj(int curAdj)511     void setCurAdj(int curAdj) {
512         mCurAdj = curAdj;
513         mApp.getWindowProcessController().setCurrentAdj(curAdj);
514     }
515 
516     @GuardedBy(anyOf = {"mService", "mProcLock"})
getCurAdj()517     int getCurAdj() {
518         return mCurAdj;
519     }
520 
521     @GuardedBy({"mService", "mProcLock"})
setSetAdj(int setAdj)522     void setSetAdj(int setAdj) {
523         mSetAdj = setAdj;
524     }
525 
526     @GuardedBy(anyOf = {"mService", "mProcLock"})
getSetAdj()527     int getSetAdj() {
528         return mSetAdj;
529     }
530 
531     @GuardedBy(anyOf = {"mService", "mProcLock"})
getSetAdjWithServices()532     int getSetAdjWithServices() {
533         if (mSetAdj >= CACHED_APP_MIN_ADJ) {
534             if (mHasStartedServices) {
535                 return ProcessList.SERVICE_B_ADJ;
536             }
537         }
538         return mSetAdj;
539     }
540 
541     @GuardedBy("mService")
setVerifiedAdj(int verifiedAdj)542     void setVerifiedAdj(int verifiedAdj) {
543         mVerifiedAdj = verifiedAdj;
544     }
545 
546     @GuardedBy("mService")
getVerifiedAdj()547     int getVerifiedAdj() {
548         return mVerifiedAdj;
549     }
550 
551     @GuardedBy({"mService", "mProcLock"})
setCurCapability(int curCapability)552     void setCurCapability(int curCapability) {
553         mCurCapability = curCapability;
554     }
555 
556     @GuardedBy(anyOf = {"mService", "mProcLock"})
getCurCapability()557     int getCurCapability() {
558         return mCurCapability;
559     }
560 
561     @GuardedBy({"mService", "mProcLock"})
setSetCapability(int setCapability)562     void setSetCapability(int setCapability) {
563         mSetCapability = setCapability;
564     }
565 
566     @GuardedBy(anyOf = {"mService", "mProcLock"})
getSetCapability()567     int getSetCapability() {
568         return mSetCapability;
569     }
570 
571     @GuardedBy({"mService", "mProcLock"})
setCurrentSchedulingGroup(int curSchedGroup)572     void setCurrentSchedulingGroup(int curSchedGroup) {
573         mCurSchedGroup = curSchedGroup;
574         mApp.getWindowProcessController().setCurrentSchedulingGroup(curSchedGroup);
575     }
576 
577     @GuardedBy(anyOf = {"mService", "mProcLock"})
getCurrentSchedulingGroup()578     int getCurrentSchedulingGroup() {
579         return mCurSchedGroup;
580     }
581 
582     @GuardedBy({"mService", "mProcLock"})
setSetSchedGroup(int setSchedGroup)583     void setSetSchedGroup(int setSchedGroup) {
584         mSetSchedGroup = setSchedGroup;
585     }
586 
587     @GuardedBy(anyOf = {"mService", "mProcLock"})
getSetSchedGroup()588     int getSetSchedGroup() {
589         return mSetSchedGroup;
590     }
591 
592     @GuardedBy({"mService", "mProcLock"})
setCurProcState(int curProcState)593     void setCurProcState(int curProcState) {
594         mCurProcState = curProcState;
595         mApp.getWindowProcessController().setCurrentProcState(mCurProcState);
596     }
597 
598     @GuardedBy(anyOf = {"mService", "mProcLock"})
getCurProcState()599     int getCurProcState() {
600         return mCurProcState;
601     }
602 
603     @GuardedBy({"mService", "mProcLock"})
setCurRawProcState(int curRawProcState)604     void setCurRawProcState(int curRawProcState) {
605         setCurRawProcState(curRawProcState, false);
606     }
607 
608     /**
609      * @return {@code true} if it's a dry run and it's going to bump the procstate of the process
610      * if it was a real run.
611      */
612     @GuardedBy({"mService", "mProcLock"})
setCurRawProcState(int curRawProcState, boolean dryRun)613     boolean setCurRawProcState(int curRawProcState, boolean dryRun) {
614         if (dryRun) {
615             return mCurRawProcState > curRawProcState;
616         }
617         mCurRawProcState = curRawProcState;
618         return false;
619     }
620 
621     @GuardedBy(anyOf = {"mService", "mProcLock"})
getCurRawProcState()622     int getCurRawProcState() {
623         return mCurRawProcState;
624     }
625 
626     @GuardedBy({"mService", "mProcLock"})
setReportedProcState(int repProcState)627     void setReportedProcState(int repProcState) {
628         mRepProcState = repProcState;
629         mApp.getWindowProcessController().setReportedProcState(repProcState);
630     }
631 
632     @GuardedBy(anyOf = {"mService", "mProcLock"})
getReportedProcState()633     int getReportedProcState() {
634         return mRepProcState;
635     }
636 
637     @GuardedBy("mService")
forceProcessStateUpTo(int newState)638     void forceProcessStateUpTo(int newState) {
639         if (mRepProcState > newState) {
640             synchronized (mProcLock) {
641                 final int prevProcState = mRepProcState;
642                 setReportedProcState(newState);
643                 setCurProcState(newState);
644                 setCurRawProcState(newState);
645                 mService.mOomAdjuster.onProcessStateChanged(mApp, prevProcState);
646             }
647         }
648     }
649 
650     @GuardedBy({"mService", "mProcLock"})
setSetProcState(int setProcState)651     void setSetProcState(int setProcState) {
652         if (ActivityManager.isProcStateCached(mSetProcState)
653                 && !ActivityManager.isProcStateCached(setProcState)) {
654             mCacheOomRankerUseCount++;
655         }
656         mSetProcState = setProcState;
657     }
658 
659     @GuardedBy(anyOf = {"mService", "mProcLock"})
getSetProcState()660     int getSetProcState() {
661         return mSetProcState;
662     }
663 
664     @GuardedBy({"mService", "mProcLock"})
setLastStateTime(long lastStateTime)665     void setLastStateTime(long lastStateTime) {
666         mLastStateTime = lastStateTime;
667     }
668 
669     @GuardedBy(anyOf = {"mService", "mProcLock"})
getLastStateTime()670     long getLastStateTime() {
671         return mLastStateTime;
672     }
673 
674     @GuardedBy({"mService", "mProcLock"})
setSavedPriority(int savedPriority)675     void setSavedPriority(int savedPriority) {
676         mSavedPriority = savedPriority;
677     }
678 
679     @GuardedBy(anyOf = {"mService", "mProcLock"})
getSavedPriority()680     int getSavedPriority() {
681         return mSavedPriority;
682     }
683 
684     @GuardedBy({"mService", "mProcLock"})
setServiceB(boolean serviceb)685     void setServiceB(boolean serviceb) {
686         mServiceB = serviceb;
687     }
688 
689     @GuardedBy(anyOf = {"mService", "mProcLock"})
isServiceB()690     boolean isServiceB() {
691         return mServiceB;
692     }
693 
694     @GuardedBy({"mService", "mProcLock"})
setServiceHighRam(boolean serviceHighRam)695     void setServiceHighRam(boolean serviceHighRam) {
696         mServiceHighRam = serviceHighRam;
697     }
698 
699     @GuardedBy(anyOf = {"mService", "mProcLock"})
isServiceHighRam()700     boolean isServiceHighRam() {
701         return mServiceHighRam;
702     }
703 
704     @GuardedBy("mProcLock")
setNotCachedSinceIdle(boolean notCachedSinceIdle)705     void setNotCachedSinceIdle(boolean notCachedSinceIdle) {
706         mNotCachedSinceIdle = notCachedSinceIdle;
707     }
708 
709     @GuardedBy("mProcLock")
isNotCachedSinceIdle()710     boolean isNotCachedSinceIdle() {
711         return mNotCachedSinceIdle;
712     }
713 
714     @GuardedBy("mProcLock")
setHasStartedServices(boolean hasStartedServices)715     void setHasStartedServices(boolean hasStartedServices) {
716         mHasStartedServices = hasStartedServices;
717         if (hasStartedServices) {
718             mApp.mProfile.addHostingComponentType(HOSTING_COMPONENT_TYPE_STARTED_SERVICE);
719         } else {
720             mApp.mProfile.clearHostingComponentType(HOSTING_COMPONENT_TYPE_STARTED_SERVICE);
721         }
722     }
723 
724     @GuardedBy("mProcLock")
hasStartedServices()725     boolean hasStartedServices() {
726         return mHasStartedServices;
727     }
728 
729     @GuardedBy({"mService", "mProcLock"})
setHasForegroundActivities(boolean hasForegroundActivities)730     void setHasForegroundActivities(boolean hasForegroundActivities) {
731         mHasForegroundActivities = hasForegroundActivities;
732     }
733 
734     @GuardedBy(anyOf = {"mService", "mProcLock"})
hasForegroundActivities()735     boolean hasForegroundActivities() {
736         return mHasForegroundActivities;
737     }
738 
739     @GuardedBy({"mService", "mProcLock"})
setRepForegroundActivities(boolean repForegroundActivities)740     void setRepForegroundActivities(boolean repForegroundActivities) {
741         mRepForegroundActivities = repForegroundActivities;
742     }
743 
744     @GuardedBy(anyOf = {"mService", "mProcLock"})
hasRepForegroundActivities()745     boolean hasRepForegroundActivities() {
746         return mRepForegroundActivities;
747     }
748 
749     @GuardedBy("mService")
setHasShownUi(boolean hasShownUi)750     void setHasShownUi(boolean hasShownUi) {
751         mHasShownUi = hasShownUi;
752     }
753 
754     @GuardedBy("mService")
hasShownUi()755     boolean hasShownUi() {
756         return mHasShownUi;
757     }
758 
759     @GuardedBy("mService")
setHasTopUi(boolean hasTopUi)760     void setHasTopUi(boolean hasTopUi) {
761         mHasTopUi = hasTopUi;
762         mApp.getWindowProcessController().setHasTopUi(hasTopUi);
763     }
764 
765     @GuardedBy("mService")
hasTopUi()766     boolean hasTopUi() {
767         return mHasTopUi;
768     }
769 
770     @GuardedBy("mService")
setHasOverlayUi(boolean hasOverlayUi)771     void setHasOverlayUi(boolean hasOverlayUi) {
772         mHasOverlayUi = hasOverlayUi;
773         mApp.getWindowProcessController().setHasOverlayUi(hasOverlayUi);
774     }
775 
776     @GuardedBy("mService")
hasOverlayUi()777     boolean hasOverlayUi() {
778         return mHasOverlayUi;
779     }
780 
781     @GuardedBy("mService")
isRunningRemoteAnimation()782     boolean isRunningRemoteAnimation() {
783         return mRunningRemoteAnimation;
784     }
785 
786     @GuardedBy("mService")
setRunningRemoteAnimation(boolean runningRemoteAnimation)787     void setRunningRemoteAnimation(boolean runningRemoteAnimation) {
788         if (mRunningRemoteAnimation == runningRemoteAnimation) {
789             return;
790         }
791         mRunningRemoteAnimation = runningRemoteAnimation;
792         if (DEBUG_OOM_ADJ) {
793             Slog.i(TAG, "Setting runningRemoteAnimation=" + runningRemoteAnimation
794                     + " for pid=" + mApp.getPid());
795         }
796         mService.updateOomAdjLocked(mApp, OOM_ADJ_REASON_UI_VISIBILITY);
797     }
798 
799     @GuardedBy({"mService", "mProcLock"})
setProcStateChanged(boolean procStateChanged)800     void setProcStateChanged(boolean procStateChanged) {
801         mProcStateChanged = procStateChanged;
802     }
803 
804     @GuardedBy(anyOf = {"mService", "mProcLock"})
hasProcStateChanged()805     boolean hasProcStateChanged() {
806         return mProcStateChanged;
807     }
808 
809     @GuardedBy({"mService", "mProcLock"})
setReportedInteraction(boolean reportedInteraction)810     void setReportedInteraction(boolean reportedInteraction) {
811         mReportedInteraction = reportedInteraction;
812     }
813 
814     @GuardedBy(anyOf = {"mService", "mProcLock"})
hasReportedInteraction()815     boolean hasReportedInteraction() {
816         return mReportedInteraction;
817     }
818 
819     @GuardedBy({"mService", "mProcLock"})
setInteractionEventTime(long interactionEventTime)820     void setInteractionEventTime(long interactionEventTime) {
821         mInteractionEventTime = interactionEventTime;
822         mApp.getWindowProcessController().setInteractionEventTime(interactionEventTime);
823     }
824 
825     @GuardedBy(anyOf = {"mService", "mProcLock"})
getInteractionEventTime()826     long getInteractionEventTime() {
827         return mInteractionEventTime;
828     }
829 
830     @GuardedBy({"mService", "mProcLock"})
setFgInteractionTime(long fgInteractionTime)831     void setFgInteractionTime(long fgInteractionTime) {
832         mFgInteractionTime = fgInteractionTime;
833         mApp.getWindowProcessController().setFgInteractionTime(fgInteractionTime);
834     }
835 
836     @GuardedBy(anyOf = {"mService", "mProcLock"})
getFgInteractionTime()837     long getFgInteractionTime() {
838         return mFgInteractionTime;
839     }
840 
841     @GuardedBy("mService")
setForcingToImportant(Object forcingToImportant)842     void setForcingToImportant(Object forcingToImportant) {
843         mForcingToImportant = forcingToImportant;
844     }
845 
846     @GuardedBy("mService")
getForcingToImportant()847     Object getForcingToImportant() {
848         return mForcingToImportant;
849     }
850 
851     @GuardedBy("mService")
setAdjSeq(int adjSeq)852     void setAdjSeq(int adjSeq) {
853         mAdjSeq = adjSeq;
854     }
855 
856     @GuardedBy("mService")
decAdjSeq()857     void decAdjSeq() {
858         mAdjSeq--;
859     }
860 
861     @GuardedBy("mService")
getAdjSeq()862     int getAdjSeq() {
863         return mAdjSeq;
864     }
865 
866     @GuardedBy("mService")
setCompletedAdjSeq(int completedAdjSeq)867     void setCompletedAdjSeq(int completedAdjSeq) {
868         mCompletedAdjSeq = completedAdjSeq;
869     }
870 
871     @GuardedBy("mService")
decCompletedAdjSeq()872     void decCompletedAdjSeq() {
873         mCompletedAdjSeq--;
874     }
875 
876     @GuardedBy("mService")
getCompletedAdjSeq()877     int getCompletedAdjSeq() {
878         return mCompletedAdjSeq;
879     }
880 
881     @GuardedBy("mService")
setContainsCycle(boolean containsCycle)882     void setContainsCycle(boolean containsCycle) {
883         mContainsCycle = containsCycle;
884     }
885 
886     @GuardedBy("mService")
containsCycle()887     boolean containsCycle() {
888         return mContainsCycle;
889     }
890 
891     @GuardedBy({"mService", "mProcLock"})
setWhenUnimportant(long whenUnimportant)892     void setWhenUnimportant(long whenUnimportant) {
893         mWhenUnimportant = whenUnimportant;
894         mApp.getWindowProcessController().setWhenUnimportant(whenUnimportant);
895     }
896 
897     @GuardedBy(anyOf = {"mService", "mProcLock"})
getWhenUnimportant()898     long getWhenUnimportant() {
899         return mWhenUnimportant;
900     }
901 
902     @GuardedBy("mService")
setLastTopTime(long lastTopTime)903     void setLastTopTime(long lastTopTime) {
904         mLastTopTime = lastTopTime;
905     }
906 
907     @GuardedBy("mService")
getLastTopTime()908     long getLastTopTime() {
909         return mLastTopTime;
910     }
911 
912     @GuardedBy("mService")
isEmpty()913     boolean isEmpty() {
914         return mCurProcState >= PROCESS_STATE_CACHED_EMPTY;
915     }
916 
917     @GuardedBy("mService")
isCached()918     boolean isCached() {
919         return mCurAdj >= CACHED_APP_MIN_ADJ;
920     }
921 
922     @GuardedBy("mService")
getCacheOomRankerUseCount()923     int getCacheOomRankerUseCount() {
924         return mCacheOomRankerUseCount;
925     }
926 
927     @GuardedBy("mService")
setSystemNoUi(boolean systemNoUi)928     void setSystemNoUi(boolean systemNoUi) {
929         mSystemNoUi = systemNoUi;
930     }
931 
932     @GuardedBy("mService")
isSystemNoUi()933     boolean isSystemNoUi() {
934         return mSystemNoUi;
935     }
936 
937     @GuardedBy("mService")
setAdjType(String adjType)938     void setAdjType(String adjType) {
939         if (TRACE_OOM_ADJ) {
940             Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
941                     "oom:" + mApp.processName + "/u" + mApp.uid, 0);
942             Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
943                     "oom:" + mApp.processName + "/u" + mApp.uid, adjType, 0);
944         }
945         mAdjType = adjType;
946     }
947 
948     @GuardedBy("mService")
getAdjType()949     String getAdjType() {
950         return mAdjType;
951     }
952 
953     @GuardedBy({"mService", "mProcLock"})
setAdjTypeCode(int adjTypeCode)954     void setAdjTypeCode(int adjTypeCode) {
955         mAdjTypeCode = adjTypeCode;
956     }
957 
958     @GuardedBy(anyOf = {"mService", "mProcLock"})
getAdjTypeCode()959     int getAdjTypeCode() {
960         return mAdjTypeCode;
961     }
962 
963     @GuardedBy({"mService", "mProcLock"})
setAdjSource(Object adjSource)964     void setAdjSource(Object adjSource) {
965         mAdjSource = adjSource;
966     }
967 
968     @GuardedBy(anyOf = {"mService", "mProcLock"})
getAdjSource()969     Object getAdjSource() {
970         return mAdjSource;
971     }
972 
973     @GuardedBy({"mService", "mProcLock"})
setAdjSourceProcState(int adjSourceProcState)974     void setAdjSourceProcState(int adjSourceProcState) {
975         mAdjSourceProcState = adjSourceProcState;
976     }
977 
978     @GuardedBy(anyOf = {"mService", "mProcLock"})
getAdjSourceProcState()979     int getAdjSourceProcState() {
980         return mAdjSourceProcState;
981     }
982 
983     @GuardedBy({"mService", "mProcLock"})
setAdjTarget(Object adjTarget)984     void setAdjTarget(Object adjTarget) {
985         mAdjTarget = adjTarget;
986     }
987 
988     @GuardedBy(anyOf = {"mService", "mProcLock"})
getAdjTarget()989     Object getAdjTarget() {
990         return mAdjTarget;
991     }
992 
993     @GuardedBy("mService")
isReachable()994     boolean isReachable() {
995         return mReachable;
996     }
997 
998     @GuardedBy("mService")
setReachable(boolean reachable)999     void setReachable(boolean reachable) {
1000         mReachable = reachable;
1001     }
1002 
1003     @GuardedBy("mService")
resetCachedInfo()1004     void resetCachedInfo() {
1005         mCachedHasActivities = VALUE_INVALID;
1006         mCachedIsHeavyWeight = VALUE_INVALID;
1007         mCachedHasVisibleActivities = VALUE_INVALID;
1008         mCachedIsHomeProcess = VALUE_INVALID;
1009         mCachedIsPreviousProcess = VALUE_INVALID;
1010         mCachedHasRecentTasks = VALUE_INVALID;
1011         mCachedIsReceivingBroadcast = VALUE_INVALID;
1012         mCachedAdj = ProcessList.INVALID_ADJ;
1013         mCachedForegroundActivities = false;
1014         mCachedProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
1015         mCachedSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
1016         mCachedAdjType = null;
1017     }
1018 
1019     @GuardedBy("mService")
getCachedHasActivities()1020     boolean getCachedHasActivities() {
1021         if (mCachedHasActivities == VALUE_INVALID) {
1022             mCachedHasActivities = mApp.getWindowProcessController().hasActivities() ? VALUE_TRUE
1023                     : VALUE_FALSE;
1024             if (mCachedHasActivities == VALUE_TRUE) {
1025                 mApp.mProfile.addHostingComponentType(HOSTING_COMPONENT_TYPE_ACTIVITY);
1026             } else {
1027                 mApp.mProfile.clearHostingComponentType(HOSTING_COMPONENT_TYPE_ACTIVITY);
1028             }
1029         }
1030         return mCachedHasActivities == VALUE_TRUE;
1031     }
1032 
1033     @GuardedBy("mService")
getCachedIsHeavyWeight()1034     boolean getCachedIsHeavyWeight() {
1035         if (mCachedIsHeavyWeight == VALUE_INVALID) {
1036             mCachedIsHeavyWeight = mApp.getWindowProcessController().isHeavyWeightProcess()
1037                     ? VALUE_TRUE : VALUE_FALSE;
1038         }
1039         return mCachedIsHeavyWeight == VALUE_TRUE;
1040     }
1041 
1042     @GuardedBy("mService")
getCachedHasVisibleActivities()1043     boolean getCachedHasVisibleActivities() {
1044         if (mCachedHasVisibleActivities == VALUE_INVALID) {
1045             mCachedHasVisibleActivities = mApp.getWindowProcessController().hasVisibleActivities()
1046                     ? VALUE_TRUE : VALUE_FALSE;
1047         }
1048         return mCachedHasVisibleActivities == VALUE_TRUE;
1049     }
1050 
1051     @GuardedBy("mService")
getCachedIsHomeProcess()1052     boolean getCachedIsHomeProcess() {
1053         if (mCachedIsHomeProcess == VALUE_INVALID) {
1054             if (mApp.getWindowProcessController().isHomeProcess()) {
1055                 mCachedIsHomeProcess = VALUE_TRUE;
1056                 mService.mAppProfiler.mHasHomeProcess = true;
1057             } else {
1058                 mCachedIsHomeProcess = VALUE_FALSE;
1059             }
1060         }
1061         return mCachedIsHomeProcess == VALUE_TRUE;
1062     }
1063 
1064     @GuardedBy("mService")
getCachedIsPreviousProcess()1065     boolean getCachedIsPreviousProcess() {
1066         if (mCachedIsPreviousProcess == VALUE_INVALID) {
1067             if (mApp.getWindowProcessController().isPreviousProcess()) {
1068                 mCachedIsPreviousProcess = VALUE_TRUE;
1069                 mService.mAppProfiler.mHasPreviousProcess = true;
1070             } else {
1071                 mCachedIsPreviousProcess = VALUE_FALSE;
1072             }
1073         }
1074         return mCachedIsPreviousProcess == VALUE_TRUE;
1075     }
1076 
1077     @GuardedBy("mService")
getCachedHasRecentTasks()1078     boolean getCachedHasRecentTasks() {
1079         if (mCachedHasRecentTasks == VALUE_INVALID) {
1080             mCachedHasRecentTasks = mApp.getWindowProcessController().hasRecentTasks()
1081                     ? VALUE_TRUE : VALUE_FALSE;
1082         }
1083         return mCachedHasRecentTasks == VALUE_TRUE;
1084     }
1085 
1086     @GuardedBy("mService")
getCachedIsReceivingBroadcast(int[] outSchedGroup)1087     boolean getCachedIsReceivingBroadcast(int[] outSchedGroup) {
1088         if (mCachedIsReceivingBroadcast == VALUE_INVALID) {
1089             mCachedIsReceivingBroadcast = mService.isReceivingBroadcastLocked(mApp, outSchedGroup)
1090                     ? VALUE_TRUE : VALUE_FALSE;
1091             if (mCachedIsReceivingBroadcast == VALUE_TRUE) {
1092                 mCachedSchedGroup = outSchedGroup[0];
1093                 mApp.mProfile.addHostingComponentType(HOSTING_COMPONENT_TYPE_BROADCAST_RECEIVER);
1094             } else {
1095                 mApp.mProfile.clearHostingComponentType(HOSTING_COMPONENT_TYPE_BROADCAST_RECEIVER);
1096             }
1097         }
1098         return mCachedIsReceivingBroadcast == VALUE_TRUE;
1099     }
1100 
1101     @GuardedBy("mService")
getCachedCompatChange(@achedCompatChangeId int cachedCompatChangeId)1102     boolean getCachedCompatChange(@CachedCompatChangeId int cachedCompatChangeId) {
1103         if (mCachedCompatChanges[cachedCompatChangeId] == VALUE_INVALID) {
1104             mCachedCompatChanges[cachedCompatChangeId] = mService.mOomAdjuster
1105                     .isChangeEnabled(cachedCompatChangeId, mApp.info, false /* default */)
1106                     ? VALUE_TRUE : VALUE_FALSE;
1107         }
1108         return mCachedCompatChanges[cachedCompatChangeId] == VALUE_TRUE;
1109     }
1110 
1111     @GuardedBy("mService")
computeOomAdjFromActivitiesIfNecessary(OomAdjuster.ComputeOomAdjWindowCallback callback, int adj, boolean foregroundActivities, boolean hasVisibleActivities, int procState, int schedGroup, int appUid, int logUid, int processCurTop)1112     void computeOomAdjFromActivitiesIfNecessary(OomAdjuster.ComputeOomAdjWindowCallback callback,
1113             int adj, boolean foregroundActivities, boolean hasVisibleActivities, int procState,
1114             int schedGroup, int appUid, int logUid, int processCurTop) {
1115         if (mCachedAdj != ProcessList.INVALID_ADJ) {
1116             return;
1117         }
1118         callback.initialize(mApp, adj, foregroundActivities, hasVisibleActivities, procState,
1119                 schedGroup, appUid, logUid, processCurTop);
1120         final int minLayer = Math.min(ProcessList.VISIBLE_APP_LAYER_MAX,
1121                 mApp.getWindowProcessController().computeOomAdjFromActivities(callback));
1122 
1123         mCachedAdj = callback.adj;
1124         mCachedForegroundActivities = callback.foregroundActivities;
1125         mCachedHasVisibleActivities = callback.mHasVisibleActivities ? VALUE_TRUE : VALUE_FALSE;
1126         mCachedProcState = callback.procState;
1127         mCachedSchedGroup = callback.schedGroup;
1128         mCachedAdjType = callback.mAdjType;
1129 
1130         if (mCachedAdj == ProcessList.VISIBLE_APP_ADJ) {
1131             mCachedAdj += minLayer;
1132         }
1133     }
1134 
1135     @GuardedBy("mService")
getCachedAdj()1136     int getCachedAdj() {
1137         return mCachedAdj;
1138     }
1139 
1140     @GuardedBy("mService")
getCachedForegroundActivities()1141     boolean getCachedForegroundActivities() {
1142         return mCachedForegroundActivities;
1143     }
1144 
1145     @GuardedBy("mService")
getCachedProcState()1146     int getCachedProcState() {
1147         return mCachedProcState;
1148     }
1149 
1150     @GuardedBy("mService")
getCachedSchedGroup()1151     int getCachedSchedGroup() {
1152         return mCachedSchedGroup;
1153     }
1154 
1155     @GuardedBy("mService")
getCachedAdjType()1156     String getCachedAdjType() {
1157         return mCachedAdjType;
1158     }
1159 
1160     @GuardedBy("mService")
shouldScheduleLikeTopApp()1161     boolean shouldScheduleLikeTopApp() {
1162         return mScheduleLikeTopApp;
1163     }
1164 
1165     @GuardedBy("mService")
setScheduleLikeTopApp(boolean scheduleLikeTopApp)1166     void setScheduleLikeTopApp(boolean scheduleLikeTopApp) {
1167         mScheduleLikeTopApp = scheduleLikeTopApp;
1168     }
1169 
1170     @GuardedBy("mService")
getFollowupUpdateUptimeMs()1171     long getFollowupUpdateUptimeMs() {
1172         return mFollowupUpdateUptimeMs;
1173     }
1174 
1175     @GuardedBy("mService")
setFollowupUpdateUptimeMs(long updateUptimeMs)1176     void setFollowupUpdateUptimeMs(long updateUptimeMs) {
1177         mFollowupUpdateUptimeMs = updateUptimeMs;
1178     }
1179 
1180     @GuardedBy(anyOf = {"mService", "mProcLock"})
makeAdjReason()1181     public String makeAdjReason() {
1182         if (mAdjSource != null || mAdjTarget != null) {
1183             StringBuilder sb = new StringBuilder(128);
1184             sb.append(' ');
1185             if (mAdjTarget instanceof ComponentName) {
1186                 sb.append(((ComponentName) mAdjTarget).flattenToShortString());
1187             } else if (mAdjTarget != null) {
1188                 sb.append(mAdjTarget.toString());
1189             } else {
1190                 sb.append("{null}");
1191             }
1192             sb.append("<=");
1193             if (mAdjSource instanceof ProcessRecord) {
1194                 sb.append("Proc{");
1195                 sb.append(((ProcessRecord) mAdjSource).toShortString());
1196                 sb.append("}");
1197             } else if (mAdjSource != null) {
1198                 sb.append(mAdjSource.toString());
1199             } else {
1200                 sb.append("{null}");
1201             }
1202             return sb.toString();
1203         }
1204         return null;
1205     }
1206 
1207     @GuardedBy({"mService", "mProcLock"})
onCleanupApplicationRecordLSP()1208     void onCleanupApplicationRecordLSP() {
1209         if (TRACE_OOM_ADJ) {
1210             Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1211                     "oom:" + mApp.processName + "/u" + mApp.uid, 0);
1212         }
1213         setHasForegroundActivities(false);
1214         mHasShownUi = false;
1215         mForcingToImportant = null;
1216         mCurRawAdj = mSetRawAdj = mCurAdj = mSetAdj = mVerifiedAdj = ProcessList.INVALID_ADJ;
1217         mCurCapability = mSetCapability = PROCESS_CAPABILITY_NONE;
1218         mCurSchedGroup = mSetSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
1219         mCurProcState = mCurRawProcState = mSetProcState = PROCESS_STATE_NONEXISTENT;
1220         for (int i = 0; i < mCachedCompatChanges.length; i++) {
1221             mCachedCompatChanges[i] = VALUE_INVALID;
1222         }
1223     }
1224 
1225     @GuardedBy("mService")
isBackgroundRestricted()1226     boolean isBackgroundRestricted() {
1227         return mBackgroundRestricted;
1228     }
1229 
1230     @GuardedBy("mService")
setBackgroundRestricted(boolean restricted)1231     void setBackgroundRestricted(boolean restricted) {
1232         mBackgroundRestricted = restricted;
1233     }
1234 
1235     @GuardedBy("mService")
isCurBoundByNonBgRestrictedApp()1236     boolean isCurBoundByNonBgRestrictedApp() {
1237         return mCurBoundByNonBgRestrictedApp;
1238     }
1239 
1240     @GuardedBy("mService")
setCurBoundByNonBgRestrictedApp(boolean bound)1241     void setCurBoundByNonBgRestrictedApp(boolean bound) {
1242         mCurBoundByNonBgRestrictedApp = bound;
1243     }
1244 
1245     @GuardedBy("mService")
isSetBoundByNonBgRestrictedApp()1246     boolean isSetBoundByNonBgRestrictedApp() {
1247         return mSetBoundByNonBgRestrictedApp;
1248     }
1249 
1250     @GuardedBy("mService")
setSetBoundByNonBgRestrictedApp(boolean bound)1251     void setSetBoundByNonBgRestrictedApp(boolean bound) {
1252         mSetBoundByNonBgRestrictedApp = bound;
1253     }
1254 
1255     @GuardedBy("mService")
updateLastInvisibleTime(boolean hasVisibleActivities)1256     void updateLastInvisibleTime(boolean hasVisibleActivities) {
1257         if (hasVisibleActivities) {
1258             mLastInvisibleTime = Long.MAX_VALUE;
1259         } else if (mLastInvisibleTime == Long.MAX_VALUE) {
1260             mLastInvisibleTime = SystemClock.elapsedRealtime();
1261         }
1262     }
1263 
1264     @GuardedBy("mService")
1265     @ElapsedRealtimeLong
getLastInvisibleTime()1266     long getLastInvisibleTime() {
1267         return mLastInvisibleTime;
1268     }
1269 
1270     @GuardedBy("mService")
setNoKillOnBgRestrictedAndIdle(boolean shouldNotKill)1271     void setNoKillOnBgRestrictedAndIdle(boolean shouldNotKill) {
1272         mNoKillOnBgRestrictedAndIdle = shouldNotKill;
1273     }
1274 
1275     @GuardedBy("mService")
shouldNotKillOnBgRestrictedAndIdle()1276     boolean shouldNotKillOnBgRestrictedAndIdle() {
1277         return mNoKillOnBgRestrictedAndIdle;
1278     }
1279 
1280     @GuardedBy("mService")
setSetCached(boolean cached)1281     void setSetCached(boolean cached) {
1282         mSetCached = cached;
1283     }
1284 
1285     @GuardedBy("mService")
isSetCached()1286     boolean isSetCached() {
1287         return mSetCached;
1288     }
1289 
1290     @GuardedBy("mService")
setSetNoKillOnBgRestrictedAndIdle(boolean shouldNotKill)1291     void setSetNoKillOnBgRestrictedAndIdle(boolean shouldNotKill) {
1292         mSetNoKillOnBgRestrictedAndIdle = shouldNotKill;
1293     }
1294 
1295     @GuardedBy("mService")
isSetNoKillOnBgRestrictedAndIdle()1296     boolean isSetNoKillOnBgRestrictedAndIdle() {
1297         return mSetNoKillOnBgRestrictedAndIdle;
1298     }
1299 
1300     @GuardedBy("mService")
setLastCanKillOnBgRestrictedAndIdleTime(@lapsedRealtimeLong long now)1301     void setLastCanKillOnBgRestrictedAndIdleTime(@ElapsedRealtimeLong long now) {
1302         mLastCanKillOnBgRestrictedAndIdleTime = now;
1303     }
1304 
1305     @ElapsedRealtimeLong
1306     @GuardedBy("mService")
getLastCanKillOnBgRestrictedAndIdleTime()1307     long getLastCanKillOnBgRestrictedAndIdleTime() {
1308         return mLastCanKillOnBgRestrictedAndIdleTime;
1309     }
1310 
setCacheOomRankerRss(long rss, long rssTimeMs)1311     public void setCacheOomRankerRss(long rss, long rssTimeMs) {
1312         mCacheOomRankerRss = rss;
1313         mCacheOomRankerRssTimeMs = rssTimeMs;
1314     }
1315 
1316     @GuardedBy("mService")
getCacheOomRankerRss()1317     public long getCacheOomRankerRss() {
1318         return mCacheOomRankerRss;
1319     }
1320 
1321     @GuardedBy("mService")
getCacheOomRankerRssTimeMs()1322     public long getCacheOomRankerRssTimeMs() {
1323         return mCacheOomRankerRssTimeMs;
1324     }
1325 
1326     @GuardedBy({"mService", "mProcLock"})
dump(PrintWriter pw, String prefix, long nowUptime)1327     void dump(PrintWriter pw, String prefix, long nowUptime) {
1328         if (mReportedInteraction || mFgInteractionTime != 0) {
1329             pw.print(prefix); pw.print("reportedInteraction=");
1330             pw.print(mReportedInteraction);
1331             if (mInteractionEventTime != 0) {
1332                 pw.print(" time=");
1333                 TimeUtils.formatDuration(mInteractionEventTime, SystemClock.elapsedRealtime(), pw);
1334             }
1335             if (mFgInteractionTime != 0) {
1336                 pw.print(" fgInteractionTime=");
1337                 TimeUtils.formatDuration(mFgInteractionTime, SystemClock.elapsedRealtime(), pw);
1338             }
1339             pw.println();
1340         }
1341         pw.print(prefix); pw.print("adjSeq="); pw.print(mAdjSeq);
1342         pw.print(" lruSeq="); pw.println(mApp.getLruSeq());
1343         pw.print(prefix); pw.print("oom adj: max="); pw.print(mMaxAdj);
1344         pw.print(" curRaw="); pw.print(mCurRawAdj);
1345         pw.print(" setRaw="); pw.print(mSetRawAdj);
1346         pw.print(" cur="); pw.print(mCurAdj);
1347         pw.print(" set="); pw.println(mSetAdj);
1348         pw.print(prefix); pw.print("mCurSchedGroup="); pw.print(mCurSchedGroup);
1349         pw.print(" setSchedGroup="); pw.print(mSetSchedGroup);
1350         pw.print(" systemNoUi="); pw.println(mSystemNoUi);
1351         pw.print(prefix); pw.print("curProcState="); pw.print(getCurProcState());
1352         pw.print(" mRepProcState="); pw.print(mRepProcState);
1353         pw.print(" setProcState="); pw.print(mSetProcState);
1354         pw.print(" lastStateTime=");
1355         TimeUtils.formatDuration(getLastStateTime(), nowUptime, pw);
1356         pw.println();
1357         pw.print(prefix); pw.print("curCapability=");
1358         ActivityManager.printCapabilitiesFull(pw, mCurCapability);
1359         pw.print(" setCapability=");
1360         ActivityManager.printCapabilitiesFull(pw, mSetCapability);
1361         pw.println();
1362         if (mBackgroundRestricted) {
1363             pw.print(" backgroundRestricted=");
1364             pw.print(mBackgroundRestricted);
1365             pw.print(" boundByNonBgRestrictedApp=");
1366             pw.print(mSetBoundByNonBgRestrictedApp);
1367         }
1368         pw.println();
1369         if (mHasShownUi || mApp.mProfile.hasPendingUiClean()) {
1370             pw.print(prefix); pw.print("hasShownUi="); pw.print(mHasShownUi);
1371             pw.print(" pendingUiClean="); pw.println(mApp.mProfile.hasPendingUiClean());
1372         }
1373         pw.print(prefix); pw.print("cached="); pw.print(isCached());
1374         pw.print(" empty="); pw.println(isEmpty());
1375         if (mServiceB) {
1376             pw.print(prefix); pw.print("serviceb="); pw.print(mServiceB);
1377             pw.print(" serviceHighRam="); pw.println(mServiceHighRam);
1378         }
1379         if (mNotCachedSinceIdle) {
1380             pw.print(prefix); pw.print("notCachedSinceIdle="); pw.print(mNotCachedSinceIdle);
1381             if (mService.mAppProfiler.isProfilingPss()) {
1382                 pw.print(" initialIdlePss=");
1383             } else {
1384                 pw.print(" initialIdleRss=");
1385             }
1386             pw.println(mApp.mProfile.getInitialIdlePssOrRss());
1387         }
1388         if (hasTopUi() || hasOverlayUi() || mRunningRemoteAnimation) {
1389             pw.print(prefix); pw.print("hasTopUi="); pw.print(hasTopUi());
1390             pw.print(" hasOverlayUi="); pw.print(hasOverlayUi());
1391             pw.print(" runningRemoteAnimation="); pw.println(mRunningRemoteAnimation);
1392         }
1393         if (mHasForegroundActivities || mRepForegroundActivities) {
1394             pw.print(prefix);
1395             pw.print("foregroundActivities="); pw.print(mHasForegroundActivities);
1396             pw.print(" (rep="); pw.print(mRepForegroundActivities); pw.println(")");
1397         }
1398         if (mSetProcState > ActivityManager.PROCESS_STATE_SERVICE) {
1399             pw.print(prefix);
1400             pw.print("whenUnimportant=");
1401             TimeUtils.formatDuration(mWhenUnimportant - nowUptime, pw);
1402             pw.println();
1403         }
1404         if (mLastTopTime > 0) {
1405             pw.print(prefix); pw.print("lastTopTime=");
1406             TimeUtils.formatDuration(mLastTopTime, nowUptime, pw);
1407             pw.println();
1408         }
1409         if (mLastInvisibleTime > 0 && mLastInvisibleTime < Long.MAX_VALUE) {
1410             pw.print(prefix); pw.print("lastInvisibleTime=");
1411             final long elapsedRealtimeNow = SystemClock.elapsedRealtime();
1412             final long currentTimeNow = System.currentTimeMillis();
1413             final long lastInvisibleCurrentTime =
1414                     currentTimeNow - elapsedRealtimeNow + mLastInvisibleTime;
1415             TimeUtils.dumpTimeWithDelta(pw, lastInvisibleCurrentTime, currentTimeNow);
1416             pw.println();
1417         }
1418         if (mHasStartedServices) {
1419             pw.print(prefix); pw.print("hasStartedServices="); pw.println(mHasStartedServices);
1420         }
1421     }
1422 }
1423