Comparison between sleep wake-up for Linux and Android (1)

Source: Internet
Author: User
Tags linux sleep
Comparison between Linux and Android sleep (1)

Introduction to Linux sleep (suspend)

In Linux (PC and embedded systems), sleep mainly consists of three main steps:

1. Freeze user-mode access and kernel-mode tasks;

2. Call the registered device and suspend callback functions in the registration order;

3. Sleep the core device and sleep the CPU into a sleep frozen process. The kernel sets the state of all processes in the process list to stop and saves the context of all processes. When these processes are restored, they do not know that they have been frozen, but simply continue to execute. How can we break Linux into sleep? You can read and write the Sys File/sys/power/State to sleep the system. For example:

Echo standby>/sys/power/State

The command is used to sleep the system. You can also use the following command:

CAT/sys/power/State

To check which sleep modes are supported by the kernel.

 

Linux sleep and Wakeup

Related File Path:

linux/kernel/power/main.c
linux/kernel/arch/xxx/mach-xxx/pm.clinux/driver/base/power/main.c

Next, let's take a detailed look at how Linux sleeps/wakes up. The kernel/power/Main. c file is the entrance to the entire framework. You can read and write the Sys File/sys/power/State to change the control system to a low-power state. the user's read/write operations on/sys/power/State call main. state_store () in C. You can write strings defined in constchar * const pm_states [], such as "on", "mem", "standby", and "disk ".

Then state_store () will call enter_state (). It will first check some status parameters and then synchronize the file system. The following is the code:

/** 261 *      enter_state - Do common work of entering low-power state. 262 *      @state:         pm_state structure for state we're entering. 263 * 264 *      Make sure we're the only ones trying to enter a sleep state. Fail 265 *      if someone has beat us to it, since we don't want anything weird to 266 *      happen when we wake up. 267 *      Then, do the setup for suspend, enter the state, and cleaup (after 268 *      we've woken up). 269 */ 270int enter_state(suspend_state_t state) 271{ 272        int error; 273 274        if (!valid_state(state)) 275                return -ENODEV; 276 277        if (!mutex_trylock(&pm_mutex)) 278                return -EBUSY; 279 280        printk(KERN_INFO "PM: Syncing filesystems ... "); 281        sys_sync(); 282        printk("done.\n"); 283 284        pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); 285        error = suspend_prepare(); 286        if (error) 287                goto Unlock; 288 289        if (suspend_test(TEST_FREEZER)) 290                goto Finish; 291 292        pr_debug("PM: Entering %s sleep\n", pm_states[state]); 293        pm_restrict_gfp_mask(); 294        error = suspend_devices_and_enter(state); 295        pm_restore_gfp_mask(); 296 297 Finish: 298        pr_debug("PM: Finishing wakeup.\n"); 299        suspend_finish(); 300 Unlock: 301        mutex_unlock(&pm_mutex); 302        return error; 303}

Prepare and freeze Processes

After entering suspend_prepare (), it will assign a virtual terminal to suspend to output the information pm_prepare_console (), and then broadcast the policy -- pm_notifier_call_chain () that the system wants to enter suspend (), shut down the user-state helper process usermodehelper_disable (), and call suspend_freeze_processes () once to freeze all processes. The current state of all processes will be saved here, some processes may refuse to enter the frozen state. If such a process exists, it will lead to freezing failure. This function will discard the frozen process and restore all the frozen processes. suspend_thaw_processes () and usermodehelper_enable ().

  84/**  85 *      suspend_prepare - Do prep work before entering low-power state.  86 *  87 *      This is common code that is called for each state that we're entering.  88 *      Run suspend notifiers, allocate a console and stop all processes.  89 */  90static int suspend_prepare(void)  91{  92        int error;  93  94        if (!suspend_ops || !suspend_ops->enter)  95                return -EPERM;  96  97        pm_prepare_console();  98  99        error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); 100        if (error) 101                goto Finish; 102 103        error = suspend_freeze_processes(); 104        if (!error) 105                return 0; 106 107        suspend_stats.failed_freeze++; 108        dpm_save_failed_step(SUSPEND_FREEZE); 109 Finish: 110        pm_notifier_call_chain(PM_POST_SUSPEND); 111        pm_restore_console(); 112        return error; 113}

