Original:
https://mp.weixin.qq.com/s/HJihdfu9zVYEuvHVV0WXwQ
1. Introduction
Conventional power management methods in Linux systems include shutdown (Power off), standby (Standby or Hibernate), and reboot (Reboot). These methods were used during the era of PCs or servers before the widespread adoption of embedded Linux. In that primitive era of computer science, driven by Moore’s Law, humanity was focused on improving computing power and processing performance, and did not pay much attention to power consumption.
In this context, the Linux power management mechanisms developed are coarse, static, and passive. Please refer to the introduction below for more details.
2. Manifestations of Conventional Power Management in Linux Operating Systems
In the Linux operating system, the operations related to conventional power management are shown in the images below:
The first part is the interface for system shutdown, reboot, etc., which includes three operation options: Hibernate, Restart, and Shutdown;
The second and third parts are the settings for “power management properties”. The so-called power management properties can configure how long the system remains in an Inactive state under different power supply modes (such as AC Power, Battery, etc. Since the machine is a PC, there is no battery power option), after which the system will turn off the display or enter Sleep mode.
This article will discuss the meanings of the terms mentioned above and their implementation in the kernel. Before we begin, let’s explain the meanings of these terms.
Shutdown is easy to understand; it means turning off the computer. It also implies no longer using the computer.
Restart is also easy to understand; it means rebooting the system. During the reboot process, no longer using the computer.
Hibernate can be translated as winter sleep.
2.1 Hibernate
When you hear the word “winter sleep”, does it catch your attention? Animal hibernation is a natural power management method designed by nature. In the animal kingdom, hibernation refers to a physiological state similar to deep sleep that animals (usually warm-blooded) enter by lowering their body temperature, during which they consume less energy, thus achieving energy savings.
In the computer world, designers have also borrowed the concept of “hibernation”. When the computer is not in use, it saves all current states (executing programs, displayed images, sounds being played, etc.) to a non-volatile memory (such as a hard drive) and then turns off the computer. When restarted, the system reads the previous state from the memory and restores it, making it seem to the user as if the computer had never been turned off. If we were to translate the computer’s “hibernation” into the animal world, it would look like this: a dinosaur, waddling along, eating bananas, strolling through the woods. Then, a bored monkey comes along and shouts “Freeze!”, and the dinosaur freezes in place. After ten thousand years, the bored monkey returns and shouts “Move!”, and the dinosaur continues waddling and eating bananas, as if nothing had happened.
2.2 Sleep
Sleep, as in sleep. This term is also borrowed from the biological world. Can you imagine the difference between “sleep” and “hibernation”? “Sleep” is light; one can wake up at any time. In computers, Hibernate requires saving the state to non-volatile memory, and waking up may take longer (because the access speed of non-volatile memory is relatively slow). If you want to wake up faster, you can save the state in RAM, which is Sleep. However, this comes at a cost; RAM needs to be powered, which consumes energy. You cannot have both!
2.3 Auto Sleep
Auto Sleep allows you to set how long the system remains in an Inactive state before automatically entering Sleep mode. For example, I am writing this article and then my wife calls me to kneel on a washboard for two hours. During those two hours, the computer is not being used, and if it does not enter Sleep mode, it will consume a lot of energy. To avoid this unnecessary consumption, the system can be set to automatically sleep after a certain condition is met (such as 20 minutes of inactivity).
Auto put display to Sleep works on a similar principle, but the target is the Display (monitor, etc.).
3. Software Architecture of Conventional Power Management
Based on the above description, conventional power management mainly handles shutdown, reboot, hibernation (Hibernate), and sleep (Sleep, also known as Suspend in the Kernel). In the kernel, it can be roughly divided into three software layers:
API Layer, which provides interfaces to user space, where the interfaces for shutdown and reboot are system calls (in newer kernels, there is a new way for the shutdown interface, which will be discussed later), and the interfaces for Hibernate and Suspend are sysfs.
PM Core, located in the kernel/power/ directory, mainly handles the core logic related to hardware.
PM Driver, which is divided into two parts: one is architecture-related Drivers that provide a Driver framework. The other part is specific architecture-related Drivers, which are also the content that power management driver development needs to involve (the modules outlined in red in the image).
Additionally, power management is a system-level module, thus it involves various aspects such as device models, process management, etc. We can explore these in detail in subsequent analyses.