Average unix load averageload Calculation Method
Average load? Indicates the average number of processes in a period of time, that is, the system is busy. The average load and CPU usage are different, which can indicate the system's busy schedule. The calculation and related aspects of the system's average load will be briefly introduced below.
View Method
Use the uptime command in linux, or view/proc/loadavg to view the system load average load. The uptime command displays the average load of the system in the past 1 minute, 5 minutes, and 10 minutes.
[Root @ localhost ~] # Uptime
19:32:09 up 5 days, 5 users, load average: 0.05, 0.04, 0.05
[Root @ localhost ~] # Cat/proc/loadavg
0.04 0.04 0.05 1/394 23203
So what is the working principle of the uptime command to calculate the load average?
Calculation Method
For single cpu and multi-cpu scenarios, the average load of the system is slightly different. A single cpu is the simplest case. For example, in the past average minute, the number of processes that determine the system is running or waiting indicates the average load of the system, but it is slightly different in linux, those processes in the io wait state will also be included for computing. In this way, the CPU utilization may be very different from the average load. When most processes perform IO, even if the average load is large, there will be no high CPU utilization. In addition, some systems process processes and threads differently, some compute for each thread, and some focus only on processes. For threads with hyper-Threading Technology, it may be another way of processing. The average load of multiple CPUs is calculated by dividing the number of CPUs by one CPU.
File: kernel/timer. c:
unsigned long avenrun[3];static inline void calc_load(unsigned long ticks){unsigned long active_tasks; /* fixed-point */static int count = LOAD_FREQ;count -= ticks;if (count < 0) {count += LOAD_FREQ;active_tasks = count_active_tasks();CALC_LOAD(avenrun[0], EXP_1, active_tasks);CALC_LOAD(avenrun[1], EXP_5, active_tasks);CALC_LOAD(avenrun[2], EXP_15, active_tasks);}}
Sched. h In the kernel
/* * These are the constant used to fake the fixed-point load-average * counting. Some notes: * - 11 bit fractions expand to 22 bits by the multiplies: this gives * a load-average precision of 10 bits integer + 11 bits fractional * - if you want to count load-averages more often, you need more * precision, or rounding will get you. With 2-second counting freq, * the EXP_n values would be 1981, 2034 and 2043 if still using only * 11 bit fractions. */extern unsigned long avenrun[]; /* Load averages */extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);#define FSHIFT 11 /* nr of bits of precision */#define FIXED_1 (1< >= FSHIFT;extern unsigned long total_forks;extern int nr_threads;DECLARE_PER_CPU(unsigned long, process_counts);extern int nr_processes(void);extern unsigned long nr_running(void);extern unsigned long nr_uninterruptible(void);extern unsigned long nr_iowait(void);extern unsigned long nr_iowait_cpu(int cpu);extern unsigned long this_cpu_load(void);
Ewma Algorithm
Linux uses this algorithm in many aspects, such as the CBQ algorithm of the TC system, which is a statistical algorithm.