[go: up one dir, main page]

CN115562852A - Sleep lock optimization method and device, electronic equipment and storage medium - Google Patents

Sleep lock optimization method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN115562852A
CN115562852A CN202211111502.2A CN202211111502A CN115562852A CN 115562852 A CN115562852 A CN 115562852A CN 202211111502 A CN202211111502 A CN 202211111502A CN 115562852 A CN115562852 A CN 115562852A
Authority
CN
China
Prior art keywords
lock
preset
time
sleep
pressure
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211111502.2A
Other languages
Chinese (zh)
Inventor
李培锋
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Guang Dong Ming Chuang Software Technology Corp ltd
Original Assignee
Guang Dong Ming Chuang Software Technology Corp ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Guang Dong Ming Chuang Software Technology Corp ltd filed Critical Guang Dong Ming Chuang Software Technology Corp ltd
Priority to CN202211111502.2A priority Critical patent/CN115562852A/en
Publication of CN115562852A publication Critical patent/CN115562852A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5022Mechanisms to release resources
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5038Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/505Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the load

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

本申请实施例提供了一种睡眠锁优化方法、装置、电子设备及存储介质。该睡眠锁优化方法包括:首先获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种,然后若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。通过上述方法,通过将CPU负载、内存回收压力和存储资源压力与预设参数阈值进行比较,在CPU负载、内存回收压力和存储资源压力均满足预设参数阈值后再减少睡眠锁的乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。

Figure 202211111502

Embodiments of the present application provide a sleep lock optimization method, device, electronic equipment, and storage medium. The sleep lock optimization method includes: first obtaining current system resource parameters, the system resource parameters including at least one of CPU load, memory recovery pressure and storage resource pressure, and then if the system resource parameters meet the preset parameter threshold, Then reduce the optimistic spin time of the sleep lock. Through the above method, by comparing the CPU load, memory recovery pressure and storage resource pressure with the preset parameter thresholds, reduce the optimistic spin of the sleep lock after the CPU load, memory recovery pressure and storage resource pressure all meet the preset parameter thresholds Time, you can adjust the optimistic spin time of the sleep lock, thereby reducing the system load and the lock waiting time of the process, thereby avoiding the excessive CPU load caused by the optimistic spin time being too long.

Figure 202211111502

Description

睡眠锁优化方法、装置、电子设备及存储介质Sleep lock optimization method, device, electronic equipment and storage medium

技术领域technical field

本申请属于计算机技术领域,具体涉及一种睡眠锁优化方法、装置、设备及可读存储介质。The present application belongs to the field of computer technology, and in particular relates to a sleep lock optimization method, device, equipment and readable storage medium.

背景技术Background technique

在系统进程需要的锁中,比如自旋锁和睡眠锁,由于进程从睡眠状态切换到唤醒状态,会耗时并消耗CPU资源,让系统运行的性能降低。为了减少CPU资源的损耗并节省时间,可以对睡眠锁添加乐观自旋操作。乐观自旋是系统进程申请锁失败后,不立即进入睡眠,而是先在一定的时间阈值内进行自旋等待。相关的睡眠锁优化方法,在对睡眠锁的乐观自旋时间进行设置时,通常会将睡眠锁的乐观自旋时间设置得过长导致CPU的负载过大。Among the locks required by the system process, such as spin locks and sleep locks, since the process switches from the sleep state to the wake-up state, it will take time and consume CPU resources, reducing the performance of the system. In order to reduce the consumption of CPU resources and save time, an optimistic spin operation can be added to the sleep lock. Optimistic spin means that after the system process fails to apply for a lock, it does not go to sleep immediately, but spins and waits within a certain time threshold. In related sleep lock optimization methods, when setting the optimistic spin time of the sleep lock, the optimistic spin time of the sleep lock is usually set too long, resulting in an excessive load on the CPU.

发明内容Contents of the invention

鉴于上述问题,本申请提出一种睡眠锁优化方法、装置、电子设备及存储介质,以实现改善上述问题。In view of the above problems, the present application proposes a sleep lock optimization method, device, electronic equipment, and storage medium, so as to improve the above problems.

第一方面,本申请实施例提供了一种睡眠锁优化方法,所述方法包括:获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种;若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。In the first aspect, the embodiment of the present application provides a sleeping lock optimization method, the method comprising: obtaining current system resource parameters, the system resource parameters including at least one of CPU load, memory recovery pressure and storage resource pressure ; If the system resource parameter meets the preset parameter threshold, reduce the optimistic spin time of the sleep lock.

第二方面,本申请实施例提供了一种睡眠锁优化装置,所述装置包括系统资源参数获取单元、系统资源参数比较单元。系统资源参数获取单元,用于获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种;系统资源参数比较单元,用于若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。In a second aspect, the embodiment of the present application provides a sleep lock optimization device, the device includes a system resource parameter acquisition unit and a system resource parameter comparison unit. A system resource parameter acquisition unit, configured to acquire current system resource parameters, the system resource parameters including at least one of CPU load, memory recovery pressure, and storage resource pressure; a system resource parameter comparison unit, configured to determine if the system resource If the parameter meets the preset parameter threshold, the optimistic spin time of the sleep lock is reduced.

第三方面,本申请实施例提供了一种电子设备,包括一个或多个处理器以及存储器;一个或多个程序,其中所述一个或多个程序被存储在所述存储器中并被配置为由所述一个或多个处理器执行,所述一个或多个程序配置用于执行上述的方法。In a third aspect, the embodiment of the present application provides an electronic device, including one or more processors and memory; one or more programs, wherein the one or more programs are stored in the memory and configured as Executed by the one or more processors, the one or more programs are configured to execute the above method.

第四方面,本申请实施例提供了一种计算机可读存储介质,所述计算机可读存储介质中存储有程序代码,其中,在所述程序代码运行时执行上述的方法。In a fourth aspect, the embodiment of the present application provides a computer-readable storage medium, where a program code is stored in the computer-readable storage medium, wherein the above method is executed when the program code is running.

本申请实施例提供了一种睡眠锁优化方法、装置、电子设备及存储介质。该睡眠锁优化方法包括:首先获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种,然后若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。通过上述方法,通过将CPU负载、内存回收压力和存储资源压力与预设参数阈值进行比较,在CPU负载、内存回收压力和存储资源压力均满足预设参数阈值后再减少睡眠锁的乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。Embodiments of the present application provide a sleep lock optimization method, device, electronic equipment, and storage medium. The sleep lock optimization method includes: first obtaining current system resource parameters, the system resource parameters including at least one of CPU load, memory recovery pressure and storage resource pressure, and then if the system resource parameters meet the preset parameter threshold, Then reduce the optimistic spin time of the sleep lock. Through the above method, by comparing the CPU load, memory recovery pressure and storage resource pressure with the preset parameter thresholds, reduce the optimistic spin of the sleep lock after the CPU load, memory recovery pressure and storage resource pressure all meet the preset parameter thresholds Time, you can adjust the optimistic spin time of the sleep lock, thereby reducing the system load and the lock waiting time of the process, thereby avoiding the excessive CPU load caused by the optimistic spin time being too long.

附图说明Description of drawings

为了更加清楚地说明本申请实施例中的技术方案,下面将对实施例描述中所需要使用的附图作简单地介绍,显而易见的,下面描述中的附图仅仅是本申请的一些实施例,对于本领域技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings that need to be used in the description of the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present application. For those skilled in the art, other drawings can also be obtained based on these drawings without any creative effort.

图1示出了本申请一实施例提出的一种睡眠锁优化方法的流程图;Fig. 1 shows a flow chart of a sleep lock optimization method proposed by an embodiment of the present application;

图2示出了本申请另一实施例提出的一种睡眠锁优化方法的流程图;FIG. 2 shows a flow chart of a sleep lock optimization method proposed in another embodiment of the present application;

图3示出了本申请再一实施例提出的一种获取存储资源压力的流程图;Fig. 3 shows a flow chart of obtaining storage resource pressure proposed by yet another embodiment of the present application;

图4示出了本申请再一实施例提出的一种获取内存回收压力的流程图;FIG. 4 shows a flow chart of obtaining memory recovery pressure proposed by yet another embodiment of the present application;

图5示出了本申请再一实施例提出的一种获取CPU负载的流程图;FIG. 5 shows a flow chart of obtaining CPU load proposed by another embodiment of the present application;

图6示出了本申请再一实施例提出的一种睡眠锁优化方法的流程图;FIG. 6 shows a flow chart of a sleep lock optimization method proposed in another embodiment of the present application;

图7示出了本申请再一实施例提出的一种睡眠锁优化方法的流程图;FIG. 7 shows a flow chart of a sleep lock optimization method proposed in another embodiment of the present application;

图8示出了本申请再一实施例提出的一种睡眠锁优化方法的流程图;FIG. 8 shows a flow chart of a sleep lock optimization method proposed in another embodiment of the present application;

图9示出了本申请实施例提出的一种睡眠锁优化装置的结构框图;FIG. 9 shows a structural block diagram of a sleep lock optimization device proposed by an embodiment of the present application;

图10示出了本申请实时中的用于执行本申请实施例的睡眠锁优化方法的电子设备的结构框图;FIG. 10 shows a structural block diagram of an electronic device for executing the sleep lock optimization method of the embodiment of the present application in real time of the present application;

图11示出了本申请实时中的用于保存或者携带实现根据本申请实施例的睡眠锁优化方法的程序代码的存储单元。FIG. 11 shows a storage unit for storing or carrying program codes for implementing the sleep lock optimization method according to the embodiment of the present application in real time of the present application.

具体实施方式detailed description

下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本申请一部分实施例,而不是全部的实施例。基于本申请中的实施例,本领域普通技术人员在没有作出创造性劳动前提下锁获得的所以其他实施例,都属于本申请保护的范围。The following will clearly and completely describe the technical solutions in the embodiments of the application with reference to the drawings in the embodiments of the application. Apparently, the described embodiments are only some of the embodiments of the application, not all of them. Based on the embodiments in this application, all other embodiments obtained by persons of ordinary skill in the art without making creative efforts all belong to the scope of protection of this application.

在系统进程需要的锁中,比如自旋锁和睡眠锁,由于进程从睡眠状态切换到唤醒状态,会耗时并消耗系统资源,让系统运行的性能降低。在自旋锁中,当进程需要持锁时,若发现锁已经被其它进程持有,则CPU会不断运行,让持锁的进程解锁;在睡眠锁中,当进程需要持锁时,发现锁已经被其它进程持有,则该进程会进入睡眠状态,直到持锁的进程解锁才被唤醒。Among the locks required by the system process, such as spin locks and sleep locks, since the process switches from the sleep state to the wake-up state, it will take time and consume system resources, reducing the performance of the system. In the spin lock, when the process needs to hold the lock, if it finds that the lock is already held by other processes, the CPU will continue to run to unlock the process holding the lock; in the sleep lock, when the process needs to hold the lock, the lock will be found If the lock is already held by another process, the process will go to sleep and will not be woken up until the lock-holding process is unlocked.

发明人在对相关的的睡眠锁优化方法的研究中发现,相关的睡眠锁方法,一般是当用户申请持睡眠锁时,如果该睡眠锁已经被其它进程持有了,则该进程会进入睡眠状态,直到持锁的进程释放睡眠锁,该进程才被系统唤醒。但在上述方法中,若持锁的进程持有时间较短便将睡眠锁释放,此时进程仍然处于睡眠状态,则该进程可能不能立即持锁,而是会等待一段时间,同时,进程从睡眠状态切换至唤醒状态不仅会消耗一定的CPU负载,还会消耗一定的时间。In the research of the related sleep lock optimization method, the inventor found that the related sleep lock method generally means that when the user applies for holding the sleep lock, if the sleep lock is already held by other processes, the process will go to sleep state, until the process holding the lock releases the sleep lock, the process is not woken up by the system. However, in the above method, if the lock-holding process is held for a short period of time, the sleep lock will be released. At this time, the process is still in the sleep state, and the process may not be able to hold the lock immediately, but will wait for a period of time. Switching from sleep state to wake state will not only consume a certain amount of CPU load, but also consume a certain amount of time.

因此,发明人提出了本申请中的睡眠锁优化方法、装置、电子设备及可读存储介质。首先获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种,然后若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。通过上述方法,通过将CPU负载、内存回收压力和存储资源压力与预设参数阈值进行比较,在CPU负载、内存回收压力和存储资源压力均满足预设参数阈值后再减少睡眠锁的乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。Therefore, the inventor proposes the sleep lock optimization method, device, electronic equipment and readable storage medium in this application. First obtain the current system resource parameters, the system resource parameters include at least one of CPU load, memory recovery pressure and storage resource pressure, and then if the system resource parameters meet the preset parameter threshold, then reduce the optimistic self-control of the sleep lock spin time. Through the above method, by comparing the CPU load, memory recovery pressure and storage resource pressure with the preset parameter thresholds, reduce the optimistic spin of the sleep lock after the CPU load, memory recovery pressure and storage resource pressure all meet the preset parameter thresholds Time, you can adjust the optimistic spin time of the sleep lock, thereby reducing the system load and the lock waiting time of the process, thereby avoiding the excessive CPU load caused by the optimistic spin time being too long.

