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