A log storage method and electronic device

By storing the most recently written log block in the circular buffer into non-volatile memory before a fault occurs, the problem of invalid logs after an electronic device restart is solved, enabling more accurate fault analysis and location.

CN122308710APending Publication Date: 2026-06-30HONOR DEVICE CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
HONOR DEVICE CO LTD
Filing Date
2024-12-31
Publication Date
2026-06-30

Smart Images

  • Figure CN122308710A_ABST
    Figure CN122308710A_ABST
Patent Text Reader

Abstract

This application relates to the field of terminal technology, and more particularly to a log storage method and an electronic device. The method enables the storage of the top N log blocks (those whose write times are closest to the time of the fault occurrence) from a plurality of log blocks stored in a circular buffer of a random access memory before the electronic device restarts due to a fault, in a non-volatile memory, where N is an integer greater than 1. The method includes: in response to a fault in the electronic device, obtaining a first offset for each of the plurality of log blocks stored in the circular buffer; determining a target log block from the plurality of log blocks based on the first offset; the first offset indicating the starting storage location of the log block when the plurality of log blocks are written to memory sequentially according to their write times.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of terminal technology, and in particular to a log storage method and an electronic device. Background Technology

[0002] During operation, electronic devices sometimes experience system-wide malfunctions such as crashes, black screens, or system failures. After such malfunctions, the electronic device can restart its operating system to restore normal operation. Before restarting due to a malfunction, the electronic device stores its operational logs from random access memory (RAM) to non-volatile memory (NDM) to prevent log loss after the restart. After restarting, the electronic device can upload the operational logs from NDM to a server, facilitating fault analysis and localization based on these logs.

[0003] In conventional technologies, there is a problem that electronic devices may obtain operational logs that are useless or irrelevant to fault location and analysis before restarting. Summary of the Invention

[0004] This application provides a log storage method and an electronic device, which can store the top N log blocks whose write time is closest to the time of the fault occurrence from multiple log blocks stored in the circular buffer of the random access memory in a non-volatile memory before the electronic device restarts due to a fault. Here, N is an integer greater than 1.

[0005] To achieve the above objectives, the embodiments of this application adopt the following technical solutions:

[0006] A first aspect provides a log storage method applied to an electronic device, the electronic device including random access memory (RAM) and non-volatile memory (NVMemory); a circular buffer of the RAM is used to store the operation log of the electronic device. The method includes: in response to a fault in the electronic device, obtaining a first offset of each log block among a plurality of log blocks stored in the circular buffer; determining a target log block from the plurality of log blocks based on the first offset; the first offset indicating the starting storage location of the log block when the plurality of log blocks are sequentially written to memory according to their write times; the target log block being the top N log blocks whose write times are closest to the fault occurrence time; N being an integer greater than 1; the size of the target log block being less than or equal to the size of a preset region in the NVMemory; storing the target log block in the preset region; and restarting the electronic device. The preset region may, for example, be a raw partition in the NVMemory.

[0007] In this application, because the circular buffer has the characteristic of circular writing, the storage location of a log block in the circular buffer cannot reflect the writing order of the log block. Therefore, this application first obtains the first offset of multiple log blocks. The first offset indicates the starting storage location of multiple log blocks when they are written to memory sequentially according to their write times. The first offset reflects the writing order of the log blocks. For example, the smaller the first offset, the earlier the write time; the larger the first offset, the later the write time. Then, the electronic device can determine the target log block from the multiple log blocks based on the first offset of the multiple log blocks. The target log block is the top N log blocks whose write times are closest to the time of the fault occurrence, where N is an integer greater than 1. Furthermore, the size of the target log block is less than or equal to the size of a preset area. Then, the electronic device can store the target log block in the preset area and restart the electronic device. In this way, before the electronic device restarts due to a fault, it can obtain and store log blocks from the circular buffer whose write times are closest to the time of the fault occurrence. Log blocks whose write times are closer to the time of the fault occurrence are more useful for fault analysis and location. In this way, electronic devices can obtain log blocks that are useful for fault location and analysis, which helps in fault location and analysis and product development.

[0008] In one possible implementation of the first aspect, the electronic device obtains a first offset of multiple log blocks based on a first log block. Specifically, the electronic device determines the first log block from the multiple log blocks based on indication information, where the indication information indicates the log block most recently written to the circular buffer. That is, the first log block is the log block most recently written to the circular buffer. In other words, the first log block is the log block whose write time is closest to the time of the fault occurrence among the multiple log blocks. The electronic device obtains a first offset of each of the multiple log blocks based on a second offset of the first log block. The second offset of the log block indicates the starting storage location of the log block in the circular buffer. The second offset is the actual starting storage location of the log block in the circular buffer. The first offset is mapped from the second offset. That is, the multiple log blocks stored in the circular buffer all include corresponding second offsets. The electronic device can map the second offset of each of the multiple log blocks to a first offset based on the second offset of the first log block. These multiple log blocks include the first log block.

[0009] In one possible implementation of the first aspect, the indication information may be at least one of a log block identifier, a second offset of the log block, and a fourth offset of the log block. The log block identifier is used to uniquely identify a log block. The second offset of the log block is used to indicate the starting storage location of the log block in the circular buffer. The fourth offset of the log block is used to indicate the ending storage location of the log block in the circular buffer. Multiple log blocks stored in the circular buffer each include a corresponding fourth offset.

[0010] In one possible implementation of the first aspect, for each of the multiple log blocks: if the second offset of the log block is greater than the second offset of the first log block, the second offset of the log block is determined as the first offset of the log block; if the second offset of the log block is less than or equal to the second offset of the first log block, the first offset of the log block is determined based on the second offset of the log block and the size of the circular buffer, for example, the sum of the second offset of the log block and the size of the circular buffer is determined as the first offset of the log block. Due to the cyclic writing characteristic of the circular buffer, after mapping in this way, the first offset can indicate the starting storage location of the log block when multiple log blocks are sequentially written to memory according to the write time.

[0011] In one possible implementation of the first aspect, the electronic device can determine a target log block from multiple log blocks according to a first offset, based on the following method: obtaining a target offset; the target offset is an offset of a preset area size from a third offset of the first log block in a first direction, when multiple log blocks are sequentially written to memory according to write times; the first direction is opposite to the direction of sequential writing; the third offset is used to indicate the end storage position of the first log block when multiple log blocks are sequentially written to memory according to write times; determining the target log block from the multiple log blocks based on the first offset and the target offset. When multiple log blocks are sequentially written to memory according to write times, the first log block has the latest write time and the first offset of the first log block is the largest. The target offset can be obtained by offsetting the preset area size from the mapped end storage position of the first log block, i.e., the third offset, in the first direction. The target log block is included between the target offset and the third offset of the first log block. The first direction is opposite to the direction of sequential writing.