下面将结合附图具体描述本申请的各实施例。Various embodiments of the present application will be described in detail below with reference to the accompanying drawings.

请参阅图1,本申请实施例提供了一种睡眠锁优化方法,所述方法包括:Please refer to Fig. 1, the embodiment of the present application provides a sleep lock optimization method, the method includes:

步骤S110:获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种。Step S110: Obtain current system resource parameters, where the system resource parameters include at least one of CPU load, memory recovery pressure, and storage resource pressure.

作为一种方式,系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种。比如,系统资源参数可以包括CPU负载、内存回收压力和存储资源压力中的任意一种;或者,系统资源参数可以包括CPU负载、内存回收压力和存储资源压力中的任意两种;或者,系统资源参数可以包括CPU负载、内存回收压力和存储资源压力。其中,CPU负载用于表征CPU资源的紧张程度,内存回收压力用于表征系统内存的紧张程度,存储资源压力用于表征IO负载的紧张程度。可选的,可以在系统通过任务调度子系统进行任务调度时,通过任务调度子系统获取当前的CPU负载;可以在进行内存回收的过程中获取当前的内存回收压力;在系统通过任务调度子系统进行任务调度时,通过任务调度子系统获取当前的存储资源压力。As a manner, the system resource parameter includes at least one of CPU load, memory recovery pressure, and storage resource pressure. For example, the system resource parameter may include any one of CPU load, memory recovery pressure, and storage resource pressure; or, the system resource parameter may include any two of CPU load, memory recovery pressure, and storage resource pressure; or, the system resource Parameters can include CPU load, memory reclamation pressure, and storage resource pressure. Among them, CPU load is used to represent the tightness of CPU resources, memory recovery pressure is used to represent the tightness of system memory, and storage resource pressure is used to represent the tightness of IO load. Optionally, when the system performs task scheduling through the task scheduling subsystem, the current CPU load can be obtained through the task scheduling subsystem; the current memory recovery pressure can be obtained during the memory recovery process; When performing task scheduling, the current storage resource pressure is obtained through the task scheduling subsystem.

在本申请实施例中,可以在系统进行任务调度时获取CPU负载。CPU负载可以用来显示一段时间内的CPU的运行负载的大小,CPU的负载越大,运行的任务就越多,CPU长时间处于高负载状态,容易对CPU和其它计算机硬件的使用寿命造成不良影响,因此可以通过监测CPU负载的大小,来避免CPU长时间处于高负载的运行状态。In the embodiment of the present application, the CPU load may be obtained when the system performs task scheduling. The CPU load can be used to display the operating load of the CPU within a period of time. The greater the CPU load, the more tasks are running. If the CPU is in a high load state for a long time, it is easy to cause adverse effects on the service life of the CPU and other computer hardware. Therefore, by monitoring the size of the CPU load, the CPU can be prevented from running under a high load for a long time.

作为一种方式,为了对CPU资源进行合理利用,在获取到CPU的负载状态后,若监测到CPU处于高负载状态,可以将一部分正在运行的占用CPU资源多的任务终止,进而将CPU负载降低;若检测到CPU处于低负载状态,可以再计划运行一部分任务。As a method, in order to make reasonable use of CPU resources, after obtaining the CPU load status, if the CPU is detected to be in a high load status, some running tasks that occupy a lot of CPU resources can be terminated, thereby reducing the CPU load. ; If it detects that the CPU is in a low load state, you can plan to run some tasks.

作为一种方式,系统进行内存回收主要有两个原因,一是系统需要为任何时刻突发的内存申请提供足够的内存,以便cache的使用和其它相关内存的使用,不至于让系统的剩余内存长期处于很少的状态;二是当存在大于空闲内存的内存申请时,由于系统内存短缺,不能满足内存分配的请求,系统会直接触发强制内存回收机制,尝试回收内存来解决问题。内核对于上述两种原因,分别有两种不同的机制,一是Linux系统设计了kswapd后台回收线程,当内核给进程分配内存时,由于系统内存低于low水位线,系统无法在内存低于low水位线的情况下分配内存,因此会唤醒kswapd后台回收线程来异步回收内存,kswapd后台回收线程虽然在系统启动时就会创建,但大多数时候都处于睡眠状态,只有在进程由于内存不足导致分配失败时会被唤醒,从而可以通过kswapd后台回收线程来回收内存,供进程使用;二是Linux系统会触发直接内存回收,在内核调用页分配函数分配内存时,由于系统内存短缺,不能满足分配请求,内核就会触发页面回收机制,尝试回收内存来解决问题。在进行内存分配时,只有当水位线处于low水位线和min水位线之间时,kswapd后台回收线程才进行工作,当水位线低于min水位线时则会触发直接内存回收。其中,内核是系统的核心,它的主要职责包括进程管理、磁盘管理、任务调度和内存管理,能将用户输入的命令转换成计算机硬件能够理解的机器语言,可以直接与硬件联系,并将应用软件发起的请求发送给硬件。As a method, there are two main reasons for the system to reclaim memory. One is that the system needs to provide enough memory for any sudden memory application at any time, so that the use of cache and other related memory will not let the remaining memory of the system The second is that when there is a memory request larger than free memory, due to the shortage of system memory, the memory allocation request cannot be satisfied, the system will directly trigger the forced memory recovery mechanism, and try to reclaim memory to solve the problem. The kernel has two different mechanisms for the above two reasons. One is that the Linux system has designed the kswapd background recovery thread. When the kernel allocates memory to the process, because the system memory is lower than the low water level, the system cannot save the memory below the low watermark. The memory is allocated under the water level, so the kswapd background recycling thread will be awakened to asynchronously reclaim the memory. Although the kswapd background recycling thread will be created when the system starts, it is in a sleep state most of the time. Only when the process is allocated due to insufficient memory When it fails, it will be awakened, so that the kswapd background recovery thread can be used to reclaim memory for process use; second, the Linux system will trigger direct memory recovery. When the kernel calls the page allocation function to allocate memory, due to the shortage of system memory, the allocation request cannot be satisfied , the kernel will trigger the page recycling mechanism and try to reclaim memory to solve the problem. When performing memory allocation, only when the water level is between the low water level and the min water level, the kswapd background recovery thread will work, and when the water level is lower than the min water level, direct memory recovery will be triggered. Among them, the kernel is the core of the system. Its main responsibilities include process management, disk management, task scheduling and memory management. Software-initiated requests are sent to hardware.

在本申请实施例中,在进行内存分配时,由于系统内存低于预设内存,可能导致内存分配失败,从而使得进程进入慢速路径,在进程进入慢速路径后,会触发内存回收操作。在执行内存回收操作的过程中可以获得单次内存回收持续时间。作为一种方式,可以将单次内存回收持续时间作为内存回收压力,从而获得当前的内存回收压力。In the embodiment of the present application, when the memory allocation is performed, because the system memory is lower than the preset memory, the memory allocation may fail, so that the process enters the slow path. After the process enters the slow path, the memory recovery operation will be triggered. The duration of a single memory recovery can be obtained during the memory recovery operation. As a way, the duration of a single memory recovery can be used as the memory recovery pressure to obtain the current memory recovery pressure.

其中,当系统内存大小高于低水位线时,系统可以快速分配和获取内存,该过程为快速路径;而当系统内存大小低于低水位线时,系统则无法正常分配内存,就要进入到慢速路径。Among them, when the system memory size is higher than the low water mark, the system can quickly allocate and acquire memory, and this process is a fast path; while when the system memory size is lower than the low water mark, the system cannot normally allocate memory and will enter the slow path.

其中,内存回收的类别可以是文件映射的页和匿名页,其中,文件映射的页可以包括pagecache、slab中的dcache、icache、用户进程的可执行程序的代码段以及文件映射页面;匿名页可以包括进程使用各种api申请到的物理内存,包括堆、栈、进程间通信中的共享内存、pipe、bss段、数据段以及tmpfs的页,这部分内容没有办法直接回写,需要为它们创建swap区域,这些页也转化成了文件映射的页,能够回写到磁盘。同时,根据页是否是脏的,系统在进行内存回收的时候的处理方式也有所不同,脏页需要先回写到磁盘再回收,而干净的页可以直接释放。Wherein, the category of memory reclamation can be the page of file mapping and anonymous page, wherein, the page of file mapping can comprise pagecache, dcache in slab, icache, the code segment of the executable program of user process and file mapping page; Anonymous page can be Including the physical memory requested by the process using various APIs, including heap, stack, shared memory in inter-process communication, pipe, bss segment, data segment, and tmpfs pages. There is no way to write back these parts directly, and they need to be created In the swap area, these pages are also converted into file-mapped pages, which can be written back to disk. At the same time, depending on whether the page is dirty, the system handles memory reclamation differently. Dirty pages need to be written back to disk before recycling, while clean pages can be released directly.

在本申请实施例中,可以在系统进行任务调度时获取存储资源压力。存储资源压力可以用来显示一段时间内的IO的运行压力的大小,因此可以通过检测IO负载的大小,将IO负载作为当前的存储资源压力,以此来避免存储资源长时间处于高负载的运行状态。In the embodiment of the present application, the storage resource pressure can be obtained when the system performs task scheduling. Storage resource pressure can be used to display the size of the IO operating pressure for a period of time. Therefore, by detecting the size of the IO load, the IO load can be used as the current storage resource pressure to avoid long-term high-load operation of storage resources. state.

作为一种方式,可以在检测到刚开始进行内存回收时,开始获取当前的内存回收压力。具体的,在内存低于低水位线时,系统会触发进行内存回收。内存回收可以包括kswapd内存回收和直接内存回收,前述两种内存回收的触发条件不同,因此,可以通过检测内存回收的触发条件来确定内存回收方式,进而确定内存回收的开始时间。在确定了内存回收的开始时间后,开始获取当前的内存回收压力。As a way, when it is detected that memory reclamation has just started, it can start to get the current memory reclamation pressure. Specifically, when the memory is lower than the low water mark, the system will trigger memory recovery. Memory reclamation can include kswapd memory reclamation and direct memory reclamation. The trigger conditions of the aforementioned two kinds of memory reclamation are different. Therefore, the memory reclamation method can be determined by detecting the trigger conditions of memory reclamation, and then the start time of memory reclamation can be determined. After determining the start time of memory recovery, start to obtain the current memory recovery pressure.

作为另一种方式,可以在监测到系统通过任务调度子系统进行任务调度时,通过任务调度子系统获取当前的CPU负载。具体的,系统中可以设置有任务调度子系统,该任务调度子系统可以包括进行任务调度以及数据读取的功能。在本申请实施例中,主要运用任务调度子系统的数据读取功能,读取当前的CPU负载。可选的,可以预先为每一种任务设置一个任务标识,进而可以通过检测任务标识来确定任务是否被任务调度子系统调度,当检测到对应的任务标识,确定任务调度子系统正在进行任务调度,开始获取当前的CPU负载。As another way, when it is detected that the system performs task scheduling through the task scheduling subsystem, the current CPU load can be obtained through the task scheduling subsystem. Specifically, a task scheduling subsystem may be provided in the system, and the task scheduling subsystem may include functions of task scheduling and data reading. In the embodiment of the present application, the data reading function of the task scheduling subsystem is mainly used to read the current CPU load. Optionally, a task identifier can be set for each task in advance, and then it can be determined whether the task is scheduled by the task scheduling subsystem by detecting the task identifier. When the corresponding task identifier is detected, it is determined that the task scheduling subsystem is scheduling the task , start to get the current CPU load.

作为另一种方式,可以在检测到系统通过任务调度子系统进行任务调度时,通过任务调度子系统获取当前的IO负载。具体的,系统中可以设置有任务调度子系统,该任务调度子系统可以包括进行任务调度以及数据读取的功能。在本申请实施例中,主要运用任务调度子系统的数据读取功能,读取当前的IO负载,将IO负载作为当前时刻的系统资源压力。可选的,可以预先为每一种任务设置一个任务标识,进而可以通过检测任务标识来确定任务是否被任务调度子系统调度,当检测到对应的任务标识,确定任务调度子系统正在进行任务调度,开始获取当前的IO负载,即系统资源压力。As another manner, when it is detected that the system performs task scheduling through the task scheduling subsystem, the current IO load may be obtained through the task scheduling subsystem. Specifically, a task scheduling subsystem may be provided in the system, and the task scheduling subsystem may include functions of task scheduling and data reading. In the embodiment of the present application, the data reading function of the task scheduling subsystem is mainly used to read the current IO load, and the IO load is used as the system resource pressure at the current moment. Optionally, a task identifier can be set for each task in advance, and then it can be determined whether the task is scheduled by the task scheduling subsystem by detecting the task identifier. When the corresponding task identifier is detected, it is determined that the task scheduling subsystem is scheduling the task , start to get the current IO load, that is, the system resource pressure.

