Switching frequency determination method and device, storage medium and electronic equipment

By creating device files and load statistics entries at the operating system kernel level, and monitoring thread switching events in real time, the problem of frequent kernel thread switching is solved, and system performance and CPU utilization is improved.

CN119311404AActive Publication Date: 2025-01-14INSPUR SUZHOU INTELLIGENT TECH CO LTD
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202411320360.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-20
Publication Date
2025-01-14
Estimated Expiration
2044-09-20

AI Technical Summary

Technical Problem

In the prior art, kernel thread switching events are not monitored in real time at the operating system kernel level, resulting in frequent kernel thread switching, affecting storage system performance and CPU utilization.

Method used

By creating device files and load statistics entries, obtaining information about thread switching events, calculating thread switching frequency, and allocating kernel space between kernel state and user state to store thread information, real-time monitoring of thread switching events is achieved.

Benefits of technology

Real-time monitoring of thread switching events at the operating system kernel level is realized, which reduces performance problems caused by frequent kernel thread switching and improves CPU utilization and storage system performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119311404A_ABST
    Figure CN119311404A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a switching frequency determination method and device, a storage medium and electronic device.The switching frequency determination method comprises the steps that an equipment file and a load statistics entry are created, the equipment file is used for indicating a communication interface between a kernel mode and a user mode, and the load statistics entry is used for counting the kernel mode and the user mode; the load statistics entry is to-be-acquired information under the condition that a thread switching event is executed, and the thread switching event is used for indicating an event that the thread is switched between the kernel mode and the user mode; when it is determined that a thread switching event exists, thread information corresponding to the thread switching event is obtained according to the device file, and 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. Through the embodiment of the invention, the problem that a method for monitoring the thread switching event in real time on the kernel layer of an operating system does not exist in the related technology can be solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computers, and in particular, to a method and device for determining a switching frequency, a storage medium, and an electronic device. Background Art

[0002] The performance of the operating system kernel directly determines the overall performance of the operating system. The key to the performance of the operating system lies in the kernel thread scheduling mechanism.

[0003] At present, the scheduling mechanism of the kernel layer of the operating system generally adopts a priority preemptive scheduling strategy. In the priority preemptive scheduling strategy, each kernel thread is assigned a priority, and the kernel thread with a high priority is scheduled first. However, the priority preemptive scheduling strategy will cause the problem of frequent kernel thread switching. The problem of frequent kernel thread switching may cause the following problems: frequent kernel thread switching reduces the cache resource utilization in the storage system, thereby affecting the performance of the storage system; frequent kernel thread switching will lead to large consumption of central processing unit (CPU) resources, increase CPU pressure, and reduce CPU utilization.

[0004] Therefore, it is necessary to effectively monitor kernel thread switching events to determine whether kernel threads switch frequently. However, there is no method for real-time monitoring of thread switching events at the operating system kernel level in the related art.

[0005] Therefore, the problem in the related art that there is no method for real-time monitoring of thread switching events at the operating system kernel level has not yet been effectively solved. Summary of the invention

[0006] The embodiments of the present application provide a method and device for determining a switching frequency, a storage medium, and an electronic device, so as to at least solve the problem in the related art that there is no method for monitoring thread switching events in real time at the operating system kernel level.

[0007] According to an embodiment of the present application, a method for determining a switching frequency is provided, specifically comprising: creating a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state; when it is determined that there is a thread switching event, 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 a device file and a load statistics entry, 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 a target space size of a target kernel space to be applied for according to the configuration parameters, wherein the target kernel space is used to store the acquired thread information; calling a second function according to the first interface to apply for a first kernel space of a target space size from a 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; determining the first kernel space as the target kernel space when determining that the return value is the first return value; releasing the kernel space corresponding to the kernel state when determining that the return value corresponding to the second function is the second return value, and calling the second function again according to the first interface to apply for a second kernel space of a 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; determining the second kernel space as the target kernel space when determining that the received return value sent by the space allocator is the first return value.

[0009] In an exemplary embodiment, after determining the first kernel space as the target kernel space or determining the second kernel space as the target kernel space, the method also includes: mapping the target kernel space to the user space corresponding to the user state to allow the first program corresponding to the user state 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 also includes: when the thread executes the target event, determining the existence of the thread switching event, wherein the target event includes one of the following: the thread switches from the kernel state to the user state, the thread switches from the user state to the kernel state, and the thread is preempted by an interrupt.

