Android basic performance data acquisition (/ proc /)

First, the system memory

Read command:

/proc/meminfo

Java code:

	private void click(){
        try{
            String cmd = "/proc/meminfo";
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(cmd)), 1000);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null){
                sb.append(line).append("\n");
            }
            Log.e("/proc/meminfo : " + sb.toString());
            reader.close();
        }catch (Exception ex){
            ex.printStackTrace();
            Log.e("click [/proc/meminfo] exception : " + ex.toString());
        }
    }

Output:

	MemTotal:        5859784 kB
    MemFree:          394708 kB
    MemAvailable:    2660552 kB
    Buffers:            4640 kB
    Cached:          2209396 kB
    SwapCached:        58852 kB
    Active:          2008348 kB
    Inactive:        1365356 kB
    Active(anon):     897500 kB
    Inactive(anon):   350412 kB
    Active(file):    1110848 kB
    Inactive(file):  1014944 kB
    Unevictable:       67908 kB
    Mlocked:           67908 kB
    SwapTotal:       2293756 kB
    SwapFree:        1555948 kB
    Dirty:               480 kB
    Writeback:             0 kB
    AnonPages:       1207652 kB
    Mapped:           476364 kB
    Shmem:             22764 kB
    Slab:             339180 kB
    SReclaimable:     119880 kB
    SUnreclaim:       219300 kB
    KernelStack:       55888 kB
    PageTables:        76060 kB
    NFS_Unstable:          0 kB
    Bounce:                0 kB
    WritebackTmp:          0 kB
    CommitLimit:     5223648 kB
    Committed_AS:   99107264 kB
    VmallocTotal:   263061440 kB
    VmallocUsed:           0 kB
    VmallocChunk:          0 kB
    CmaTotal:         917504 kB
    CmaFree:           34788 kB
    IonTotalCache:    165936 kB
    IonTotalUsed:     195136 kB
    PActive(anon):         0 kB
    PInactive(anon):       0 kB
    PActive(file):         0 kB
    PInactive(file):       0 kB
    Isolate1Free:       6276 kB
    Isolate2Free:       5568 kB
    RsvTotalUsed:     276484 kB

Field Indication Description:

	MemTotal:       所有可用RAM大小。
    MemFree:        LowFree与HighFree的总和,被系统留着未使用的内存。
    Buffers:        用来给文件做缓冲大小。
    Cached:         被高速缓冲存储器(cache memory)用的内存的大小(等于diskcache minus SwapCache)。
    SwapCached:     被高速缓冲存储器(cache memory)用的交换空间的大小。已经被交换出来的内存,仍然被存放在swapfile中,用来在需要的时候很快的被替换而不需要再次打开I/O端口。
    Active:         在活跃使用中的缓冲或高速缓冲存储器页面文件的大小,除非非常必要,否则不会被移作他用。
    Inactive:       在不经常使用中的缓冲或高速缓冲存储器页面文件的大小,可能被用于其他途径。
    SwapTotal:      交换空间的总大小。
    SwapFree:       未被使用交换空间的大小。
    Dirty:          等待被写回到磁盘的内存大小。
    Writeback:      正在被写回到磁盘的内存大小。
    AnonPages:     未映射页的内存大小。
    Mapped:         设备和文件等映射的大小。
    Slab:           内核数据结构缓存的大小,可以减少申请和释放内存带来的消耗。
    SReclaimable:   可收回Slab的大小。
    SUnreclaim:    不可收回Slab的大小(SUnreclaim+SReclaimable=Slab)。
    PageTables:    管理内存分页页面的索引表的大小。
    NFS_Unstable:   不稳定页表的大小。

The total capacity of system memory: only need to read the first field "/ proc / meminfo" file "MemTotal" on it.

The system is idle memory: just need to get through ActivityManager.

	//系统空闲内存
    public static long getSysFreeMemory(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        return mi.availMem;
    }

Second, the process memory

Process memory limit:

	//进程内存上限
    public static int getMemoryMax() {
        return (int) (Runtime.getRuntime().maxMemory()/1024);
    }

Total memory process:

	//进程总内存
    public static int getPidMemorySize(int pid, Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        int[] myMempid = new int[] { pid };
        Debug.MemoryInfo[] memoryInfo = am.getProcessMemoryInfo(myMempid);
        int memSize = memoryInfo[0].getTotalPss();
        //        dalvikPrivateDirty: The private dirty pages used by dalvik。
        //        dalvikPss :The proportional set size for dalvik.
        //        dalvikSharedDirty :The shared dirty pages used by dalvik.
        //        nativePrivateDirty :The private dirty pages used by the native heap.
        //        nativePss :The proportional set size for the native heap.
        //        nativeSharedDirty :The shared dirty pages used by the native heap.
        //        otherPrivateDirty :The private dirty pages used by everything else.
        //        otherPss :The proportional set size for everything else.
        //        otherSharedDirty :The shared dirty pages used by everything else.
        return memSize;
    }