步骤S120:若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。Step S120: If the system resource parameter meets the preset parameter threshold, reduce the optimistic spin time of the sleep lock.

在本申请实施例中,预设参数阈值用于表征对应的系统资源参数满足条件的阈值。其中,预设参数阈值可以包括预设负载阈值、预设压力阈值和预设资源阈值中的至少一种,预设负载阈值、预设压力阈值和预设资源阈值分别与CPU负载、内存回收压力和存储资源压力相对应。In the embodiment of the present application, the preset parameter threshold is used to characterize the threshold that the corresponding system resource parameter satisfies a condition. Wherein, the preset parameter threshold may include at least one of a preset load threshold, a preset pressure threshold, and a preset resource threshold, and the preset load threshold, the preset pressure threshold, and the preset resource threshold are respectively related to the CPU load and the memory recovery pressure. Corresponding to storage resource pressure.

其中,睡眠锁的乐观自旋是指在进程申请锁失败后,进入睡眠状态前,在一定的时间阈值内进行自旋等待。其中时间阈值为系统设置。Among them, the optimistic spin of the sleep lock means that after the process fails to apply for the lock, it spins and waits within a certain time threshold before entering the sleep state. The time threshold is set by the system.

在本申请实施例中,可以通过前述方法获取的系统资源参数与预先设置的预设参数阈值进行比较,在确定当前的系统资源参数满足预设参数阈值的情况下,减少睡眠锁的乐观自旋时间。其中,当前的系统资源参数满足预设参数阈值可以包括CPU负载满足预设负载阈值,内存回收压力满足压力阈值,以及存储资源压力满足预设资源阈值中的至少一种。比如,在系统资源参数只包括CPU负载的情况下,则当前的系统资源参数满足预设参数阈值包括CPU负载满足预设负载阈值;在系统资源参数包括CPU负载、内存回收压力和存储资源压力的情况下,当前的系统资源参数满足预设参数阈值可以包括CPU负载满足预设负载阈值,内存回收压力满足压力阈值,以及存储资源压力满足预设资源阈值。In the embodiment of the present application, the system resource parameters obtained by the aforementioned method are compared with the preset preset parameter thresholds, and when it is determined that the current system resource parameters meet the preset parameter thresholds, the optimistic spin of the sleep lock is reduced time. Wherein, the current system resource parameter meeting the preset parameter threshold may include at least one of CPU load meeting the preset load threshold, memory reclamation pressure meeting the pressure threshold, and storage resource pressure meeting the preset resource threshold. For example, in the case where the system resource parameters only include the CPU load, the current system resource parameters satisfy the preset parameter threshold including the CPU load meeting the preset load threshold; In some cases, the current system resource parameter meeting the preset parameter threshold may include that the CPU load meets the preset load threshold, the memory reclamation pressure meets the pressure threshold, and the storage resource pressure meets the preset resource threshold.

在检测到当前的系统资源参数满足预设参数阈值的情况下,减少睡眠锁的乐观自旋时间。在减少睡眠锁的乐观自旋时间时,可以根据当前的系统资源参数的大小来确定对应减少的乐观自旋时间,当前的系统资源参数越大,减少的乐观自旋时间越多。When it is detected that the current system resource parameter meets the preset parameter threshold, the optimistic spin time of the sleep lock is reduced. When reducing the optimistic spin time of the sleep lock, the corresponding reduced optimistic spin time can be determined according to the size of the current system resource parameter. The larger the current system resource parameter is, the more the reduced optimistic spin time is.

本申请提供的一种睡眠锁优化方法,首先获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种,然后若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。通过上述方法,通过将CPU负载、内存回收压力和存储资源压力与预设参数阈值进行比较,在CPU负载、内存回收压力和存储资源压力均满足预设参数阈值后再减少睡眠锁的乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。A sleeping lock optimization method provided by the present application, first obtains the current system resource parameters, the system resource parameters include at least one of CPU load, memory recovery pressure and storage resource pressure, and then if the system resource parameters meet the preset Set the parameter threshold to reduce the optimistic spin time of the sleep lock. Through the above method, by comparing the CPU load, memory recovery pressure and storage resource pressure with the preset parameter thresholds, reduce the optimistic spin of the sleep lock after the CPU load, memory recovery pressure and storage resource pressure all meet the preset parameter thresholds Time, you can adjust the optimistic spin time of the sleep lock, thereby reducing the system load and the lock waiting time of the process, thereby avoiding the excessive CPU load caused by the optimistic spin time being too long.

请参阅图2,本申请实施例提供了一种睡眠锁优化方法,所述方法包括:Please refer to Fig. 2, the embodiment of the present application provides a sleep lock optimization method, the method includes:

步骤S201:响应于线程发送的持锁申请,获取当前的睡眠锁的状态。Step S201: Obtain the current state of the sleep lock in response to the lock request sent by the thread.

在本申请实施例中,线程在持睡眠锁之前需要先申请持锁,因此,线程可以发送持锁申请,系统在接收到线程发送的持锁申请后,响应于该持锁申请,获取当前的睡眠锁的状态。In the embodiment of this application, the thread needs to apply for the lock before holding the sleep lock. Therefore, the thread can send the lock application. After receiving the lock application sent by the thread, the system responds to the lock application and obtains the current The state of the sleep lock.

步骤S202:若所述睡眠锁为非空闲状态,获取当前的睡眠锁的锁类型。Step S202: If the sleep lock is not idle, acquire the lock type of the current sleep lock.

作为一种方式,若检测到当前的睡眠锁被其它线程持锁,则确定睡眠锁为非空闲状态,线程获取当前的睡眠锁的锁类型。其中,睡眠锁的锁类型可以包括专用锁和通用锁两种类型。其中,专用锁为只与单一的系统资源相关联的睡眠锁,系统资源包括但不限于CPU资源、内存资源、存储资源或网络资源;通用锁为与CPU资源、内存资源和存储资源相关联的睡眠锁。As a manner, if it is detected that the current sleep lock is held by other threads, then it is determined that the sleep lock is in a non-idle state, and the thread acquires the lock type of the current sleep lock. Wherein, the lock type of the sleep lock may include two types of special lock and general lock. Among them, the dedicated lock is a sleep lock associated only with a single system resource. System resources include but not limited to CPU resources, memory resources, storage resources or network resources; general locks are sleep locks associated with CPU resources, memory resources and storage resources. sleep lock.

作为另一种方式,若检测到当前的睡眠锁未被其它线程持锁,则确定睡眠锁为空闲状态,线程直接持锁。As another way, if it is detected that the current sleep lock is not held by other threads, it is determined that the sleep lock is in an idle state, and the thread directly holds the lock.

步骤S203:若所述睡眠锁的锁类型为通用锁,检测所述系统资源参数是否满足所述预设参数阈值。Step S203: If the lock type of the sleep lock is a general lock, detect whether the system resource parameter satisfies the preset parameter threshold.

在本申请实施例中,系统资源参数包括三部分参数,一是CPU负载,二是内存回收压力,三是存储资源压力;预设参数阈值包括三部分参数,一是预设负载阈值,二是预设压力阈值,三是预设资源阈值。其中,预设负载阈值对应于CPU负载,预设压力阈值对应于内存回收压力,预设资源阈值对应于存储资源压力。其中,预设参数阈值可以为一固定数值,也可以根据线程进行调整,在此不作具体限定。In the embodiment of this application, the system resource parameters include three parts of parameters, one is CPU load, the other is memory recovery pressure, and the third is storage resource pressure; the preset parameter threshold includes three parts of parameters, one is the preset load threshold, and the other is The preset pressure threshold, and the third is the preset resource threshold. Wherein, the preset load threshold corresponds to CPU load, the preset pressure threshold corresponds to memory recovery pressure, and the preset resource threshold corresponds to storage resource pressure. Wherein, the preset parameter threshold may be a fixed value, or may be adjusted according to threads, which is not specifically limited here.

作为一种方式,获取存储资源压力的方法如步骤S2031和步骤S2032所述,获取内存回收压力的方法如步骤S2033、S2034和S2035所述,获取CPU负载的方法如步骤S2036和步骤S2037所述。As a method, the method for obtaining storage resource pressure is described in steps S2031 and S2032, the method for obtaining memory recovery pressure is described in steps S2033, S2034 and S2035, and the method for obtaining CPU load is described in steps S2036 and S2037.

当系统获取到CPU负载、内存回收压力和存储资源压力后,可以将CPU负载、内存回收压力和存储资源压力与预设参数阈值进行比较判断,来确定系统资源参数是否满足预设参数阈值。After the system obtains the CPU load, memory recovery pressure, and storage resource pressure, it can compare and judge the CPU load, memory recovery pressure, and storage resource pressure with the preset parameter thresholds to determine whether the system resource parameters meet the preset parameter thresholds.

步骤S2031获取预设时间段内的系统IO总量。Step S2031 obtains the total amount of system IO within a preset time period.

在本申请实施例中,系统在进行任务调度时,可以获取某一时间段的各个IO节点的IO总量。其中,时间段可以被设置为任意数值,示例性的,该时间段可以设置为4ms。In the embodiment of the present application, when the system performs task scheduling, it can obtain the total amount of IO of each IO node in a certain period of time. Wherein, the time period may be set to any value, for example, the time period may be set to 4ms.

同时,每次进行IO负载计算有可能导致系统的负载增加,进而影响系统性能,因此可以每间隔预设时间段后获取系统的IO总量,从而减少IO负载计算给系统性能带来的影响。其中,该预设时间段可以预先设置,也可以根据任务的不同进行设置。示例性的,若调度的任务的数量大于预设数量阈值,则将预设时间段进行延长;若调度的任务的数量小于或等于预设数量阈值,则将预设时间段进行缩短,使得可以减少IO负载计算的次数,即减少存储资源压力的计算次数,从而可以减少存储资源压力计算对系统带来的影响。At the same time, each IO load calculation may lead to an increase in the system load, thereby affecting system performance. Therefore, the total IO of the system can be obtained after each preset time period, thereby reducing the impact of IO load calculation on system performance. Wherein, the preset time period can be set in advance, or can be set according to different tasks. Exemplarily, if the number of scheduled tasks is greater than the preset number threshold, the preset time period is extended; if the number of scheduled tasks is less than or equal to the preset number threshold, the preset time period is shortened, so that Reduce the number of IO load calculations, that is, reduce the number of storage resource pressure calculations, thereby reducing the impact of storage resource pressure calculations on the system.

步骤S2032:基于所述预设时间段的IO总量,计算得到IO负载,将所述IO负载作为当前的系统资源参数。Step S2032: Calculate the IO load based on the total amount of IO in the preset time period, and use the IO load as a current system resource parameter.

在本申请实施例中,系统通过任务调度获得各个IO节点的IO总量,从而将各个IO节点的IO总量通过计算处理可以得到当前的IO负载,计算公式如下:IO负载=IO总量/时间,即该IO负载的数值由IO总量除以时间得到。In the embodiment of this application, the system obtains the total amount of IO of each IO node through task scheduling, so that the current IO load can be obtained by calculating the total amount of IO of each IO node. The calculation formula is as follows: IO load = total amount of IO/ Time, that is, the value of the IO load is obtained by dividing the total amount of IO by the time.

在通过上述方法获取到当前的IO负载后,可以通过系统检测该任务是否调度完成,若完成该任务调度,则系统进行下一次的任务调度,并结束此次IO的负载监测。After obtaining the current IO load through the above method, the system can detect whether the task scheduling is completed. If the task scheduling is completed, the system will perform the next task scheduling and end the IO load monitoring this time.

示例性,步骤S2031、步骤S2032所述的过程可以如图3所示,系统在进行IO负载监测时,首先系统会调度子系统,以此来获取一段时间系统的IO总量,再根据系统的IO总量计算IO负载,然后调度下一个任务,最后结束监测。Exemplarily, the process described in step S2031 and step S2032 can be shown in Figure 3. When the system is monitoring the IO load, the system will first schedule the subsystems to obtain the total IO of the system for a period of time, and then according to the system's The total amount of IO calculates the IO load, then schedules the next task, and finally ends the monitoring.

步骤S2033:若当前的空闲内存低于预设内存,则开始进行内存回收操作。Step S2033: If the current free memory is lower than the preset memory, start memory recovery operation.

在本申请实施例中,系统在进行内存回收操作之前,需要监测当前系统的空闲内存与预设内存之间的关系,若当前系统的空闲内存低于预设内存,则触发开始进行内存回收操作。其中,预设内存用于表征当前系统的空闲内存很低,预设内存可以为用户设置的一个具体数值,也可以为系统内含的内存阈值,系统内含的内存阈值可以为low水位线,在此不做具体限定。In the embodiment of this application, before the system performs the memory reclamation operation, it needs to monitor the relationship between the free memory of the current system and the preset memory. If the free memory of the current system is lower than the preset memory, the memory reclamation operation will be triggered . Among them, the preset memory is used to indicate that the free memory of the current system is very low. The preset memory can be a specific value set by the user, or it can be the memory threshold contained in the system. The memory threshold contained in the system can be the low water level, No specific limitation is made here.

