一、方法
複製程式碼 程式碼如下:
/**
* 執行一個shell命令,並返回字串值
*
* @param cmd
* 命令名稱&引數組成的陣列(例如:{“/system/bin/cat”, “/proc/version”})
* @param workdirectory
* 命令執行路徑(例如:”system/bin/”)
* @return 執行結果組成的字串
* @throws IOException
*/
public static synchronized String run(String[] cmd, String workdirectory)
throws IOException {
StringBuffer result = new StringBuffer();
try {
// 建立作業系統程序(也可以由Runtime.exec()啟動)
// Runtime runtime = Runtime.getRuntime();
// Process proc = runtime.exec(cmd);
// InputStream inputstream = proc.getInputStream();
ProcessBuilder builder = new ProcessBuilder(cmd);
InputStream in = null;
// 設定一個路徑(絕對路徑了就不一定需要)
if (workdirectory != null) {
// 設定工作目錄(同上)
builder.directory(new File(workdirectory));
// 合併標準錯誤和標準輸出
builder.redirectErrorStream(true);
// 啟動一個新程序
Process process = builder.start();
// 讀取程序標準輸出流
in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1) {
result = result.append(new String(re));
}
}
// 關閉輸入流
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result.toString();
}
二、用途
執行Linux下的top、ps等命令,這些命令你也通過adb可以執行檢視效果。
1)top命令如下:
複製程式碼 程式碼如下:
adb shell
$ top -h
top -h
Usage: top [-m max_procs] [-n iterations] [-d delay] [-s sort_column] [-t] [-h]
-m num Maximum number of processes to display. // 最多顯示多少個程序
-n num Updates to show before exiting. // 重新整理次數
-d num Seconds to wait between updates. // 重新整理間隔時間(預設5秒)
-s col Column to sort by <cpu,vss,rss,thr> // 按哪列排序
-t Show threads instead of processes. // 顯示執行緒資訊而不是程序
-h Display this help screen. // 顯示幫助文件
$ top -n 1
top -n 1
就不把執行效果放上來了,總之結果表述如下:
複製程式碼 程式碼如下:
User 35%, System 13%, IOW 0%, IRQ 0% // CPU佔用率
User 109 Nice 0 Sys 40 Idle 156 IOW 0 IRQ 0 SIRQ 1 = 306 // CPU使用情況
PID CPU% S #THR VSS RSS PCY UID Name // 程序屬性
xx xx% x xx xx xx xx xx xx
CPU佔用率:
User 使用者程序
System 系統程序
IOW IO等待時間
IRQ 硬中斷時間
CPU使用情況(指一個最小時間片內所佔時間,單位jiffies。或者指所佔程序數):
User 處於使用者態的執行時間,不包含優先值為負程序
Nice 優先值為負的程序所佔用的CPU時間
Sys 處於核心態的執行時間
Idle 除IO等待時間以外的其它等待時間
IOW IO等待時間
IRQ 硬中斷時間
SIRQ 軟中斷時間
程序屬性:
PID 程序在系統中的ID
CPU% 當前瞬時所以使用CPU佔用率
S 程序的狀態,其中S表示休眠,R表示正在執行,Z表示僵死狀態,N表示該程序優先值是負數。
#THR 程式當前所用的執行緒數
VSS Virtual Set Size 虛擬耗用記憶體(包含共享庫佔用的記憶體)
RSS Resident Set Size 實際使用實體記憶體(包含共享庫佔用的記憶體)
PCY OOXX,不知道什麼東東
UID 執行當前程序的使用者id
Name 程式名稱android.process.media
// ps:記憶體佔用大小有如下規律:VSS >= RSS >= PSS >= USS
// PSS Proportional Set Size 實際使用的實體記憶體(比例分配共享庫佔用的記憶體)
// USS Unique Set Size 程序獨自佔用的實體記憶體(不包含共享庫佔用的記憶體)
在附件Android系統->android top.txt檔案內,自個總結的。
2)執行程式碼
複製程式碼 程式碼如下:
// top命令
public static final String[] TOP = { “/system/bin/top”, “-n”, “1” };
// 現在執行top -n 1,我們只需要第二行(用第二行求得CPU佔用率,精確資料)
// 第一行:User 35%, System 13%, IOW 0%, IRQ 0% // CPU佔用率
// 第二行:User 109 Nice 0 Sys 40 Idle 156 IOW 0 IRQ 0 SIRQ 1 = 306
// // CPU使用情況
public static synchronized String run(String[] cmd) {
String line = “”;
InputStream is = null;
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
is = proc.getInputStream();
// 換成BufferedReader
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
do {
line = buf.readLine();
// 前面有幾個空行
if (line.startsWith(“User”)) {
// 讀到第一行時,我們再讀取下一行
line = buf.readLine();
break;
}
} while (true);
if (is != null) {
buf.close();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
// 獲取指定應用的top命令獲取的資訊
// PID CPU% S #THR VSS RSS PCY UID Name // 程序屬性
// 如果當前應用不在執行則返回null
public static synchronized String run(String[] cmd, String pkgName) {
String line = null;
InputStream is = null;
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
is = proc.getInputStream();
// 換成BufferedReader
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
do {
line = buf.readLine();
// 讀取到相應pkgName跳出迴圈(或者未找到)
if (null == line || line.endsWith(pkgName)) {
break;
}
} while (true);
if (is != null) {
buf.close();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
您可能感興趣的文章:
android壓力測試命令monkey詳解android除錯工具adb命令大全logcat命令使用方法和檢視android系統日誌緩衝區內容的方法Android ADB常用命令總結使用adb命令向Android模擬器中匯入通訊錄聯絡人的方法Android中刪除檔案以及資料夾的命令記錄Android中使用am命令實現在命令列啟動程式詳解Android筆記之:App除錯的幾個命令的實踐與分析Android常用命令集錦(圖文並茂適應於初學者)android裝置不識別awk命令 缺少busybox怎麼辦
写评论
很抱歉,必須登入網站才能發佈留言。