Method for preloading memory page, electronic equipment and chip system
By generating and storing PTE snapshots of page objects accessed during application hot start, and preloading file page data and anonymous page data, the problem of page missing exceptions in electronic devices during application hot start is solved, and the application startup speed and user experience are improved. .
Patent Information
- Application Number
- CN202310842576.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-28
- Publication Date
- 2024-08-30
AI Technical Summary
When an electronic device is hot-starting an application, page faults occur due to the memory recycling mechanism, resulting in long application startup times and poor user experience.
By generating and storing PTE snapshots of page objects accessed during application hot startup, preloading file page data and anonymous page data into physical memory reduces page missing anomalies and improves application hot startup speed.
It effectively reduces page missing anomalies during application hot startup, speeds up application startup, and improves user experience.
Smart Images

Figure CN118567736A
Abstract
Description
[0001] This application is a divisional application of the invention patent application with application number 202310174653.0 submitted to the China Patent Office, application date February 28, 2023, and invention name “A method for preloading memory pages, electronic device and chip system”. Technical Field
[0002] The embodiments of the present application relate to the field of terminal application technology, and in particular to a method for preloading memory pages, an electronic device, and a chip system. Background Art
[0003] As the functions of electronic devices become more and more powerful, more and more application programs are installed in the electronic devices. Users can use the functions provided by these application programs based on the application programs installed in the electronic devices.
[0004] When an application is hot-started, due to the memory recycling mechanism, a page fault exception usually occurs when accessing a swapped out or released memory page again. After a page fault exception occurs, the system will put the application process in the startup process into a blocking queue until the page fault exception is resolved. For example, after a page fault exception occurs, physical memory is allocated to the application, the relevant file page data and anonymous page data are loaded in the allocated physical memory, and the mapping relationship between the application's virtual memory and the physical address of the allocated physical memory is filled in the page table entry (PTE) used to store the mapping relationship between virtual memory and physical memory. This will result in a longer application hot-start time and a poor user experience. Summary of the invention
[0005] The embodiments of the present application provide a method for preloading memory pages, an electronic device, and a chip system, which can speed up the hot start of applications and improve user experience.
[0006] In order to achieve the above objectives, this application adopts the following technical solutions:
[0007] In a first aspect, an embodiment of the present application provides a method for preloading a memory page, comprising:
[0008] The kernel receives the pre-read information, the pre-read information including the identifier of the first application and the hot start identifier;
[0009] In response to the pre-read information, the kernel reads a PTE snapshot corresponding to the identifier of the first application from the first storage space, the PTE snapshot being generated for the PTEs of the file page and the anonymous page based on the page objects accessed during the historical hot start of the first application, the first storage space being located in the physical memory or the disk;
[0010] The kernel loads the file page data corresponding to the PTE snapshot on disk and the anonymous page data corresponding to the swap partition into physical memory;
[0011] The kernel writes a mapping relationship between the virtual memory of the file page data and the physical memory of the file page data, and a mapping relationship between the virtual memory of the anonymous page data and the physical memory of the anonymous page data in the PTE of the first application.
[0012] In the present application, a PTE snapshot is generated for the PTE of the file page and the anonymous page, which are page objects accessed during the hot start of the application, and the PTE snapshot is stored in the first storage space; when the application is subsequently hot started, the PTE snapshot corresponding to the identifier of the same application is read from the first storage space; then the file page data corresponding to the PTE snapshot in the disk and the anonymous page data in the swap partition are loaded into the physical memory, and the mapping relationship between the virtual memory and the physical memory of the file page data, and the mapping relationship between the virtual memory and the physical memory of the anonymous page data are written in the PTE; because the file page data and the anonymous page data have been preloaded in advance, and the mapping relationship between the virtual memory and the physical memory of the file page data, and the mapping relationship between the virtual memory and the physical memory of the anonymous page data are written in the PTE, the page missing abnormality during the hot start of the application is reduced, and the speed of the hot start of the application is accelerated.
[0013] As another implementation of the first aspect of the present application, before the kernel receives the pre-read information, the method further includes:
[0014] The kernel receives the capture information, the capture information including the identifier of the first application and the hot start identifier;
[0015] In response to the captured information, the kernel generates a PTE snapshot corresponding to the identifier of the first application according to the PTEs of the file pages and anonymous pages in the page table corresponding to the identifier of the first application;
[0016] The kernel stores the PTE snapshot corresponding to the identifier of the first application in the first storage space.
[0017] In the present application, a PTE snapshot of the application can be generated by using the PTEs of file pages and anonymous pages in the page table corresponding to the application during the historical hot start process, so as to preload the file page data and anonymous page data according to the PTE snapshot during the subsequent hot start (both file pages and anonymous pages are memory pages).
[0018] As another implementation of the first aspect of the present application, in response to the capture information, the kernel generates a PTE snapshot corresponding to the identifier of the first application according to the page objects in the page table corresponding to the identifier of the first application being the PTE of the file page and the anonymous page, including:
[0019] In response to the capture information, the kernel determines that a PTE snapshot corresponding to the identifier of the first application does not exist in the first storage space;
[0020] The kernel generates a PTE snapshot corresponding to the identifier of the first application according to the PTEs of the file pages and anonymous pages in the page table corresponding to the identifier of the first application.
[0021] In this application, it is not necessary to generate a PTE snapshot according to the PTE written in the hot start process after each hot start of the application. In the case where the PTE snapshot corresponding to the application does not exist in the first storage space, it is necessary to generate the PTE snapshot corresponding to the application according to the PTE whose page objects in the page table corresponding to the application are file pages and anonymous pages; in the case where the PTE snapshot corresponding to the application already exists in the first storage space, it is not necessary to generate the PTE snapshot corresponding to the application according to the PTE whose page objects in the page table corresponding to the application are file pages and anonymous pages.
[0022] As another implementation of the first aspect of the present application, the kernel generates a PTE snapshot corresponding to the identifier of the first application according to the page objects in the page table corresponding to the identifier of the first application, which are PTEs of file pages and anonymous pages, including:
[0023] The kernel sets the status bit in the page table corresponding to the identifier of the first application to a preset character, and the page object to a PTE of a preset type, and generates a PTE snapshot corresponding to the identifier of the first application. The preset character is used to indicate that the virtual memory corresponding to the PTE has been mapped to the physical memory (for example, it can be determined by pte_present) and has been accessed (for example, it can be determined by checking pte_young), and the preset types include active, referenced, and anon.
[0024] In this application, the PTE for generating a PTE snapshot can be determined based on the status bit in the PTE data structure and the page object (i.e., page flag). The type of anonymous page is anon, and file pages of type active and referenced can be selected to generate PTE snapshots. By selecting some types of file pages to generate PTE snapshots, the time consumption of the pre-reading process caused by loading a large number of file pages and the application startup speed can be balanced.
[0025] As another implementation of the first aspect of the present application, before the kernel receives the pre-read information, the method further includes:
[0026] The monitoring module detects that the first application is started to the pre-reading node, and the pre-reading node is located before the ActivityStart stage in the application startup process;
[0027] The monitoring module sends a pre-reading instruction to the pre-reading module;
[0028] In response to the pre-reading instruction, the pre-reading module transmits the pre-reading information to the kernel through a system call.
[0029] In the present application, the process of pre-reading file pages and anonymous pages is implemented through the kernel of the kernel space; the time node of pre-reading is detected by the monitoring module of the user space; the information interaction between the user space and the kernel space is realized by means of system calls; in order to increase the speed of application startup, the time node of pre-reading can be determined before the ActivityStart stage where a large number of page fault exceptions occur.
[0030] As another implementation of the first aspect of the present application, before the kernel receives the capture information, the method further includes:
[0031] The monitoring module detects that the first application is started to the capture node, and the capture node is located within the first time after or at the end of the DoFrame phase in the application startup process;
[0032] The monitoring module sends a capture instruction to the pre-reading module;
[0033] In response to the capture instruction, the pre-reading module transmits the capture information to the kernel through a system call.
[0034] In the present application, the process of generating a PTE snapshot is implemented by the kernel in the kernel space; the time node of generating the PTE snapshot is detected by the monitoring module in the user space; the information interaction between the user space and the kernel space is realized by means of system calls; in order to be able to generate some types of file pages and anonymous pages filled with page fault exceptions during the hot start of the application, the time node for capturing the PTE information can be determined at the end of the DoFrame phase in the application startup process or within the first time thereafter.
[0035] As another implementation of the first aspect of the present application, before the kernel receives the capture information, the method further includes:
[0036] The monitoring module detects that the first application is started to a capture node, and the capture node is located within the first time at the end of or after the ActivityStart phase in the application startup process;
[0037] The monitoring module sends a capture instruction to the pre-reading module;
[0038] In response to the capture instruction, the pre-reading module transmits the capture information to the kernel through a system call.
[0039] In the present application, according to actual conditions, PTE information can also be captured at the end of the ActivityStart phase when a large number of page fault exceptions occur during the application startup process or within the first time thereafter. At this time, the captured PTE information already contains most of the file page data and anonymous page data required during the application hot start process.
[0040] As another implementation of the first aspect of the present application, the method further includes:
[0041] The kernel receives the deletion information, the deletion information including the identifier of the first application;
[0042] In response to the deletion information, the kernel deletes the PTE snapshot corresponding to the identifier of the first application stored in the first storage space.
[0043] In this application, in order to save storage space, when the application is deleted from the electronic device, the PTE snapshot of the application can be deleted together.
[0044] As another implementation of the first aspect of the present application, before the kernel receives the deletion information, the method further includes:
[0045] The monitoring module receives an uninstall instruction of the first application;
[0046] In response to the uninstall instruction, the monitoring module transmits deletion information to the kernel through a system call.
[0047] As another implementation method of the first aspect of the present application, PTE includes a mapping relationship between virtual memory and physical memory, and the PTE snapshot includes the file name of the file page data corresponding to the PTE and the storage address of the file page data in the disk, as well as the storage address of the anonymous page data corresponding to the PTE in the swap partition.
[0048] In a second aspect, an electronic device is provided, comprising a processor, wherein the processor is used to run a computer program stored in a memory to implement any method of the first aspect of the present application.
[0049] In a third aspect, a chip system is provided, including a processor, wherein the processor is coupled to a memory, and the processor executes a computer program stored in the memory to implement any method of the first aspect of the present application.
[0050] In a fourth aspect, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by one or more processors, the method of any one of the first aspects of the present application is implemented.
[0051] In a fifth aspect, an embodiment of the present application provides a computer program product, which, when executed on a device, enables the device to execute any one of the methods in the first aspect of the present application.
[0052] It can be understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] Figure 1 A schematic diagram of a hardware structure of an electronic device to which the method for preloading memory pages provided in an embodiment of the present application is applied;
[0054] Figure 2 A schematic diagram of data flow from a virtual address to a physical address provided in an embodiment of the present application;
[0055] Figure 3 A schematic diagram of a page missing abnormality during cold start of an application provided in an embodiment of the present application;
[0056] Figure 4 A schematic diagram of a page missing abnormality during hot start of an application provided in an embodiment of the present application;
[0057] Figure 5 A data structure relationship diagram of a virtual space describing a process provided in an embodiment of the present application;
[0058] Figure 6 A schematic diagram of a PTE data structure provided in an embodiment of the present application;
[0059] Figure 7 A schematic diagram of time nodes for capturing a PTE snapshot and preloading a PTE snapshot during a cold start of an application provided in an embodiment of the present application;
[0060] Figure 8 A schematic diagram of time nodes for capturing PTE snapshots and preloading PTE snapshots during hot start of an application provided in an embodiment of the present application;
[0061] Fig. 9 A schematic diagram of the technical architecture of a method for preloading memory pages provided in an embodiment of the present application;
[0062] Fig.10 A timing diagram of capturing a PTE snapshot during a cold start of an application provided in an embodiment of the present application;
[0063] Fig.11 A timing diagram of preloading a PTE snapshot during a cold start of an application provided in an embodiment of the present application;
[0064] Fig.12 A schematic diagram of a process flow executed by the kernel when an application is cold started according to an embodiment of the present application;
[0065] Fig.13 A schematic diagram of a process flow executed by the kernel when an application provided in an embodiment of the present application is uninstalled;
[0066] Fig.14 A schematic diagram of a process flow executed by the kernel when an application is hot-started provided in an embodiment of the present application;
[0067] Fig.15A schematic diagram comparing the latency and IO wait of the application startup method and the native process provided in an embodiment of the present application. DETAILED DESCRIPTION
[0068] In the following description, specific details such as specific system structures and technologies are provided for illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application can also be implemented in other embodiments without these specific details.
[0069] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.
[0070] It should also be understood that in the embodiments of the present application, "one or more" refers to one, two or more than two; "and / or" describes the association relationship of associated objects, indicating that three relationships may exist; for example, A and / or B may represent: A exists alone, A and B exist at the same time, and B exists alone, where A and B may be singular or plural. The character " / " generally indicates that the associated objects before and after are in an "or" relationship.
[0071] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", "fourth", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0072] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0073] A method for preloading memory pages provided in an embodiment of the present application can be applied to an electronic device. The electronic device can be a tablet computer, a mobile phone, a wearable device, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), and other electronic devices. The embodiment of the present application does not limit the specific type of the electronic device.
[0074] Figure 1 The schematic diagram of the structure of an electronic device is shown. The electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. Among them, the sensor module 180 may include a pressure sensor 180A, a touch sensor 180K, an ambient light sensor 180L, etc.
[0075] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0076] The processor 110 may include one or more processing units, for example: the processor 110 may include an application processor (application processor, AP), a modem processor, a graphics processor (graphics processing unit, GPU), an image signal processor (image signal processor, ISP), a controller, a memory, a video codec, a digital signal processor (digital signal processor, DSP), a coprocessor (sensor coprocessor, SCP), a baseband processor, and / or a neural-network processing unit (neural-network processing unit, NPU), etc. Among them, different processing units can be independent devices or integrated in one or more processors. For example, the processor 110 is used to execute the application startup method in the embodiment of the present application.
[0077] The controller may be the nerve center and command center of the electronic device 100. The controller may generate an operation control signal according to the instruction operation code and the timing signal to complete the control of fetching and executing instructions.
[0078] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0079] The USB interface 130 is an interface that complies with USB standard specifications, and may specifically be a Mini USB interface, a Micro USB interface, a USB Type C interface, etc. The USB interface 130 may be used to connect a charger to charge the electronic device 100, and may also be used to transmit data between the electronic device 100 and a peripheral device.
[0080] It is understandable that the interface connection relationship between the modules illustrated in the embodiment of the present application is only a schematic illustration and does not constitute a structural limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.
[0081] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement a data storage function, such as storing music, video and other files in the external memory card.
[0082] The internal memory 121 can be used to store computer executable program codes, and the executable program codes include instructions. The processor 110 executes various functional applications and data processing of the electronic device 100 by running the instructions stored in the internal memory 121. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area can store an operating system and an application required for at least one function (such as a sound playback function, an image playback function, etc.).
[0083] In addition, the internal memory 121 may include a high-speed random access memory and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0084] The charging management module 140 is used to receive charging input from a charger. The charger may be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 may receive charging input from the wired charger through the USB interface 130 .
[0085] The power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140 to power the processor 110, the internal memory 121, the external memory, the display screen 194, the camera 193, and the wireless communication module 160.
[0086] In some other embodiments, the power management module 141 may also be disposed in the processor 110. In some other embodiments, the power management module 141 and the charging management module 140 may also be disposed in the same device.
[0087] The wireless communication function of the electronic device 100 can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.
[0088] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of antennas. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.
[0089] The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc., applied to the electronic device 100. The mobile communication module 150 may include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves from the antenna 1, and filter, amplify, etc. the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 1.
[0090] The wireless communication module 160 can provide wireless communication solutions including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to the electronic device 100. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, modulates the frequency of the electromagnetic wave signal and performs filtering, and sends the processed signal to the processor 110. The wireless communication module 160 can also receive the signal to be sent from the processor 110, modulate the frequency of it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.
[0091] In some embodiments, antenna 1 of electronic device 100 is coupled to mobile communication module 150, and antenna 2 is coupled to wireless communication module 160, so that electronic device 100 can communicate with the network and other devices through wireless communication technology.
[0092] The electronic device 100 can implement audio functions such as music playing and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.
[0093] The audio module 170 is used to convert digital audio signals into analog audio signals for output, and is also used to convert analog audio inputs into digital audio signals. The audio module 170 can also be used to encode and decode audio signals. In some embodiments, the audio module 170 can be arranged in the processor 110, or some functional modules of the audio module 170 can be arranged in the processor 110.
[0094] The speaker 170A, also called a "speaker", is used to convert an audio electrical signal into a sound signal. The electronic device 100 can listen to music or listen to a hands-free call through the speaker 170A.
[0095] The receiver 170B, also called a "earpiece", is used to convert audio electrical signals into sound signals. When the electronic device 100 receives a call or voice message, the voice can be received by placing the receiver 170B close to the human ear.
[0096] Microphone 170C, also called "microphone" or "microphone", is used to convert sound signals into electrical signals. When making a call or sending a voice message, the user can speak by putting their mouth close to microphone 170C to input the sound signal into microphone 170C. The electronic device 100 can be provided with at least one microphone 170C. In other embodiments, the electronic device 100 can be provided with two microphones 170C, which can not only monitor voice information but also realize noise reduction function. In other embodiments, the electronic device 100 can also be provided with three, four or more microphones 170C to realize the collection of sound signals, noise reduction, identification of sound sources, realization of directional recording functions, etc. For example, microphone 170C can be used to collect voice information involved in the embodiments of the present application.
[0097] The earphone interface 170D is used to connect a wired earphone and can be a USB interface 130 or a 3.5 mm open mobile terminal platform (OMTP) standard interface or a cellular telecommunications industry association of the USA (CTIA) standard interface.
[0098] The pressure sensor 180A is used to sense the pressure signal and can convert the pressure signal into an electrical signal. In some embodiments, the pressure sensor 180A can be set on the display screen 194. There are many types of pressure sensors 180A, such as resistive pressure sensors, inductive pressure sensors, capacitive pressure sensors, etc. A capacitive pressure sensor can be a parallel plate including at least two conductive materials. When a force acts on the pressure sensor 180A, the capacitance between the electrodes changes. The electronic device 100 determines the intensity of the pressure based on the change in capacitance. When a touch operation acts on the display screen 194, the electronic device 100 detects the intensity of the touch operation based on the pressure sensor 180A. The electronic device 100 can also calculate the position of the touch based on the detection signal of the pressure sensor 180A.
[0099] The touch sensor 180K is also called a "touch panel". The touch sensor 180K can be set on the display screen 194, and the touch sensor 180K and the display screen 194 form a touch screen, also called a "touch screen". The touch sensor 180K is used to detect touch operations acting on or near it. The touch sensor can pass the detected touch operation to the application processor to determine the type of touch event. Visual output related to the touch operation can be provided through the display screen 194. In other embodiments, the touch sensor 180K can also be set on the surface of the electronic device 100, which is different from the position of the display screen 194.
[0100] The ambient light sensor 180L is used to sense the ambient light brightness. The electronic device 100 can adaptively adjust the brightness of the display screen 194 according to the perceived ambient light brightness. The ambient light sensor 180L can also be used to automatically adjust the white balance when taking pictures. The ambient light sensor 180L can also cooperate with the proximity light sensor 180G to detect whether the electronic device 100 is in a pocket to prevent accidental touches.
[0101] The key 190 includes a power key, a volume key, etc. The key 190 may be a mechanical key or a touch key. The electronic device 100 may receive key input and generate key signal input related to user settings and function control of the electronic device 100.
[0102] Motor 191 can generate vibration prompts. Motor 191 can be used for incoming call vibration prompts, and can also be used for touch vibration feedback.
[0103] The electronic device 100 implements the display function through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.
[0104] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.
[0105] The camera 193 is used to capture still images or videos. The object generates an optical image through the lens and projects it onto the photosensitive element. The photosensitive element can be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the optical signal into an electrical signal, and then passes the electrical signal to the ISP to be converted into a digital image signal. The ISP outputs the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in a standard RGB, YUV or other format. In some embodiments, the electronic device 100 may include 1 or N cameras 193, where N is a positive integer greater than 1.
[0106] The SIM card interface 195 is used to connect a SIM card. The SIM card can be connected to and separated from the electronic device 100 by inserting it into the SIM card interface 195 or pulling it out from the SIM card interface 195. The electronic device 100 can support 1 or N SIM card interfaces, where N is a positive integer greater than 1. The SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, and the like. Multiple cards can be inserted into the same SIM card interface 195 at the same time. The types of the multiple cards can be the same or different. The SIM card interface 195 can also be compatible with different types of SIM cards. The SIM card interface 195 can also be compatible with external memory cards. The electronic device 100 interacts with the network through the SIM card to implement functions such as calls and data communications. In some embodiments, the electronic device 100 uses an eSIM, i.e., an embedded SIM card. The eSIM card can be embedded in the electronic device 100 and cannot be separated from the electronic device 100.
[0107] The embodiment of the present application does not particularly limit the specific structure of the execution subject of a method for preloading memory pages, as long as the code of the method for preloading memory pages of the embodiment of the present application can be run to communicate according to the method for preloading memory pages provided by the embodiment of the present application. For example, the execution subject of the method for preloading memory pages provided by the embodiment of the present application can be a functional module in an electronic device that can call and execute a program, or a communication device applied to an electronic device, such as a chip.
[0108] In paged memory management, the operating system can allocate virtual memory to a process. For example, in a 32-bit operating system, the maximum space of virtual memory allocated to each process is 4GB. Virtual memory can be divided into pages of fixed size, each page is a page. For example, virtual memory can be divided into pages of 4KB (or 8KB, 16KB, etc.). At the same time, the kernel can also divide the physical memory of the electronic device into pages of the same size, and the pages in the physical memory can be recorded as physical pages. In this way, whether it is virtual memory or physical memory, pages are the smallest allocation unit.
[0109] The data corresponding to the user program (for example, the code segment, data segment, and stack segment, etc.) is stored in the physical memory. The user program cannot directly access or view the physical memory. However, the user program can directly access or view the virtual memory. Therefore, it is necessary to establish a mapping relationship between the virtual memory and the physical memory, that is, the mapping relationship from the virtual memory to the physical memory.
[0110] As mentioned earlier, the smallest allocation unit in virtual memory and physical memory is a page. Each page in virtual memory has a virtual address, and each page in physical memory has a physical address. Therefore, the mapping relationship between virtual memory and physical memory can be represented by recording the mapping relationship between the virtual address of virtual memory and the physical address of physical memory.
[0111] Reference Figure 2 , a flow diagram from virtual address to physical address provided by an embodiment of the present application. When the central processing unit (CPU) needs to access data or obtain instructions, it sends the virtual address corresponding to the data or instruction to the memory management unit (MMU), and the memory management unit is responsible for finding the physical address corresponding to the virtual address according to the above mapping relationship. The memory management unit sends the found physical address to the physical memory, and the physical memory reads or writes the relevant data or instructions after performing corresponding access according to the physical address.
[0112] The memory management unit saves a page table for each user program. The page table stores the mapping relationship between virtual pages (the virtual pages correspond to virtual addresses) and physical pages (the physical pages correspond to physical addresses). When a physical page is assigned to a virtual page, a record is added to the page table to save the mapping relationship between the virtual page and the physical page. Subsequently, the received virtual address can be converted into a physical address based on the mapping relationship between the virtual page and the physical page saved in the page table.
[0113] Of course, after the memory management unit receives a virtual address, it first needs to determine whether the virtual address is valid, whether it is in the physical memory, and whether it is protected.
[0114] If the virtual address is valid and there is a corresponding physical address in the physical memory and it is not protected (that is, it is accessible), the virtual address can be used as an index to find the physical address corresponding to the virtual address in the page table.
[0115] However, there may also be the following situation that triggers a page fault exception: the virtual address is valid, but the physical address corresponding to the virtual address does not exist in the physical memory.
[0116] This may be due to the lazy allocation mechanism, that is, when a user program is started, virtual memory will be allocated for the user program, but physical memory will not be allocated at this time, so there is naturally no mapping relationship between virtual memory and physical memory. When the user program needs to access certain data during operation, it will search for the physical address corresponding to the virtual address in the physical memory according to the virtual address corresponding to the data. However, since the physical memory has not been allocated, the physical address corresponding to the virtual address cannot be found in the physical memory, which will trigger a page fault exception. This page fault exception is more likely to occur during the cold start process of the application.
[0117] This situation may also be caused by the access data being swapped out of the physical memory. For example, after the user program switches to the background, due to limited physical memory resources, the operating system will start memory page replacement and put the physical pages that have not been used for a long time (for example, anonymous pages) into the swap partition to give the freed physical memory resources to other user programs. If the data accessed by the user program is located in the swap partition, the physical address corresponding to the virtual address will not be found in the physical memory, which will trigger a page fault exception. The operating system may also recycle physical pages that have not been used for a long time (for example, file pages) to give the freed physical memory resources to other user programs. If the data accessed by the user program has been recycled from the physical memory to the disk, the physical address corresponding to the virtual address will not be found in the physical memory, which will trigger a page fault exception. This page fault exception occurs more frequently during the hot start of an application.
[0118] Regardless of the type of page fault exception, it may be necessary to read data from the disk or swap partition (for example, read file page data from the disk, read anonymous page data from the swap partition) to the physical memory, establish a mapping relationship between the virtual address of the read memory page data (including file page data and anonymous page data) and the current physical address of the read data, fill in the PTE, and the user program can continue to run. This situation seriously blocks the operation of the user program.
[0119] Of course, in actual applications, there may be other abnormal situations. The embodiment of the present application does not focus on the causes of page fault exceptions, so they will not be listed one by one.
[0120] After a page fault exception occurs, the operating system will put the user program into the blocking queue until the page fault exception problem is resolved, and then the user program will be put into the ready queue and continue to run. Therefore, after a page fault exception occurs, the operation of the user program will be blocked.
[0121] During the application startup process, the above-mentioned page fault exception may occur more frequently, resulting in a slower application startup process and a poor user experience.
[0122] The application startup methods include cold startup and hot startup. When the application is started, if the process of the application does not exist in the system, the system needs to create a process for the application, indicating that the application is started cold this time.
[0123] As an example of application cold start, after the electronic device is turned on, the user clicks the application icon on the system desktop to start the application. This startup process of the application is a cold start.
[0124] As another example of application cold start, after the user starts the application, the user switches the application to background operation, and the system kills the application process due to resource recovery. When the user starts the application again, the current startup process of the application is a cold start.
[0125] Reference Figure 3 , which is a schematic diagram of a page fault exception during the cold start of an application provided in an embodiment of the present application. In this diagram, the horizontal axis represents the various stages in the cold start of an application, among which several stages are highlighted: ApplicationStart, BindApplication, ActivityStart, Choreographer, and DoFrame. For details of these stages, please refer to the description of the subsequent embodiments. Black vertical lines indicate that a page fault exception has occurred. The thicker the black vertical lines, the more page fault exceptions have occurred in a short period of time. Figure 3It can be understood that during the cold start process of the application, more page fault exceptions occurred in the BindApplication phase, and a small number of page fault exceptions also occurred in the ActivityStart phase. These page fault exceptions will cause the cold start process of the application to be slower.
[0126] When an application is started, if a process for the application already exists in the system, the system does not need to create a process for the application. Instead, the system uses the existing process in the system to start creating the Activity and subsequent operations, indicating that the application is started as a hot start.
[0127] As an example of application hot start, after the user starts the application, the user switches the application to the background. The user can switch the application to the foreground in a variety of ways. For example, when the application is running in the background, click the icon of the application on the system desktop; when the application is running in the background, enter the application from the negative one screen; when the application is running in the background, start the application through a small program, etc. Before the application is switched to the foreground, the process of the application has always existed in the system and has not been killed by the system. In the case where the process of the application is not killed, the startup process of the application switching from the background to the foreground is a hot start.
[0128] Reference Figure 4 , which is a schematic diagram of a page fault exception during the application hot start process provided in an embodiment of the present application. In this diagram, the horizontal axis represents the application hot start process, in which several stages are highlighted: ActivityStart, ActivityResume, Choreographer, and DoFrame. These stages are described in detail in the subsequent embodiments. Black vertical lines indicate that a page fault exception has occurred. The thicker the black vertical lines, the more page fault exceptions have occurred in a short period of time. Figure 4 It can be understood that during the application hot start process, many page fault anomalies occurred in the ActivityStart stage, ActivityResume stage, Choreographer stage and DoFrame stage. These page fault anomalies will cause the application hot start process to be slow.
[0129] pass Figure 3 and Figure 4 It is understandable that during the application startup process, page fault exceptions will frequently occur, which will result in a slower application startup and a poor user experience.
[0130] Figure 3 and Figure 4 The number of page fault exceptions in is only used for comparison between stages and does not indicate the absolute number of page fault exceptions that occur in each stage.
[0131] To solve the above problem, an embodiment of the present application provides a method for preloading memory pages. After the application is started, the information of missing pages during the application startup process can be recorded through a page table entry (PTE) snapshot to generate a PTE snapshot, and the PTE snapshot is stored on the disk (it can also be stored in the physical memory during a hot start); when the application is started subsequently, the memory page data corresponding to the recorded missing page information can be preloaded in the physical memory based on the PTE snapshot stored on the disk (or physical memory).
[0132] For cold start, the generated PTE snapshot includes the file name and storage address of the file page data corresponding to the PTE written during this startup, so the preloaded data is the file page data corresponding to the PTE snapshot; the above preloading process may be before the process is created. After the file page data is preloaded in the physical memory, there is no need to load the file page data from the disk to the physical memory during the subsequent startup process, which can save the time of loading the file page data from the disk to the physical memory and improve the speed of application cold start.
[0133] In addition, during the cold start process, after the process is started, the system will allocate virtual memory for the process, establish a mapping relationship between the virtual address and the physical address according to the physical address of the loaded file page data in the physical memory, and fill in the PTE (for example, fill in the mapping relationship between the virtual address and the physical address). After filling in the PTE, the occurrence of page fault exceptions can be reduced, and the speed of cold start of the application can be improved.
[0134] For hot start, the generated PTE snapshot includes the file name and storage address of the file page data corresponding to the PTE accessed during this startup, as well as the storage address of the anonymous page data corresponding to the PTE accessed during this startup in the swap partition, etc. Therefore, the data loaded in the physical memory includes the file page data (for example, the data recovered to the disk) and the anonymous page data (for example, the data swapped to the swap partition); since the application process itself exists in the system, the virtual space of the process has been allocated long ago, so during the hot start process, after the memory page data is loaded in the physical memory according to the PTE snapshot, the mapping relationship between the virtual address and the physical address can be directly established according to the physical address of the loaded memory page data in the physical memory, and the PTE can be filled (for example, the mapping relationship between the virtual address and the physical address is filled). This reduces the occurrence of page fault exceptions and improves the speed of application hot start.
[0135] For the convenience of subsequent description, memory page data is uniformly used to represent data preloaded in physical memory according to PTE snapshots regardless of cold start or hot start.
[0136] In order to more clearly understand the method for preloading memory pages provided in the embodiment of the present application, the source of the PTE snapshot is first described.
[0137] When an application is cold started, the system creates a process for the application and allocates virtual memory (for example, 4 GB) to the process. When an application is hot started, the process of the application is not killed in the system, and the virtual memory of the process also exists.
[0138] The virtual space corresponding to a process is described by two data structures, one is mm_struct and the other is vm_area_struct.
[0139] See also Figure 5 Each process has only one mm_struct structure. The mm_struct structure describes the entire virtual space of a process. The mm_struct structure includes the process's page directory pointer (Page Global Directory, pgd) and multiple pointers mmap pointing to the vm_area_struct structure.
[0140] When the system allocates virtual space to a process, it allocates it according to the segment, that is, it can allocate multiple virtual memory areas (VMAs) to a process, and each virtual area corresponds to a vm_area_struct. When n virtual areas are allocated to a process, there will be n vm_area_structs, and there will be n pointers mmap in the mm_struct structure that point to different vm_area_struct structures.
[0141] Taking the vm_area_struct corresponding to a virtual interval as an example, vm_start and vm_end respectively represent the starting address and the ending address of the area pointed to by the virtual interval.
[0142] vm_flags describes the properties of this virtual interval. Flags can be VM_READ (readable), VM_WRITE (writable), VM_EXEC (executable), or VM_SHARED (shared by multiple processes).
[0143] Other parameters in vm_area_struct are not listed one by one.
[0144] Each virtual interval is composed of many virtual pages, and the virtual pages in each virtual interval point to physical pages through page table entries (PTE) in the page table (PT).
[0145] The page directory pointer pgd points to the page table of the process. The page table records the mapping relationship between virtual pages and physical pages. The page table contains multiple page table entries (PTE). Each page table entry points to a page frame. The page frame is the physical page in the real physical memory, and data can be stored in the physical page.
[0146] Taking a 4GB virtual memory as an example, it is divided into virtual pages of 4KB. Then there are 1024*1024 virtual pages in the virtual memory. Correspondingly, the page table of the process contains 1024*1024 page table entries PTE.
[0147] When a process needs to access a virtual address, it can find the vm_area_struct where the virtual address is located from the mm_struct structure. If the information recorded in the vm_area_struct shows that the virtual address has not been assigned a physical address, a page fault exception will be triggered, a physical address will be assigned to the virtual address, and the relevant information of the virtual address in the vm_area_struct will be modified (for example, modify the unallocated physical memory to the allocated physical memory), and fill the mapping relationship between the virtual address and the physical address into the PTE corresponding to the virtual address. Therefore, the vm_area_struct and PTE can record the relevant information of all page fault exceptions during the application startup process.
[0148] The embodiment of the present application needs to traverse the PTE to find the PTE filled (or accessed) in the current startup process to generate a PTE snapshot. However, the page table of a process contains 1024*1024 page table entry PTEs, and the traversal process is very time-consuming.
[0149] As mentioned above, when accessing a virtual address, the corresponding PTE can be found from vm_area_struct. Similarly, when traversing PTEs, it is also possible to quickly determine from vm_area_struct whether to traverse the PTEs corresponding to each virtual address. As an example, the virtual space corresponding to the application process contains n (for example, 3, 4, etc.) virtual intervals, each of which corresponds to a vm_area_struct. If the first virtual interval is not mapped to physical memory, there is no need to traverse the vm_area_struct corresponding to the virtual interval, and directly jump to the vm_area_struct corresponding to the second virtual interval, which can improve the speed of traversing PTEs; after jumping to the vm_area_struct corresponding to the second virtual interval, according to the relevant information of each virtual address in the vm_area_struct corresponding to the second virtual interval (for example, whether it is valid, whether it has a physical address, whether it is accessible, etc.), the PTE corresponding to the valid, physical address, and accessible virtual address is found from the page table. In this way, PTEs can be quickly traversed.
[0150] Through the traversal method described above, PTE can be quickly traversed to search for memory pages filled (or accessed for hot start) during this startup process, and then PTE snapshots are generated based on these searched PTEs. When the application is subsequently started, the memory page data can be preloaded in the physical memory based on the stored PTE snapshot, saving the process of loading memory page data in the physical memory during the application startup process; after the memory page data is loaded in the physical memory, the PTE can also be filled when the process startup completes the allocation of virtual space or the process itself has allocated virtual space, which can also reduce the occurrence of page fault exceptions, thereby improving the startup speed of the application.
[0151] In the embodiment of the present application, a five-level mapped page table is used as an example to describe the PTE in the page table.
[0152] Reference Figure 5 The page table includes a page directory pointer (Page Global Directory, pgd), a page four directory pointer (page four directory, p4d), a page upper directory pointer (Page Upper Directory, pud), a page middle directory pointer (Page Middle Directory, pmd), and a page table entry (Page Table Entry, PTE).
[0153] The pgd corresponding to each process is different. When a new process is created, a new page directory pointer pgd is created for the new process. pgd contains the addresses of several p4ds, p4d contains the addresses of several puds, pud contains the addresses of several pmds, and pmd contains the addresses of several page table entries PTE. Each page table entry PTE points to a page frame, which is the physical page in the real physical memory.
[0154] Reference Figure 6 , which is an example of the structure of a page table entry PTE.
[0155] Bits 12 to 31 in PTE are address bits, which store the 4K aligned starting physical address corresponding to the virtual page. Usually, the address of PTE is page aligned. Since only the upper 20 bits are valid address bits, the page table entry pointing to the physical address only needs the upper 20 bits to represent the physical address. Therefore, the remaining 12 bits can be used for other purposes as status bits.
[0156] As an example, the P (also known as present) bit indicates whether the virtual page corresponding to the page table entry has been mapped to the physical memory; if the bit is 0, it means that the virtual page has not been mapped, the data corresponding to the virtual page has been swapped out from the physical memory, or the virtual page is no longer used. When the bit is 0, it indicates that the virtual page corresponding to the page table entry has been mapped to the physical memory.
[0157] The R / W bit indicates the read / write properties of the physical page pointed to by the virtual page corresponding to the page table entry, for example, 0 is read-only and 1 is readable and writable. The R / W bit in the PTE is set by software based on the virtual interval to which the virtual page belongs, that is, the VM_READ / VM_WRITE properties set for a virtual interval will be copied to the PTE of the virtual page contained in this virtual interval.
[0158] U / S indicates the access permission of the physical page pointed to by the virtual page corresponding to the page table entry. For example, 0 means that it can only be accessed by the kernel.
[0159] The specific meanings of other bits will not be explained one by one.
[0160] In addition, each PTE corresponds to a pageflag (ie, page object), which records whether the physical page represented by the PTE belongs to a file page or an anonymous page (anon), as well as the specific type of the file page, for example, active, referenced.
[0161] For an application, only some PTE pages may be used during the application startup process. For example, there may be 1024*1024 PTE pages in 4GB of virtual memory. Only some PTE pages may be used during the application startup process. Therefore, it is necessary to filter out valid PTE pages (PTE pages used during this startup process). In actual applications, valid PTE pages can be identified from PTEs through the status bit (for example, the P bit indicates that it has been mapped to physical memory); then snapshot information is generated based on the valid PTE pages, and the PTE snapshot of the application at this startup can be obtained. The PTE snapshot can be stored in the disk (hot startup can be stored in physical memory). When the application is started next time, the memory page data corresponding to the information in the PTE snapshot is pre-loaded from the disk (or physical memory) into the physical memory, saving the process of loading memory page data from the disk or swap partition to the physical memory during the startup process; and, if the PTE can be filled, the PTE is pre-filled, thereby reducing the occurrence of page fault exceptions and improving the speed of application startup.
[0162] It should be noted that when the application is cold started, if the application version is consistent, the file page read is also fixed. Therefore, after the application cold start is completed, the valid PTE corresponding to the file page in the missing page information of the application during this cold start process (for example, it can be determined based on the page object whether it is a file page) (for example, it can be determined based on the P bit that it has been mapped to the physical memory) generates a PTE snapshot corresponding to the cold start and stores it in the form of a file on the disk. Therefore, for a cold start, a PTE snapshot can be generated based on the PTE with the page object as a file page, and the file page is pre-read during a subsequent cold start. Therefore, for a cold start, a method for preloading memory pages provided in an embodiment of the present application can also be understood as a method for preloading file pages.
[0163] When the application is hot-started, due to the memory recycling mechanism, it may be necessary to generate a PTE snapshot for the PTE corresponding to the anonymous page in the missing page information. In addition, the application hot-start process itself takes a short time. If all file pages are preloaded, it may also affect the speed of the application hot-start. Therefore, you can select the PTE corresponding to some types of file pages to generate a PTE snapshot according to the actual situation. For example, you can select the active and referenced types.
[0164] It should be noted that in actual applications, different status bits may be selected to determine the physical page for generating a PTE snapshot according to actual conditions, or PTEs corresponding to different types of file pages may be selected to generate a PTE snapshot.
[0165] Based on the above description, it can be understood that after the startup is completed, some types of file pages (for example, active, referenced) and anonymous pages in the missing page information during this hot start process can be applied to generate a PTE snapshot corresponding to the hot start, and stored in the disk in the form of a file, or can also be cached in the memory.
[0166] When the application is subsequently cold started, the PTE snapshot corresponding to the cold start is read from the disk; when the application is subsequently hot started, the PTE snapshot corresponding to the hot start is read from the disk or cache.
[0167] The following will describe the specific implementation of the method for preloading memory pages provided in an embodiment of the present application based on the above principles.
[0168] Take the example of the startup method when a user clicks the application icon on the system desktop for the first time after turning on an electronic device. This startup method is a cold start. The cold start process mainly includes:
[0169] The system starts to load and start the application and creates a process for the application. This process can be recorded as ApplicationStart.
[0170] The system binds the created process to the application, and this process can be recorded as BindApplication.
[0171] The application process creates and starts the first Activity, which is recorded as ActivityStart.
[0172] Create a Choreographer object to monitor Vsync so that drawing tasks can be performed according to the screen refresh rate. This process is recorded as Choreographer.
[0173] Process drawing tasks according to the screen refresh rate, for example, the measurement, layout, and drawing process of the window corresponding to the first Activity. The drawing stage in this process can be recorded as DoFrame.
[0174] pass Figure 3 It can be understood that when the application is cold started, page fault exceptions mainly occur in the BindApplication phase, and a small number of page fault exceptions occur in the ActivityStart phase. After the ActivityStart phase ends, page fault exceptions rarely occur.
[0175] Reference Figure 7 In (a), after the ActivityStart phase ends, the status bits of the PTE in the page table and the pageflag pointed to by the PTE can be traversed to obtain the file pages used during the cold start of this application, and a PTE snapshot can be generated based on these used file pages.
[0176] As another embodiment of the present application, refer to Figure 7 (b) in the figure does not mean that page fault exception will not occur at all after the ActivityStart phase ends, but the probability of occurrence is small. Therefore, in order to reduce the probability of page fault exception during subsequent cold start of the application, the status bits of PTE in the page table and the pageflag pointed to by PTE can be traversed at the end of the DoFrame phase (the interface of the first Activity will be displayed after the DoFrame phase ends) or within a certain period of time thereafter, to obtain the file pages used during the cold start of this application, and generate PTE snapshots based on the file names of the file page data stored in these used file pages, the storage addresses on the disk, etc.
[0177] The reasons why the PTE snapshot can be used in subsequent cold starts include: during this cold start, the file page data will be read from the disk to the physical memory, and the physical address of the file page data will be filled in the PTE; in order to be able to read the file page data from the disk to the physical memory in advance during the next cold start, the file name of the file page data in the physical page corresponding to the physical address in the PTE filled this time and the storage address of the file page data on the disk can be generated into a PTE snapshot. In this way, during subsequent cold starts, the storage address of the file page data on the disk used in the cold start process of the application can be obtained based on the PTE snapshot, and this part of the file page data can be read from the disk to the physical memory. After the application process is started and the virtual space is allocated, a mapping relationship can be established between the physical address and the virtual address of this part of the file page data in the physical memory, and the PTE can be continued to be filled, which will reduce the occurrence of page fault exceptions.
[0178] As another embodiment of the present application, in actual applications, it is also possible to traverse the status bits of the PTE in the page table and the pageflag pointed to by the PTE at any time point between the end of the ActiveStart phase and the end of the DoFrame phase, obtain the file pages used during the cold start of this application, and generate a PTE snapshot based on these used file pages.
[0179] Regardless of the PTE snapshot generated at any of the above-listed time points, it can be stored in the disk of the electronic device for use by the application in a subsequent cold start. Of course, the timing of generating the PTE snapshot and storing it in the disk of the electronic device can be later, as long as it can be successfully stored in the next cold start. As an example, it can be stored immediately after generation, or when the application switches to background operation or ends operation.
[0180] Reference Figure 7In (c), during the subsequent cold start process of the application, since the page fault exception mainly occurs in the BindApplication stage and thereafter, it is necessary to read the PTE snapshot stored during the historical cold start of the application from the disk of the electronic device before the BindApplication stage, and then load the file page data required by the process of the application in the physical memory according to the information recorded in the PTE snapshot, saving the process of loading the file page data during the startup process. Subsequently, after BindApplication (after BindApplication, the process has been fully started and virtual space has been allocated), the PTE information can be filled, reducing the probability of page fault exceptions in the subsequent startup process and increasing the speed of application startup.
[0181] As another embodiment of the present application, after the electronic device receives a click operation on the icon of the application on the system desktop, before the BindApplication phase begins, the PTE snapshot stored during the historical cold start of the application can be read from the disk of the electronic device, and then the file page data required by the process of the application can be loaded in the physical memory according to the information recorded in the PTE snapshot, saving the process of loading file page data during the startup process. Subsequently, after BindApplication, the PTE information can be filled, reducing the probability of subsequent page fault exceptions and increasing the speed of application startup.
[0182] The hot start of an application means that after the application is returned to the background, the application process is not destroyed. Therefore, in the hot start, starting from ActivityStart, the hot start process of the application mainly includes:
[0183] The application process creates and starts the last Activity before returning to the background. This process is recorded as ActivityStart.
[0184] The last Activity before returning to the background enters the resume state, and this process is recorded as ActivityResume.
[0185] Create a Choreographer object to monitor Vsync so that drawing tasks can be performed according to the screen refresh frequency. This process is recorded as Choreographer.
[0186] Process drawing tasks according to the screen refresh rate, for example, the measurement, layout, and drawing process of the window corresponding to the first Activity. The drawing stage in this process can be recorded as DoFrame.
[0187] pass Figure 4It can be understood that when the application is hot started, page fault exceptions mainly occur in the ActivityStart stage, ActivityResume stage and Choreographer stage.
[0188] Reference Figure 8 In (a), at the end of or after the DoFrame phase, the PTE in the page table can be traversed, and according to the status bit and the pageflag pointed to by the PTE, some file pages (for example, the selection of Active type and referenced type file pages) and anonymous pages (for example, the selection of anon type according to the pageflag corresponding to the PTE) used in the current application hot start can be obtained, and a PTE snapshot can be generated based on these used file pages and anonymous pages.
[0189] In actual applications, the duration of the hot start itself is relatively short. If all the page fault exception information during the hot start process is still obtained, the process of reading the PTE snapshot from the disk or memory and preloading it during the subsequent hot start will become longer, which undoubtedly prolongs the duration of the hot start. Therefore, a balance can be struck between the time consumed in preloading and the time consumed incurred by the page fault exception. For example, by selecting the more important page fault exception information during the hot start process to generate a PTE snapshot, the preloading time during the subsequent hot start will become shorter, thereby reducing the overall duration of the hot start.
[0190] Most of the important page fault exception information during the hot start process is in the ActivityStart stage, refer to Figure 8 In (b), at the end of the ActivityStart phase or in a short time thereafter, the status bits of the PTE in the page table and the pageflag pointed to by the PTE can be traversed to obtain some file pages (for example, the active type and referenced type file pages are selected according to the pageflag corresponding to the PTE) and anonymous pages (for example, the anon type is selected according to the pageflag corresponding to the PTE) used during the hot start of this application, and a PTE snapshot can be generated based on these used file pages and anonymous pages.
[0191] As another embodiment of the present application, at any time point between the ActivityStart stage and the end of the DoFrame stage, the status bits of the PTE in the page table and the pageflag pointed to by the PTE can be traversed to obtain some file pages used during the hot start of this application (for example, the selection of active type and referenced type file pages is determined according to the pageflag corresponding to the PTE) and anonymous pages (for example, the selection of anon type is determined according to the pageflag corresponding to the PTE), and a PTE snapshot is generated based on the information of these used file pages and anonymous pages (for example, the file name of the file stored in the file page, the storage address on the disk, and other information, and the storage address of the data stored in the anonymous page in the swap partition).
[0192] The PTE snapshot generated at any of the time points listed above can be stored in the memory or disk of the electronic device for use in subsequent hot start of the application.
[0193] Reference Figure 8 In (c), during the subsequent hot start process of the application, since page fault exceptions have already occurred in large numbers in the ActivityStart stage, it is necessary to read the PTE snapshot stored during the historical hot start of the application from the memory or disk of the electronic device before the ActivityStart stage, and then load the file page data and anonymous page data required by the application process according to the information recorded in the PTE snapshot. Since the process itself exists in the system during the hot start and the virtual space of the process has been allocated, the PTE information can be pre-filled, which reduces the probability of page fault exceptions and improves the speed of application startup.
[0194] As another embodiment of the present application, after the electronic device detects the hot start of the application, the PTE snapshot stored during the historical hot start of the application can be read from the memory or disk of the electronic device, and then the file page data and anonymous page data required by the application process can be loaded according to the information recorded in the PTE snapshot, so that the PTE information can be pre-filled, the probability of page fault exceptions is reduced, and the speed of application startup is improved.
[0195] In order to more clearly understand the method for preloading memory pages provided in an embodiment of the present application, the process of obtaining and storing a PTE snapshot and the process of preloading a PTE snapshot will be described in detail below.
[0196] Linux divides the virtual address space into two parts: user space and kernel space. User space cannot directly access kernel space. When a user process must access the kernel or use kernel functions, a system call is required.
[0197] The kernel space provides a function entry for system calls; in order to enable the user space to use these system call entries, these system calls can be encapsulated in the user space to obtain an encapsulation interface that can be directly used by the user space process.
[0198] When a user space process needs to use a system call to access certain resources on a hardware device or operating system, the user space process uses the system call through the encapsulation interface, and the system call implements the access to the hardware device or system resources through the kernel function in the kernel. That is, the system call can serve as an information exchanger between the user space and the kernel space.
[0199] Reference Fig. 9 In the system architecture diagram shown, there are multiple applications at the application layer, for example, application A and application B.
[0200] In the application framework layer, there are a monitoring module, a pre-reading module and a capturing module, wherein the pre-reading module and the capturing module can exist as one module or as two independent modules.
[0201] The monitoring module can be a separate module for monitoring the application startup process, or it can be some services in the system that can monitor the application startup process. After monitoring that the application is started at a certain time node (for example, a time node that requires pre-reading), the monitoring module can send a pre-reading instruction to the pre-reading module. After monitoring that the application is started at a certain time node (for example, after the DoFrame stage), the monitoring module can send a capture instruction to the capture module.
[0202] After the pre-reading module receives the pre-reading instruction (or the capturing module receives the capturing instruction), a system call may be made to transmit the unique identifier and the starting mode of the application currently being started to the called kernel function.
[0203] There are kernel functions in the kernel layer, and the kernel functions are used to implement different functions, for example, the pre-reading function provided in the embodiment of the present application, and the function of traversing PTE to generate PTE snapshots provided in the embodiment of the present application.
[0204] The hardware layer has disks and physical memory. The disk is used to store the PTE snapshots generated during cold start and the file page data required during application startup. During subsequent startups, the kernel first reads the PTE snapshots from the disk, and then preloads the file page data corresponding to the information recorded in the PTE snapshots from the disk into the physical memory. The PTE snapshots generated during hot start can be stored in the disk or physical memory according to actual needs.
[0205] To better understand the interaction between the modules, refer to Fig.10The timing diagram of the process of generating and storing PTE snapshots when the application is cold started is shown. The monitoring module and the capture module are located in the user space, and the kernel is located in the kernel space.
[0206] S101, the monitoring module detects that the application runs to a capture node when it is started.
[0207] In the embodiment of the present application, if the application startup process is a cold start, the capture node is pre-set in Figure 7 (a) to Figure 7 Any node between the nodes shown in (b) in .
[0208] S102, after the monitoring module detects that the application runs to the capture node when it is started, the monitoring module sends a capture instruction to the capture module.
[0209] S103, the capture module makes a system call, and transmits a unique identifier and a startup mode identifier of the application currently being started to the called kernel function.
[0210] In the embodiment of the present application, each application has a unique identifier, which is used to distinguish different applications. When storing the PTE snapshot corresponding to the application, the unique identifier of the application is associated to distinguish it from the PTE snapshots of other applications; the startup method is used to determine whether the generated PTE snapshot is a hot start or a cold start.
[0211] In the embodiment of the present application, the startup mode identifier indicates that this startup is a cold start.
[0212] S104, the kernel traverses the PTE to obtain information about valid file pages in this startup process.
[0213] If the boot mode identifier indicates that this boot is a cold boot, the file names and storage addresses in the disk space of all valid file pages (for example, valid file pages are: the PTE status bit indicates that it has been mapped to the physical address, and pageflag is the file page) are obtained.
[0214] S105, the kernel generates a PTE snapshot based on the information of the traversed valid file page. Of course, the PTE snapshot may also include other information of the file page data, which is not limited in the embodiment of the present application.
[0215] S106: The kernel stores the cold start PTE snapshot corresponding to the application identifier in the disk.
[0216] In this way, the information of all valid file pages during the cold start of the application can be obtained for use in subsequent cold starts of the application.
[0217] Reference Fig.11The timing diagram of the process of pre-reading PTE snapshots when the application is cold started is shown. The monitoring module and the pre-reading module are located in the user space, and the kernel is located in the kernel space.
[0218] S111, the monitoring module detects that the application starts and proceeds to the pre-reading node.
[0219] In the embodiment of the present application, if the application startup process is a cold start, the pre-read node is Figure 7 Any node before the start of BindApplication shown in (c).
[0220] S112, after the monitoring module detects that the application starts and reaches the pre-reading node, the monitoring module sends a pre-reading instruction to the pre-reading module.
[0221] S113, the pre-reading module makes a system call, and transmits the unique identifier and the startup mode of the application currently being started to the called kernel function.
[0222] In the embodiment of the present application, each application has a unique identifier for distinguishing different applications to obtain the PTE snapshot corresponding to the application; the startup mode identifier is used to determine whether to obtain the PTE snapshot during the historical hot start or the PTE snapshot during the historical cold start.
[0223] The startup mode identifier in the embodiment of the present application indicates that this startup is a cold start.
[0224] S114, after the kernel is called by the system, the kernel may read the cold start PTE snapshot corresponding to the application identifier from the disk based on the received application identifier.
[0225] S115, after the kernel reads the PTE snapshot, the file page data corresponding to the PTE snapshot read from the disk is loaded into the physical memory.
[0226] Subsequently, after the application process is started and successfully applies for virtual space, the kernel can fill in PTE information in the page table of the process corresponding to the application identifier according to the physical address where the file page data loaded in the physical memory is currently located (for example, fill in the mapping relationship between the virtual address and the physical address).
[0227] Through this preloading method, the file page information required during the cold start of the application can be loaded in the physical memory in advance, saving the process of loading file page data during the startup process. In addition, the PTE information can be filled after the application process is started and the virtual memory is applied for, reducing the occurrence of page fault exceptions and improving the speed of application cold start.
[0228] It should be noted that in the specific implementation, there is also the following logic: if the disk has stored the PTE snapshot of the same application during the historical cold start, then the PTE snapshot of the same application during the subsequent cold start does not need to be stored again. Accordingly, when the application is cold started, there will be no pre-reading process only if the disk has stored the PTE snapshot of the application during the historical cold start.
[0229] Reference Fig.12 , which is a logic flow chart for cold starting an application provided in an embodiment of the present application.
[0230] At some point after the cold start begins, the kernel will passively receive pre-read information (this information is used to indicate pre-reading). The pre-read information carries the application identifier and the boot mode identifier. The kernel will determine whether the disk has a PTE snapshot corresponding to the application identifier and the boot mode identifier; if the disk has a PTE snapshot corresponding to the application identifier and the boot mode identifier, the PTE snapshot will be read from the disk, and the file page data corresponding to the PTE snapshot will be loaded into the physical memory; of course, if the disk does not have a PTE snapshot corresponding to the application identifier and the boot mode identifier, the process ends.
[0231] At some point after the cold start begins, the kernel will passively receive capture information (this information is used to indicate the capture of PTE snapshots). Usually, the capture information is received later than the pre-read information. The capture information carries the application identifier and the boot mode identifier. The kernel will determine whether the disk has a PTE snapshot corresponding to the application identifier and the boot mode. If the disk does not have a PTE snapshot corresponding to the application identifier and the boot mode identifier, the PTE is traversed, a PTE snapshot is generated based on valid file pages, and the PTE snapshot is associated with the received application identifier and the boot mode identifier and stored on the disk. Of course, if the disk has a PTE snapshot corresponding to the application identifier and the boot mode identifier, the process ends.
[0232] In actual applications, when an application is uninstalled, the PTE snapshot of the application needs to be deleted from the disk.
[0233] Reference Fig.13 , which is a logical flow chart of when an application provided in an embodiment of the present application is uninstalled.
[0234] The kernel receives the deletion information (which is used to indicate the deletion of the PTE snapshot), which carries the application identifier. The kernel determines whether the PTE snapshot corresponding to the application identifier exists on the disk; if the disk exists, the PTE snapshot corresponding to the application identifier is deleted from the disk; if the disk does not exist, the process ends.
[0235] The above embodiment uses the application cold start as an example to describe the interaction between the modules. The following uses the application hot start as an example to describe the interaction between the modules. The differences between the application hot start and the cold start mainly include:
[0236] (1) The timing of pre-reading is different. However, for the kernel, the pre-reading information is passively received.
[0237] (2) The timing of capturing a PTE snapshot may be different. Similarly, for the kernel, the information indicating the captured snapshot is passively received.
[0238] (3) The missing page information for generating the PTE snapshot is different, and accordingly, the memory page data loaded is different. During a cold start, the kernel can generate a PTE snapshot based on all valid file pages of the application, and accordingly, the file page data is loaded; during a hot start, the kernel can generate a PTE snapshot based on some valid file pages and valid anonymous pages of the application. During a hot start, the kernel can also generate a PTE snapshot for the most recently accessed memory pages with reference to pte_young in the PTE status bit, and accordingly, the file page data and anonymous page data are loaded.
[0239] (4) The operations after loading the memory page data corresponding to the PTE snapshot in the physical memory are different. During a cold start, after loading the file page data corresponding to the PTE snapshot in the physical memory, it is necessary to wait for the application process to start and complete the application of virtual memory before continuing to perform the operation of filling the PTE. During a hot start, the application process already exists and has applied for virtual memory. Therefore, the kernel can establish a mapping relationship between the virtual address and the physical address based on the physical address of the file page data loaded in the physical memory, and fill the PTE.
[0240] (5) The storage space of PTE snapshots may be different. The PTE snapshot corresponding to a cold start needs to be stored on disk. The PTE snapshot corresponding to a hot start can be stored on disk or in memory. This is because when the application process is killed, the relevant information of the application stored in memory will be cleared. Therefore, even if the PTE snapshot corresponding to the cold start of the application is stored in memory, the PTE snapshot in memory will have been cleared when the application is cold started next time, and pre-reading cannot be performed.
[0241] In addition, during hot start, if the PTE snapshot is stored in the physical memory, the memory space storing the PTE snapshot, the memory space storing the page table, and the memory space storing other data of the application (eg, code segments, etc.) are different storage spaces in the physical memory.
[0242] Of course, if the PTE snapshot is set to be stored in physical memory in advance, the PTE snapshot of the application when hot starting is read from physical memory during pre-reading. If the PTE snapshot is set to be stored in disk in advance, the PTE snapshot of the application when hot starting is read from disk during pre-reading.
[0243] The interaction diagram between modules during hot start of application can be referred to Fig.10 and Fig.11 The diagram shows the interaction between modules when the application is cold started.
[0244] Reference Fig.14 , which is a processing logic diagram of the kernel when the application is hot-started according to the implementation of this application.
[0245] At a certain moment after the start of the hot start, the kernel will passively receive pre-read information (this information is used to indicate pre-reading), which carries the application identifier and the boot mode identifier. The kernel will determine whether the physical memory (or disk) has a PTE snapshot corresponding to the application identifier and the boot mode identifier; if the physical memory (or disk) has a PTE snapshot corresponding to the application identifier and the boot mode identifier, the memory page data (including file page data and anonymous page data) corresponding to the PTE snapshot is loaded into the physical memory, and the PTE information is filled in the page table according to the physical address where the memory page data is located (for example, the mapping relationship between the virtual address and the physical address is filled); of course, if the physical memory (or disk) does not have a PTE snapshot corresponding to the application identifier and the boot mode identifier, the process ends.
[0246] At some point after the start of the hot start, the kernel will passively receive capture information (this information is used to indicate the capture of PTE snapshots). Usually, the capture information is received later than the pre-read information. The capture information carries the application identifier and the boot mode identifier. The kernel will determine whether the physical memory (or disk) has a PTE snapshot corresponding to the application identifier and the boot mode identifier. If the physical memory (or disk) does not have a PTE snapshot corresponding to the application identifier and the boot mode identifier, the PTE is traversed, and a PTE snapshot is generated based on valid file pages of some types and valid anonymous pages. The PTE snapshot is associated with the received application identifier and the boot mode identifier and stored in the physical memory (or disk). Of course, if the physical memory (or disk) has a PTE snapshot corresponding to the application identifier and the boot mode identifier, the process ends.
[0247] In actual applications, when an application is uninstalled, the PTE snapshot of the application also needs to be deleted from the physical memory (or disk).
[0248] As another embodiment of the present application, the startup mode of the application may also include a warm startup. Warm startup and hot startup may be combined into one startup mode and adopt the same startup process. The application startup method provided in the embodiment of the present application may be applied to multiple startup modes of the application. The embodiment of the present application starts three times on multiple applications in three startup modes respectively, and the overall latency is reduced by 13%, and the IO wait of the UI thread (i.e., how much time in a sampling period is the CPU idle and there is at least one unfinished disk IO request) is reduced by 47%. For details, please refer to Table 1 and Fig.15 shown.
[0249] Table 1 Comparison of latency, IO wait, and benefits between the native process and the application startup method provided by this application (unit: ms)
[0250]
[0251]
[0252] It can be understood from Table 1 that the method of preloading memory pages during application startup provided by the embodiment of the present application can improve the total latency by more than 10%; and can improve the IO wait by even more.
[0253] To more clearly compare the latency of the two application startup methods, you can refer to Fig.15 The total latency and IO wait of the application startup method provided by this application are significantly lower than those of the native process.
[0254] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0255] The embodiment of the present application further provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented.
[0256] The embodiment of the present application also provides a computer program product. When the computer program product is executed on a first device, the electronic device can implement the steps in the above-mentioned method embodiments.
[0257] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may at least include: any entity or device capable of carrying the computer program code to the first device, a recording medium, a computer memory, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), an electric carrier signal, a telecommunication signal, and a software distribution medium. For example, a USB flash drive, a mobile hard disk, a magnetic disk or an optical disk. In some jurisdictions, according to legislation and patent practice, computer-readable media cannot be electric carrier signals and telecommunication signals.
[0258] The embodiment of the present application also provides a chip system, the chip system includes a processor, the processor is coupled to a memory, and the processor executes a computer program stored in the memory to implement the steps of any method embodiment of the present application. The chip system can be a single chip or a chip module composed of multiple chips.
[0259] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0260] Those of ordinary skill in the art will appreciate that the units and method steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0261] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.
Claims
1. A method for preloading a memory page, characterized in that: The method is applied to a kernel of an electronic device, and comprises: Receiving pre-read information, the pre-read information including an identifier of the first application and a hot start identifier; In response to the pre-read information, reading a page table entry snapshot corresponding to the identifier of the first application from a first storage space; Loading the file page data corresponding to the page table entry snapshot in the second storage space and the anonymous page data corresponding to the third storage space into the physical memory; A mapping relationship between the virtual memory of the file page data and the physical memory of the file page data and a mapping relationship between the virtual memory of the anonymous page data and the physical memory of the anonymous page data are written into the page table entry of the first application.
2. The method according to claim 1, characterized in that The method further includes: generating the page table entry snapshot based on the page table entry accessed during the first application history hot start process, wherein the page objects in the page table entry are file pages and anonymous pages.
3. The method according to claim 2, characterized in that The first storage space is a physical memory or a disk, the second storage space is a disk, and the third storage space is a swap partition.
4. The method according to claim 3, characterized in that Before receiving the pre-read information, the method further includes: receiving capture information, the capture information including an identifier of the first application and a hot start identifier; In response to the captured information, generating a snapshot of a page table entry corresponding to the identifier of the first application according to the page table entry corresponding to the identifier of the first application, wherein the page objects in the page table entry are file pages and anonymous pages; The page table entry snapshot corresponding to the identifier of the first application is stored in the first storage space.
5. The method according to claim 4, characterized in that The step of generating, in response to the captured information and according to the page table entry corresponding to the identifier of the first application, a snapshot of the page table entry corresponding to the identifier of the first application includes: In response to the captured information, determining that a page table entry snapshot corresponding to the identifier of the first application does not exist in the first storage space; A page table entry snapshot corresponding to the identifier of the first application is generated according to the page table entry corresponding to the identifier of the first application, wherein the page objects in the page table entry are file pages and anonymous pages.
6. The method according to claim 4 or 5, characterized in that The generating, according to the page table entry corresponding to the identifier of the first application, a snapshot of the page table entry corresponding to the identifier of the first application includes: The status bit in the page table corresponding to the identifier of the first application is set to a preset character, and the page object is a page table item of a preset type, and a snapshot of the page table item corresponding to the identifier of the first application is generated. The preset character is used to indicate that the virtual memory corresponding to the page table item has been mapped to the physical memory and has been accessed, and the preset types include active, referenced, and anon.
7. The method according to any one of claims 1 to 5, characterized in that: The method further comprises: receiving deletion information, wherein the deletion information includes an identifier of the first application; In response to the deletion information, the page table entry snapshot corresponding to the identifier of the first application stored in the first storage space is deleted.
8. The method according to any one of claims 1 to 3, characterized in that The page table entry includes a mapping relationship from virtual memory to physical memory, and the page table entry snapshot includes a file name of the file page data corresponding to the page table entry and a storage address of the file page data in the disk, as well as a storage address of the anonymous page data corresponding to the page table entry in the swap partition.
9. An electronic device, characterized in that: The electronic device comprises a processor, and the processor is used to run a computer program stored in a memory, so that the electronic device implements the method according to any one of claims 1 to 8.
10. A chip system, characterized in that: The method comprises a processor coupled to a memory, wherein the processor executes a computer program stored in the memory to implement the method according to any one of claims 1 to 8.
