1. 程式人生 > >android用memtester記憶體壓力測試

android用memtester記憶體壓力測試

硬體搭建起來能進入系統,首要就是測試記憶體的穩定性,需要一款記憶體測試工具。

一般都是選擇memtester這款Linux軟體,下載地址如下:http://pyropus.ca/software/memtester/

把memtester解壓到Android/external/下(此文預設讀者已有Android SDK)

-----------------------------------------------------------------------------------------------------------------------------

移植:

1:分別把conf-ld和conf-cc檔案中的"cc"(一般在檔案首行)替換成“arm-linux-androideabi-gcc”

2:編寫Android.mk,程式碼如下:

按 Ctrl+C 複製程式碼 LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := memtester.c tests.c
LOCAL_MODULE := memtester
LOCAL_C_INCLUDES := $(LOCAL_PATH)
#LOCAL_MODULE_PATH :=  output path of module!
include $(BUILD_EXECUTABLE)

Line 1:返回當前路徑;Line 2:清除LOCAL_XXX變數;Line 3:編譯模組原始碼檔案;

Line 4:模組輸出名字;Line 5:額外的C標頭檔案;Line 6:生成編譯模組的路徑;

Line 7 生產可執行檔案(亦可是庫,根據變數而定)。

Line 1 2 3 4 7是Android.mk最基本的模板。

到此移植完成!

-----------------------------------------------------------------------------------------------------------------------------

編譯:

cd android,source build/envsetup.sh,lunch對應的平臺

mmm  android/external/memtester-xxx ("xxx"為你的memtester版本)

最終在/system/bin生成可執行檔案memtester!

以下是自己的Java執行memtester過程:

主檔案:

package com.example.ddr;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;


import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;




