A method and device for releasing kernel cache
The method provides precise cache release in Linux systems by identifying and releasing specific cache objects, ensuring minimal impact on critical processes and enhancing system stability.
Patent Information
- Application Number
- CN202410644358.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-21
- Publication Date
- 2025-07-15
- Estimated Expiration
- 2044-05-21
AI Technical Summary
The prior art lacks targetedness when manually releasing caches in operating systems, which can easily affect the performance of important processes.
By obtaining the identity of the cached object to be released, determining its inode, and releasing the specified cached object, including processes or files, in a targeted manner, avoiding the performance impact on important processes.
The cache is managed in a refined manner, ensuring that the cache release does not affect the performance of important processes, and improving the stability and performance of the operating system.
Smart Images

Figure CN118585322B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of servers, and particularly to a method and device for releasing kernel caches. Background Art
[0002] In an operating system, memory management is one of the key subsystems for effectively managing and allocating the system's memory resources. Memory management involves many technologies, and managing and maintaining the operating system kernel cache is one of them.
[0003] The existing mechanism of the operating system kernel will automatically trigger cache recycling when the memory is about to run out. In some special scenarios, cache recycling can also be manually executed by the user.
[0004] For example, currently, the Linux kernel provides three ways to clear caches, including:
[0005] 1. echo 1 > / proc / sys / vm / drop_caches # Clear page cache;
[0006] 2. echo 2 > / proc / sys / vm / drop_caches # Clear directory entries and inodes;
[0007] 3. echo 3 > / proc / sys / vm / drop_caches # Clear page cache, directory entries, and inodes.
[0008] These three methods will traverse all superblocks of the operating system and all inodes under the superblocks. When it is confirmed that the page cache corresponding to the inode can be released, it will be released without discrimination. However, in a production environment, in scenarios where cache needs to be manually released specially, releasing caches without discrimination will affect the performance of important processes and may lead to an impact on the performance of important processes. Summary of the Invention
[0009] In view of this, embodiments of the present application provide a method and device for releasing kernel caches to achieve targeted cache release.
[0010] To solve the above problems, the technical solutions provided by the embodiments of the present application are as follows:
[0011] In a first aspect, an embodiment of the present application provides a method for releasing kernel caches, the method including:
[0012] Obtain the identifier of the cache object to be released; wherein, the cache object to be released does not belong to the cache objects prohibited from being released; the object to be released is a process or a file;
[0013] Determine the inode of the cache object to be released according to the identifier of the cache object to be released;
[0014] Determine the cache occupied by the file corresponding to the inode of the cache object to be released;
[0015] Release the cache occupied by the file corresponding to the inode of the cache object to be released.
[0016] In this way, the cache of the specified cache object to be released can be released targeted, ensuring that the cache release will not affect the performance of important processes.
[0017] In a possible implementation, the cache object to be released is a cache file to be released;
[0018] Determine the inode of the cache object to be released according to the identifier of the cache object to be released, including:
[0019] Extract the file identifier of the cache file to be released from the absolute address of the cache file to be released;
[0020] Determine the inode of the file identifier of the cache file to be released.
[0021] The cache can be released targeted at the file granularity, customize the file to be released, and implement fine-grained maintenance of the operating system cache to ensure the stability of the operating system operation.
[0022] In a possible implementation, the cache object to be released is a process to be released;
[0023] Determine the inode of the cache object to be released according to the identifier of the cache object to be released, including:
[0024] Determine the task structure corresponding to the process to be released according to the identifier of the process to be released;
[0025] Obtain the inode of the file used by the process to be released from the task structure corresponding to the process to be released.
[0026] The cache can be released targeted at the process granularity, customize the process to be released, and implement fine-grained maintenance of the operating system cache to ensure the stability of the operating system operation.
[0027] In a possible implementation, before releasing the cache occupied by the file corresponding to the inode of the cache object to be released, the method further includes:
[0028] Determine whether the target cache page is in use; the target cache page is any cache page in the cache occupied by the file corresponding to the inode of the cache object to be released;
[0029] When the target cache page is not in use, release the target cache page.
[0030] In this way, the files currently being used in the cache are not released to avoid affecting the processes currently using those files.
[0031] In a possible implementation, before obtaining the identifier of the cache object to be released, the method further includes:
[0032] In response to a viewing request, determine the inode of the cache object; the viewing request is used to determine the cache occupied by the cache object;
[0033] Determine the cache occupied by the file corresponding to the inode of the cache object;
[0034] Based on the cache occupied by the file corresponding to the inode of the cache object, determine the cache object to be released.
[0035] The cache object to be released can be determined by viewing the cache occupied by the file corresponding to the cache object.
[0036] In a possible implementation, based on the cache occupied by the file corresponding to the inode of the cache object, determining the cache object to be released includes:
[0037] Determine whether the cache occupied by the file corresponding to the inode of the cache object exceeds a preset threshold;
[0038] When the cache occupied by the file corresponding to the inode of the cache object exceeds the preset threshold, determine whether the cache object belongs to the cache objects that are prohibited from being released;
[0039] When the cache object does not belong to the cache objects that are prohibited from being released, determine the cache object as the cache object to be released.
[0040] The determined cache object to be released is the one whose occupied cache exceeds the preset threshold and does not belong to the cache objects that are prohibited from being released, so as to release the cache objects that occupy too much cache without affecting important processes or files.
[0041] In a possible implementation, the cache object is a cache file;
[0042] In response to a viewing request, determining the inode of the cache object includes:
[0043] In response to a viewing request, obtain the inode of the cache file from the superblock structure.
[0044] Whether to release the cache file can be determined at the granularity of the cache file, which is more convenient for precisely maintaining the use of the cache.
[0045] In a possible implementation, the cache object is a process;
[0046] In response to a viewing request, determining the inode of the cache object includes:
[0047] In response to a viewing request, obtaining the inode of the file used by the process from the task structure corresponding to the process.
[0048] It is possible to determine whether to release the files used by the cached process at the granularity of the cached process, which is more convenient for precise maintenance of the use of the cache.
[0049] In a possible implementation, determining the cache occupied by the file corresponding to the inode of the cache object includes:
[0050] Obtaining the cache occupied by the file corresponding to the target inode;
[0051] Accumulating the cache sizes occupied by the files corresponding to the target inode to obtain the cache occupied by the file corresponding to the target inode; the target inode is any one of the inodes of the cache object.
[0052] Thus, it is possible to more accurately determine the cache occupied by the files corresponding to each cache object.
[0053] In a second aspect, an embodiment of the present application provides a kernel cache release device, which includes:
[0054] An obtaining unit, configured to obtain an identifier of a cache object to be released; wherein, the cache object to be released does not belong to the cache objects prohibited from being released; the object to be released is a process or a file;
[0055] A first determination unit, configured to determine the inode of the cache object to be released according to the identifier of the cache object to be released;
[0056] A second determination unit, configured to determine the cache occupied by the file corresponding to the inode of the cache object to be released;
[0057] A release unit, configured to release the cache occupied by the file corresponding to the inode of the cache object to be released.
[0058] In a third aspect, an embodiment of the present application provides a computing device, including: a memory and a processor;
[0059] The memory is used to store program instructions;
[0060] The processor is used to run the program instructions so that the computing device implements the kernel cache release method as described in the first aspect and its various possible implementations.
[0061] Fourthly, an embodiment of the present application provides a computer-readable storage medium. Instructions are stored in the computer-readable storage medium. When the instructions run on a computing device, the computing device is caused to execute the kernel cache release method involved in the first aspect and its various possible implementation manners as described above.
[0062] Fifthly, an embodiment of the present application provides a computer program product, including computer program instructions. When the computer program instructions run on a computing device, the computing device is caused to execute the kernel cache release method involved in the first aspect and its various possible implementation manners. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] Figure 1 is a schematic diagram of an exemplary application scenario provided by an embodiment of the present application;
[0064] Figure 2 is a schematic diagram of a computing device provided by an embodiment of the present application;
[0065] Figure 3 is a flowchart of a kernel cache release method provided by an embodiment of the present application;
[0066] Figure 4 is a flowchart of a kernel cache release method provided by an embodiment of the present application;
[0067] Figure 5 is a flowchart of another kernel cache release method provided by an embodiment of the present application;
[0068] Figure 6 is a flowchart of a method for determining a cache object to be released provided by an embodiment of the present application;
[0069] Figure 7 is a flowchart of another method for determining a cache object to be released provided by an embodiment of the present application;
[0070] Figure 8 is a flowchart of another method for determining a cache object to be released provided by an embodiment of the present application;
[0071] Figure 9 is a schematic diagram of an exemplary kernel cache release method in an embodiment of the present application;
[0072] Figure 10 is a schematic diagram of a kernel cache release device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0073] To make the above objects, features, and advantages of the embodiments of the present application more obvious and understandable, the embodiments of the present application will be further described in detail below with reference to the drawings and specific embodiments.
[0074] To facilitate the understanding and explanation of the technical solutions provided by the embodiments of this application, the following will first explain the professional terms involved in the embodiments of this application.
[0075] Cache: Used to store recently used data so that they can be accessed more quickly. Linux uses caches to store frequently accessed pages and files, mainly to solve the problem of performance mismatch at both ends of the system. When an application needs to access data, if the data is already in the cache, the data can be directly read from the cache instead of from the disk, which can greatly improve the access speed and performance.
[0076] Buffer: A special type of storage area. In Linux, buffers are used to store data read from the disk or data to be written to the disk. When data is read from or written to the disk, the buffer can speed up the read and write operations.
[0077] Super block: The core data structure of the file system, which represents the entire file system and stores the metadata of the file system. The super block stores information such as the file block size set by the file system, operation functions, and information about all inodes (index nodes). Each file system has a super block, which contains global file information, such as the used space on the hard disk, available space for data blocks, inode node information, etc. A super block is connected to other super blocks through a linked list to form a doubly circular linked list. Each super block represents a file system on the Linux operating system, and the Linux operating system can have multiple file systems.
[0078] Index node (inode for short): A special data structure in the file system used to store metadata information about file system objects. Each file or directory in the file system has an associated inode. The inode contains metadata of the file, such as file size, file owner, file permissions, timestamp, etc. This information helps the operating system manage and access files.
[0079] LRU (least recently used): It is a commonly used page replacement algorithm used to determine which pages should be discarded when memory is insufficient. In the Linux kernel, the LRU algorithm is used to manage pages in physical memory and swap space. In the Linux kernel, the LRU algorithm is mainly implemented through a pair of doubly linked lists: the active list and the inactive list. Each list contains all the pages belonging to the process user-space address space or the page cache. Among them, the active list stores the pages that have been recently accessed, while the inactive list stores the pages that have not been accessed for a period of time. This partition management method improves efficiency and reduces the number of page transfers. When performing cache recycling, the Linux kernel starts recycling from the tail of the inactive list.
[0080] Next, the background technology related to the embodiments of the present application will be described.
[0081] In an operating system, such as the Linux operating system, memory management is one of the key subsystems for effectively managing and allocating the system's memory resources. Memory management involves many technologies, one of which is the management and maintenance of cache and buffer. Cache and buffer are technologies adopted by the operating system to improve data access speed.
[0082] In the Linux operating system, when cache and buffer are implemented in the system, especially in the IO (input / output) system, cache and buffer are integrated and have the functions of both a buffer and a cache. For example, the kernel buffer has the functions of both a cache and a buffer, which improves the file access rate and also avoids frequent writing of data to the disk, playing a role in protecting the disk and improving the file writing speed.
[0083] In the subsequent embodiments of the present application, cache and buffer are not specifically distinguished and are collectively referred to as kernel cache or cache. Since the cache usually stores data in units of pages, it can also be called page cache.
[0084] To solve the problem that the scenario of manually releasing the cache in the operating system is not targeted and will affect the performance of important processes, the embodiments of the present application provide a method and device for releasing the kernel cache, which can specifically release the cache of a specified process or a specified file.
[0085] To facilitate the understanding of the kernel cache release method provided by the embodiments of the present application, the following is combined with Figure 1An example of the described scenario will be used for illustration. Refer to Figure 1 As shown, this figure is a schematic diagram of an exemplary application scenario provided by an embodiment of this application.
[0086] The embodiments of this application can be applied to the operating system kernel. Subsequently, the Linux operating system kernel will be used as an example for illustration.
[0087] It should be noted that in addition to the Linux operating system, the above operating system can also be other types of operating systems. For example, the above operating system can also be a Fusion operating system (OS), etc. The embodiments of this application do not make specific limitations on the type of the operating system. All operating systems applicable to the methods proposed in the embodiments of this application are within the protection scope of this application.
[0088] The embodiments of this application provide a method for releasing kernel caches. When the system memory is tight, which is generally caused by excessive cache occupation, the usage of the cache can be quickly located and the cache can be released in a targeted manner. In the embodiments of this application, the specific usage of the cache in the current operating system can be obtained, that is, the file cache information and the process cache information can be obtained, so as to clearly understand the processes that occupy a large amount of cache and the files that occupy a large amount of cache in the operating system. In order not to affect the performance of important processes in the current operating system, when releasing the cache, ordinary processes can be specified to release the cache, so as to ensure that the performance of important processes will not be affected by the cache release and improve the stability of the system performance. If it is found that a certain special file occupies too much cache, the cache occupied by this file can also be specified to be released, so as to ensure that the performance of the operating system is not affected to the greatest extent.
[0089] It should be noted that the above important processes and ordinary processes can be determined by the user according to actual operation requirements, and the division of the important processes and ordinary processes can be changed.
[0090] Those skilled in the art can understand that Figure 1 The schematic framework diagram shown is only an example in which the implementation manner of this application can be realized. The applicable scope of the implementation manner of this application is not limited by any aspect of this framework.
[0091] The method for releasing kernel caches provided by the embodiments of this application can be applied to the operating system kernel, and this operating system kernel can run on a computing device. This computing device can be, for example, a server. Refer to Figure 2 As shown, this figure is a schematic diagram of a computing device provided by an embodiment of this application. The computing device 1000 includes: a bus 1002, a processor 1004, a memory 1006, and a communication interface 1008.
[0092] The processor 1004, the memory 1006, and the communication interface 1008 communicate with each other via the bus 1002. The computing device 1000 can be a server or a terminal device. It should be understood that the present application does not limit the number of processors and memories in the computing device 1000.
[0093] The bus 1002 can be a peripheral component interconnect (PCI) bus, an extended industry standard architecture (EISA) bus, or the like. The bus can be divided into an address bus, a data bus, a control bus, etc. Figure 8 Only one line is used to represent it in the figure, but it does not mean that there is only one bus or one type of bus. The bus 1002 can include a path for transmitting information between various components of the computing device 1000 (for example, the memory 1006, the processor 1004, and the communication interface 1008).
[0094] The processor 1004 can include any one or more of processors such as a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0095] The memory 1006 can include a volatile memory, such as a random access memory (RAM). The memory 1006 can also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid state drive (SSD).
[0096] The memory 1006 stores executable program code, and the processor 1004 executes the executable program code to implement the kernel cache release method provided by the embodiments of the present application. Specifically, the memory 1006 stores instructions for executing the kernel cache release method.
[0097] The communication interface 1008 uses a transceiver module such as, but not limited to, a network interface card or a transceiver to implement communication between the computing device 1000 and other devices or a communication network.
[0098] To facilitate the understanding of the embodiments of the present application, a method for releasing kernel cache provided by the embodiments of the present application will be described below with reference to the accompanying drawings.
[0099] See Figure 3 As shown, this figure is a flowchart of a method for releasing kernel cache provided by the embodiments of the present application. As Figure 3 shown, the method may include S301 - S304:
[0100] S301: Obtain the identifier of the cache object to be released.
[0101] In a possible implementation, the computing device obtains the identifier of the cache object to be released; the cache object to be released is a cache object that occupies more cache than a preset threshold, and this cache object does not belong to the objects prohibited from being released. The cache object to be released is a process or a file. The cache object to be released has an identifier, and each identifier uniquely corresponds to a cache object to be released.
[0102] In a possible implementation, the computing device can first obtain the occupancy of the cache, and then use files or processes that occupy more cache than the preset threshold as cache objects to be released.
[0103] S302: Determine the inode of the cache object to be released according to the identifier of the cache object to be released.
[0104] Releasing the cache object to be released is actually releasing the cache occupied by the cache object to be released. When the cache object to be released is a cache file to be released, the released cache is the cache occupied by the cache file to be released. When the cache object to be released is a process to be released, the released cache is the cache occupied by the files used by the process to be released.
[0105] In practical applications, an inode is a special data structure that stores the metadata of a file. There is a corresponding relationship between the inode and the file. One item in the inode is whether the file is stored in the cache. If it is stored in the cache, it also includes information such as the position and size in the cache. To find the files of the cache object to be released, it is necessary to first determine the inodes corresponding to these files. When the cache object to be released is a cache file to be released, the inode of the cache object to be released can be one; when the cache object to be released is a process to be released, the inode of the cache object to be released can be one or more. By obtaining the identifier of the cache object to be released, the inode of the cache object to be released can be determined. The specific implementation of S302 to determine the inode of the cache object to be released through the identifier of the cache object to be released can be seen in the description of the subsequent embodiments.
[0106] S303: Determine the cache occupied by the file corresponding to the inode of the cache object to be released.
[0107] After obtaining the inode of the cache object to be released, the file corresponding to the inode of the cache object to be released can be found, and these files are determined to be the files used by the cache object file itself or the process. Through the inode of the cache object to be released, the cache situation occupied by these files can be determined, such as which cache pages are occupied and the addresses of the occupied cache pages.
[0108] S304: Release the cache occupied by the file corresponding to the inode of the cache object to be released.
[0109] As can be seen from the above description, the cache occupied by the file corresponding to the inode of the cache object to be released is the cache occupied by the file of the cache object to be released, and these files need to be released to achieve targeted release of the cache occupied by the cache object to be released.
[0110] In a possible implementation, before S304 releases the cache occupied by the file corresponding to the inode of the cache object to be released, it may further include:
[0111] Determine whether the target cache page is in use; the target cache page is any cache page in the cache occupied by the file corresponding to the inode of the cache object to be released.
[0112] In the case where the target cache page is not in use, release the target cache page.
[0113] Files are generally stored in the cache in units of pages. The file corresponding to the inode of the cache object to be released may occupy one or more cache pages in the cache. If the file corresponding to the inode of the cache object to be released is currently being used in the cache, the release may affect the process currently using the file. Therefore, all cache pages corresponding to the file are traversed, and when the cache page is not in use, the cache page is released.
[0114] In the embodiment of the present application, the computing device obtains the identifier of the cache object to be released, determines the inode through this identifier, and the inode corresponds to the file, which may include whether the file is stored in the cache and the storage location. Through the inode of the cache object to be released, the file corresponding to this inode in the cache can be found, and these files are the files that need to be released in the cache. In this way, the cache of the specified cache object to be released can be released in a targeted manner, ensuring that the release of the cache will not affect the performance of important processes.
[0115] The following takes the cache object to be released as the cache file to be released as an example to continue to describe the kernel cache release method provided by the embodiment of the present application. See Figure 4 As shown in the figure, the figure is a flowchart of a kernel cache release method provided by the embodiment of the present application, which may include:
[0116] S401: Obtain the absolute address of the cache file to be released.
[0117] When the computing device needs to release the cache file to be released that occupies the cache, the computing device can obtain the absolute address of the cache file to be released. The absolute address of the cache file to be released can be understood as the address where the cache file to be released is stored on the hard disk. At this time, the identifier of the cache object to be released is included in the absolute address of the cache file to be released.
[0118] S402: Determine the inode of the cache file to be released based on the absolute address of the cache file to be released.
[0119] In one implementable manner, the computing device extracts the file identifier of the cache file to be released from the absolute address of the cache file to be released; determines the inode of the file identifier of the cache file to be released, which is the inode of the cache file to be released.
[0120] It should be noted that S402 is a possible implementation manner for S302 to determine the inode corresponding to the cache object to be released according to the identifier of the cache object to be released. Among them, the file identifier of the cache file to be released is in the absolute address of the cache file to be released. For example, the file name of the cache file to be released. The inode corresponding to the cache file to be released can be found in the kernel from the file identifier of the cache file to be released.
[0121] S403: Determine the cache occupied by the file corresponding to the inode of the cache file to be released.
[0122] Through this inode, the occupancy situation of the cache file to be released in the cache can be found, such as which cache pages are occupied and the addresses of the occupied cache pages, etc.
[0123] S404: Release the cache occupied by the file corresponding to the inode of the cache file to be released.
[0124] The implementation processes of S303 - S304 and S403 - S404 are similar. For relevant descriptions, reference can be made to the above embodiments and will not be elaborated here.
[0125] In the embodiments of the present application, the cache can be released targeted at the file granularity, and the files to be released can be customized to achieve refined maintenance of the operating system cache to ensure the stability of the operating system operation.
[0126] The following takes the cache object to be released as an example of the process to be released, and continues to describe the kernel cache release method provided by the embodiments of the present application. Refer to Figure 5 As shown in the figure, the figure is a flowchart of another kernel cache release method provided by the embodiments of the present application, which may include:
[0127] S501: Obtain the identifier of the process to be released.
[0128] On the computing device, obtain the identifier of the process to be released, where the identifier of the process to be released can be a process number or a process name. At this time, the identifier of the cache object to be released is the identifier of the process to be released.
[0129] S502: Determine the inode of the process to be released based on the identifier of the process to be released.
[0130] In one possible implementation, the computing device determines the task structure corresponding to the process to be released according to the identifier of the process to be released; obtains the inode of the file used by the process to be released from the task structure corresponding to the process to be released.
[0131] S502 is a possible implementation of S302 to determine the inode of the cache object to be released according to the identifier of the cache object to be released. Among them, the task structure task_struct corresponding to the process to be released can be obtained through the identifier of the process to be released. There is a file structure of the file used by the process to be released in the task structure, and the inode of the file used by the process to be released can be obtained from it, which is the inode corresponding to the process to be released. It can be understood that the file used by the process to be released can be one or more, and each file used by the process to be released corresponds to an inode.
[0132] S503: Determine the cache occupied by the file corresponding to the inode of the process to be released.
[0133] Through the inodes of each process to be released, the occupancy of the file used by the process to be released in the cache can be found, such as which cache pages are occupied and the addresses of the occupied cache pages.
[0134] S504: Release the cache occupied by the file corresponding to the inode of the process to be released.
[0135] The implementation processes of S303 - S304 and S503 - S504 are similar, and the relevant descriptions can be referred to the above embodiments and will not be elaborated here.
[0136] In the embodiments of the present application, the cache can be released targeted at the process granularity, the process to be released can be customized, and the operating system cache can be maintained finely to ensure the stability of the operating system operation.
[0137] In the embodiments of the present application, before obtaining the identifier of the cache object to be released, the cache object to be released can also be determined. See Figure 6 As shown, this figure of the present application is a flowchart of a method for determining a cache object to be released provided by the embodiments of the present application, which may include:
[0138] S601: In response to a viewing request, determine the inode of the cached object.
[0139] When a computing device obtains the cache occupied by a cached object, a viewing request may be generated; this viewing request is used to determine the cache occupied by the cached object. In response to the viewing request for this cached object, the inode of the cached object may be determined first. The above-mentioned cached object may be a cached file or a process. When the cached object is a cached file, the inode of the cached object is the inode of the cached file; when the cached object is a process, the inode of the cached object is the inode of the file used by the process. The specific implementation of S601 to determine the inode of the cached object in response to the viewing request may refer to the description in the subsequent embodiments.
[0140] S602: Determine the cache occupied by the file corresponding to the inode of the cached object.
[0141] Through the inode corresponding to the cached object, the occupancy of the corresponding file in the cache can be found, such as which cache pages are occupied and the addresses of the occupied cache pages.
[0142] In one possible implementation, S602 to determine the cache occupied by the file corresponding to the inode of the cached object may include:
[0143] A1: Obtain the cache occupied by the file corresponding to the target inode.
[0144] A2: Accumulate the cache sizes occupied by the files corresponding to the target inode to obtain the cache occupied by the file corresponding to the target inode; the target inode is any one of the inodes of the cached object.
[0145] The files corresponding to the same inode of the cached object may be saved in different positions in the cache. By accumulating the cache sizes occupied by the files corresponding to the same inode, the occupancy of the cache by the file corresponding to this inode can be obtained, such as the size information of the occupied cache.
[0146] S603: Based on the cache occupied by the file corresponding to the inode of the cached object, determine the cached object to be released.
[0147] After determining the cache occupied by the file corresponding to the inode of the cached object, the cache occupancy of each cached object can be obtained, and then the cached object to be released can be determined from each cached object.
[0148] In one possible implementation, the specific implementation of S603 to determine the object to be released based on the cache occupied by the file corresponding to the inode of the cached object may include:
[0149] B1: Determine whether the cache occupied by the file corresponding to the inode of the cache object exceeds a preset threshold.
[0150] B2: When the cache occupied by the file corresponding to the inode of the cache object exceeds the preset threshold, determine whether the cache object belongs to a cache object that is prohibited from being released.
[0151] B3: When the cache object does not belong to a cache object that is prohibited from being released, determine the cache object as a cache object to be released.
[0152] When the cache occupied by the file corresponding to the inode of a certain cache object exceeds the preset threshold, it means that the cache object occupies too much cache. If this cache object is not a cache object that is prohibited from being released, then this cache object is determined as an object to be released to release the occupied cache. If this cache object is a cache object that is prohibited from being released, then the cache occupied by this cache object is not released, which can ensure that important files or processes are not affected. Among them, the preset threshold can be set according to the actual situation, and the present application embodiment does not limit the value of the preset threshold.
[0153] In a feasible manner, cache objects that are prohibited from being released can be added to the whitelist. It can be understood that the cache objects in the whitelist are important processes or files. When it is necessary to release cache objects, the cache occupied by the cache objects in the whitelist should be retained. Then, when determining whether a cache object belongs to a cache object that is prohibited from being released, the whitelist can be searched. If this cache object is in the whitelist, then this cache object belongs to a cache object that is prohibited from being released. If this cache object is not in the whitelist, then this cache object can be determined as a cache object to be released. In this way, the cache occupied by important processes or files can be retained through the whitelist mechanism.
[0154] In the embodiments of the present application, it is possible to determine whether to release a cache object in terms of the cache object granularity, which is more convenient for precisely maintaining the use of the cache.
[0155] The following takes the cache object as a cache file as an example to continue to describe the kernel cache release method provided by the embodiments of the present application. See Figure 7 As shown in the figure, the figure is a flowchart of another method for determining a cache object to be released provided by the embodiments of the present application, which may include:
[0156] S701: In response to a viewing request, read the inode of the cache file from the superblock structure.
[0157] S701 is a possible implementation for S601 to determine the inode of the cached object in response to a viewing request. When the relevant information of a file in the cache is to be viewed, a viewing request can be generated, and this viewing request is used to determine the cache occupied by the cached file. Traverse each superblock structure superblock in the kernel, and traverse each inode in each superblock structure superblock. An inode corresponds to a file and can include whether the corresponding file is stored in the cache and the storage location. Then, through the content in each inode, if it is determined that the file corresponding to the inode exists in the cache, then this inode is the inode of the cached file. For example, inode 1 corresponds to file 1, inode 2 corresponds to file 2, and file 2 exists in the cache. By traversing each inode, it can be known that file 2 corresponding to inode 2 is in the cache, and inode 2 of file 2 is obtained.
[0158] S702: Determine the cache occupied by the file corresponding to the inode of the cached file.
[0159] The file corresponding to the inode of each cached file is the cached file itself. Through the inode of the cached file, the occupancy of the cached file in the cache can be found, such as which cache pages are occupied and the addresses of the occupied cache pages.
[0160] S703: Based on the cache occupied by the file corresponding to the inode of the cached file, determine the cache object to be released.
[0161] After determining that the file corresponding to the inode of the cached file occupies the cache, the cache occupancy of each cached file can be obtained, and then the cached file to be released can be determined from each cached file.
[0162] S703 is a possible implementation of S603. The implementation process of S703 is similar to that of S603. For relevant descriptions, reference can be made to the above embodiments and will not be elaborated here.
[0163] In the embodiments of the present application, the file with a larger cache occupancy can be determined as the cached file to be released in terms of the granularity of the cached file, and the cache can be released more precisely.
[0164] The following takes the cache object as a process as an example to continue to illustrate the kernel cache release method provided by the embodiments of the present application. Refer to Figure 8 As shown in the figure, this figure is a flowchart of another method for determining the cache object to be released provided by the embodiments of the present application, which may include:
[0165] S801: In response to a viewing request, obtain the inode of the file used by the process from the task structure corresponding to the process.
[0166] S801 is a possible implementation for S601 to determine the inode of the cached object in response to a viewing request. When viewing the relevant information of a process in the cache, a viewing request can be generated, which is used to determine the cache occupied by the process. Traverse all processes to obtain the task structure task_struck corresponding to the process. Each task structure has a file structure of the file used by the corresponding process, from which the inode of the file used by the process can be obtained.
[0167] S802: Determine the cache occupied by the file corresponding to the inode of the file used by the process.
[0168] The file corresponding to the inode of the file used by the process is the file used by the process. Through the inode of the file used by the process, the cache occupancy of the file used by the process in the cache can be found, such as which cache pages are occupied and the addresses of the occupied cache pages. The cache occupied by the files corresponding to each inode in the task structure of a process is the cache occupied by the process. For example, process A uses file 1 and file 2. The task structure of process A includes inode 1 corresponding to file 1 and inode 2 corresponding to file 2. Through inode 1 and inode 2, the cache occupancy of file 1 and file 2 in the cache can be obtained, and the cache occupied by process A can be obtained.
[0169] S803: Based on the cache occupied by the file corresponding to the inode of the file used by the process, determine the cache object to be released.
[0170] After determining the cache occupied by the file corresponding to the inode of the file used by the process, the cache occupancy of the file used by the process can be obtained, and then the process to be released can be determined from the process.
[0171] S803 is a possible implementation of S603. The implementation process of S803 is similar to that of S603. For relevant descriptions, refer to the above embodiments and will not be elaborated here.
[0172] In the embodiments of the present application, the process with a large cache occupancy can be determined as the process to be released in terms of the process granularity, and the cache can be released more precisely.
[0173] See Figure 9As shown in the figure, a schematic diagram of a method for releasing kernel caches in an actual application is shown. In an embodiment of the present application, parameters can be obtained through the procfs interface. This includes obtaining the absolute address of the cache file to be released to release the cache occupied by the specified file; or obtaining the identifier of the process to be released to release the cache occupied by the specified process; or obtaining a viewing request for the cache file to determine the cache file to be released according to the cache occupation situation of each cache file; or obtaining a viewing request for the process to determine the process to be released according to the cache occupation situation of each process. In a specific implementation, the procfs interface can be one or multiple. Thus, files or processes that occupy a large amount of cache can be accurately located, and the cache of the specified file or the specified process can be released purposefully, ensuring that the released cache will not affect the performance of important processes.
[0174] Based on the method for releasing kernel caches provided in the above method embodiment, an embodiment of the present application also provides a device for releasing kernel caches, which will be described below with reference to the accompanying drawings.
[0175] See Figure 10 As shown in the figure, this figure is a schematic structural diagram of a device for releasing kernel caches provided in an embodiment of the present application. As Figure 10 As shown in the figure, the device for releasing kernel caches includes:
[0176] An acquisition unit 1101, configured to acquire the identifier of the cache object to be released; wherein, the cache object to be released does not belong to the cache object prohibited from being released; the object to be released is a process or a file;
[0177] A first determination unit 1102, configured to determine the inode of the cache object to be released according to the identifier of the cache object to be released;
[0178] A second determination unit 1103, configured to determine the cache occupied by the file corresponding to the inode of the cache object to be released;
[0179] A release unit 1104, configured to release the cache occupied by the file corresponding to the inode of the cache object to be released.
[0180] In a possible implementation manner, the cache object to be released is a cache file to be released; the first determination unit is specifically configured to:
[0181] Extract the file identifier of the cache file to be released from the absolute address of the cache file to be released;
[0182] Determine the inode of the file identifier of the cache file to be released.
[0183] In a possible implementation manner, the cache object to be released is a process to be released; the first determination unit is specifically configured to:
[0184] Determine the task structure corresponding to the process to be released according to the identifier of the process to be released;
[0185] Obtain the inode of the file used by the process to be released from the task structure corresponding to the process to be released.
[0186] In a possible implementation manner, the apparatus further includes:
[0187] A third determination unit, configured to determine whether a target cache page is in use; the target cache page is any cache page in the cache occupied by the file corresponding to the inode of the cache object to be released;
[0188] The release unit is specifically configured to: release the target cache page when the target cache page is not in use.
[0189] In a possible implementation manner, the apparatus further includes:
[0190] A fourth determination unit, configured to determine the inode of the cache object in response to a viewing request; the viewing request is used to determine the cache occupied by the cache object;
[0191] A fifth determination unit, configured to determine the cache occupied by the file corresponding to the inode of the cache object;
[0192] A sixth determination unit, configured to determine the cache object to be released based on the cache occupied by the file corresponding to the inode of the cache object.
[0193] In a possible implementation manner, the sixth determination unit is specifically configured to:
[0194] Determine whether the cache occupied by the file corresponding to the inode of the cache object exceeds a preset threshold;
[0195] When the cache occupied by the file corresponding to the inode of the cache object exceeds the preset threshold, determine whether the cache object belongs to the cache object that is prohibited from being released;
[0196] When the cache object does not belong to the cache object that is prohibited from being released, determine the cache object as the cache object to be released.
[0197] In a possible implementation manner, the cache object is a cached file; the fourth determination unit is specifically configured to:
[0198] In response to the viewing request, obtain the inode of the cached file from the superblock structure.
[0199] In a possible implementation manner, the cache object is a process; the fourth determination unit is specifically configured to:
[0200] In response to a viewing request, obtain the inode of the file used by the process from the task structure corresponding to the process.
[0201] In a possible implementation manner, the fifth determination unit is specifically configured to:
[0202] Obtain the cache occupied by the file corresponding to the target inode;
[0203] Accumulate the cache size occupied by the file corresponding to the target inode to obtain the cache occupied by the file corresponding to the target inode; the target inode is any one of the inodes of the cache objects.
[0204] An embodiment of the present application further provides a computing device, including: a memory and a processor; the memory and the processor are coupled;
[0205] The memory is used to store program instructions;
[0206] The processor is used to run the program instructions so that the computing device implements the kernel cache release method as described above.
[0207] An embodiment of the present application further provides a computer-readable storage medium, in which instructions are stored. When the instructions run on a computing device, the computing device is caused to execute the kernel cache release method as described above.
[0208] An embodiment of the present application further provides a computer program product, including computer program instructions. When the computer program instructions run on a computing device, the computing device is caused to execute the kernel cache release method as described above.
[0209] In the embodiment of the present application, the identifier of a certain cache object to be released can be obtained, and the inode of the cache object to be released is determined through the identifier. The inode corresponds to a file, which can include whether the file is stored in the cache and the storage location. Through the inode, the file corresponding to the inode in the cache can be found, and these files are the files that need to be released in the cache. In this way, the cache of the specified cache object to be released can be released targeted, ensuring that the cache release will not affect the performance of important processes. In addition, according to the cache occupancy of each cache object, the cache object to be released is determined, so that the cache release is more targeted.
[0210] It should be noted that the embodiments in this specification are described in a progressive manner. The key points of each embodiment are the differences from other embodiments. The same and similar parts between the embodiments can be referred to each other. For the systems or devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple, and the relevant parts can be referred to the description of the method part.
[0211] It should be understood that in this application, "at least one (item)" means one or more, and "a plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that there can be three relationships. For example, "A and / or B" can mean: only A exists, only B exists, and both A and B exist at the same time. Among them, A and B can be singular or plural. The character " / " generally represents an "or" relationship between the associated objects before and after. "At least one (item) of the following" or its similar expression refers to any combination of these items, including any combination of single item (item) or plural items (items). For example, at least one (item) of a, b, or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.
[0212] It should also be noted that in this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprising", "including" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or also includes elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the existence of additional identical elements in the process, method, article or device comprising the said element.
[0213] The steps of the methods or algorithms described in connection with the embodiments disclosed herein can be implemented directly in hardware, in software modules executed by a processor, or in a combination of both. The software modules can be placed in a random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.
[0214] The above description of the disclosed embodiments enables those skilled in the art to implement or use this application. Various modifications to these embodiments will be obvious to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application will not be limited to the embodiments shown herein, but rather to the broadest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for releasing kernel cache, characterized in that The method is applied to an operating system kernel, and the method includes: Obtain the identifier of the cache object to be released; wherein, the cache object to be released is a cache object that occupies a cache exceeding a preset threshold and does not belong to the cache objects prohibited from being released; the object to be released is a specified process to be released that occupies resources in the operating system, or a specified cache file to be released that occupies resources in the operating system; Determine the inode of the cache object to be released according to the identifier of the cache object to be released; Determine the cache occupied by the file corresponding to the inode of the cache object to be released; Release the cache occupied by the file corresponding to the inode of the cache object to be released; When the cache object to be released is a specified cache file to be released; the step of determining the inode of the cache object to be released according to the identifier of the cache object to be released includes: Extract the file identifier of the cache file to be released from the absolute address of the cache file to be released; Determine the inode of the file identifier of the cache file to be released; or, When the cache object to be released is a specified process to be released; the step of determining the inode of the cache object to be released according to the identifier of the cache object to be released includes: Determine the task structure corresponding to the process to be released according to the identifier of the process to be released; Obtain the inode of the file used by the process to be released from the task structure corresponding to the process to be released.
2. The method according to claim 1, wherein Before releasing the cache occupied by the file corresponding to the inode of the cache object to be released, the method further includes: Determine whether the target cache page is in use; the target cache page is any cache page in the cache occupied by the file corresponding to the inode of the cache object to be released; Release the target cache page when the target cache page is not in use.
3. The method according to any one of claims 1-2, characterized in that, Before obtaining the identifier of the cache object to be released, the method further includes: In response to a viewing request, determine the inode of the cache object; the viewing request is used to determine the cache occupied by the cache object; Determine the cache occupied by the file corresponding to the inode of the cache object; Based on the cache occupied by the file corresponding to the inode of the cache object, determine the cache object to be released.
4. The method according to claim 3, characterized in that The step of determining the cache object to be released based on the cache occupied by the file corresponding to the inode of the cache object includes: Determine whether the cache occupied by the file corresponding to the inode of the cache object exceeds a preset threshold; When the cache occupied by the file corresponding to the inode of the cache object exceeds the preset threshold, determine whether the cache object belongs to the cache objects prohibited from being released; When the cache object does not belong to the cache objects prohibited from being released, determine the cache object as the cache object to be released.
5. The method according to claim 3, wherein The cache object is a cache file; The step of determining the inode of the cache object in response to the viewing request includes: In response to the viewing request, obtain the inode of the cache file from the superblock structure.
6. The method according to claim 3, wherein The cache object is a process; The step of determining the inode of the cache object in response to the viewing request includes: In response to a viewing request, obtain the inode of the file used by the process from the task structure corresponding to the process.
7. The method according to claim 3, characterized in that The determining the cache occupied by the file corresponding to the inode of the cache object includes: Obtain the cache occupied by the file corresponding to the target inode; Accumulate the cache sizes of the files corresponding to the target inode to obtain the cache occupied by the file corresponding to the target inode; the target inode is any one of the inodes of the cache object.
8. The method according to any one of claims 4 to 6, characterized in that The determining the cache occupied by the file corresponding to the inode of the cache object includes: Obtain the cache occupied by the file corresponding to the target inode; Accumulate the cache sizes of the files corresponding to the target inode to obtain the cache occupied by the file corresponding to the target inode; the target inode is any one of the inodes of the cache object.
9. A computing device, characterized in that, Includes: A memory and a processor; The memory is used to store program instructions; The processor is used to run the program instructions so that the computing device implements the kernel cache release method according to any one of claims 1-8.
Citation Information
Patent Citations
Cache cleaning method, device and client side
CN103714016A
File accessing method and related device
CN105378687A
Batch task processing method and device, computer equipment and storage medium
CN111078733A
File processing method and device, electronic equipment and storage medium thereof
CN116302154A