`
huangqinqin
  • 浏览: 358982 次
  • 性别: Icon_minigender_2
  • 来自: 福州
社区版块
存档分类
最新评论

Android中cpu,memory,Battery的计算

 
阅读更多

1 Memory的分配(RAM而非ROM)
网址:
•http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android/2299813#2299813
•http://unixfoo.blogspot.tw/2008/02/know-about-procmeminfo.html
•http://www.redhat.com/advice/tips/meminfo.html

1.1 读取系统文件:/proc/meminfo



图1

•  MemTotal: Total usable RAM in kilobytes (i.e. physical memory minus a few reserved bytes and the kernel binary code)
•  MemFree: The amount of physical RAM left unused by the system.
•  Buffers: The amount of physical RAM used for file buffers.
•  Cached: The amount of physical RAM used as cache memory. Memory in the pagecache (diskcache) minus SwapCache.
•  SwapCache: This is the amount of Swap used as cache memory. Memory that once was swapped out, is swapped back in, but is still in the swapfile.

解析上述文件内容,并以键值对的形式保存。

1.2 计算总Memory大小及剩余Memory大小
总Memory大小 = MemTotal
剩余Memory大小 = MemFree + Buffers + Cached
1.3 Android中获取memory信息
1》通过ActivityManager.MemoryInfo获取系统memory信息。
availMem:系统可用内存(大于实际可用内存,包括一些优先级低的应用及后台服务所占内存,但当优先级高的process需请求memory时,这些优先级低的process可以随时被kill掉,前提是当前处于lowMemory状态)。
totalMem:android4.1.2(API level 16)及以上支持
2》通过Debug.MemoryInfo获取该进程(正在运行的app)memory信息。
dalvikPrivateDirty:进程占用内存大小(独占,不和任何其他processes共享)

2 Cpu的占用率

2.1 计算总Cpu占用率
2.1.1 读取系统文件:/proc/stat

图2
(注意:Intr 这行还有很多参数,这里我们无需考虑)
上图中第一行的数值表示的是CPU总的使用情况,所以我们只要用第一行的数字计算就可以了。下表解析第一行各数值的含义:
参数         解析(单位:jiffies)
user (3082057)    从系统启动开始累计到当前时刻,处于用户态的运行时间,不包含 nice值为负的进程。
nice (62814)      从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间
system (1636874)  从系统启动开始累计到当前时刻,处于核心态的运行时间
idle (4204930)     从系统启动开始累计到当前时刻,除IO等待时间以外的其它等待时间
iowait (29277) 从系统启动开始累计到当前时刻,IO等待时间(since 2.5.41)
irq (253)        从系统启动开始累计到当前时刻,硬中断时间(since 2.6.0-test4)
softirq (2868)    从系统启动开始累计到当前时刻,软中断时间(since 2.6.0-test4)
stealstolen(0)   which is the time spent in other operating systems when running in a virtualized environment(since 2.6.11)
guest(0)         which is the time spent running a virtual  CPU  for  guest operating systems under the control of the Linux kernel(since 2.6.24)

2.1.2 计算总cpu时间

结论一:总的cpu时间totalCpuTime = user + nice + system + idle + iowait + irq + softirq + stealstolen  +  guest

2.1.3 总Cpu使用率的计算

计算方法:
3.1、  采样两个足够短的时间间隔(如1s)的Cpu快照,分别记作t1,t2,其中t1、t2的结构均为:
(user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)的9元组;
3.1.1、  计算总的Cpu时间片totalCpuTime
a)         把第一次的所有cpu使用情况求和,得到s1;
b)         把第二次的所有cpu使用情况求和,得到s2;
c)         s2 - s1得到这个时间间隔内的所有时间片,即totalCpuTime = s2 - s1 ;
3.2、计算空闲时间idle
idle对应第四列的数据,用第二次的第四列 - 第一次的第四列即可
idle=第二次的第四列 - 第一次的第四列
3.3、计算cpu使用率
pcpu =100* (total-idle)/total

2.2计算单个App Cpu 的占用率

2.2.1 读取系统文件:/proc/pid/stat

图3
/proc/pid/stat中记录了进程号为pid的进程所有活动信息,与Cpu使用率相关参数有(上述字段从索引第14个到17个):
参数                             解释
pid=13533                       进程号
utime=1583                      该任务在用户态运行的时间,单位为jiffies
stime=430                       该任务在核心态运行的时间,单位为jiffies
cutime=1044825                  所有曾经在用户态运行的线程所占的时间,单位为jiffies
cstime=48675                    所有曾经在核心态运行的线程所占的时间,单位为jiffies

2.2.2 计算App所占cpu时间

结论二:进程的总Cpu时间processCpuTime = utime + stime + cutime + cstime,(该值包括其所有线程的cpu时间)。

2.2.3 单个App Cpu的使用率

计算方法:
1.        采样两个足够短的时间间隔(如1s)的cpu快照与进程快照,
a)         每一个cpu快照均为(user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)的9元组;
b)         每一个进程快照均为 (utime、stime、cutime、cstime)的4元组;
2.        分别根据结论一、结论二计算出两个时刻的总的cpu时间与进程的cpu时间,分别记作:totalCpuTime1、totalCpuTime2、processCpuTime1、processCpuTime2
3.        计算该进程的cpu使用率pcpu = 100*( processCpuTime2 – processCpuTime1) / (totalCpuTime2 – totalCpuTime1) (按100%计算,如果是多核情况下还需乘以cpu的个数);

3 电池电量消耗统计

3.1 Android中影响电池电量大量消耗的主要因素
大量网络数据传输(视频)、传感器(游戏)、大量文本数据的解析(解析器中Tree,Stream样式)、程序内部大量使用锁机制(WakeLock)、没有养成手动释放内存的习惯(造成GC多次被创建)等。

图4

图5
3.2 Android中获取电池电量
1》 定义广播接收器
    class BatteryReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
        intent.getExtras().getInt("level");
        intent.getExtras().getInt("scale");
        intent.getExtras().getInt("health");
       intent.getExtras().getInt("plugged");
        intent.getExtras().getBoolean("present");
       intent.getExtras().getInt("status");
      intent.getExtras().getString("technology");
        intent.getExtras().getInt("temperature");
        intent.getExtras().getInt("voltage");
        }
}
2》注册广播Intent.ACTION_BATTERY_CHANGED
BatteryReceiver  mReceiver = new BatteryReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mReceiver, filter);

3》反注册广播

unregisterReceiver(mReceiver)


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics