Question

I'm doing some research about how to troubleshoot local memory pressure. Correct me if I'm wrong but as I understand it is that SQL Server uses a CLOCK algorithm for the cache- and userstores. Each clock has two hands, an external and an internal.

My interest is for the internal clock hand. If a worker that accesses a cache notices that a cache is greater than a certain percentage of memory, the internal clock hand starts to free memory for that cache.

Using the dmv dm_os_memory_cache_clock_hands I can see information about the clock hands. The column rounds_count represents how many times the clock hands scanned the entire cache. last_tick_time is the time the hand last moved. round_start_time is time of the last sweep.

What is the difference between last_tick_time and round_start_time? Is the cache not cleaned when the hand is moved?

How many entries get removed with each tick of the clock?

Can somebody helps me understanding the working of this clock algorithm in SQL Server?

Was it helpful?

Solution

Correct me if I'm wrong but as I understand it is that SQL Server uses a CLOCK algorithm for the cache- and userstores. Each clock has two hands, an external and an internal.

Yep, almost like what you have said but let me add something more. Cache and Userstores shares common costing and algorithm called as Least Recently Used(LRU) algorithm. This LRU mechanism has what is called as "clock algorithm". This algorithm has what is called as "hands". There are various eviction policies which work inside SQl Server to remove "old" entries from SQL Server caches and to help this SQL Server has 2 clock hands:

  1. External: Implements Global policy

  2. Internal: Implements local policy

Policy is something which you can consider as rule when dealing with removing old entries from caches.

If a worker that accesses a cache notices that a cache is greater than a certain percentage of memory, the internal clock hand starts to free memory for that cache.

There is lot more that happens it is not like that internal clock will "only" free memory by removing old entries after total memory utilized is greater than certain percentage, LRU and internal memory pressure is also taken into the account.

What is the difference between last_tick_time and round_start_time? Is the cache not cleaned when the hand is moved?

last_tick_time - time when a clock hand moved last.

round_start_time - time when a clock hand started current round. The round which it did to sweep the caches.

How many entries get removed with each tick of the clock?

There is no predefined value, after LRU algorithm has done its task other thread would see how many entries in cache are "old" and then would remove it. If there is memory pressure the value may increase and the sweeping may happen too often.

Can somebody helps me understanding the working of this clock algorithm in SQL Server

Slava Oaks from Microsoft has already written a lot about it. I will quote from his blog

In SQL Server 2005 we introduced new common caching frameworkAll caches except for Buffer Pool leverage it. The framework consist of set of stores and ResourceMonitor. There are three types of stores: cache store, user store (the name here is related to internal usage of the store - it does not have to do anything with a user) and object store. Both cache and user stores share common LRU mechanism and leverage common costing. Procedure cache is an example of a cache store and metadata cache is an example of a user store. Object stores are just pools of memory blocks and don't require either LRU nor costing. Network library leverage object store for pooling network buffers.

Store's LRU mechanism is a rather simple - we use variation of thoroughly discussed in operating system's literature clock algorithm. As you remember clock algorithm has a hand. When hand moves it decreases a cost of an entry. Once cost of an entry reaches 0, the entry can be discarded from the cache. Cost gets reset whenever an entry is reused. When dealing with multiple caches a designer has to take into account global and local eviction policies. Global policies consider global picture and enable running of LRU, clock algorithm, across all the caches. In its turn, local policies look at the local resource consumption only and run LRU for a given cache only.

In order to satisfy global and local policies SQL Server's stores implement two hands: external and internal (please don't confuse a clock hand names with types of memory pressure. There is no connection!). External clock hand implements global policy and internal clock hand implements local policy. Even though every cache has its own external hand, external hands move "synchronously" - one at a time. Their task is to simulate global hand.

Additional Reading: The Clock Hands of Buffer cache

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top