步骤S2034:获取所述内存回收操作对应的开始时间和结束时间。Step S2034: Obtain the start time and end time corresponding to the memory recovery operation.

在本申请实施例中,在系统进行内存回收操作的过程中,系统在内存回收操作开始时记录开始时间,在内存回收操作结束时记录结束时间。In the embodiment of the present application, when the system performs the memory recovery operation, the system records the start time when the memory recovery operation starts, and records the end time when the memory recovery operation ends.

步骤S2035:基于所述开始时间和所述结束时间,确定当前的内存回收压力。Step S2035: Determine the current memory recovery pressure based on the start time and the end time.

在本申请实施例中,基于内存回收操作对应的开始时间和结束时间,可以得到内存回收操作的单次内存回收持续时间。其中,单次内存回收持续时间可以为结束时间减去开始时间,单次内存回收持续时间的数值大小即为内存回收压力的数值大小。In the embodiment of the present application, based on the start time and end time corresponding to the memory recovery operation, the duration of a single memory recovery operation of the memory recovery operation can be obtained. Wherein, the duration of a single memory recovery may be the end time minus the start time, and the value of the duration of a single memory recovery is the value of memory recovery pressure.

当通过上述方法获取到内存回收压力后,可以通过系统判断系统当前的空闲内存是否大于预设内存。若系统监测到进行一次内存回收后,系统当前的空闲内存还是低于预设内存,则再次进行内存回收操作,同时记录单次内存回收持续时间的数值;若系统监测到进行一次内存回收后,系统当前的空闲内存大于预设内存后,则将单次内存回收持续时间设置为0,并结束内存回收操作,以防止系统在对内存进行判断时出现误判断,避免内存回收压力的监测出现错误。After the memory recovery pressure is obtained through the above method, the system can determine whether the current free memory of the system is greater than the preset memory. If the system detects that the current free memory of the system is still lower than the preset memory after a memory reclamation, it will perform the memory reclamation operation again and record the value of the duration of a single memory reclamation; if the system detects that a memory reclamation is performed, When the current free memory of the system is greater than the preset memory, the duration of a single memory recovery is set to 0, and the memory recovery operation is ended to prevent the system from misjudging the memory and avoid errors in the monitoring of memory recovery pressure .

示例性,步骤S2033、步骤S2034和步骤S2035所述的过程可以如图4所示,系统在进行内存负载监测时,首先会进行内存分配,在系统发现系统当前的空闲内存低于预设内存时,进行内存回收。在进行内存回收时,将内存回收的开始时间记为时间戳1,将内存回收的结束时间记为时间戳2。通过时间戳1和时间戳2可以计算得到单次内存回收持续时间,将单次内存回收持续时间记为STALL_TIME,该STALL_TIME为时间戳2减去时间戳1,在当次内存回收结束后,可以先判断系统当前的空闲内存是否大于预设内存,若系统当前的空闲内存大于或等于预设内存,则确定系统当前的空闲内存充足,进而可以将STALL_TIME设置为0,并结束内存回收;若系统当前的空闲内存小于预设内存,则系统当前的空闲内存不充足,需要继续进行内存回收。Exemplarily, the process described in step S2033, step S2034 and step S2035 can be shown in Figure 4, when the system is performing memory load monitoring, memory allocation will be performed first, and when the system finds that the current free memory of the system is lower than the preset memory , for memory recovery. When memory recovery is performed, the start time of memory recovery is recorded as timestamp 1, and the end time of memory recovery is recorded as time stamp 2. The duration of a single memory recovery can be calculated through timestamp 1 and timestamp 2, and the duration of a single memory recovery is recorded as STALL_TIME. The STALL_TIME is timestamp 2 minus timestamp 1. After the current memory recovery is over, you can First judge whether the current free memory of the system is greater than the preset memory, if the current free memory of the system is greater than or equal to the preset memory, then determine that the current free memory of the system is sufficient, and then set STALL_TIME to 0, and end memory recovery; if the system If the current free memory is less than the preset memory, the current free memory of the system is not sufficient, and memory recovery needs to be continued.

步骤S2036:获取预设时间段内CPU的运行时间和空闲时间。Step S2036: Obtain the running time and idle time of the CPU within a preset time period.

在本申请实施例中,系统在进行任务调度时,可以获取某一时间段内的各个CPU节点的运行时间和空闲时间。其中,时间段可以被设置为任意数值,示例性的,该时间段可以设置为1s。其中,CPU空闲时间主要是由于CPU和I/O设备运行速度不匹配而产生的,而系统为了能够充分利用CPU资源,不允许CPU存在过多的空闲时间,系统一般从利用CPU空闲时间和减少CPU空闲时间两个方面来提出解决方案,其中常用的方法包括多道程序、DMA和I/O通道。In the embodiment of the present application, when the system performs task scheduling, it can obtain the running time and idle time of each CPU node within a certain period of time. Wherein, the time period may be set to any value, for example, the time period may be set to 1s. Among them, the CPU idle time is mainly caused by the mismatch between CPU and I/O device operating speeds. In order to make full use of CPU resources, the system does not allow the CPU to have too much idle time. The system generally uses CPU idle time and reduces Two aspects of CPU idle time are used to propose solutions, among which commonly used methods include multi-programming, DMA and I/O channels.

同时,每次进行CPU负载计算有可能导致系统的负载增加,进而影响系统性能,因此可以每间隔预设时间段后获取一次CPU的运行时间和空闲时间,从而可以减少CPU负载计算给系统性能带来的影响。其中,该预设时间段可以预先设置,也可以根据任务不同进行设置。示例性的,该预设时间段可以简单地设置为100ms,也可以根据调度的任务的数量对预设时间段进行设置,比如,若调度的任务的数量大于预设数量阈值,则将预设时间段进行延长;若调度的任务的数量小于或等于预设数量阈值,则将预设时间段进行缩短,使得可以减少CPU负载计算的次数,从而可以减少CPU负载计算对系统带来的影响。At the same time, each CPU load calculation may lead to an increase in the system load, thereby affecting system performance. Therefore, the running time and idle time of the CPU can be obtained after a preset period of time, thereby reducing the impact of CPU load calculation on system performance. coming impact. Wherein, the preset time period can be set in advance, or can be set according to different tasks. Exemplarily, the preset time period can be simply set to 100ms, and the preset time period can also be set according to the number of scheduled tasks, for example, if the number of scheduled tasks is greater than the preset number threshold, the preset The time period is extended; if the number of scheduled tasks is less than or equal to the preset number threshold, the preset time period is shortened, so that the number of CPU load calculations can be reduced, thereby reducing the impact of CPU load calculations on the system.

步骤S2037:基于所述运行时间和所述空闲时间,确定当前的CPU负载。Step S2037: Determine the current CPU load based on the running time and the idle time.

在本申请实施例中,系统通过任务调度获得各个CPU节点的运行时间和空闲时间,从而将各个CPU节点的运行时间和空闲时间通过计算处理可以得到当前的CPU负载,计算式如下:CPU负载=运行时间/(空闲时间+运行时间),即该CPU负载的数值由运行时间除以运行时间和空闲时间之和得到。In this embodiment of the application, the system obtains the running time and idle time of each CPU node through task scheduling, so that the current CPU load can be obtained by calculating the running time and idle time of each CPU node. The calculation formula is as follows: CPU load = Running time/(idle time+running time), that is, the value of the CPU load is obtained by dividing the running time by the sum of the running time and idle time.

在通过上述方法获取到当前的CPU负载后,可以通过系统监测该任务是否调度完成,若完成该任务调度,则系统进行下一次的任务调度,并结束此次CPU的负载监测。After obtaining the current CPU load through the above method, the system can monitor whether the task scheduling is completed. If the task scheduling is completed, the system will perform the next task scheduling and end the current CPU load monitoring.

示例性的,步骤S2036和步骤S2037所述的过程可以如图5所示,系统在进行监测CPU负载时,系统通过调度子系统获取一段时间系统CPU运行时间和空闲时间,再根据运行时间和空闲时间计算CPU负载,负载计算完成后再调度下一个任务,最后监测结束。Exemplarily, the process described in step S2036 and step S2037 can be shown in Figure 5. When the system is monitoring the CPU load, the system obtains the running time and idle time of the system CPU for a period of time through the scheduling subsystem, and then according to the running time and idle time The time calculates the CPU load, and after the load calculation is completed, the next task is scheduled, and finally the monitoring ends.

步骤S204:若所述系统资源满足所述预设参数阈值,则减少所述睡眠锁的乐观自旋时间。Step S204: If the system resource satisfies the preset parameter threshold, reduce the optimistic spin time of the sleep lock.

作为一种方式,若所述系统资源参数包括CPU负载,且所述CPU负载大于预设负载阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间。As a manner, if the system resource parameter includes a CPU load, and the CPU load is greater than a preset load threshold, it is determined that the preset parameter threshold is met, and the optimistic spin time of the sleep lock is reduced.

作为一种方式,将当前的CPU负载与预设负载阈值进行比较,确定满足条件后减少睡眠锁的乐观自选时间。包括:若CPU负载大于预设负载阈值,则满足预设参数阈值,减少睡眠锁的乐观自旋时间。As a way, the current CPU load is compared with a preset load threshold to determine an optimistic self-selection time for reducing the sleep lock after the condition is met. Including: if the CPU load is greater than the preset load threshold, the preset parameter threshold is satisfied, and the optimistic spin time of the sleep lock is reduced.

在本申请实施例中,系统减少睡眠锁的乐观自旋时间之前需要对系统资源参数与预设参数阈值进行比较,在满足条件后才能减少睡眠锁的乐观自旋时间。由于进程申请持锁后,睡眠锁为非空闲状态,触发获取CPU负载操作,系统通过获取CPU的运行时间和空闲时间来得到CPU负载,并将CPU负载与预设负载阈值进行比较,若CPU负载大于预设负载阈值,则系统减少睡眠锁的乐观自旋时间。In the embodiment of the present application, before reducing the optimistic spin time of the sleep lock, the system needs to compare the system resource parameter with the preset parameter threshold, and the optimistic spin time of the sleep lock can be reduced only after the condition is met. After the process applies for the lock, the sleep lock is in a non-idle state, which triggers the operation of obtaining the CPU load. The system obtains the CPU load by obtaining the running time and idle time of the CPU, and compares the CPU load with the preset load threshold. If the CPU load is greater than the preset load threshold, the system reduces the optimistic spin time of the sleep lock.

作为另一种方式,若所述系统资源参数包括内存回收压力,且所述内存回收压力大于预设压力阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间。As another manner, if the system resource parameter includes memory reclamation pressure, and the memory reclamation pressure is greater than a preset pressure threshold, it is determined that the preset parameter threshold is satisfied, and the optimistic spin time of the sleep lock is reduced.

作为一种方式,将当前的内存回收压力与预设压力阈值进行比较,确定满足条件后减少睡眠锁的乐观自旋时间。包括:若内存回收压力大于预设压力阈值,则满足预设参数阈值,减少睡眠锁的乐观自旋时间。As a way, the current memory recovery pressure is compared with the preset pressure threshold, and the optimistic spin time of the sleep lock is reduced after the condition is met. Including: if the memory recovery pressure is greater than the preset pressure threshold, the preset parameter threshold is met, and the optimistic spin time of the sleep lock is reduced.

在本申请实施例中,系统减少睡眠锁的乐观自旋时间之前需要对系统资源参数与预设参数阈值进行比较,在满足条件后才能减少睡眠锁的乐观自旋时间。由于进程申请持锁后,睡眠锁为非空闲状态,进而触发获取内存回收压力操作,系统通过获取单次内存回收持续时间来获取内存回收压力,将内存回收压力与预设压力阈值进行比较,若内存回收压力大于预设压力阈值,则系统减少睡眠锁的乐观自旋时间。In the embodiment of the present application, before reducing the optimistic spin time of the sleep lock, the system needs to compare the system resource parameter with the preset parameter threshold, and the optimistic spin time of the sleep lock can be reduced only after the condition is met. After the process applies for the lock, the sleep lock is in a non-idle state, which triggers the operation of obtaining memory recovery pressure. The system obtains the memory recovery pressure by obtaining the duration of a single memory recovery, and compares the memory recovery pressure with the preset pressure threshold. If If the memory recovery pressure is greater than the preset pressure threshold, the system reduces the optimistic spin time of the sleep lock.

可选的,若所述系统资源参数包括存储资源压力,且所述存储资源压力大于预设资源阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间。Optionally, if the system resource parameters include storage resource pressure, and the storage resource pressure is greater than a preset resource threshold, it is determined that the preset parameter threshold is met, and the optimistic spin time of the sleep lock is reduced.

作为一种方式,将当前的存储资源压力与预设资源阈值进行比较,确定满足条件后减少睡眠锁的乐观自旋时间。包括:存储资源压力大于预设压力阈值,则满足预设参数阈值,减少睡眠锁的乐观自旋时间。As a way, the current storage resource pressure is compared with the preset resource threshold, and the optimistic spin time of the sleep lock is reduced after the condition is satisfied. Including: if the storage resource pressure is greater than the preset pressure threshold, the preset parameter threshold is met, and the optimistic spin time of the sleep lock is reduced.

