Fetch Application CPU , Memory Usage ,Running Process
Below given generic functions can be used in your program without any major mod.
Reference sites :
http://android-test-tw.blogspot.in/2012/10/dumpsys-information-android-open-source.html
http://www.m2catalyst.com/tutorial-finding-cpu-usage-for-individual-android-apps/
http://stackoverflow.com/questions/15148193/how-to-get-higher-precision-of-cpu-than-that-from-top-command
http://stackoverflow.com/questions/11201659/whats-android-adb-shell-dumpsys-tool-and-its-benefits
http://codeseekah.com/2012/10/21/android-shell-tricks-ps/ -(Android shell tricks: ps)
//Fetches the CPU usage of the given Process - Currently all process @ 0%
private String process_name_cpu(String Process_name)
{
try{
ArrayList<String> list2 = new ArrayList<String>();
//adb shell dumpsys cpuinfo
//(adb shell ps | grep com.android.phone | awk '{ system("adb shell cat /proc/" $2 "/stat");}' | awk '{print $14+$15;}')
//ps -eo pcpu,pid,user,args | sort -r -k1 | less
//ps -t -x -P -p -c
//ps -t -x -P -p -c [pid|name]
//com.android.calculator2|2302
//Process p2 = Runtime.getRuntime().exec("ps -t -x -P -p -c ["+2302+"]",null,null);
Process p2 = Runtime.getRuntime().exec("top -m 150 -d 1 -n 1",null,null);//top -m 150 -d 1 -n 1",null,null);
BufferedReader reader2 = new BufferedReader(new InputStreamReader(p2.getInputStream()));
int i2 = 0;
String line2 = reader2.readLine();
String a[]={""};
// System.out.println(line2);
while (line2 != null) {
// Log.e("Output " + i2, line2);
list2.add(line2);
line2 = reader2.readLine();
i2++ ;
}
p2.waitFor();
for (int i=0 ;i<=list2.size();i++)
{
if(list2.get(i).contains(Process_name)==true)
{
a=list2.get(i).toString().split(" ");
break;
}
}
for(int i=0;i<=a.length;i++)
{
if(a[i].contains("%")==true){
System.out.println("cpu utilization" + a[i]);
return a[i];
}
}
}catch (Throwable t)
{
System.out.println("Unable to fetch cpu data ---\n"+t.fillInStackTrace());
}
return null;
}
//---------------------------------------------------------------
//Description: Retrieves the process name from the application
//Arguements required : application_name -- String Type---Input
//Ex: app_to_process_name("system")
//returns cpu usage --String Type (ex:com.android.system)
private String app_to_process_name(String application_name)
{
try{
ArrayList<AppActivity> res = new ArrayList<AppActivity>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
String sApp_name,sProcess_name;
for(int i=0;i<packs.size();i++)
{
PackageInfo p = packs.get(i);
if ((p.versionName == null))
{
continue ;
}
sApp_name = p.applicationInfo.loadLabel(getPackageManager()).toString();
sProcess_name=p.applicationInfo.processName.toString();
if(sApp_name.contentEquals(application_name))
{
//System.out.println("gotcha"+ sProcess_name );
return sProcess_name; //break;
}
}
}catch (Throwable t)
{
System.out.println("Unable to fetch the process name from the app\n"+t.getMessage());
}
return null;
}
//Description: Retreives the memory consumed by the particular process
//Arguements required : sProcess -- String Type---Input
//Ex: process_memory("com.android.system")
//returns memory usage --integer Type (ex: 22845) /// in kb
// NOTE: Fetchs the PSS memory of the process
private int process_memory(String sProcess)
{
try{
ActivityManager localActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); // use Context.ACTIVITY_SERVICE not the literal "activity"
List<ActivityManager.RunningAppProcessInfo> procsInfo = localActivityManager.getRunningAppProcesses();
int[] pids = new int[procsInfo.size()];
for (int i = 0; i < procsInfo.size(); i++)
{
ActivityManager.RunningAppProcessInfo info = procsInfo.get(i);
pids[i] = info.pid;
}
Debug.MemoryInfo[] procsMemInfo = localActivityManager.getProcessMemoryInfo(pids);
for (int idx = 0; idx < procsMemInfo.length; idx++)
{
if(procsInfo.get(idx).processName.toString().equalsIgnoreCase(sProcess))
{
System.out.println("memory" + procsMemInfo[idx].getTotalPss());
return procsMemInfo[idx].getTotalPss();
}
}
}catch (Throwable t)
{
Log.v("process missing","Unable to find the process");
System.out.println("Process not found---\n"+t.getMessage());
// return 0;
}
return 0;
}
No comments:
Post a Comment