Sleep peripherals

Now, all processes (including workqueue [queue_work ()]/kthread) have stopped, and kernel-state tasks may have semaphores when they are stopped, so if you unlock the semaphores In the peripherals at this time, there may be deadlocks, so you should be very careful when making the lock/unlock lock in the suspend () function of the peripherals, we recommend that you do not wait for the lock in suspend () during design. in addition, some logs cannot be output during suspend, so once a problem occurs, debugging is very difficult.

Then, the kernel will try to release some memory and call suspend_devices_and_enter () to sleep all peripherals. In this function, if the platform registers suspend_pos (usually defined and registered in the board-level definition), suspend_ops-> begin () will be called here, and then driver/base/power/main. in C, device_suspend ()-> dpm_suspend () will be called, and they will call the suspend () callback of the driver in sequence to sleep all devices.

When all devices sleep, suspend_ops-> prepare () will be called. This function usually makes some preparations to let the machine sleep. in Linux, non-startup CPUs in multi-core CPUs will be turned off. You can see through comments that the other CPUs will not cause race condion, and then only one CPU will be running.

Suspend_ops is an on-board power management operation, which is usually registered in the file ARCH/XXX/Mach-xxx/PM. C.

Next, suspend_enter () will be called. This function will disable arch IRQ and call device_power_down (). It will call the suspend_late () function, this function is the final function called by the system to sleep. It usually performs the final check in this function. if the check is correct, sleep all the system devices and buses and call suspend_pos-> enter () to enable CPU power-saving. at this time, it is sleep. code execution stops here.

 195/** 196 *      suspend_devices_and_enter - suspend devices and enter the desired system 197 *                                  sleep state. 198 *      @state:           state to enter 199 */ 200int suspend_devices_and_enter(suspend_state_t state) 201{ 202        int error; 203        bool wakeup = false; 204 205        if (!suspend_ops) 206                return -ENOSYS; 207 208        trace_machine_suspend(state); 209        if (suspend_ops->begin) { 210                error = suspend_ops->begin(state); 211                if (error) 212                        goto Close; 213        } 214        suspend_console(); 215        suspend_test_start(); 216        error = dpm_suspend_start(PMSG_SUSPEND); 217        if (error) { 218                printk(KERN_ERR "PM: Some devices failed to suspend\n"); 219                goto Recover_platform; 220        } 221        suspend_test_finish("suspend devices"); 222        if (suspend_test(TEST_DEVICES)) 223                goto Recover_platform; 224 225        do { 226                error = suspend_enter(state, &wakeup); 227        } while (!error && !wakeup 228                && suspend_ops->suspend_again && suspend_ops->suspend_again()); 229 230 Resume_devices: 231        suspend_test_start(); 232        dpm_resume_end(PMSG_RESUME); 233        suspend_test_finish("resume devices"); 234        resume_console(); 235 Close: 236        if (suspend_ops->end) 237                suspend_ops->end(); 238        trace_machine_suspend(PWR_EVENT_EXIT); 239        return error; 240 241 Recover_platform: 242        if (suspend_ops->recover) 243                suspend_ops->recover(); 244        goto Resume_devices; 245}

Resume wake-up

If the system is interrupted or other events are awakened during sleep, the following code starts to run. The wake-up sequence is the opposite to that of sleep, therefore, the system device and bus will first wake up, enable system interruption, enable non-startup CPU to stop during sleep, and call suspend_ops-> finish (), and in suspend_devices_and_enter () the function will also wake up each device, enable the virtual terminal, and finally call suspend_ops-> end ().

In the return to the enter_state () function, when suspend_devices_and_enter () returns, the peripherals are awakened, but the processes and tasks are still frozen. suspend_finish () is called here () to unfreeze these processes and tasks, and send a running y to indicate that the system has exited from the suspend status and wakened the terminal.

By now, all sleep and wakeup have been completed, and the system continues to run.

Source Address: http://hi.baidu.com/ch_ff/blog/item/005b61fb603c278359ee90aa.html

 

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.