[0011] In an exemplary embodiment, obtaining thread information corresponding to the thread switching event according to the device file includes: when determining that the thread switching event starts to execute through a third function in the device file, determining a hook function corresponding to the thread switching event according to the device file; and obtaining the thread information according to the hook function.

[0012] In an exemplary embodiment, determining a first switching frequency corresponding to the thread switching event according to the thread information includes: determining a statistical period corresponding to the thread switching event; determining a first number of thread switching events within the current statistical period according to the first thread information corresponding to the current statistical period; and determining a first switching frequency corresponding to the thread switching event within the current statistical period according to the first number.

[0013] In an exemplary embodiment, after determining the first switching frequency corresponding to the thread switching event according to the thread information, the method also includes: determining a second number of thread switching events in the previous statistical period according to 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 event in the previous statistical period according to the second number; determining a size relationship between the first switching frequency and the second switching frequency; and triggering an alarm mechanism when the size relationship indicates that the first switching frequency is greater than the second switching frequency.

[0014] According to another embodiment of the present application, a switching frequency determination device is provided, comprising: a creation module, used to create a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state; an acquisition module, used to obtain thread information corresponding to the thread switching event according to the device file when it is determined that there is a thread switching event, wherein the thread information is information corresponding to the load statistics entry; and a determination module, used to determine a first switching frequency corresponding to the thread switching event according to the thread information.

[0015] According to another embodiment of the present application, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the steps in the above method embodiment when running.

[0016] According to another embodiment of the present application, an electronic device is 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 execute the steps in the above method embodiment.

[0017] According to another embodiment of the present application, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps in the above method embodiment are implemented.

[0018] Through the method for determining the switching frequency of the present application, a device file for indicating the communication interface between the kernel state and the user state is created, and a load statistical entry for indicating the information to be obtained when executing a thread switching event is created; when it is determined that there is a thread switching event in which a thread switches between the kernel state and the user state, thread information corresponding to the thread switching event is obtained according to the device file, and the first switching frequency corresponding to the thread switching event is determined according to the thread information. That is to say, the embodiment of the present application creates a device file and a load statistical entry, and when a thread switching event occurs, thread information corresponding to the thread switching event is obtained through the device file, and the thread information is the information corresponding to the load statistical entry. Then, the first switching frequency corresponding to the thread switching event is determined according to the thread information to realize the monitoring of the thread switching event. Through the embodiment of the present application, the problem that there is no method for real-time monitoring of thread switching events at the operating system kernel level in the related art can be solved. Then, through the embodiment of the present application, thread switching events can be monitored in real time at the operating system kernel level. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 It is a hardware structure block diagram of a computer terminal of a method for determining a switching frequency according to an embodiment of the present application;

[0020] Figure 2 is a flow chart of a method for determining a switching frequency according to an embodiment of the present application;

[0021] Figure 3 It is a flow chart of a storage system kernel thread switching tracking mechanism and alarm implementation method according to an optional embodiment of the present application;

[0022] Figure 4 It is a structural block diagram of a device for determining a switching frequency according to an embodiment of the present application. DETAILED DESCRIPTION

[0023] The embodiments of the present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0024] It should be noted that the terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.

[0025] The method embodiments provided in the embodiments of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Taking running on a computer terminal as an example, Figure 1 1 is a hardware structure block diagram of a computer terminal of a method for determining a switching frequency according to an embodiment of the present application. Figure 1 As shown, the computer terminal may include one or more ( Figure 1Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data, wherein the above-mentioned computer terminal may also include a transmission device 106 and an input / output device 108 for communication functions. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above-mentioned computer terminal. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations shown.

[0026] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the method for determining the interactive state in the embodiment of the present application. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, to implement the above method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories can be connected to the computer terminal via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0027] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of a computer terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0028] Figure 2 is a flow chart of a method for determining a switching frequency according to an embodiment of the present application, which can be applied to Figure 1 In a computer terminal, such as Figure 2 As shown, the process includes the following steps:

[0029] Step S202, creating a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state;

[0030] The load statistics entry may include: the start time and end time of the thread switching event, the system time for filling the load statistics entry, the system call number when the thread switching event occurs, etc.

[0031] Step S204, when it is determined that there is a thread switching event, 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;

[0032] It is understandable that the above thread information is information used to fill in the load statistics entry. For example, the load statistics entry includes the start time of the thread switching event. The thread information may be the specific start time of the thread switching event, for example, 10:00.

