Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 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.net.Uri; |
| 24 | import android.os.*; |
| 25 | import android.util.Log; |
| 26 | import android.util.LongSparseArray; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 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 currentPss, currentUss; |
| 44 | public long[] pss = new long[256]; |
| 45 | public long[] uss = new long[256]; |
| 46 | //= new Meminfo[(int) (30 * 60 / (UPDATE_RATE / 1000))]; // 30 minutes |
| 47 | public long max = 1; |
| 48 | public int head = 0; |
| 49 | public ProcessMemInfo(int pid, String name) { |
| 50 | this.pid = pid; |
| 51 | this.name = name; |
| 52 | } |
| 53 | }; |
| 54 | public final LongSparseArray<ProcessMemInfo> mData = new LongSparseArray<ProcessMemInfo>(); |
| 55 | public final ArrayList<Long> mPids = new ArrayList<Long>(); |
| 56 | private int[] mPidsArray = new int[0]; |
| 57 | |
| 58 | Handler mHandler = new Handler() { |
| 59 | @Override |
| 60 | public void handleMessage(Message m) { |
| 61 | switch (m.what) { |
| 62 | case MSG_START: |
| 63 | mHandler.removeMessages(MSG_UPDATE); |
| 64 | mHandler.sendEmptyMessage(MSG_UPDATE); |
| 65 | break; |
| 66 | case MSG_STOP: |
| 67 | mHandler.removeMessages(MSG_UPDATE); |
| 68 | break; |
| 69 | case MSG_UPDATE: |
| 70 | update(); |
| 71 | mHandler.removeMessages(MSG_UPDATE); |
| 72 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE, UPDATE_RATE); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | ActivityManager mAm; |
| 79 | |
| 80 | public ProcessMemInfo getMemInfo(int pid) { |
| 81 | return mData.get(pid); |
| 82 | } |
| 83 | |
| 84 | public int[] getTrackedProcesses() { |
| 85 | return mPidsArray; |
| 86 | } |
| 87 | |
| 88 | public void startTrackingProcess(int pid, String name) { |
| 89 | mPids.add(new Long(pid)); |
| 90 | final int N = mPids.size(); |
| 91 | mPidsArray = new int[N]; |
| 92 | StringBuffer sb = new StringBuffer("Now tracking processes: "); |
| 93 | for (int i=0; i<N; i++) { |
| 94 | final int p = mPids.get(i).intValue(); |
| 95 | mPidsArray[i] = p; |
| 96 | sb.append(p); sb.append(" "); |
| 97 | } |
| 98 | mData.put(pid, new ProcessMemInfo(pid, name)); |
| 99 | Log.v(TAG, sb.toString()); |
| 100 | } |
| 101 | |
| 102 | void update() { |
| 103 | Debug.MemoryInfo[] dinfos = mAm.getProcessMemoryInfo(mPidsArray); |
| 104 | for (int i=0; i<dinfos.length; i++) { |
| 105 | Debug.MemoryInfo dinfo = dinfos[i]; |
| 106 | final long pid = mPids.get(i).intValue(); |
| 107 | final ProcessMemInfo info = mData.get(pid); |
| 108 | info.head = (info.head+1) % info.pss.length; |
| 109 | info.pss[info.head] = info.currentPss = dinfo.getTotalPss(); |
| 110 | info.uss[info.head] = info.currentUss = dinfo.getTotalPrivateDirty(); |
| 111 | if (info.currentPss > info.max) info.max = info.currentPss; |
| 112 | if (info.currentUss > info.max) info.max = info.currentUss; |
| 113 | //Log.v(TAG, "update: pid " + pid + " pss=" + info.currentPss + " uss=" + info.currentUss); |
| 114 | } |
| 115 | |
| 116 | // XXX: notify listeners |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onCreate() { |
| 121 | mAm = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onDestroy() { |
| 126 | mHandler.sendEmptyMessage(MSG_STOP); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 131 | Log.i(TAG, "Received start id " + startId + ": " + intent); |
| 132 | |
Daniel Sandler | 4988340 | 2013-06-25 13:20:23 -0400 | [diff] [blame^] | 133 | if (intent != null) { |
| 134 | if (ACTION_START_TRACKING.equals(intent.getAction())) { |
| 135 | final int pid = intent.getIntExtra("pid", -1); |
| 136 | final String name = intent.getStringExtra("name"); |
| 137 | startTrackingProcess(pid, name); |
| 138 | } |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | mHandler.sendEmptyMessage(MSG_START); |
| 142 | |
| 143 | return START_STICKY; |
| 144 | } |
| 145 | |
| 146 | public class MemoryTrackerInterface extends Binder { |
| 147 | MemoryTracker getService() { |
| 148 | return MemoryTracker.this; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private final IBinder mBinder = new MemoryTrackerInterface(); |
| 153 | |
| 154 | public IBinder onBind(Intent intent) { |
| 155 | mHandler.sendEmptyMessage(MSG_START); |
| 156 | |
| 157 | return mBinder; |
| 158 | } |
| 159 | } |