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        // Skip the first two lines since there
105        // is a new media.log introudced recently.
106        String memusage = poList[2].concat("\n");
107        return memusage;
108    }
109
110    private int getVsize() {
111        String memoryUsage = captureMemInfo();
112        String[] poList2 = memoryUsage.split("\t|\\s+");
113        String vsize = poList2[3];
114        int vsizevalue = Integer.parseInt(vsize);
115        Log.v(TAG, "VSIZE = " + vsizevalue);
116        return vsizevalue;
117    }
118
119    // Write the startup media memory mOutput to the file
120    public void getStartMemoryLog() throws Exception {
121        String memusage = null;
122        mStartMemory = getVsize();
123        mOutput.write(mTestName + '\n');
124        mOutput.write("Start memory : " + mStartMemory + "\n");
125        memusage = captureMemInfo();
126        mOutput.write(memusage);
127    }
128
129    // Write the ps mediaserver mOutput to the file
130    public void getMemoryLog() throws Exception {
131        String memusage = null;
132        memusage = captureMemInfo();
133        mOutput.write(memusage);
134        mOutput.flush();
135    }
136
137    public void getMemorySummary() throws Exception {
138        int endMemory = 0;
139        int memDiff = 0;
140
141        endMemory = getVsize();
142        memDiff = endMemory - mStartMemory;
143
144        mOutput.write("End Memory :" + endMemory + "\n");
145        if (memDiff < 0) {
146            memDiff = 0;
147        }
148        mOutput.write(mTestName + " total diff = " + memDiff);
149        mOutput.write("\n\n");
150        validateProcessStatus();
151        mOutput.flush();
152        mOutput.close();
153    }
154}
155