[0033] Step S206: determining a first switching frequency corresponding to the thread switching event according to the thread information.

[0034] Through the method for determining the switching frequency of the present application, a device file for indicating the communication interface between the kernel state and the user state is created, and a load statistical entry for indicating the information to be obtained when executing a thread switching event is created; when it is determined that there is a thread switching event in which a thread switches between the kernel state and the user state, thread information corresponding to the thread switching event is obtained according to the device file, and the first switching frequency corresponding to the thread switching event is determined according to the thread information. That is to say, the embodiment of the present application creates a device file and a load statistical entry, and when a thread switching event occurs, thread information corresponding to the thread switching event is obtained through the device file, and the thread information is the information corresponding to the load statistical entry. Then, the first switching frequency corresponding to the thread switching event is determined according to the thread information to realize the monitoring of the thread switching event. Through the embodiment of the present application, the problem that there is no method for real-time monitoring of thread switching events at the operating system kernel level in the related art can be solved. Then, through the embodiment of the present application, thread switching events can be monitored in real time at the operating system kernel level.

[0035] Optionally, after creating the device file and load statistics entry in the above step S202, the method also includes: obtaining the configuration parameters corresponding to the kernel state, and determining the first interface according to the 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 acquired thread information; calling the second function according to the first interface to apply for the 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 the return value sent by the space allocator; when it is determined that the return value is the first return value, determining the first kernel space as the target kernel space; when it is determined that the return value corresponding to the second function is the 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 the 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; when it is determined that the received return value sent by the space allocator is 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] According to the above embodiment, the specific solution for dynamically and securely allocating kernel space in the system for storing context information during thread switching includes:

[0038] 1) Obtain kernel state configuration parameters: It is necessary to obtain configuration parameters related to kernel thread switching events from the system or user space program. Configuration parameters may include: tracking granularity corresponding to kernel thread switching events, number of record entries, alarm thresholds, etc. Configuration parameters will guide the subsequent allocation and use of kernel space.

[0039] 2) Determine the first interface: Determine the interface (i.e., the first interface) for interacting with the kernel state through a specific function (i.e., the first function) in the device file. The first interface is a channel for communication between the user state and the kernel state, and commands can be sent and responses can be received through the first interface.

[0040] 3) Determine the target space size: Calculate the kernel space size (i.e., target space size) required based on the obtained configuration parameters. The target space size is determined based on 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) Apply for kernel space: Use the determined first interface to call the space allocator function (i.e., the second function) corresponding to the kernel state, and request to allocate kernel space of the target space size. In the Linux (an operating system) kernel of the x86 (a system architecture) architecture, the second function can be "_get_free_pages" (a function) or "kmalloc" (a function), etc. The space allocator will allocate a continuous memory block (the size of the memory block is the target space size) and return a pointer or status value (i.e., the return value).

[0042] 5) Process the return value: Receive the return value of the space allocator and check the return value. If the return value is a valid kernel space pointer (i.e., the first return value), it proves that the kernel space allocation is successful, and this space (i.e., the first kernel space) is determined as the target kernel space for storing thread information. If the allocation fails (i.e., the return value is the second return value), it is necessary to release the invalid space in the kernel space that has been attempted to be allocated after a period of time, and retry calling the second function to request the allocation of the target kernel space until the target kernel space is successfully allocated.

[0043] 6) Loop retry and space allocation: In the case of initial allocation failure, the space previously attempted to be allocated will be released after a period of time, and then the second function will be called again through the first interface to request allocation of the second kernel space of the target space size. If the allocation is successful this time and the first return value is received, the second kernel space is determined as the target kernel space for storing thread switching information.

[0044] The above technical solution can ensure the correct allocation and use of kernel space, and provide necessary memory resources for subsequent load statistics and thread switching event tracking. At the same time, the loop retry mechanism improves the adaptability of the system under memory shortage conditions.

[0045] Optionally, after determining the first kernel space as the target kernel space or determining the second kernel space as the target kernel space in the above step S204, the method also includes: mapping the target kernel space to the user space corresponding to the user state to allow the first program corresponding to the user state to access the target kernel space.

[0046] It can be understood that by mapping the target kernel space to the user space through the mapping mechanism in the above technical solution, the kernel space can be directly accessed in the user state to reduce the number of system calls and the thread switching overhead. That is, through memory mapping, the user state program can directly access the kernel data, thereby 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 the above step S204, the method also includes: when the thread executes the target event, determining that the thread switching event exists, wherein the target event includes one of the following: the thread switches from the kernel state to the user state, the thread switches from the user state to the kernel state, and the thread is preempted by an interrupt.

