Linux Compute CPU Usage __linux

Source: Internet
Author: User
Tags cpu usage
Calculates the total CPU utilization Totalcpuuse 1) Samples Two short enough time interval CPU snapshots, that is, read/proc/stat files, get the following data for two points: CPUT1 (user1, Nice1, System1, Idle1, IOW
Ait1, Irq1, softirq1, Stealstolen1, guest1);
CPUT2 (User2, Nice2, system2, Idle2, Iowait2, IRQ2, SOFTIRQ2, Stealstolen2, Guest2); 2 Calculate total CPU Time totalcputime:cputime1 = user1 + Nice1 + system1 + idle1 + iowait1 + irq1 + softirq1 + stealstolen1 + Guest
1;
CPUTime2 = user2 + nice2 + system2 + idle2 + iowait2 + irq2 + softirq2 + stealstolen2 + guest2;

Totalcputime = cputime2–cputime1;

3 COMPUTE CPU idle time idlecputime:idlecputime = Idle2–idle1;

4 Calculate the total CPU utilization rate Totalcpuuse:totalcpuuse = (totalcputime–idlecputime)/totalcputime; Calculates the CPU utilization of a process Processcpuuse 1) Samples Two short enough time interval CPU snapshots and corresponding process snapshots, that is, read the/proc/stat file to get the following data for two points: CPUT1 (user1, Nice1, System
1, Idle1, Iowait1, irq1, softirq1, Stealstolen1, guest1);
CPUT2 (User2, Nice2, system2, Idle2, Iowait2, IRQ2, SOFTIRQ2, Stealstolen2, Guest2); Read the/proc/[pid]/stat file to obtain the following data for two points: ProcessT1 (utime1, stime1, cutime1, CstiME1);
ProcessT2 (Utime2, Stime2, cutime2, cstime2); 2 Calculate total CPU time totalcputime and process time processtime:cputime1 = user1 + Nice1 + system1 + idle1 + iowait1 + irq1 + softirq1 + St
Ealstolen1 + guest1;
CPUTime2 = user2 + nice2 + system2 + idle2 + iowait2 + irq2 + softirq2 + stealstolen2 + guest2;

Totalcputime = cputime2–cputime1;
processTime1 = utime1 + stime1 + cutime1 + cstime1;
ProcessTime2 = utime2 + stime2 + cutime1 + cstime2;

Processtime = processtime2–processtime1;

3 Compute the CPU utilization rate of the process processcpuuse:processcpuuse = Processtime/totalcputime; Calculates the CPU utilization of a thread Threadcpuuse 1) Samples Two short enough time interval CPU snapshots and corresponding thread snapshots, that is, read the/proc/stat file to obtain the following data for two points: CPUT1 (user1, Nice1, system1
, Idle1, Iowait1, irq1, softirq1, Stealstolen1, guest1);
CPUT2 (User2, Nice2, system2, Idle2, Iowait2, IRQ2, SOFTIRQ2, Stealstolen2, Guest2);
Read the/proc/[pid]/task/[tid]/stat file to obtain the following data for two points: threadT1 (utime1, stime1);
ThreadT2 (utime2, stime2); 2 Calculate total CPU time Totalcputime and thread time threadtime:cputime1 = user1 + Nice1 + system1 + idle1 + iowait1 + irq1 + softirq1 + stealstolen1 + guest1;
CPUTime2 = user2 + nice2 + system2 + idle2 + iowait2 + irq2 + softirq2 + stealstolen2 + guest2;

Totalcputime = cputime2–cputime1;
threadTime1 = utime1 + stime1;
ThreadTime2 = utime2 + stime2;

ThreadTime = threadtime2–threadtime1;



3 Compute the CPU utilization rate of the thread threadcpuuse:threadcpuuse = threadtime/totalcputime; 2.5. Calculation 2.5.1 and basic idea of CPU utilization in multi-core cases first, get the total CPU time by reading the/proc/stat file, read the/proc/[pid]/stat get the process CPU time, read the/proc/[pid]/task/[ti

D]/stat gets the thread CPU time and reads the/proc/cpuinfo to get the number of CPUs. The CPU usage of a process or thread is calculated in multi-core cases, and the above method is usually used in relation to the total elapsed time of all cores in the CPU, and we usually get used to the process or thread occupancy rate for a single core.

So we can calculate the CPU occupancy rate according to the above method, then multiply the result by the CPU kernel number, can get process or line threads relative to a single core occupancy rate.

2.5.2, calculates the total CPU utilization rate with 2.4.2. 2.5.3, calculate CPU utilization of a process Mprocesscpuuse 1) with 2.4.3 compute the CPU utilization of a process Processcpuuse 2) Read/proc/cpuinfo file to obtain logical CPU (processor)

Number (see 1.1): Processornum 3) The CPU utilization rate of the process is Mprocesscpuuse:mprocesscpuuse = Processcpuuse * Processornum in multi-core cases; 2.5.4, calculate the CPU utilization rate of a thread Mthreadcpuuse 1) with 2.4.4 Compute the CPU utilization of a thread Threadcpuuse 2 the number of/proc/cpuinfo files to obtain a logical CPU (see 1.1): Processornum 3) The CPU utilization of the threads in multi-core cases m Threadcpuuse:mthreadcpuuse = Threadcpuuse * PROCESSORNUM;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.