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