在本申请实施例中,系统减少睡眠锁的乐观自旋时间之前需要对系统资源参数与预设参数阈值进行比较,在满足条件后才能减少睡眠锁的乐观自旋时间。由于进程申请持锁后,睡眠锁为非空闲状态,进而触发获取存储资源压力操作,系统通过获取一段时间内系统的IO总量来获取IO负载,将获取的IO负载作为存储资源压力,将存储资源压力与预设资源阈值进行比较,若存储资源压力大于预设资源阈值,则系统减少睡眠锁的乐观自旋时间。In the embodiment of the present application, before reducing the optimistic spin time of the sleep lock, the system needs to compare the system resource parameter with the preset parameter threshold, and the optimistic spin time of the sleep lock can be reduced only after the condition is met. After the process applies for the lock, the sleep lock is not idle, which triggers the operation of obtaining storage resource pressure. The system obtains the IO load by obtaining the total IO of the system within a period of time. The resource pressure is compared with the preset resource threshold. If the storage resource pressure is greater than the preset resource threshold, the system reduces the optimistic spin time of the sleep lock.

可选的,若所述系统资源参数包括CPU负载、内存回收压力、存储资源压力,所述CPU负载大于预设负载阈值,且所述内存回收压力大于预设压力阈值,且所述存储资源压力大于预设资源阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间。Optionally, if the system resource parameters include CPU load, memory recovery pressure, and storage resource pressure, the CPU load is greater than a preset load threshold, and the memory recovery pressure is greater than a preset pressure threshold, and the storage resource pressure If it is greater than the preset resource threshold, it is determined that the preset parameter threshold is met, and the optimistic spin time of the sleep lock is reduced.

在本申请实施例中,系统减少睡眠锁的乐观自旋时间之前需要对系统资源参数与预设参数阈值进行比较,在满足条件后才能减少睡眠锁的乐观自旋时间。由于进程申请持锁后,睡眠锁为非空闲状态,进而触发获取CPU负载、内存回收压力和存储资源压力的操作,系统通过获取系统资源参数,将系统资源参数与预设参数阈值进行比较,若系统资源参数大于预设参数阈值,确定满足预设参数阈值,减少睡眠锁的乐观自旋时间。In the embodiment of the present application, before reducing the optimistic spin time of the sleep lock, the system needs to compare the system resource parameter with the preset parameter threshold, and the optimistic spin time of the sleep lock can be reduced only after the condition is met. Since the sleep lock is in a non-idle state after the process applies for the lock, the operation of obtaining the CPU load, memory recovery pressure, and storage resource pressure is triggered. The system obtains the system resource parameters and compares the system resource parameters with the preset parameter thresholds. If If the system resource parameter is greater than the preset parameter threshold, it is determined that the preset parameter threshold is met, and the optimistic spin time of the sleep lock is reduced.

步骤S205:若所述系统资源参数未满足预设参数阈值,则保持所述睡眠锁的乐观自旋时间为预设时间。Step S205: If the system resource parameter does not meet the preset parameter threshold, keep the optimistic spin time of the sleep lock as the preset time.

作为一种方式,将系统资源参数与预设参数阈值进行比较,确定系统资源参数未满足预设参数阈值后保持睡眠锁的乐观自旋时间。步骤S205的具体步骤包括:若所述系统资源参数包括CPU负载、内存回收压力、存储资源压力,且CPU负载小于或等于预设负载阈值,且内存回收压力小于或等于预设压力阈值,且存储资源压力小于或等于预设资源阈值,则系统资源参数未满足预设参数阈值,保持睡眠锁的乐观自旋时间。As a manner, the system resource parameter is compared with a preset parameter threshold to determine an optimistic spin time for maintaining the sleep lock after the system resource parameter does not meet the preset parameter threshold. The specific steps of step S205 include: if the system resource parameters include CPU load, memory recovery pressure, and storage resource pressure, and the CPU load is less than or equal to a preset load threshold, and the memory recovery pressure is less than or equal to a preset pressure threshold, and storing If the resource pressure is less than or equal to the preset resource threshold, the system resource parameters do not meet the preset parameter threshold, and the optimistic spin time of the sleep lock is maintained.

在本申请实施例中,系统保持睡眠锁的乐观自旋时间之前需要对系统资源参数与预设参数阈值进行比较,在未满足条件后才能保持睡眠锁的乐观自旋时间。由于进程申请持锁后,睡眠锁为非空闲状态,进而触发获取CPU负载、内存回收压力和存储资源压力操作,将CPU负载、内存回收压力和存储资源压力与预设负载阈值、预设压力阈值和预设资源阈值进行比较,若CPU负载小于或等于预设负载阈值,且内存回收压力小于或等于预设压力阈值,且存储资源压力小于或等于预设资源阈值,则系统保持睡眠锁的乐观自旋时间。In the embodiment of the present application, before the system maintains the optimistic spin time of the sleep lock, the system needs to compare the system resource parameter with the preset parameter threshold, and the optimistic spin time of the sleep lock can only be maintained after the condition is not met. Since the sleep lock is not idle after the process applies for the lock, it triggers the operation of obtaining CPU load, memory recovery pressure and storage resource pressure, and compares the CPU load, memory recovery pressure and storage resource pressure with the preset load threshold and preset pressure threshold Compared with the preset resource threshold, if the CPU load is less than or equal to the preset load threshold, and the memory recovery pressure is less than or equal to the preset pressure threshold, and the storage resource pressure is less than or equal to the preset resource threshold, the system maintains the optimism of the sleep lock spin time.

本申请提供的一种睡眠锁优化方法,首先响应于线程发送的持锁申请,获取当前的睡眠锁的状态,然后若所述睡眠锁为空闲状态,获取当前的睡眠锁的锁类型,若所述睡眠锁的锁类型为通用锁,检测所述系统资源参数是否满足所述预设参数阈值,若所述系统资源满足所述预设参数阈值,则减少所述睡眠锁的乐观自旋时间,若所述系统资源参数未满足预设参数阈值,则保持所述睡眠锁的乐观自旋时间为预设时间。通过上述方法,通过将特定资源与预设参数阈值进行比较,在特定资源满足预设参数阈值后减少乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。A sleep lock optimization method provided by the present application first responds to the lock holding application sent by the thread to obtain the state of the current sleep lock, and then if the sleep lock is in an idle state, obtains the lock type of the current sleep lock. The lock type of the sleep lock is a general lock, and it is detected whether the system resource parameter meets the preset parameter threshold, and if the system resource meets the preset parameter threshold, the optimistic spin time of the sleep lock is reduced, If the system resource parameter does not meet the preset parameter threshold, keep the optimistic spin time of the sleep lock as the preset time. Through the above method, by comparing the specific resource with the preset parameter threshold and reducing the optimistic spin time after the specific resource meets the preset parameter threshold, the optimistic spin time of the sleep lock can be adjusted, thereby reducing the system load and process load etc. Lock time, thereby avoiding excessive CPU load caused by too long optimistic spin time.

请参阅图6,本申请实施例提供了一种睡眠锁优化方法,所述方法包括:Referring to Fig. 6, the embodiment of the present application provides a sleep lock optimization method, the method comprising:

步骤S301:响应于线程发送的持锁申请,获取当前的睡眠锁的状态。Step S301: In response to the lock request sent by the thread, obtain the current state of the sleep lock.

步骤S301具体可以参考上述实施例中的详细解释,故不在本实施例中进行赘述。For details of step S301, reference may be made to the detailed explanations in the foregoing embodiments, so details are not described in this embodiment.

步骤S302:若所述睡眠锁为非空闲状态,获取当前的睡眠锁的锁类型。Step S302: if the sleep lock is not idle, acquire the lock type of the current sleep lock.

步骤S302具体可以参考上述实施例中的详细解释,故不在本实施例中进行赘述。For details of step S302, reference may be made to the detailed explanations in the foregoing embodiments, so details are not described in this embodiment.

步骤S303:若所述睡眠锁的锁类型为专用锁,检测所述系统资源参数是否满足所述预设参数阈值。Step S303: If the lock type of the sleep lock is a dedicated lock, detect whether the system resource parameter meets the preset parameter threshold.

在本申请实施例中,系统资源参数包括CPU负载,内存回收压力和存储资源压力,同时,专用锁也有其它与之相关联的系统资源,包括网络资源和USB传输资源。根据与专用锁相关联的系统资源不同,系统资源参数也不同,专用锁只针对一种系统资源参数进行比较判断。示例性的,若专用锁涉及的系统资源为CPU资源,则预设参数阈值为预设负载阈值;若专用锁涉及的系统资源为内存回收资源,则预设参数阈值为预设压力阈值;若专用锁涉及的系统资源为存储资源,则预设参数阈值为预设资源阈值。In the embodiment of the present application, the system resource parameters include CPU load, memory recovery pressure and storage resource pressure. Meanwhile, the dedicated lock also has other system resources associated with it, including network resources and USB transmission resources. According to the different system resources associated with the dedicated lock, the system resource parameters are also different, and the dedicated lock only compares and judges one kind of system resource parameter. Exemplarily, if the system resource involved in the dedicated lock is a CPU resource, the preset parameter threshold is a preset load threshold; if the system resource involved in the dedicated lock is a memory recovery resource, the preset parameter threshold is a preset pressure threshold; if The system resources involved in the dedicated lock are storage resources, and the preset parameter threshold is the preset resource threshold.

当系统根据专用锁的不同获取到系统资源参数后,可以将系统资源参数与预设参数阈值进行比较判断,来确定系统资源参数是否满足预设参数阈值。After the system obtains the system resource parameters according to different dedicated locks, it can compare and judge the system resource parameters with the preset parameter thresholds to determine whether the system resource parameters meet the preset parameter thresholds.

作为一种方式,获取存储资源压力的方法如步骤S2031和步骤S2032所述,获取内存回收压力的方法如步骤S2033、S2034和S2035所述,获取CPU负载的方法如步骤S2036和步骤S2037所述。As a method, the method for obtaining storage resource pressure is described in steps S2031 and S2032, the method for obtaining memory recovery pressure is described in steps S2033, S2034 and S2035, and the method for obtaining CPU load is described in steps S2036 and S2037.

对于专用锁,若所述系统资源参数包括CPU负载、内存回收压力或存储资源压力中任一参数,且所述系统资源压力小于或等于预设参数阈值,确定未满足所述预设参数阈值,则保持所述睡眠锁的乐观自旋时间为预设时间。For a dedicated lock, if the system resource parameters include any parameter in CPU load, memory recovery pressure, or storage resource pressure, and the system resource pressure is less than or equal to a preset parameter threshold, it is determined that the preset parameter threshold is not met, Then keep the optimistic spin time of the sleep lock as the preset time.

在本申请实施例中,专用锁相关联的系统资源只涉及一种,系统资源包含但不限于CPU负载、内存回收压力、存储资源压力、网络资源或USB传输资源中的一种,当系统资源参数小于或等于预设资源阈值时,系统资源参数不满足预设参数阈值,系统保持睡眠锁的乐观自旋时间为预设时间。其中,乐观自旋的预设时间为系统自行设置。In this embodiment of the application, only one type of system resource is associated with a dedicated lock, and the system resource includes but is not limited to one of CPU load, memory recovery pressure, storage resource pressure, network resource, or USB transmission resource. When the parameter is less than or equal to the preset resource threshold, the system resource parameter does not meet the preset parameter threshold, and the system keeps the optimistic spin time of the sleep lock as the preset time. Among them, the preset time of optimistic spin is set by the system itself.

步骤S304:若所述系统资源参数满足所述预设参数阈值,获取所述线程的类型。Step S304: If the system resource parameter satisfies the preset parameter threshold, acquire the type of the thread.

在本申请实施例中,若CPU负载大于预设负载阈值,或内存回收压力大于预设压力阈值,或存储资源压力大于预设资源阈值,则系统获取线程类型。线程类型可以分为后台普通线程和后台非普通线程,其中,后台普通线程和后台非普通线程为系统自行标志。In this embodiment of the present application, if the CPU load is greater than a preset load threshold, or the memory recovery pressure is greater than the preset pressure threshold, or the storage resource pressure is greater than the preset resource threshold, the system acquires the thread type. Thread types can be divided into background ordinary threads and background non-ordinary threads, among which, background ordinary threads and background non-ordinary threads are marked by the system itself.

步骤S305:若所述线程的类型不为后台普通线程,则减少所述睡眠锁的乐观自旋时间。Step S305: If the type of the thread is not a background ordinary thread, reduce the optimistic spin time of the sleep lock.

在本申请实施例中,若是专用锁,且系统检测到线程类型不为后台普通线程,则减少睡眠锁的乐观自旋时间。In the embodiment of this application, if it is a dedicated lock, and the system detects that the thread type is not a background ordinary thread, then reduce the optimistic spin time of the sleep lock.

