MediaTestUtil.java revision 9dab78470592be7542b9c0f9090603ed0dc80949
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.FileOutputStream;
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.Writer;
23
24import android.os.Debug;
25import android.os.Environment;
26import android.util.Log;
27
28/**
29 *
30 * Utilities for media framework test.
31 *
32 */
33public class MediaTestUtil {
34    private MediaTestUtil(){
35    }
36
37    private static String TAG = "MediaTestUtil";
38    private static final String STORAGE_PATH =
39        Environment.getExternalStorageDirectory().toString();
40    private static int mMediaStartMemory = 0;
41    private static int mDrmStartMemory = 0;
42
43    //Catpure the heapdump for memory leaksage analysis
44    public static void getNativeHeapDump (String name) throws Exception {
45        System.gc();
46        System.runFinalization();
47        Thread.sleep(1000);
48        FileOutputStream o = new FileOutputStream(STORAGE_PATH + '/' +name + ".dump");
49        Debug.dumpNativeHeap(o.getFD());
50        o.close();
51    }
52
53    public static String captureMemInfo(String type) {
54        String cm = "ps ";
55        cm += type;
56        String memoryUsage = null;
57
58        int ch;
59        try {
60            Process p = Runtime.getRuntime().exec(cm);
61            InputStream in = p.getInputStream();
62            StringBuffer sb = new StringBuffer(512);
63            while ((ch = in.read()) != -1) {
64                sb.append((char) ch);
65            }
66            memoryUsage = sb.toString();
67        } catch (IOException e) {
68            Log.v(TAG, e.toString());
69        }
70        String[] poList = memoryUsage.split("\r|\n|\r\n");
71        String memusage = poList[1].concat("\n");
72        return memusage;
73    }
74
75    public static int getMediaServerVsize() {
76        String memoryUsage = captureMemInfo("mediaserver");
77        String[] poList2 = memoryUsage.split("\t|\\s+");
78        String vsize = poList2[3];
79        int vsizevalue = Integer.parseInt(vsize);
80        Log.v(TAG, "VSIZE = " + vsizevalue);
81        return vsizevalue;
82    }
83
84    public static int getDrmServerVsize() {
85        String memoryUsage = captureMemInfo("drmserver");
86        String[] poList2 = memoryUsage.split("\t|\\s+");
87        String vsize = poList2[3];
88        int vsizevalue = Integer.parseInt(vsize);
89        Log.v(TAG, "VSIZE = " + vsizevalue);
90        return vsizevalue;
91    }
92
93    // Write the ps mediaserver output to the file
94    public static void getMediaServerMemoryLog(Writer output, int writeCount, int totalCount)
95            throws Exception {
96        String memusage = null;
97
98        if (writeCount == 0) {
99            mMediaStartMemory = getMediaServerVsize();
100            output.write("Start memory : " + mMediaStartMemory + "\n");
101        }
102        memusage = captureMemInfo("mediaserver");
103        output.write(memusage);
104    }
105
106    // Write the ps drmserver output to the file
107    public static void getDrmServerMemoryLog(Writer output, int writeCount, int totalCount)
108            throws Exception {
109        String memusage = null;
110
111        if (writeCount == 0) {
112            mDrmStartMemory = getDrmServerVsize();
113            output.write("Start memory : " + mDrmStartMemory + "\n");
114        }
115        memusage = captureMemInfo("drmserver");
116        output.write(memusage);
117    }
118
119    // Write the ps drmserver output to the file
120    public static void getDrmServerMemorySummary(Writer output, String tag) throws Exception {
121
122        getTestMemorySummary(output, tag, "drmMem");
123    }
124
125    // Write the ps drmserver output to the file
126    public static void getMediaServerMemorySummary(Writer output, String tag) throws Exception {
127
128        getTestMemorySummary(output, tag, "mediaMem");
129    }
130
131    public static void getTestMemorySummary(Writer output, String tag, String type)
132            throws Exception {
133
134        int endMemory = 0;
135        int memDiff = 0;
136
137        if (type == "mediaMem") {
138            endMemory = getMediaServerVsize();
139            memDiff = endMemory - mMediaStartMemory;
140        } else if (type == "drmMem") {
141            endMemory = getDrmServerVsize();
142            memDiff = endMemory - mDrmStartMemory;
143        }
144        output.write("End Memory :" + endMemory + "\n");
145        if (memDiff < 0) {
146            memDiff = 0;
147        }
148        output.write(tag + " total diff = " + memDiff);
149        output.write("\n\n");
150    }
151}
152