[0048] Among them, thread interruption preemption means that in the operating system, when a thread is running, due to the occurrence of hardware interrupts (for example: network data packet arrival, timer interrupt, etc.) or software interrupts (for example: system call, signal processing, etc.), the currently running thread is suspended and the control is temporarily handed over to the kernel state. After the kernel state handles the interrupt event, it may reschedule the thread and select another thread to run.

[0049] Optionally, the above-mentioned step S206 of obtaining thread information corresponding to the thread switching event according to the device file includes: when it is determined that the thread switching event starts to be executed through a third function in the device file, determining a hook function corresponding to the thread switching event according to the device file; and obtaining the thread information according to the hook function.

[0050] It is understandable that when the user space program calls the third function in the device file, the kernel receives a signal, which is the beginning of the thread switch record. The kernel determines the hook function to be called based on the configuration of the device file. 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 of the current thread switch, including but not limited to: thread identifier (TID): used to uniquely identify a thread; switch timestamp: records the specific time when the thread switch occurs, which is used for subsequent time statistics and analysis; thread state: records the state before and after the thread 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 is helpful for troubleshooting and performance analysis.

[0051] Through the hook function, the kernel state can dynamically capture thread switching events and record detailed thread information, which is very important for performance monitoring and system stability analysis.

[0052] Optionally, the above-mentioned step S206 of determining the first switching frequency corresponding to the thread switching event according to 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 according to the first thread information corresponding to the current statistical period; and determining the first switching frequency corresponding to the thread switching event within the current statistical period according to the first number.

[0053] Among them, after determining the first switching frequency corresponding to the thread switching event according to the thread information, the method also includes: determining the second number of thread switching events in the previous statistical period according to 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 according to the second number; determining the size relationship between the first switching frequency and the second switching frequency; and triggering an alarm mechanism when the size relationship indicates that the first switching frequency is greater than the second switching frequency.

[0054] It can be understood that the technical solution for determining the first switching frequency and after determining the first switching frequency mainly includes:

[0055] (1) Determine a first switching frequency according to 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 every second or every minute, or it can be triggered based on a specific event. During this period, the system will record all thread switching events that occur.

[0057] 2) Determine the first number: The first number refers to the number of thread switching events that occur in the current statistical period. The system needs to have a mechanism to accurately record the occurrence of each thread switch, including the switched thread ID, the switching time and other information.

[0058] 3) Calculating the first switching frequency: By dividing the first number by the duration of the current statistical period, the thread switching frequency in the current period can be obtained, which reflects the frequency of thread switching in the current statistical period.

[0059] (2) Determine the second switching frequency based on historical data.

[0060] After determining the first switching frequency, it is also necessary to determine the second switching frequency based on the data of the previous statistical cycle. Specifically:

[0061] 1) Determine the second number: The second number is the number of thread switching events that occurred in the last statistical period. This also requires the system to record all thread switching information in the last period.

[0062] 2) Calculating the second switching frequency: By dividing the second number by the duration of the previous statistical cycle, the thread switching frequency in the previous cycle can be obtained.

[0063] (3) Compare frequencies and trigger alarms.

[0064] 1) Determine the magnitude relationship: compare the first switching frequency of the current statistical period with the second switching frequency of the previous statistical period to determine the magnitude relationship between the two.

[0065] 2) Triggering the alarm mechanism: If the first switching frequency is greater than the second switching frequency and exceeds the set threshold (i.e., the ratio between the first switching frequency and the second switching frequency exceeds the preset reference frequency), the alarm mechanism is triggered. This indicates that the thread switching in 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 the system administrator or monitoring system to pay attention to the current thread switching situation. Measures may need to be taken to optimize the system, such as adjusting the scheduling strategy, reducing unnecessary thread creation or system calls, optimizing the lock strategy, etc., to avoid performance degradation caused by frequent thread switching.

[0067] In summary, by comparing the thread switching frequencies in different cycles, the system performance can be dynamically monitored and potential performance bottlenecks can be discovered in a timely manner, which helps to improve the stability and response speed of the system.

[0068] In order to better understand the process of the above-mentioned method for determining the switching frequency, the implementation process of the above-mentioned method for determining the switching frequency is described below in combination with an optional embodiment, but it is not used to limit the technical solution of the embodiment of the present application.

