First, the method
Copy Code code as follows:
/**
* Execute a shell command and return the string value
*
* @param cmd
* An array of command names & parameters (for example: {"/system/bin/cat", "/proc/version"})
* @param workdirectory
* Command execution path (for example: "system/bin/")
* @return A string of execution results
* @throws IOException
*/
public static synchronized string run (string[] cmd, string workdirectory)
Throws IOException {
StringBuffer result = new StringBuffer ();
try {
Create an operating system process (can also be started by runtime.exec ())
Runtime Runtime = Runtime.getruntime ();
Process proc = runtime.exec (cmd);
InputStream InputStream = Proc.getinputstream ();
Processbuilder builder = new Processbuilder (cmd);
InputStream in = null;
Set a path (absolute path is not necessarily necessary)
if (workdirectory!= null) {
Set working directory (IBID.)
Builder.directory (New File (workdirectory));
Merging standard errors and standard output
Builder.redirecterrorstream (TRUE);
Start a new process
Process process = Builder.start ();
Read process standard output stream
in = Process.getinputstream ();
byte[] re = new byte[1024];
while (In.read (re)!=-1) {
result = Result.append (new String (re));
}
}
Close the input stream
if (in!= null) {
In.close ();
}
catch (Exception ex) {
Ex.printstacktrace ();
}
return result.tostring ();
}
Second, use
Perform commands such as top, PS, etc. under Linux, and you can also perform a viewing effect through the ADB.
1 The top command is as follows:
Copy Code code as follows:
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. How many processes are displayed at most
-N num Updates to show before exiting. Refresh Times
-D num Seconds to between updates. Refresh interval (default 5 seconds)
-S Col column to sort by <cpu,vss,rss,thr>//by which column
-T show threads instead of processes. Display thread information rather than process
-H Display this help screen. Show Help Documents
$ top-n 1
Top-n 1
Do not put the implementation of the effect, in short, the results are expressed as follows:
Copy Code code as follows:
User 35%, System 13%, IOW 0, IRQ 0%//CPU Usage
User 109 + Nice 0 + Sys + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306//CPU Usage
PID cpu% S #THR VSS RSS pcy UID Name//Process Properties
XX xx% x xx xx xx xxx xx xx
CPU occupancy Rate:
User process
System process
IOW io Wait Time
IRQ Hard Interrupt Time
CPU usage (refers to the time in a minimum time slice, unit jiffies. or the number of processes in the account):
User's run time in client state, does not contain priority value as negative process
CPU time occupied by a process with a negative nice priority value
Sys is in the nuclear mentality of running time
Idle waiting time other than IO wait time
IOW io Wait Time
IRQ Hard Interrupt Time
SIRQ Soft Interrupt Time
Process properties:
The ID of the PID process in the system
cpu% current instantaneous so use CPU occupancy rate
The state of the S process, where s indicates hibernation, R indicates that it is running, Z indicates zombie state, n indicates that the process priority value is negative.
#THR the number of threads currently used by the program
VSS virtual Set Size VM consumes memory (contains memory occupied by shared libraries)
RSS resident Set Size actually uses physical memory (contains memory occupied by shared libraries)
Pcy Ooxx, do not know what Dongdong
UID runs the user ID of the current process
Name program Names Android.process.media
PS: Memory footprint has the following rules: VSS >= RSS >= PSS >= USS
PSS proportional the physical memory that the Set Size actually uses (proportional allocation of memory occupied by shared libraries)
The physical memory occupied by the USS Unique Set Size process alone (does not contain memory occupied by shared libraries)
In the attached Android system->android Top.txt file, from a summary.
2) Executing code
Copy Code code as follows:
Top command
public static final string[] top = {"/system/bin/top", "-N", "1"};
Now execute Top-n 1, we only need the second line (CPU occupancy rate, exact data)
First line: User 35%, System 13%, IOW 0, IRQ 0%/CPU Usage
Second line: User 109 + Nice 0 + Sys + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306
CPU Usage
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 ();
Change to BufferedReader.
BufferedReader buf = new BufferedReader (new InputStreamReader (IS));
do {
line = Buf.readline ();
There are a few empty lines in front
if (Line.startswith ("User")) {
When we read the first line, we read the next row.
line = Buf.readline ();
Break
}
} while (true);
if (is!= null) {
Buf.close ();
Is.close ();
}
catch (IOException e) {
E.printstacktrace ();
}
return line;
}
Gets the information that the top command for the specified application gets
PID cpu% S #THR VSS RSS pcy UID Name//Process Properties
Returns null if the current application is not running
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 ();
Change to BufferedReader.
BufferedReader buf = new BufferedReader (new InputStreamReader (IS));
do {
line = Buf.readline ();
Read to the corresponding pkgname jump out of circulation (or not found)
if (Null = line | | line.endswith (pkgname)) {
Break
}
} while (true);
if (is!= null) {
Buf.close ();
Is.close ();
}
catch (IOException e) {
E.printstacktrace ();
}
return line;
}