Switching frequency determination method and apparatus, storage medium, and electronic device
By creating device files and load statistics entries at the operating system kernel level and dynamically allocating kernel space to store thread information, the problem of frequent thread switching is solved, real-time monitoring of thread switching events is achieved, and system performance is improved.
Patent Information
- Application Number
- CN202411320360.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-20
- Publication Date
- 2026-05-15
- Estimated Expiration
- 2044-09-20
AI Technical Summary
There is no existing technology for real-time monitoring of thread switching events at the operating system kernel level, which leads to frequent kernel thread switching, affecting storage system performance and CPU utilization.
Device files and load statistics entries are created. Information on thread switching events is obtained through these interfaces. Kernel space is dynamically allocated to store thread information, and the switching frequency is calculated based on the thread information to achieve real-time monitoring of thread switching events.
It enables real-time monitoring of thread switching events at the operating system kernel level, reducing the problem of frequent kernel thread switching and improving storage system performance and CPU utilization.
Smart Images

Figure CN119311404B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computers, and more specifically, to a method and apparatus for determining a switching frequency, a storage medium, and an electronic device. Background Technology
[0002] The performance of the operating system kernel directly determines the overall performance of the operating system. The key to operating system performance lies in the kernel thread scheduling mechanism.
[0003] Currently, the scheduling mechanism at the operating system kernel level generally employs a priority-based preemptive scheduling strategy. In this strategy, each kernel thread is assigned a priority, and threads with higher priorities are scheduled first. However, this strategy can lead to frequent kernel thread switching. Frequent kernel thread switching can cause the following problems: it reduces the utilization of cache resources in the storage system, thus affecting storage system performance; it also leads to higher CPU resource consumption, increasing CPU load and reducing CPU utilization.
[0004] Therefore, it is necessary to effectively monitor kernel thread switching events to determine whether kernel thread switching is frequent. However, there is no method in related technologies to monitor thread switching events in real time at the operating system kernel level.
[0005] Therefore, the problem that there is no method for real-time monitoring of thread switching events at the operating system kernel level in related technologies has not yet been effectively solved. Summary of the Invention
[0006] This application provides a method and apparatus for determining the switching frequency, a storage medium, and an electronic device, to at least solve the problem in related technologies that there is no method for real-time monitoring of thread switching events at the operating system kernel level.
[0007] According to one embodiment of this application, a method for determining a switching frequency is provided, specifically including: creating a device file and a load statistics entry, wherein the device file is used to indicate the communication interface between kernel mode and user mode, the load statistics entry is information to be obtained when a thread switching event is executed, and the thread switching event is used to indicate an event in which a thread switches between the kernel mode and the user mode; when a thread switching event is determined to exist, obtaining thread information corresponding to the thread switching event according to the device file, wherein the thread information is information corresponding to the load statistics entry; and determining a first switching frequency corresponding to the thread switching event according to the thread information.
[0008] In an exemplary embodiment, after creating the device file and load statistics entries, the method further includes: obtaining configuration parameters corresponding to the kernel state, and determining a first interface according to a first function in the device file; determining the target space size of the target kernel space to be applied for according to the configuration parameters, wherein the target kernel space is used to store the obtained thread information; calling a second function according to the first interface to apply for a first kernel space of the target space size from the space allocator corresponding to the kernel state according to the second function, wherein the kernel space corresponding to the kernel state includes: the first kernel space; receiving a return value sent by the space allocator; if the return value is determined to be a first return value, determining the first kernel space as the target kernel space; if the return value corresponding to the second function is determined to be a second return value, releasing the kernel space corresponding to the kernel state, and calling the second function again according to the first interface to apply for a second kernel space of the target space size from the space allocator according to the second function, wherein the kernel space corresponding to the kernel state includes: the second kernel space; if the received return value sent by the space allocator is determined to be the first return value, determining the second kernel space as the target kernel space.
[0009] In an exemplary embodiment, after determining the first kernel space as the target kernel space or the second kernel space as the target kernel space, the method further includes: mapping the target kernel space to the user space corresponding to the user mode, so as to allow the first program corresponding to the user mode to access the target kernel space.
[0010] In an exemplary embodiment, before obtaining the thread information corresponding to the thread switching event according to the device file, the method further includes: determining that the thread switching event exists when the thread executes a target event, wherein the target event includes one of the following: the thread switches from the kernel mode to the user mode, the thread switches from the user mode to the kernel mode, or the thread is interrupted and preempted.
[0011] In one exemplary embodiment, obtaining thread information corresponding to the thread switching event according to the device file includes: determining a hook function corresponding to the thread switching event according to the device file when the thread switching event is determined to start execution by a third function in the device file; and obtaining the thread information according to the hook function.
[0012] In an exemplary embodiment, determining the first switching frequency corresponding to the thread switching event based on the thread information includes: determining the statistical period corresponding to the thread switching event; determining the first number of thread switching events within the current statistical period based on the first thread information corresponding to the current statistical period; and determining the first switching frequency corresponding to the thread switching events within the current statistical period based on the first number.
[0013] In an exemplary embodiment, after determining the first switching frequency corresponding to the thread switching event based on the thread information, the method further includes: determining a second number of thread switching events in the previous statistical period based on the second thread information corresponding to the previous statistical period of the current statistical period; determining a second switching frequency corresponding to the thread switching events in the previous statistical period based on the second number; determining the magnitude relationship between the first switching frequency and the second switching frequency; and triggering an alarm mechanism if the magnitude relationship indicates that the first switching frequency is greater than the second switching frequency.
[0014] According to another embodiment of this application, a device for determining a switching frequency is provided, comprising: a creation module, configured to create a device file and a load statistics entry, wherein the device file is used to indicate the communication interface between kernel mode and user mode, and the load statistics entry is information to be acquired when a thread switching event is executed, the thread switching event being used to indicate an event in which a thread switches between the kernel mode and the user mode; an acquisition module, configured to acquire thread information corresponding to the thread switching event according to the device file when it is determined that a thread switching event exists, wherein the thread information is information corresponding to the load statistics entry; and a determination module, configured to determine a first switching frequency corresponding to the thread switching event according to the thread information.
[0015] According to yet another embodiment of this application, a computer-readable storage medium is also provided, wherein a computer program is stored in the computer program, and the computer program is configured to execute the steps in the above method embodiments when it is run.
[0016] According to yet another embodiment of this application, an electronic device is also provided, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in the above method embodiments.
[0017] According to yet another embodiment of this application, a computer program product is also provided, including a computer program that, when executed by a processor, implements the steps in the above method embodiments.
[0018] The method for determining the switching frequency in this application creates a device file to indicate the communication interface between kernel mode and user mode, and creates a load statistics entry to indicate the information to be acquired when a thread switching event occurs. When a thread switching event is determined to exist, indicating a switch between kernel mode and user mode, the thread information corresponding to the thread switching event is obtained from the device file, and a first switching frequency corresponding to the thread switching event is determined based on the thread information. In other words, this application creates a device file and load statistics entries. When a thread switching event occurs, the thread information corresponding to the thread switching event is obtained from the device file; the thread information is the information corresponding to the load statistics entry. Then, the first switching frequency corresponding to the thread switching event is determined based on the thread information to achieve monitoring of the thread switching event. This application solves the problem in related technologies that there is no method for real-time monitoring of thread switching events at the operating system kernel level. Therefore, this application enables real-time monitoring of thread switching events at the operating system kernel level. Attached Figure Description
[0019] Figure 1 This is a hardware structure block diagram of a computer terminal for a method of determining a switching frequency according to an embodiment of this application.
[0020] Figure 2 This is a flowchart of a method for determining a switching frequency according to an embodiment of this application;
[0021] Figure 3 This is a flowchart of a storage system kernel thread switching tracking mechanism and alarm implementation method according to an optional embodiment of this application;
[0022] Figure 4 This is a structural block diagram of a switching frequency determination device according to an embodiment of this application. Detailed Implementation
[0023] The embodiments of this application will be described in detail below with reference to the accompanying drawings and examples.
[0024] It should be noted that the terms "first," "second," etc., in the specification, claims, and drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0025] The methods and embodiments provided in this application can be executed on a mobile terminal, a computer terminal, or a similar computing device. Taking running on a computer terminal as an example, Figure 1 This is a hardware structure block diagram of a computer terminal for a method of determining a switching frequency according to an embodiment of this application. For example... Figure 1 As shown, a computer terminal may include one or more ( Figure 1Only one is shown in the diagram. A processor 102 (which may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.) and a memory 104 for storing data are also shown. The computer terminal may further include a transmission device 106 for communication functions and an input / output device 108. Those skilled in the art will understand that... Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the computer terminal described above. For example, the computer terminal may also include components that are more complex than those described above. Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.
[0026] The memory 104 can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the method for determining the interaction state in this embodiment. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, thus implementing the above-described method. The memory 104 may include high-speed random access memory and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to a computer terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0027] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by a communication provider for the computer terminal. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be a Radio Frequency (RF) module used for wireless communication with the Internet.
[0028] Figure 2 This is a flowchart of a method for determining a switching frequency according to an embodiment of this application, which can be applied to... Figure 1 In computer terminals, such as Figure 2 As shown, the process includes the following steps:
[0029] Step S202: Create a device file and a load statistics entry, wherein the device file is used to indicate the communication interface between kernel mode and user mode, and the load statistics entry is information to be obtained in the event of a thread switching event, wherein the thread switching event is used to indicate an event in which a thread switches between the kernel mode and the user mode;
[0030] The aforementioned load statistics entries may include: the start time and end time of the thread switching event, the system time for filling the load statistics entries, and the system call number when the thread switching event occurred.
[0031] Step S204: If a thread switching event is determined to exist, obtain the thread information corresponding to the thread switching event according to the device file, wherein the thread information is the information corresponding to the load statistics entry;
[0032] It is understandable that the above thread information is used to populate the load statistics entries. For example, if the load statistics entries include the start time of the thread switching event, then the thread information can be the specific start time of this thread switching event, such as 10:00.
[0033] Step S206: Determine the first switching frequency corresponding to the thread switching event based on the thread information.
[0034] The method for determining the switching frequency in this application creates a device file to indicate the communication interface between kernel mode and user mode, and creates a load statistics entry to indicate the information to be acquired when a thread switching event occurs. When a thread switching event is determined to exist, indicating a switch between kernel mode and user mode, the thread information corresponding to the thread switching event is obtained from the device file, and a first switching frequency corresponding to the thread switching event is determined based on the thread information. In other words, this application creates a device file and load statistics entries. When a thread switching event occurs, the thread information corresponding to the thread switching event is obtained from the device file; the thread information is the information corresponding to the load statistics entry. Then, the first switching frequency corresponding to the thread switching event is determined based on the thread information to achieve monitoring of the thread switching event. This application solves the problem in related technologies that there is no method for real-time monitoring of thread switching events at the operating system kernel level. Therefore, this application enables real-time monitoring of thread switching events at the operating system kernel level.
[0035] Optionally, after creating the device file and load statistics entries in step S202 above, the method further includes: obtaining the configuration parameters corresponding to the kernel state, and determining a first interface according to a first function in the device file; determining the target space size of the target kernel space to be applied for according to the configuration parameters, wherein the target kernel space is used to store the obtained thread information; calling a second function according to the first interface to apply for a first kernel space of the target space size from the space allocator corresponding to the kernel state according to the second function, wherein the kernel space corresponding to the kernel state includes: the first kernel space; receiving a return value sent by the space allocator; if the return value is determined to be a first return value, determining the first kernel space as the target kernel space; if the return value corresponding to the second function is determined to be a second return value, releasing the kernel space corresponding to the kernel state, and calling the second function again according to the first interface to apply for a second kernel space of the target space size from the space allocator according to the second function, wherein the kernel space corresponding to the kernel state includes: the second kernel space; if the received return value sent by the space allocator is determined to be the first return value, determining the second kernel space as the target kernel space.
[0036] It is understandable that the second return value mentioned above can be NULL (a function return value).
[0037] As can be seen from the above embodiments, specific schemes for dynamically and securely allocating kernel space within the system to store context information during thread switching include:
[0038] 1) Obtain kernel-mode configuration parameters: It is necessary to obtain the configuration parameters related to the kernel thread switching event from the system or user-space program. The configuration parameters may include: the tracking granularity corresponding to the kernel thread switching event, the number of record entries, alarm thresholds, etc. The configuration parameters will guide the subsequent allocation and use of kernel space.
[0039] 2) Determine the first interface: A specific function (i.e., the first function) in the device file determines the interface (i.e., the first interface) used for interaction with kernel mode. The first interface is the communication channel between user mode and kernel mode, through which commands can be sent and responses can be received.
[0040] 3) Determine the target space size: Calculate the required kernel space size (i.e., the target space size) based on the obtained configuration parameters. The target space size is determined according to the requirements in the configuration parameters, such as the number of load statistics entries to be recorded and the space required for each entry.
[0041] 4) Requesting Kernel Space: Using a defined first interface, the corresponding kernel space allocator function (i.e., the second function) is called to request the allocation of kernel space of the target size. In the Linux kernel (an operating system) based on the x86 architecture, the second function can be "_get_free_pages" (a function) or "kmalloc" (a function), etc. The space allocator allocates a contiguous block of memory (the size of the memory block is the target space size) and returns a pointer or status value (i.e., the return value).
[0042] 5) Handling Return Values: Receive the return value from the space allocator and examine it. If the return value is a valid kernel space pointer (i.e., the first return value), it proves that the kernel space allocation was successful, and this space (i.e., the first kernel space) is designated as the target kernel space for storing thread information. If the allocation fails (i.e., the return value is the second return value), the space in the kernel space that was attempted to be allocated but was invalid needs to be released after a period of time, and the second function should be called again to request the allocation of the target kernel space until the target kernel space is successfully allocated.
[0043] 6) Looping and Retrying with Space Allocation: If the initial allocation fails, the previously attempted allocation space will be released after a period of time. Then, the second function will be called again through the first interface to request the allocation of a second kernel space of the target size. If this allocation is successful and the first return value is received, the second kernel space will be designated as the target kernel space to store thread switching information.
[0044] The above technical solutions ensure the correct allocation and use of kernel space, providing the necessary memory resources for subsequent load statistics and thread switching event tracking. Simultaneously, the loop retry mechanism improves the system's adaptability under memory pressure.
[0045] Optionally, after determining the first kernel space as the target kernel space or the second kernel space as the target kernel space in step S204 above, the method further includes: mapping the target kernel space to the user space corresponding to the user mode, so as to allow the first program corresponding to the user mode to access the target kernel space.
[0046] Understandably, by mapping the target kernel space to user space using the aforementioned technical solution, the kernel space can be directly accessed from user mode, reducing the number of system calls and thus the overhead of thread switching. In other words, through memory mapping, user-mode programs can directly access kernel data, improving the speed and efficiency of data processing.
[0047] Optionally, before obtaining the thread information corresponding to the thread switching event according to the device file in step S204 above, the method further includes: determining that the thread switching event exists when the thread executes the target event, wherein the target event includes one of the following: the thread switches from the kernel mode to the user mode, the thread switches from the user mode to the kernel mode, or the thread is interrupted and preempted.
[0048] Among them, thread interruption and preemption refers to the situation in the operating system where, when a thread is running, it is paused due to a hardware interrupt (such as the arrival of a network packet, timer interrupt, etc.) or a software interrupt (such as a system call, signal processing, etc.), and control is temporarily handed over to the kernel mode. After the kernel mode processes the interrupt event, it may reschedule the thread and select another thread to run.
[0049] Optionally, step S206 above, obtaining the thread information corresponding to the thread switching event according to the device file, includes: if the thread switching event is determined to start execution through a third function in the device file, determining the hook function corresponding to the thread switching event according to the device file; and obtaining the thread information according to the hook function.
[0050] Understandably, when a user-space program calls a third function in the device file, the kernel receives a signal, marking the start of thread switching logging. Based on the device file configuration, the kernel determines the hook function to be called. This function is registered during initialization and is used to capture information at specific event points. When the hook function is called, it collects context information about the current thread switch, including but not limited to: Thread Identifier (TID): used to uniquely identify a thread; Switch Timestamp: records the specific time the thread switch occurred, used for subsequent time statistics and analysis; Thread State: records the state of the thread before and after the switch. The hook function may also read specific register values, such as EIP (Instruction Pointer), to determine the location of the instruction being executed before the thread switch, which aids in troubleshooting and performance analysis.
[0051] Hook functions allow the kernel to dynamically capture thread switching events and record detailed thread information, which is crucial for performance monitoring and system stability analysis.
[0052] Optionally, step S206 above, which determines the first switching frequency corresponding to the thread switching event based on the thread information, includes: determining the statistical period corresponding to the thread switching event; determining the first number of thread switching events within the current statistical period based on the first thread information corresponding to the current statistical period; and determining the first switching frequency corresponding to the thread switching events within the current statistical period based on the first number.
[0053] The method further includes, after determining the first switching frequency corresponding to the thread switching event based on the thread information, the method further includes: determining the second number of thread switching events in the previous statistical period based on the second thread information corresponding to the previous statistical period of the current statistical period; determining the second switching frequency corresponding to the thread switching event in the previous statistical period based on the second number; determining the magnitude relationship between the first switching frequency and the second switching frequency; and triggering an alarm mechanism if the magnitude relationship indicates that the first switching frequency is greater than the second switching frequency.
[0054] It is understandable that determining the first switching frequency and the technical solutions following that determination mainly include:
[0055] (1) Determine the first switching frequency based on the thread information.
[0056] 1) Determine the statistical period: The statistical period refers to the time period used to calculate the thread switching frequency. It can be fixed, such as per second or per minute, or it can be triggered by a specific event. Within this period, the system will record all thread switching events that occur.
[0057] 2) Determine the first quantity: The first quantity refers to the number of thread switching events that occur within the current statistical period. The system needs to have a mechanism to accurately record each thread switching event, including the thread identifier (ID) and switching time.
[0058] 3) Calculate the first switching frequency: By dividing the first quantity by the duration of the current statistical period, the thread switching frequency within the current period can be obtained. This reflects the frequency of thread switching within the current statistical period.
[0059] (2) Determine the second switching frequency based on historical data.
[0060] After determining the first switching frequency, the second switching frequency also needs to be determined based on the data from the previous statistical period. Specifically:
[0061] 1) Determine the second quantity: The second quantity is the number of thread switching events that occurred in the previous statistical period. This also requires the system to record all thread switching information from the previous period.
[0062] 2) Calculate the second switching frequency: By dividing the second quantity by the duration of the previous statistical period, the thread switching frequency in the previous period can be obtained.
[0063] (3) Compare frequencies and trigger alarms.
[0064] 1) Determine the size relationship: Compare the first switching frequency of the current statistical period with the second switching frequency of the previous statistical period to determine the size relationship between the two.
[0065] 2) Triggering an alarm mechanism: If the first switching frequency is greater than the second switching frequency and exceeds a set threshold (i.e., the ratio between the first and second switching frequencies exceeds a preset base frequency), an alarm mechanism is triggered. This indicates that thread switching within the current cycle is relatively frequent, which may have a negative impact on system performance.
[0066] The purpose of triggering the alarm mechanism is to remind system administrators or monitoring systems to pay attention to the current thread switching situation, which may require measures to optimize the system, such as adjusting the scheduling strategy, reducing unnecessary thread creation or system calls, and optimizing the locking strategy, in order to avoid performance degradation caused by frequent thread switching.
[0067] In summary, by comparing the thread switching frequency in different cycles, system performance can be dynamically monitored, potential performance bottlenecks can be identified in a timely manner, and this helps to improve system stability and response speed.
[0068] To better understand the process of determining the switching frequency described above, the implementation flow of the method will be further explained below with reference to optional embodiments, but this is not intended to limit the technical solution of the embodiments of this application.
[0069] Key terms used in the optional embodiments of this application include:
[0070] 1) Kernel Mode and User Mode: Kernel mode and user mode are two important CPU operating states in a computer operating system, differing significantly in terms of permissions, resource access, and instruction execution. Kernel mode: Can execute all types of instructions, including those that may affect system stability. This is because the kernel needs comprehensive management and control of the system. User mode: Can only execute non-privileged instructions. For operations requiring privileged instructions, user programs must request kernel assistance through system calls, etc. User mode and kernel mode can switch between each other. The transitions between kernel mode and user mode include:
[0071] (1) User mode to kernel mode: When a user-mode program requests kernel services through system calls, interrupts, or exceptions, it triggers a transition from user mode to kernel mode. This transition is unidirectional and consumes certain system resources.
[0072] (2) Kernel mode to user mode: After the kernel completes the service requested by the user, it will actively return control to the user-mode program. This transition is also unidirectional and controlled by the kernel.
[0073] Kernel mode and user mode are two important CPU operating states in an operating system, differing significantly in terms of permissions, resource access, and instruction execution. These differences ensure the stability and security of the operating system and provide necessary resources and services for user programs.
[0074] 2) Instruction Pointer Register (EIP Register): The EIP register is an important component of the CPU, storing the memory address of the instruction the CPU is about to execute. By modifying the value of the EIP register, functions such as program flow control, function calls and returns, and exception handling can be implemented.
[0075] When a Linux system handles a system call or interrupt, the kernel allocates space for a Processor Registers Context Structure (pt_regs structure) on a specific stack (e.g., the kernel stack) and accesses this structure via pointers or macros. For example, on x86 architectures, a pointer to the current task's pt_regs structure can be obtained using the current_pt_regs() macro (this is not a standard macro, and the specific name may vary depending on the kernel version and architecture). However, in most cases, this pointer is implicitly handled through assembly code and the kernel's context management mechanism.
[0076] This application provides an optional embodiment of a storage system kernel thread switching tracking mechanism and alarm implementation method. Kernel thread switching recording and tracking are implemented through modules such as an initialization module, a switching record module, interface calls, load statistics, and alarm reporting. The load statistics module assesses the current system switching load, and if it exceeds the average threshold, an alarm is reported. Necessary resources are initialized during system startup or module loading, such as memory allocation, data structure initialization, and configuration reading. Relevant information is recorded during kernel thread switching, such as switching time, source thread identifier (ID), and target thread ID. A character device is implemented for communication with user space. User space programs can send commands through this interface, such as querying the current record or clearing the record. After receiving the command, the kernel module executes the corresponding operation and returns the result through the interface. The thread switching frequency within the system is periodically counted to assess the system load. The load is compared with a preset threshold to determine if it is too high.
[0077] Figure 3 This is a flowchart of a storage system kernel thread switching tracking mechanism and alarm implementation method according to an optional embodiment of this application, such as... Figure 3 As shown:
[0078] Steps S301-S306 are steps implemented by the initialization module.
[0079] Step S301: Confirm that the switching recording function is enabled.
[0080] The thread switching log is a kernel-level technology used to capture and record kernel thread switching events. It involves modifying the kernel scheduler and registering specific hook functions that are automatically called during kernel thread switching to record relevant context information, such as the switching time, thread ID, and system call number.
[0081] Step S302: Create device file.
[0082] Create a character-type device file for the switching module, implement the device file operation interface (e.g., read, write, control, etc.), and translate these operations into specific control instructions for the kernel to control the hardware device.
[0083] Step S303: Determine the kernel interface function that implements the record switching function.
[0084] The interface functions corresponding to the device file operation interface mainly include:
[0085] 1) `kernel_trace_buffer_mmap`: Allocates memory in kernel space and maps it to user space, mapping the contents of the trace buffer to a specific interface in user space. It is used to efficiently transfer trace data from kernel space to user space for further analysis and processing. It retrieves the configuration parameters for switching records and calculates the required memory size (i.e., the target space size). Then, it requests `_get_free_pages` (free pages) memory from the kernel page allocator (i.e., the space allocator). It checks the return value; if it is NULL (i.e., the second return value), it indicates that memory allocation failed. It handles the error according to the specific situation, returns an error code, releases the allocated resources, and retrys. It uses the allocated memory; if the memory allocation is successful, it uses the memory block to store data. In the module unloading function, it uses functions such as `free_pages` to release the currently allocated memory.
[0086] 2) Implement the `kernel_trace_ioctl` function: The kernel's `ioctl` (Input / Output Control) is a system call that enables bidirectional communication between device drivers and user-space programs. It controls device characteristics through control commands. The user-space program interface sends control commands to the device driver and then receives data from the driver to control specific functions and operations of the device. Return value: Returns 0 on success and -1 on failure with the corresponding error code. Detailed functions may include:
[0087] (1) start_tracing function (i.e. the third function): Starts the current record switching by registering a record start function to the kernel's scheduling function.
[0088] (2) The copy_from_user function is used to obtain parameter information from user-mode modules;
[0089] (3) kernel_tracer function: The main function of this function is to track and record activities in the kernel.
[0090] (4) The preempt_cOunt_inc function is used to increase the preemption count of the current thread to ensure that the kernel will not be preempted when subsequent code segments are executed.
[0091] (5) stop_tracing function: Ends the current record switching by registering a record stopping function to the kernel scheduling function.
[0092] The current thread information is represented by a `task_struct` structure, which contains various process state information, such as register values, memory addresses, and scheduling information. When a process switches from user mode to kernel mode, the kernel needs to save the current register state so that execution can resume after processing kernel-mode tasks. This is done using the `pt_regs` structure. The `pt_regs` structure is associated with a specific context (e.g., interrupt context, system call context) and is saved and restored via kernel stack operations when these contexts are activated. The kernel allocates space for the `pt_regs` structure on the kernel stack and accesses this structure via pointers or macros.
[0093] Step S304: Register the hook function pointer variable.
[0094] Register hook functions through kernel interface functions to track and record thread switching events.
[0095] Step S305: Create a load statistics entry structure.
[0096] Allocate memory space of the corresponding size based on the configured number of entries (ENTRY_NUM). Load statistics entries include:
[0097] 1) Current_time (a field): Used to record the system time when the entry is populated;
[0098] 2) Last_entry_time (a field): used to record the recording time of the previous record;
[0099] 3) Start_time (a field): Used to record the start time when switching threads;
[0100] 4) Cpu_id (a field): Records the CPUID (i.e., the identifier of the central processing unit) where the current thread switch occurred;
[0101] 5) Cur_pid (a field): The PID (Process Identifier) number of the thread that was switched;
[0102] 6) Parent_pid (a field): Records the PID number of the parent process of the currently switched thread;
[0103] 7) System_call (one field): The system call number where the switch occurred;
[0104] 8) Eip (a field): Records the CPU instruction address.
[0105] 9) Run_time (a field): Records the time the thread runs.
[0106] When the switching record module is running, if a thread switch occurs, the structure for populating load statistics entries is implemented according to the driver's interface.
[0107] In addition, switching the recording module can achieve the following process:
[0108] Every system call in the Linux operating system involves a task switch. The kernel scheduling function implemented in this method initiates a switch to record the tracing functionality of the switching module with each scheduling cycle. Enabling and disabling the switching module is achieved through enabling and disabling by the upper-layer application.
[0109] The following operating system behaviors trigger the thread switching logging module to record thread switching information:
[0110] 1) The task switches from kernel mode to user mode;
[0111] 2) The task switches from user mode to kernel mode;
[0112] 3) The process is interrupted and preempted;
[0113] When the above scenarios occur, the following key actions will be performed:
[0114] The switching recording module records the number of clock cycles for each task execution: one clock cycle is read when the task starts execution, and the number of clock cycles is read again when the task ends execution. The difference between the two clock cycle readings is the execution time of the task. This time difference is saved to the Run time in step S305.
[0115] When switching between threads, the type of task is determined, and the system call number is obtained through the value of the orig_eax register (a type of register). This system call number is used to record the system call for switching between threads and is stored in System_call in step S305.
[0116] By obtaining the current clock ticks (clock cycles) and combining them with the CPU frequency, the system time for thread switching is calculated, and the system time is saved to the Current time in step S305.
[0117] Obtain the process PID and parent process PID through the process context, and record them in Cur_pid and Parent_pid in step S305.
[0118] By reading the instruction pointer register of EIP, the address of the instruction that caused the interrupt or exception is obtained and recorded in EIP in step S305.
[0119] Step S306 is implemented through the load statistics module.
[0120] Step S306: Determine whether there is a sudden increase in the current load by running the load statistics module.
[0121] The memory space allocated by the switching record module is used cyclically during runtime. After each round of space usage, the load statistics module is triggered to run once. The statistics module iterates through the load statistics entries, counting the number of thread switches per unit time according to different time granularities (e.g., ms, us, ns). The data within a statistical period (pre_status_cycles, i.e., the previous statistical period) is used to calculate a base frequency (i.e., the second switching frequency). The number of periods can be configured through kernel parameters, and one period represents one cycle of the memory space allocated by the switching record module. After this cycle is completed, the load statistics module calculates the frequency of the current cycle (i.e., the current statistical period) (i.e., the first switching frequency). This frequency is compared with the base frequency. If it exceeds the base frequency threshold ratio frequency_ratio (configurable), an alarm report is triggered by the alarm module, and then the base frequency is updated.
[0122] The generation of the reference frequency is dynamic. After each cycle, the earliest record is deleted from pre_status_cycles (a function), and the latest record is added to recalculate the reference frequency.
[0123] If it is determined that the current load has increased suddenly, proceed to step S307; if it is determined that the current load has not increased suddenly, proceed to step S306.
[0124] Step S307 is implemented through the load alarm module.
[0125] Step S307: If it is determined that the current load has increased sharply, that is, if the thread switching frequency is greater than the base frequency, an alarm is issued.
[0126] By reporting alerts on the storage software stack, the system is alerted to excessive load during process switching, providing a performance impact warning.
[0127] This application provides an optional embodiment of a storage system kernel thread switching tracking mechanism and alarm implementation method. Kernel thread switching is recorded and tracked through modules such as a switching record module, interface calls, load statistics, and alarm reporting. The load statistics module assesses the current system switching load, and an alarm is reported if the load exceeds the average threshold. This serves as a performance early warning mechanism, reminding users to allocate business pressure appropriately. It facilitates performance monitoring and evaluation, enhances system observability, improves system stability, and can also be used for fault diagnosis and troubleshooting.
[0128] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods described in the various embodiments of this application.
[0129] This embodiment also provides a switching frequency determination device, which is used to implement the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can refer to a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0130] Figure 4 This is a structural block diagram of a switching frequency determination device according to an embodiment of this application, such as... Figure 4 As shown, the device includes:
[0131] Module 42 is used to create device files and load statistics entries. The device files are used to indicate the communication interface between kernel mode and user mode. The load statistics entries are information to be obtained when a thread switching event is executed. The thread switching event is used to indicate the event in which a thread switches between the kernel mode and the user mode.
[0132] The acquisition module 44 is used to acquire thread information corresponding to the thread switching event according to the device file when it is determined that a thread switching event exists, wherein the thread information is information corresponding to the load statistics entry; the determination module 46 is used to determine the first switching frequency corresponding to the thread switching event according to the thread information.
[0133] The switching frequency determination device of this application creates a device file to indicate the communication interface between kernel mode and user mode, and creates a load statistics entry to indicate the information to be obtained when a thread switching event occurs. When a thread switching event is determined to exist, in which a thread switches between kernel mode and user mode, the thread information corresponding to the thread switching event is obtained from the device file, and a first switching frequency corresponding to the thread switching event is determined based on the thread information. In other words, this application creates a device file and load statistics entries. When a thread switching event occurs, the thread information corresponding to the thread switching event is obtained from the device file, and the thread information is the information corresponding to the load statistics entry. Then, the first switching frequency corresponding to the thread switching event is determined based on the thread information to achieve monitoring of the thread switching event. This application solves the problem in related technologies that there is no method for real-time monitoring of thread switching events at the operating system kernel level. Therefore, this application enables real-time monitoring of thread switching events at the operating system kernel level.
[0134] In an exemplary embodiment, the creation module 42 is further configured to: obtain configuration parameters corresponding to the kernel state; determine a first interface according to a first function in the device file; determine the target space size of the target kernel space to be applied for according to the configuration parameters, wherein the target kernel space is used to store the obtained thread information; call a second function according to the first interface to apply for a first kernel space of the target space size from the space allocator corresponding to the kernel state according to the second function, wherein the kernel space corresponding to the kernel state includes: the first kernel space; receive a return value sent by the space allocator; if the return value is determined to be a first return value, determine the first kernel space as the target kernel space; if the return value corresponding to the second function is determined to be a second return value, release the kernel space corresponding to the kernel state, and call the second function again according to the first interface to apply for a second kernel space of the target space size from the space allocator according to the second function, wherein the kernel space corresponding to the kernel state includes: the second kernel space; if the received return value sent by the space allocator is determined to be the first return value, determine the second kernel space as the target kernel space.
[0135] In an exemplary embodiment, the acquisition module 44 is further configured to map the target kernel space to the user space corresponding to the user mode, so as to allow the first program corresponding to the user mode to access the target kernel space.
[0136] In an exemplary embodiment, the acquisition module 44 is further configured to determine the existence of the thread switching event when the thread executes the target event, wherein the target event includes one of the following: the thread switches from the kernel mode to the user mode, the thread switches from the user mode to the kernel mode, or the thread is interrupted and preempted.
[0137] In an exemplary embodiment, the acquisition module 44 is further configured to, when the thread switching event is determined to start execution by a third function in the device file, determine the hook function corresponding to the thread switching event according to the device file; and acquire the thread information according to the hook function.
[0138] In an exemplary embodiment, the determining module 46 is further configured to determine the statistical period corresponding to the thread switching event; determine the first number of thread switching events in the current statistical period based on the first thread information corresponding to the current statistical period; and determine the first switching frequency corresponding to the thread switching events in the current statistical period based on the first number.
[0139] In an exemplary embodiment, the determining module 46 is further configured to determine a second number of thread switching events in the previous statistical period based on the second thread information corresponding to the previous statistical period of the current statistical period; determine a second switching frequency corresponding to the thread switching events in the previous statistical period based on the second number; determine the size relationship between the first switching frequency and the second switching frequency; and trigger an alarm mechanism if the size relationship indicates that the first switching frequency is greater than the second switching frequency.
[0140] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in the above method embodiments when it is run.
[0141] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.
[0142] Optionally, in this embodiment, the storage medium may be configured to store program code for performing the following steps:
[0143] S1, Create a device file and a load statistics entry, wherein the device file is used to indicate the communication interface between kernel mode and user mode, and the load statistics entry is information to be obtained in the event of a thread switching event, wherein the thread switching event is used to indicate an event in which a thread switches between the kernel mode and the user mode;
[0144] S2, if it is determined that a thread switching event exists, obtain the thread information corresponding to the thread switching event according to the device file, wherein the thread information is the information corresponding to the load statistics entry;
[0145] S3, determine the first switching frequency corresponding to the thread switching event based on the thread information.
[0146] Embodiments of this application also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in the above method embodiments.
[0147] In one exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0148] Optionally, in this embodiment, the processor can be configured to perform the following steps via a computer program:
[0149] S1, Create a device file and a load statistics entry, wherein the device file is used to indicate the communication interface between kernel mode and user mode, and the load statistics entry is information to be obtained in the event of a thread switching event, wherein the thread switching event is used to indicate an event in which a thread switches between the kernel mode and the user mode;
[0150] S2, if it is determined that a thread switching event exists, obtain the thread information corresponding to the thread switching event according to the device file, wherein the thread information is the information corresponding to the load statistics entry;
[0151] S3, determine the first switching frequency corresponding to the thread switching event based on the thread information.
[0152] Embodiments of this application also provide a computer program product, including a computer program that, when executed by a processor, is implemented to perform the steps in the above method embodiments.
[0153] Optionally, in this embodiment, the above-described computer program product can be executed by a processor using the following steps:
[0154] S1, Create a device file and a load statistics entry, wherein the device file is used to indicate the communication interface between kernel mode and user mode, and the load statistics entry is information to be obtained in the event of a thread switching event, wherein the thread switching event is used to indicate an event in which a thread switches between the kernel mode and the user mode;
[0155] S2, if it is determined that a thread switching event exists, obtain the thread information corresponding to the thread switching event according to the device file, wherein the thread information is the information corresponding to the load statistics entry;
[0156] S3, determine the first switching frequency corresponding to the thread switching event based on the thread information.
[0157] Specific examples in this embodiment can be found in the examples described in the above embodiments and exemplary implementations, and will not be repeated here.
[0158] Obviously, those skilled in the art should understand that the modules or steps of this application described above can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. They can be implemented using computer-executable program code, and thus can be stored in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those presented here, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, this application is not limited to any particular combination of hardware and software.
[0159] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the principles of this application should be included within the protection scope of this application.
Claims
1. A method for determining a switching frequency, characterized in that, include: Create a device file and a load statistics entry, wherein the device file is used to indicate the communication interface between kernel mode and user mode, and the load statistics entry is information to be obtained in the event of a thread switching event, wherein the thread switching event is used to indicate an event in which a thread switches between the kernel mode and the user mode; If a thread switching event is determined to exist, the thread information corresponding to the thread switching event is obtained from the device file, wherein the thread information is information corresponding to the load statistics entry; Determine the first switching frequency corresponding to the thread switching event based on the thread information; Determining the first switching frequency corresponding to the thread switching event based on the thread information includes: Determine the statistical period corresponding to the thread switching event; The first number of thread switching events within the current statistical period is determined based on the first thread information corresponding to the current statistical period. The first switching frequency corresponding to the thread switching event within the current statistical period is determined based on the first quantity.
2. The method according to claim 1, characterized in that, After creating the device file and load statistics entries, the method further includes: Obtain the configuration parameters corresponding to the kernel state, and determine the first interface according to the first function in the device file; The target size of the target kernel space to be applied for is determined according to the configuration parameters, wherein the target kernel space is used to store the acquired thread information; The second function is called according to the first interface to request a first kernel space of the target size from the space allocator corresponding to the kernel state, wherein the kernel space corresponding to the kernel state includes: the first kernel space; Receive the return value sent by the space allocator; If the return value is determined to be the first return value, the first kernel space is determined to be the target kernel space; If the return value of the second function is determined to be the second return value, the kernel space corresponding to the kernel state is released, and the second function is called again according to the first interface to apply for a second kernel space of the target size from the space allocator according to the second function, wherein the kernel space corresponding to the kernel state includes: the second kernel space; If the received return value from the space allocator is determined to be the first return value, the second kernel space is determined to be the target kernel space.
3. The method according to claim 2, characterized in that, After determining the first kernel space as the target kernel space or the second kernel space as the target kernel space, the method further includes: The target kernel space is mapped to the user space corresponding to the user mode, so as to allow the first program corresponding to the user mode to access the target kernel space.
4. The method according to claim 1, characterized in that, Before obtaining the thread information corresponding to the thread switching event based on the device file, the method further includes: When the thread executes the target event, it is determined that the thread switching event exists, wherein the target event includes one of the following: the thread switches from the kernel mode to the user mode, the thread switches from the user mode to the kernel mode, or the thread is interrupted and preempted.
5. The method according to claim 1, characterized in that, The thread information corresponding to the thread switching event is obtained from the device file, including: If the thread switching event is determined to start execution through the third function in the device file, the hook function corresponding to the thread switching event is determined according to the device file; The thread information is obtained according to the hook function.
6. The method according to claim 1, characterized in that, After determining the first switching frequency corresponding to the thread switching event based on the thread information, the method further includes: The second number of thread switching events in the previous statistical period is determined based on the second thread information corresponding to the previous statistical period of the current statistical period; The second switching frequency corresponding to the thread switching event in the previous statistical period is determined based on the second quantity; Determine the magnitude relationship between the first switching frequency and the second switching frequency; An alarm mechanism is triggered when the size relationship indicates that the first switching frequency is greater than the second switching frequency.
7. A device for determining a switching frequency, characterized in that, include: A creation module is used to create device files and load statistics entries. The device files are used to indicate the communication interface between kernel mode and user mode. The load statistics entries are information to be obtained in the event of a thread switching event. The thread switching event is used to indicate the event in which a thread switches between kernel mode and user mode. The acquisition module is used to acquire thread information corresponding to the thread switching event according to the device file when it is determined that a thread switching event exists, wherein the thread information is information corresponding to the load statistics entry; The determining module is used to determine the first switching frequency corresponding to the thread switching event based on the thread information; The determining module is further configured to determine the statistical period corresponding to the thread switching event; determine the first number of thread switching events in the current statistical period based on the first thread information corresponding to the current statistical period; and determine the first switching frequency corresponding to the thread switching events in the current statistical period based on the first number.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps of the method according to any one of claims 1 to 6.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.