[0012] In one possible implementation of the first aspect, a third offset is determined based on a fourth offset of the first log block and the size of the circular buffer. For example, the third offset is determined by the sum of the fourth offset of the first log block and the size of the circular buffer. The fourth offset of the first log block is used to indicate the end storage location of the first log block within the circular buffer. A target offset is determined based on the third offset and the size of a preset region. For example, the target offset is determined by the difference between the third offset and the size of the preset region.

[0013] In one possible implementation of the first aspect, the log block whose first offset is greater than or equal to the target offset among multiple log blocks is determined as the target log block.

[0014] In a second aspect, an electronic device is provided, comprising: a memory, a camera, and one or more processors; the camera, the memory, and the processors are coupled; wherein the memory is used to store computer program code, the computer program code including computer instructions; when the computer instructions are executed by the processor, the electronic device performs the method as described in any of the first aspects.

[0015] Thirdly, a chip system is provided that can be applied to an electronic device including memory. The chip system includes one or more interface circuits and one or more processors. The interface circuits and processors are interconnected via lines. The interface circuits are used to receive signals from the aforementioned memory and send the signals to the processors, the signals including computer instructions stored in the memory. When the processor executes the computer instructions, the electronic device performs a method as described in the first aspect and any of its possible design embodiments.

[0016] Fourthly, a computer-readable storage medium is provided, including computer instructions that, when executed on an electronic device, cause the electronic device to perform the method as described in any of the first aspects.

[0017] Fifthly, a computer program product comprising a computer program / instructions that, when executed by a processor, implement the steps of any of the methods in the first aspect.

[0018] In understanding, the beneficial effects that can be achieved by the electronic device of any possible design of the second aspect, the chip system of the third aspect, the computer-readable storage medium of the fourth aspect, and the computer program product of the fifth aspect can be referred to as the beneficial effects of the first aspect and any possible design, which will not be repeated here. Attached Figure Description

[0019] Figure 1 A schematic diagram illustrating the data writing and reading order of a circular buffer provided in an embodiment of this application;

[0020] Figure 2 This is a schematic diagram of a data structure in a circular buffer provided in an embodiment of this application;

[0021] Figure 3 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application;

[0022] Figure 4 A schematic diagram of the hardware and software architecture of an electronic device provided in an embodiment of this application;

[0023] Figure 5 A schematic flowchart illustrating a storage method provided in an embodiment of this application;

[0024] Figure 6 This is a schematic diagram of another data structure in a circular buffer provided in an embodiment of this application;

[0025] Figure 7 A schematic diagram illustrating the location mapping of log blocks provided in an embodiment of this application;

[0026] Figure 8 This is a schematic diagram showing the position of a target offset in a circular buffer, as provided in an embodiment of this application. Detailed Implementation

[0027] Electronic devices may malfunction due to hardware or software issues during operation. When a malfunction occurs, the device can upload its operational logs to a cloud server. Developers can then use these logs to pinpoint the problem. Once the problem is identified, developers can trigger the cloud server to push updated data or alert messages to the device. These operational logs also help developers avoid similar issues during product development.

[0028] Runtime logs can include, for example, kernel logs. Kernel logs record real-time information about the electronic device's operation, such as startup information, driver loading information, and device status information. The electronic device can write the runtime log to random access memory (RAM) by calling functions like `printk()`, specifically to a ring buffer within RAM. A ring buffer is a storage area in RAM that operates on a first-in, first-out (FIFO) principle. For example, a ring buffer includes write pointers and read pointers. The write pointer points to the current location where data can be written, and the read pointer points to the current location where data can be read. Data is written sequentially from the beginning to the end of the ring buffer. When the write pointer reaches the end of the ring buffer, it wraps back to the beginning. Similarly, data is read sequentially from the beginning to the end of the ring buffer. When the read pointer reaches the end of the ring buffer, it also wraps back to the beginning. This allows for cyclical writing and reading of data.

[0029] The electronic device writes the operation log to a circular buffer in units of blocks. The block size is not fixed; for example, a log block can be 10 bytes, 64 bytes, 128 bytes, 512 bytes, etc. It should be understood that the size of the log block written to the circular buffer each time the electronic device writes is related to the amount of operation log data to be written. For example, if the amount of operation log data to be written is large, the log block written to the circular buffer will be large; if the amount of operation log data to be written is small, the log block written to the circular buffer will be small.

[0030] First, combine Figure 1 This section describes the process of writing and reading data from a circular buffer. For example, Figure 1 A schematic diagram of a circular buffer is shown. (For example...) Figure 1 As shown in (a), during the first write, the read pointer and write pointer point to the same location, such as the beginning of the circular buffer. The beginning of the circular buffer can be, for example, an offset of 0. Figure 1 As shown in (b), after writing log block 1, the write pointer moves backward and points to the end of log block 1, indicating that the next write can begin from the end of log block 1. Here, the direction from the start position of the circular buffer to the end position is "back," and the direction from the end position of the circular buffer to the start position can be "forward." Since no data is read, the position of the read pointer remains unchanged. Figure 1As shown in (c), after writing log block 2, the write pointer moves forward and points to the end of log block 2, indicating that the next write can begin from the end of log block 2. Since no data is read, the read pointer remains unchanged. Figure 1 As shown in (d), after writing log block 3, since the write pointer has reached the end of the circular buffer, it wraps back to the beginning of the circular buffer and points to that beginning. At this point, the circular buffer is full of data, and the electronic device can begin overwriting from the beginning of the circular buffer. Overwriting can be understood as overwriting existing data while writing data to a region. Figure 1 As shown in (e), the electronic device overwrites the location of log block 1 with log block 4. The write pointer moves forward to the end of log block 4, indicating that the next write can begin from the end of log block 4. Simultaneously, the read pointer moves forward to the beginning of log block 2. This is done to ensure that the electronic device always reads the oldest data (i.e., the earliest written data), adhering to the first-in, first-out (FIFO) principle. Figure 1 This illustrates the process of writing data to a circular buffer; the process of reading data from the circular buffer is similar to the process of writing data. For example, Figure 1 As shown in (f), after the electronic device reads log block 2, the read pointer moves backward and points to the beginning of log block 3, indicating that the next reading can start from the beginning of log block 3.

[0031] When an electronic device malfunctions, it can recover by restarting its operating system. Before restarting, the electronic device can read its runtime logs from a circular buffer and store them in non-volatile memory (NVM). This prevents the loss of runtime logs after a restart. After restarting, the electronic device can upload the runtime logs from the NVM to a cloud-based server, allowing developers to analyze and locate the fault based on these logs.

[0032] For example, electronic devices can store runtime logs in a pre-defined area within NVM, such as a raw partition. A raw partition is an area pre-allocated from NVM by developers before the electronic device leaves the factory. Raw partitions are typically used to store the electronic device's configuration information. Furthermore, after the electronic device leaves the factory, it cannot reclaim or modify the data in the raw partition, and the data in the raw partition cannot be directly read by the file system. NVM may, for example, include universal flash storage (UFS). The raw partition may, for example, reside within UFS. The following describes a feasible method for uploading runtime logs to an electronic device.

