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
17package com.android.mediaframeworktest;
18
19import java.io.BufferedWriter;
20import java.io.File;
21import java.io.FileOutputStream;
22import java.io.FileWriter;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.Writer;
26
27import android.os.Debug;
28import android.os.Environment;
29import android.util.Log;
30
31/**
32 *
33 * Utilities for media framework test.
34 *
35 */
36public class MediaTestUtil {
37
38    private static String TAG = "MediaTestUtil";
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;
45
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 {
58        System.gc();
59        System.runFinalization();
60        Thread.sleep(1000);
61        FileOutputStream o = new FileOutputStream(STORAGE_PATH + '/' + name + ".dump");
62        Debug.dumpNativeHeap(o.getFD());
63        o.close();
64    }
65
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() {
86        String cm = "ps ";
87        cm += mProcessName;
88        Log.v(TAG, cm);
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
108    private int getVsize() {
109        String memoryUsage = captureMemInfo();
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
117    // Write the startup media memory mOutput to the file
118    public void getStartMemoryLog() throws Exception {
119        String memusage = null;
120        mStartMemory = getVsize();
121        mOutput.write(mTestName + '\n');
122        mOutput.write("Start memory : " + mStartMemory + "\n");
123        memusage = captureMemInfo();
124        mOutput.write(memusage);
125    }
126
127    // Write the ps mediaserver mOutput to the file
128    public void getMemoryLog() throws Exception {
129        String memusage = null;
130        memusage = captureMemInfo();
131        mOutput.write(memusage);
132        mOutput.flush();
133    }
134
135    public void getMemorySummary() throws Exception {
136        int endMemory = 0;
137        int memDiff = 0;
138
139        endMemory = getVsize();
140        memDiff = endMemory - mStartMemory;
141
142        mOutput.write("End Memory :" + endMemory + "\n");
143        if (memDiff < 0) {
144            memDiff = 0;
145        }
146        mOutput.write(mTestName + " total diff = " + memDiff);
147        mOutput.write("\n\n");
148        validateProcessStatus();
149        mOutput.flush();
150        mOutput.close();
151    }
152}
153