1 /* 2 * Copyright (C) 2013 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.launcher3; 18 19 import android.app.ActivityManager; 20 import android.app.Service; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.*; 24 import android.util.Log; 25 import android.util.LongSparseArray; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 public class MemoryTracker extends Service { 31 public static final String TAG = MemoryTracker.class.getSimpleName(); 32 public static final String ACTION_START_TRACKING = "com.android.launcher3.action.START_TRACKING"; 33 34 private static final long UPDATE_RATE = 5000; 35 36 private static final int MSG_START = 1; 37 private static final int MSG_STOP = 2; 38 private static final int MSG_UPDATE = 3; 39 40 public static class ProcessMemInfo { 41 public int pid; 42 public String name; 43 public long startTime; 44 public long currentPss, currentUss; 45 public long[] pss = new long[256]; 46 public long[] uss = new long[256]; 47 //= new Meminfo[(int) (30 * 60 / (UPDATE_RATE / 1000))]; // 30 minutes 48 public long max = 1; 49 public int head = 0; ProcessMemInfo(int pid, String name, long start)50 public ProcessMemInfo(int pid, String name, long start) { 51 this.pid = pid; 52 this.name = name; 53 this.startTime = start; 54 } getUptime()55 public long getUptime() { 56 return System.currentTimeMillis() - startTime; 57 } 58 }; 59 public final LongSparseArray<ProcessMemInfo> mData = new LongSparseArray<ProcessMemInfo>(); 60 public final ArrayList<Long> mPids = new ArrayList<Long>(); 61 private int[] mPidsArray = new int[0]; 62 private final Object mLock = new Object(); 63 64 Handler mHandler = new Handler() { 65 @Override 66 public void handleMessage(Message m) { 67 switch (m.what) { 68 case MSG_START: 69 mHandler.removeMessages(MSG_UPDATE); 70 mHandler.sendEmptyMessage(MSG_UPDATE); 71 break; 72 case MSG_STOP: 73 mHandler.removeMessages(MSG_UPDATE); 74 break; 75 case MSG_UPDATE: 76 update(); 77 mHandler.removeMessages(MSG_UPDATE); 78 mHandler.sendEmptyMessageDelayed(MSG_UPDATE, UPDATE_RATE); 79 break; 80 } 81 } 82 }; 83 84 ActivityManager mAm; 85 startTrackingMe(Context context, String name)86 public static void startTrackingMe(Context context, String name) { 87 context.startService(new Intent(context, MemoryTracker.class) 88 .setAction(MemoryTracker.ACTION_START_TRACKING) 89 .putExtra("pid", android.os.Process.myPid()) 90 .putExtra("name", name) 91 ); 92 } 93 getMemInfo(int pid)94 public ProcessMemInfo getMemInfo(int pid) { 95 return mData.get(pid); 96 } 97 getTrackedProcesses()98 public int[] getTrackedProcesses() { 99 return mPidsArray; 100 } 101 startTrackingProcess(int pid, String name, long start)102 public void startTrackingProcess(int pid, String name, long start) { 103 synchronized (mLock) { 104 final Long lpid = Long.valueOf(pid); 105 106 if (mPids.contains(lpid)) return; 107 108 mPids.add(lpid); 109 updatePidsArrayL(); 110 111 mData.put(pid, new ProcessMemInfo(pid, name, start)); 112 } 113 } 114 updatePidsArrayL()115 void updatePidsArrayL() { 116 final int N = mPids.size(); 117 mPidsArray = new int[N]; 118 StringBuffer sb = new StringBuffer("Now tracking processes: "); 119 for (int i=0; i<N; i++) { 120 final int p = mPids.get(i).intValue(); 121 mPidsArray[i] = p; 122 sb.append(p); sb.append(" "); 123 } 124 Log.v(TAG, sb.toString()); 125 } 126 update()127 void update() { 128 synchronized (mLock) { 129 Debug.MemoryInfo[] dinfos = mAm.getProcessMemoryInfo(mPidsArray); 130 for (int i=0; i<dinfos.length; i++) { 131 Debug.MemoryInfo dinfo = dinfos[i]; 132 if (i > mPids.size()) { 133 Log.e(TAG, "update: unknown process info received: " + dinfo); 134 break; 135 } 136 final long pid = mPids.get(i).intValue(); 137 final ProcessMemInfo info = mData.get(pid); 138 info.head = (info.head+1) % info.pss.length; 139 info.pss[info.head] = info.currentPss = dinfo.getTotalPss(); 140 info.uss[info.head] = info.currentUss = dinfo.getTotalPrivateDirty(); 141 if (info.currentPss > info.max) info.max = info.currentPss; 142 if (info.currentUss > info.max) info.max = info.currentUss; 143 // Log.v(TAG, "update: pid " + pid + " pss=" + info.currentPss + " uss=" + info.currentUss); 144 if (info.currentPss == 0) { 145 Log.v(TAG, "update: pid " + pid + " has pss=0, it probably died"); 146 mData.remove(pid); 147 } 148 } 149 for (int i=mPids.size()-1; i>=0; i--) { 150 final long pid = mPids.get(i).intValue(); 151 if (mData.get(pid) == null) { 152 mPids.remove(i); 153 updatePidsArrayL(); 154 } 155 } 156 } 157 } 158 159 @Override onCreate()160 public void onCreate() { 161 mAm = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 162 163 // catch up in case we crashed but other processes are still running 164 List<ActivityManager.RunningServiceInfo> svcs = mAm.getRunningServices(256); 165 for (ActivityManager.RunningServiceInfo svc : svcs) { 166 if (svc.service.getPackageName().equals(getPackageName())) { 167 Log.v(TAG, "discovered running service: " + svc.process + " (" + svc.pid + ")"); 168 startTrackingProcess(svc.pid, svc.process, 169 System.currentTimeMillis() - (SystemClock.elapsedRealtime() - svc.activeSince)); 170 } 171 } 172 173 List<ActivityManager.RunningAppProcessInfo> procs = mAm.getRunningAppProcesses(); 174 for (ActivityManager.RunningAppProcessInfo proc : procs) { 175 final String pname = proc.processName; 176 if (pname.startsWith(getPackageName())) { 177 Log.v(TAG, "discovered other running process: " + pname + " (" + proc.pid + ")"); 178 startTrackingProcess(proc.pid, pname, System.currentTimeMillis()); 179 } 180 } 181 } 182 183 @Override onDestroy()184 public void onDestroy() { 185 mHandler.sendEmptyMessage(MSG_STOP); 186 } 187 188 @Override onStartCommand(Intent intent, int flags, int startId)189 public int onStartCommand(Intent intent, int flags, int startId) { 190 Log.v(TAG, "Received start id " + startId + ": " + intent); 191 192 if (intent != null) { 193 if (ACTION_START_TRACKING.equals(intent.getAction())) { 194 final int pid = intent.getIntExtra("pid", -1); 195 final String name = intent.getStringExtra("name"); 196 final long start = intent.getLongExtra("start", System.currentTimeMillis()); 197 startTrackingProcess(pid, name, start); 198 } 199 } 200 201 mHandler.sendEmptyMessage(MSG_START); 202 203 return START_STICKY; 204 } 205 206 public class MemoryTrackerInterface extends Binder { getService()207 MemoryTracker getService() { 208 return MemoryTracker.this; 209 } 210 } 211 212 private final IBinder mBinder = new MemoryTrackerInterface(); 213 onBind(Intent intent)214 public IBinder onBind(Intent intent) { 215 mHandler.sendEmptyMessage(MSG_START); 216 217 return mBinder; 218 } 219 } 220