[0033] When an electronic device is running, it writes its runtime log to a circular buffer using the `printk()` function. If the electronic device malfunctions, such as a Panic-level failure (a kernel error that prevents the operating system from continuing to function), the operating system stops working, and the device ceases writing the runtime log. The runtime log in the circular buffer remains as it was at the time of the failure. Since the device needs to reboot to resume normal operation, the runtime log in the RAM-based circular buffer is lost after reboot. Therefore, before rebooting after a failure, the electronic device reads the runtime log from the RAM-based circular buffer. The device then stores the acquired runtime log in a raw partition of the NVM. After rebooting, the electronic device retrieves the runtime log from the NVM raw partition and uploads it to a server located in the cloud.

[0034] The size of the circular buffer is typically larger than the size of the raw partition. For example, the raw partition is typically 1MB, while the circular buffer is usually larger, such as 4MB or 3MB. When the circular buffer is larger than the raw partition, before the electronic device restarts after a failure, it typically only reads the logs located at the beginning of the circular buffer, which are the same size as the raw partition (e.g., 1MB), and stores the acquired logs on the raw partition. The beginning of the circular buffer is the area near the start of the circular buffer. Because only the logs at the beginning of the circular buffer, which are the same size as the raw partition, are retrieved, the electronic device may not be able to retrieve the runtime logs written to the circular buffer by the electronic device within the period most recently since the failure. The following discussion uses a raw partition size of 1MB as an example, combined with... Figure 2 Let me introduce it.

[0035] like Figure 2As shown in (a), assuming that after a failure, the most recently written log block in the circular buffer is log block 5. Log blocks 1 through 5 were all written sequentially to the circular buffer by the electronic device before the failure, with log block 5 being the closest to the failure occurrence, followed by log block 4. If the electronic device only reads the 1MB log at the beginning of the circular buffer, it cannot access the log data from the period closest to the failure occurrence. For example, if the electronic device only reads the 1MB log at the beginning of the circular buffer, such as log blocks 1 and 2, their write times are much earlier than the failure occurrence, making them largely useless for fault analysis and localization. Furthermore, assuming that after the failure, the most recently written log block in the circular buffer is log block 3 or 4, conventional techniques also fail to provide access to the log data from the period closest to the failure occurrence.

[0036] like Figure 2 As shown in (b), assuming that after a failure, the most recently written log block in the circular buffer is log block 6. Log blocks 2-5 and log block 6 were all written to the circular buffer by the electronic device before the failure. Among them, log block 6 was written closest to the failure time, followed by log block 5. If the electronic device only reads the 1MB log block at the head of the circular buffer, it can only read log block 6 and log block 2. Log block 2 is the earliest log block written to the circular buffer among the multiple log blocks stored therein. Since the write time of log block 2 is much earlier than the failure time, log block 2 is not very useful for failure analysis and location. In this embodiment, although the electronic device can obtain the log block closest to the failure time, such as log block 6, it does not obtain log blocks closer to the failure time, such as log block 5; instead, it obtains the log block furthest from the failure time, such as log block 2.

[0037] In other words, the electronic device may not be able to retrieve log blocks written to the circular buffer within the period most recently before the failure occurred. There are even cases where the electronic device cannot retrieve log blocks written to the circular buffer within the period most recently before the failure occurred. This means that the logs retrieved by the electronic device are useless or largely irrelevant for fault analysis and localization.

[0038] Therefore, this application provides a log storage method and an electronic device. The method is applied to an electronic device including RAM and an NVM. When the electronic device malfunctions, it can retrieve a target log block from a circular buffer in RAM and store it in a preset area of ​​the NVM. This preset area can be a raw partition as described above. The target log block includes the top N log blocks stored in the circular buffer whose write times are closest to the time of the malfunction. Here, N is an integer greater than or equal to 1. Thus, before the electronic device restarts due to a malfunction, it can retrieve and store log blocks closest to the time of the malfunction from the circular buffer. This allows for the acquisition of log blocks useful for fault location and analysis, aiding in fault localization and analysis, and contributing to product development.

[0039] The method provided in this application can be applied to electronic devices including RAM and NVM. These electronic devices may include mobile phones, tablets, laptops, netbooks, personal digital assistants (PDAs), wearable devices (e.g., smartwatches, smart bracelets), in-vehicle devices, virtual reality devices, etc., and this application does not impose any limitations on them. In this application, the aforementioned electronic device is an electronic device capable of running an operating system and installing applications. Optionally, the operating system running on the electronic device may be... system, system, Systems, etc.

[0040] Take a mobile phone as an example. Figure 3 As shown, the electronic device 300 may include: a processor 310, a memory 320, a universal serial bus (USB) interface 330, a power management module 340, antennas such as antenna 1 and antenna 2, a communication module 350, a display screen 360, an audio module 370, a camera 380, a sensor module 390, etc.

[0041] Processor 310 may include one or more processing units, such as application processors (APs), central processing units (CPUs), modem processors, graphics processing units (GPUs), ISPs, controllers, memory, video codecs, digital signal processors (DSPs), baseband processors, and / or neural network processing units (NPUs). Different processing units may be independent devices or integrated into one or more processors. The controller may serve as the central nervous system and command center of the electronic device 300. The controller can generate operation control signals based on instruction opcodes and timing signals to control instruction fetching and execution.

[0042] The memory 320 can be used to store computer executable program code, which includes instructions. The processor 310 executes various functional applications and data processing of the electronic device by running the instructions stored in the memory 320. The memory 320 may include a program storage area and a data storage area. The program storage area may store the operating system and at least one application program required for a function (such as sound playback, interface display, etc.). The data storage area may store data created during the use of the electronic device (such as notification messages).

[0043] In this embodiment of the application, the processor 310 can execute a log storage method provided in this embodiment by running instructions stored in the memory 320.

[0044] Specifically, the memory 320 may include RAM and NVM. RAM is an internal memory that can directly exchange data with the processor 310, also called main memory (or RAM). It can be read and written at any time and is very fast, typically serving as a temporary data storage medium for the operating system or other running programs. In this embodiment, the RAM includes a circular buffer for storing the operating logs of the electronic device 300. NVM is a type of memory that can retain stored data even without a power supply. In this embodiment, the NVM includes a preset area.

[0045] In this embodiment, the processor 310 can obtain the first offset of multiple log blocks stored in a circular buffer in RAM before the electronic device restarts due to a fault. The first offset indicates the starting storage location of a log block when multiple log blocks are sequentially written to memory according to their write times. Sequential writing refers to data being written continuously in memory according to address order. For example, data is written continuously from low address to high address. That is, in this application, although multiple log blocks are cyclically written to the circular buffer, the processor 310 can obtain the first offset corresponding to these multiple log blocks if they were written to memory in a sequential manner. The first offset corresponding to these multiple log blocks has the following characteristics: the write time corresponds to the write order. For example, the first offset of the earliest written log block is small, and the first offset of the latest written log block is large. The processor 310 can also read a target log block from the circular buffer in RAM based on the first offset of the multiple log blocks and store the target log block in a preset area of ​​the NVM. The circular buffer stores multiple log blocks. The target log block is the N log blocks whose writing time to the circular buffer is closest to the time of the failure; where N is an integer greater than or equal to 1. The size of the target log block is less than or equal to the size of the preset area.