GT3.1 open source code for acquiring data memory:
https://github.com/Tencent/GT/blob/ed3a289a897d0ea14676a7c7d92344f5b398991e/android/GT_APP/app/src/main/java/com/tencent/wstt/gt/api/utils/MemUtils .java


Three, CPU articles

CPU Busy represent processes or threads:

The main CPU acquired in two ways:

  • One is to use the top command or dumpsys cpuinfo,
  • The second is reading / proc / stat file, and then parse the relevant parameters, to calculate their own.

The following describes the second method, but also personally recommended method

/ Proc file system is a pseudo file system exists only among the memory, external memory without taking up space. It provides a filesystem interface to communicate with the kernel process. Users and applications can obtain information through the system / proc, and may change some of the kernel parameters. Due to information systems, such as the process is dynamically changing, so the user or application read the file / proc directory, proc file system is a dynamic system kernel is read out from the required information and submit. Proc file can be obtained from the system, processes, threads of cpu time slice usage, the data acquisition time slice twice can obtain the process CPU utilization, CPU occupancy rate = (T2- Process Process T1) / (T2- system system T1) is the ratio of the time slice.

1, CPU time slice acquisition system

Acquisition system CPU time slice usage: reading proc / stat, the contents of the following documents:

	cpu 2032004 102648 238344 167130733 758440 15159 17878 0
    cpu0 1022597 63462 141826 83528451 366530 9362 15386 0
    cpu1 1009407 39185 96518 83602282 391909 5796 2492 0
    intr 303194010 212852371 3 0 0 11 0 0 2 1 1 0 0 3 0 11097365 0 72615114 6628960 0 179 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    ctxt 236095529
    btime 1195210746
    processes 401389
    procs_running 1
    procs_blocked 0

Meaning of each field in the first line:

	user (14624) 从系统启动开始累计到当前时刻,处于用户态的运行时间,不包含 nice值为负进程。 
    nice (771) 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间 
    system (8484) 从系统启动开始累计到当前时刻,处于核心态的运行时间 
    idle (283052) 从系统启动开始累计到当前时刻,除IO等待时间以外的其它等待时间 
    iowait (0) 从系统启动开始累计到当前时刻,IO等待时间(since 2.5.41) 
    irq (0) 从系统启动开始累计到当前时刻,硬中断时间(since 2.6.0-test4) 
    softirq (62) 从系统启动开始累计到当前时刻,软中断时间(since 2.6.0-test4) 

The total cpu time totalCpuTime = user + nice + system + idle + iowait + irq + softirq

2, CPU time slice acquisition process and thread

Get Process CPU time slice usage: read proc / pid / stat, get thread CPU time slice usage: read proc / pid / task / tid / stat, the same content of these two documents, as follows

6873 (a.out) R 6723 6873 6723 34819 6873 8388608 77 0 0 0 41958 31 0 0 25 0 3 0 5882654 1409024 56 4294967295 134512640 134513720 3215579040 0 2097798 0 0 0 0 0 0 0 17 0 0 0

