WO2022178869A1 - 一种缓存替换方法和装置 - Google Patents

一种缓存替换方法和装置 Download PDF

Info

Publication number
WO2022178869A1
WO2022178869A1 PCT/CN2021/078278 CN2021078278W WO2022178869A1 WO 2022178869 A1 WO2022178869 A1 WO 2022178869A1 CN 2021078278 W CN2021078278 W CN 2021078278W WO 2022178869 A1 WO2022178869 A1 WO 2022178869A1
Authority
WO
WIPO (PCT)
Prior art keywords
cache
cache line
line
value
access
Prior art date
Application number
PCT/CN2021/078278
Other languages
English (en)
French (fr)
Inventor
信恒超
曾红义
孟怀中
Original Assignee
华为技术有限公司
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 华为技术有限公司 filed Critical 华为技术有限公司
Priority to CN202180093712.0A priority Critical patent/CN116897335A/zh
Priority to PCT/CN2021/078278 priority patent/WO2022178869A1/zh
Publication of WO2022178869A1 publication Critical patent/WO2022178869A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F3/00Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
    • G06F3/06Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers

Definitions

  • the embodiments of the present application relate to the field of computer technologies, and in particular, to a cache replacement method and device.
  • Cache memory cache is a special memory used to store data frequently accessed by the central processing unit (CPU) to improve the access speed of the CPU.
  • CPU central processing unit
  • a reasonable cache replacement strategy can effectively improve the utilization of the cache.
  • Existing cache replacement generally implements cache replacement through a first input first output (FIFO) strategy, the least recently used (LRU) strategy, or a random strategy.
  • FIFO first input first output
  • LRU least recently used
  • the existing cache replacement strategy is not flexible enough to accurately control the time point when each cache line is replaced, resulting in insufficient cache utilization.
  • the embodiments of the present application provide a cache replacement method and device, which can more accurately control the time point when each cache line is replaced, and can improve the utilization rate of the cache.
  • a first aspect of the embodiments of the present application provides a cache replacement method, where the cache includes a plurality of cache lines, each cache line corresponds to a counter, and the method includes: the cache controller obtains an access request and a first access number; The access request includes the destination address of the requested access, the first access times is the predetermined number of times the first cache line will be accessed, and the first cache line is the cache line requested by the access request; the cache controller is based on the above Destination address, query the cache, if the destination address does not hit any cache line in the cache, and there is a second cache line in the cache, the above cache controller replaces the second cache line with the first cache line, and replaces the first cache line with the first cache line
  • the value of the corresponding counter is initialized; wherein, the second cache line is the cache line whose access count reaches the second access count, and the second access count is the predetermined number of times the second cache line will be accessed.
  • the above-mentioned first access times may be carried in the access request.
  • the number of times the data will be accessed is predictable, so the number of times the cache line is accessed is maintained by the counter.
  • the number of cache lines can be replaced, which can not only reduce the probability of false replacement, but also accurately control the time point when each cache line is replaced, and improve the utilization of the cache.
  • the number of accesses in this embodiment of the present application may be the number of times of reading, or may be the number of times of writing, or may include both the number of times of reading and the number of times of writing.
  • the above-mentioned first access times may be delivered to the cache controller through page table or register configuration or other methods.
  • the access times of the second cache line reach the second access times.
  • the number of times is that the value of the counter corresponding to the second cache line above is 0.
  • the second cache line when writing the second cache line from memory into the cache, if the value of the counter corresponding to the second cache line is initialized to 0, then every time the second cache line is accessed, the second cache line corresponds to When the value of the counter corresponding to the second cache line is the second number of accesses, it is determined that the number of accesses of the second cache line reaches the second number of accesses, so that the access of the second cache line can be accurately controlled The number of times, and the point in time when the second cache line was replaced.
  • the method further includes: when the counter corresponding to the first cache line is initialized, the value is the first In the case of the number of accesses, the value of the counter corresponding to the first cache line is decremented by one; or, when the value of the counter corresponding to the first cache line is 0 when initialized, the value of the counter corresponding to the first cache line is decremented. plus one.
  • the cache controller controls the counter to count accordingly, so that the number of times the cache line is accessed can reach the predetermined number of accesses.
  • the cache line whose number of accesses reaches a predetermined number of accesses can be replaced first, so that the time point when each cache line is replaced can be more accurately controlled and the probability of the cache being replaced by mistake can be reduced. Improve cache utilization.
  • the above method also Including: if it is determined that there is a third cache line in the cache, the cache controller replaces the third cache line with the first cache line, and initializes the value of the counter corresponding to the first cache line; wherein, the third cache line is the retention time in the cache. The cache line exceeds the preset duration.
  • each cache line in the above cache corresponds to a retention domain
  • the above method further includes: the cache controller periodically scans each cache line in the cache. Each time a cache line is scanned, the cache controller increases the value of the retention field corresponding to the cache line by one; every time the cache line is accessed, the cache controller clears the value of the retention field corresponding to the cache line. Based on this solution, the retention time of each cache line in the cache can be determined by setting a retention domain for each cache line. The larger the value of the retention domain, the longer the retention time of the cache line corresponding to the retention domain.
  • the value of the retention domain corresponding to the third cache line above exceeds a preset threshold. Based on this solution, in the case where there is no second cache line in the cache, the cache utilization rate of the cache can be further improved by replacing the cache line that has been lingering for a long time in the cache.
  • the destination address does not hit any cache line in the cache, and the second cache line and the third cache do not exist in the cache.
  • the above method further includes: the cache controller determines the cache line to be replaced, replaces the cache line to be replaced with the first cache line, and initializes the value of the counter corresponding to the first cache line. Based on this scheme, when the destination address misses the cache, if cache replacement is required, but all cache lines in the cache are accessed less than the predetermined number of accesses, and there is no long-term lingering cache line, the cache controller can determine The cache line to be replaced is replaced, and the cache line to be replaced is replaced to improve the access speed of the processor.
  • the above-mentioned cache controller determines the cache line to be replaced, including: the cache controller adopts a first-in-first-out FIFO strategy, a least recently used LRU strategy, or Random strategy to determine the cache line to be replaced. Based on this solution, the cache line to be replaced in the cache can be determined through the FIFO strategy, the LRU strategy, or the random strategy, and the access speed of the processor can be improved by replacing the cache line to be replaced.
  • the above method further includes: the processor acquires the first access count; and the processor sends the first access count to the cache controller.
  • the processor can issue a predetermined number of times each cache line will be accessed through software, so that the cache controller can know the total number of times each cache line will be accessed, so that the cache controller maintains each cache line through the counter.
  • the cache line can be replaced first when the number of times the cache line is accessed reaches a predetermined number of access times, which can reduce the probability of the cache being replaced by mistake and improve the utilization of the cache.
  • the above access request is a read request or a write request.
  • the counter corresponding to the cache line will count accordingly, so that the cache controller can determine the cache line that has been read. total number of times.
  • the counter corresponding to the cache line will count accordingly, so that the cache controller can determine the total number of times the cache line has been written.
  • the cache line is read once, and the counter corresponding to the cache line will be counted accordingly, and each time the processor initiates a write request, the cache line is written once. , the counter corresponding to the cache line will also count accordingly, so that the cache controller can determine the total number of times the cache line has been read and written.
  • a second aspect of the embodiments of the present application provides a cache replacement device, the cache includes a plurality of cache lines, each cache line corresponds to a counter, and the cache replacement device includes: a cache controller for acquiring an access request and a first A number of accesses; the access request includes the destination address of the requested access, the first access number is the predetermined number of times the first cache line will be accessed, and the first cache line is the cache line accessed by the access request; the cache controller, It is also used to query the cache based on the destination address. If the destination address does not hit any cache line in the cache, and there is a second cache line in the cache, the cache controller replaces the second cache line with the first cache line, and replaces the first cache line with the first cache line.
  • the value of the counter corresponding to the cache line is initialized; wherein, the second cache line is the cache line whose access count reaches the second access count, and the second access count is the predetermined number of times the second cache line will be accessed.
  • the number of accesses of the second cache line reaches the second number of accesses is the first number of accesses.
  • the value of the counter corresponding to the second cache line is 0.
  • the above-mentioned cache controller is also used for: when the counter corresponding to the first cache line is initialized. When the value is the first access times, the value of the counter corresponding to the first cache line is decremented by one; or, when the value of the counter corresponding to the first cache line is 0 during initialization, the value of the counter corresponding to the first cache line is decremented by one. The value of the counter is incremented by one.
  • the above cache control It is also used to: if it is determined that there is a third cache line in the cache, use the first cache line to replace the third cache line, and initialize the value of the counter corresponding to the first cache line; wherein, the third cache line is stranded in the cache A cache line whose duration exceeds the preset duration.
  • each cache line in the above cache corresponds to a retention domain
  • the cache controller is also used to: periodically scan each cache line in the cache Each time a cache line is scanned, the cache controller increases the value of the retention field corresponding to the cache line by one; every time the cache line is accessed, the cache controller clears the value of the retention field corresponding to the cache line.
  • the value of the retention domain corresponding to the third cache line above exceeds a preset threshold.
  • the destination address does not hit any cache line in the cache, and the second cache line and the third cache do not exist in the cache.
  • the cache controller is also used to: determine the cache line to be replaced, replace the cache line to be replaced with the first cache line, and initialize the value of the counter corresponding to the first cache line.
  • the above cache controller is specifically configured to adopt a first-in-first-out FIFO strategy, a least recently used LRU strategy, or a random strategy to determine the above to be replaced. cache line.
  • the above cache replacement device further includes a processor, the processor is configured to: acquire the first number of accesses; send the first access number to the cache controller a number of visits.
  • the above access request is a read request or a write request.
  • a third aspect of the embodiments of the present application provides an electronic device, the electronic device includes a cache, a cache controller, and a processor, the cache includes a plurality of cache lines, and each cache line corresponds to a counter; the processor, Used to send an access request and the first access times to the cache controller; the access request includes the destination address of the requested access, the first access times is the predetermined number of times the first cache line will be accessed, and the first cache line is the access The cache line of the request request access; the cache controller is used to obtain the access request and the first access number; based on the destination address, the cache is queried, if the destination address does not hit any cache line in the cache, and there is a first cache line in the cache Two cache lines, the cache controller replaces the second cache line with the first cache line, and initializes the value of the counter corresponding to the first cache line; wherein, the second cache line is the cache line whose number of visits reaches the second number of visits, and the first cache line The second access number is the predetermined number of
  • FIG. 1 is a schematic structural diagram of a computing system according to an embodiment of the present application.
  • FIG. 2 is a schematic flowchart of a cache replacement method provided by an embodiment of the present application.
  • FIG. 3 is a schematic structural diagram of another computing system provided by an embodiment of the present application.
  • FIG. 4 is a schematic flowchart of another cache replacement method provided by an embodiment of the present application.
  • FIG. 5 is a schematic flowchart of another cache replacement method provided by an embodiment of the present application.
  • FIG. 6 is a schematic flowchart of another cache replacement method provided by an embodiment of the present application.
  • FIG. 7 is a schematic flowchart of another cache replacement method provided by an embodiment of the present application.
  • FIG. 8 is a schematic diagram of the composition of an electronic device according to an embodiment of the present application.
  • At least one (a) of a, b or c may represent: a, b, c, a and b, a and c, b and c, or a and b and c, where a, b and c can be single or multiple.
  • words such as “first” and “second” are used to distinguish the same or similar items that have basically the same function and effect, Those skilled in the art can understand that words such as “first” and “second” do not limit the quantity and execution order.
  • the "first” in the first access times and the "second” in the second access times in this embodiment of the present application are only used to distinguish different access times.
  • the descriptions of the first, second, etc. appearing in the embodiments of the present application are only used for illustration and distinguishing the description objects, and have no order. any limitations of the examples.
  • the cache is a temporary memory located between the CPU and the memory.
  • the capacity of the cache is smaller than that of the memory but the exchange speed is faster, which can balance the speed mismatch between the CPU and the memory.
  • the cache generally stores data frequently accessed by the CPU, which can improve the access speed of the CPU.
  • the CPU issues a memory access request, it will first check whether there is request data in the cache. If the data requested by the CPU hits the cache, the data can be returned directly without accessing memory. If the data requested by the CPU is missed in the cache, the corresponding data in the memory must be written to the cache first, and then returned to the CPU.
  • the cache line in the cache can be replaced by a reasonable cache replacement strategy to improve the utilization of the cache.
  • a cache replacement strategy is to perform cache replacement by first input first output (FIFO), the least recently used (LRU), or random method RAND.
  • FIFO is used to replace the cache line that was loaded to the cache earliest.
  • LRU is used to replace the least recently used cache line.
  • RAND random method is used to randomly select a cache line for replacement.
  • adopting the FIFO strategy and random strategy may replace the cache lines that will be used again in the near future, while leaving the cache lines that will not be used in the near future in the cache, resulting in low cache utilization and affecting CPU usage. access speed.
  • the LRU strategy can identify the least recently used cache lines, the cache replacement strategy is not flexible enough to accurately control the time when each cache line is replaced, resulting in insufficient cache utilization.
  • Another cache replacement strategy uses the historical information of each cache line to predict whether the cache line will be read again in the future. If the hardware predictor believes that the cache line will not be read again, then the cache line in the cache When replacing, the cache line can be replaced first.
  • an embodiment of the present application provides a cache replacement method. It can more accurately control the time point when each cache line is replaced, and improve the utilization of the cache.
  • the cache replacement method provided by the embodiment of the present application can be applied to the computing system shown in FIG. 1 .
  • the computing system includes a CPU, a cache, and a memory
  • the cache includes a cache controller
  • all hardware control logic of the cache Both are implemented by the cache controller.
  • the cache controller checks whether the cache has requested data based on the destination address in the access request. If the data requested by the CPU hits the cache, it can directly return the data without accessing the memory. If the data requested by the CPU is not hit in the cache, and it is determined to load the data requested by the CPU into the cache, you can select the cache line to be replaced in the cache, and replace the cache line in the cache with the cache line obtained based on the destination address in the memory The cache line to be replaced.
  • the unit of data replacement is the cache line. That is, the granularity of data exchange between cache and memory is cache line.
  • the embodiment of the present application sets a counter for each cache line, and the counter uses To maintain the number of times the cache line has been accessed.
  • the number of times data will be accessed is somewhat predictable.
  • AI artificial intelligence
  • the training set can be divided into 32 mini batches and deployed on 32 computing nodes (for example, AI core), then the weights will be read by each of the 32 computing nodes Take it once, that is, the number of times the data is read is 32 times in total.
  • the data can be replaced from the cache first, so the time point when each cache line is replaced can be accurately controlled and the utilization of the cache can be improved.
  • the cache in this embodiment of the present application may be constituted by a static random-access memory (static random-access memory, SRAM).
  • SRAM static random-access memory
  • the cache can be integrated on the CPU, can also be integrated on the memory, or can be set independently.
  • Figure 1 only illustrates that the cache is independent of the CPU and memory as an example.
  • a cache replacement method is provided in an embodiment of the present application, and the cache replacement method includes the following steps:
  • the cache controller acquires the access request and the first access times.
  • the access request includes the destination address of the requested access, the first access times is the predetermined number of times the first cache line will be accessed, and the first cache line is the cache line that is requested to be accessed by the access request obtained by the cache controller.
  • the first access times obtained by the cache controller may be carried in the access request, or may not be carried in the access request, which is not limited in this embodiment of the present application.
  • acquiring the access request by the cache controller may include: the cache controller receives the access request from the processor.
  • the foregoing access request may be a read request or a write request, which is not limited in this embodiment of the present application.
  • the above-mentioned first access times may be a predetermined number of times the first cache line will be read.
  • the above-mentioned first access times may also be a predetermined number of times the first cache line will be written.
  • the above-mentioned first number of accesses may also be the sum of the predetermined number of times the first cache line will be read and the number of times the first cache line will be written.
  • each cache line in the cache corresponds to a counter, and the counter is used to count the number of times the cache line is accessed. For example, every time the first cache line accessed by the CPU request is accessed, the counter corresponding to the first cache line will be counted. When the number of times the first cache line is accessed reaches the first number of accesses, if cache replacement is required, The first cache line can be replaced first.
  • a counter corresponding to the first cache line is used to count the number of times the first cache line is read.
  • the counter corresponding to the first cache line is used to count the number of times the first cache line is written.
  • the counter corresponding to the first cache line is used to calculate the number of times the first cache line is read and written. count.
  • the processor may issue the access count (access counter, AC) of each cache line through software.
  • the processor acquires the first access count, and sends the first access count to the cache controller through software.
  • the above-mentioned first number of accesses may be the number of times the first cache line will be accessed predetermined by the programmer, or may be the number of times the first cache line will be accessed predetermined by a software algorithm.
  • programmers apply for memory, they can pass the first number of accesses as parameters.
  • the operating system creates a page table
  • the first number of accesses is written into the page table, and when the CPU or accelerator issues an access request, the request When accessing the physical address corresponding to the page table, the first access times can be obtained from the page table and sent to the cache controller.
  • the number of times that data will be accessed in the future can be predicted in advance through software, and the number of accesses can be issued through page table or register configuration or other methods, so that the cache controller can be based on each cache line. The number of times it has been accessed to determine whether the cache line can be replaced.
  • the CPU can take out the access times AC through a memory management unit (memory management unit, MMU) and send it to the cache controller.
  • MMU memory management unit
  • the number of accesses can also be issued in other ways.
  • the accelerator can also take out the number of accesses AC through the system memory management unit (SMMU), and the accelerator can be a neural network processor (network memory management unit, SMMU). process unit, NPU), graphics processing unit (graphics processing unit, GPU), or digital signal processor (digital signal processing, DSP) and other processors, which are not limited in this embodiment of the present application.
  • SMMU system memory management unit
  • process unit, NPU graphics processing unit
  • GPU graphics processing unit
  • DSP digital signal processor
  • the cache controller queries the cache based on the destination address.
  • the cache controller may query the cache based on the destination address in the access request, and determine whether the data requested to be accessed is hit in the cache. If the requested data hits the cache, the data is returned to the CPU. If the requested data is not hit in the cache, it is determined that the requested data is not stored in the cache.
  • the cache controller determines that the destination address does not hit any cache line in the cache, and there is no free cache line in the cache, if the data accessed by the CPU request needs to be stored in the cache, the cache can be further determined. Whether there is a cache line with the number of accesses reaching the predetermined number of accesses in the cache, and if there is a cache line with the number of accesses reaching the predetermined number of accesses in the cache, the method may further include step S203.
  • the cache controller replaces the second cache line with the first cache line, and initializes the value of the counter corresponding to the first cache line .
  • the second cache line is the cache line whose access times reach the second access times, and the second access times is the predetermined number of times the second cache line will be accessed.
  • the above-mentioned number of accesses of the second cache line reaching the second number of accesses refers to the value of the counter corresponding to the second cache line.
  • the second access number as the predetermined number of times the second cache line will be read, and the number AC of the second cache line will be read as 32 as an example.
  • the value of the counter corresponding to the second cache line can be initialized to 32.
  • the second cache line corresponds to The value of the counter is decremented by one accordingly.
  • the value of the counter corresponding to the second cache line is 0, it is determined that the number of reads of the second cache line reaches the second number of accesses, so the second cache line may not be read again, and further caching is performed if necessary Replace, the second cache line can be replaced.
  • the value of the counter corresponding to the second cache line is initialized to 32 as an example, and the cache returns the data in the second cache line to the processor every time , that is, the second cache line is read once, then the value of the counter corresponding to the second cache line is decremented by one.
  • the value of the counter corresponding to the second cache line is decremented to 0 it is determined that the number of accesses of the second cache line reaches the second number of accesses 32.
  • the number of accesses of the second cache line reaching the second number of accesses means that the value of the counter corresponding to the second cache line is the second access number. frequency.
  • the second access number as the predetermined number of times the second cache line will be read, and the number of times the second cache line will be read is 32 as an example.
  • the value of the counter corresponding to the second cache line can be initialized to 0.
  • the value of the counter corresponding to the second cache line is corresponding plus one.
  • the value of the counter corresponding to the second cache line is 32, it is determined that the number of reads of the second cache line reaches the second number of accesses, so the second cache line may not be read again, and further caching is performed if necessary Replace, the second cache line can be replaced.
  • the cache line in the cache whose number of accesses reaches a predetermined number of accesses is preferentially replaced. Since in some special fields, the number of times the data will be accessed is predictable, the number of times the cache line is accessed is maintained through a counter. If cache replacement is required, the number of accesses in the cache can reach the cache line of the predetermined number of accesses. It can not only reduce the probability of false replacement, but also accurately control the time point when each cache line is replaced, and improve the utilization of the cache.
  • the cache controller when the cache controller replaces the second cache line with the first cache line, it can obtain the first cache line in memory based on the destination address in the access request, and replace the second cache line with the first cache line to write to the cache. middle.
  • the cache controller may initialize the value of the counter corresponding to the first cache line to 0, or initialize the value of the counter corresponding to the first cache line to For the first access times, the value of the counter corresponding to the first cache line may also be initialized to another value, which is not limited in this embodiment of the present application.
  • This embodiment only takes an example of initializing the counter corresponding to the cache line to 0 or the number of accesses corresponding to the cache line when the cache line is loaded into the cache from the memory. It is understandable that the counter initialization value is different, and the corresponding counting strategy may be different.
  • the cache controller After replacing the second cache line in the cache with the first cache line, if the value of the counter corresponding to the first cache line is initialized to 0, then every time the first cache line is accessed, the cache controller will The value of the counter corresponding to a cache line is increased by one. When the value of the counter corresponding to the first cache line reaches the first number of accesses, it is determined that the first cache line will not be accessed again. The first cache line is replaced.
  • the cache The controller decrements the value of the counter corresponding to the first cache line by one. When the value of the counter corresponding to the first cache line reaches 0, it is determined that the first cache line will not be accessed any more. The first cache line is replaced.
  • the cache controller replaces the second cache line with the first cache line, and after initializing the value of the counter corresponding to the first cache line, the cache controller may return the data requested by the processor in the first cache line to the processor. , and increment or decrement the value of the counter corresponding to the first cache line. Subsequently, when the processor accesses the first cache line again, each time the first cache line is accessed, the value of the counter corresponding to the first cache line is correspondingly increased or decreased by one. It is understandable that each time a cache line in the cache controller is accessed by the processor, the value of the counter corresponding to the cache line will be incremented or decremented by one.
  • the cache controller When loading the cache line from the memory into the cache in this embodiment of the present application, the cache controller will initialize the counter corresponding to the cache line.
  • the initialized value of the counter corresponding to each cache line may be 0, or may be a predetermined number of times the cache line will be accessed, and of course may be other values, which are not limited in this embodiment of the present application.
  • the embodiments of the present application only take the value of the cache line initialization as 0 or the predetermined number of times the cache line will be accessed as an example for description.
  • the embodiment of the present application performs cache replacement, by initializing the value of the counter corresponding to the replaced cache line, each time the cache line is accessed, the counter is counted accordingly, so that the cache line can be accessed.
  • the number of accesses reaches the predetermined number of accesses
  • the cache line whose number of accesses reaches the predetermined number of accesses can be replaced first, so that the time point when each cache line is replaced can be more accurately controlled. Reduce the probability of the cache being replaced by mistake and improve the utilization of the cache.
  • an embodiment of the present application further provides a cache replacement method. As shown in FIG. 4 , if it is determined that the destination address is hit in the cache, the method may further include steps S204 or S205 after the foregoing steps S201-S202.
  • the cache controller decrements the value of the counter corresponding to the first cache line by one.
  • step S201 if the destination address accessed by the access request in step S201 hits in the cache, that is, the cache stores the data requested by the access request, then each time the first cache line where the data is located is accessed once, the cache controller will The counter corresponding to the first cache line is counted accordingly.
  • the processor decrements the value of the counter corresponding to the first cache line by one.
  • the cache controller determines that the number of accesses of the first cache line reaches a predetermined number of accesses.
  • the value of the counter corresponding to the first cache line is 0 when initialized, if the destination address hits the cache, the first cache line accessed by the access request will be accessed once, and the cache controller will The value of the counter corresponding to the first cache line is incremented by one.
  • the cache controller determines that the number of accesses of the first cache line reaches the predetermined number of accesses.
  • the cache controller controls the counter to count accordingly, so that the number of times the cache line is accessed
  • the cache line whose number of accesses reaches the predetermined number of accesses can be replaced first, so that the time point when each cache line is replaced can be more accurately controlled and the cache line can be reduced. The probability of being replaced by mistake increases the utilization of the cache.
  • the embodiment of the present application further provides a cache replacement method, as shown in FIG. 5 , after the above steps S201-S202, the method also further The following steps can be included:
  • the cache controller When the destination address misses any cache line in the cache and the second cache line does not exist in the cache, if it is determined that there is a third cache line in the cache, the cache controller replaces the third cache line with the first cache line , and initialize the value of the counter corresponding to the first cache line.
  • the third cache line is the cache line that stays in the cache for longer than the preset time.
  • the third cache line that has been lingering for a long time in the cache can be replaced. go out. That is to say, when performing cache replacement, if all cache lines are accessed less than the predetermined number of access times, these cache lines may be accessed in the future. However, it may be that the predetermined number of accesses is not set accurately enough, causing some cache lines to be accessed less than the predetermined number of accesses, which will cause these cache lines to stay in the cache for a long time, so there is no cache line in the cache.
  • the third cache line that has been lingering in the cache for a long time can be replaced to improve the inaccuracy of the access times setting, resulting in the problem that the cache line stays in the cache for a long time and affects the cache utilization.
  • the retention time of the third cache line can be maintained by setting a retention domain for each cache line.
  • the cache controller may periodically scan each cache line in the cache, and each time a cache line is scanned, the cache controller increases the value of the retention field corresponding to the cache line by one, and each time the cache line is accessed once, The cache controller clears the value of the holdover field corresponding to the cache line to zero. Therefore, the larger the value of the retention field, the longer the retention time of the cache line corresponding to the retention field.
  • the above-mentioned third cache line may be a cache line whose retention domain value exceeds a preset threshold.
  • the cache controller controls the counter to count accordingly, so that the number of times the cache line is accessed
  • the cache line whose number of accesses reaches the predetermined number of accesses can be replaced first, so that the time point when each cache line is replaced can be more accurately controlled and the cache line can be reduced.
  • the probability of being replaced by mistake increases the utilization of the cache.
  • the cache utilization rate can be further improved by replacing the cache line that has been lingering for a long time in the cache.
  • an embodiment of the present application further provides a cache replacement method.
  • the method may further include the following steps:
  • the cache controller determines the cache line to be replaced.
  • the foregoing step S207 may include: the cache controller adopts a first-in-first-out FIFO strategy, a least recently used LRU strategy, or a random strategy to determine the cache line to be replaced.
  • the cache controller adopts the FIFO strategy, the least recently used LRU strategy, or the random strategy to determine the specific implementation of the cache line to be replaced, which will not be repeated here.
  • the cache controller can Determine the cache line to be replaced based on the FIFO strategy, LRU strategy, or random strategy.
  • the cache controller replaces the cache line to be replaced with the first cache line, and initializes the value of the counter corresponding to the first cache line.
  • the cache controller may initialize the value of the counter corresponding to the first cache line.
  • the cache controller may initialize the value of the counter corresponding to the first cache line to 0, each time the first cache line is accessed once, the value of the counter corresponding to the first cache line is incremented by one.
  • the cache controller initializes the value of the counter corresponding to the first cache line to the first number of reads, each time the first cache line is accessed, the value of the counter corresponding to the first cache line is decremented by one.
  • the cache controller controls the counter to count accordingly, so that the number of times the cache line is accessed
  • the cache line whose number of accesses reaches the predetermined number of accesses can be replaced first, so that the time point when each cache line is replaced can be more accurately controlled and the cache line can be reduced. The probability of being replaced by mistake increases the utilization of the cache.
  • the cache line to be replaced can be determined through the FIFO strategy, the LRU strategy, or the random strategy, and the cache line to be replaced can be replaced to Improve the access speed of the processor.
  • the embodiment of the present application also provides a cache replacement method, as shown in FIG. 7 , the method includes the following steps:
  • the cache controller obtains an access request.
  • the access request includes the destination address of the requested access and the first access times, the first access times is the predetermined number of times the first cache line will be accessed, and the first cache line is the access request obtained by the cache controller. cache line.
  • the cache controller queries the cache based on the destination address to determine whether the destination address is hit in the cache.
  • step S703 or S704 is continued; if it is determined that the destination address does not hit any cache line in the cache, step S705 is continued.
  • the cache controller decrements the value of the counter corresponding to the first cache line by one.
  • the cache controller increases the value of the counter corresponding to the first cache line by one.
  • the cache controller determines whether a second cache line exists in the cache.
  • the second cache line is the cache line whose access times reach the second access times, and the second access times is the predetermined number of times the second cache line will be accessed.
  • step S706 If it is determined that the second cache line exists in the cache, proceed to step S706; if it is determined that the second cache line does not exist in the cache, proceed to step S707.
  • the cache controller replaces the second cache line with the first cache line, and initializes the value of the counter corresponding to the first cache line.
  • the cache controller determines whether a third cache line exists in the cache.
  • the third cache line is the cache line that stays in the cache for longer than the preset time.
  • step S708 If it is determined that the third cache line exists in the cache, proceed to step S708; if it is determined that the third cache line does not exist in the cache, proceed to step S709.
  • the cache controller replaces the third cache line with the first cache line, and initializes the value of the counter corresponding to the first cache line.
  • the cache controller determines the cache line to be replaced, replaces the cache line to be replaced with the first cache line, and initializes the value of the counter corresponding to the first cache line.
  • the data returned by the cache controller is the data in the first cache line requested to be accessed by the processor.
  • the value of the counter corresponding to the first cache line is correspondingly increased or decreased by one. For example, if the value of the counter corresponding to the first cache line is initialized to 0 during initialization, then when the data is returned in step S710, it is determined that the first cache line has been read once, and the value of the counter corresponding to the first cache line is incremented by one . For another example, if the value of the counter corresponding to the first cache line is initialized to the first access times during initialization, then when the data is returned in step S710, it is determined that the first cache line is read once, and the counter corresponding to the first cache line is determined. value minus one.
  • the cache controller controls the counter to count accordingly, so that the number of times the cache line is accessed
  • the cache line whose number of accesses reaches the predetermined number of accesses can be replaced first, so that the time point when each cache line is replaced can be more accurately controlled and the cache line can be reduced.
  • the probability of being replaced by mistake increases the utilization of the cache.
  • the cache utilization rate can be further improved by replacing the cache line that has been lingering for a long time in the cache.
  • An embodiment of the present application further provides a cache replacement device, the cache replacement device includes a cache and a cache controller, and the cache controller is used to implement the cache replacement method shown in any of FIG. 2 and FIG. 4 to FIG. 7 .
  • the electronic device includes a processor, a cache, a cache controller, and a memory.
  • the processor is configured to send an access request to the cache controller, where the access request includes requesting access to The destination address and the first access times mentioned above.
  • the cache controller is used to obtain the access request and query the cache based on the destination address in the access request. If the address does not hit any cache line in the cache, and the second cache line exists in the cache, the cache controller replaces the second cache line with the first cache line, and initializes the value of the counter corresponding to the first cache line.
  • the cache controller may obtain the above-mentioned first cache line from the memory.
  • the cache controller shown in FIG. 8 is further configured to implement the cache replacement method shown in any one of FIG. 2 and FIG. 4 to FIG. 7 .
  • the steps of the methods or algorithms described in conjunction with the disclosure of the present application may be implemented in a hardware manner, or may be implemented in a manner in which a processor executes software instructions.
  • Software instructions can be composed of corresponding software modules, and software modules can be stored in random access memory (RAM), flash memory, erasable programmable read-only memory (erasable programmable read-only memory, EPROM), electrically erasable programmable Programmable read only memory (electrically EPROM, EEPROM), registers, hard disk, removable hard disk, compact disk read only memory (CD-ROM), or any other form of storage medium known in the art.
  • An exemplary storage medium is coupled to the processor, such that the processor can read information from, and write information to, the storage medium.
  • the storage medium can also be an integral part of the processor.
  • the processor and storage medium may reside in an ASIC.
  • the ASIC may be located in the core network interface device.
  • the processor and the storage medium may also exist in the core network interface device as discrete components.
  • the functions described in the present invention may be implemented in hardware, software, firmware, or any combination thereof.
  • the functions may be stored on or transmitted over as one or more instructions or code on a computer-readable medium.
  • Computer-readable media includes both computer storage media and communication media including any medium that facilitates transfer of a computer program from one place to another.
  • a storage medium can be any available medium that can be accessed by a general purpose or special purpose computer.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Human Computer Interaction (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Memory System Of A Hierarchy Structure (AREA)

Abstract

本申请实施例公开了一种缓存替换方法和装置,涉及计算机技术领域,改善了现有技术中缓存的利用率不高的问题。具体方案为:缓存控制器获取访问请求和第一访问次数;访问请求包括请求访问的目的地址,第一访问次数为预先确定的访问请求请求访问的缓存行会被访问的次数;缓存控制器基于目的地址,查询缓存,若目的地址未命中缓存中的任意一个缓存行,且缓存中存在第二缓存行,缓存控制器采用第一缓存行替换第二缓存行,并将第一缓存行对应的计数器的值初始化;其中,第二缓存行为访问次数达到第二访问次数的缓存行,第二访问次数为预先确定的第二缓存行会被访问的次数。

Description

一种缓存替换方法和装置 技术领域
本申请实施例涉及计算机技术领域,尤其涉及一种缓存替换方法和装置。
背景技术
高速缓冲存储器cache是一种特殊的存储器,用于存储中央处理器(central processing unit,CPU)频繁访问的数据,以提高CPU的访问速度。但是,由于cache的存储空间有限,因此合理的缓存替换策略能够有效的提高cache的利用率。
现有的缓存替换一般通过先进先出(first input first output,FIFO)策略、最近最久未使用(the least recently used,LRU)策略、或随机策略等方法实现缓存替换。但是,现有的缓存替换策略不够灵活,无法准确的控制每个缓存行cache line被替换的时间点,导致cache的利用率不够高。
发明内容
本申请实施例提供一种缓存替换方法和装置,可以较为准确的控制每个cache line被替换的时间点,能够提高cache的利用率。
为达到上述目的,本申请实施例采用如下技术方案:
本申请实施例的第一方面,提供一种缓存替换方法,该缓存包括多个缓存行cache line,每个cache line对应一个计数器,该方法包括:缓存控制器获取访问请求和第一访问次数;该访问请求包括请求访问的目的地址,该第一访问次数为预先确定的第一cache line会被访问的次数,该第一cache line为上述访问请求请求访问的cache line;上述缓存控制器基于上述目的地址,查询缓存,若该目的地址未命中缓存中的任意一个cache line,且缓存中存在第二cache line,上述缓存控制器采用第一cache line替换第二cache line,并将第一cache line对应的计数器的值初始化;其中,第二cache line为访问次数达到第二访问次数的cache line,该第二访问次数为预先确定的第二cache line会被访问的次数。基于本方案,在对cache中的cache line进行替换时,通过将访问次数达到预先确定的访问次数的cache line替换出去,能够较为准确的控制每个cache line被替换的时间点,降低cache被误替换的概率,提高cache的利用率。
可选的,上述第一访问次数可以携带在访问请求中。
可以理解的,由于在一些专用领域中,数据会被访问的次数是可以预见的,因此通过计数器维护cache line被访问的次数,如果需要进行缓存替换,通过将cache中访问次数达到预先确定的访问次数的cache line替换出去,从而不仅能够降低误替换发生的概率,而且能够准确的控制每个cache line被替换的时间点,提升cache的利用率。
可选的,本申请实施例中的访问次数可以为读取次数,也可以为写入次数,还可以既包括读取次数又包括写入次数。
可选的,上述第一访问次数可以通过页表或者寄存器配置或其他方式下发给缓存控制器。
结合第一方面,在一种可能的实现方式中,在上述第二cache line对应的计数器初始化时的值为上述第二访问次数的情况下,上述第二cache line的访问次数达到上述第二访问次数为上述第二cache line对应的计数器的值为0。基于本方案,在将第二cache line从内存写入cache中时,如果将第二cache line对应的计数器的值初始化为第二访问次数,那么该第二cache line每被访问一次,该第二cache line对应的计数器的值减一,当该第二cache line对应的计数器的值为0时,确定第二cache line的访问次数达到第二访问次数,从而能够准确的控制第二cache line的访问次数,以及第二cache line被替换的时间点。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,在上述第二cache line对应的计数器初始化时的值为0的情况下,上述第二cache line的访问次数达到上述第二访问次数为上述第二cache line对应的计数器的值为上述第二访问次数。基于本方案,在将第二cache line从内存写入cache中时,如果将第二cache line对应的计数器的值初始化为0,那么该第二cache line每被访问一次,该第二cache line对应的计数器的值加一,当该第二cache line对应的计数器的值为第二访问次数时,确定第二cache line的访问次数达到第二访问次数,从而能够准确的控制第二cache line的访问次数,以及第二cache line被替换的时间点。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,若上述目的地址在缓存中命中,上述方法还包括:在第一cache line对应的计数器初始化时的值为第一访问次数的情况下,将该第一cache line对应的计数器的值减一;或者,在第一cache line对应的计数器初始化时的值为0的情况下,将第一cache line对应的计数器的值加一。基于本方案,通过为缓存中的每个cache line设置一个计数器,cache line每被访问一次,缓存控制器控制计数器进行相应的计数,从而能够在该cache line被访问的次数到达预先确定的访问次数时,如果需要进行缓存替换,可以优先将该访问次数到达预先确定的访问次数的cache line替换出去,从而能够较为准确的控制每个cache line被替换的时间点,降低cache被误替换的概率,提高cache的利用率。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,在上述目的地址未命中缓存中的任意一个cache line,且缓存中不存在第二cache line情况下,上述方法还包括:若确定缓存中存在第三cache line,缓存控制器采用第一cache line替换第三cache line,并将第一cache line对应的计数器的值初始化;其中,第三cache line为缓存中滞留时长超过预设时长的cache line。基于本方案,在进行缓存替换时,如果cache中不存在第二cache line,可以将cache中长期滞留的第三cache line替换出去,改善访问次数设置的不准确,导致cache line长期滞留在cache中影响cache利用率的问题。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,上述缓存中的每个cache line对应一个滞留域,上述方法还包括:缓存控制器周期性的扫描缓存中的每个cache line,每扫描到一次cache line,缓存控制器将该cache line对应的滞留域的值加一;cache line每被访问一次,缓存控制器将该cache line对应的滞留域的值清零。基于本方案,可以通过为每个cache line设置滞留域,确定每个cache line在cache中滞留的时长,滞留域的数值越大,代表该滞留域对应的cache line滞留的时间越长。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,上述第三cache line对应的滞留域的值超过预设阈值。基于本方案,在缓存中不存在第二cache line情况下,可以通过将缓存中长期滞留的cache line替换出去,能够进一步提高cache的利用率。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,在上述目的地址未命中缓存中的任意一个cache line,且缓存中不存在上述第二cache line及上述第三cache line的情况下,上述方法还包括:缓存控制器确定待替换cache line,采用第一cache line替换待替换cache line,并将第一cache line对应的计数器的值初始化。基于本方案,目的地址未命中cache时,如果需要进行缓存替换,但cache中所有cache line被访问的次数均未达到预先确定的访问次数,而且没有长期滞留的cache line,那么缓存控制器可以确定待替换的cache line,并将该待替换的cache line替换出去,以提高处理器的访问速度。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,上述缓存控制器确定待替换cache line,包括:缓存控制器采用先进先出FIFO策略、最近最少使用LRU策略、或随机策略,确定待替换cache line。基于本方案,通过FIFO策略、LRU策略、或随机策略,可以确定cache中的待替换cache line,通过将该待替换的cache line替换出去,能够提高处理器的访问速度。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,上述方法还包括:处理器获取第一访问次数;处理器向上述缓存控制器发送所述第一访问次数。基于本方案,处理器可以通过软件下发预先确定的每个cache line会被访问的次数,使得缓存控制器可以获知每个cache line会被访问的总次数,从而缓存控制器在通过计数器维护每个cache line被访问的次数时,可以在cache line被访问的次数到达预先确定的访问次数时,优先将该cache line替换出去,从而能够降低cache被误替换的概率,提高cache的利用率。
结合第一方面和上述可能的实现方式,在另一种可能的实现方式中,上述访问请求为读请求或写请求。基于本方案,当访问次数为读取次数时,处理器每发起读请求读取一次cache line,该cache line对应的计数器会进行相应的计数,从而缓存控制器能够确定该cache line被读取的总次数。当访问次数为写入次数时,处理器每发起写请求写入一次cache line,该cache line对应的计数器会进行相应的计数,从而缓存控制器能够确定该cache line被写入的总次数。当访问次数既包括读取次数又包括写入次数时,处理器每发起读请求读取一次cache line,该cache line对应的计数器会进行相应的计数,处理器每发起写请求写入一次cache line,该cache line对应的计数器也会进行相应的计数,从而缓存控制器能够确定该cache line被读取及被写入的总次数。
本申请实施例的第二方面,提供一种缓存替换装置,该缓存包括多个缓存行cache line,每个cache line对应一个计数器,缓存替换装置包括:缓存控制器,用于获取访问请求和第一访问次数;该访问请求包括请求访问的目的地址,该第一访问次数为预先确定的第一cache line会被访问的次数,第一cache line为访问请求请求访问的cache line;缓存控制器,还用于基于目的地址,查询缓存,若目的地址未命中缓存中的任意一个cache line,且缓存中存在第二cache line,缓存控制器采用第一cache line替换第 二cache line,并将第一cache line对应的计数器的值初始化;其中,该第二cache line为访问次数达到第二访问次数的cache line,第二访问次数为预先确定的第二cache line会被访问的次数。
结合第二方面,在一种可能的实现方式中,在上述第二cache line对应的计数器初始化时的值为第二访问次数的情况下,第二cache line的访问次数达到第二访问次数为第二cache line对应的计数器的值为0。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,在上述第二cache line对应的计数器初始化时的值为0的情况下,第二cache line的访问次数达到第二访问次数为第二cache line对应的计数器的值为第二访问次数。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,若上述目的地址在缓存中命中,上述缓存控制器,还用于:在第一cache line对应的计数器初始化时的值为第一访问次数的情况下,将第一cache line对应的计数器的值减一;或者,在第一cache line对应的计数器初始化时的值为0的情况下,将第一cache line对应的计数器的值加一。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,在上述目的地址未命中缓存中的任意一个cache line,且缓存中不存在第二cache line情况下,上述缓存控制器,还用于:若确定缓存中存在第三cache line,采用第一cache line替换第三cache line,并将第一cache line对应的计数器的值初始化;其中,第三cache line为缓存中滞留时长超过预设时长的cache line。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,上述缓存中的每个cache line对应一个滞留域,缓存控制器,还用于:周期性的扫描缓存中的每个cache line,每扫描到一次cache line,缓存控制器将该cache line对应的滞留域的值加一;cache line每被访问一次,缓存控制器将该cache line对应的滞留域的值清零。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,上述第三cache line对应的滞留域的值超过预设阈值。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,在上述目的地址未命中缓存中的任意一个cache line,且缓存中不存在上述第二cache line及上述第三cache line的情况下,缓存控制器,还用于:确定待替换cache line,采用第一cache line替换待替换cache line,并将第一cache line对应的计数器的值初始化。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,上述缓存控制器,具体用于采用先进先出FIFO策略、最近最少使用LRU策略、或随机策略,确定上述待替换cache line。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,上述缓存替换装置还包括处理器,该处理器,用于:获取第一访问次数;向缓存控制器发送该第一访问次数。
结合第二方面和上述可能的实现方式,在另一种可能的实现方式中,上述访问请求为读请求或写请求。
上述第二方面的效果描述可以参考第一方面的效果描述,在此不再赘述。
本申请实施例的第三方面,提供一种电子设备,该电子设备包括缓存、缓存控制 器及处理器,该缓存包括多个缓存行cache line,每个cache line对应一个计数器;该处理器,用于向缓存控制器发送访问请求和第一访问次数;访问请求包括请求访问的目的地址,第一访问次数为预先确定的第一cache line会被访问的次数,第一cache line为所述访问请求请求访问的cache line;缓存控制器,用于获取该访问请求和第一访问次数;基于目的地址,查询缓存,若目的地址未命中所述缓存中的任意一个cache line,且缓存中存在第二cache line,缓存控制器采用第一cache line替换第二cache line,并将第一cache line对应的计数器的值初始化;其中,第二cache line为访问次数达到第二访问次数的cache line,第二访问次数为预先确定的所述第二cache line会被访问的次数。
附图说明
图1为本申请实施例提供的一种计算系统的结构示意图;
图2为本申请实施例提供的一种缓存替换方法的流程示意图;
图3为本申请实施例提供的另一种计算系统的结构示意图;
图4为本申请实施例提供的另一种缓存替换方法的流程示意图;
图5为本申请实施例提供的另一种缓存替换方法的流程示意图;
图6为本申请实施例提供的另一种缓存替换方法的流程示意图;
图7为本申请实施例提供的另一种缓存替换方法的流程示意图;
图8为本申请实施例提供的一种电子设备的组成示意图。
具体实施方式
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行描述。在本申请中,“至少一个”是指一个或者多个,“多个”是指两个或两个以上。“和/或”,描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B的情况,其中A,B可以是单数或者复数。字符“/”一般表示前后关联对象是一种“或”的关系。“以下至少一项(个)”或其类似表达,是指的这些项中的任意组合,包括单项(个)或复数项(个)的任意组合。例如,a,b或c中的至少一项(个),可以表示:a,b,c,a和b,a和c,b和c,或,a和b和c,其中a、b和c可以是单个,也可以是多个。另外,为了便于清楚描述本申请实施例的技术方案,在本申请的实施例中,采用了“第一”、“第二”等字样对功能和作用基本相同的相同项或相似项进行区分,本领域技术人员可以理解“第一”、“第二”等字样并不对数量和执行次序进行限定。比如,本申请实施例中的第一访问次数中的“第一”和第二访问次数中的“第二”仅用于区分不同的访问次数。本申请实施例中出现的第一、第二等描述,仅作示意与区分描述对象之用,没有次序之分,也不表示本申请实施例中对设备个数的特别限定,不能构成对本申请实施例的任何限制。
需要说明的是,本申请中,“示例性的”或者“例如”等词用于表示作例子、例证或说明。本申请中被描述为“示例性的”或者“例如”的任何实施例或设计方案不应被解释为比其他实施例或设计方案更优选或更具优势。确切而言,使用“示例性的”或者“例如”等词旨在以具体方式呈现相关概念。
高速缓冲存储器cache是位于CPU与内存之间的临时存储器,cache的容量比内存小但交换速度快,能够平衡CPU和内存之间速度不匹配的问题。
cache中一般存储CPU频繁访问的数据,能够提高CPU的访问速度。当CPU发出内存访问请求时,会先查看cache内是否有请求数据。如果CPU请求的数据在cache中命中,可以直接返回该数据,无需访问内存。如果CPU请求的数据在cache中未命中,那么要先把内存中的相应数据写入缓存,再将其返回给CPU。
但是,由于cache的容量有限,如果CPU请求的数据在cache中未命中,而且cache中有空闲缓存行cache line,那么可以将CPU请求的数据由内存载入cache中。如果CPU请求的数据在cache中未命中,而且cache中没有空闲的cache line,那么可以通过合理的缓存替换策略,对cache中的cache line进行替换,以提高cache的利用率。
一种缓存替换策略是通过先进先出(first input first output,FIFO)、最近最久未使用(the least recently used,LRU)、或随机法RAND进行缓存替换。例如,采用FIFO将最早载入cache的cache line进行替换。再例如,采用LRU将最近最少使用的cache line进行替换。再例如,采用随机法,随机选择一条cache line进行替换。
但是,采用FIFO策略和随机策略有可能将近期会被再次用到的cache line替换出去,而将近期不会被用到的cache line滞留在cache中,造成cache的利用率较低,影响CPU的访问速度。LRU策略虽然可以识别近期使用最少的cache line,但是该缓存替换策略不够灵活,无法准确的控制每个cache line被替换的时间点,导致cache的利用率不够高。
另一种缓存替换策略,通过每个cache line的历史信息预测该cache line未来是否会被再次读取,如果硬件预测器认为该cache line不会被再次读取,那么在对cache中的cache line进行替换时,可以优先将该cache line进行替换。
但是,基于历史信息预测cache line是否会被再次读取的可靠性较低,偶然性较大,无法与业务本身的特点结合,因此,无法较为精确的控制每个cache line被替换的时间点。
为了改善现有技术中的缓存替换策略不够灵活,无法准确的控制每个cache line被替换的时间点,导致cache的利用率不够高的问题,本申请实施例提供一种缓存替换方法,该方法能够较为准确的控制每个cache line被替换的时间点,提高cache的利用率。
本申请实施例提供的缓存替换方法可以应用于图1所示的计算系统,如图1所示,该计算系统包括CPU、缓存cache和内存,cache中包括缓存控制器,cache的所有硬件控制逻辑均由缓存控制器实现。
当CPU发出访问请求后,cache控制器基于访问请求中的目的地址查看cache是否有请求数据,如果CPU请求的数据在cache中命中,可以直接返回该数据,无需访问内存。如果CPU请求的数据在cache中未命中,而且确定要将CPU请求的数据载入cache,那么可以在cache中选择待替换的cache line,并采用内存中基于目的地址获取的cache line替换cache中的待替换cache line。
需要说明的是,当CPU请求的数据在cache中未命中时,若需要对cache中的数据进行替换,数据替换的单位为缓存行cache line。即cache与内存之间的数据交换的粒度为cache line。
如图1所示,为了较为精确的控制cache line被替换的时间点,提升cache的利用 率,降低cache line被误替换的概率,本申请实施例为每个cache line设置一个计数器,该计数器用于维护该cache line被访问的次数。
示例性的,在一些专用的领域,数据会被访问的次数在一定程度是可以预见的。比如,人工智能(artificial intelligence,AI)训练中,可以将训练集分成32个mini batch,并将其部署在32个计算节点(比如,AI core),那么权重就会被32个计算节点各读取一次,即该数据被读取的次数总共为32次。当该数据被读取32次后,如果需要进行缓存替换,就可以优先将这些数据从cache中替换出去,因此能够准确的控制每个cache line被替换的时间点,提升cache的利用率。
可选的,本申请实施例中的cache可以由静态随机存取存储器(static random-access memory,SRAM)构成。
可选的,cache可以集成在CPU上,也可以集成在内存上,还可以独立设置。图1仅以cache独立于CPU和内存为例进行示意。
结合图1,如图2所示,为本申请实施例提供的一种缓存替换方法,该缓存替换方法包括以下步骤:
S201、缓存控制器获取访问请求和第一访问次数。
该访问请求包括请求访问的目的地址,该第一访问次数为预先确定的第一cache line会被访问的次数,该第一cache line为缓存控制器获取的访问请求请求访问的cache line。
可选的,缓存控制器获取的第一访问次数可以携带在访问请求中,也可以不携带在访问请求中,本申请实施例对此并不限定。
可选的,缓存控制器获取访问请求可以包括:缓存控制器接收来自处理器的访问请求。
可选的,上述访问请求可以为读请求,也可以为写请求,本申请实施例对此并不限定。
可选的,上述第一访问次数可以为预先确定的第一cache line会被读取的次数。或者,上述第一访问次数也可以为预先确定的第一cache line会被写入的次数。或者,上述第一访问次数还可以为预先确定的第一cache line会被读取的次数,以及第一cache line会被写入的次数的总和。
本申请实施例中,cache中的每个cache line对应一个计数器,该计数器用于对该cache line被访问的次数进行计数。例如,CPU请求访问的第一cache line每被访问一次,该第一cache line对应的计数器会进行计数,当该第一cache line被访问的次数达到第一访问次数时,如果需要进行缓存替换,就可以优先将该第一cache line替换出去。
示例性的,当上述第一访问次数为预先确定的第一cache line会被读取的次数时,第一cache line对应的计数器用于对该第一cache line被读取的次数进行计数。当上述第一访问次数为预先确定的第一cache line会被写入的次数时,第一cache line对应的计数器用于对该第一cache line被写入的次数进行计数。当上述第一访问次数为预先确定的第一cache line会被读取及写入的次数时,第一cache line对应的计数器用于对该第一cache line被读取以及被写入的次数进行计数。
可选的,在上述步骤S201之前还可以包括:处理器可以通过软件下发每个cache  line会被访问次数(access counter,AC)。例如,处理器获取第一访问次数,并通过软件向缓存控制器下发第一访问次数。
示例性的,上述第一访问次数可以是程序编程人员预先确定的第一cache line会被访问的次数,也可以是通过软件算法预先确定的第一cache line会被访问的次数。例如,编程人员在申请内存时,可以将第一访问次数传参传下来,在操作系统建立页表的时候,将第一访问次数写入到页表中,在CPU或者加速器发出访问请求,请求访问该页表对应的物理地址时,可以从页表中获取第一访问次数,并发送给缓存控制器。也就是说,本申请实施例中可以通过软件提前预测数据未来会被访问的次数,并通过页表或者寄存器配置或其他方式下发该访问次数,从而使得缓存控制器能够基于每个cache line会被访问的次数,确定该cache line是否可以被替换。
例如,如图3所示,CPU可以通过内存管理单元(memory management unit,MMU)将访问次数AC带出,发送给缓存控制器。可选的,访问次数也可以通过其他方式下发,例如,也可以是加速器通过系统内存管理单元(system memory management unit,SMMU)将访问次数AC带出,该加速器可以为神经网络处理器(network process unit,NPU)、图形处理器(graphics processing unit,GPU)、或数字信号处理器(digital signal processing,DSP)等处理器,本申请实施例对此并不限定。
S202、缓存控制器基于目的地址,查询缓存。
示例性的,缓存控制器可以基于访问请求中的目的地址,查询缓存,确定请求访问的数据是否在cache中命中。若请求访问的数据在cache中命中,将该数据返回给CPU。若请求访问的数据在cache中未命中,确定cache中未存储请求访问的数据。
可选的,在缓存控制器确定目的地址未命中cache中的任意一个cache line,而且cache中没有空闲的cache line的情况下,如果需要将CPU请求访问的数据存入cache中,可以进一步确定cache中是否存在访问次数到达预先确定的访问次数的cache line,如果cache中存在访问次数到达预先确定的访问次数的cache line,该方法还可以包括步骤S203。
S203、若目的地址未命中缓存中的任意一个cache line,且缓存中存在第二cache line,缓存控制器采用第一cache line替换第二cache line,并将第一cache line对应的计数器的值初始化。
其中,第二cache line为访问次数达到第二访问次数的cache line,第二访问次数为预先确定的第二cache line会被访问的次数。
可选的,在第二cache line对应的计数器初始化时的值为第二访问次数的情况下,上述第二cache line的访问次数达到第二访问次数是指第二cache line对应的计数器的值为0。
例如,以第二访问次数为预先确定的第二cache line会被读取的次数,该第二cache line会被读取的次数AC为32为例。如图3所示,在将第二cache line载入缓存中时,可以将第二cache line对应的计数器的值初始化为32,该第二cache line每被读取一次,该第二cache line对应的计数器的值相应的减一。当该第二cache line对应的计数器的值为0时,确定该第二cache line的读取次数达到第二访问次数,因此该第二cache line可能不会再被读取,进而如果需要进行缓存替换,可以将该第二cache line替换出去。 需要说明的是,以将第二cache line从内存载入缓存中时,将第二cache line对应的计数器的值初始化为32为例,缓存每向处理器返回一次该第二cache line中的数据,即为该第二cache line被读取一次,那么该第二cache line对应的计数器的值减一。当该第二cache line对应的计数器的值减为0时,确定第二cache line的访问次数达到第二访问次数32。
可选的,在第二cache line对应的计数器初始化时的值为0的情况下,上述第二cache line的访问次数达到第二访问次数是指第二cache line对应的计数器的值为第二访问次数。
例如,以第二访问次数为预先确定的第二cache line会被读取的次数,该第二cache line会被读取的次数为32为例。在将第二cache line载入缓存中时,可以将第二cache line对应的计数器的值初始化为0,该第二cache line每被读取一次,该第二cache line对应的计数器的值相应的加一。当该第二cache line对应的计数器的值为32时,确定该第二cache line的读取次数达到第二访问次数,因此该第二cache line可能不会再被读取,进而如果需要进行缓存替换,可以将该第二cache line替换出去。
可以理解的,本申请实施例在对cache中的cache line进行替换时,优先替换的是cache中访问次数达到预先确定的访问次数的cache line。由于在一些专用领域中,数据会被访问的次数是可以预见的,因此通过计数器维护cache line被访问的次数,如果需要进行缓存替换,可以将cache中访问次数达到预先确定的访问次数的cache line替换出去,从而不仅能够降低误替换发生的概率,而且能够准确的控制每个cache line被替换的时间点,提升cache的利用率。
可选的,缓存控制器采用第一cache line替换第二cache line时,可以基于访问请求中的目的地址,在内存中获取第一cache line,并用第一cache line替换第二cache line写入缓存中。
可选的,缓存控制器在将第二cache line替换为第一cache line后,可以将第一cache line对应的计数器的数值初始化为0,也可以将第一cache line对应的计数器的数值初始化为第一访问次数,还可以将第一cache line对应的计数器的数值初始化为其他数值,本申请实施例对此并不限定。本实施例仅以在cache line从内存载入缓存中时,将cache line对应的计数器初始化为0或该cache line对应的访问次数为例进行说明。可以理解的,计数器初始化的值不同,对应的计数策略可能不同。
例如,在将cache中的第二cache line替换为第一cache line后,如果将第一cache line对应的计数器的值初始化为0,那么该第一cache line每被访问一次,缓存控制器将第一cache line对应的计数器的值加一,当第一cache line对应的计数器的值到达第一访问次数时,确定该第一cache line不会再被访问,如果需要进行缓存替换,可以优先将该第一cache line替换出去。
再例如,在将cache中的第二cache line替换为第一cache line后,如果将第一cache line对应的计数器的值初始化为第一访问次数,那么该第一cache line每被访问一次,缓存控制器将第一cache line对应的计数器的值减一,当第一cache line对应的计数器的值到达0时,确定该第一cache line不会再被访问,如果需要进行缓存替换,可以优先将该第一cache line替换出去。
可选的,缓存控制器将第二cache line替换为第一cache line,将第一cache line对应的计数器的值初始化以后,缓存控制器可以向处理器返回第一cache line中处理器请求的数据,并将该第一cache line对应的计数器的值加一或减一。后续当处理器再次访问第一cache line时,每访问一次第一cache line,该第一cache line对应的计数器的值相应的加一或减一。可以理解的,缓存控制器中的cache line每被处理器访问一次,该cache line对应的计数器的值将加一或减一。
本申请实施例在将cache line从内存载入缓存中时,缓存控制器会对该cache line对应的计数器进行初始化。可选的,每个cache line对应的计数器初始化的值可以为0,也可以为预先确定的该cache line会被访问次数,当然也可以为其它数值,本申请实施例对此并不限定。本申请实施例仅以cache line初始化的值为0或预先确定的该cache line会被访问次数为例进行说明。
可以理解的,本申请实施例在进行缓存替换时,通过将替换后的cache line对应的计数器的值初始化,该cache line每被访问一次,计数器进行相应的计数,从而能够在该cache line被访问的次数到达预先确定的访问次数时,如果需要进行缓存替换,可以优先将该访问次数到达预先确定的访问次数的cache line替换出去,从而能够较为准确的控制每个cache line被替换的时间点,降低cache被误替换的概率,提高cache的利用率。
可选的,本申请实施例还提供一种缓存替换方法,如图4所示,若确定目的地址在cache中命中,该方法在上述步骤S201-S202之后还可以包括步骤S204或S205。
S204、在第一cache line对应的计数器初始化时的值为第一访问次数的情况下,缓存控制器将第一cache line对应的计数器的值减一。
示例性的,如果步骤S201中的访问请求请求访问的目的地址在cache中命中,即缓存中存储了访问请求请求的数据,那么该数据所在的第一cache line每被访问一次,缓存控制器对第一cache line对应的计数器进行相应的计数。
可选的,在第一cache line对应的计数器初始化时的值为第一访问次数的情况下,若目的地址在cache中命中,那么访问请求请求访问的第一cache line会被访问一次,缓存控制器将该第一cache line对应的计数器的值减一。
可以理解的,当第一cache line对应的计数器的值减为0时,缓存控制器确定该第一cache line的访问次数达到预先确定的访问次数。
S205、在第一cache line对应的计数器初始化时的值为0的情况下,缓存控制器将第一cache line对应的计数器的值加一。
可选的,在第一cache line对应的计数器初始化时的值为0的情况下,若目的地址在cache中命中,那么访问请求请求访问的第一cache line会被访问一次,缓存控制器将该第一cache line对应的计数器的值加一。
可以理解的,当第一cache line对应的计数器的值达到第一访问次数时,缓存控制器确定该第一cache line的访问次数达到预先确定的访问次数。
本申请实施例提供的缓存替换方法,通过为缓存中的每个cache line设置一个计数器,cache line每被访问一次,缓存控制器控制计数器进行相应的计数,从而能够在该cache line被访问的次数到达预先确定的访问次数时,如果需要进行缓存替换,可以优 先将该访问次数到达预先确定的访问次数的cache line替换出去,从而能够较为准确的控制每个cache line被替换的时间点,降低cache被误替换的概率,提高cache的利用率。
可选的,为了防止访问次数设置的不够准确,导致cache line长期滞留在cache中,本申请实施例还提供一种缓存替换方法,如图5所示,在上述步骤S201-S202以后该方法还可以包括以下步骤:
S206、在目的地址未命中缓存中的任意一个cache line,且缓存中不存在第二cache line情况下,若确定缓存中存在第三cache line,缓存控制器采用第一cache line替换第三cache line,并将第一cache line对应的计数器的值初始化。
其中,第三cache line为缓存中滞留时长超过预设时长的cache line。
可选的,在目的地址未命中cache时,如果需要进行缓存替换,但cache中所有cache line被访问的次数均未达到预先确定的访问次数,那么可以将cache中长期滞留的第三cache line替换出去。也就是说,在进行缓存替换时,如果所有cache line被访问的次数均未达到预先确定的访问次数,那么这些cache line未来都有可能被访问。但是有可能是因为预先确定的访问次数设置的不够准确,导致一些cache line被访问的次数未达到预先确定的访问次数,这将造成这些cache line长期滞留在cache中,因此在cache中不存在第二cache line的情况下,可以将cache中长期滞留的第三cache line替换出去,改善访问次数设置的不准确,导致cache line长期滞留在cache中影响cache利用率的问题。
可选的,上述第三cache line的滞留时长可以通过为每个cache line设置滞留域的方式进行维护。示例性的,缓存控制器可以周期性的扫描缓存中的每个cache line,每扫描到一次cache line,缓存控制器将该cache line对应的滞留域的值加一,cache line每被访问一次,缓存控制器将该cache line对应的滞留域的值清零。因此,滞留域的数值越大,代表该滞留域对应的cache line滞留的时间越长。
可选的,上述第三cache line可以为滞留域的值超过预设阈值的cache line。
本申请实施例提供的缓存替换方法,通过为缓存中的每个cache line设置一个计数器,cache line每被访问一次,缓存控制器控制计数器进行相应的计数,从而能够在该cache line被访问的次数到达预先确定的访问次数时,如果需要进行缓存替换,可以优先将该访问次数到达预先确定的访问次数的cache line替换出去,从而能够较为准确的控制每个cache line被替换的时间点,降低cache被误替换的概率,提高cache的利用率。而且在缓存中不存在第二cache line情况下,可以通过将缓存中长期滞留的cache line替换出去,能够进一步提高cache的利用率。
可选的,本申请实施例还提供一种缓存替换方法,如图6所示,在上述步骤S201-S202以后,该方法还可以包括以下步骤:
S207、在目的地址未命中缓存中的任意一个cache line,且缓存中不存在第二cache line及第三cache line的情况下,缓存控制器确定待替换cache line。
可选的,上述步骤S207可以包括:缓存控制器采用先进先出FIFO策略、最近最少使用LRU策略、或随机策略,确定待替换cache line。对于缓存控制器采用先进先出FIFO策略、最近最少使用LRU策略、或随机策略,确定待替换cache line的具体 实现方式,在此不再赘述。
示例性的,在目的地址未命中cache时,如果需要进行缓存替换,但cache中所有cache line被访问的次数均未达到预先确定的访问次数,而且没有长期滞留的cache line,那么缓存控制器可以基于FIFO策略、LRU策略、或随机策略,确定待替换的cache line。
S208、缓存控制器采用第一cache line替换待替换cache line,并将第一cache line对应的计数器的值初始化。
示例性的,缓存控制器用第一cache line将待替换cache line替换出去后,可以初始化第一cache line对应的计数器的值。可选的,当缓存控制器将第一cache line对应的计数器的值初始化为0时,该第一cache line每被访问一次,该第一cache line对应的计数器的值加一。当缓存控制器将第一cache line对应的计数器的值初始化为第一读取次数时,该第一cache line每被访问一次,该第一cache line对应的计数器的值减一。
本申请实施例提供的缓存替换方法,通过为缓存中的每个cache line设置一个计数器,cache line每被访问一次,缓存控制器控制计数器进行相应的计数,从而能够在该cache line被访问的次数到达预先确定的访问次数时,如果需要进行缓存替换,可以优先将该访问次数到达预先确定的访问次数的cache line替换出去,从而能够较为准确的控制每个cache line被替换的时间点,降低cache被误替换的概率,提高cache的利用率。而且在缓存中不存在第二cache line以及第三cache line的情况下,可以通过FIFO策略、LRU策略、或随机策略,确定待替换的cache line,并将该待替换的cache line替换出去,以提高处理器的访问速度。
本申请实施例还提供一种缓存替换方法,如图7所示,该方法包括以下步骤:
S701、缓存控制器获取访问请求。
该访问请求包括请求访问的目的地址以及第一访问次数,该第一访问次数为预先确定的第一cache line会被访问的次数,该第一cache line为缓存控制器获取的访问请求请求访问的cache line。
S702、缓存控制器基于目的地址,查询缓存,确定目的地址是否在缓存中命中。
若确定目的地址在缓存中命中,继续执行步骤S703或S704;若确定目的地址未命中缓存中的任意一个cache line,继续执行步骤S705。
S703、在第一cache line对应的计数器初始化时的值为第一访问次数的情况下,缓存控制器将第一cache line对应的计数器的值减一。
S704、在第一cache line对应的计数器初始化时的值为0的情况下,缓存控制器将第一cache line对应的计数器的值加一。
S705、缓存控制器确定缓存中是否存在第二cache line。
其中,第二cache line为访问次数达到第二访问次数的cache line,第二访问次数为预先确定的第二cache line会被访问的次数。
若确定缓存中存在第二cache line,继续执行步骤S706;若确定缓存中不存在第二cache line,继续执行步骤S707。
S706、缓存控制器采用第一cache line替换第二cache line,并将第一cache line对应的计数器的值初始化。
S707、缓存控制器确定缓存中是否存在第三cache line。
其中,第三cache line为缓存中滞留时长超过预设时长的cache line。
若确定缓存中存在第三cache line,继续执行步骤S708;若确定缓存中不存在第三cache line,继续执行步骤S709。
S708、缓存控制器采用第一cache line替换第三cache line,并将第一cache line对应的计数器的值初始化。
S709、缓存控制器确定待替换cache line,采用第一cache line替换待替换cache line,并将第一cache line对应的计数器的值初始化。
S710、缓存控制器返回数据。
示例性的,缓存控制器返回的数据为处理器请求访问的第一cache line中的数据。
可选的,缓存控制器返回数据,即为该第一cache line被读取一次,那么该第一cache line对应的计数器的值相应的加一或减一。例如,如果初始化时将第一cache line对应的计数器的值初始化为0,那么步骤S710返回数据时,确定该第一cache line被读取一次,将该第一cache line对应的计数器的值加一。再例如,如果初始化时将第一cache line对应的计数器的值初始化为第一访问次数,那么步骤S710返回数据时,确定该第一cache line被读取一次,将该第一cache line对应的计数器的值减一。
可以理解的,上述步骤S701-S709的具体实现方式可以参考前述步骤S201-S207中相应步骤的实现方式,在此不再赘述。
本申请实施例提供的缓存替换方法,通过为缓存中的每个cache line设置一个计数器,cache line每被访问一次,缓存控制器控制计数器进行相应的计数,从而能够在该cache line被访问的次数到达预先确定的访问次数时,如果需要进行缓存替换,可以优先将该访问次数到达预先确定的访问次数的cache line替换出去,从而能够较为准确的控制每个cache line被替换的时间点,降低cache被误替换的概率,提高cache的利用率。而且在缓存中不存在第二cache line情况下,可以通过将缓存中长期滞留的cache line替换出去,能够进一步提高cache的利用率。
本申请实施例还提供一种缓存替换装置,该缓存替换装置包括缓存及缓存控制器,缓存控制器用于实现图2、图4至图7中任一所示的缓存替换方法。
本申请实施例还提供一种电子设备,如图8所示,该电子设备包括处理器、缓存、缓存控制器,以及内存,处理器用于向缓存控制器发送访问请求,该访问请求包括请求访问的目的地址以及上述第一访问次数。缓存控制器用于获取该访问请求,并基于访问请求中的目的地址,查询缓存。若的地址未命中缓存中的任意一个cache line,且缓存中存在上述第二cache line,缓存控制器采用第一cache line替换第二cache line,并将第一cache line对应的计数器的值初始化。可选的,缓存控制器可以从内存中获取上述第一cache line。
可选的,图8所示的缓存控制器还用于实现图2、图4至图7中任一所示的缓存替换方法。
结合本申请公开内容所描述的方法或者算法的步骤可以硬件的方式来实现,也可以是由处理器执行软件指令的方式来实现。软件指令可以由相应的软件模块组成,软件模块可以被存放于随机存取存储器(random access memory,RAM)、闪存、可擦 除可编程只读存储器(erasable programmable ROM,EPROM)、电可擦可编程只读存储器(electrically EPROM,EEPROM)、寄存器、硬盘、移动硬盘、只读光盘(CD-ROM)或者本领域熟知的任何其它形式的存储介质中。一种示例性的存储介质耦合至处理器,从而使处理器能够从该存储介质读取信息,且可向该存储介质写入信息。当然,存储介质也可以是处理器的组成部分。处理器和存储介质可以位于ASIC中。另外,该ASIC可以位于核心网接口设备中。当然,处理器和存储介质也可以作为分立组件存在于核心网接口设备中。
本领域技术人员应该可以意识到,在上述一个或多个示例中,本发明所描述的功能可以用硬件、软件、固件或它们的任意组合来实现。当使用软件实现时,可以将这些功能存储在计算机可读介质中或者作为计算机可读介质上的一个或多个指令或代码进行传输。计算机可读介质包括计算机存储介质和通信介质,其中通信介质包括便于从一个地方向另一个地方传送计算机程序的任何介质。存储介质可以是通用或专用计算机能够存取的任何可用介质。
以上所述的具体实施方式,对本发明的目的、技术方案和有益效果进行了进一步详细说明,所应理解的是,以上所述仅为本发明的具体实施方式而已,并不用于限定本发明的保护范围,凡在本发明的技术方案的基础之上,所做的任何修改、等同替换、改进等,均应包括在本发明的保护范围之内。

Claims (23)

  1. 一种缓存替换方法,其特征在于,所述缓存包括多个缓存行cache line,每个所述cache line对应一个计数器,所述方法包括:
    缓存控制器获取访问请求和第一访问次数;所述访问请求包括请求访问的目的地址,所述第一访问次数为预先确定的第一cache line会被访问的次数,所述第一cache line为所述访问请求请求访问的cache line;
    所述缓存控制器基于所述目的地址,查询所述缓存,若所述目的地址未命中所述缓存中的任意一个cache line,且所述缓存中存在第二cache line,所述缓存控制器采用所述第一cache line替换所述第二cache line,并将所述第一cache line对应的计数器的值初始化;其中,所述第二cache line为访问次数达到第二访问次数的cache line,所述第二访问次数为预先确定的所述第二cache line会被访问的次数。
  2. 根据权利要求1所述的方法,其特征在于,在所述第二cache line对应的计数器初始化时的值为所述第二访问次数的情况下,所述第二cache line的访问次数达到所述第二访问次数为所述第二cache line对应的计数器的值为0。
  3. 根据权利要求1所述的方法,其特征在于,在所述第二cache line对应的计数器初始化时的值为0的情况下,所述第二cache line的访问次数达到所述第二访问次数为所述第二cache line对应的计数器的值为所述第二访问次数。
  4. 根据权利要求1-3中任一项所述的方法,其特征在于,若所述目的地址在所述缓存中命中,所述方法还包括:
    在所述第一cache line对应的计数器初始化时的值为所述第一访问次数的情况下,将所述第一cache line对应的计数器的值减一;或者,
    在所述第一cache line对应的计数器初始化时的值为0的情况下,将所述第一cache line对应的计数器的值加一。
  5. 根据权利要求1-4中任一项所述的方法,其特征在于,在所述目的地址未命中所述缓存中的任意一个cache line,且所述缓存中不存在所述第二cache line情况下,所述方法还包括:
    若确定所述缓存中存在第三cache line,所述缓存控制器采用所述第一cache line替换所述第三cache line,并将所述第一cache line对应的计数器的值初始化;其中,所述第三cache line为所述缓存中滞留时长超过预设时长的cache line。
  6. 根据权利要求5所述的方法,其特征在于,所述缓存中的每个所述cache line对应一个滞留域,所述方法还包括:
    所述缓存控制器周期性的扫描所述缓存中的每个所述cache line,每扫描到一次所述cache line,所述缓存控制器将该cache line对应的滞留域的值加一;所述cache line每被访问一次,所述缓存控制器将该cache line对应的滞留域的值清零。
  7. 根据权利要求6所述的方法,其特征在于,所述第三cache line对应的滞留域的值超过预设阈值。
  8. 根据权利要求5-7中任一项所述的方法,其特征在于,在所述目的地址未命中所述缓存中的任意一个cache line,且所述缓存中不存在所述第二cache line及所述第三cache line的情况下,所述方法还包括:
    所述缓存控制器确定待替换cache line,采用所述第一cache line替换所述待替换cache line,并将所述第一cache line对应的计数器的值初始化。
  9. 根据权利要求8所述的方法,其特征在于,所述缓存控制器确定所述待替换cache line,包括:
    所述缓存控制器采用先进先出FIFO策略、最近最少使用LRU策略、或随机策略,确定所述待替换cache line。
  10. 根据权利要求1-9中任一项所述的方法,其特征在于,所述方法还包括:
    处理器获取所述第一访问次数;
    所述处理器向所述缓存控制器发送所述第一访问次数。
  11. 根据权利要求1-10中任一项所述的方法,其特征在于,所述访问请求为读请求或写请求。
  12. 一种缓存替换装置,其特征在于,所述缓存包括多个缓存行cache line,每个所述cache line对应一个计数器,所述缓存替换装置包括:
    缓存控制器,用于获取访问请求和第一访问次数;所述访问请求包括请求访问的目的地址,所述第一访问次数为预先确定的第一cache line会被访问的次数,所述第一cache line为所述访问请求请求访问的cache line;
    所述缓存控制器,还用于基于所述目的地址,查询所述缓存,若所述目的地址未命中所述缓存中的任意一个cache line,且所述缓存中存在第二cache line,所述缓存控制器采用所述第一cache line替换所述第二cache line,并将所述第一cache line对应的计数器的值初始化;其中,所述第二cache line为访问次数达到第二访问次数的cache line,所述第二访问次数为预先确定的所述第二cache line会被访问的次数。
  13. 根据权利要求12所述的装置,其特征在于,在所述第二cache line对应的计数器初始化时的值为所述第二访问次数的情况下,所述第二cache line的访问次数达到所述第二访问次数为所述第二cache line对应的计数器的值为0。
  14. 根据权利要求12所述的装置,其特征在于,在所述第二cache line对应的计数器初始化时的值为0的情况下,所述第二cache line的访问次数达到所述第二访问次数为所述第二cache line对应的计数器的值为所述第二访问次数。
  15. 根据权利要求12-14中任一项所述的装置,其特征在于,若所述目的地址在所述缓存中命中,所述缓存控制器,还用于:
    在所述第一cache line对应的计数器初始化时的值为所述第一访问次数的情况下,将所述第一cache line对应的计数器的值减一;或者,
    在所述第一cache line对应的计数器初始化时的值为0的情况下,将所述第一cache line对应的计数器的值加一。
  16. 根据权利要求12-15中任一项所述的装置,其特征在于,在所述目的地址未命中所述缓存中的任意一个cache line,且所述缓存中不存在所述第二cache line情况下,所述缓存控制器,还用于:
    若确定所述缓存中存在第三cache line,采用所述第一cache line替换所述第三cache line,并将所述第一cache line对应的计数器的值初始化;其中,所述第三cache line为所述缓存中滞留时长超过预设时长的cache line。
  17. 根据权利要求16所述的装置,其特征在于,所述缓存中的每个所述cache line对应一个滞留域,所述缓存控制器,还用于:
    周期性的扫描所述缓存中的每个所述cache line,每扫描到一次所述cache line,所述缓存控制器将该cache line对应的滞留域的值加一;所述cache line每被访问一次,所述缓存控制器将该cache line对应的滞留域的值清零。
  18. 根据权利要求17所述的装置,其特征在于,所述第三cache line对应的滞留域的值超过预设阈值。
  19. 根据权利要求16-18中任一项所述的装置,其特征在于,在所述目的地址未命中所述缓存中的任意一个cache line,且所述缓存中不存在所述第二cache line及所述第三cache line的情况下,所述缓存控制器,还用于:
    确定待替换cache line,采用所述第一cache line替换所述待替换cache line,并将所述第一cache line对应的计数器的值初始化。
  20. 根据权利要求19所述的装置,其特征在于,所述缓存控制器,具体用于采用先进先出FIFO策略、最近最少使用LRU策略、或随机策略,确定所述待替换cache line。
  21. 根据权利要求12-20中任一项所述的装置,其特征在于,所述缓存替换装置还包括处理器,所述处理器,用于:
    获取所述第一访问次数;
    向所述缓存控制器发送所述第一访问次数。
  22. 根据权利要求12-21中任一项所述的装置,其特征在于,所述访问请求为读请求或写请求。
  23. 一种电子设备,其特征在于,所述电子设备包括缓存、缓存控制器及处理器,所述缓存包括多个缓存行cache line,每个cache line对应一个计数器;
    所述处理器,用于向所述缓存控制器发送访问请求和第一访问次数;所述访问请求包括请求访问的目的地址,所述第一访问次数为预先确定的第一cache line会被访问的次数,所述第一cache line为所述访问请求请求访问的cache line;
    所述缓存控制器,用于获取所述访问请求和第一访问次数;基于所述目的地址,查询所述缓存,若所述目的地址未命中所述缓存中的任意一个cache line,且所述缓存中存在第二cache line,所述缓存控制器采用所述第一cache line替换所述第二cache line,并将所述第一cache line对应的计数器的值初始化;其中,所述第二cache line为访问次数达到第二访问次数的cache line,所述第二访问次数为预先确定的所述第二cache line会被访问的次数。
PCT/CN2021/078278 2021-02-26 2021-02-26 一种缓存替换方法和装置 WO2022178869A1 (zh)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN202180093712.0A CN116897335A (zh) 2021-02-26 2021-02-26 一种缓存替换方法和装置
PCT/CN2021/078278 WO2022178869A1 (zh) 2021-02-26 2021-02-26 一种缓存替换方法和装置

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
PCT/CN2021/078278 WO2022178869A1 (zh) 2021-02-26 2021-02-26 一种缓存替换方法和装置

Publications (1)

Publication Number Publication Date
WO2022178869A1 true WO2022178869A1 (zh) 2022-09-01

Family

ID=83047652

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2021/078278 WO2022178869A1 (zh) 2021-02-26 2021-02-26 一种缓存替换方法和装置

Country Status (2)

Country Link
CN (1) CN116897335A (zh)
WO (1) WO2022178869A1 (zh)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116107926A (zh) * 2023-02-03 2023-05-12 摩尔线程智能科技(北京)有限责任公司 缓存替换策略的管理方法、装置、设备、介质和程序产品
CN117349199A (zh) * 2023-11-30 2024-01-05 摩尔线程智能科技(北京)有限责任公司 缓存管理装置及系统

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117349198A (zh) * 2023-11-30 2024-01-05 摩尔线程智能科技(北京)有限责任公司 缓存行替换方法及装置

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102667737A (zh) * 2009-12-21 2012-09-12 索尼公司 缓冲存储器和缓冲存储器控制单元
US20130297882A1 (en) * 2011-01-12 2013-11-07 Fujitsu Limited Cache memory device, control unit of cache memory, information processing apparatus, and cache memory control method
CN104583891A (zh) * 2012-09-25 2015-04-29 英特尔公司 用于非易失性主存储器系统中的自适应高速缓存更换的装置、系统和方法
CN105094686A (zh) * 2014-05-09 2015-11-25 华为技术有限公司 数据缓存方法、缓存和计算机系统
CN108139872A (zh) * 2016-01-06 2018-06-08 华为技术有限公司 一种缓存管理方法、缓存控制器以及计算机系统
CN109074319A (zh) * 2016-04-08 2018-12-21 高通股份有限公司 高速缓存中的分配的选择性绕过

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102667737A (zh) * 2009-12-21 2012-09-12 索尼公司 缓冲存储器和缓冲存储器控制单元
US20130297882A1 (en) * 2011-01-12 2013-11-07 Fujitsu Limited Cache memory device, control unit of cache memory, information processing apparatus, and cache memory control method
CN104583891A (zh) * 2012-09-25 2015-04-29 英特尔公司 用于非易失性主存储器系统中的自适应高速缓存更换的装置、系统和方法
CN105094686A (zh) * 2014-05-09 2015-11-25 华为技术有限公司 数据缓存方法、缓存和计算机系统
CN108139872A (zh) * 2016-01-06 2018-06-08 华为技术有限公司 一种缓存管理方法、缓存控制器以及计算机系统
CN109074319A (zh) * 2016-04-08 2018-12-21 高通股份有限公司 高速缓存中的分配的选择性绕过

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116107926A (zh) * 2023-02-03 2023-05-12 摩尔线程智能科技(北京)有限责任公司 缓存替换策略的管理方法、装置、设备、介质和程序产品
CN116107926B (zh) * 2023-02-03 2024-01-23 摩尔线程智能科技(北京)有限责任公司 缓存替换策略的管理方法、装置、设备、介质和程序产品
CN117349199A (zh) * 2023-11-30 2024-01-05 摩尔线程智能科技(北京)有限责任公司 缓存管理装置及系统

Also Published As

Publication number Publication date
CN116897335A (zh) 2023-10-17

Similar Documents

Publication Publication Date Title
WO2022178869A1 (zh) 一种缓存替换方法和装置
CN105740164B (zh) 支持缓存一致性的多核处理器、读写方法、装置及设备
US8176258B2 (en) System and method for cache management
US8949544B2 (en) Bypassing a cache when handling memory requests
JP2020046963A (ja) メモリシステムおよび制御方法
US8443152B2 (en) Cache memory capable of adjusting burst length of write-back data in write-back operation
CN111414132B (zh) 带异构存储器的主存储设备、计算机系统及数据管理方法
US20080086599A1 (en) Method to retain critical data in a cache in order to increase application performance
US20060282620A1 (en) Weighted LRU for associative caches
JP2003337834A (ja) サイズ変更可能なキャッシュ・センシティブ・ハッシュ・テーブル
US20180081567A1 (en) Buffer allocation and memory management using mapping table
US11698757B2 (en) Memory system and method of controlling nonvolatile memory
JP2008502069A (ja) メモリ・キャッシュ制御装置及びそのためのコヒーレンシ動作を実行する方法
US11762597B2 (en) Memory system and control method controlling nonvolatile memory in accordance with command issued by processor
US11782848B2 (en) Home agent based cache transfer acceleration scheme
WO2024066195A1 (zh) 缓存管理方法及装置、缓存装置、电子装置和介质
US20160334996A1 (en) In-flash immutable object processing
CN115269454A (zh) 数据访问方法、电子设备和存储介质
CN114217861A (zh) 数据处理方法及装置、电子装置和存储介质
CN113138851A (zh) 一种缓存管理方法及装置
CN116027982A (zh) 数据处理方法、设备及可读存储介质
CN113253939B (zh) 数据处理方法、装置、电子设备及存储介质
US10303375B2 (en) Buffer allocation and memory management
CN115080459A (zh) 缓存管理方法及装置、计算机可读存储介质
CN112559574B (zh) 数据处理方法、装置、电子设备及可读存储介质

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 21927305

Country of ref document: EP

Kind code of ref document: A1

WWE Wipo information: entry into national phase

Ref document number: 202180093712.0

Country of ref document: CN

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 21927305

Country of ref document: EP

Kind code of ref document: A1