[0046] The power management module 340 is used to connect the battery to the processor 310. The power management module 340 receives battery and / or power input to power the processor 310, memory 320, communication module 350, display screen 360, and camera 380, etc. The power management module 340 can also be used to monitor parameters such as battery capacity, battery cycle count, and battery health status (leakage current, impedance). In some other embodiments, the power management module 340 may also be located within the processor 310.

[0047] The communication module 350 can provide solutions for wireless communication applications on the electronic device 300, 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), and infrared (IR). The communication module 350 can be one or more devices integrating at least one communication processing module. The communication module 350 receives electromagnetic waves via an antenna, performs frequency modulation and filtering of the electromagnetic wave signal, and sends the processed signal to the processor 310. The communication module 350 can also receive signals to be transmitted from the processor 310, perform frequency modulation and amplification, and convert them into electromagnetic waves for radiation via the antenna.

[0048] In some embodiments, the antenna of the electronic device 300 is coupled to the communication module 350, enabling the electronic device 300 to communicate with networks and other devices via wireless communication technologies. The wireless communication technologies may include Global System for Mobile Communications (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Time Division Code Division Multiple Access (TD-SCDMA), Long Term Evolution (LTE), BT, Global Navigation Satellite System (GNSS), WLAN, NFC, FM, and / or IR technologies. The GNSS may include Global Positioning System (GPS), BeiDou Navigation Satellite System (BDS), GLONASS, and / or Galileo.

[0049] Electronic device 300 implements display functions through a GPU, display screen 360, and application processor. The GPU is a microprocessor for image processing, connecting the display screen 360 and the application processor. The GPU is used to perform mathematical and geometric calculations and for graphics rendering. Processor 310 may include one or more GPUs, which execute program instructions to generate or modify display information.

[0050] The display screen 360 is used to display images, videos, etc. The display screen 360 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 (AMOLED), a flexible light-emitting diode (FLED), a Mini-LED, a Micro-OLED, a quantum dot light-emitting diode (QLED), etc.

[0051] Electronic device 300 can achieve shooting and recording functions through ISP, camera 380, video codec, GPU, display 360 and application processor.

[0052] The audio module 370 is used to convert digital audio information into analog audio signal output, and also to convert analog audio input into digital audio signal. The audio module 370 can also be used for encoding and decoding audio signals. In some embodiments, the audio module 370 may be located in the processor 310, or some functional modules of the audio module 370 may be located in the processor 310.

[0053] The camera 380 is used to capture still images or videos. An object passes through the lens, generating an optical image that is projected onto a photosensitive element. This photosensitive element can be a charge-coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, which is then passed to an ISP (Image Signal Processor) for conversion into a digital image signal. The ISP outputs the digital image signal to a DSP (Digital Signal Processor) for further processing. The DSP converts the digital image signal into standard image signals in formats such as RGB and YUV.

[0054] The sensor module 390 may include pressure sensors, gyroscope sensors, barometric pressure sensors, magnetic sensors, accelerometers, distance sensors, proximity sensors, fingerprint sensors, temperature sensors, touch sensors, ambient light sensors, and bone conduction sensors, etc.

[0055] It is understood that the structure illustrated in this embodiment does not constitute a specific limitation on the electronic device 300. In other embodiments, the electronic device 300 may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.

[0056] Taking the aforementioned electronic device 300 as an example, which is a mobile phone, the software system of the electronic device 300 can adopt a layered architecture, event-driven architecture, microkernel architecture, microservice architecture, or cloud architecture. This application embodiment uses a layered architecture... Taking the system as an example, the software structure of electronic device 300 is illustrated.

[0057] Figure 4 This is a software structure block diagram of the electronic device 300 according to an embodiment of this application.

[0058] A layered architecture divides software into several layers, each with a clear role and function. Layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into five layers, from top to bottom: the application layer, the application framework layer, the system library layer, the native service layer, and the kernel layer.

[0059] The application layer can include a series of application packages.

[0060] like Figure 4 As shown, the application package can include a primary application, a gallery, a calling app, a map app, a navigation app, WLAN, Bluetooth, music, video, SMS, social media, and other applications. The primary application can interact with the cloud-side server. For example, the primary application can send operational logs to the server. The primary application can also receive update data or reminder messages pushed by the server.

[0061] The application framework layer provides application programming interfaces (APIs) and a programming framework for applications in the application layer. The application framework layer includes some predefined functions.

[0062] like Figure 4 As shown, the application framework layer may include a view system, resource manager, notification manager, input system, etc.

[0063] The system library layer can include multiple functional modules. For example: surface manager, media libraries, 3D graphics processing libraries (e.g., OpenGL ES), 2D graphics engines (e.g., SGL), etc.

[0064] The Native layer is located below the system runtime library layer and above the kernel layer. It mainly contains local services and some linked libraries, and can be implemented using C and C++. The Native layer provides upper-layer applications with interfaces and services for interacting with the underlying hardware. It also provides direct control and management of hardware devices, enabling applications to fully utilize hardware resources. In this embodiment, the Native layer may include an upload module used to read log blocks from a preset partition of the NVM and send them to the first application in the upper layer.

[0065] The kernel layer is the core of the operating system. It is responsible for managing system hardware resources, providing basic services (such as process scheduling, memory management, file systems, and network communication), and ensuring system security and stability. The kernel layer also includes drivers for hardware devices, such as camera drivers, sensor drivers, and touchscreen drivers.

[0066] In this embodiment, the kernel layer includes a writing module and a fault handling module. The writing module writes the operation log of the electronic device 300 into a circular buffer located in RAM. The fault handling module reads the operation log from the circular buffer and stores it in a preset area of ​​the NVM when a fault is detected in the electronic device.

[0067] In this embodiment, during the operation of the electronic device 300, the kernel-layer write module writes the runtime log into a circular buffer located in RAM via a write function. When the fault handling module in the kernel layer detects a fault in the electronic device 300, it executes a storage method provided in this embodiment to read the target log block from the circular buffer. Then, the fault handling module stores the read target log block in a preset area located in NVM. This preset area is the raw partition mentioned earlier. Afterwards, the electronic device 300 restarts. After restarting, the upload module located in the local service layer can read the target log block from the preset area in NVM and send it to a first application. The first application can then upload the target log block to a server located in the cloud. This allows developers to remotely locate and analyze fault problems. Optionally, the fault handling module can parse the read target log block and store the parsed target log block in the preset area of ​​NVM. After the electronic device restarts, the upload module can send the parsed target log block to the first application, which can then upload it to a server located in the cloud.

[0068] The following uses a mobile phone as an example to illustrate a log storage method and an electronic device provided in this application, with reference to the accompanying drawings.

[0069] In response to a phone malfunction, the phone obtains the first offset of each log block in the circular buffer, such as offset 1. The first offset indicates the starting storage location of the log block if it were written to memory sequentially according to its write time. A smaller first offset indicates an earlier write time, and a larger first offset indicates a later write time. The write time is the moment the log block is written to the circular buffer. The first offset is mapped from a second offset of the log block. The second offset indicates the actual starting storage location of the log block in the circular buffer. For example, offset 2 below. In other words, the first offset indicates the starting storage location of the log blocks in memory if they were written sequentially rather than cyclically. That is, the first offset is a mapped virtual storage location. After obtaining the first offset, the phone can determine the target log block from the multiple log blocks. The target log block is the N log blocks whose write time is closest to the malfunction time; N is an integer greater than 1. The size of the target log block is less than or equal to the size of a preset region in non-volatile memory. Finally, the phone stores the target log block in the preset region and restarts the phone.

[0070] The following is combined Figure 5 This plan will now be introduced. Figure 5 This is a flowchart illustrating a log storage method provided in an embodiment of this application. Figure 5 The methods shown include:

[0071] S1, in response to a mobile phone malfunction, determines the first log block from multiple log blocks stored in the circular buffer.

[0072] In response to a mobile phone malfunction, the phone can determine the first log block from multiple log blocks stored in a circular buffer based on an indication message. The indication message specifies the most recently written log block to the circular buffer. Specifically, the first log block is the most recently written log block in the circular buffer; that is, it is the log block whose write time is closest to the time of the malfunction among the multiple log blocks stored in the circular buffer.

[0073] Optionally, the indication information may be located at a preset position in the metadata of the circular buffer. For example, S1 may include: S11 and S12 (not shown in the figure).

[0074] S11, in response to a phone malfunction, the phone retrieves metadata from the circular buffer.

[0075] The metadata of the circular buffer records information about log blocks. This information includes the log block's identifier and its initial storage location.

[0076] A log block identifier is used to uniquely identify a log block.

[0077] The first storage location of a log block indicates its location within the circular buffer. This first storage location can be represented by a second offset (e.g., offset 2) and a fourth offset (e.g., offset 4). Offset 2 indicates the start position of the log block within the circular buffer. Offset 4 indicates the end position of the log block within the circular buffer. The data between the start and end positions of the log block constitutes the data included in that log block. For example, offset 2 is the offset from the start position of the circular buffer to the start position of the log block. Offset 4 is the offset from the start position of the circular buffer to the end position of the log block. The units for offsets 2 and 4 can be bytes. Offset 4 is greater than offset 2. Each log block in the circular buffer includes corresponding offsets 2 and 4.

[0078] For example, the metadata includes a block list, which contains information about the log blocks stored in the circular buffer. Taking a circular buffer storing five log blocks as an example, the block list could be as shown in Table 1.

[0079] Table 1

[0080]

[0081]

[0082] As shown in Table 1, log blocks 3 through 7 are stored consecutively in the first storage location within the circular buffer. In another example, the first storage locations of multiple log blocks in the circular buffer can also be non-contiguous. For example, log block 3 starts at position 00 and ends at position 10; log block 4 starts at position 15 and ends at position 25; log block 5 starts at position 30 and ends at position 40; log block 6 starts at position 45 and ends at position 55; and log block 7 starts at position 60 and ends at position 70. That is, the first storage locations of multiple log blocks in the circular buffer can be consecutive or non-contiguous. This application does not impose specific limitations on this.

[0083] The indication information may be at least one of the following: the identifier of the log block, the second offset of the log block, and the fourth offset of the log block.

[0084] In some embodiments, the log block list further includes preset identifiers, which are used to indicate whether a log block is invalid or valid. A valid log block is one that has not been read, while an invalid log block is one that has been read or overwritten. The preset identifiers can be, for example, identifier 1 and identifier 2, where identifier 1 indicates that the log block is invalid, and identifier 2 indicates that the log block is valid. In response to a mobile phone malfunction, the phone can first determine, based on the preset identifiers, multiple log blocks stored in the circular buffer, all of which are valid log blocks. Then, the phone determines the first log block from the multiple log blocks stored in the circular buffer.

[0085] Optionally, in response to a phone malfunction, the phone retrieves the current metadata of the circular buffer. For example, the metadata of the circular buffer may be stored in a preset location in RAM. The phone can retrieve the current metadata of the circular buffer from the preset location in RAM. Optionally, the phone may include a fault handling module for retrieving the metadata of the circular buffer at the time of the malfunction when a phone malfunction is detected. This fault handling module may be a fault handling module located in the kernel layer.

[0086] Optionally, the metadata may also include information such as the parameters of the circular buffer, the position of the read pointer, and the position of the write pointer. When writing a log block, the phone can start writing from the position pointed to by the write pointer in the metadata of the circular buffer. When reading a log block, the phone can start reading from the position pointed to by the read pointer in the metadata of the circular buffer. The metadata of the circular buffer is dynamically updated. For example, when writing or reading a log block, the positions of the write pointer and read pointer in the metadata, as well as the information of the log block stored in the circular buffer, are updated synchronously, and the preset identifier corresponding to the log block is also updated. The parameters of the circular buffer may include the base address of the circular buffer and the size of the circular buffer. The base address of the circular buffer may, for example, be the starting address of the circular buffer in RAM.

[0087] S12, the phone identifies the first log block based on the instructions.

[0088] The first log block is the most recent log block written to the circular buffer by the phone. In other words, the first log block is the log block whose write time is closest to the time of the failure among the multiple log blocks currently stored in the circular buffer.

[0089] After receiving the instruction, the phone can compare it with the information of log blocks in the log block list and select the log block with matching information as the first log block. It should be understood that the metadata of the circular buffer is dynamically updated, and this instruction is also dynamically updated. For example, this instruction is updated synchronously when a log block is written.

[0090] After identifying the first log block, the phone can map the first storage location of multiple log blocks stored in the circular buffer to the second storage location based on the first storage location of the first log block.

[0091] For example, a mobile phone can determine the log block identified as Head_id as the most recently written valid log block, i.e., the first log block, based on the indication information in the metadata. The phone can obtain the starting position of this log block from the log block list in the metadata; for example, it could be Head_id_begin, whose value could be offset 2 as mentioned earlier, and the ending position of the log block could be Head_id_end, whose value could be offset 4 as mentioned earlier. Since the circular buffer is written cyclically, the first valid log block after the first log block is the earliest written log block, such as the log block identified as first_id. Furthermore, all log blocks included from the Head_id_end of the log block identified as Head_id to the first_id_end of the log block identified as first_id are log blocks written to the circular buffer sequentially according to their write time. Due to the circular write characteristic of the circular buffer, the first log block can be located at any position in the circular buffer. Figure 6 As shown in (a), the first log block can be log block b5, which is located in the middle of the circular buffer. Figure 6 As shown in (b) above, the first log block can be log block b3, which is located at the beginning of the circular buffer. Figure 6 As shown in (c), the first log block is log block b7 and is located at the end of the circular buffer.

[0092] The storage order of multiple log blocks in the circular buffer is inconsistent with the writing order of multiple log blocks. For example, ... Figure 6 In (a) of the diagram, the first storage location of log block b5 precedes the first storage location of log block b6; however, log block b5 is written later than log block b6, meaning the write time of log block b5 is after the write time of log block b6. For example... Figure 6In (b) of the diagram, log block b3 is stored at the beginning of the circular buffer, but its write time is the latest. That is, the write time of log block b3 is later than the write times of log blocks b4-b7. It should be understood that in this application, the starting position of the circular buffer is taken as the origin, the direction from the starting position to the ending position of the circular buffer is "back," and the direction from the ending position to the starting position is "forward." The closer the storage location of a log block is to the starting position of the circular buffer, the earlier its storage location; the farther the storage location of a log block is from the starting position of the circular buffer, the later its storage location. Alternatively, the direction from smallest to largest offset is "back," and the direction from largest to smallest offset is "forward."

[0093] Therefore, it is necessary to perform location mapping on the first storage locations of multiple log blocks. After location mapping, the mapped storage location (second storage location) can reflect the write order of the log blocks. For example, the earlier the second storage location of a log block is, the earlier the log block was written; the later the second storage location of a log block is, the later the log block was written. Then, after location mapping, the mobile phone can read log blocks whose size is less than or equal to the value A from the end position after mapping the first log block. This log block whose size is less than or equal to the value A is the target log block. The value A is the size of the raw partition.

[0094] For example, the mobile phone can perform S2 to perform position mapping on multiple log blocks stored in the current circular buffer, wherein the position mapping is used to map the first storage location of the log block to the second storage location.

[0095] S2, the mobile phone maps the first storage location of each log block in the current circular buffer to the second storage location based on the first storage location of the first log block.

[0096] The first storage location can be represented by offsets 2 and 4. The second storage location can be represented by offsets 1 and 3. Offsets 2 and 4 can be obtained from the block list in the metadata of the circular buffer. Offset 1 can be mapped from offset 2, and offset 3 can be mapped from offset 4. Offset 2 indicates the starting storage location of a log block when multiple log blocks are written to memory sequentially according to the write time. Offset 3 indicates the ending storage location of a log block when multiple log blocks are written to memory sequentially according to the write time.

[0097] Optionally, the phone can map the offsets 2 of multiple log blocks currently stored in the buffer to offsets 1, based on the offset 2 of the first log block. Optionally, the phone can map the offsets 4 of multiple log blocks currently stored in the buffer to offsets 3, based on the offset 2 of the first log block. Optionally, the phone only needs to map the offset 4 of the first log block to offset 3, reducing the amount of data and lowering power consumption.

[0098] For a log block included in the circular buffer, if the log block is located after the first log block, then offset 1 of the log block equals offset 2, and offset 3 equals offset 4. If the log block is located before the first log block or is the first log block, then offset 1 of the log block equals offset 2 plus a value B, and offset 3 equals offset 4 plus a value B. The value B is the same as the size of the circular buffer. The value B can be, for example, an integer multiple of bytes.

[0099] Specifically, for each log block among multiple log blocks: if the offset 2 of the log block is greater than the offset 2 of the first log block, then the offset 2 of the log block is determined as the offset 1 of the log block. If the offset 2 of the log block is less than or equal to the offset 2 of the first log block, then the offset 1 of the log block is determined based on the offset 2 of the log block and the size of the circular buffer. For example, the sum of the offset 2 of the log block and the value B is used as the offset 1.

[0100] For example, the phone iterates through all log blocks stored in the current circular buffer and performs the following operations for each log block: If the offset 2 of the log block is less than or equal to the offset 2 of the first log block, then the offset 1 of the log block = the offset 2 of the log block + the value B. If the offset 2 of the log block is greater than the offset 2 of the first log block, then the offset 1 of the log block = the offset 2 of the log block. Optionally, if the offset 2 of the log block is less than or equal to the offset 2 of the first log block, then the offset 3 of the log block = the offset 4 of the log block + the value B. If the offset 2 of the log block is greater than the offset 2 of the first log block, then the offset 3 of the log block = the offset 4 of the log block.

[0101] The following example uses a circular buffer where log blocks b3-b7 are stored sequentially from the start to the end. Figure 7 This storage location mapping method is described by way of example. Figure 7 The length of the circular buffer is 50 bytes, that is, the value B is 50 bytes.

[0102] For example, such as Figure 7As shown in (a), assume the first log block is log block b5. Log block b4 was written closest to log block b5, and log block b6 was the earliest log block written to the circular buffer. The offset 2 of log block b5 is 20, and the offset 4 is 30. Since the offset 2 of both log blocks b6 and b7 is greater than the offset 2 of log block b5, the offset 2 of log block b6 is taken as offset 1, and the offset 4 of log block b6 is taken as offset 3. The offset 2 of log block b7 is taken as offset 1, and the offset 4 of log block b7 is taken as offset 3. Since the offset 2 of log blocks b3 and b4 is less than the offset 2 of log block b5, the offset 2 of log block b3 plus the value B is taken as the offset 1 of log block b3. For example, the offset 1 of log block b3 is (00+50)=50. The offset 4 of log block b3 plus the value B is used as the offset 3 of log block b3. For example, the offset 3 of log block b3 is (10+50)=60. The offset 2 of log block b4 plus the value B is used as the offset 1 of log block b4. For example, the offset 1 of log block b4 is (10+50)=60. The offset 4 of log block b4 plus the value B is used as the offset 3 of log block b4. For example, the offset 3 of log block b4 is (20+50)=70. Log block b5 is the first log block. Therefore, the offset 2 of log block b5 plus the value B is used as the offset 1 of log block b5. For example, the offset 1 of log block b5 is (20+50)=70. The offset 4 of log block b5 plus the value B is used as the offset 3 of log block b5. For example, the offset of log block b5 is 3 = (30 + 50) = 80. It can be seen that position mapping is similar to virtually concatenating log blocks, such as log blocks b3-b5, from the start of the circular buffer to the end of the first log block at the end of the circular buffer. It should be understood that position mapping does not change the actual storage location of multiple log blocks in the circular buffer; it only maps one storage location to another virtual storage location. In other words, after position mapping, the storage location of multiple log blocks in the circular buffer remains unchanged. Figure 7 As shown in (a), after location mapping, the first offset of a log block indicates the write order of the log block. A smaller first offset indicates an earlier write time; a larger first offset indicates a later write time. Figure 7 As shown in (a), the first offset of multiple log blocks increases sequentially from the earliest written log block, such as log block b6, and the write times of multiple log blocks are from earliest to latest.

[0103] For example, such as Figure 7As shown in (b), the first log block is log block b3, and the write times of log blocks b4-b7 are all earlier than the write time of log block b3. Among them, the write time of log block b7 is closest to the write time of log block b3, and log block b4 is the earliest log block written to the circular buffer among these log blocks. After mapping in the same way, the offset 1 of log block b4 is 10, and the offset 3 is 20. The offset 1 of log block b5 is 20, and the offset 3 is 30. The offset 1 of log block b6 is 30, and the offset 3 is 40. The offset 1 of log block b7 is 40, and the offset 3 is 50. The offset 1 of log block b3 is 50, and the offset 3 is 60. It can be seen that the position mapping is similar to virtually splicing the log blocks included in the circular buffer from the starting position to the ending position of the first log block, such as log block b3, to the ending position of the circular buffer. After position mapping, the order of the first offsets of the multiple log blocks is consistent with the write order of the multiple log blocks. A smaller first offset indicates an earlier write operation; a larger first offset indicates a later write operation. Figure 7 As shown in (b), the first offset of multiple log blocks increases sequentially from the earliest written log block, such as log block b4, and the write times of multiple log blocks are from earliest to latest.

[0104] For example, such as Figure 7 As shown in (c), the first log block is log block b7, and the write times of log blocks b3-b6 are all earlier than the write time of log block b3. Among them, the write time of log block b6 is closest to the write time of log block b3, and log block b3 is the earliest log block written to the circular buffer among these log blocks. After mapping in the same way, the offset 1 of log block b3 is 50, and the offset 3 of log block b3 is 60. The offset 1 of log block b4 is 60, and the offset 3 of log block b4 is 70. The offset 1 of log block b5 is 70, and the offset 3 of log block b5 is 80. The offset 1 of log block b6 is 80, and the offset 3 of log block b6 is 90. The offset 1 of log block b7 is 90, and the offset 3 of log block b6 is 100. Position mapping is similar to virtually concatenating log blocks (e.g., log blocks b3-b7) from the start of the circular buffer to the end of the first log block at the end of the circular buffer. After position mapping, the order of the first offsets of multiple log blocks corresponds to the write order of the log blocks. For example, a smaller first offset indicates an earlier write time, and a larger first offset indicates a later write time. Figure 7 As shown in (c), the first offset of multiple log blocks increases sequentially from the earliest written log block, such as log block b3, and the write times of multiple log blocks are from earliest to latest.

[0105] It should be understood that all log blocks stored in the current circular buffer are valid log blocks. In other words, this application only performs position mapping on the multiple valid log blocks stored in the circular buffer. After position mapping, the phone executes S3.

[0106] S3, the mobile phone determines the target offset.

[0107] The mobile phone can determine the target offset based on the offset 3 (the third offset) of the first log block and the size of the raw partition. The target offset is the size of the raw partition offset from the offset 3 of the first log block in a first direction, opposite to the direction of sequential writing, assuming multiple log blocks are written to memory sequentially at each write time. The target offset is less than the offset 3 of the first log block, and the difference between the offset 3 of the first log block and the target offset is the size of the raw partition. In other words, offsetting the offset 3 of the first log block forward by the size of the raw partition yields the target offset. Since sequential writing proceeds from low to high addresses according to the write time, the first direction is opposite to the direction of sequential writing. For example, target offset = offset 3 of the first log block - value A.

[0108] The mobile phone can determine the target offset based on the offset 4 (fourth offset) of the first log block, the size of the raw partition, and the size of the preset area. The target offset is the amount of time offset by the first value from the offset 4 of the first log block in a second direction. The second direction is the same as the direction of sequential writing. The first value = size of the circular buffer - size of the raw partition. The target offset is greater than the offset 4 of the first log block, and the difference between the target offset and the offset 4 of the first log block is the first value. In other words, offsetting the offset 4 of the first log block by the first value yields the target offset. That is, since the offset 3 of the first log block = the offset 4 of the first log block + value B, the target offset = the offset 4 of the first log block + (value B - value A).

[0109] After obtaining the target offset, the phone can determine the target log block based on it. The target log block is the N log blocks in the current circular buffer whose write times are closest to the time of the fault occurrence. For example, the target log block could be any log block written between 20 seconds before and the time of the fault occurrence. Since the phone needs to store the target log block in the raw partition, its size is less than or equal to the size of the raw partition. The write time is the moment the phone writes the log block to the circular buffer. N is an integer greater than 1.

[0110] For example, these N log blocks include a first log block and a second log block. The first log block is the latest written log block among the N log blocks. The second log block is any of the N log blocks whose write time is earlier than that of the first log block. The number of second log blocks is greater than or equal to 1.

[0111] Specifically, when the number of second log blocks is equal to one, this second log block is the log block whose write time is closest to the write time of the first log block among the multiple log blocks currently stored in the circular buffer. When the number of second log blocks is greater than one, such as when the second log blocks include log block a and log block b, and the write time of log block b is earlier than the write time of log block a, then log block a is the log block whose write time is closest to the write time of the first log block among the multiple log blocks currently stored in the circular buffer, and log block b is the log block whose write time is closest to the write time of log block a among the multiple log blocks currently stored in the circular buffer.

[0112] S4, the phone traverses the log blocks at offset 1, reads the log blocks with offset 1 greater than or equal to the target offset from the circular buffer, and saves them in the raw partition.

[0113] After obtaining the target offset, the system iterates through the offsets of all log blocks stored in the current circular buffer. For each log block stored in the circular buffer, the following operations are performed: It checks whether the offset of the log block is greater than or equal to the target offset. If the offset of the log block is greater than or equal to the target offset, it means that the second storage location of the log block is after the location corresponding to the target offset. The phone can then read all log blocks whose second storage location is after the location corresponding to the target offset as the target log block. Specifically, if the offset of the log block is greater than or equal to the target offset, the log block is read from the circular buffer and stored in the raw partition. If the offset of the log block is less than the target offset, the system iterates through the offsets of the next log block until the iteration is complete. After the iteration is complete, the phone can read log blocks whose offset is greater than or equal to the target offset from the circular buffer and save them in the raw partition. The log blocks whose offset is greater than or equal to the target offset are the target log blocks mentioned earlier.

[0114] by Figure 8As shown in (a), the target offset corresponds to position B. The second storage location of log block b4 is after position B, such as when the offset 1 of log block b4 is greater than the target offset. The second storage location of log block b5 is after position B, such as when the offset 1 of log block b5 is greater than the target offset. The mobile phone can read log blocks b4 and b5 from the circular buffer and store them in the raw partition. That is, log blocks b4 and b5 are the target log blocks.

[0115] When value B is less than or equal to value A, the target offset = the offset of the first log block, which is 4. That is, if the size of the raw partition is greater than or equal to the size of the circular buffer, the phone will read all log blocks stored in the circular buffer as the target log block and store them in the raw partition. Combined with... Figure 7 For example, in (b) of the text, Figure 8 As shown in (b), the size of the raw partition is greater than or equal to the size of the circular buffer, and the target offset is equal to the offset 4 of log block b3, meaning the position corresponding to the target offset is at the end of log block b3, such as position C. Log blocks b4, b5, b6, b7, and b3 are all after position C; for example, the offsets 1 of log blocks b4, b5, b6, b7, and b3 are all greater than or equal to the target offset. Therefore, the mobile phone can read log blocks b4, b5, b6, b7, and b3 from the circular buffer and store them in the raw partition. That is, log blocks b4, b5, b6, b7, and b3 are the target log blocks.

[0116] In other words, in response to a phone malfunction, the phone can pre-determine whether value B is greater than or equal to value A. If value B is not greater than, but less than or equal to, value A, the phone does not need to execute any commands. Figure 5 The method shown allows the phone to directly read all the log blocks currently stored in the circular buffer as the target log block and store them in the raw partition. The phone only executes the command if value B is greater than value A. Figure 5 The method shown is used to read the target log block from the circular buffer and store it in the raw partition.

[0117] After storing the target log block in the raw partition, the phone can reboot the operating system to return to normal.

[0118] S5, the phone restarts, and after restarting, the phone sends the target log block to the server.

[0119] Since the target log block has already been stored from the circular buffer in RAM to the raw partition in NVM, it will not be lost after a reboot. Therefore, after a reboot, the phone can read the target log block from the raw partition and send it to the server.

[0120] As can be seen, the log storage method provided in this application allows a mobile phone to retrieve a target log block from a circular buffer in RAM before restarting due to a fault, and store the target log block in a preset area in NVM. This preset area can be a raw partition as described above. In other words, the target log block is the N log blocks among the multiple log blocks stored in the circular buffer whose write time is closest to the fault occurrence time. Here, N is an integer greater than or equal to 1, and the size of the target log block is the size of the preset area. In this way, before the mobile phone restarts due to a fault, it can retrieve and store log blocks closest to the fault occurrence time from the circular buffer. This allows for the acquisition of log blocks useful for fault location and analysis, which is helpful for fault localization and analysis, and contributes to product development.

[0121] This application provides an electronic device, which includes a memory and one or more processors. The memory stores computer program code. The computer program code includes computer instructions. When the processor executes the computer instructions, the electronic device can perform various functions or steps performed by the mobile phone in the above method embodiments. The structure of the electronic device can be referred to... Figure 3 The structure of the electronic device 300 shown.

[0122] This application embodiment also provides a computer storage medium, which includes computer instructions, when the computer instructions are executed in the aforementioned electronic device (such as...). Figure 3 When the electronic device 300 shown is run, it causes the electronic device to perform the various functions or steps in the above method embodiments.

[0123] This application also provides a computer program product that, when run on a computer, causes the computer to perform the various functions or steps described in the above method embodiments.

[0124] This application also provides a chip system including at least one processor and at least one interface circuit. The processor and the interface circuit are interconnected via lines. For example, the interface circuit can be used to receive signals from other devices (e.g., the memory of an electronic device). As another example, the interface circuit can be used to send signals to other devices (e.g., the processor). Exemplarily, the interface circuit can read instructions stored in the memory and send the instructions to the processor. When the instructions are executed by the processor, the electronic device can perform the steps in the above embodiments. Of course, the chip system may also include other discrete devices, and this application does not specifically limit this.

[0125] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.

[0126] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another apparatus, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0127] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units; that is, it can be located in one place or distributed in multiple different locations. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0128] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0129] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, in essence, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0130] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A log storage method, characterized in that, Applied to electronic devices, the electronic devices including random access memory and non-volatile memory; The circular buffer of the random access memory is used to store the operation log of the electronic device, and the method includes: In response to a failure of the electronic device, a first offset of each log block among a plurality of log blocks stored in the circular buffer is obtained; based on the first offset, a target log block is determined from the plurality of log blocks; the first offset is used to indicate the starting storage location of the log block when the plurality of log blocks are written to memory sequentially according to the write time; the target log block is the N log blocks among the plurality of log blocks whose write time is closest to the time of the failure; N is an integer greater than 1; the size of the target log block is less than or equal to the size of a preset area in the non-volatile memory; Store the target log block in the preset area and restart the electronic device.

