android用memtester内存压力测试

来源:互联网 发布:linux桌面启动器 编辑:程序博客网 时间:2024/06/06 00:24

移植memtester到android平台(移植这段转别人的,自己验证过,OK,感谢原创)

硬件搭建起来能进入系统,首要就是测试内存的稳定性,需要一款内存测试工具。

一般都是选择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)


0 0