[0069] The key terms involved in the optional embodiments of this application include:

[0070] 1) Kernel Mode and User Mode: Kernel Mode and User Mode are two important CPU working states in computer operating systems. They have significant differences in permissions, resource access, and execution of instructions. Among them, Kernel Mode: can execute all types of instructions, including instructions that may affect system stability. This is because the kernel needs to comprehensively manage and control the system. User Mode: can only execute non-privileged instructions. For operations that require the execution of privileged instructions, user programs must request the help of the kernel through system calls and other methods. User Mode and Kernel Mode can be converted to each other. The conversion between Kernel Mode and User Mode includes:

[0071] (1) User state to kernel state: When a user state program requests kernel services through system calls, interrupts, or exceptions, it triggers a transition from user state to kernel state. This transition is one-way and consumes certain system resources.

[0072] (2) Kernel state to user state: When the kernel completes the service requested by the user, it will actively return control to the user state program. This conversion is also one-way and controlled by the kernel.

[0073] That is, kernel state and user state are two important CPU working states in the operating system, and they have significant differences in permissions, resource access, instruction execution, etc. 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 part of the CPU. It stores the memory address of the instruction that the CPU is about to execute. By modifying the value of the EIP register, program flow control, function call and return, and exception handling can be implemented.

[0075] When the Linux system processes a system call or interrupt, the kernel allocates space for the register context structure (Processor Registers Context Structure, referred to as pt_regs structure) on a specific stack (for example, the kernel stack) and accesses this structure through pointers or macros. For example, on the x86 architecture, you can get the pt_regs structure pointer of the current task through the current_pt_regs() macro (this is not a standard macro, the specific name may vary depending on the kernel version and architecture). But in most cases, this pointer is implicitly handled by the assembly code and the kernel's context management mechanism.

[0076] The optional embodiment of the present application provides a storage system kernel thread switching tracking mechanism and alarm implementation method. Kernel thread switching recording and tracking are implemented through modules such as initialization module, switching recording module, interface call, load statistics and alarm reporting, and the current system switching load is evaluated through the load statistics module. If it exceeds the average threshold, an alarm will be reported. When the system starts or the module is loaded, the necessary resources are initialized, such as memory allocation, data structure initialization, and configuration reading. When the kernel thread switches, relevant information is recorded, such as: switching time, source thread identification (ID), target thread ID, etc. A character device is implemented for communicating with the user space. The user space program can send commands through this interface, such as querying the current record, clearing the record, etc. After receiving the command, the kernel module performs the corresponding operation and returns the result through the interface. The thread switching frequency in the system is regularly counted to evaluate the system load. Compare with the preset threshold to evaluate whether the system load is too high.

[0077] Figure 3 is a flow chart of a storage system kernel thread switching tracking mechanism and alarm implementation method according to an optional embodiment of the present application, such as Figure 3 As shown:

[0078] Among them, step S301 to step S306 are steps implemented by the initialization module.

[0079] Step S301, determine to enable the switching recording function.

[0080] The switch record function is a kernel-level technical implementation that is used to capture and record kernel thread switch events. It involves modifying the kernel scheduler, registering specific hook functions, and automatically calling these functions to record relevant context information when the kernel thread switches, such as switch time, thread ID, system call number, etc.

[0081] Step S302, creating a device file.

[0082] Create a character device file for the switching module, implement the operation interface of the device file (for example, read, write, control, etc.), and convert these operations into specific control instructions for the kernel to the hardware device.

[0083] Step S303: determine the kernel interface function for implementing the switching record function.

[0084] That is, the interface functions corresponding to the operation interface of the device file mainly include:

[0085] 1) kernel_trace_buffer_mmap: Apply for kernel space memory and map it to user space, map the contents of the trace buffer to a specific interface in user space. Used to efficiently transfer trace data from kernel space to user space for further analysis and processing. Get the configuration parameters of the switch record and calculate the memory size to be applied for (i.e., the target space size). Then use the application_get_free_pages (free page) memory from the kernel page allocator (i.e., space allocator). Check the return value. If it is NULL (i.e., the second return value), it means that the memory allocation failed. Handle the error according to the specific situation and return an error code, release the allocated resources, retry, etc. Use the allocated memory. If the memory allocation is successful, use the memory block to store data. In the module unloading function, use free_pages (a function) and other functions to release the currently applied memory.