步骤S306:若所述线程的类型是后台普通线程,则控制所述睡眠锁进入睡眠。Step S306: If the type of the thread is a background ordinary thread, control the sleep lock to enter sleep.

在本申请实施例中,若是专用锁,且系统检测到线程类型为后台普通线程,则控制睡眠锁进入睡眠状态。In the embodiment of the present application, if it is a dedicated lock and the system detects that the thread type is a background ordinary thread, the sleep lock is controlled to enter the sleep state.

本申请提供的一种睡眠锁优化方法,首先响应于线程发送的持锁申请,获取当前的睡眠锁的状态,然后若所述睡眠锁为非空闲状态,获取当前的睡眠锁的锁类型,若所述睡眠锁的锁类型为专用锁,检测所述系统资源参数是否满足所述预设参数阈值,若所述系统资源参数满足所述预设参数阈值,获取所述线程的类型,若所述线程的类型不为后台普通线程,则减少所述睡眠锁的乐观自旋时间,若所述线程的类型是后台普通线程,则控制所述睡眠锁进入睡眠。通过上述方法,通过将特定资源与预设参数阈值进行比较,在特定资源满足预设参数阈值后减少乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。A sleep lock optimization method provided by the present application first responds to the lock holding application sent by the thread to obtain the state of the current sleep lock, and then if the sleep lock is in a non-idle state, obtains the lock type of the current sleep lock, if The lock type of the sleep lock is a dedicated lock, detecting whether the system resource parameter meets the preset parameter threshold, and if the system resource parameter meets the preset parameter threshold, obtain the type of the thread, if the The type of thread is not background ordinary thread, then reduce the optimistic spin time of described sleep lock, if the type of described thread is background ordinary thread, then control described sleep lock to enter sleep. Through the above method, by comparing the specific resource with the preset parameter threshold and reducing the optimistic spin time after the specific resource meets the preset parameter threshold, the optimistic spin time of the sleep lock can be adjusted, thereby reducing the system load and process load etc. Lock time, thereby avoiding excessive CPU load caused by too long optimistic spin time.

请参阅图7,本申请实施例提供了一种睡眠锁优化方法,所述方法包括:Referring to Figure 7, the embodiment of the present application provides a sleep lock optimization method, the method includes:

步骤S401:进程申请持锁。Step S401: The process applies for holding a lock.

步骤S402:检测锁是否空闲,若锁空闲,则执行步骤S403;若锁不空闲,则执行步骤S404。Step S402: Detect whether the lock is free, if the lock is free, execute step S403; if the lock is not idle, execute step S404.

步骤S403:进程持锁。Step S403: The process holds the lock.

在本申请实施例中,若系统检测到锁空闲,则进程持锁。In this embodiment of the application, if the system detects that the lock is idle, the process holds the lock.

步骤S404:检测是否为专用锁,若是专用锁,则执行步骤S405;若不是专用锁,则执行步骤S409。Step S404: Detect whether it is a dedicated lock, if it is a dedicated lock, execute step S405; if not, execute step S409.

步骤S405:获取特定资源,检测特定资源是否紧张,若特定资源紧张,则执行步骤S406;若特定资源不紧张,则执行步骤S409。Step S405: Acquire specific resources, check whether the specific resources are in short supply, if the specific resources are in short supply, perform step S406; if the specific resources are not in short supply, perform step S409.

在本申请实施例中,特定资源可以包括CPU资源、内存资源、存储资源和网络资源,在此不作具体限定。特定资源与专用锁一一对应,示例性的,专用锁为网络锁,则特定资源对应为网络资源。In this embodiment of the present application, specific resources may include CPU resources, memory resources, storage resources, and network resources, which are not specifically limited here. Specific resources are in one-to-one correspondence with dedicated locks. Exemplarily, if the dedicated lock is a network lock, then the specific resource corresponds to a network resource.

当系统获取到特定资源后,可以将特定资源与预设参数阈值进行比较,来确定特定资源是否紧张,进而来确定是获取后台普通线程还是保持正常乐观自旋时间。After the system acquires a specific resource, it can compare the specific resource with the preset parameter threshold to determine whether the specific resource is tight, and then determine whether to obtain the background ordinary thread or maintain the normal optimistic spin time.

步骤S406:获取线程属性,检测是否为后台普通线程,若是后台普通线程,则执行步骤S407;若不是后台普通线程,则执行步骤S408。Step S406: Obtain thread attributes, check whether it is a background ordinary thread, if it is a background ordinary thread, execute step S407; if it is not a background ordinary thread, execute step S408.

步骤S407:睡眠。Step S407: sleep.

步骤S408:减少乐观自旋时间。Step S408: Decrease the optimistic spin time.

在本申请实施例中,若不是后台普通线程,则减少睡眠锁一定的乐观自旋时间,减少程度可以为一固定值,也可以根据专用锁类型来进行调整,在此不作具体限定。In the embodiment of the present application, if it is not a background ordinary thread, then reduce the certain optimistic spin time of the sleep lock. The degree of reduction can be a fixed value, or can be adjusted according to the type of special lock, which is not specifically limited here.

步骤S409:正常乐观自旋时间。Step S409: Normal optimistic spin time.

在本申请实施例中,睡眠锁的正常乐观自旋时间可以为一固定值,也可以根据专用锁类型进行调整,在此不做具体限定。In the embodiment of the present application, the normal optimistic spin time of the sleep lock may be a fixed value, or may be adjusted according to the type of the dedicated lock, which is not specifically limited here.

本申请提供的一种睡眠锁优化方法,首先进程申请持锁,然后检测锁是否空闲,若锁空闲,则进程持锁,若锁不空闲,则检测是否为专用锁,若是专用锁,则获取特定资源,若不是专用锁,则正常乐观自旋时间,然后检测特定资源是否紧张,若特定资源紧张,则获取线程属性,若特定资源不紧张,则正常乐观自旋时间,再检测是否为后台普通线程,若是后台普通线程,则睡眠,若不是后台普通线程,则减少乐观自旋时间。通过上述方法,通过将特定资源与预设参数阈值进行比较,在特定资源满足预设参数阈值后减少乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。In the sleep lock optimization method provided by this application, firstly, the process applies for holding a lock, and then checks whether the lock is free. If the lock is free, the process holds the lock. If the lock is not free, then checks whether it is a dedicated lock. For a specific resource, if it is not a dedicated lock, use the normal optimistic spin time, and then check whether the specific resource is tight, if the specific resource is tight, get the thread attribute, if the specific resource is not tight, then the normal optimistic spin time, and then check whether it is the background Ordinary thread, if it is a background ordinary thread, sleep, if it is not a background ordinary thread, reduce the optimistic spin time. Through the above method, by comparing the specific resource with the preset parameter threshold and reducing the optimistic spin time after the specific resource meets the preset parameter threshold, the optimistic spin time of the sleep lock can be adjusted, thereby reducing the system load and process load etc. Lock time, thereby avoiding excessive CPU load caused by too long optimistic spin time.

请参阅图8,本申请实施例提供了一种睡眠锁优化方法,所述方法包括:Please refer to FIG. 8, the embodiment of the present application provides a sleep lock optimization method, the method includes:

步骤S501:进程申请持锁。Step S501: The process applies for holding a lock.

步骤S501具体可以参考上述实施例中的详细解释,故不在本实施例中进行赘述。For details of step S501, reference may be made to the detailed explanations in the foregoing embodiments, so details are not repeated in this embodiment.

步骤S502:检测锁是否空闲,若锁空闲,则执行步骤S503;若锁不空闲,则执行步骤S504。Step S502: Detect whether the lock is free, if the lock is free, execute step S503; if the lock is not idle, execute step S504.

步骤S503:进程持锁。Step S503: The process holds the lock.

步骤S503具体可以参考上述实施例中的详细解释,故不在本实施例中进行赘述。For details of step S503, reference may be made to the detailed explanation in the foregoing embodiments, so details are not repeated in this embodiment.

步骤S504:检测是否为通用锁,若是通用锁,则执行步骤S505;若不是通用锁,则执行步骤S507。Step S504: Detect whether it is a general lock, if it is a general lock, execute step S505; if not, execute step S507.

步骤S505:获取系统资源,检测系统资源是否紧张,若系统资源紧张,则执行步骤S506;若系统资源不紧张,则执行步骤S507。Step S505: Acquire system resources, check whether the system resources are tight, if the system resources are tight, execute step S506; if the system resources are not tight, execute step S507.

在本申请实施例中,系统资源包括三部分参数,一是CPU资源,二是内存资源,三是存储资源,当系统获取到CPU资源、内存资源和存储资源后,可以根据获取的CPU资源、内存资源和存储资源计算除系统资源参数,系统资源参数包括三部分参数,一是CPU负载,二是内存回收压力,三是存储资源压力,将得到的CPU负载、内存回收压力和存储资源压力与预设参数阈值进行比较判断,来确定系统资源参数是否满足预设参数阈值,进而来确定是减少睡眠锁乐观自旋时间还是保持正常乐观自旋时间。In this embodiment of the application, system resources include three parts of parameters, one is CPU resources, the other is memory resources, and the third is storage resources. After the system obtains CPU resources, memory resources, and storage resources, it can Calculation of memory resources and storage resources In addition to system resource parameters, system resource parameters include three parts of parameters, one is CPU load, the other is memory recovery pressure, and the third is storage resource pressure. The obtained CPU load, memory recovery pressure and storage resource pressure are compared with The preset parameter thresholds are compared and judged to determine whether the system resource parameters meet the preset parameter thresholds, and then to determine whether to reduce the optimistic spin time of the sleep lock or maintain the normal optimistic spin time.

步骤S506:减少乐观自旋时间。Step S506: Decrease the optimistic spin time.

在本申请实施例中,系统根据系统资源参数满足预设参数阈值的情况来减少睡眠锁的乐观自旋时间,示例性的,若CPU负载、内存回收压力和存储资源压力中的其中之一满足预设参数阈值,则减少睡眠锁三分之一的正常乐观自旋时间,若其中的两个参数满足预设参数阈值,则减少睡眠锁三分之二的乐观自旋时间,若三个参数均满足预设参数阈值,则睡眠锁直接进入睡眠。In this embodiment of the application, the system reduces the optimistic spin time of the sleep lock according to the condition that the system resource parameters meet the preset parameter threshold. For example, if one of the CPU load, memory recovery pressure, and storage resource pressure satisfies The preset parameter threshold will reduce the normal optimistic spin time of the sleep lock by one-third. If two of the parameters meet the preset parameter threshold, the optimistic spin time of the sleep lock will be reduced by two-thirds. If the three parameters If all meet the preset parameter threshold, the sleep lock will go to sleep directly.

步骤S507:正常乐观自旋时间。Step S507: Normal optimistic spin time.

步骤S507具体可以参考上述实施例中的详细解释,故不在本实施例中进行赘述。For details of step S507, reference may be made to the detailed explanations in the foregoing embodiments, so details are not described in this embodiment.

本申请提供的一种睡眠锁优化方法,首先进程持锁,然后检测锁是否空闲,若锁空闲,则进程持锁,若锁不空闲,则检测是否为通用锁,若是通用锁,则获取系统资源,若不是通用锁,则正常乐观自旋,再检测系统资源是否紧张,若系统资源紧张,则减少乐观自旋时间,若系统资源不紧张,则正常乐观自旋时间。通过上述方法,通过将特定资源与预设参数阈值进行比较,在特定资源满足预设参数阈值后减少乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。A sleeping lock optimization method provided by this application. First, the process holds the lock, and then detects whether the lock is free. If the lock is free, the process holds the lock. If the lock is not free, it detects whether it is a general lock. If the resource is not a general-purpose lock, it will normally spin optimistically, and then check whether the system resources are tight. If the system resources are tight, reduce the optimistic spin time. If the system resources are not tight, the optimistic spin time will be normal. Through the above method, by comparing the specific resource with the preset parameter threshold and reducing the optimistic spin time after the specific resource meets the preset parameter threshold, the optimistic spin time of the sleep lock can be adjusted, thereby reducing the system load and process load etc. Lock time, thereby avoiding excessive CPU load caused by too long optimistic spin time.

请参阅图9,本申请实施例提供了一种睡眠锁优化装置600,所述装置600包括:Please refer to FIG. 9, the embodiment of the present application provides a sleep lock optimization device 600, the device 600 includes:

系统资源参数获取单元610,用于获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种。The system resource parameter acquisition unit 610 is configured to acquire current system resource parameters, where the system resource parameters include at least one of CPU load, memory recovery pressure, and storage resource pressure.

作为一种方式,系统资源参数获取单元610用于获取预设时间段内的系统IO总量;基于所述预设时间段的IO总量,计算得到IO负载,将所述IO负载作为当前的系统资源压力。As a method, the system resource parameter acquisition unit 610 is used to obtain the total amount of system IO within a preset time period; based on the total amount of IO in the preset time period, the IO load is calculated, and the IO load is used as the current System resource pressure.

