Loading core/java/com/android/internal/os/ProcessCpuTracker.java +14 −10 Original line number Diff line number Diff line Loading @@ -64,7 +64,9 @@ public class ProcessCpuTracker { /** Stores user time and system time in 100ths of a second. */ private final long[] mProcessStatsData = new long[4]; /** Stores user time and system time in 100ths of a second. */ /** Stores user time and system time in 100ths of a second. Used for * public API to retrieve CPU use for a process. Must lock while in use. */ private final long[] mSinglePidStatsData = new long[4]; private static final int[] PROCESS_FULL_STATS_FORMAT = new int[] { Loading Loading @@ -533,9 +535,10 @@ public class ProcessCpuTracker { /** * Returns the total time (in clock ticks, or 1/100 sec) spent executing in * both user and system code. * both user and system code. Safe to call without lock held. */ public long getCpuTimeForPid(int pid) { synchronized (mSinglePidStatsData) { final String statFile = "/proc/" + pid + "/stat"; final long[] statsData = mSinglePidStatsData; if (Process.readProcFile(statFile, PROCESS_STATS_FORMAT, Loading @@ -546,6 +549,7 @@ public class ProcessCpuTracker { } return 0; } } /** * Returns the delta time (in clock ticks, or 1/100 sec) spent at each CPU Loading services/core/java/com/android/server/am/ActivityManagerService.java +14 −11 Original line number Diff line number Diff line Loading @@ -1077,13 +1077,16 @@ public final class ActivityManagerService extends ActivityManagerNative /** * Runtime CPU use collection thread. This object's lock is used to * protect all related state. * perform synchronization with the thread (notifying it to run). */ final Thread mProcessCpuThread; /** * Used to collect process stats when showing not responding dialog. * Protected by mProcessCpuThread. * Used to collect per-process CPU use for ANRs, battery stats, etc. * Must acquire this object's lock when accessing it. * NOTE: this lock will be held while doing long operations (trawling * through all processes in /proc), so it should never be acquired by * any critical paths such as when holding the main activity manager lock. */ final ProcessCpuTracker mProcessCpuTracker = new ProcessCpuTracker( MONITOR_THREAD_CPU_USAGE); Loading Loading @@ -1597,7 +1600,7 @@ public final class ActivityManagerService extends ActivityManagerNative infoMap.put(mi.pid, mi); } updateCpuStatsNow(); synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { final int N = mProcessCpuTracker.countStats(); for (int i=0; i<N; i++) { ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i); Loading Loading @@ -1896,7 +1899,7 @@ public final class ActivityManagerService extends ActivityManagerNative if (memInfo != null) { updateCpuStatsNow(); long nativeTotalPss = 0; synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { final int N = mProcessCpuTracker.countStats(); for (int j=0; j<N; j++) { ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(j); Loading @@ -1914,7 +1917,7 @@ public final class ActivityManagerService extends ActivityManagerNative } } memInfo.readMemInfo(); synchronized (this) { synchronized (ActivityManagerService.this) { if (DEBUG_PSS) Slog.d(TAG, "Collected native and kernel memory in " + (SystemClock.uptimeMillis()-start) + "ms"); mProcessStats.addSysMemUsageLocked(memInfo.getCachedSizeKb(), Loading Loading @@ -2185,7 +2188,7 @@ public final class ActivityManagerService extends ActivityManagerNative return; } synchronized (mActivityManagerService.mProcessCpuThread) { synchronized (mActivityManagerService.mProcessCpuTracker) { pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentLoad()); pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentState( SystemClock.uptimeMillis())); Loading Loading @@ -2381,7 +2384,7 @@ public final class ActivityManagerService extends ActivityManagerNative } void updateCpuStatsNow() { synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { mProcessCpuMutexFree.set(false); final long now = SystemClock.uptimeMillis(); boolean haveNewCpuStats = false; Loading Loading @@ -5076,7 +5079,7 @@ public final class ActivityManagerService extends ActivityManagerNative String cpuInfo = null; if (MONITOR_CPU_USAGE) { updateCpuStatsNow(); synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { cpuInfo = mProcessCpuTracker.printCurrentState(anrTime); } info.append(processCpuTracker.printCurrentLoad()); Loading Loading @@ -13743,7 +13746,7 @@ public final class ActivityManagerService extends ActivityManagerNative findPid = Integer.parseInt(args[opti]); } catch (NumberFormatException e) { } synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { final int N = mProcessCpuTracker.countStats(); for (int i=0; i<N; i++) { ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i); Loading Loading @@ -13905,7 +13908,7 @@ public final class ActivityManagerService extends ActivityManagerNative // If we are showing aggregations, also look for native processes to // include so that our aggregations are more accurate. updateCpuStatsNow(); synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { final int N = mProcessCpuTracker.countStats(); for (int i=0; i<N; i++) { ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i); services/core/java/com/android/server/am/ActivityStack.java +3 −8 Original line number Diff line number Diff line Loading @@ -1042,11 +1042,8 @@ final class ActivityStack { if (prev.app != null && prev.cpuTimeAtResume > 0 && mService.mBatteryStatsService.isOnBattery()) { long diff; synchronized (mService.mProcessCpuThread) { diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid) long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid) - prev.cpuTimeAtResume; } if (diff > 0) { BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics(); synchronized (bsi) { Loading Loading @@ -1097,9 +1094,7 @@ final class ActivityStack { // TODO: To be more accurate, the mark should be before the onCreate, // not after the onResume. But for subsequent starts, onResume is fine. if (next.app != null) { synchronized (mService.mProcessCpuThread) { next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid); } } else { next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process } Loading Loading
core/java/com/android/internal/os/ProcessCpuTracker.java +14 −10 Original line number Diff line number Diff line Loading @@ -64,7 +64,9 @@ public class ProcessCpuTracker { /** Stores user time and system time in 100ths of a second. */ private final long[] mProcessStatsData = new long[4]; /** Stores user time and system time in 100ths of a second. */ /** Stores user time and system time in 100ths of a second. Used for * public API to retrieve CPU use for a process. Must lock while in use. */ private final long[] mSinglePidStatsData = new long[4]; private static final int[] PROCESS_FULL_STATS_FORMAT = new int[] { Loading Loading @@ -533,9 +535,10 @@ public class ProcessCpuTracker { /** * Returns the total time (in clock ticks, or 1/100 sec) spent executing in * both user and system code. * both user and system code. Safe to call without lock held. */ public long getCpuTimeForPid(int pid) { synchronized (mSinglePidStatsData) { final String statFile = "/proc/" + pid + "/stat"; final long[] statsData = mSinglePidStatsData; if (Process.readProcFile(statFile, PROCESS_STATS_FORMAT, Loading @@ -546,6 +549,7 @@ public class ProcessCpuTracker { } return 0; } } /** * Returns the delta time (in clock ticks, or 1/100 sec) spent at each CPU Loading
services/core/java/com/android/server/am/ActivityManagerService.java +14 −11 Original line number Diff line number Diff line Loading @@ -1077,13 +1077,16 @@ public final class ActivityManagerService extends ActivityManagerNative /** * Runtime CPU use collection thread. This object's lock is used to * protect all related state. * perform synchronization with the thread (notifying it to run). */ final Thread mProcessCpuThread; /** * Used to collect process stats when showing not responding dialog. * Protected by mProcessCpuThread. * Used to collect per-process CPU use for ANRs, battery stats, etc. * Must acquire this object's lock when accessing it. * NOTE: this lock will be held while doing long operations (trawling * through all processes in /proc), so it should never be acquired by * any critical paths such as when holding the main activity manager lock. */ final ProcessCpuTracker mProcessCpuTracker = new ProcessCpuTracker( MONITOR_THREAD_CPU_USAGE); Loading Loading @@ -1597,7 +1600,7 @@ public final class ActivityManagerService extends ActivityManagerNative infoMap.put(mi.pid, mi); } updateCpuStatsNow(); synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { final int N = mProcessCpuTracker.countStats(); for (int i=0; i<N; i++) { ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i); Loading Loading @@ -1896,7 +1899,7 @@ public final class ActivityManagerService extends ActivityManagerNative if (memInfo != null) { updateCpuStatsNow(); long nativeTotalPss = 0; synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { final int N = mProcessCpuTracker.countStats(); for (int j=0; j<N; j++) { ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(j); Loading @@ -1914,7 +1917,7 @@ public final class ActivityManagerService extends ActivityManagerNative } } memInfo.readMemInfo(); synchronized (this) { synchronized (ActivityManagerService.this) { if (DEBUG_PSS) Slog.d(TAG, "Collected native and kernel memory in " + (SystemClock.uptimeMillis()-start) + "ms"); mProcessStats.addSysMemUsageLocked(memInfo.getCachedSizeKb(), Loading Loading @@ -2185,7 +2188,7 @@ public final class ActivityManagerService extends ActivityManagerNative return; } synchronized (mActivityManagerService.mProcessCpuThread) { synchronized (mActivityManagerService.mProcessCpuTracker) { pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentLoad()); pw.print(mActivityManagerService.mProcessCpuTracker.printCurrentState( SystemClock.uptimeMillis())); Loading Loading @@ -2381,7 +2384,7 @@ public final class ActivityManagerService extends ActivityManagerNative } void updateCpuStatsNow() { synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { mProcessCpuMutexFree.set(false); final long now = SystemClock.uptimeMillis(); boolean haveNewCpuStats = false; Loading Loading @@ -5076,7 +5079,7 @@ public final class ActivityManagerService extends ActivityManagerNative String cpuInfo = null; if (MONITOR_CPU_USAGE) { updateCpuStatsNow(); synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { cpuInfo = mProcessCpuTracker.printCurrentState(anrTime); } info.append(processCpuTracker.printCurrentLoad()); Loading Loading @@ -13743,7 +13746,7 @@ public final class ActivityManagerService extends ActivityManagerNative findPid = Integer.parseInt(args[opti]); } catch (NumberFormatException e) { } synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { final int N = mProcessCpuTracker.countStats(); for (int i=0; i<N; i++) { ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i); Loading Loading @@ -13905,7 +13908,7 @@ public final class ActivityManagerService extends ActivityManagerNative // If we are showing aggregations, also look for native processes to // include so that our aggregations are more accurate. updateCpuStatsNow(); synchronized (mProcessCpuThread) { synchronized (mProcessCpuTracker) { final int N = mProcessCpuTracker.countStats(); for (int i=0; i<N; i++) { ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
services/core/java/com/android/server/am/ActivityStack.java +3 −8 Original line number Diff line number Diff line Loading @@ -1042,11 +1042,8 @@ final class ActivityStack { if (prev.app != null && prev.cpuTimeAtResume > 0 && mService.mBatteryStatsService.isOnBattery()) { long diff; synchronized (mService.mProcessCpuThread) { diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid) long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid) - prev.cpuTimeAtResume; } if (diff > 0) { BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics(); synchronized (bsi) { Loading Loading @@ -1097,9 +1094,7 @@ final class ActivityStack { // TODO: To be more accurate, the mark should be before the onCreate, // not after the onResume. But for subsequent starts, onResume is fine. if (next.app != null) { synchronized (mService.mProcessCpuThread) { next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid); } } else { next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process } Loading