Occupancy rate determination method and device, storage medium and electronic equipment
By configuring a counter for each CPU core, calculating its occupancy rate and optimizing task allocation, the problem of poor CPU occupancy statistics in the prior art is solved, and task execution efficiency and stability in a multi-core CPU environment are improved.
Patent Information
- Application Number
- CN202411363410.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2024-12-31
AI Technical Summary
The existing technology lacks an efficient CPU occupancy statistics scheme, and it is impossible to effectively obtain the CPU occupancy statistics of each task on a multi-core CPU, which affects the allocation of tasks on a multi-core CPU and efficient execution in real time.
By configuring a counter for each CPU core, the maximum count, number of flips and initial/end count values of the counter are obtained, combined with the idle usage time, the total occupancy rate of the CPU core is calculated, and the task occupancy rate is determined based on the task execution time, dynamic statistics and Match appropriate CPU cores to optimize task allocation.
It realizes accurate occupancy statistics for each CPU core and task, improves the stability and real-time nature of the micro-kernel operating system, can promptly notify overload conditions, and improves task execution efficiency and reliability.
Smart Images

Figure CN119226111A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a method and device for determining occupancy rate, a storage medium, and an electronic device. Background Art
[0002] The operating system kernel architecture is mainly divided into macro kernel architecture and micro kernel architecture. Most of the early operating systems were Monolithic Kernel, which means that the entire operating system including Scheduling, Filesystem, Networking, Devicedriver, Memorymanagement, and Paging are all completed in the kernel. Until now, the widely used operating systems, such as UNIX, Linux, and Windows, are still mostly macro kernel operating systems. However, as the operating system becomes more and more complex, putting all these functions in the kernel makes the design difficulty increase rapidly, and the real-time performance of the operating system will also decrease.
[0003] The microkernel is a design concept opposite to the macrokernel. Its purpose is to shrink the kernel to the minimum and remove all possible functional modules from the kernel. Ideally, only Address Space Support, IPC (Inter-Process Communication) and Scheduling are left in the kernel, and other functional modules run as user processes. For the kernel, they are no different from general user processes. They communicate with other user processes through IPC. At the same time, modularization facilitates the addition and deletion of system functions, has a certain degree of flexibility, and will not cause the entire system to crash due to the modification of a function or the occurrence of an error, which greatly improves the reliability of the system.
[0004] CPU occupancy rate usually refers to the ratio of the time the CPU processes non-idle tasks to the total running time. The CPU occupancy rate of each non-idle task refers to the ratio of the time the task is processed on the CPU to the specified total running time. Microkernel operating systems support running multiple tasks on multi-core processors, but current microkernel operating systems cannot obtain CPU occupancy statistics for each task on each CPU core, and there is no task management solution for reasonably allocating tasks on multi-core CPUs and executing them in real time and efficiently.
[0005] In view of the technical problem that there is a lack of efficient statistical scheme for CPU occupancy in the prior art, no effective solution has been proposed yet. Summary of the invention
[0006] The embodiments of the present application provide a method and device for determining CPU occupancy, a storage medium, and an electronic device to at least solve the technical problem in the related art that the prior art lacks an efficient statistical solution for CPU occupancy.
[0007] According to an embodiment of the present application, a method for determining occupancy is provided, including: obtaining parameter information of a counter corresponding to a first CPU core, and obtaining idle usage time of the first CPU core performing idle tasks, wherein the first CPU core is any CPU core among multiple CPU cores, and the parameter information includes: the maximum count of the counter, the number of rollovers of the counter, the end count value of the counter at the end of counting, and the initial count value of the counter; the total occupancy of the first CPU core is determined based on the parameter information and the idle usage time.
[0008] In an exemplary embodiment, determining the total occupancy of the first CPU core according to the parameter information and the idle usage time includes: determining the total usage time of the first CPU core according to the parameter information by the following formula: utilization_total_i=count_max*num_overflow+count_end-count_start, wherein utilization_total_i is the total usage time, count_max is the maximum count, num_overflow is the number of flips, count_end is the end count value, and count_start is the initial count value; determining the total occupancy by the following formula: r_total_i=1-utilisation_IDLE_i / utilization_total_i, wherein r_total_i is the total occupancy and utilization_IDLE_i is the idle usage time.
[0009] In an exemplary embodiment, after obtaining parameter information of a counter corresponding to a first CPU core, the method further includes: obtaining a task execution time of a first task of the first CPU core, wherein the first task is any one of a plurality of tasks executed by the first CPU core; and determining a task occupancy rate of the first task in the first CPU core based on the task execution time and the parameter information.
[0010] In an exemplary embodiment, the task occupancy rate of the first task in the first CPU core is determined according to the task execution time and the parameter information, including: determining the total usage time of the first CPU core according to the parameter information by the following formula: utilization_total_i=count_max*num_overflow+count_end-count_start, wherein utilization_total_i is the total usage time, count_max is the maximum count, num_overflow is the number of flips, count_end is the end count value, and count_start is the initial count value; and determining the ratio of the task execution time to the total usage time as the task occupancy rate of the first task in the first CPU core.
[0011] In an exemplary embodiment, obtaining the task execution time of the first task of the first CPU core includes: in the case of detecting that the first task switches to the second task, obtaining the target parameter information of the counter at the first moment, wherein the first moment is the moment of switching to the second task, and the target parameter information includes: the maximum count, the first flip number of the counter at the first moment, the second flip number of the counter at the second moment, the first count value of the counter at the first moment, the second count value of the counter at the second moment, the second moment being the moment with the smallest time difference from the first moment among multiple third moments of switching to the first task, and the multiple third moments are all earlier than the first moment; determining whether the counter has flipped within a first time period according to the first flip number and the second flip number, wherein the start time of the first time period is the second moment, and the end time of the first time period is the first moment; after determining that the counter has flipped within the first time period When the counter has not flipped within the time period, the difference between the first count value and the second count value is determined as the first task execution time, and the sum of the first task execution time and the second task execution time is determined as the task execution time, wherein the second task execution time is the task execution time of the first task within the second time period, the start time of the second time period is the start time of the counter, and the end time of the second time period is the second moment; when it is determined that the counter has flipped within the first time period, the first task execution time is calculated by the following formula: time=count_max-start_time+count, and the sum of the first task execution time and the second task execution time is determined as the task execution time, wherein time is the first task execution time, count_max is the maximum count, start_time is the second count value, and count is the first count value.
[0012] In an exemplary embodiment, after determining the total occupancy of the first CPU core according to the parameter information and the usage time, the method further includes: when the total occupancy of each CPU core in the multiple CPU cores has been obtained, determining a first list, a second list, and a third list, wherein the first list includes: identification information of CPU cores whose total occupancy is less than a first threshold, the second list includes: identification information of CPU cores whose total occupancy is less than a second threshold and greater than the first threshold, and the third list includes: identification information of CPU cores whose total occupancy is greater than the second threshold.
[0013] In an exemplary embodiment, the method also includes: when receiving a task creation request, determining whether the task creation request carries a core binding parameter, wherein the core binding parameter is used to indicate the CPU core to which the fourth task corresponding to the task creation request is to be bound; when the task creation request carries the core binding parameter, binding the fourth task to the second CPU core indicated by the core binding parameter; when the task creation request does not carry the core binding parameter, matching the third CPU core in the multiple CPU cores according to the first list and the second list, and when matching the third CPU core, binding the fourth task to the third CPU core.
[0014] In an exemplary embodiment, after determining the first list, the second list and the third list, the method further includes: determining the third CPU core indicated by the third list; obtaining the task occupancy rate of each of the multiple fifth tasks of the third CPU core; and determining a processing method for the third CPU core based on the multiple task occupancy rates, wherein the processing method is used to reduce the total occupancy rate of the third CPU core.
[0015] According to another embodiment of the embodiments of the present application, a device for determining occupancy is also provided, including: an acquisition module, used to obtain parameter information of a counter corresponding to a first CPU core, and obtain idle usage time of the first CPU core performing idle tasks, wherein the first CPU core is any CPU core among multiple CPU cores, and the parameter information includes: the maximum count of the counter, the number of rollovers of the counter, the end count value of the counter at the end of counting, and the initial count value of the counter; a determination module, used to determine the total occupancy of the first CPU core based on the parameter information and the idle usage time.
[0016] According to another aspect of the embodiments 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 above-mentioned occupancy determination method when running.
[0017] According to another aspect of an embodiment of the present application, there is also provided an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the occupancy determination method through the computer program.
[0018] In an embodiment of the present application, parameter information of a counter in a first CPU core is first obtained, as well as the idle usage time of the first CPU core executing an idle task. The first CPU core is any CPU core among multiple CPU cores in a microkernel operating system, and the parameter information includes: a maximum count value of the counter, a rollover number of the counter, an end count value of the counter at the end of counting, and an initial count value of the counter. Then, the total CPU occupancy rate of the first CPU core can be determined based on these parameter information and the idle usage time. By adopting the above scheme, a counter is configured for each CPU core, and the occupancy rate of the CPU core is quickly counted according to preset rules. This solves the problem in the related art that there is a lack of efficient CPU occupancy rate statistical schemes in the prior art. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0020] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0021] Figure 1 It is a hardware environment diagram of a method for determining occupancy rate according to an embodiment of the present application;
[0022] Figure 2 is a flow chart of a method for determining an occupancy rate according to an embodiment of the present application;
[0023] Figure 3 is a schematic diagram of a multi-CPU core architecture according to an embodiment of the present application;
[0024] Figure 4 It is an overall block diagram of a statistical solution for multi-core and multi-task CPU occupancy according to an embodiment of the present application;
[0025] Figure 5 It is a flowchart of assigning tasks to CPU cores according to an embodiment of the present application;
[0026] Figure 6 It is a flowchart of a task multi-core overload warning solution according to an embodiment of the present application;
[0027] Figure 7 It is a structural block diagram of a device for determining occupancy rate according to an embodiment of the present application. DETAILED DESCRIPTION
[0028] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.
[0029] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, subsystem, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0030] According to one aspect of an embodiment of the present application, a method for determining occupancy is provided. The method for determining occupancy is widely used in scenarios such as software system operation. Optionally, in this embodiment, the method for determining occupancy can be applied to Figure 1 In the hardware environment composed of the terminal device 102 and the server 104 shown in FIG. Figure 1 As shown, the server 104 is connected to the terminal device 102 via a network, and can be used to provide services (such as application services, etc.) for the terminal or a client installed on the terminal. A database can be set on the server or independently of the server to provide data storage services for the server 104. Cloud computing and / or edge computing services can be configured on the server or independently of the server to provide data computing services for the server 104.
[0031] The above network may include but is not limited to at least one of the following: wired network, wireless network. The above wired network may include but is not limited to at least one of the following: wide area network, metropolitan area network, local area network, and the above wireless network may include but is not limited to at least one of the following: WIFI (Wireless Fidelity), Bluetooth. The terminal device 102 may not be limited to smart devices such as PCs, mobile phones, tablet computers, and engineering equipment such as motors.
[0032] In this embodiment, a method for determining occupancy rate is provided, which is applied to the above-mentioned microkernel operating system. Figure 2is a flow chart of a method for determining an occupancy rate according to an embodiment of the present application, the flow chart comprising the following steps:
[0033] Step S202, obtaining parameter information of a counter corresponding to a first CPU core, and obtaining an idle usage time of the first CPU core executing an idle task, wherein the first CPU core is any CPU core among multiple CPU cores, and the parameter information includes: a maximum count of the counter, a rollover number of the counter, an end count value of the counter when counting ends, and an initial count value of the counter;
[0034] like Figure 3 As shown in the figure, each core of the multi-core CPU is equipped with a private counter counter. When the operating system starts, the counter on each core is configured and started. The counter counts up according to the time interval t. When the count reaches the maximum value, an overflow interrupt is generated and the count continues to accumulate from 0. In the task control block structure of the microkernel operating system, a CPU occupancy statistics structure track is added. The structure consists of a single task CPU occupancy count utilisation, a start time start_time, and a counter overflow number num_overflow.
[0035] Modern high-performance CPUs have single-core and multi-core architectures. Microkernel operating systems support single-core startup and multi-core SMP (Symmetric Multi-Processing) symmetric multi-processing startup. After starting the microkernel operating system on a single-core CPU, create a monitor statistical task and an IDLE idle task (i.e., the idle task mentioned above). On a multi-core CPU architecture, it is necessary to create a monitor_n statistical task and an IDLE_n idle task respectively after starting the microkernel operating system on each core. The monitor monitoring task needs to be set to a higher priority than all statistical tasks, and the IDLE idle task needs to be set to the lowest priority. The IDLE task is a special processing task. After the CPU enters the IDLE task, it does not perform any calculations but only waits for other tasks to execute, that is, the CPU is in an idle state.
[0036] Step S204: determine the total occupancy rate of the first CPU core according to the parameter information and the idle usage time.
[0037] Through the above steps, the parameter information of the counter in the first CPU core is first obtained, and the idle usage time of the first CPU core executing the idle task is obtained. The first CPU core is any CPU core in the multiple CPU cores in the microkernel operating system, and the parameter information includes: the maximum count value of the counter, the number of rollovers of the counter, the end count value of the counter at the end of counting, and the initial count value of the counter; then, the total CPU occupancy rate of the first CPU core can be determined according to these parameter information and the idle usage time; by adopting the above scheme, by configuring a counter for each CPU core, the occupancy rate of the CPU core is quickly counted according to preset rules; thereby solving the problem in the related technology that there is a lack of efficient CPU occupancy rate statistical scheme in the prior art.
[0038] Optionally, determining the total occupancy of the first CPU core according to the parameter information and the idle usage time includes: determining the total usage time of the first CPU core according to the parameter information by the following formula: utilization_total_i=count_max*num_overflow+count_end-count_start, wherein utilization_total_i is the total usage time, count_max is the maximum count, num_overflow is the number of flips, count_end is the end count value, and count_start is the initial count value; determining the total occupancy by the following formula: r_total_i=1-utilisation_IDLE_i / utilization_total_i, wherein r_total_i is the total occupancy and utilization_IDLE_i is the idle usage time.
[0039] The statistical task monitor is performed according to the specified statistical interval s (usually in seconds). When the statistical task starts, all statistical variable values are cleared and the counter value is recorded as count_start. When the statistics are completed, the counter value is recorded as count_end. When the statistics are completed, the total utilization time of the current CPU core i is utilization_total_i = count_max*num_overflow+count_end-count_start; the idle utilization_idle_i time of the current CPU core i is the IDLE idle task execution time IDLE.track.utilisation; the total CPU occupancy rate on the current CPU core i is r_total_i = 1-utilisation_IDLE_i / utilization_total_i; the total occupancy rate of any CPU core within the time interval s can be dynamically counted by the above statistical calculation method.
[0040] Based on the above steps, after obtaining the parameter information of the counter corresponding to the first CPU core, the method also includes: obtaining the task execution time of the first task of the first CPU core, wherein the first task is any one of the multiple tasks executed by the first CPU core; determining the task occupancy of the first task in the first CPU core based on the task execution time and the parameter information.
[0041] Through the built-in counter in the CPU core, the task execution time of each of the multiple tasks executed by the CPU core can be counted, so as to count the task occupancy rate of each task in the CPU core according to the task execution time and the parameter information of the counter.
[0042] The Task Control Block (TCB) used by the microkernel operating system is a data structure containing task-related information, including all the information required for task execution. It should be noted that the information contained in the task control blocks of different real-time kernels is usually different, but most of them include the name of the task, the starting address of the task execution, the status of the task, the priority of the task, the context of the task, etc.
[0043] It should be noted that tasks in a microkernel operating system are also called threads, which are sequential instruction streams that complete a certain function. They are the smallest unit of operating system scheduling. A CPU core can only run one task at a time (a multi-CPU core system can run multiple tasks at the same time). If there are multiple tasks in the system, the CPU needs to switch between several tasks, which is equivalent to the concurrent execution of multiple tasks from a macro perspective. When the CPU runs which task is determined by the operating system scheduling algorithm.
[0044] Common operating system scheduling strategies include time slice round-robin and priority preemption. The time slice round-robin scheduling strategy is the oldest, simplest and fairest system scheduling strategy. The operating system assigns a time period to each task, that is, the time it is allowed to run. When the time slice ends, if the task is still running, it will be forced to switch to other tasks for execution. If the time slice is set too short, too many task switches will reduce CPU efficiency. If it is set too long, the response to short interactive requests will be poor. The priority preemptive scheduling strategy is a scheduling strategy that schedules according to task priority. The operating system assigns a priority to each task. When a high-priority task enters the ready state, it terminates the low-priority task and then executes the high-priority task. The original priority preemptive scheduling strategy does not allow tasks to set the same priority. The improved priority preemptive scheduling strategy allows tasks to set the same priority. When the same priority tasks are ready, time slice round-robin or first-in-first-out strategies are used for scheduling. The advantages of preemptive scheduling are good real-time performance and fast response. Microkernel operating systems usually adopt priority preemptive task scheduling strategies.
[0045] Task switching (context switching) refers to the process of saving the context of the current task and restoring the context of the task that needs to be executed. When a task switch occurs, the context of the currently running task needs to be saved through the task control block of the task, and the context of the task that needs to be put into operation is restored from the corresponding task control block. Under certain conditions, tasks will switch between different states. For tasks in the ready state, after obtaining the CPU, they are in the running state. If a task in the running state is preempted by a high-priority task or the execution times out, the task will return to the ready state; if a task in the running state needs to wait for resources, the task will be switched to the waiting state. For tasks in the waiting state, if the required resources are met, they will be converted to the ready state and wait to be scheduled for execution.
[0046] Specifically, determining the task occupancy rate of the first task in the first CPU core according to the task execution time and the parameter information, including: determining the total usage time of the first CPU core according to the parameter information by the following formula: utilization_total_i=count_max*num_overflow+count_end-count_start, wherein utilization_total_i is the total usage time, count_max is the maximum count, num_overflow is the number of flips, count_end is the end count value, and count_start is the initial count value; determining the ratio of the task execution time to the total usage time as the task occupancy rate of the first task in the first CPU core.
[0047] First, the total usage time of the CPU core is calculated according to the parameter information of the counter and the above formula, and then the ratio of the task execution time to the total usage time is calculated to obtain the task occupancy rate of the task in the CPU core.
[0048] In an optional embodiment, obtaining the task execution time of the first task of the first CPU core includes: in the case of detecting that the first task switches to the second task, obtaining the target parameter information of the counter at the first moment, wherein the first moment is the moment of switching to the second task, and the target parameter information includes: the maximum count, the first flip number of the counter at the first moment, the second flip number of the counter at the second moment, the first count value of the counter at the first moment, the second count value of the counter at the second moment, the second moment being the moment with the smallest time difference with the first moment among multiple third moments of switching to the first task, and the multiple third moments are all earlier than the first moment; determining whether the counter has flipped within a first time period according to the first flip number and the second flip number, wherein the start time of the first time period is the second moment, and the end time of the first time period is the first moment; in determining When the counter has not flipped over in the time period, the difference between the first count value and the second count value is determined as the first task execution time, and the sum of the first task execution time and the second task execution time is determined as the task execution time, wherein the second task execution time is the task execution time of the first task in the second time period, the start time of the second time period is the start time of the counter, and the end time of the second time period is the second moment; when it is determined that the counter has flipped over in the first time period, the first task execution time is calculated by the following formula: time=count_max-start_time+count, and the sum of the first task execution time and the second task execution time is determined as the task execution time, wherein time is the first task execution time, count_max is the maximum count, start_time is the second count value, and count is the first count value.
[0049] Each time the counter detects a task switch in the CPU core (for example, a switch from task A (i.e., the first task) to task B (i.e., the second task), the CPU time occupied by the current task A (i.e., the task execution time) is calculated. First, it is determined whether the counter of task A has been rolled over during this execution. If no rollover has occurred, the formula A.track.utilisation = A.track.utilisation + (count-A.track.start_time) is used to calculate the first task execution time of task A during this execution. The "A.track.utilisation" on the right side of the equation represents the second task execution time historically counted for task A, i.e., task A may have undergone multiple task switches, and an execution time statistics will be performed at each switch, and the sum is determined as the task execution time of task A at the current moment. If a rollover has occurred, the task execution time is calculated by the formula A.track.utilisation = A.track.utilisation + (count_max-A.track.start_time+count), where "(count_max-A.track.start_time+count)" is the first task execution time.
[0050] like Figure 4 As shown, Figure 4 The overall block diagram of the statistical solution for multi-core and multi-task CPU occupancy includes the following steps:
[0051] Step 1: Start counting the number of machines, initialize statistical parameters, and start CPU usage statistics;
[0052] Step 2, when switching tasks, for example, switching from task A to task B, first calculate the CPU time occupied by the current task A, and determine whether the counter has flipped during the execution of task A according to the number of counter flips; if there has been no overflow flip, the calculation method is A.track.utilisation = A.track.utilisation + (count-A.track.start_time); if there has been an overflow flip, the calculation method is A.track.utilisation = A.track.utilisation + (count_max-A.track.start_time + count); where count_max is the maximum count of the counter, and the start time start_time of task B is assigned to the current counter value, that is, B.track.start_time = count;
[0053] Step 3, the statistical task monitor is executed periodically according to the specified statistical interval s (usually in seconds). When the statistical task starts, all statistical variable values are cleared and the counter value is recorded as count_start. When the statistical task ends, the counter value is recorded as count_end. When the statistical task ends, the total utilization time of the current CPU core i is utilization_total_i=count_max*num_overflow+count_end-count_start; the idle utilization_idle_i time of the current CPU core i is the IDLE idle task execution time IDLE.track.utilisation; the occupancy time of task n of the current CPU core i is utilization_task_n_i=task_n_i.track.utilisation; the total CPU occupancy rate of the current CPU core i is r_total_i=1-utilisation_IDLE_i / utilization_total_i; the CPU occupancy rate of task n on the current CPU core i is r_task_n_i=utilisation_task_n_i / utilization_total_i. The above statistical calculation method can dynamically count the total occupancy of any CPU core within the time interval s and the CPU occupancy data of any task on any CPU core.
[0054] Based on the above steps, after determining the total occupancy of the first CPU core according to the parameter information and the usage time, the method also includes: when the total occupancy of each CPU core in the multiple CPU cores has been obtained, determining a first list, a second list and a third list, wherein the first list includes: identification information of CPU cores whose total occupancy is less than a first threshold, the second list includes: identification information of CPU cores whose total occupancy is less than a second threshold and greater than the first threshold, and the third list includes: identification information of CPU cores whose total occupancy is greater than the second threshold.
[0055] CPU affinity is a scheduling property of the microkernel operating system, which can bind tasks to specified CPU cores for execution. Traditional programming based on microkernel operating systems requires users to plan the tasks that need to be run on each CPU core in advance to avoid the situation where some CPU cores have too high occupancy rates, and some CPU cores have high idle rates and low overall CPU execution efficiency. At the same time, when the CPU core occupancy rate exceeds 90% or even reaches 100%, the execution efficiency and real-time performance of the task will be catastrophically affected. On the one hand, this places very high demands on programmers; on the other hand, since some tasks are dynamically changing, their CPU occupancy rates cannot be evaluated in detail in advance, and unreasonable task allocation will occur, leading to the above-mentioned serious situation.
[0056] Normally, we consider that when the CPU core occupancy rate is less than 75% (equivalent to the first threshold mentioned above), it is a case of high load capacity, and tasks can be executed on this CPU core first; when the CPU core occupancy rate is greater than 75% and less than 90% (equivalent to the second threshold mentioned above), it is a case of medium load capacity, and when there is no CPU core with high load capacity, tasks can be executed on this CPU core first; when the CPU core occupancy rate is greater than 90%, it is a case of low load capacity, and in order to ensure the execution of existing tasks, tasks will not be allowed to be executed on this CPU core. According to the above classification of occupancy rates, multiple CPU cores can be divided into lists, and different CPU cores can be divided into different lists according to the real-time total CPU core occupancy rate, so as to facilitate task allocation.
[0057] It should be noted that the above 75% and 90% are only used as examples. In the specific application process, the above first threshold and the second threshold can be adjusted according to needs, for example, the second threshold can be adjusted to 95%, etc. This application does not impose any restrictions on this.
[0058] Optionally, the method also includes: when receiving a task creation request, determining whether the task creation request carries a core binding parameter, wherein the core binding parameter is used to indicate the CPU core to which the fourth task corresponding to the task creation request is to be bound; when the task creation request carries the core binding parameter, binding the fourth task to the second CPU core indicated by the core binding parameter; when the task creation request does not carry the core binding parameter, matching the third CPU core in the multiple CPU cores according to the first list and the second list, and when matching the third CPU core, binding the fourth task to the third CPU core.
[0059] In order to avoid the situation where the real-time performance of the task is reduced during the switching between CPU cores and the overall execution efficiency is low due to frequent switching, this application only selects the CPU core for task execution when the task is created, and prohibits execution core switching during task execution.
[0060] The process of task selection CPU core is as follows Figure 5 As shown, when the user calls the create task interface, first determine whether to pass in the core binding parameter. If the core binding parameter is passed in, the task is bound to the CPU core indicated by the core binding parameter according to the core binding parameter; if the core binding parameter is not passed in, it is regarded as requiring the microkernel operating system to perform reasonable core binding. In combination with the characteristics of modern multi-core CPUs, this application designs the following scheme: First, search for a high-load capacity interface, that is, search and return the first CPU core number with a CPU occupancy rate less than 75% in the order of CPU core numbers from small to large; search for a medium-load capacity interface, that is, search and return the first CPU core number with a CPU occupancy rate greater than 75% and less than 90% in the order of CPU core numbers from small to large; that is, first search in sequence according to the core number of each CPU core in the multiple CPU cores according to the first list, if the CPU core in the first list is not matched, then continue to match the CPU core in the second list, and after matching the third CPU core in the first list or the second list, bind the task to the third CPU core; if no suitable CPU core is found in the first list or the second list (that is, the above-mentioned third CPU core is not found), the task creation fails.
[0061] Based on the above steps, after determining the first list, the second list and the third list, the method also includes: determining the third CPU core indicated by the third list; obtaining the task occupancy rate of each of the multiple fifth tasks of the third CPU core; and determining a processing method for the third CPU core based on the multiple task occupancy rates, wherein the processing method is used to reduce the total occupancy rate of the third CPU core.
[0062] like Figure 6 As shown, Figure 6 A task multi-core overload warning scheme is shown. The microkernel operating system searches for overloaded CPU cores and returns their serial numbers, that is, generates the above-mentioned third list. If there is no overloaded CPU core, monitoring continues; if there is, the user is notified of the task occupancy rate of each task on the current overloaded CPU core through an IPC message, so that the user can handle the emergency.
[0063] Through the CPU occupancy rate statistics method of the microkernel operating system proposed in the above embodiment, the total occupancy rate of tasks on each CPU core (i.e., total occupancy rate) and the occupancy rate of each task (i.e., task occupancy rate) can be counted. The statistical method is simple and efficient and has very little impact on the stability and real-time performance of the microkernel operating system. In addition, the above scheme is based on the high-precision counter hardware function of the CPU, and the statistical results of the total CPU occupancy rate and the CPU occupancy rate of each task are very accurate; when the CPU occupancy rate exceeds the warning value, the user can be notified in time to perform relevant processing, effectively improving the execution efficiency, real-time performance and reliability of the task.
[0064] 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 such an 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 of each embodiment of the present application.
[0065] Figure 7 is a structural block diagram of a device for determining occupancy rate according to an embodiment of the present application; Figure 7 As shown, including:
[0066] The acquisition module 72 is used to acquire parameter information of a counter corresponding to the first CPU core, and acquire idle usage time of the first CPU core executing an idle task, wherein the first CPU core is any CPU core among multiple CPU cores, and the parameter information includes: a maximum count of the counter, a rollover number of the counter, an end count value of the counter when counting ends, and an initial count value of the counter;
[0067] The determination module 74 is used to determine the total occupancy rate of the first CPU core according to the parameter information and the idle usage time.
[0068] Through the above-mentioned device, the parameter information of the counter in the first CPU core is first obtained, and the idle usage time of the first CPU core executing the idle task is obtained. The first CPU core is any CPU core in the multiple CPU cores in the microkernel operating system, and the parameter information includes: the maximum count value of the counter, the number of rollovers of the counter, the end count value of the counter at the end of counting, and the initial count value of the counter; then, the total CPU occupancy rate of the first CPU core can be determined according to these parameter information and the idle usage time; by adopting the above-mentioned scheme, by configuring a counter for each CPU core, the occupancy rate of the CPU core is quickly counted according to the preset rules; thereby solving the problem in the related technology that there is a lack of efficient CPU occupancy rate statistical scheme in the prior art.
[0069] Optionally, the above-mentioned determination module 74 is further used to determine the total usage time of the first CPU core according to the parameter information by the following formula: utilization_total_i=count_max*num_overflow+count_end-count_start, wherein utilization_total_i is the total usage time, count_max is the maximum count, num_overflow is the number of flips, count_end is the end count value, and count_start is the initial count value; determine the total occupancy rate by the following formula: r_total_i=1-utilisation_IDLE_i / utilization_total_i, wherein r_total_i is the total occupancy rate and utilization_IDLE_i is the idle usage time.
[0070] Optionally, the above-mentioned acquisition module 72 is also used to obtain the task execution time of the first task of the first CPU core, wherein the first task is any one of multiple tasks executed by the first CPU core; and determine the task occupancy rate of the first task in the first CPU core based on the task execution time and the parameter information.
[0071] Optionally, the above-mentioned determination module 74 is also used to determine the total usage time of the first CPU core according to the parameter information by the following formula: utilization_total_i=count_max*num_overflow+count_end-count_start, wherein utilization_total_i is the total usage time, count_max is the maximum count, num_overflow is the number of flips, count_end is the end count value, and count_start is the initial count value; the ratio of the task execution time to the total usage time is determined as the task occupancy rate of the first task in the first CPU core.
[0072] Optionally, the acquisition module 72 is further used to acquire target parameter information of the counter at a first moment when it is detected that the first task switches to the second task, wherein the first moment is the moment of switching to the second task, and the target parameter information includes: the maximum count, the first flip number of the counter at the first moment, the second flip number of the counter at the second moment, the first count value of the counter at the first moment, the second count value of the counter at the second moment, the second moment being the moment with the smallest time difference with the first moment among multiple third moments of switching to the first task, and the multiple third moments are all earlier than the first moment; determine whether the counter has flipped within a first time period according to the first flip number and the second flip number, wherein the start time of the first time period is the second moment, and the end time of the first time period is the first moment; after determining that the counter has not flipped within the first time period In the case where a rollover has occurred, the difference between the first count value and the second count value is determined as the first task execution time, and the sum of the first task execution time and the second task execution time is determined as the task execution time, wherein the second task execution time is the task execution time of the first task within the second time period, the start time of the second time period is the start time of the counter, and the end time of the second time period is the second moment; in the case where it is determined that the counter has rolled over within the first time period, the first task execution time is calculated by the following formula: time=count_max-start_time+count, and the sum of the first task execution time and the second task execution time is determined as the task execution time, wherein time is the first task execution time, count_max is the maximum count, start_time is the second count value, and count is the first count value.
[0073] Optionally, the above-mentioned determination module 74 is also used to determine the first list, the second list and the third list when the total occupancy of each CPU core in the multiple CPU cores has been obtained, wherein the first list includes: identification information of CPU cores whose total occupancy is less than a first threshold, the second list includes: identification information of CPU cores whose total occupancy is less than a second threshold and greater than the first threshold, and the third list includes: identification information of CPU cores whose total occupancy is greater than the second threshold.
[0074] Optionally, the above-mentioned determination module 74 is also used to determine whether the task creation request carries a core binding parameter when receiving the task creation request, wherein the core binding parameter is used to indicate the CPU core to which the fourth task corresponding to the task creation request is to be bound; when the task creation request carries the core binding parameter, bind the fourth task to the second CPU core indicated by the core binding parameter; when the task creation request does not carry the core binding parameter, match the third CPU core in the multiple CPU cores according to the first list and the second list, and when the third CPU core is matched, bind the fourth task to the third CPU core.
[0075] Optionally, the above-mentioned determination module 74 is also used to determine the third CPU core indicated by the third list; obtain the task occupancy rate of each of the multiple fifth tasks of the third CPU core; determine the processing method for the third CPU core based on the multiple task occupancy rates, wherein the processing method is used to reduce the total occupancy rate of the third CPU core.
[0076] An embodiment of the present application further provides a storage medium, which includes a stored program, wherein the program executes any of the above methods when it is run.
[0077] Optionally, in this embodiment, the storage medium may be configured to store program codes for executing the following steps:
[0078] S1, obtaining parameter information of a counter corresponding to a first CPU core, and obtaining idle usage time of the first CPU core executing an idle task, wherein the first CPU core is any CPU core among multiple CPU cores, and the parameter information includes: a maximum count of the counter, a rollover number of the counter, an end count value of the counter at the end of counting, and an initial count value of the counter;
[0079] S2: Determine a total occupancy rate of the first CPU core according to the parameter information and the idle usage time.
[0080] 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 any one of the above method embodiments.
[0081] Optionally, 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.
[0082] Optionally, in this embodiment, the processor may be configured to perform the following steps through a computer program:
[0083] S1, obtaining parameter information of a counter corresponding to a first CPU core, and obtaining idle usage time of the first CPU core executing an idle task, wherein the first CPU core is any CPU core among multiple CPU cores, and the parameter information includes: a maximum count of the counter, a rollover number of the counter, an end count value of the counter at the end of counting, and an initial count value of the counter;
[0084] S2: Determine a total occupancy rate of the first CPU core according to the parameter information and the idle usage time.
[0085] Optionally, in this embodiment, the above-mentioned storage medium may include but is not limited to: 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, and other media that can store program codes.
[0086] Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiments and optional implementation modes, and this embodiment will not be described in detail here.
[0087] 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, and optionally, 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.
[0088] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A method for determining occupancy rate, applied to a microkernel operating system, characterized in that: include: Obtain parameter information of a counter corresponding to a first CPU core, and obtain idle usage time of the first CPU core executing an idle task, wherein the first CPU core is any CPU core among multiple CPU cores, and the parameter information includes: a maximum count of the counter, a rollover number of the counter, an end count value of the counter when counting ends, and an initial count value of the counter; The total occupancy rate of the first CPU core is determined according to the parameter information and the idle usage time.
2. The method for determining occupancy rate according to claim 1, characterized in that: Determining the total occupancy rate of the first CPU core according to the parameter information and the idle usage time includes: According to the parameter information, the total usage time of the first CPU core is determined by the following formula: utilization_total_i=count_max*num_overflow+count_end-count_start, where: utilization_total_i is the total utilization time, count_max is the maximum count, num_overflow is the number of rollovers, count_end is the end count value, and count_start is the initial count value; the total occupancy rate is determined by the following formula: r_total_i=1-utilisation_IDLE_i / utilization_total_i, wherein r_total_i is the total occupancy rate, and utilization_IDLE_i is the idle utilization time.
3. The method for determining occupancy rate according to claim 1, characterized in that: After acquiring parameter information of the counter corresponding to the first CPU core, the method further includes: Obtaining a task execution time of a first task of the first CPU core, wherein the first task is any one of a plurality of tasks executed by the first CPU core; Determine the task occupancy rate of the first task in the first CPU core according to the task execution time and the parameter information.
4. The method for determining occupancy rate according to claim 3, characterized in that: Determining the task occupancy rate of the first task in the first CPU core according to the task execution time and the parameter information includes: According to the parameter information, the total usage time of the first CPU core is determined by the following formula: utilization_total_i=count_max*num_overflow+count_end-count_start, where: utilization_total_i is the total utilization time, count_max is the maximum count, num_overflow is the number of rollovers, count_end is the end count value, and count_start is the initial count value; the ratio of the task execution time to the total utilization time is determined as the task occupancy rate of the first task in the first CPU core.
5. The method for determining occupancy rate according to claim 3, characterized in that: Obtaining the task execution time of the first task of the first CPU core includes: In the case of detecting that the first task is switched to the second task, obtaining target parameter information of the counter at the first moment, wherein the first moment is the moment of switching to the second task, and the target parameter information includes: the maximum count, the first flip number of the counter at the first moment, the second flip number of the counter at the second moment, the first count value of the counter at the first moment, the second count value of the counter at the second moment, the second moment being the moment with the smallest time difference with the first moment among multiple third moments of switching to the first task, and the multiple third moments are all earlier than the first moment; Determining whether the counter has flipped in a first time period according to the first flip number and the second flip number, wherein the start time of the first time period is the second time period and the end time of the first time period is the first time period; In the case where it is determined that the counter has not flipped over in the first time period, the difference between the first count value and the second count value is determined as the first task execution time, and the sum of the first task execution time and the second task execution time is determined as the task execution time, wherein the second task execution time is the task execution time of the first task in the second time period, the start time of the second time period is the start time of the counter, and the end time of the second time period is the second moment; When it is determined that the counter has rolled over within the first time period, the first task execution time is calculated by the following formula: time=count_max-start_time+count, and the sum of the first task execution time and the second task execution time is determined as the task execution time, wherein time is the first task execution time, count_max is the maximum count, start_time is the second count value, and count is the first count value.
6. The method for determining occupancy rate according to claim 1, characterized in that: After determining the total occupancy rate of the first CPU core according to the parameter information and the usage time, the method further includes: In the case where the total occupancy of each CPU core in the multiple CPU cores has been obtained, a first list, a second list and a third list are determined, wherein the first list includes: identification information of CPU cores whose total occupancy is less than a first threshold, the second list includes: identification information of CPU cores whose total occupancy is less than a second threshold and greater than the first threshold, and the third list includes: identification information of CPU cores whose total occupancy is greater than the second threshold.
7. The method for determining occupancy rate according to claim 6, characterized in that: The method further comprises: In the case of receiving a task creation request, determining whether the task creation request carries a core binding parameter, wherein the core binding parameter is used to indicate a CPU core to be bound to the fourth task corresponding to the task creation request; In a case where the task creation request carries the core binding parameter, binding the fourth task to the second CPU core indicated by the core binding parameter; When the task creation request does not carry the core binding parameter, a third CPU core is matched in the multiple CPU cores according to the first list and the second list, and when the third CPU core is matched, the fourth task is bound to the third CPU core.
8. The method for determining occupancy rate according to claim 6, characterized in that: After determining the first list, the second list and the third list, the method further includes: Determining a third CPU core indicated by the third list; Obtain the task occupancy rate of each of the multiple fifth tasks of the third CPU core; determine a processing method for the third CPU core according to the multiple task occupancy rates, wherein the processing method is used to reduce the total occupancy rate of the third CPU core.
9. A device for determining occupancy rate, characterized in that: include: an acquisition module, configured to acquire parameter information of a counter corresponding to a first CPU core, and acquire idle usage time of the first CPU core executing an idle task, wherein the first CPU core is any CPU core among multiple CPU cores, and the parameter information includes: a maximum count of the counter, a rollover number of the counter, an end count value of the counter when counting ends, and an initial count value of the counter; A determination module is used to determine the total occupancy rate of the first CPU core according to the parameter information and the idle usage time.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein the program executes the method described in any one of claims 1 to 8 when executed.
11. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to execute the method according to any one of claims 1 to 8 through the computer program.
