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