The meaning of each field:

	pid=6873                  进程(包括轻量级进程,即线程)号
    comm=a.out                应用程序或命令的名字
    task_state=R              任务的状态,R:runnign, S:sleeping (TASK_INTERRUPTIBLE), D:disk sleep (TASK_UNINTERRUPTIBLE), T: stopped, T:tracing stop,Z:zombie, X:dead
    ppid=6723                 父进程ID
    pgid=6873                 线程组号
    sid=6723                  c该任务所在的会话组ID
    tty_nr=34819(pts/3)       该任务的tty终端的设备号,INT(34817/256=主设备号,(34817-主设备号)=次设备号
    tty_pgrp=6873             终端的进程组号,当前运行在该任务所在终端的前台任务(包括shell 应用程序)的PID。
    task->flags=8388608       进程标志位,查看该任务的特性
    min_flt=77                该任务不需要从硬盘拷数据而发生的缺页(次缺页)的次数
    cmin_flt=0                累计的该任务的所有的waited-for进程曾经发生的次缺页的次数目
    maj_flt=0                 该任务需要从硬盘拷数据而发生的缺页(主缺页)的次数
    cmaj_flt=0                累计的该任务的所有的waited-for进程曾经发生的主缺页的次数目
    utime=1587                该任务在用户态运行的时间,单位为jiffies
    stime=1                   该任务在核心态运行的时间,单位为jiffies
    cutime=0                  累计的该任务的所有的waited-for进程曾经在用户态运行的时间,单位为jiffies
    cstime=0                  累计的该任务的所有的waited-for进程曾经在核心态运行的时间,单位为jiffies
    priority=25               任务的动态优先级
    nice=0                    任务的静态优先级
    num_threads=3             该任务所在的线程组里线程的个数
    it_real_value=0           由于计时间隔导致的下一个 SIGALRM 发送进程的时延,以 jiffy 为单位.
    start_time=5882654        该任务启动的时间,单位为jiffies
    vsize=1409024           (page)该任务的虚拟地址空间大小
    rss=56(page)             该任务当前驻留物理地址空间的大小
    rlim=4294967295(bytes)  该任务能驻留物理地址空间的最大值
    start_code=134512640     该任务在虚拟地址空间的代码段的起始地址
    end_code=134513720       该任务在虚拟地址空间的代码段的结束地址
    start_stack=3215579040   该任务在虚拟地址空间的栈的结束地址
    kstkesp=0                esp(32 位堆栈指针) 的当前值, 与在进程的内核堆栈页得到的一致.
    kstkeip=2097798          指向将要执行的指令的指针, EIP(32 位指令指针)的当前值.
    pendingsig=0             待处理信号的位图,记录发送给进程的普通信号
    block_sig=0              阻塞信号的位图
    sigign=0                 忽略的信号的位图
    sigcatch=082985          被俘获的信号的位图
    wchan=0                  如果该进程是睡眠状态,该值给出调度的调用点
    nswap                    被swapped的页数,当前没用
    cnswap                   所有子进程被swapped的页数的和,当前没用
    exit_signal=17           该进程结束时,向父进程所发送的信号
    task_cpu(task)=0         运行在哪个CPU上
    task_rt_priority=0       实时进程的相对优先级别
    task_policy=0            进程的调度策略,0=非实时进程,1=FIFO实时进程;2=RR实时进程

The total Cpu time processCpuTime process = utime + stime + cutime + cstime
total Cpu time thread threadCpuTime = utime + stime + cutime + cstime

Data acquisition time slice twice acquiring process CPU utilization
CPU occupancy rate = (T2- Process Process T1) / (T2- system system T1) time slice ratio

注:7.0以上的Android系统,/proc/stat无权限获取。



Fourth, traffic

流量 : Indicates the current usage of the network process.

There are two ways:

  • Android class is acquired by TrafficStats provided;
  • Calculated by taking the proc file contents.

Look at the first method GT3.1 used:

TrafficStats Source View TrafficStats class is a start from your phone is switched offered by Android, the cumulative total traffic to use now, or one or more statistical flow process or application used, of course, this traffic includes the Wifi and mobile data network Gprs.

	//系统流量统计:
    TrafficStats.getTotalRxBytes() ——获取从此次开机起总接受流量(流量是分为上传与下载两类的);
    TrafficStats.getTotalTxBytes()——获取从此次开机起总发送流量;
    TrafficStats.getMobileRxBytes()——获取从此次开机起不包括Wifi的接受流量,即只统计数据网Gprs接受的流量;
    TrafficStats.getMobileTxBytes()——获取从此次开机起不包括Wifi的发送流量,即只统计数据网Gprs发送的流量;
    //进程流量统计:
    TrafficStats.getUidRxBytes(mUid)
    TrafficStats.getUidTxBytes(mUid)  

Get Process Flow method:

u0_aAndroid is the beginning of the application process, UID applications for Android from the beginning 10000, 19999 to the end.

	//获取流量数据,上行和下行
	//这里mUid是应用的uid,非进程id pid,注意区分
	//uid获取可根据包名得到,方法如下:
	public static int getUidByPkgName(String pkgname)  {
        PackageManager pm = getPackageManager();
        try {
            ApplicationInfo ai = pm.getApplicationInfo(pkgname, 0);
            Log.i(TAG,String.valueOf(ai.uid));
            return ai.uid;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
	}
	public  static TrafficInfo collect(int mUid) {
        long upload  = TrafficStats.getUidRxBytes(mUid);
        long download = TrafficStats.getUidTxBytes(mUid);
	}

GT3.1 open source code for acquiring traffic data:

https://github.com/Tencent/GT/blob/ed3a289a897d0ea14676a7c7d92344f5b398991e/android/GT_APP/app/src/main/java/com/tencent/wstt/gt/api/utils/NetUtils.java

Published 100 original articles · won praise 45 · views 640 000 +

Guess you like

Origin blog.csdn.net/wangzhongshun/article/details/102780915