可选的,系统资源参数获取单元610还用于若当前的空闲内存低于预设内存,则开始进行内存回收操作;获取所述内存回收操作对应的开始时间和结束时间;基于所述开始时间和所述结束时间,确定当前的内存回收压力。Optionally, the system resource parameter acquisition unit 610 is also configured to start a memory reclamation operation if the current free memory is lower than the preset memory; acquire the corresponding start time and end time of the memory reclamation operation; based on the start time and the end time, determine the current memory recovery pressure.

可选的,系统资源参数获取单元610还用于获取预设时间段内CPU的运行时间和空闲时间;基于所述运行时间和所述空闲时间,确定当前的CPU负载。Optionally, the system resource parameter acquiring unit 610 is further configured to acquire the running time and idle time of the CPU within a preset time period; based on the running time and the idle time, determine the current CPU load.

系统资源参数比较单元620,用于若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。The system resource parameter comparison unit 620 is configured to reduce the optimistic spin time of the sleep lock if the system resource parameter satisfies a preset parameter threshold.

作为一种方式,系统资源参数比较单元620用于若所述系统资源参数包括CPU负载,且所述CPU负载大于预设负载阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间;或者,若所述系统资源参数包括内存回收压力,且所述内存回收压力大于预设压力阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间;或者,若所述系统资源参数包括存储资源压力,且所述存储资源压力大于预设资源阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间。As a method, the system resource parameter comparison unit 620 is configured to determine that the preset parameter threshold is met if the system resource parameter includes a CPU load, and the CPU load is greater than a preset load threshold, and reduce the optimism of the sleep lock. Spin time; or, if the system resource parameter includes memory recovery pressure, and the memory recovery pressure is greater than a preset pressure threshold, determine that the preset parameter threshold is met, and reduce the optimistic spin time of the sleep lock; or , if the system resource parameters include storage resource pressure, and the storage resource pressure is greater than a preset resource threshold, determine that the preset parameter threshold is satisfied, and reduce the optimistic spin time of the sleep lock.

可选的,系统资源参数比较单元620还用于若所述系统资源参数未满足预设参数阈值,则保持所述睡眠锁的乐观自旋时间为预设时间。Optionally, the system resource parameter comparison unit 620 is further configured to keep the optimistic spin time of the sleep lock as a preset time if the system resource parameter does not meet a preset parameter threshold.

可选的,系统资源参数比较单元620还用于若所述系统资源参数包括CPU负载、内存回收压力、存储资源压力,且所述CPU负载小于或等于预设负载阈值,且所述内存回收压力小于或等于预设压力阈值,且所述存储资源压力小于或等于预设资源阈值,确定未满足所述预设参数阈值,则保持所述睡眠锁的乐观自旋为预设时间。Optionally, the system resource parameter comparison unit 620 is further configured to if the system resource parameters include CPU load, memory recovery pressure, and storage resource pressure, and the CPU load is less than or equal to a preset load threshold, and the memory recovery pressure is less than or equal to a preset pressure threshold, and the storage resource pressure is less than or equal to a preset resource threshold, if it is determined that the preset parameter threshold is not met, then keep the optimistic spin of the sleep lock for a preset time.

可选的,系统资源参数比较单元620还包括响应于线程发送的持锁申请,获取当前的睡眠锁的状态;若所述睡眠锁为空闲状态,获取当前的睡眠锁的锁类型;若所述睡眠锁的锁类型为专用锁,检测所述系统资源参数是否满足所述预设参数阈值。Optionally, the system resource parameter comparison unit 620 also includes obtaining the state of the current sleep lock in response to the lock application sent by the thread; if the sleep lock is in an idle state, obtaining the lock type of the current sleep lock; if the The lock type of the sleep lock is a dedicated lock, and it is detected whether the system resource parameter meets the preset parameter threshold.

可选的,系统资源参数比较单元620还包括若所述系统资源参数满足所述预设参数阈值,获取所述线程的类型;若所述线程的类型不为后台普通线程,则减少所述睡眠锁的乐观自旋时间。Optionally, the system resource parameter comparison unit 620 also includes acquiring the type of the thread if the system resource parameter meets the preset parameter threshold; if the type of the thread is not a background ordinary thread, reducing the sleep time. The optimistic spin time of the lock.

可选的,系统资源参数比较单元620还包括若所述线程的类型是后台普通线程,则控制所述睡眠锁进入睡眠。Optionally, the system resource parameter comparison unit 620 further includes controlling the sleep lock to sleep if the type of the thread is a background ordinary thread.

可选的,系统资源参数比较单元620还包括若所述睡眠锁的锁类型为通用锁,检测所述系统资源参数是否满足所述预设参数阈值。Optionally, the system resource parameter comparing unit 620 further includes detecting whether the system resource parameter satisfies the preset parameter threshold if the lock type of the sleep lock is a general lock.

需要说明的是,本申请中装置实施例与前述方法实施例是互相对应的,装置实施例中具体的原理可以参见前述方法实施例中的内容,此处不再赘述。It should be noted that the device embodiments in this application correspond to the aforementioned method embodiments, and the specific principles in the device embodiments may refer to the content of the aforementioned method embodiments, and will not be repeated here.

下面将结合图10对本申请提供的一种电子设备进行说明。An electronic device provided by the present application will be described below with reference to FIG. 10 .

请参阅图10,基于上述的睡眠锁优化方法、装置,本申请实施例还提供了另一种可以执行前述睡眠锁优化方式的电子设备700。电子设备700包括相互耦合的一个或多个(图中仅示出一个)处理器702、存储器704以及网络模块706。其中,该存储器704中存储有可以执行前述实施例中内容的程序,而处理器702可以执行该存储器704中存储的程序。Referring to FIG. 10 , based on the above sleep lock optimization method and apparatus, the embodiment of the present application also provides another electronic device 700 that can implement the aforementioned sleep lock optimization method. The electronic device 700 includes one or more (only one is shown in the figure) processors 702 , memory 704 and network module 706 coupled to each other. Wherein, the memory 704 stores programs capable of executing the content in the foregoing embodiments, and the processor 702 can execute the programs stored in the memory 704 .

其中,处理器702可以包括一个或者多个处理核。处理器702利用各种接口和线路连接整个电子设备700内的各个部分,通过运行或执行存储在存储器704内的指令、程序、代码集或指令集,以及调用存储在存储器704内的数据,执行服务器700的各种功能和处理数据。可选地,处理器702可以采用数字信号处理(Digital Signal Processing,DSP)、现场可编程门阵列(Field-Programmable Gate Array,FPGA)、可编程逻辑阵列(ProgrammableLogic Array,PLA)中的至少一种硬件形式来实现。处理器702可集成中央处理器(CentralProcessing Unit,CPU)、图像处理器(Graphics Processing Unit,GPU)和调制解调器等中的一种或几种的组合。其中,CPU主要处理操作系统、用户界面和应用程序等;GPU用于负责显示内容的渲染和绘制;调制解调器用于处理无线通信。可以理解的是,上述调制解调器也可以不集成到处理器702中,单独通过一块通信芯片进行实现。Wherein, the processor 702 may include one or more processing cores. The processor 702 uses various interfaces and lines to connect various parts of the entire electronic device 700, and executes or executes instructions, programs, code sets or instruction sets stored in the memory 704, and calls data stored in the memory 704, to execute Various functions of the server 700 and processing data. Optionally, the processor 702 may use at least one of Digital Signal Processing (Digital Signal Processing, DSP), Field-Programmable Gate Array (Field-Programmable Gate Array, FPGA), and Programmable Logic Array (ProgrammableLogic Array, PLA). implemented in the form of hardware. The processor 702 may integrate one or a combination of a central processing unit (Central Processing Unit, CPU), an image processor (Graphics Processing Unit, GPU), a modem, and the like. Among them, the CPU mainly handles the operating system, user interface and application programs, etc.; the GPU is used to render and draw the displayed content; the modem is used to handle wireless communication. It can be understood that the above modem may also not be integrated into the processor 702, but may be realized by a communication chip alone.

存储器704可以包括随机存储器(Random Access Memory,RAM),也可以包括只读存储器(Read-Only Memory)。存储器704可用于存储指令、程序、代码、代码集或指令集。存储器704可包括存储程序区和存储数据区,其中,存储程序区可存储用于实现操作系统的指令、用于实现至少一个功能的指令(比如触控功能、声音播放功能、图像播放功能等)、用于实现下述各个方法实施例的指令等。存储数据区还可以存储电子设备700在使用中所创建的数据(比如电话本、音视频数据、聊天记录数据)等。The memory 704 may include a random access memory (Random Access Memory, RAM), or may include a read-only memory (Read-Only Memory). Memory 704 may be used to store instructions, programs, codes, sets of codes, or sets of instructions. The memory 704 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system and instructions for implementing at least one function (such as a touch function, a sound playback function, an image playback function, etc.) , instructions for implementing the following method embodiments, and the like. The storage data area can also store data created by the electronic device 700 during use (such as phonebook, audio and video data, chat record data) and the like.

所述网络模块706用于接收以及发送电磁波,实现电磁波与电信号的相互转换,从而与通讯网络或者其他设备进行通讯,例如和音频播放设备进行通讯。所述网络模块706可包括各种现有的用于执行这些功能的电路元件,例如,天线、射频收发器、数字信号处理器、加密/解密芯片、用户身份模块(SIM)卡、存储器等等。所述网络模块706可与各种网络如互联网、企业内部网、无线网络进行通讯或者通过无线网络与其他设备进行通讯。上述的无线网络可包括蜂窝式电话网、无线局域网或者城域网。例如,网络模块706可以与基站进行信息交互。The network module 706 is used to receive and send electromagnetic waves, realize mutual conversion between electromagnetic waves and electrical signals, and communicate with communication networks or other devices, for example, communicate with audio playback devices. The network module 706 may include various existing circuit elements for performing these functions, such as antennas, radio frequency transceivers, digital signal processors, encryption/decryption chips, Subscriber Identity Module (SIM) cards, memory, etc. . The network module 706 can communicate with various networks such as the Internet, intranet, wireless network or communicate with other devices through the wireless network. The wireless network mentioned above may include a cellular telephone network, a wireless local area network or a metropolitan area network. For example, the network module 706 can perform information exchange with the base station.

请参考图11,其示出了本申请实施例提供的一种计算机可读存储介质的结构框图。该计算机可读介质800中存储有程序代码,所述程序代码可以被处理器调用执行上述方法实施例中所描述的方法。Please refer to FIG. 11 , which shows a structural block diagram of a computer-readable storage medium provided by an embodiment of the present application. Program codes are stored in the computer-readable medium 800, and the program codes can be invoked by a processor to execute the methods described in the foregoing method embodiments.

计算机可读存储介质800可以是诸如闪存、EEPROM(电可擦除可编程只读存储器)、EPROM、硬盘或者ROM之类的电子存储器。可选地,计算机可读存储介质800包括非易失性计算机可读介质(non-transitory computer-readable storage medium)。计算机可读存储介质800具有执行上述方法中的任何方法步骤的程序代码810的存储空间。这些程序代码可以从一个或者多个计算机程序产品中读出或者写入到这一个或者多个计算机程序产品中。程序代码810可以例如以适当形式进行压缩。The computer readable storage medium 800 may be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read Only Memory), EPROM, hard disk, or ROM. Optionally, the computer-readable storage medium 800 includes a non-transitory computer-readable storage medium (non-transitory computer-readable storage medium). The computer-readable storage medium 800 has a storage space for program code 810 for executing any method steps in the above-mentioned methods. These program codes can be read from or written into one or more computer program products. Program code 810 may, for example, be compressed in a suitable form.

本申请实施例提供的一种睡眠锁优化方法、装置、电子设备及存储介质。该睡眠锁优化方法包括:首先获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种,然后若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。通过上述方法,通过将CPU负载、内存回收压力和存储资源压力与预设参数阈值进行比较,在CPU负载、内存回收压力和存储资源压力均满足预设参数阈值后再减少睡眠锁的乐观自旋时间,可以调整睡眠锁乐观自旋的时间,从而减少了系统负载以及进程的等锁时间,进而避免了由于乐观自旋时间过长导致的CPU负载过大。Embodiments of the present application provide a sleep lock optimization method, device, electronic equipment, and storage medium. The sleep lock optimization method includes: first obtaining current system resource parameters, the system resource parameters including at least one of CPU load, memory recovery pressure and storage resource pressure, and then if the system resource parameters meet the preset parameter threshold, Then reduce the optimistic spin time of the sleep lock. Through the above method, by comparing the CPU load, memory recovery pressure and storage resource pressure with the preset parameter thresholds, reduce the optimistic spin of the sleep lock after the CPU load, memory recovery pressure and storage resource pressure all meet the preset parameter thresholds Time, you can adjust the optimistic spin time of the sleep lock, thereby reducing the system load and the lock waiting time of the process, thereby avoiding the excessive CPU load caused by the optimistic spin time being too long.