[0086] 2) Implement kernel_trace_ioctl function: ioctl (Input / Output Control) in the kernel is a system call that implements two-way communication between the device driver and the user space program. The device characteristics are controlled by the control commands to the device. The user space program interface implements the functions and operations specific to the control device by sending control commands to the device driver and then receiving data from the driver. Return value: 0 on success, -1 on failure and set the corresponding error code. Detailed functions may include:

[0087] (1) start_tracing function (i.e., the third function): starts the switching recording by registering a recording start function to the kernel scheduling function.

[0088] (2) copy_from_user function: used to obtain parameter information of the user-mode module;

[0089] (3) kernel_tracer function: The main function of this function is to track and record activities in the kernel.

[0090] (4) preempt_cOunt_inc function: used to increase the preemption count of the current thread to ensure that the kernel will not be preempted when the subsequent code segment is executed.

[0091] (5) stop_tracing function: This function registers a recording stop function to the kernel scheduling function to end the switching recording.

[0092] Among them, the current thread information is represented by a task_struct (a structure), which contains various status information of the process, such as register values, memory addresses, scheduling information, etc. When the process switches from user mode to kernel mode, the kernel needs to save the current register state so that it can resume execution after processing the kernel mode task. At this time, the pt_regs structure is used to save these register states. The pt_regs structure is associated with specific contexts (for example, interrupt context, system call context) and is saved and restored through the kernel stack operation when these contexts are activated. The kernel allocates space for the pt_regs structure on the kernel stack and accesses this structure through pointers or macros.

[0093] Step S304, registering the hook function pointer variable.

[0094] The hook function is registered through the kernel interface function to realize the tracking and recording of thread switching events through the hook function.

[0095] Step S305: Create a load statistics entry structure.

[0096] Apply for memory space of corresponding size according to 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 filled;

[0098] 2) Last_entry_time (a field): used to record the time when the last record was recorded;

[0099] 3) Start_time (a field): used to record the start time of thread switching;

[0100] 4) Cpu_id (a field): records the CPUID (i.e., the identifier of the central processing unit) where the current thread switch occurs;

[0101] 5) Cur_pid (a field): the PID (process identifier) ​​number of the thread where the switch occurred;

[0102] 6) Parent_pid (a field): records the PID number of the parent process of the current switching thread;

[0103] 7) System_call (a field): the system call number where the switch occurs;

[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 recording module is running, if the thread is switched, the structure of the load statistics entry is filled according to the driver interface.

[0107] In addition, the switching recording module can achieve the following process:

[0108] Each time a system call is made in the Linux operating system, a task switch will occur. The kernel scheduling function implemented by this method will start switching the tracking function of the recording module each time it is scheduled. The switching module is turned on and off by enabling and disabling it through the upper-layer application.

[0109] The following behaviors of the operating system trigger the switch recording module to record thread switching information:

[0110] 1) The task switches from kernel state to user state;

[0111] 2) The task switches from user mode to kernel mode;

[0112] 3) The process is interrupted and preempted;

[0113] When the above scenario occurs, the following key actions will be performed:

[0114] The switching record module records the number of clock cycles for each task execution: one clock cycle is read when the task starts executing, and the clock cycle number is read again when the task ends executing. The difference between the two clock cycle numbers is the running time of the task. The time difference is saved in the Run time in step S305.

[0115] When the thread task is switched, the type of task is determined, and the system call number is obtained through the value of the orig_eax register (a register) to record the system call of the thread task switch. The system call number is stored in System_call in step S305.

[0116] The system time of thread switching is calculated by obtaining the current clock ticks (clock cycle) and combining the CPU frequency, and the system time is saved in the Current time in step S305.

[0117] The PID of the process and the PID of the parent process are obtained through the process context and recorded in Cur_pid and Parent_pid in step S305.

[0118] The address of the instruction causing the interruption or exception is obtained by reading the instruction pointer register of EIP and recorded in the EIP in step S305.

[0119] Step S306 is implemented by the load statistics module.

[0120] Step S306, determining whether the current load has increased unnecessarily by running the load statistics module.

[0121] The memory space requested by the switching record module is used cyclically during operation. After each round of space usage is completed, the operation of the load statistics module will be triggered. The module responsible for statistics will traverse the load statistics entries and count the number of thread switches per unit time according to different time granularities (for example: ms, us, ns). The data within a statistical period (pre_status_cycles, that is, the previous statistical period) is used to calculate a reference frequency of the switching frequency (that is, the second switching frequency). The number of cycles can be configured through kernel parameters. One cycle represents a cycle of the memory space requested by the switching record module. After the cycles are completed in sequence, the load statistics module will calculate the frequency of the current cycle (that is, the current statistical period) (that is, the first switching frequency), and compare this frequency with the reference frequency. If it exceeds the reference frequency threshold ratio frequency_ratio (configurable), it will trigger the alarm module to report the alarm, and then update the reference frequency.