public class MainActivity extends Activity {
private static final String TAG = null;
private static TextView test_result = null;
static String test_setText = "";
static int Testing_times = 0;
String asdddd1 = "screencap -p /sdcard/screen1.png";
String asdddd2 = "screencap -p /sdcard/screen2.png";
String dir = "/data/data/com.example.ddr/files/";  
Context ctxDealFile = null;
String oldPath1 = "file:///android_asset/memtester";
String oldPath2 = "file:///memtester";
String newPath1 = Environment.getRootDirectory().getPath();//"system/";//"system/bin/";
String MemtesterFileName = "/data/memtester_result.txt";
String simpleMemtester_1 = "memtester -b -g -f 1190400--1190400 64M 1";
String complexMemtester_1 = "memtester -b -j 0 -k 4 -g -l 40 -a 64M";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        test_result = (TextView)findViewById(R.id.Memtester_01);
        System.out.println("csh ddr star");
        Memtester(simpleMemtester_1);
        Memtester(complexMemtester_1);
        //ReadTxtFile(MemtesterFileName);
        //copyFile(oldPath2,newPath1);
        /*String uiFileName = "memtester";
        
try {
ctxDealFile = this.createPackageContext("com.example.ddr",
Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}        
deepFile(ctxDealFile, uiFileName);*/
    }
    public void deepFile(Context ctxDealFile, String path) {
try {
String str[] = ctxDealFile.getAssets().list(path);
if (str.length > 0) {
System.out.println("csh deepFile 1");
File file = new File("/system/" + path);
file.mkdirs();
for (String string : str) {
path = path + "/" + string;
System.out.println("zhoulc:\t" + path);
// textView.setText(textView.getText()+"\t"+path+"\t");
deepFile(ctxDealFile, path);
path = path.substring(0, path.lastIndexOf('/'));
}
} else {
System.out.println("csh deepFile 2");
InputStream is = ctxDealFile.getAssets().open(path);
//FileOutputStream fos = new FileOutputStream(new File(getFilesDir().getAbsolutePath()+"/memtester"));
FileOutputStream fos = ctxDealFile.openFileOutput(path, 
ctxDealFile.MODE_WORLD_READABLE |ctxDealFile.MODE_WORLD_WRITEABLE);
byte[] buffer = new byte[1024];
int count = 0;
while (true) {
count++;
int len = is.read(buffer);
if (len == -1) {
break;
}
fos.write(buffer, 0, len);
}
is.close();
fos.close();

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Process p;
Process p2;
try {
p = Runtime.getRuntime().exec("chmod 777 " + dir + path);
p.waitFor();
p2 = Runtime.getRuntime().exec("cp /data/data/com.example.ddr/files/memtester > /data/data/com.example.ddr/files/memtester2");
p2.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
    public void  Memtester(String command)
    {
        Runtime r = Runtime.getRuntime();
        Process p;
        Testing_times ++;
            try {
                p = r.exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));
                String inline;
                while ((inline = br.readLine()) != null) {
                    System.out.println(inline);
                }
                br.close();
p.waitFor();
ReadTxtFile(MemtesterFileName);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
    }
    /*public void  Memtester2(String command)
    {
    DataOutputStream os = null;
    Process p;
    try{
    p =Runtime.getRuntime().exec(isRoot?COMMAND_SU:COMMAND_SH);
    os = new DataOutputStream(p.getOutputStream());
    for (String command:commands){
    if(command==null){
    continue;
    }


    //donnotuseos.writeBytes(commmand),avoidchinesecharseterror
    os.write(command.getBytes());
    os.writeBytes(COMMAND_LINE_END);
    os.flush();


    try{
    }catch(Exception e){
    e.printStackTrace();
    }
    }*/
    public static String ReadTxtFile(String strFilePath)
    {
        String path = strFilePath;
        String content = "";
            File file = new File(path);
            if (file.isDirectory())
            {
                Log.d("TestFile", "The File doesn't not exist.");
            }
            else
            {
                try {
                    InputStream instream = new FileInputStream(file); 
                    if (instream != null) 
                    {
                        InputStreamReader inputreader = new InputStreamReader(instream);
                        BufferedReader buffreader = new BufferedReader(inputreader);
                        String line;
                        while (( line = buffreader.readLine()) != null) {
                            content += line + "\n";
                            //if(inline.equals("SUCCESS")){
                            if(line.indexOf("SUCCESS")!=-1){
                            test_setText += Testing_times + " : " + "SUCCESS \n";
                            test_result.setText(test_setText);
                            }else{
                            test_setText += Testing_times + " : " + "FAIL \n";
                            test_result.setText(test_setText);
                            }
                        }                
                        instream.close();
                    }
                }
                catch (java.io.FileNotFoundException e) 
                {
                    Log.d("TestFile", "The File doesn't not exist.");
                } 
                catch (IOException e) 
                {
                     Log.d("TestFile", e.getMessage());
                }
            }
            return content;
    }    


    /*
    private void simpleMemtester(){
    Log.d(TAG, "do simple memory test");
    openCmdList1.clear();
    try {
    openCmdList1.add("memtester -b -g -f 1190400--1190400 64M 1");
    exec(openCmdList1);
    memoryTestResult();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }




    private void complexMemtester(){
    Log.d(TAG, "do complex memory test");
    openCmdList1.clear();
    try {
    openCmdList1.add("memtester -b -j 0 -k 4 -g -l 40 -a 64M");
    exec(openCmdList1);
    memoryTestResult();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    */


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2.把APK預置到程式碼裡面給個系統許可權:

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)


LOCAL_MODULE_TAGS := optional
LOCAL_CERTIFICATE := platform
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SDK_VERSION := current
LOCAL_PACKAGE_NAME := ddr


include $(BUILD_PACKAGE)
# Use the following include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))

3,配置檔案配置手機UID許可權:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.memtester_ckt"
    android:versionCode="1"
    android:versionName="1.0"
android:sharedUserId="android.uid.system">


    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

4,把檔案用android程式碼預置到手機system/bin下面,也就給手機配置了指令碼環境:

  CSH_DDR_PATH := packages/apps/ddr/assets
PRODUCT_COPY_FILES := $(CSH_DDR_PATH)/memtester:system/bin/memtester $(PRODUCT_COPY_FILES)