Yu Shan Emily Lau | 62f755a | 2011-02-25 20:07:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.mediaframeworktest; |
| 18 | |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 19 | import java.io.BufferedWriter; |
| 20 | import java.io.File; |
Yu Shan Emily Lau | 62f755a | 2011-02-25 20:07:24 -0800 | [diff] [blame] | 21 | import java.io.FileOutputStream; |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 22 | import java.io.FileWriter; |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.io.Writer; |
Yu Shan Emily Lau | 62f755a | 2011-02-25 20:07:24 -0800 | [diff] [blame] | 26 | |
| 27 | import android.os.Debug; |
| 28 | import android.os.Environment; |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Yu Shan Emily Lau | 62f755a | 2011-02-25 20:07:24 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * |
| 33 | * Utilities for media framework test. |
| 34 | * |
| 35 | */ |
| 36 | public class MediaTestUtil { |
Yu Shan Emily Lau | 62f755a | 2011-02-25 20:07:24 -0800 | [diff] [blame] | 37 | |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 38 | private static String TAG = "MediaTestUtil"; |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 39 | private static final String STORAGE_PATH = Environment.getExternalStorageDirectory().toString(); |
| 40 | private int mStartMemory = 0; |
| 41 | private int mStartPid = 0; |
| 42 | private Writer mOutput = null; |
| 43 | private String mTestName = null; |
| 44 | private String mProcessName = null; |
Yu Shan Emily Lau | 62f755a | 2011-02-25 20:07:24 -0800 | [diff] [blame] | 45 | |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 46 | public MediaTestUtil(String memoryOutFileName, String testName, String processName) |
| 47 | throws Exception { |
| 48 | File memoryOut = new File(memoryOutFileName); |
| 49 | mOutput = new BufferedWriter(new FileWriter(memoryOut, true)); |
| 50 | mProcessName = processName; |
| 51 | mTestName = testName; |
| 52 | mStartPid = getPid(); |
| 53 | mStartMemory = getVsize(); |
| 54 | } |
| 55 | |
| 56 | // Catpure the heapdump for memory leaksage analysis |
| 57 | public static void getNativeHeapDump(String name) throws Exception { |
Yu Shan Emily Lau | 62f755a | 2011-02-25 20:07:24 -0800 | [diff] [blame] | 58 | System.gc(); |
| 59 | System.runFinalization(); |
| 60 | Thread.sleep(1000); |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 61 | FileOutputStream o = new FileOutputStream(STORAGE_PATH + '/' + name + ".dump"); |
Yu Shan Emily Lau | 62f755a | 2011-02-25 20:07:24 -0800 | [diff] [blame] | 62 | Debug.dumpNativeHeap(o.getFD()); |
| 63 | o.close(); |
| 64 | } |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 65 | |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 66 | private void validateProcessStatus() throws Exception { |
| 67 | int currentPid = getPid(); |
| 68 | //Process crash |
| 69 | if (mStartPid != currentPid) { |
| 70 | mOutput.write(mProcessName + " died. Test failed\n"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private int getPid() { |
| 75 | String memoryUsage = null; |
| 76 | int pidvalue = 0; |
| 77 | memoryUsage = captureMemInfo(); |
| 78 | String[] poList2 = memoryUsage.split("\t|\\s+"); |
| 79 | String pid = poList2[1]; |
| 80 | pidvalue = Integer.parseInt(pid); |
| 81 | Log.v(TAG, "PID = " + pidvalue); |
| 82 | return pidvalue; |
| 83 | } |
| 84 | |
| 85 | private String captureMemInfo() { |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 86 | String cm = "ps "; |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 87 | cm += mProcessName; |
| 88 | Log.v(TAG, cm); |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 89 | String memoryUsage = null; |
| 90 | |
| 91 | int ch; |
| 92 | try { |
| 93 | Process p = Runtime.getRuntime().exec(cm); |
| 94 | InputStream in = p.getInputStream(); |
| 95 | StringBuffer sb = new StringBuffer(512); |
| 96 | while ((ch = in.read()) != -1) { |
| 97 | sb.append((char) ch); |
| 98 | } |
| 99 | memoryUsage = sb.toString(); |
| 100 | } catch (IOException e) { |
| 101 | Log.v(TAG, e.toString()); |
| 102 | } |
| 103 | String[] poList = memoryUsage.split("\r|\n|\r\n"); |
| 104 | String memusage = poList[1].concat("\n"); |
| 105 | return memusage; |
| 106 | } |
| 107 | |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 108 | private int getVsize() { |
| 109 | String memoryUsage = captureMemInfo(); |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 110 | String[] poList2 = memoryUsage.split("\t|\\s+"); |
| 111 | String vsize = poList2[3]; |
| 112 | int vsizevalue = Integer.parseInt(vsize); |
| 113 | Log.v(TAG, "VSIZE = " + vsizevalue); |
| 114 | return vsizevalue; |
| 115 | } |
| 116 | |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 117 | // Write the startup media memory mOutput to the file |
| 118 | public void getStartMemoryLog() throws Exception { |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 119 | String memusage = null; |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 120 | mStartMemory = getVsize(); |
| 121 | mOutput.write(mTestName + '\n'); |
| 122 | mOutput.write("Start memory : " + mStartMemory + "\n"); |
| 123 | memusage = captureMemInfo(); |
| 124 | mOutput.write(memusage); |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 127 | // Write the ps mediaserver mOutput to the file |
| 128 | public void getMemoryLog() throws Exception { |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 129 | String memusage = null; |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 130 | memusage = captureMemInfo(); |
| 131 | mOutput.write(memusage); |
| 132 | mOutput.flush(); |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 135 | public void getMemorySummary() throws Exception { |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 136 | int endMemory = 0; |
| 137 | int memDiff = 0; |
| 138 | |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 139 | endMemory = getVsize(); |
| 140 | memDiff = endMemory - mStartMemory; |
| 141 | |
| 142 | mOutput.write("End Memory :" + endMemory + "\n"); |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 143 | if (memDiff < 0) { |
| 144 | memDiff = 0; |
| 145 | } |
Yu Shan Emily Lau | 75afb97 | 2011-03-17 20:36:37 -0700 | [diff] [blame] | 146 | mOutput.write(mTestName + " total diff = " + memDiff); |
| 147 | mOutput.write("\n\n"); |
| 148 | validateProcessStatus(); |
| 149 | mOutput.flush(); |
| 150 | mOutput.close(); |
Yu Shan Emily Lau | 9dab7847 | 2011-03-16 15:44:22 -0700 | [diff] [blame] | 151 | } |
| 152 | } |