Method, device and electronic device for controlling CPU resource usage
By determining the CPU running cycle and working time so that they are close to the fractional part of the maximum CPU usage, the problem of CPU resource usage control in the container cluster is solved, and business smoothness and resource utilization optimization are achieved.
Patent Information
- Application Number
- CN202111088852.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-16
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2041-09-16
AI Technical Summary
In container clusters, existing technologies are difficult to effectively control CPU resource usage, resulting in business lags or insufficient utilization of CPU resources.
By obtaining the preset maximum CPU usage and maximum sleep duration of the target object, determine the CPU running cycle and working duration parameters to minimize the absolute value of the difference between the decimal part of the maximum CPU usage and the maximum CPU usage, thereby controlling the CPU usage of the target object. The amount does not exceed the maximum occupied amount.
It achieves a reasonable CPU running cycle and working time to ensure smooth business, and a reasonable number of containers or Pods, optimizing the overall control of CPU resources.
Smart Images

Figure CN113791868B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of container clusters, and in particular to a method, device and electronic device for controlling CPU resource usage. Background Art
[0002] In a container cluster, containers are usually managed in units of Pods; Pods are a collection of containers, that is, a Pod includes multiple containers, and Pods run in virtual machines. Through the cgroup mechanism, the maximum resource usage of each container in a Pod or the maximum resource usage of a Pod can be limited to avoid excessive competition for resources between containers or Pods, which may cause business abnormalities. Taking CPU (Central Processing Unit) resources as an example, in the cgroup mechanism, when a process occupies the CPU for a certain threshold, the process will be forced to sleep to control the CPU usage of the process. Based on this, it is necessary to set the CPU operation cycle in the cgroup and set the maximum operation time of the container or Pod in the operation cycle. If the operation cycle is set too long, it is easy to cause the process corresponding to the container or Pod to stop running for too long, resulting in business jams; if the operation cycle is set too short, fewer containers or Pods are allowed to run in one operation cycle, which is not conducive to the overall control of CPU resources. Summary of the invention
[0003] In view of this, the purpose of the present invention is to provide a method, device and electronic device for controlling the CPU resource usage, so as to obtain a more reasonable CPU operation cycle and CPU working time, thereby ensuring smooth business operation. At the same time, the amount of containers or Pods running in one operation cycle is also relatively reasonable, which is conducive to the overall control of CPU resources.
[0004] In a first aspect, an embodiment of the present invention provides a method for controlling CPU resource usage, the method comprising: obtaining a preset maximum CPU usage of a target object, and a maximum sleep duration of a business running in the target object; wherein the target object comprises a target Pod or a target container; within the maximum sleep duration range, determining a CPU operating cycle and a CPU working duration parameter, wherein the absolute value of the difference between a ratio of a CPU working duration parameter to a CPU operating cycle and a decimal part of the maximum CPU usage is minimum; determining the CPU working duration based on the CPU working duration parameter; within the CPU operating cycle, controlling the maximum CPU usage duration of a process corresponding to the target object to be the CPU working duration, so as to control the CPU usage of the target object not to exceed the maximum CPU usage.
[0005] Furthermore, within the range of the longest sleep duration, the step of determining the CPU operation cycle and the CPU working duration parameters includes: obtaining multiple groups of optional parameter groups; wherein each group of optional parameter groups includes an optional cycle value and an optional duration parameter; the optional cycle value is between the preset minimum cycle value and the maximum sleep duration range, and the optional cycle value is an integer multiple of the preset minimum cycle value; the optional duration parameter is between the preset minimum cycle value and the target optional cycle value, and the optional duration parameter is an integer multiple of the preset minimum cycle value; the target optional cycle value is the optional cycle corresponding to the optional duration parameter in any optional parameter group; determining the target parameter group from multiple groups of optional parameter groups; wherein the optional duration parameter in the target parameter group is the CPU working duration parameter, and the optional cycle value is the CPU operation cycle.
[0006] Furthermore, the step of obtaining multiple groups of optional parameter groups includes: obtaining multiple optional period values between a preset minimum period value and a maximum sleep duration range; wherein the difference between adjacent optional period values is the preset minimum period; for each optional period, obtaining at least one optional duration parameter between the preset minimum period value and the numerical range of the optional period; wherein, if the optional duration parameters include multiple, the difference between adjacent optional duration parameters is the preset minimum period; respectively forming optional parameter groups with the optional period value and each optional duration parameter to obtain at least one group of optional parameter groups corresponding to the optional period value; and determining at least one group of optional parameter groups corresponding to each optional period as the final multiple groups of optional parameter groups.
[0007] Furthermore, the step of determining a target parameter group from a plurality of optional parameter groups includes: for each optional parameter group, determining a ratio between an optional duration parameter and an optional period value in the optional parameter group; determining an absolute value of a difference between the ratio and a decimal part of a maximum CPU occupancy; and determining the optional parameter group with the smallest absolute value as the target parameter group.
[0008] Furthermore, based on the CPU working time parameter, the step of determining the CPU working time includes: determining the product value of the CPU operating cycle and the integer part of the maximum CPU occupancy; and determining the sum of the product value and the CPU working time parameter as the CPU working time.
[0009] Furthermore, the step of controlling the maximum time that the process corresponding to the target object occupies the CPU to be the CPU working time within the CPU operating cycle so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy includes: during the operation of the target object, within the CPU operating cycle, controlling the maximum time that the process corresponding to the target object occupies the CPU to be the CPU working time through the resource management process so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy.
[0010] Furthermore, the resource management process includes the cgroup process.
[0011] In a second aspect, an embodiment of the present invention provides a device for controlling CPU resource occupancy, the device comprising: an acquisition module, used to obtain a preset maximum CPU occupancy of a target object, and a maximum sleep duration of a business running in the target object; wherein the target object comprises a target Pod or a target container; a parameter determination module, used to determine a CPU operating cycle and a CPU working duration parameter within the maximum sleep duration range; wherein the absolute value of the difference between the ratio of the CPU working duration parameter to the CPU operating cycle and the decimal part of the maximum CPU occupancy is minimum; a duration determination module, used to determine the CPU working duration based on the CPU working duration parameter; and an occupancy control module, used to control, within the CPU operating cycle, the maximum duration that a process corresponding to the target object occupies the CPU to be the CPU working duration, so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy.
[0012] In a third aspect, an embodiment of the present invention provides an electronic device, including a processor and a memory, the memory storing machine executable instructions that can be executed by the processor, and the processor executing the machine executable instructions to implement any method of controlling CPU resource usage of the first aspect.
[0013] In a fourth aspect, an embodiment of the present invention provides a machine-readable storage medium, which stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement any method of controlling CPU resource usage of the first aspect.
[0014] The embodiments of the present invention bring the following beneficial effects:
[0015] The present invention provides a method, device and electronic device for controlling CPU resource occupancy, which obtain a preset maximum CPU occupancy of a target object and a maximum sleep duration of a business running in the target object; wherein the target object includes a target Pod or a target container; within the maximum sleep duration range, a CPU operation cycle and a CPU working duration parameter are determined, wherein the absolute value of the difference between the ratio of the CPU working duration parameter to the CPU operation cycle and the decimal part of the maximum CPU occupancy is the smallest; based on the CPU working duration parameter, the CPU working duration is determined; within the CPU operation cycle, the maximum duration of the CPU occupied by a process corresponding to the target object is controlled to be the CPU working duration, so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy. In this method, by minimizing the absolute value of the difference between the ratio of the determined CPU working time parameter and the CPU operating cycle and the decimal part of the maximum CPU usage, the ratio of the determined CPU working time parameter and the CPU operating cycle can be made closest to the decimal part of the maximum CPU usage, thereby making the minimum part of the CPU usage of the target object closest to the decimal part of the maximum CPU usage of the target object. At the same time, the integer part of the CPU usage of the target object is usually consistent with the integer part of the maximum CPU usage. Therefore, the obtained CPU operating cycle and CPU working time can control the CPU usage of the target object not to exceed the maximum CPU usage, thereby meeting the CPU resource control requirements of the target object. Within the limited maximum sleep time, since the CPU working time parameter and the CPU operating cycle are determined within the maximum sleep time allowed for running the business within the target object, there will be no obvious jamming of the business, thereby ensuring smooth business operation. At the same time, the amount of containers or Pods running within an operating cycle is also relatively reasonable, which is conducive to the overall control of CPU resources.
[0016] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention are realized and obtained by the structures particularly pointed out in the description, claims and drawings.
[0017] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without paying any creative work.
[0019] Figure 1 A flow chart of a method for controlling CPU resource usage provided by an embodiment of the present invention;
[0020] Figure 2 A schematic diagram of a file system structure of a resource management process provided by an embodiment of the present invention;
[0021] Figure 3 A schematic diagram of the structure of a resource management process running device provided by an embodiment of the present invention;
[0022] Figure 4 A schematic flow chart of another method for controlling CPU resource usage provided by an embodiment of the present invention;
[0023] Figure 5 A schematic diagram of the structure of a device for controlling CPU resource usage provided by an embodiment of the present invention;
[0024] Figure 6 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0025] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0026] At present, in Kata container instances, the cgroup mechanism of the Linux operating system kernel is usually used inside the virtual machine to limit the maximum CPU resources occupied by a single container or Pod, that is, the cgroup mechanism limits the amount of resources occupied by containers or Pods to avoid excessive competition for resources between containers or Pods, which may cause business abnormalities. The working principle of the cgroup mechanism is that in each CPU operation cycle, when the time the target process in the container or Pod occupies CPU resources reaches a certain threshold, it will be forced to sleep to achieve the purpose of limiting the CPU resource occupation of the target process. Based on this, it is necessary to set the CPU operation cycle in the cgroup mechanism and set the maximum operation time of the container or Pod in the operation cycle; if the operation cycle is set too long, it is easy to cause the process corresponding to the container or Pod to stop running for too long, resulting in business jams; if the operation cycle is set too short, fewer containers or Pods are allowed to run in one operation cycle, which is not conducive to the overall control of CPU resources.
[0027] For example, if the CPU has 0.5 cores and the CPU operation cycle is set to 1 second, in order to achieve the utilization rate of 0.5 core CPU, the maximum working time in each operation cycle can be set to 0.5 seconds. At this time, when the CPU inside the virtual machine is fully loaded, it will work for 0.5 seconds and sleep for 0.5 seconds. This will cause obvious lag problems in the business, especially for network business, which will constantly jitter between 1ms delay and 100ms delay. If the CPU operation cycle is set to 0.001 seconds, the CPU inside the virtual machine will work for 0.0005 seconds and sleep for 0.0005 seconds when it is fully loaded, which will appear much smoother. However, fewer containers or Pods are allowed to run within the 0.001 second operation cycle, which is not conducive to the overall control of CPU resources.
[0028] Based on this, an embodiment of the present invention provides a method, device and electronic device for controlling CPU resource usage. The technology can be applied to a container cluster, and in particular, can be applied to a server with a function of controlling resource usage.
[0029] To facilitate understanding of this embodiment, a method for controlling CPU resource usage disclosed in an embodiment of the present invention is first introduced in detail, and the method is applied to a physical machine server. The physical machine is the name of the physical computer relative to the virtual machine. The physical machine provides the virtual machine with a hardware environment, and is sometimes also referred to as a "host" or "host". Through the cooperation of the physical machine and the virtual machine, multiple operating systems (one external operating system and several operating systems in the virtual machine) can be installed on a computer, and communication can be achieved between several operating systems, just like there are multiple computers.
[0030] like Figure 1 As shown, the method comprises the following steps:
[0031] Step S102, obtaining the preset maximum CPU usage of the target object and the maximum sleep duration of the business running in the target object; wherein the target object includes a target Pod or a target container;
[0032] The above target Pod is a collection of a group of containers, that is, a Pod includes multiple containers, and the Pod runs in a virtual machine. The above target Pod is an abstract concept, and the virtual machine is a specific means that can be used to implement the target Pod. In this embodiment, the target Pod and the virtual machine are one-to-one corresponding. In general, the target Pod can be regarded as a virtual machine, and each container in the Pod is equivalent to a process running in the virtual machine.
[0033] The preset maximum CPU usage of the target object mentioned above usually refers to the amount of CPU resources that the target object can occupy during operation; usually, in order to avoid excessive competition for CPU resources between Pods or containers, the maximum CPU usage of each target object is preset to reasonably allocate CPU resources. The maximum sleep time of the business running in the target object mentioned above usually needs to be set in advance based on the business type and actual needs through experience or calculation, such as 0.01 seconds. In actual implementation, the preset maximum CPU usage of the target object and the maximum sleep time of the business running in the target object can be obtained by running the target process created by the virtual machine of the target object.
[0034] Specifically, the preset maximum CPU usage of the target object can be read from a specified configuration file through the container operation management process. The above-mentioned specified configuration file can be a configuration file written in YAML (another markup language), and the maximum CPU usage available for the target object is written in the specified configuration file. The above-mentioned specified configuration file can pass the preset maximum CPU usage of the target object to the container operation management process.
[0035] Step S104, determining the CPU operation cycle and the CPU working time parameter within the longest sleep time range, wherein the absolute value of the difference between the ratio of the CPU working time parameter to the CPU operation cycle and the fractional part of the maximum CPU occupancy is the smallest;
[0036] The above-mentioned CPU operation cycle is usually within the range of the longest sleep duration, and the above-mentioned CPU working duration parameter is used to determine the CPU working duration, so that the business running in the target object can run normally within the CPU operation cycle for the CPU working duration. Specifically, usually within the range of the longest sleep duration, multiple CPU operation cycles can be determined, and multiple CPU working duration parameters can be determined between the preset minimum cycle and multiple CPU operation cycles; and then each CPU operation cycle corresponds to multiple CPU working duration parameters, and for each CPU operation cycle, the ratio of the multiple CPU working duration parameters corresponding to the CPU operation cycle to the CPU operation cycle corresponding to the CPU working duration parameter is calculated, and multiple ratios can be obtained. The multiple ratios can be compared with the decimal part of the CPU occupancy, and the CPU operation cycle and CPU working duration parameters corresponding to the ratio with the smallest absolute value of the difference with the decimal part of the CPU occupancy among the multiple ratios are determined.
[0037] Step S106, determining the CPU working time based on the CPU working time parameter;
[0038] The CPU working time mentioned above usually refers to the longest working time for the target object within the CPU operation cycle. In actual implementation, the CPU working time can be determined according to the CPU working time parameter, the integer part of the maximum CPU occupancy, and the CPU operation cycle. The CPU working time mentioned above can make the CPU occupancy of the target object reach the maximum CPU occupancy as much as possible and not exceed the maximum CPU occupancy.
[0039] Step S108, during the CPU operation cycle, the longest duration of the CPU occupation by the process corresponding to the target object is controlled to be the CPU working duration, so as to control the CPU occupation of the target object not to exceed the maximum CPU occupation.
[0040] During the CPU operating cycle, multiple Pods and multiple containers can occupy CPU resources. The above CPU working time can enable the business running in the target object to run normally during the CPU operating cycle for the CPU working time, and the maximum time that the process corresponding to the target object occupies the CPU is controlled to be the CPU working time, so that the CPU usage of the target object can reach the maximum CPU usage as much as possible without exceeding the maximum CPU usage.
[0041] The present invention provides a method for controlling CPU resource occupancy, which obtains a preset maximum CPU occupancy of a target object and a maximum sleep duration of a business running in the target object; wherein the target object includes a target Pod or a target container; within the maximum sleep duration range, a CPU operation cycle and a CPU working duration parameter are determined, wherein the absolute value of the difference between the ratio of the CPU working duration parameter to the CPU operation cycle and the decimal part of the maximum CPU occupancy is minimum; based on the CPU working duration parameter, the CPU working duration is determined; within the CPU operation cycle, the maximum duration of a process corresponding to the target object occupying the CPU is controlled to be the CPU working duration, so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy. In this method, by minimizing the absolute value of the difference between the ratio of the determined CPU working time parameter and the CPU operating cycle and the decimal part of the maximum CPU usage, the ratio of the determined CPU working time parameter and the CPU operating cycle can be made closest to the decimal part of the maximum CPU usage, thereby making the minimum part of the CPU usage of the target object closest to the decimal part of the maximum CPU usage of the target object. At the same time, the integer part of the CPU usage of the target object is usually consistent with the integer part of the maximum CPU usage. Therefore, the obtained CPU operating cycle and CPU working time can control the CPU usage of the target object not to exceed the maximum CPU usage, thereby meeting the CPU resource control requirements of the target object. Within the limited maximum sleep time, since the CPU working time parameter and the CPU operating cycle are determined within the maximum sleep time allowed for running the business within the target object, there will be no obvious jamming of the business, thereby ensuring smooth business operation. At the same time, the amount of containers or Pods running within an operating cycle is also relatively reasonable, which is conducive to the overall control of CPU resources.
[0042] The embodiment of the present invention also provides a method for controlling the CPU resource usage. This embodiment is implemented on the basis of the above embodiment. This embodiment mainly describes the specific implementation process of the step of determining the CPU operation cycle and the CPU working time parameter within the maximum sleep time range (implemented by steps 202-203), the specific implementation process of the step of determining the CPU working time based on the CPU working time parameter (implemented by step 204), and the specific implementation process of the step of controlling the maximum CPU usage time of the process corresponding to the target object to be the CPU working time within the CPU operation cycle so as to control the CPU usage of the target object not to exceed the maximum CPU usage (implemented by step 205), which specifically includes the following steps:
[0043] Step 201, obtaining the preset maximum CPU usage of the target object and the maximum sleep duration of the business running in the target object; wherein the target object includes a target Pod or a target container;
[0044] Step 202, obtaining multiple groups of optional parameter groups; wherein each group of optional parameter groups includes an optional period and an optional duration parameter; the optional period is between a preset minimum period value and a maximum sleep duration, and the optional period is an integer multiple of the preset minimum period value; the optional duration parameter is between a preset minimum period value and a target optional period value, and the optional duration parameter is an integer multiple of the preset minimum period value; the target optional period value is an optional period corresponding to the optional duration parameter in any optional duration parameter;
[0045] The above-mentioned preset minimum period can be preset according to the operating system installed on the server, for example, the minimum period supported by Linux is 0.001 seconds. For example, if the maximum sleep duration is set to 1 second and the minimum period is set to 0.1 seconds, then the optional period is between the preset minimum period and the maximum sleep duration, and the optional period is an integer multiple of the preset minimum period value, so the optional period usually includes: 0.1, 0.2, ..., 0.9, 1, multiple or all. The target optional period and the optional duration parameter are in the same optional parameter group. The target optional period corresponding to each optional duration parameter is the optional period in the optional parameter group to which the optional duration parameter belongs, and the optional duration parameter is between the preset minimum period and the target optional period, and the optional duration parameter is an integer multiple of the preset minimum period. Therefore, each optional period corresponds to multiple optional duration parameters. When the optional period is 0.1, the optional duration parameter corresponding to the optional period value may be 0.1; when the optional period is 0.2, the optional duration parameter corresponding to the optional period may be 0.1 or 0.2; when the optional period is 0.3, the optional duration parameter corresponding to the optional period may be multiple or all of 0.1, 0.2, and 0.3; when the optional period is 1, the optional duration parameter corresponding to the optional period includes multiple or all of 0.1, 0.2, 0.3, …, 0.9, and 1.
[0046] A possible implementation:
[0047] (1) obtaining a plurality of optional periods between a preset minimum period and a maximum sleep duration, wherein the difference between adjacent optional periods is the preset minimum period;
[0048] Specifically, the preset minimum period can be determined as the first optional period; the above example is continued to illustrate, and the preset minimum period 0.1 is determined as the first optional period; multiple optional periods are obtained between the preset minimum period 0.1 and the maximum sleep duration 1, specifically including: all values of 0.1, 0.2, 0.3, ..., 0.9, 1, and specifically 10 optional periods can be obtained. And the difference between adjacent optional periods is 0.1, that is, the preset minimum period.
[0049] (2) for each optional period, obtaining at least one optional duration parameter between a preset minimum period and a numerical range of the optional period; wherein, if the optional duration parameters include multiple ones, the difference between adjacent optional duration parameters is the preset minimum period;
[0050] (3) The optional period and each optional duration parameter are respectively combined into an optional parameter group to obtain at least one optional parameter group corresponding to the optional period;
[0051] (4) Determine at least one group of optional parameter groups corresponding to each optional period value as the final multiple groups of optional parameter groups.
[0052] For each optional period in step (1), the preset minimum period 0.1 is determined as the first optional duration parameter of the optional period, specifically including 10 groups of optional parameter groups: (0.1, 0.1), (0.2, 0.1), (0.3, 0.1), (0.4, 0.1), (0.5, 0.1), (0.6, 0.1), (0.7, 0.1), (0.8, 0.1), (0.9, 0.1), (1, 0.1). Then, multiple optional duration parameters are obtained between the preset minimum period 0.1 and the optional period. Specifically, an optional duration parameter 0.2 can be obtained between the preset minimum period 0.1 and the optional period 0.2, that is, one group of optional parameter groups is included: (0.2, 0.2). Then, between the preset minimum period 0.1 and the optional period 0.3, two optional duration parameters 0.2 and 0.3 can be obtained, including two groups of optional parameter groups: (0.3, 0.2) and (0.3, 0.3). Then, between the preset minimum period 0.1 and the optional period 0.4, three optional duration parameters 0.2, 0.3, and 0.4 can be obtained, including three groups of optional parameter groups: (0.4, 0.2), (0.4, 0.3), and (0.4, 0.4). Until the optional duration parameters corresponding to the optional period 1 are obtained, nine groups of optional parameter groups are obtained: (1, 0.2), (1, 0.3), (1, 0.4), (1, 0.5), (1, 0.6), (1, 0.7), (1, 0.8), (1, 0.9), and (1, 1), and finally multiple groups of optional parameter groups are obtained.
[0053] Step 203, determining a target parameter group from a plurality of optional parameter groups; wherein the optional duration parameter in the target parameter group is a CPU working duration parameter, and the optional period is a CPU operating period;
[0054] Specifically, the ratio of the optional duration parameter to the optional period in each optional parameter group can be calculated, and a target ratio closest to the decimal part of the maximum CPU usage is determined from multiple ratios, that is, a target ratio with the smallest absolute value of the difference from the decimal part of the maximum CPU usage is determined from multiple ratios, and the optional parameter group corresponding to the target ratio is determined as the target parameter group. The optional duration parameter in the target parameter group is the CPU working duration parameter, and the optional period in the target parameter group is the CPU operating period.
[0055] A possible implementation:
[0056] For each optional parameter group, determine the ratio between the optional duration parameter and the optional period in the optional parameter group; determine the absolute value of the difference between the ratio and the fractional part of the maximum CPU usage; and determine the optional parameter group with the smallest absolute value as the target parameter group.
[0057] For example, if the maximum CPU usage is 1.5, first divide "1.5" into an integer part and a decimal part, the integer part "1" represents CPUs, and the decimal part "0.5" represents CPUf. At the same time, set a variable minp and define minp as "1". If the preset minimum cycle is 0.001 seconds, that is, the minimum CPU operation cycle of the cgroup process supported by the Linux kernel is 0.001 seconds, where the optional cycle is represented by i and the optional duration parameter is represented by j. Start enumeration from the preset minimum cycle 0.001 seconds and enumerate until the longest sleep duration (for example, 0.01 seconds). For each enumerated optional cycle, add a preset minimum cycle 0.001 seconds to the corresponding optional duration parameter each time, record this value as j, and stop enumerating when j=i. For each set of optional parameter groups (i, j), it can be easily understood that it means that in each i time, the CPU is allowed to work for a maximum of j time. That is to say, in this case, the CPU occupancy rate is limited to j / i.
[0058] Then, calculate the value of |j / i-CPUf| and record it as p; compare p and minp. If p is less than minp, record i and j and set the value of minp to p until a set of target parameter groups (i, j) is obtained, which minimizes the p value.
[0059] Among them, the optional duration parameter in the target parameter group is the CPU working duration parameter, and the optional cycle is the CPU operating cycle; specifically, the optional duration parameter in the target parameter group is determined as the CPU working duration parameter, and the optional cycle value in the target parameter group is determined as the CPU operating cycle;
[0060] Continuing with the above example, the above target parameter group (i, j) can be expressed as period and quota, that is, (period, quota); finally, period and quota are obtained, where period is the CPU operation cycle and quota is an optional duration parameter.
[0061] Step 204, determining the product value of the CPU operation cycle and the integer part of the maximum CPU occupancy; and determining the sum of the product value and the CPU working time parameter as the CPU working time.
[0062] Continuing with the above example, quota'=quota+period*CPUs is set, that is, the product of the CPU operation period period and the integer part of the maximum CPU usage CPUs is calculated; then the sum of the product value period*CPUs and the CPU working time parameter quota is determined as the CPU working time quota'.
[0063] To further illustrate, multiple groups of optional parameter groups can be determined first, including: a first group of optional parameter groups (0.001, 0.001); a second group of optional parameter groups (0.002, 0.001); a third group of optional parameter groups (0.002, 0.002); a fourth group of optional parameter groups (0.003, 0.001); a fifth group of optional parameter groups (0.003, 0.002), and so on.
[0064] Among them, the first group of optional parameters is (0.001, 0.001), at this time j / i = (0.001 / 0.001) = 1; p = |j / i-CPUf| = 1-0.5 = 0.5; p = 0.5 is less than minp = 1; at this time, record (0.001, 0.001) and set minp = 0.5.
[0065] The second group of optional parameters is (0.002, 0.001), at this time j / i = (0.001 / 0.002) = 0.5; p = |j / i-CPUf| = |0.5-0.5| = 0; p = 0 is less than minp = 0.5; at this time, record (0.002, 0.001) and set minp = 0.
[0066] The third group of optional parameters is (0.002, 0.002), at this time j / i = (0.002 / 0.002) = 1; p = |j / i-CPUf| = |1-0.5| = 0.5; p = 0.5 is greater than minp = 0; at this time, (0.002, 0.001) is not recorded, and minp is not reset.
[0067] The fourth group of optional parameters is (0.003, 0.001), at this time j / i = (0.001 / 0.003) = 0.3333; p = |j / i-CPUf| = |0.3333-0.5| = 0.1667; p = 0.1667 is greater than minp = 0; at this time, (0.003, 0.001) is not recorded, and minp is not reset.
[0068] The fifth group of optional parameters is (0.003, 0.002), at this time j / i = (0.002 / 0.003) = 0.6667; p = |j / i-CPUf| = |0.6667-0.5| = 0.01667; p = 0.1667 is less than minp = 0; at this time, (0.003, 0.002) is not recorded, and minp is not reset.
[0069] The sixth optional parameter group is (0.003, 0.003), at this time j / i = (0.003 / 0.003) = 1; p = |j / i-CPUf| = |1-0.5| = 0.5; p = 0.5 is greater than minp = 0; at this time, (0.003, 0.003) is not recorded, and minp is not reset.
[0070] According to the above enumeration method, multiple groups of optional parameter groups can be obtained until the nth group of optional parameter groups (0.01, 0.01) is enumerated; finally, according to the p value recorded in each group of optional parameter groups, the optional parameter group corresponding to the smallest p value is determined as the target parameter group (period, quota). Finally, quota' is set equal to quota+period*CPUs, and period is the CPU operation cycle, and quota' is the maximum working time of the CPU for the target Pod during the operation cycle.
[0071] Step 205, during the operation of the target object, within the CPU operation cycle, the maximum CPU occupancy time of the process corresponding to the target object is controlled by the resource management process to be the CPU working time, so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy.
[0072] During the operation of the target object, the CPU usage of the target object is controlled by the resource management process based on the CPU operation cycle and the CPU working time. The above resource management process is a mechanism that can limit the number of resources used by the process. The resource management process can control the CPU usage of the target Pod and the CPU usage of each target container in the target Pod. The above resource management process is provided by the operating system kernel and exists in both the physical machine server and the target virtual machine. In this embodiment, the resource management process of the target virtual machine is mainly created in the physical machine server.
[0073] The resource management process controls the CPU usage of the target object based on the CPU operation cycle and CPU working time. The principle is: in each CPU operation cycle, when the target container (or target Pod) occupies the CPU for a period of time equal to the CPU working time, it is forced to sleep to achieve the purpose of limiting the CPU usage of the target container (or target Pod). For example, if the CPU operation cycle is 1 second and the CPU working time is 0.5 seconds, when the CPU inside the target virtual machine is fully loaded, when the target container (or target Pod) occupies the CPU for 0.5 seconds, it is forced to sleep for 0.5 seconds. If the CPU operation cycle is 0.001 seconds, when the CPU inside the target virtual machine is fully loaded, when the target process (or target Pod) occupies the CPU for 0.0005 seconds, it is forced to sleep for 0.0005 seconds.
[0074] In the above method, by minimizing the absolute value of the difference between the ratio of the determined CPU working time parameter and the CPU operating cycle and the decimal part of the maximum CPU occupancy, the ratio of the determined CPU working time parameter and the CPU operating cycle can be made closest to the decimal part of the maximum CPU occupancy, thereby making the minimum part of the CPU occupancy of the target object closest to the decimal part of the maximum CPU occupancy of the target object. At the same time, the integer part of the CPU occupancy of the target object is usually consistent with the integer part of the maximum CPU occupancy. Therefore, the obtained CPU operating cycle and CPU working time can control the CPU occupancy of the target object not to exceed the maximum CPU occupancy, thereby meeting the CPU resource control requirements of the target object. Within the limited maximum sleep time, since the CPU working time parameter and the CPU operating cycle are determined within the maximum sleep time allowed for running the business within the target object, there will be no obvious jamming of the business, thereby ensuring smooth business operation. At the same time, the amount of containers or Pods running within an operating cycle is also relatively reasonable, which is conducive to the overall control of CPU resources.
[0075] The following describes the creation process of the target object and resource management process in detail, which includes the following steps:
[0076] Step 301, start a container operation management process; wherein the container operation management process is used to: create a target virtual machine to run a target object in the target virtual machine;
[0077] The above container operation management process can be understood as a code flow. After the code is run, the above target virtual machine will be created. It can also be understood as a framework for building a target Pod. Usually, this code flow includes relevant code for creating the target virtual machine, which is usually named createSandbox. The above container operation management process runs in a physical server and manages the specified virtual machine on the physical server by calling the interface provided by the kernel of the physical server operating system.
[0078] In the specific implementation, a code flow is started on the physical server, that is, the container operation management process is started; after the code is finished running, the target virtual machine is created. The target virtual machine is mainly used to run the target object, that is, one or more containers in the target Pod, and each container only installs one process.
[0079] In addition, the above-mentioned container operation management process includes the Kata runtime process, which runs on the physical machine server and manages the specified virtual machine on the current physical machine server by calling the interface provided by the operating system kernel of the physical machine server. The Kata runtime process is specifically a code flow. Starting to run Kataruntime means starting the container operation management process. Usually, the container operation management process will include multiple sub-processes or multiple execution commands, which are used to complete different management or business.
[0080] Furthermore, the container operation management process also includes a subprocess for creating a virtual machine monitor; the subprocess is a piece of code, usually named createHypervisor; the virtual machine monitor is usually installed in the operating system of the physical machine server.
[0081] Step 302, creating a resource management process for the target virtual machine;
[0082] Specifically, after starting the above container operation management process, the container operation management process is executed, that is, in the process of creating the target virtual machine, the resource management process of the target virtual machine can be created by calling the corresponding system interface.
[0083] Among them, the step of creating a resource management process of the target virtual machine, a possible implementation method is:
[0084] (1) Through the container operation management process, call the specified system call interface in the operating system kernel of the physical machine server to create a resource management process for the target virtual machine;
[0085] Specifically, in the relevant code of Kata runtime, that is, in the above container operation management process, the resource management process of the target virtual machine can be created by calling the specified system call interface in the operating system kernel of the physical machine server. The above operating system can be Linux, Windows and other systems. The above specified system call interface is an interface that can create a process, such as: fork, clone and other interfaces. The above resource management process includes a cgroup process, which provides a mechanism for controlling the use of CPU resources by a group of specific processes.
[0086] In a possible implementation, a resource management process of a target virtual machine is created before executing a subprocess for creating a virtual machine monitor; specifically, the execution order can be set in the container operation management process, and after the container operation management process is started, the resource management process for creating a target virtual machine is executed first to create a resource management process for the target virtual machine, and then the subprocess for creating a virtual machine monitor is executed to create a virtual machine monitor. After the container operation management process is executed, the target virtual machine is created.
[0087] (2) Add the container operation management process to the resource management process so as to control the CPU usage of the target virtual machine created by the container operation management process through the resource management process.
[0088] The CPU usage of the above-mentioned target virtual machine needs to be controlled by the resource management process. Since the resource management process created above is not bound to the target virtual machine, in order to enable the resource management process to control the CPU usage of the target virtual machine, the container running management process can be added to the resource management process so that the resource management process can control the CPU usage of the target virtual machine created by the container running management process.
[0089] In addition, after the step of determining the CPU operating cycle and the CPU working time, the method further includes: executing a subprocess for creating a virtual machine monitor.
[0090] Specifically, the subprocess for creating a virtual machine monitor can be executed when the specified position is executed through the preset execution order in the Kata runtime process. It can be understood that in the code running the Kata runtime, when the code related to creating a virtual machine monitor is run (usually the related code named createHypervisor), the subprocess for creating a virtual machine monitor is executed. The above-mentioned virtual machine monitor is a layer of code located between the operating system and the computer hardware, which can be understood as system software. It can maintain multiple efficient and isolated program environments (i.e. virtual machines), which support users to directly access real hardware. Usually, the virtual machine monitor manages the real resources of the computer system and can provide an interface for the virtual machine.
[0091] Step 303: After the container operation management process is executed, the creation of the target virtual machine is completed.
[0092] After the above container operation management process is executed, that is, after the code flow corresponding to the container operation management process is executed, the target virtual machine will be created, which can also be understood as the framework of the Pod is built. Then the target object can be run in the created target virtual machine.
[0093] Finally, during the operation of the target object, the CPU usage of the target object is controlled by the resource management process based on the CPU operation cycle and CPU working time, that is, the CPU usage of the virtual machine management process is controlled, thereby controlling the CPU usage of the target object in the target virtual machine; wherein the target virtual machine corresponds to the target Pod one by one.
[0094] The above-mentioned virtual machine management process is executed by kata-runtime through relevant commands and runs on the physical machine. The above-mentioned resource management process can control the CPU usage of QEMU (Quick EMUlator, virtual operating system simulator) based on the CPU operating cycle and CPU working time, which can limit all CPU usage inside the virtual machine and control the CPU usage of the virtual machine management process, so as to achieve the purpose of controlling the CPU usage of the target object in the target virtual machine. QEMU is a complete set of virtualization solutions, which is implemented in pure software, including processor virtualization, memory virtualization and simulation of various virtual devices. The above-mentioned target Pod is an abstract concept, and the target virtual machine is a specific means that can be used to implement the target Pod. In general, the target Pod can be regarded as a virtual machine, and each container in the Pod is equivalent to a process running in the virtual machine. Therefore, the above-mentioned target Pod and the target virtual machine are one-to-one corresponding.
[0095] In the above method, in the process of creating a virtual machine to run the target object, the CPU operating cycle and the CPU working time are determined in the resource management process through the preset maximum CPU usage of the target object; when the virtual machine is created, the resource management process can control the CPU usage of the target object based on the CPU operating cycle and the CPU working time. This method can accurately control the resource usage of the Pod according to the actual needs of the Pod, improve the flexibility of resource management of the Pod, and realize the limitation of the CPU usage of the Pod and the container at the same time based on the cgroup mechanism.
[0096] In addition, in the process of controlling the CPU usage of the target object in the target virtual machine through the resource management process, since the CPU operation cycle and CPU working time in the resource management process are determined by the pre-configured CPU usage, it will not affect the resource management process's control of the resource usage of each container in the target Pod, thus achieving the resource management process's control of the resource usage of the target Pod and the container at the same time. In addition, by determining the CPU operation cycle and CPU working time, the CPU usage of the target object can be controlled with minimal impact on the user.
[0097] See also Figure 2 The file system structure diagram of the resource management process is shown in the figure. The resource management process is described in detail below. Among them, system and file system are both directory names. Cgroups can be created through directory names. The cgroup (i.e. the above resource management process) file system includes multiple subsystems, namely: blkio usually sets input / output limits for block devices, such as physical devices (disks, solid-state drives, USB, etc.). memory usually sets the memory limits used by tasks in the cgroup and automatically generates reports on memory resources used by these tasks. cpu.cpuacct usually automatically generates CPU reports used by tasks in the cgroup, that is, statistics on CPU usage. cpuset usually allocates independent CPUs (in multi-core systems) and memory nodes to tasks in the cgroup. Pids can usually limit the total number of tasks that can be created in the cgroup. cgroup.procs can usually add other processes to the cgroup. cfs_period_us and cfs_quota_us are user-mode interface files that can control access to CPU resources. Each file exists independently in a pseudo-file of the cgroup virtual file system. cfs_period_us specifies the length of a period in microseconds, that is, the aforementioned CPU operating cycle. cfs_quota_us specifies the available running time in a period in microseconds, that is, the aforementioned CPU working time.
[0098] See also Figure 3The schematic diagram of the structure of the resource management process running device shown in the figure, wherein the kernel cgroup refers to the resource management process created in the physical machine server, and the virtual machine in the figure is the target virtual machine of the present invention, wherein QEMU-KVM (Kernel Virtual Machine, is a kernel driver module of Linux, which can make the Linux host a Hypervisor (virtual machine monitor)) indicates that the target virtual machine is composed of QEMU and KVM. KVM is another virtualization solution. Since KVM actually only implements support for processor virtualization features in the kernel and lacks device virtualization and corresponding user space management tools for virtual machines, it usually borrows QEMU code and simplifies it, and together with KVM, it constitutes another independent virtualization solution. In the present invention, the target virtual machine is used to run the target object, and the target object includes a target Pod and a target container. The target Pod includes one or more containers, for example, Figure 3 Container 1 and container 2 in.
[0099] See also Figure 4 The flowchart of another method for controlling CPU resource usage shown mainly describes the life cycle of the resource management process. First, containerd-shim-kata-v2, referred to as shimv2, runs the code in Kata-runtime to start the virtual machine, that is, to start the container operation management process (corresponding to the virtual machine process in the figure). Shimv2 can be understood as the entry point of Kata Containers, which implements the interface for starting the container operation management process for Kata. Then, the target virtual machine is created through the container operation management process so that the target virtual machine runs the target object. Reading the Pod limit from k8s can be understood as reading the preset maximum CPU usage of the target object pre-configured in the container cluster, that is, the aforementioned step obtains the preset maximum CPU usage of the target object.
[0100] Create a cgroup, that is, the above steps call the specified system call interface in the operating system kernel of the physical server through the container operation management process to create a resource management process for the target virtual machine. Configure the Pod limit as the cgroup limit, that is, the above steps determine the CPU operation cycle and determine the CPU working time. Put the virtual machine process in the cgroup, that is, the above steps add the container operation management process to the resource management process to control the PCU occupancy of the target object created by the container operation management process through the resource management process. In addition, the Kata runtime can end the container instance, that is, shut down the virtual machine, and then delete the cgroup. The virtual machine process is executed in the internal environment of the virtual machine.
[0101] Corresponding to the above method embodiment, the embodiment of the present invention provides a device for controlling CPU resource usage, such as Figure 5 As shown, the device comprises:
[0102] The acquisition module 51 is used to obtain the preset maximum CPU usage of the target object and the maximum sleep time of the business running in the target object; wherein the target object includes a target Pod or a target container;
[0103] The parameter determination module 52 is used to determine the CPU operation cycle and the CPU working time parameter within the maximum sleep time range; wherein the absolute value of the difference between the ratio of the CPU working time parameter to the CPU operation cycle and the fractional part of the maximum CPU occupancy is the smallest;
[0104] The duration determination module 53 is used to determine the CPU working duration based on the CPU working duration parameter;
[0105] The occupancy control module 54 is used to control the maximum CPU occupancy time of the process corresponding to the target object to be the CPU working time during the CPU operation cycle, so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy.
[0106] The present invention provides a device for controlling CPU resource occupancy, which obtains a preset maximum CPU occupancy of a target object and a maximum sleep duration of a business running in the target object; wherein the target object includes a target Pod or a target container; within the maximum sleep duration range, a CPU operation cycle and a CPU working duration parameter are determined; wherein the absolute value of the difference between the ratio of the CPU working duration parameter to the CPU operation cycle and the decimal part of the maximum CPU occupancy is minimum; based on the CPU working duration parameter, the CPU working duration is determined; within the CPU operation cycle, the maximum duration of the CPU occupied by a process corresponding to the target object is controlled to be the CPU working duration, so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy. In this method, by minimizing the absolute value of the difference between the ratio of the determined CPU working time parameter and the CPU operating cycle and the decimal part of the maximum CPU usage, the ratio of the determined CPU working time parameter and the CPU operating cycle can be made closest to the decimal part of the maximum CPU usage, thereby making the minimum part of the CPU usage of the target object closest to the decimal part of the maximum CPU usage of the target object. At the same time, the integer part of the CPU usage of the target object is usually consistent with the integer part of the maximum CPU usage. Therefore, the obtained CPU operating cycle and CPU working time can control the CPU usage of the target object not to exceed the maximum CPU usage, thereby meeting the CPU resource control requirements of the target object. Within the limited maximum sleep time, since the CPU working time parameter and the CPU operating cycle are determined within the maximum sleep time allowed for running the business within the target object, there will be no obvious jamming of the business, thereby ensuring smooth business operation. At the same time, the amount of containers or Pods running within an operating cycle is also relatively reasonable, which is conducive to the overall control of CPU resources.
[0107] Furthermore, the above-mentioned parameter determination module is also used to: obtain multiple groups of optional parameter groups; wherein each group of optional parameter groups includes an optional cycle value and an optional duration parameter; the optional cycle value is between the preset minimum cycle value and the longest sleep duration, and the optional cycle value is an integer multiple of the preset minimum cycle value; the optional duration parameter is between the preset minimum cycle value and the target optional cycle value, and the optional duration parameter is an integer multiple of the preset minimum cycle value; the target optional cycle value is the optional cycle corresponding to the optional duration parameter in any optional parameter group; determine the target parameter group from multiple groups of optional parameter groups; wherein the optional duration parameter in the target parameter group is the CPU working duration parameter, and the optional cycle value is the CPU operating cycle.
[0108] Furthermore, the above-mentioned parameter determination module is also used to: obtain multiple optional period values between a preset minimum period value and a maximum sleep duration range; wherein the difference between adjacent optional period values is the preset minimum period; for each optional period, obtain at least one optional duration parameter between the preset minimum period value and the numerical range of the optional period; wherein, if the optional duration parameters include multiple, the difference between adjacent optional duration parameters is the preset minimum period; respectively form optional parameter groups with the optional period value and obtain at least one group of optional parameter groups corresponding to the optional period value; and determine at least one group of optional parameter groups corresponding to each optional period as the final multiple groups of optional parameter groups.
[0109] Furthermore, the above-mentioned parameter determination module is also used to: determine, for each group of optional parameter groups, the ratio between the optional duration parameter and the optional period value in the optional parameter group; determine the absolute value of the difference between the ratio and the decimal part of the maximum CPU occupancy; and determine the optional parameter group with the smallest absolute value as the target parameter group.
[0110] Furthermore, the duration determination module is further used to: determine the product value of the CPU operation cycle and the integer part of the maximum CPU occupancy; and determine the sum of the product value and the CPU working duration parameter as the CPU working duration.
[0111] Furthermore, the above-mentioned occupancy control module is also used for: during the operation of the target object, within the CPU operation cycle, controlling the maximum CPU occupancy time of the process corresponding to the target object through the resource management process to be the CPU working time, so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy.
[0112] Furthermore, the resource management process includes a cgroup process.
[0113] The device for controlling CPU resource occupancy provided in the embodiment of the present invention has the same technical features as the method for controlling CPU resource occupancy provided in the above embodiment, so it can also solve the same technical problems and achieve the same technical effects.
[0114] This embodiment also provides an electronic device, including a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor executes the machine executable instructions to implement the above method of controlling CPU resource usage.
[0115] See also Figure 6 As shown, the electronic device includes a processor 100 and a memory 101, wherein the memory 101 stores machine executable instructions that can be executed by the processor 100, and the processor 100 executes the machine executable instructions to implement the above method for controlling CPU resource occupancy.
[0116] Further, Figure 6 The electronic device shown further includes a bus 102 and a communication interface 103 , and the processor 100 , the communication interface 103 and the memory 101 are connected via the bus 102 .
[0117] The memory 101 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk storage. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 103 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used. The bus 102 may be an ISA bus, a PCI bus, or an EISA bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 6 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0118] The processor 100 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 100. The above processor 100 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present invention can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in conjunction with the embodiments of the present invention can be directly embodied as a hardware decoding processor for execution, or a combination of hardware and software modules in the decoding processor for execution. The software module may be located in a storage medium mature in the art, such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 101, and the processor 100 reads the information in the memory 101 and completes the steps of the method of the above embodiment in combination with its hardware.
[0119] This embodiment also provides a machine-readable storage medium, which stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the above-mentioned method for controlling CPU resource usage.
[0120] The computer program product of the method, device and system for controlling CPU resource occupancy provided in the embodiments of the present invention includes a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the methods described in the previous method embodiments. The specific implementation can be found in the method embodiments and will not be repeated here.
[0121] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system and device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0122] In addition, in the description of the embodiments of the present invention, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, and it can be the internal communication of two components. For those skilled in the art, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0123] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc., which can store program codes.
[0124] In the description of the present invention, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present invention. In addition, the terms "first", "second", and "third" are used for descriptive purposes only, and cannot be understood as indicating or implying relative importance.
[0125] Finally, it should be noted that the above embodiments are only specific implementations of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The protection scope of the present invention is not limited thereto. Although the present invention is described in detail with reference to the above embodiments, those skilled in the art should understand that any person skilled in the art can still modify the technical solutions recorded in the above embodiments within the technical scope disclosed by the present invention, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.
Claims
1. A method for controlling CPU resource usage, characterized in that: The method comprises: Obtain the preset maximum CPU usage of the target object and the maximum sleep duration of the business running in the target object; wherein the target object includes a target Pod or a target container; Determine the CPU operation cycle and CPU working time parameters within the maximum sleep time range; wherein the absolute value of the difference between the ratio of the CPU working time parameter to the CPU operation cycle and the fractional part of the maximum CPU occupancy is the smallest; Determine the CPU working time based on the CPU working time parameter; During the CPU operation cycle, the maximum duration of the CPU occupation by the process corresponding to the target object is controlled to be the CPU working duration, so as to control the CPU occupation of the target object not to exceed the maximum CPU occupation; The step of determining the CPU operation cycle and the CPU working time parameters within the maximum sleep time range includes: Acquire multiple groups of optional parameter groups; wherein each group of the optional parameter groups includes an optional period and an optional duration parameter; the optional period is between the preset minimum period and the maximum sleep duration, and the optional period is an integer multiple of the preset minimum period; the optional duration parameter is between the preset minimum period and the target optional period, and the optional duration parameter is an integer multiple of the preset minimum period; the target optional period is the optional period corresponding to the optional duration parameter in any optional parameter group; Determine a target parameter group from the multiple groups of optional parameter groups; wherein the optional duration parameter in the target parameter group is the CPU working duration parameter, and the optional period is the CPU operating period; The step of determining a target parameter group from the plurality of optional parameter groups comprises: For each set of optional parameter groups, determining a ratio between an optional duration parameter and an optional period in the optional parameter group; Determine an absolute value of a difference between the ratio and a fractional part of the maximum CPU usage; The optional parameter group with the smallest absolute value is determined as the target parameter group.
2. The method according to claim 1, characterized in that The steps for obtaining multiple sets of optional parameter groups include: Acquire multiple optional periods between the preset minimum period and the maximum sleep duration range; wherein the difference between adjacent optional periods is the preset minimum period; For each of the optional periods, at least one optional duration parameter is obtained between the preset minimum period and the numerical range of the optional period; wherein, if the optional duration parameters include multiple ones, the difference between adjacent optional duration parameters is the preset minimum period; The optional period and each of the optional duration parameters are respectively combined into an optional parameter group to obtain at least one group of the optional parameter groups corresponding to the optional period; At least one group of the optional parameter groups corresponding to each of the optional periods is determined as the final multiple groups of optional parameter groups.
3. The method according to claim 1, characterized in that The step of determining the CPU working time based on the CPU working time parameter includes: Determine a product value of the CPU operation cycle and the integer part of the maximum CPU occupancy; The sum of the product value and the CPU working time parameter is determined as the CPU working time.
4. The method according to claim 1, characterized in that: The step of controlling the maximum duration of CPU occupation by the process corresponding to the target object to be the CPU working duration within the CPU operation cycle so as to control the CPU occupation of the target object not to exceed the maximum CPU occupation includes: During the operation of the target object, within the CPU operation cycle, the maximum CPU occupancy time of the process corresponding to the target object is controlled by the resource management process to be the CPU working time, so as to control the CPU occupancy of the target object to not exceed the maximum CPU occupancy.
5. The method according to claim 4, characterized in that The resource management process includes a cgroup process.
6. A device for controlling CPU resource usage, characterized in that: The device comprises: An acquisition module is used to obtain a preset maximum CPU usage of a target object and a maximum sleep duration of a service running in the target object; wherein the target object includes a target Pod or a target container; A parameter determination module, used for determining a CPU operation cycle and a CPU working duration parameter within the maximum sleep duration range; wherein the absolute value of the difference between the ratio of the CPU working duration parameter to the CPU operation cycle and the fractional part of the maximum CPU occupancy is the smallest; A duration determination module, used to determine the CPU working duration based on the CPU working duration parameter; an occupancy control module, used to control the maximum duration of the CPU occupation by the process corresponding to the target object to be the CPU working duration during the CPU operation cycle, so as to control the CPU occupancy of the target object not to exceed the maximum CPU occupancy; The parameter determination module is also used to: Obtain multiple groups of optional parameter groups; wherein each group of optional parameter groups includes an optional period value and an optional duration parameter; the optional period value is between a preset minimum period value and a maximum sleep duration, and the optional period value is an integer multiple of the preset minimum period value; the optional duration parameter is between a preset minimum period value and a target optional period value, and the optional duration parameter is an integer multiple of the preset minimum period value; the target optional period value is an optional period corresponding to an optional duration parameter in any optional parameter group; determine a target parameter group from multiple groups of optional parameter groups; wherein the optional duration parameter in the target parameter group is a CPU working duration parameter, and the optional period value is a CPU operating period; The parameter determination module is also used to: For each optional parameter group, determine the ratio between the optional duration parameter and the optional period value in the optional parameter group; determine the absolute value of the difference between the ratio and the decimal part of the maximum CPU usage; and determine the optional parameter group with the smallest absolute value as the target parameter group.
7. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor executes the machine executable instructions to implement the method for controlling CPU resource occupancy as described in any one of claims 1 to 5.
8. A machine-readable storage medium, characterized in that: The machine-readable storage medium stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the method for controlling CPU resource occupancy as described in any one of claims 1 to 5.
