• 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_STATE_NONEXISTENT;
20 import static android.app.ProcessMemoryState.HOSTING_COMPONENT_TYPE_EMPTY;
21 
22 import android.app.IApplicationThread;
23 import android.app.ProcessMemoryState.HostingComponentType;
24 import android.content.pm.ApplicationInfo;
25 import android.os.Debug;
26 import android.os.Process;
27 import android.os.SystemClock;
28 import android.util.DebugUtils;
29 import android.util.TimeUtils;
30 
31 import com.android.internal.annotations.CompositeRWLock;
32 import com.android.internal.annotations.GuardedBy;
33 import com.android.internal.app.procstats.ProcessState;
34 import com.android.internal.app.procstats.ProcessStats;
35 import com.android.server.am.ProcessList.ProcStateMemTracker;
36 import com.android.server.power.stats.BatteryStatsImpl;
37 
38 import java.io.PrintWriter;
39 import java.util.concurrent.atomic.AtomicInteger;
40 import java.util.concurrent.atomic.AtomicLong;
41 
42 /**
43  * Profiling info of the process, such as PSS, cpu, etc.
44  *
45  * TODO(b/297542292): Update PSS names with RSS once AppProfiler's PSS profiling has been replaced.
46  */
47 final class ProcessProfileRecord {
48     final ProcessRecord mApp;
49 
50     private final ActivityManagerService mService;
51 
52     final Object mProfilerLock;
53 
54     @GuardedBy("mProfilerLock")
55     private final ProcessList.ProcStateMemTracker mProcStateMemTracker =
56             new ProcessList.ProcStateMemTracker();
57 
58     /**
59      * Stats of pss, cpu, etc.
60      */
61     @GuardedBy("mService.mProcessStats.mLock")
62     private ProcessState mBaseProcessTracker;
63 
64     /**
65      * Last time we retrieved PSS data.
66      */
67     @GuardedBy("mProfilerLock")
68     private long mLastPssTime;
69 
70     /**
71      * Next time we want to request PSS data.
72      */
73     @GuardedBy("mProfilerLock")
74     private long mNextPssTime;
75 
76     /**
77      * Initial memory pss of process for idle maintenance.
78      */
79     @GuardedBy("mProfilerLock")
80     private long mInitialIdlePssOrRss;
81 
82     /**
83      * Last computed memory pss.
84      */
85     @GuardedBy("mProfilerLock")
86     private long mLastPss;
87 
88     /**
89      * Last computed SwapPss.
90      */
91     @GuardedBy("mProfilerLock")
92     private long mLastSwapPss;
93 
94     /**
95      * Last computed pss when in cached state.
96      */
97     @GuardedBy("mProfilerLock")
98     private long mLastCachedPss;
99 
100     /**
101      * Last computed SwapPss when in cached state.
102      */
103     @GuardedBy("mProfilerLock")
104     private long mLastCachedSwapPss;
105 
106     /**
107      * Last computed memory rss.
108      */
109     @GuardedBy("mProfilerLock")
110     private long mLastRss;
111 
112     /**
113      * Last computed rss when in cached state.
114      *
115      * This value is not set or retrieved unless Flags.removeAppProfilerPssCollection() is true.
116      */
117     @GuardedBy("mProfilerLock")
118     private long mLastCachedRss;
119 
120     /**
121      * Cache of last retrieve memory info, to throttle how frequently apps can request it.
122      */
123     @GuardedBy("mProfilerLock")
124     private Debug.MemoryInfo mLastMemInfo;
125 
126     /**
127      * Cache of last retrieve memory uptime, to throttle how frequently apps can request it.
128      */
129     @GuardedBy("mProfilerLock")
130     private long mLastMemInfoTime;
131 
132     /**
133      * Currently requesting pss for.
134      */
135     @GuardedBy("mProfilerLock")
136     private int mPssProcState = PROCESS_STATE_NONEXISTENT;
137 
138     /**
139      * The type of stat collection that we are currently requesting.
140      */
141     @GuardedBy("mProfilerLock")
142     private int mPssStatType;
143 
144     /**
145      * How long proc has run CPU at last check.
146      */
147     final AtomicLong mLastCpuTime = new AtomicLong(0);
148 
149     /**
150      * How long proc has run CPU most recently.
151      */
152     final AtomicLong mCurCpuTime = new AtomicLong(0);
153 
154     /**
155      * How long the process has spent on waiting in the runqueue since fork.
156      */
157     final AtomicLong mLastCpuDelayTime = new AtomicLong(0);
158 
159     /**
160      * Last selected memory trimming level.
161      */
162     @CompositeRWLock({"mService", "mProcLock"})
163     private int mTrimMemoryLevel;
164 
165     /**
166      * Want to clean up resources from showing UI?
167      */
168     @GuardedBy("mProcLock")
169     private boolean mPendingUiClean;
170 
171     /**
172      * Pointer to the battery stats of this process.
173      */
174     private BatteryStatsImpl.Uid.Proc mCurProcBatteryStats;
175 
176     /**
177      * When we last asked the app to do a gc.
178      */
179     @GuardedBy("mProfilerLock")
180     private long mLastRequestedGc;
181 
182     /**
183      * When we last told the app that memory is low.
184      */
185     @CompositeRWLock({"mService", "mProfilerLock"})
186     private long mLastLowMemory;
187 
188     /**
189      * Set to true when waiting to report low mem.
190      */
191     @GuardedBy("mProfilerLock")
192     private boolean mReportLowMemory;
193 
194     // ========================================================================
195     // Local copies of some process info, to avoid holding global AMS lock
196     @GuardedBy("mProfilerLock")
197     private int mPid;
198 
199     @GuardedBy("mProfilerLock")
200     private IApplicationThread mThread;
201 
202     @GuardedBy("mProfilerLock")
203     private int mSetProcState;
204 
205     @GuardedBy("mProfilerLock")
206     private int mSetAdj;
207 
208     @GuardedBy("mProfilerLock")
209     private int mCurRawAdj;
210 
211     @GuardedBy("mProfilerLock")
212     private long mLastStateTime;
213 
214     private AtomicInteger mCurrentHostingComponentTypes =
215             new AtomicInteger(HOSTING_COMPONENT_TYPE_EMPTY);
216 
217     private AtomicInteger mHistoricalHostingComponentTypes =
218             new AtomicInteger(HOSTING_COMPONENT_TYPE_EMPTY);
219 
220     private final ActivityManagerGlobalLock mProcLock;
221 
ProcessProfileRecord(final ProcessRecord app)222     ProcessProfileRecord(final ProcessRecord app) {
223         mApp = app;
224         mService = app.mService;
225         mProcLock = mService.mProcLock;
226         mProfilerLock = mService.mAppProfiler.mProfilerLock;
227     }
228 
init(long now)229     void init(long now) {
230         mLastPssTime = mNextPssTime = now;
231     }
232 
233     @GuardedBy("mService.mProcessStats.mLock")
getBaseProcessTracker()234     ProcessState getBaseProcessTracker() {
235         return mBaseProcessTracker;
236     }
237 
238     @GuardedBy("mService.mProcessStats.mLock")
setBaseProcessTracker(ProcessState baseProcessTracker)239     void setBaseProcessTracker(ProcessState baseProcessTracker) {
240         mBaseProcessTracker = baseProcessTracker;
241     }
242 
onProcessFrozen()243     void onProcessFrozen() {
244         synchronized (mService.mProcessStats.mLock) {
245             final ProcessState tracker = mBaseProcessTracker;
246             if (tracker != null) {
247                 final PackageList pkgList = mApp.getPkgList();
248                 final long now = SystemClock.uptimeMillis();
249                 synchronized (pkgList) {
250                     tracker.onProcessFrozen(now, pkgList.getPackageListLocked());
251                 }
252             }
253         }
254     }
255 
onProcessUnfrozen()256     void onProcessUnfrozen() {
257         synchronized (mService.mProcessStats.mLock) {
258             final ProcessState tracker = mBaseProcessTracker;
259             if (tracker != null) {
260                 final PackageList pkgList = mApp.getPkgList();
261                 final long now = SystemClock.uptimeMillis();
262                 synchronized (pkgList) {
263                     tracker.onProcessUnfrozen(now, pkgList.getPackageListLocked());
264                 }
265             }
266         }
267     }
268 
onProcessActive(IApplicationThread thread, ProcessStatsService tracker)269     void onProcessActive(IApplicationThread thread, ProcessStatsService tracker) {
270         if (mThread == null) {
271             synchronized (mProfilerLock) {
272                 synchronized (tracker.mLock) {
273                     final ProcessState origBase = getBaseProcessTracker();
274                     final PackageList pkgList = mApp.getPkgList();
275                     if (origBase != null) {
276                         synchronized (pkgList) {
277                             origBase.setState(ProcessStats.STATE_NOTHING,
278                                     tracker.getMemFactorLocked(), SystemClock.uptimeMillis(),
279                                     pkgList.getPackageListLocked());
280                         }
281                         origBase.makeInactive();
282                     }
283                     final ApplicationInfo info = mApp.info;
284                     final int attributionUid = getUidForAttribution(mApp);
285                     final ProcessState baseProcessTracker = tracker.getProcessStateLocked(
286                             info.packageName, attributionUid, info.longVersionCode,
287                             mApp.processName);
288                     setBaseProcessTracker(baseProcessTracker);
289                     baseProcessTracker.makeActive();
290                     pkgList.forEachPackage((pkgName, holder) -> {
291                         if (holder.state != null && holder.state != origBase) {
292                             holder.state.makeInactive();
293                         }
294                         tracker.updateProcessStateHolderLocked(holder, pkgName, attributionUid,
295                                 mApp.info.longVersionCode, mApp.processName);
296                         if (holder.state != baseProcessTracker) {
297                             holder.state.makeActive();
298                         }
299                     });
300                     mThread = thread;
301                 }
302             }
303         } else {
304             synchronized (mProfilerLock) {
305                 mThread = thread;
306             }
307         }
308     }
309 
onProcessInactive(ProcessStatsService tracker)310     void onProcessInactive(ProcessStatsService tracker) {
311         synchronized (mProfilerLock) {
312             synchronized (tracker.mLock) {
313                 final ProcessState origBase = getBaseProcessTracker();
314                 if (origBase != null) {
315                     final PackageList pkgList = mApp.getPkgList();
316                     synchronized (pkgList) {
317                         origBase.setState(ProcessStats.STATE_NOTHING,
318                                 tracker.getMemFactorLocked(), SystemClock.uptimeMillis(),
319                                 pkgList.getPackageListLocked());
320                     }
321                     origBase.makeInactive();
322                     setBaseProcessTracker(null);
323                     pkgList.forEachPackageProcessStats(holder -> {
324                         if (holder.state != null && holder.state != origBase) {
325                             holder.state.makeInactive();
326                         }
327                         holder.pkg = null;
328                         holder.state = null;
329                     });
330                 }
331                 mThread = null;
332             }
333         }
334         mCurrentHostingComponentTypes.set(HOSTING_COMPONENT_TYPE_EMPTY);
335         mHistoricalHostingComponentTypes.set(HOSTING_COMPONENT_TYPE_EMPTY);
336     }
337 
338     @GuardedBy("mProfilerLock")
getLastPssTime()339     long getLastPssTime() {
340         return mLastPssTime;
341     }
342 
343     @GuardedBy("mProfilerLock")
setLastPssTime(long lastPssTime)344     void setLastPssTime(long lastPssTime) {
345         mLastPssTime = lastPssTime;
346     }
347 
348     @GuardedBy("mProfilerLock")
getNextPssTime()349     long getNextPssTime() {
350         return mNextPssTime;
351     }
352 
353     @GuardedBy("mProfilerLock")
setNextPssTime(long nextPssTime)354     void setNextPssTime(long nextPssTime) {
355         mNextPssTime = nextPssTime;
356     }
357 
358     @GuardedBy("mProfilerLock")
getInitialIdlePssOrRss()359     long getInitialIdlePssOrRss() {
360         return mInitialIdlePssOrRss;
361     }
362 
363     @GuardedBy("mProfilerLock")
setInitialIdlePssOrRss(long initialIdlePssOrRss)364     void setInitialIdlePssOrRss(long initialIdlePssOrRss) {
365         mInitialIdlePssOrRss = initialIdlePssOrRss;
366     }
367 
368     @GuardedBy("mProfilerLock")
getLastPss()369     long getLastPss() {
370         return mLastPss;
371     }
372 
373     @GuardedBy("mProfilerLock")
setLastPss(long lastPss)374     void setLastPss(long lastPss) {
375         mLastPss = lastPss;
376     }
377 
378     @GuardedBy("mProfilerLock")
getLastCachedPss()379     long getLastCachedPss() {
380         return mLastCachedPss;
381     }
382 
383     @GuardedBy("mProfilerLock")
setLastCachedPss(long lastCachedPss)384     void setLastCachedPss(long lastCachedPss) {
385         mLastCachedPss = lastCachedPss;
386     }
387 
388     @GuardedBy("mProfilerLock")
getLastCachedRss()389     long getLastCachedRss() {
390         return mLastCachedRss;
391     }
392 
393     @GuardedBy("mProfilerLock")
setLastCachedRss(long lastCachedRss)394     void setLastCachedRss(long lastCachedRss) {
395         mLastCachedRss = lastCachedRss;
396     }
397 
398     @GuardedBy("mProfilerLock")
getLastSwapPss()399     long getLastSwapPss() {
400         return mLastSwapPss;
401     }
402 
403     @GuardedBy("mProfilerLock")
setLastSwapPss(long lastSwapPss)404     void setLastSwapPss(long lastSwapPss) {
405         mLastSwapPss = lastSwapPss;
406     }
407 
408     @GuardedBy("mProfilerLock")
getLastCachedSwapPss()409     long getLastCachedSwapPss() {
410         return mLastCachedSwapPss;
411     }
412 
413     @GuardedBy("mProfilerLock")
setLastCachedSwapPss(long lastCachedSwapPss)414     void setLastCachedSwapPss(long lastCachedSwapPss) {
415         mLastCachedSwapPss = lastCachedSwapPss;
416     }
417 
418     @GuardedBy("mProfilerLock")
getLastRss()419     long getLastRss() {
420         return mLastRss;
421     }
422 
423     @GuardedBy("mProfilerLock")
setLastRss(long lastRss)424     void setLastRss(long lastRss) {
425         mLastRss = lastRss;
426     }
427 
428     @GuardedBy("mProfilerLock")
getLastMemInfo()429     Debug.MemoryInfo getLastMemInfo() {
430         return mLastMemInfo;
431     }
432 
433     @GuardedBy("mProfilerLock")
setLastMemInfo(Debug.MemoryInfo lastMemInfo)434     void setLastMemInfo(Debug.MemoryInfo lastMemInfo) {
435         mLastMemInfo = lastMemInfo;
436     }
437 
438     @GuardedBy("mProfilerLock")
getLastMemInfoTime()439     long getLastMemInfoTime() {
440         return mLastMemInfoTime;
441     }
442 
443     @GuardedBy("mProfilerLock")
setLastMemInfoTime(long lastMemInfoTime)444     void setLastMemInfoTime(long lastMemInfoTime) {
445         mLastMemInfoTime = lastMemInfoTime;
446     }
447 
448     @GuardedBy("mProfilerLock")
getPssProcState()449     int getPssProcState() {
450         return mPssProcState;
451     }
452 
453     @GuardedBy("mProfilerLock")
setPssProcState(int pssProcState)454     void setPssProcState(int pssProcState) {
455         mPssProcState = pssProcState;
456     }
457 
458     @GuardedBy("mProfilerLock")
getPssStatType()459     int getPssStatType() {
460         return mPssStatType;
461     }
462 
463     @GuardedBy("mProfilerLock")
setPssStatType(int pssStatType)464     void setPssStatType(int pssStatType) {
465         mPssStatType = pssStatType;
466     }
467 
468     @GuardedBy(anyOf = {"mService", "mProcLock"})
getTrimMemoryLevel()469     int getTrimMemoryLevel() {
470         return mTrimMemoryLevel;
471     }
472 
473     @GuardedBy({"mService", "mProcLock"})
setTrimMemoryLevel(int trimMemoryLevel)474     void setTrimMemoryLevel(int trimMemoryLevel) {
475         mTrimMemoryLevel = trimMemoryLevel;
476     }
477 
478     @GuardedBy("mProcLock")
hasPendingUiClean()479     boolean hasPendingUiClean() {
480         return mPendingUiClean;
481     }
482 
483     @GuardedBy("mProcLock")
setPendingUiClean(boolean pendingUiClean)484     void setPendingUiClean(boolean pendingUiClean) {
485         mPendingUiClean = pendingUiClean;
486         mApp.getWindowProcessController().setPendingUiClean(pendingUiClean);
487     }
488 
getCurProcBatteryStats()489     BatteryStatsImpl.Uid.Proc getCurProcBatteryStats() {
490         return mCurProcBatteryStats;
491     }
492 
setCurProcBatteryStats(BatteryStatsImpl.Uid.Proc curProcBatteryStats)493     void setCurProcBatteryStats(BatteryStatsImpl.Uid.Proc curProcBatteryStats) {
494         mCurProcBatteryStats = curProcBatteryStats;
495     }
496 
497     @GuardedBy("mProfilerLock")
getLastRequestedGc()498     long getLastRequestedGc() {
499         return mLastRequestedGc;
500     }
501 
502     @GuardedBy("mProfilerLock")
setLastRequestedGc(long lastRequestedGc)503     void setLastRequestedGc(long lastRequestedGc) {
504         mLastRequestedGc = lastRequestedGc;
505     }
506 
507     @GuardedBy(anyOf = {"mService", "mProfilerLock"})
getLastLowMemory()508     long getLastLowMemory() {
509         return mLastLowMemory;
510     }
511 
512     @GuardedBy({"mService", "mProfilerLock"})
setLastLowMemory(long lastLowMemory)513     void setLastLowMemory(long lastLowMemory) {
514         mLastLowMemory = lastLowMemory;
515     }
516 
517     @GuardedBy("mProfilerLock")
getReportLowMemory()518     boolean getReportLowMemory() {
519         return mReportLowMemory;
520     }
521 
522     @GuardedBy("mProfilerLock")
setReportLowMemory(boolean reportLowMemory)523     void setReportLowMemory(boolean reportLowMemory) {
524         mReportLowMemory = reportLowMemory;
525     }
526 
addPss(long pss, long uss, long rss, boolean always, int type, long duration)527     void addPss(long pss, long uss, long rss, boolean always, int type, long duration) {
528         synchronized (mService.mProcessStats.mLock) {
529             final ProcessState tracker = mBaseProcessTracker;
530             if (tracker != null) {
531                 final PackageList pkgList = mApp.getPkgList();
532                 synchronized (pkgList) {
533                     tracker.addPss(pss, uss, rss, always, type, duration,
534                             pkgList.getPackageListLocked());
535                 }
536             }
537         }
538     }
539 
reportExcessiveCpu()540     void reportExcessiveCpu() {
541         synchronized (mService.mProcessStats.mLock) {
542             final ProcessState tracker = mBaseProcessTracker;
543             if (tracker != null) {
544                 final PackageList pkgList = mApp.getPkgList();
545                 synchronized (pkgList) {
546                     tracker.reportExcessiveCpu(pkgList.getPackageListLocked());
547                 }
548             }
549         }
550     }
551 
setProcessTrackerState(int procState, int memFactor)552     void setProcessTrackerState(int procState, int memFactor) {
553         synchronized (mService.mProcessStats.mLock) {
554             final ProcessState tracker = mBaseProcessTracker;
555             if (tracker != null) {
556                 if (procState != PROCESS_STATE_NONEXISTENT) {
557                     final PackageList pkgList = mApp.getPkgList();
558                     final long now = SystemClock.uptimeMillis();
559                     synchronized (pkgList) {
560                         tracker.setState(procState, memFactor, now,
561                                 pkgList.getPackageListLocked());
562                     }
563                 }
564             }
565         }
566     }
567 
568     @GuardedBy("mProfilerLock")
commitNextPssTime()569     void commitNextPssTime() {
570         commitNextPssTime(mProcStateMemTracker);
571     }
572 
573     @GuardedBy("mProfilerLock")
abortNextPssTime()574     void abortNextPssTime() {
575         abortNextPssTime(mProcStateMemTracker);
576     }
577 
578     @GuardedBy("mProfilerLock")
computeNextPssTime(int procState, boolean test, boolean sleeping, long now)579     long computeNextPssTime(int procState, boolean test, boolean sleeping, long now) {
580         return ProcessList.computeNextPssTime(procState, mProcStateMemTracker, test, sleeping, now,
581                 // Cap the Pss time to make sure no Pss is collected during the very few
582                 // minutes after the system is boot, given the system is already busy.
583                 Math.max(mService.mBootCompletedTimestamp, mService.mLastIdleTime)
584                 + mService.mConstants.FULL_PSS_MIN_INTERVAL);
585     }
586 
commitNextPssTime(ProcStateMemTracker tracker)587     private static void commitNextPssTime(ProcStateMemTracker tracker) {
588         if (tracker.mPendingMemState >= 0) {
589             tracker.mHighestMem[tracker.mPendingMemState] = tracker.mPendingHighestMemState;
590             tracker.mScalingFactor[tracker.mPendingMemState] = tracker.mPendingScalingFactor;
591             tracker.mTotalHighestMem = tracker.mPendingHighestMemState;
592             tracker.mPendingMemState = -1;
593         }
594     }
595 
abortNextPssTime(ProcStateMemTracker tracker)596     private static void abortNextPssTime(ProcStateMemTracker tracker) {
597         tracker.mPendingMemState = -1;
598     }
599 
600     /**
601      * Returns the uid that should be used for attribution purposes in profiling / stats.
602      *
603      * In most cases this returns the uid of the process itself. For isolated processes though,
604      * since the process uid is dynamically allocated and can't easily be traced back to the app,
605      * for attribution we use the app package uid.
606      */
getUidForAttribution(ProcessRecord processRecord)607     private static int getUidForAttribution(ProcessRecord processRecord) {
608         if (Process.isIsolatedUid(processRecord.uid)) {
609             return processRecord.info.uid;
610         } else {
611             return processRecord.uid;
612         }
613     }
614 
615     @GuardedBy("mProfilerLock")
getPid()616     int getPid() {
617         return mPid;
618     }
619 
620     @GuardedBy("mProfilerLock")
setPid(int pid)621     void setPid(int pid) {
622         mPid = pid;
623     }
624 
625     @GuardedBy("mProfilerLock")
getThread()626     IApplicationThread getThread() {
627         return mThread;
628     }
629 
630     @GuardedBy("mProfilerLock")
getSetProcState()631     int getSetProcState() {
632         return mSetProcState;
633     }
634 
635     @GuardedBy("mProfilerLock")
getSetAdj()636     int getSetAdj() {
637         return mSetAdj;
638     }
639 
640     @GuardedBy("mProfilerLock")
getCurRawAdj()641     int getCurRawAdj() {
642         return mCurRawAdj;
643     }
644 
645     @GuardedBy("mProfilerLock")
getLastStateTime()646     long getLastStateTime() {
647         return mLastStateTime;
648     }
649 
650     @GuardedBy({"mService", "mProfilerLock"})
updateProcState(ProcessStateRecord state)651     void updateProcState(ProcessStateRecord state) {
652         mSetProcState = state.getCurProcState();
653         mSetAdj = state.getCurAdj();
654         mCurRawAdj = state.getCurRawAdj();
655         mLastStateTime = state.getLastStateTime();
656     }
657 
addHostingComponentType(@ostingComponentType int type)658     void addHostingComponentType(@HostingComponentType int type) {
659         mCurrentHostingComponentTypes.set(mCurrentHostingComponentTypes.get() | type);
660         mHistoricalHostingComponentTypes.set(mHistoricalHostingComponentTypes.get() | type);
661     }
662 
clearHostingComponentType(@ostingComponentType int type)663     void clearHostingComponentType(@HostingComponentType int type) {
664         mCurrentHostingComponentTypes.set(mCurrentHostingComponentTypes.get() & ~type);
665     }
666 
getCurrentHostingComponentTypes()667     @HostingComponentType int getCurrentHostingComponentTypes() {
668         return mCurrentHostingComponentTypes.get();
669     }
670 
getHistoricalHostingComponentTypes()671     @HostingComponentType int getHistoricalHostingComponentTypes() {
672         return mHistoricalHostingComponentTypes.get();
673     }
674 
675     @GuardedBy("mService")
dumpPss(PrintWriter pw, String prefix, long nowUptime)676     void dumpPss(PrintWriter pw, String prefix, long nowUptime) {
677         synchronized (mProfilerLock) {
678             // TODO(b/297542292): Remove this case once PSS profiling is replaced
679             if (mService.mAppProfiler.isProfilingPss()) {
680                 pw.print(prefix);
681                 pw.print("lastPssTime=");
682                 TimeUtils.formatDuration(mLastPssTime, nowUptime, pw);
683                 pw.print(" pssProcState=");
684                 pw.print(mPssProcState);
685                 pw.print(" pssStatType=");
686                 pw.print(mPssStatType);
687                 pw.print(" nextPssTime=");
688                 TimeUtils.formatDuration(mNextPssTime, nowUptime, pw);
689                 pw.println();
690                 pw.print(prefix);
691                 pw.print("lastPss=");
692                 DebugUtils.printSizeValue(pw, mLastPss * 1024);
693                 pw.print(" lastSwapPss=");
694                 DebugUtils.printSizeValue(pw, mLastSwapPss * 1024);
695                 pw.print(" lastCachedPss=");
696                 DebugUtils.printSizeValue(pw, mLastCachedPss * 1024);
697                 pw.print(" lastCachedSwapPss=");
698                 DebugUtils.printSizeValue(pw, mLastCachedSwapPss * 1024);
699                 pw.print(" lastRss=");
700                 DebugUtils.printSizeValue(pw, mLastRss * 1024);
701             } else {
702                 pw.print(prefix);
703                 pw.print("lastRssTime=");
704                 TimeUtils.formatDuration(mLastPssTime, nowUptime, pw);
705                 pw.print(" rssProcState=");
706                 pw.print(mPssProcState);
707                 pw.print(" rssStatType=");
708                 pw.print(mPssStatType);
709                 pw.print(" nextRssTime=");
710                 TimeUtils.formatDuration(mNextPssTime, nowUptime, pw);
711                 pw.println();
712                 pw.print(prefix);
713                 pw.print("lastRss=");
714                 DebugUtils.printSizeValue(pw, mLastRss * 1024);
715                 pw.print(" lastCachedRss=");
716                 DebugUtils.printSizeValue(pw, mLastCachedRss * 1024);
717             }
718             pw.println();
719             pw.print(prefix);
720             pw.print("trimMemoryLevel=");
721             pw.println(mTrimMemoryLevel);
722             pw.print(prefix); pw.print("procStateMemTracker: ");
723             mProcStateMemTracker.dumpLine(pw);
724             pw.print(prefix);
725             pw.print("lastRequestedGc=");
726             TimeUtils.formatDuration(mLastRequestedGc, nowUptime, pw);
727             pw.print(" lastLowMemory=");
728             TimeUtils.formatDuration(mLastLowMemory, nowUptime, pw);
729             pw.print(" reportLowMemory=");
730             pw.println(mReportLowMemory);
731         }
732         pw.print(prefix);
733         pw.print("currentHostingComponentTypes=0x");
734         pw.print(Integer.toHexString(getCurrentHostingComponentTypes()));
735         pw.print(" historicalHostingComponentTypes=0x");
736         pw.println(Integer.toHexString(getHistoricalHostingComponentTypes()));
737     }
738 
dumpCputime(PrintWriter pw, String prefix)739     void dumpCputime(PrintWriter pw, String prefix) {
740         final long lastCpuTime = mLastCpuTime.get();
741         pw.print(prefix);
742         pw.print("lastCpuTime=");
743         pw.print(lastCpuTime);
744         if (lastCpuTime > 0) {
745             pw.print(" timeUsed=");
746             TimeUtils.formatDuration(mCurCpuTime.get() - lastCpuTime, pw);
747         }
748         pw.println();
749     }
750 }
751