Linux Kernel idle Process Analysis

Source: Internet
Author: User

1. What is idle?
To put it simply, idle is a process whose PID is 0. Its predecessor was the first process created by the system and the only process that was not produced by fork. In the SMP system, each processor unit has an independent running queue, and each running queue has an idle process, that is, the number of processor units and the number of idle processes. The idle time of the system is actually the "running time" of the idle process ". Since idle is a process, let's take a look at how idle is created and what specific things have been done?

2. Create an idle
We know that the system is from BIOS power-on self-check, loading the boot program (LILO/GRUB) in MBR, and then loading the Linux kernel to start running until the specified shell starts running, then the user starts to operate Linux. Generally, in the vmlinux entry startup_32 (head. s), the execution environment is set for the original process with PID 0, and then the process starts to execute start_kernel () to initialize the Linux kernel. Including page table initialization, interrupt vector table initialization, and system time initialization. Then call fork () to create the first user process:
Kernel_thread (kernel_init, null, clone_fs | clone_sighand );

This process is the famous INIT process with PID 1. It will continue to complete the remaining initialization work, and execve (/sbin/init) will become the ancestor of all other processes in the system. We will not study init this time. Let's look back at the process with PID = 0. After the INIT process is created, the process with PID = 0 calls cpu_idle () and changes to idle process.

Current_thread_info ()-> Status | = ts_polling;

In the SMP system, apart from the creation of the idle process on the master processor (processor for initialization) we mentioned above, as well as the slave processor (processor activate by the master processor) on the idle process, how did they create it? Next, let's look at the INIT process. Before init evolves into/sbin/init, it will execute some initialization work. One of them is smp_prepare_cpus (), which initializes the SMP processor, in this process, it is called when processing each slave processor.
Task = copy_process (clone_vm, 0, idle_regs (& regs), 0, null, null, 0 );
Init_idle (task, CPU );
That is, a process is re-created from init and initialized as an idle process (PID is still 0 ). The idle process on the processor performs some activate operations and then runs cpu_idle ().

Simply put, the original process (pid = 0) creates the INIT process (pid = 1) and then evolves into an idle process (pid = 0 ). The INIT process creates an idle process (pid = 0) for each slave processor (running Queue) and then evolves to/sbin/init.

3. Run Time of idle
The idle process priority is max_prio, that is, the lowest priority. In earlier versions, idle participates in scheduling, so it has the lowest priority. Idle is scheduled to run only when no other process can run. In the current version, idle does not participate in scheduling in the running queue. Instead, it includes an idle pointer in the running queue structure and points to the idle process. It runs when the scheduler finds that the running queue is empty, transfer to run.

4. Workload of idle
From the above analysis, we can see that idle will be scheduled only when there are no other ready processes in the system that can be executed. Whether it is the master processor or slave processor, the cpu_idle () function is executed at the end. So let's take a look at what cpu_idle has done.
Because the idle process does not execute any meaningful tasks, we usually consider two points: 1. Energy Saving, 2. Low exit latency.
The core code is as follows:
Void cpu_idle (void)
{
Int CPU = smp_processor_id ();

Current_thread_info ()-> Status | = ts_polling;

/* Endless idle loop with no priority at all */
While (1 ){
Tick_nohz_stop_sched_tick (1 );
While (! Need_resched ()){

Check_pgt_cache ();
RMB ();

If (rcu_pending (CPU ))
Rcu_check_callbacks (CPU, 0 );

If (cpu_is_offline (CPU ))
Play_dead ();

Local_irq_disable ();
_ Get_cpu_var (irq_stat). idle_timestamp = jiffies;
/* Don't trace irqs off for idle */
Stop_critical_timings ();
Pm_idle ();
Start_critical_timings ();
}
Tick_nohz_restart_sched_tick ();
Preempt_enable_no_resched ();
Schedule ();
Preempt_disable ();
}
}

Cyclically judge need_resched to reduce the exit delay and use idle () to save energy.
The default idle implementation is the HLT command. the HLT command suspends the CPU and waits for recovery when the hardware interruption occurs to save energy. That is, from the processor C0 state to the C1 State (see ACPI standard ). This is also the main means of various "processor cooling" tools on the Windows platform in earlier years. Of course, idle can also be defined in other ACPI or APM modules, or even a custom idle (such as NOP ).

Summary:
1. Idle is a process whose PID is 0.
2. the idle on the master processor evolved from the original process (pid = 0. The idle on the processor is obtained by the INIT process fork, but their PID is 0.
3. the idle process has the lowest priority and is not scheduled. It is only scheduled when the running queue is empty.
4. the idle loop waits for need_resched to be set. Hlt is used by default for energy saving.

Related Article

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.