[0122] The base frequency is generated dynamically. After each cycle, the earliest record is deleted in pre_status_cycles (a function), and then the latest record is added to recalculate the base frequency.

[0123] If it is determined that the current load has increased unnecessarily, step S307 is executed; if it is determined that the current load has not increased unnecessarily, step S306 is executed.

[0124] Step S307 is implemented by the load alarm module.

[0125] Step S307: When it is determined that the current load has increased unnecessarily, that is, when the thread switching frequency is greater than the reference frequency, an alarm is issued.

[0126] The storage software stack reports an alarm to remind the system that the process switching load is too high, and to warn of performance impact.

[0127] The optional embodiment of the present application provides a storage system kernel thread switching tracking mechanism and alarm implementation method. Kernel thread switching recording and tracking are achieved through switching recording modules, interface calls, load statistics and alarm reporting modules, and the current system switching load is evaluated through the load statistics module. If it exceeds the average threshold, an alarm will be reported. As a mechanism for performance warning, it reminds users to reasonably distribute business pressure. It is convenient for performance monitoring and evaluation, enhances system observability, and improves system stability. It can also be used for troubleshooting and diagnosis.

[0128] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present application.

[0129] In this embodiment, a switching frequency determination device is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware of a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.

[0130] Figure 4 is a structural block diagram of a device for determining a switching frequency according to an embodiment of the present application, such as Figure 4 As shown, the device comprises:

[0131] A creation module 42, configured to create a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state;

[0132] The acquisition module 44 is used to obtain the thread information corresponding to the thread switching event according to the device file when it is determined that there is a thread switching event, 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] Through the switching frequency determination device of the present application, a device file for indicating the communication interface between the kernel state and the user state is created, and a load statistical entry for indicating the information to be obtained when executing a thread switching event is created; when it is determined that there is a thread switching event in which a thread switches between the kernel state and the user state, thread information corresponding to the thread switching event is obtained according to the device file, and the first switching frequency corresponding to the thread switching event is determined according to the thread information. That is to say, the embodiment of the present application creates a device file and a load statistical entry, and when a thread switching event occurs, thread information corresponding to the thread switching event is obtained through the device file, and the thread information is the information corresponding to the load statistical entry. Then, the first switching frequency corresponding to the thread switching event is determined according to the thread information to realize the monitoring of the thread switching event. Through the embodiment of the present application, the problem that there is no method for real-time monitoring of thread switching events at the operating system kernel level in the related art can be solved. Then, through the embodiment of the present application, thread switching events can be monitored in real time at the operating system kernel level.

[0134] In an exemplary embodiment, the creation module 42 is also used to obtain the configuration parameters corresponding to the kernel state, and determine the first interface according to the 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 acquired thread information; call the second function according to the first interface to apply for the 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 the return value sent by the space allocator; when it is determined that the return value is the first return value, determine the first kernel space as the target kernel space; when it is determined that the return value corresponding to the second function is the 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 the 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; when it is determined that the received return value sent by the space allocator is 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 state, so as to allow the first program corresponding to the user state to access the target kernel space.

[0136] In an exemplary embodiment, the acquisition module 44 is also used 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 state to the user state, the thread switches from the user state to the kernel state, and the thread is preempted by an interrupt.

[0137] In an exemplary embodiment, the acquisition module 44 is further used to determine a hook function corresponding to the thread switching event according to the device file when it is determined that the thread switching event starts to execute through a third function in the device file; and acquire the thread information according to the hook function.

[0138] In an exemplary embodiment, the determination module 46 is also used to determine the statistical period corresponding to the thread switching event; determine 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 determine the first switching frequency corresponding to the thread switching event within the current statistical period based on the first number.

[0139] In an exemplary embodiment, the determination module 46 is also used 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 when the size relationship indicates that the first switching frequency is greater than the second switching frequency.

[0140] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps in the above method embodiment when running.

[0141] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk.

[0142] Optionally, in this embodiment, the storage medium may be configured to store program codes for executing the following steps:

[0143] S1, creating a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state;

[0144] S2, when it is determined that there is a thread switching event, 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;