上面结合附图对本发明的实施例进行了描述,但是本发明并不局限于上述的具体实施方式,上述的具体实施方式仅仅是示意性的,而不是限制性的,本领域的普通技术人员在本发明的启示下,在不脱离本发明宗旨和权利要求所保护的范围情况下,还可做出很多形式,均属于本发明的保护之内。Embodiments of the present invention have been described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific implementations, and the above-mentioned specific implementations are only illustrative, rather than restrictive, and those of ordinary skill in the art will Under the enlightenment of the present invention, without departing from the gist of the present invention and the protection scope of the claims, many forms can also be made, all of which belong to the protection of the present invention.

Claims (14)

1.一种睡眠锁优化方法,其特征在于,所述方法包括:1. A sleeping lock optimization method, characterized in that the method comprises: 获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种;Acquire current system resource parameters, where the system resource parameters include at least one of CPU load, memory recovery pressure, and storage resource pressure; 若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。If the system resource parameter satisfies the preset parameter threshold, the optimistic spin time of the sleep lock is reduced. 2.根据权利要求1所述的方法,其特征在于,所述若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间,包括:2. The method according to claim 1, wherein if the system resource parameter meets a preset parameter threshold, reducing the optimistic spin time of the sleep lock comprises: 若所述系统资源参数包括CPU负载,且所述CPU负载大于预设负载阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间;或者,If the system resource parameters include CPU load, and the CPU load is greater than a preset load threshold, determine that the preset parameter threshold is met, and reduce the optimistic spin time of the sleep lock; or, 若所述系统资源参数包括内存回收压力,且所述内存回收压力大于预设压力阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间;或者,If the system resource parameter includes memory recovery pressure, and the memory recovery pressure is greater than a preset pressure threshold, determine that the preset parameter threshold is satisfied, and reduce the optimistic spin time of the sleep lock; or, 若所述系统资源参数包括存储资源压力,且所述存储资源压力大于预设资源阈值,确定满足所述预设参数阈值,减少所述睡眠锁的乐观自旋时间。If the system resource parameter includes storage resource pressure, and the storage resource pressure is greater than a preset resource threshold, it is determined that the preset parameter threshold is satisfied, and the optimistic spin time of the sleep lock is reduced. 3.根据权利要求1所述的方法,其特征在于,所述方法还包括:3. The method according to claim 1, characterized in that the method further comprises: 若所述系统资源参数未满足预设参数阈值,则保持所述睡眠锁的乐观自旋时间为预设时间。If the system resource parameter does not meet the preset parameter threshold, keep the optimistic spin time of the sleep lock as the preset time. 4.根据权利要求3所述的方法,其特征在于,所述若所述系统资源参数未满足预设参数阈值,则保持所述睡眠锁的乐观自旋时间为预设时间,包括:4. The method according to claim 3, wherein if the system resource parameter does not meet a preset parameter threshold, keeping the optimistic spin time of the sleep lock as a preset time includes: 若所述系统资源参数包括CPU负载、内存回收压力、存储资源压力,且所述CPU负载小于或等于预设负载阈值,且所述内存回收压力小于或等于预设压力阈值,且所述存储资源压力小于或等于预设资源阈值,确定未满足所述预设参数阈值,则保持所述睡眠锁的乐观自旋时间为预设时间。If the system resource parameters include CPU load, memory recovery pressure, and storage resource pressure, and the CPU load is less than or equal to a preset load threshold, and the memory recovery pressure is less than or equal to a preset pressure threshold, and the storage resource If the pressure is less than or equal to the preset resource threshold, if it is determined that the preset parameter threshold is not met, then keep the optimistic spin time of the sleep lock as the preset time. 5.根据权利要求1所述的方法,其特征在于,所述若所述系统资源参数满足预设参数阈值,则减少所述睡眠锁的乐观自旋时间之前还包括:5. The method according to claim 1, wherein if the system resource parameter meets a preset parameter threshold, reducing the optimistic spin time of the sleep lock further includes: 响应于线程发送的持锁申请,获取当前的睡眠锁的状态;In response to the lock request sent by the thread, obtain the current state of the sleep lock; 若所述睡眠锁为非空闲状态,获取当前的睡眠锁的锁类型;If the sleep lock is in a non-idle state, acquire the lock type of the current sleep lock; 若所述睡眠锁的锁类型为专用锁,检测所述系统资源参数是否满足所述预设参数阈值。If the lock type of the sleep lock is a dedicated lock, it is detected whether the system resource parameter satisfies the preset parameter threshold. 6.根据权利要求5所述的方法,其特征在于,所述若所述系统资源参数满足预设参数阈值,则减少所述睡眠锁的乐观自旋时间,包括:6. The method according to claim 5, wherein if the system resource parameter meets a preset parameter threshold, reducing the optimistic spin time of the sleep lock comprises: 若所述系统资源参数满足所述预设参数阈值,获取所述线程的类型;If the system resource parameter satisfies the preset parameter threshold, acquire the type of the thread; 若所述线程的类型不为后台普通线程,则减少所述睡眠锁的乐观自旋时间。If the type of the thread is not a background ordinary thread, then reduce the optimistic spin time of the sleep lock. 7.根据权利要求6所述的方法,其特征在于,所述方法还包括:7. The method according to claim 6, further comprising: 若所述线程的类型是后台普通线程,则控制所述睡眠锁进入睡眠。If the thread type is a background ordinary thread, then control the sleep lock to enter sleep. 8.根据权利要求5所述的方法,其特征在于,所述方法还包括:8. The method according to claim 5, further comprising: 若所述睡眠锁的锁类型为通用锁,检测所述系统资源参数是否满足所述预设参数阈值。If the lock type of the sleeping lock is a general lock, it is detected whether the system resource parameter satisfies the preset parameter threshold. 9.根据权利要求1所述的方法,其特征在于,所述获取当前的系统资源参数,包括:9. The method according to claim 1, wherein said acquiring current system resource parameters comprises: 获取预设时间段内的系统IO总量;Obtain the total system IO within the preset time period; 基于所述预设时间段的IO总量,计算得到IO负载,将所述IO负载作为当前的系统资源参数。The IO load is calculated based on the total amount of IO in the preset time period, and the IO load is used as a current system resource parameter. 10.根据权利要求1所述的方法,其特征在于,所述获取当前的系统资源参数,包括:10. The method according to claim 1, wherein said acquiring current system resource parameters comprises: 若当前的空闲内存低于预设内存,则开始进行内存回收操作;If the current free memory is lower than the preset memory, start the memory recovery operation; 获取所述内存回收操作对应的开始时间和结束时间;Obtain the start time and end time corresponding to the memory recovery operation; 基于所述开始时间和所述结束时间,确定当前的内存回收压力。Based on the start time and the end time, determine the current memory recovery pressure. 11.根据权利要求1所述的方法,其特征在于,所述获取当前的系统资源参数,包括:11. The method according to claim 1, wherein said acquiring current system resource parameters comprises: 获取预设时间段内CPU的运行时间和空闲时间;Obtain the running time and idle time of the CPU within the preset time period; 基于所述运行时间和所述空闲时间,确定当前的CPU负载。Based on the running time and the idle time, a current CPU load is determined. 12.一种睡眠锁优化装置,其特征在于,所述装置包括:12. A sleep lock optimization device, characterized in that the device comprises: 系统资源参数获取单元,用于获取当前的系统资源参数,所述系统资源参数包括CPU负载、内存回收压力和存储资源压力中的至少一种;A system resource parameter acquisition unit, configured to acquire current system resource parameters, where the system resource parameters include at least one of CPU load, memory recovery pressure, and storage resource pressure; 系统资源参数比较单元,用于若所述系统资源参数满足预设参数阈值,则减少睡眠锁的乐观自旋时间。The system resource parameter comparison unit is configured to reduce the optimistic spin time of the sleep lock if the system resource parameter satisfies a preset parameter threshold. 13.一种电子设备,其特征在于,包括一个或多个处理器以及存储器,一个或多个程序被存储在所述存储器中并被配置为由一个或多个处理器执行权利要求1-11任一所述的方法。13. An electronic device, characterized in that it comprises one or more processors and a memory, one or more programs are stored in the memory and configured to be executed by one or more processors according to claims 1-11 any of the methods described. 14.一种计算机可读存储介质,其特征在于,所述计算机可读存储介质存储有程序代码,所述程序代码包括用于执行如权利要求1-11任一权利要求所述的方法的指令。14. A computer-readable storage medium, wherein the computer-readable storage medium stores program codes, the program codes comprising instructions for executing the method according to any one of claims 1-11 .
CN202211111502.2A 2022-09-13 2022-09-13 Sleep lock optimization method and device, electronic equipment and storage medium Pending CN115562852A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211111502.2A CN115562852A (en) 2022-09-13 2022-09-13 Sleep lock optimization method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211111502.2A CN115562852A (en) 2022-09-13 2022-09-13 Sleep lock optimization method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN115562852A true CN115562852A (en) 2023-01-03

Family

ID=84741284

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211111502.2A Pending CN115562852A (en) 2022-09-13 2022-09-13 Sleep lock optimization method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN115562852A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2025525276A (en) * 2023-09-19 2025-08-05 之江実験室 Method and apparatus for task scheduling across heterogeneous chips based on sequence generation

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20150301871A1 (en) * 2014-04-16 2015-10-22 International Business Machines Corporation Busy lock and a passive lock for embedded load management
US9262233B1 (en) * 2014-10-01 2016-02-16 Software Ag Second-order tuning of spin loops
US20160139966A1 (en) * 2014-11-18 2016-05-19 International Business Machines Corporation Almost fair busy lock
CN105830029A (en) * 2013-12-18 2016-08-03 甲骨文国际公司 System and method for supporting adaptive busy wait in a computing environment
CN109086124A (en) * 2017-06-13 2018-12-25 龙芯中科技术有限公司 Method, apparatus, electronic equipment and the storage medium of adjustment time

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105830029A (en) * 2013-12-18 2016-08-03 甲骨文国际公司 System and method for supporting adaptive busy wait in a computing environment
US20150301871A1 (en) * 2014-04-16 2015-10-22 International Business Machines Corporation Busy lock and a passive lock for embedded load management
US9262233B1 (en) * 2014-10-01 2016-02-16 Software Ag Second-order tuning of spin loops
US20160139966A1 (en) * 2014-11-18 2016-05-19 International Business Machines Corporation Almost fair busy lock
CN109086124A (en) * 2017-06-13 2018-12-25 龙芯中科技术有限公司 Method, apparatus, electronic equipment and the storage medium of adjustment time

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
JIANCHEN SHAN: "APPLES: Effificiently Handling Spin-lock Synchronization on Virtualized Platforms", 《IEEE TRANSACTIONS ON PARALLEL AND DISTRIBUTED SYSTEMS》, 31 July 2017 (2017-07-31), pages 1 - 2 *
王月: "嵌入式多核系统的可调优先级自旋锁", 《单片机与嵌入式系统应用》, 31 December 2021 (2021-12-31) *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2025525276A (en) * 2023-09-19 2025-08-05 之江実験室 Method and apparatus for task scheduling across heterogeneous chips based on sequence generation

Similar Documents

Publication Publication Date Title
CN108509260B (en) Thread identification processing method, apparatus, computer equipment and storage medium
CN111158910B (en) Memory management method and device, storage medium and electronic equipment
CN110198558B (en) Standby optimization method and device, computer equipment and storage medium
CN102789305B (en) Postpone hang
KR102048329B1 (en) Method and apparatus for running application program
CN111274039B (en) Memory recycling method and device, storage medium and electronic equipment
CN109117280B (en) Electronic device and method and storage medium for limiting inter-process communication
US9176570B2 (en) System and method for providing universal serial bus link power management policies in a processor environment
CN115421909A (en) Memory management method, device, electronic device and storage medium
CN115495211A (en) Method, device and electronic device for sorting lock waiting queue
CN109117279B (en) Electronic device and method and storage medium for limiting inter-process communication
CN116932194A (en) Thread execution method, thread execution device, electronic equipment and computer readable storage medium
CN115562852A (en) Sleep lock optimization method and device, electronic equipment and storage medium
WO2024119988A1 (en) Process scheduling method and apparatus in multi-cpu environment, electronic device, and medium
CN111913669A (en) SSD power-down speed improving method and device, computer equipment and storage medium
CN115421907A (en) Memory reclamation method, device, electronic equipment and storage medium
CN116107731A (en) Distributed cluster load control method and device
CN119473926A (en) Memory management method and device, equipment, storage medium, program product and chip
CN112114651B (en) Power saving method and device for processor cluster, chip, equipment and storage medium
CN115712337A (en) Scheduling method and device of processor, electronic equipment and storage medium
CN118939433A (en) A method, device and electronic device for adjusting cluster nodes
US10884477B2 (en) Coordinating accesses of shared resources by clients in a computing device
CN112559242B (en) Method for compressing computational core memory of domestic many-core processor
US20200285584A1 (en) Cache flush abort controller system and method
JP2009037403A (en) Effective use of core memory in multi-core processors

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination