Idle process analysis for Linux kernel

Source: Internet
Author: User

1. What is idle?

Simply put, idle is a process with a PID number of 0. Its predecessor was the first process created by the system. is also the only process that has not been produced by fork ().

In an SMP system, each processor unit has a separate execution queue and an idle process on each execution queue, that is, how many processor units there are. There are so many idle processes.

The spare time of the system, in fact, refers to the "execution time" of the idle process. Since it is idle is the process. So let's take a look at how idle was created and what it did in detail.

2. Creation of idle
We know that the system is from the BIOS power-on self-test, loaded into the MBR of the Boot program (LILO/GRUB), and then loaded into the Linux kernel began to execute, until the specified shell started execution, the user began to operate Linux.

And it's roughly the entrance to the Vmlinux.
Startup_32 (head. S) Set the operating environment for the original process with PID number 0, and then the initialization of the Linux kernel was completed when the process started running Start_kernel ().

Contains the initialization page table, initializes the interrupt vector table, initializes the system time, and so on. Then call Fork () to create the first user process:
Kernel_thread (Kernel_init, NULL, CLONE_FS | Clone_sighand); This process is known as the PID 1 init process, it will continue to complete the rest of the initialization work, and then Execve (/sbin/init), the system as the ancestors of all other processes. About INIT We do not study this time, looking back at the process of pid=0, after the creation of the INIT process, pid=0 process calls Cpu_idle () evolved into the idle process.

Current_thread_info ()->status |= ts_polling;
In the SMP system. In addition to the creation of the idle process on the main processor (the processor running the initialization work) that we talked about earlier, and the idle process from the processor (the processor activate by the main processor), how did they create it? Following the init process, Init will run part of the initialization before it evolves into/sbin/init, one of which is Smp_prepare_cpus (), which initializes the SMP processor, which is called when each slave processor is processed.
Task = copy_process (CLONE_VM, 0, Idle_regs (&regs), 0, NULL, NULL, 0);
Init_idle (task, CPU);
That is to copy a process from Init and initialize it to the idle process (the PID is still 0). The idle process from the processor will perform some activate work and then run Cpu_idle ().
The whole process is simply that the original process (pid=0) creates the init process (pid=1) and then evolves into the idle process (pid=0). The init process creates an idle process (pid=0) for each slave processor (execution queue). And then evolve into/sbin/init.



3. Idle time of execution
The idle process priority is Max_prio, which is the lowest priority.

The previous version number, Idle is the

With the scheduled. So the priority is set to the lowest level. Execution idle is dispatched when no other process is able to execute. While idle in the current version number does not participate in scheduling in the execution queue, it contains the idle pointer in the execution queue structure, pointing to the idle process, which executes when the scheduler discovers that the execution queue is empty, and is transferred into execution.

4. Idle workload from the above analysis, we can see that idle is dispatched when the system does not have other ready processes to execute. Either the main processor. or the Cpu_idle () function that is executed from the processor and finally.  So let's see what Cpu_idle has done. Since the idle process does not perform any meaningful tasks, it is generally considered two points: 1. Energy saving, 2. Low exit delay.

Its core code such as the following: 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 (); } }

The loop infers the need_resched to reduce the exit delay and uses idle () to save energy. The default idle implementation is the HLT directive. The HLT instruction keeps the CPU in a paused state while waiting for the hardware interrupt to resume, thus achieving energy saving.

That is, from the processor C0 state to the C1 state (see ACPI Standard). This is also the primary means of the various "processor cooling" tools on the Windows platform in the early years. Of course, the idle can also be defined in other ACPI or APM modules, or even a self-defined idle (for example, NOP).

Summary:
1.idle is a process with a PID of 0.
2. Idle on the main processor evolved from the original process (pid=0). Idle from the processor is obtained by the Init process fork, but their PID is 0.
The 3.Idle process is the lowest priority. And does not participate in scheduling. It is only dispatched when the execution queue is empty.
The 4.Idle loop waits for the need_resched to be placed. Use HLT to save energy by default.


Idle process analysis for Linux kernel

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.