[0145] S3: Determine a first switching frequency corresponding to the thread switching event according to the thread information.

[0146] An embodiment of the present application further provides an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute the steps in the above method embodiment.

[0147] In an 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 may be configured to perform the following steps through a computer program:

[0149] S1, creating a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state;

[0150] S2, when it is determined that there is a thread switching event, 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;

[0151] S3: Determine a first switching frequency corresponding to the thread switching event according to the thread information.

[0152] An embodiment of the present application further provides a computer program product, including a computer program, which is implemented to perform the steps in the above method embodiment when executed by a processor.

[0153] Optionally, in this embodiment, the computer program product may be executed by a processor to perform the following steps:

[0154] S1, creating a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state;

[0155] S2, when it is determined that there is a thread switching event, 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;

[0156] S3: Determine a first switching frequency corresponding to the thread switching event according to the thread information.

[0157] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.

[0158] Obviously, those skilled in the art should understand that the above modules or steps of the present application can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order from that herein, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present application is not limited to any specific combination of hardware and software.

[0159] The above description is only the preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the principles of the present application shall be included in the protection scope of the present application.

Claims

1. A method for determining a switching frequency, characterized in that: include: Creating a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state; In the case of determining that there is a thread switching event, 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; A first switching frequency corresponding to the thread switching event is determined according to the thread information.

2. The method according to claim 1, characterized in that After creating the device file and the load statistics entry, the method further includes: Acquire configuration parameters corresponding to the kernel state, and determine a first interface according to a first function in the device file; Determining a target space size of a target kernel space to be applied for according to the configuration parameters, wherein the target kernel space is used to store the acquired thread information; Calling a second function according to the first interface to apply for a first kernel space of a target space size from a 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 the return value sent by the space allocator; In a case where it is determined that the return value is a first return value, determining the first kernel space as the target kernel space; In the case of determining that the return value corresponding to the second function is the second return value, releasing the kernel space corresponding to the kernel state, and calling the second function again according to the first interface, so as 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; In a case where it is determined that the received return value sent by the space allocator is the first return value, the second kernel space is determined as 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 determining 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 state, so as to allow the first program corresponding to the user state to access the target kernel space.

4. The method according to claim 1, characterized in that: Before acquiring the thread information corresponding to the thread switching event according to the device file, the method further includes: In the case where the thread executes a 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 state to the user state, the thread switches from the user state to the kernel state, and the thread is preempted by an interrupt.

5. The method according to claim 1, characterized in that Acquiring thread information corresponding to the thread switching event according to the device file includes: In the case where it is determined through the third function in the device file that the thread switching event starts to be executed, determining a hook function corresponding to the thread switching event 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 Determining a first switching frequency corresponding to the thread switching event according to the thread information includes: Determine the statistical period corresponding to the thread switching event; Determine a first number of thread switching events in the current statistical period according to first thread information corresponding to the current statistical period; A first switching frequency corresponding to the thread switching events in the current statistical period is determined according to the first number.

7. The method according to claim 6, characterized in that After determining the first switching frequency corresponding to the thread switching event according to the thread information, the method further includes: Determining a second number of thread switching events in a previous statistical period according to second thread information corresponding to a previous statistical period of the current statistical period; Determine a second switching frequency corresponding to the thread switching event in the previous statistical period according to the second number; determine a magnitude relationship between the first switching frequency and the second switching frequency; When the magnitude relationship indicates that the first switching frequency is greater than the second switching frequency, an alarm mechanism is triggered.

8. A device for determining a switching frequency, characterized in that: include: A creation module is used to create a device file and a load statistics entry, wherein the device file is used to indicate a communication interface between a kernel state and a user state, and 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 state and the user state; an acquisition module is used to obtain thread information corresponding to the thread switching event according to the device file when it is determined that there is a thread switching event, wherein the thread information is information corresponding to the load statistics entry; and a determination module is used to determine a first switching frequency corresponding to the thread switching event according to the thread information.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program implements the steps of the method according to any one of claims 1 to 7 when executed by a processor.

10. 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, the steps of the method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Method for realizing CANopen slave station

    CN101854300A

  • Thread anomaly diagnosis method and device

    CN112905372A

  • Interrupt driver model detection method based on thread loop scheduling sequentialization

    CN115033472A

  • Load balancing method, computing device and storage medium

    CN115421927A

  • CPU dynamic allocation program, CPU dynamic allocation device and CPU dynamic allocation method

    JP2018045493A