1. What is proc?
Proc file system is a pseudo file system, which only exists in memory and does not occupy external memory space. It provides a communication interface between kernel and process in the form of file; Users and applications can get the system information through proc and change some parameters of the kernel. Because the system information, such as the process, is dynamically changed, when users or applications read the files in the / proc directory, the proc system file dynamically reads the required information from the system kernel and submits it. The system Enter the CPU time slice, so you can obtain the CPU occupancy of the process by collecting the data of the time slice twice;
Process CPU utilization = (processJiffiesTime2-processJiffiesTime1)/(totalJiffiesTime2)- totalJiffiesTime1)*100%
2. How to obtain the CPU time slice usage of a process at a certain time (processCpuTime1)?
① How to get process ID
ADB shell PS | findstr com.chaozh.ireader.dj (package name)
① Read / proc/pid/stat, which contains the activity information of a process. All values in the modified file are accumulated from the start of system startup to the current time, as follows:
3259 (aozh.iReader.dj) S 4686 4685 0 0 -1 1077952832 216626 17129 38 0 2311 458 24 25 10 -10 272 0 23069645 2541322240 78385 18446744073709551615 1 1 0 0 0 0 4612 1 1073776380 0 0 0 17 0 0 0 5 0 0 0 0 0 0 0 0 0 0
pid=6873 process(Including lightweight processes, i.e. threads)number comm=a.out The name of the application or command task_state=R Status of the task, R:runnign, S:sleeping (TASK_INTERRUPTIBLE), D:disk sleep (TASK_UNINTERRUPTIBLE), T: stopped, T:tracing stop,Z:zombie, X:dead ppid=6723 Parent process ID pgid=6873 Thread group number sid=6723 c The session group in which the task is located ID tty_nr=34819(pts/3) Of this task tty Device number of the terminal, INT(34817/256)=Main equipment number, (34817)-(main equipment No.)=Secondary equipment No tty_pgrp=6873 The process group number of the terminal, which is currently running in the foreground task of the terminal where the task is located(include shell application program)of PID. task->flags=8388608 Process flag bit to view the characteristics of the task min_flt=77 The number of page misses (secondary page misses) that occur when the task does not need to copy data from the hard disk cmin_flt=0 Accumulated all of this task waited-for The number of times the process has missed pages maj_flt=0 The number of missing pages (main missing pages) that occur when the task needs to copy data from the hard disk cmaj_flt=0 Accumulated all of this task waited-for The number of times the process has had a major page defect utime=1587 The running time of this task in user status, unit: jiffies stime=1 The time that the task runs in nuclear state, unit: jiffies cutime=0 The running time of all dead threads in user mode, unit: jiffies cstime=0 All the time that has died in the nuclear state of mind, unit: jiffies priority=25 Dynamic priority of tasks nice=0 Static priority of task num_threads=3 The number of threads in the thread group where the task is located it_real_value=0 Next due to timing interval SIGALRM Delay of sending process, in jiffy Unit. start_time=5882654 Start time of the task, unit: jiffies vsize=1409024(page) The virtual address space size of the task rss=56(page) The size of the physical address space in which the task currently resides rlim=4294967295(bytes) The maximum value of the physical address space that the task can reside in start_code=134512640 The starting address of the code segment of the task in the virtual address space end_code=134513720 The end address of the code segment of the task in the virtual address space start_stack=3215579040 The end address of the stack of the task in the virtual address space kstkesp=0 esp(32 Bit stack pointer) Current value of, Consistent with that obtained on the kernel stack page of the process. kstkeip=2097798 Pointer to the instruction to be executed, EIP(32 Bit instruction pointer)Current value of. pendingsig=0 Bitmap of the signal to be processed, recording the ordinary signal sent to the process block_sig=0 Bitmap of blocking signal sigign=0 Bitmap of ignored signals sigcatch=082985 Bitmap of captured signal wchan=0 If the process is in sleep state, this value gives the scheduled call point nswap cover swapped Number of pages, currently not used cnswap All child processes are swapped The sum of the number of pages is currently useless exit_signal=17 The signal sent to the parent process at the end of the process task_cpu(task)=0 Where does it run CPU upper task_rt_priority=0 Relative priority of real-time processes task_policy=0 Scheduling policy for process, 0=Non real time process, 1=FIFO Real time process; two=RR Real time process
The CPU time slice of the process is: processJiffies = utime + stime + cutime + cstime
The CPU time slice at T1 time is: processJiffiesT1=utimeT1 + stimeT1 + cutime T1+ cstimeT1
The CPU time slice at T2 time is: processJiffiesT2=utimeT2 + stimeT2 + cutime T2+ cstimeT2
3. How to obtain the global CPU time slice usage (totalCpuTime)?
To obtain the global CPU time slice, you need to read / proc/stat. this file contains the information of all CPU activities. All values in this file are accumulated from system startup to the current time. The contents of the file are as follows:
2|HWDUB-Q:/proc $ cat stat cpu 789254 111771 541221 17482394 21506 120516 63933 0 0 0 cpu0 81323 11220 23729 3067604 2160 2705 1246 0 0 0 cpu1 70901 12183 18777 2163569 929 1580 809 0 0 0 cpu2 65381 11090 16541 2170630 771 1391 705 0 0 0 cpu3 59074 8975 13668 2179826 730 1204 625 0 0 0 cpu4 133186 17323 117328 1970686 4237 29777 15955 0 0 0 cpu5 126784 17887 117564 1976221 4121 28215 15236 0 0 0 cpu6 128983 16623 116455 1975639 4345 27824 14840 0 0 0 cpu7 123619 16466 117155 1978216 4209 27814 14513 0 0 0 intr 33509315 0 448740 0 0 6310009 0 4765370 0 229 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 129 801458 701999 5 5 5 0 0 0 0 0 0 0 0 0 96875 0 1246058 4201 2 59600 0 476284 0 1067040 0 0 0 0 337817 1547 65 10 0 4 60 0 14 18 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 18388 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25 0 0 0 164045 895477 1 0 0 0 4 8 0 0 10 11 0 9 1190 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 18 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 10 1 0 0 0 0 0 0 0 6 10 10 0 10 0 14 144 17 0 0 0 0 0 0 0 0 38 0 124 0 0 0 0 0 0 0 0 0 1 1 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 1 1 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 1 1 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 175635 0 0 0 0 519 0 11 0 0 40611 ctxt 59790294 btime 1631759075 processes 43998 procs_running 1 procs_blocked 0 softirq 22139191 420 8182794 13548 2756773 288776 0 1015540 4601822 0 5279518 HWDUB-Q:/proc $
The value in the first line represents the total CPU usage, so we just need to calculate it with the number in the first line. Meaning of each field in the first line:
user (690147) The running time in user status accumulated from system startup to the current time, excluding nice The value is a negative process. nice (127021) Accumulated from system startup to the current time, nice Occupied by processes with negative values CPU time system (569289) The running time accumulated from the start of the system to the current time, which is in the nuclear state of mind idle (1089017) Accumulated from system startup to current time, except IO Waiting time other than waiting time iowait (13320) Accumulated from system startup to the current time, IO waiting time(since 2.5.41) irq (60126) The hard interrupt time is accumulated from the start of the system to the current time(since 2.6.0-test4) softirq (47538) The soft interrupt time is accumulated from the start of the system to the current time(since 2.6.0-test4)
The global CPU time slice is: totalJiffies = user + nice + system + idle + iowait + irq + softirq
The global CPU time slice at T1 time is: totalJiffiesT1 = userT1 + niceT1 + systemT1 + idleT1 + iowaitT1+ irqT1 + softirqT1
The global CPU time slice at T2 time is: totalJiffiesT2 = userT2 + niceT2 + systemT2 + idleT2 + iowaitT2 + irqT2 + softirqT2
The global CPU time slice running from T1 to T2 is: gapTotalJiffies = totalJiffiesT2 - totalJiffiesT1
The idle cpu time slice from T1 to T2 is: gapIdle = idleT2 - idleT1
The global CPU utilization from T1 to T2 is 100 * (gapTotalJiffies - gapIdle) / (float) gapTotalJiffies
The CPU utilization of the process from T1 to T2 is 100 * (processjiffiest2 - processjiffiest1) / (float) (totaljiffiest2 - totaljiffiest1)