2. The method according to claim 1, characterized in that, The step of obtaining the first offset of each log block among the multiple log blocks stored in the circular buffer includes: Based on the indication information, a first log block is determined from the plurality of log blocks; the indication information is used to indicate the log block most recently written to the circular buffer. Based on the second offset of the first log block, obtain the first offset of each of the plurality of log blocks; the second offset of the log block is used to indicate the starting storage position of the log block in the circular buffer.

3. The method according to claim 2, characterized in that, The step of obtaining the first offset of each of the plurality of log blocks based on the second offset of the first log block includes: For each of the plurality of log blocks: If the second offset of the log block is greater than the second offset of the first log block, the second offset of the log block is determined as the first offset of the log block; If the second offset of the log block is less than or equal to the second offset of the first log block, the first offset of the log block is determined based on the second offset of the log block and the size of the circular buffer.

4. The method according to any one of claims 1-3, characterized in that, The step of determining the target log block from the plurality of log blocks based on the first offset includes: Obtain the target offset; the target offset is the offset of the preset area from the third offset of the first log block in a first direction when the plurality of log blocks are written to the memory sequentially according to the write time; the first direction is opposite to the direction of the sequential write; the third offset is used to indicate the end storage position of the first log block when the plurality of log blocks are written to the memory sequentially according to the write time. The target log block is determined from the plurality of log blocks based on the first offset and the target offset.

5. The method according to claim 4, characterized in that, The process of obtaining the target offset includes: The third offset is determined based on the fourth offset of the first log block and the size of the circular buffer. The fourth offset of the first log block is used to indicate the end storage position of the first log block in the circular buffer. The target offset is determined based on the third offset and the size of the preset region.

6. The method according to claim 4 or 5, characterized in that, The step of determining the target log block from the plurality of log blocks based on the first offset and the target offset includes: The log block whose first offset is greater than or equal to the target offset among the plurality of log blocks is determined as the target log block.

7. An electronic device, characterized in that, The electronic device includes: a memory and one or more processors; the memory is coupled to the processors; wherein the memory is used to store computer program code, the computer program code including computer instructions; when the computer instructions are executed by the processor, the electronic device performs the method as described in any one of claims 1-6.

8. A computer-readable storage medium, characterized in that, Includes computer instructions that, when executed on an electronic device, cause the electronic device to perform the method as described in any one of claims 1-6.

9. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instructions are executed by the processor, they implement the steps of the method as described in any one of claims 1-6.