Reputation: 31
I am reading OS Design: Three Easy Pieces, and I stumbled upon this quote in chapter 19: Translation Lookaside Buffers. It talks about eviction policies and comparing the hit efficiency of LRU vs. a random selection eviction.
Such a policy (Random policy) is useful due to its simplicity and ability to avoid corner-case behaviors; for example, a “reasonable” policy such as LRU behaves quite unreasonably when a program loops over n + 1 pages with a TLB of size n; in this case, LRU misses upon every access, whereas random does much better
I think I understand why LRU misses upon every access. For example, take an empty TLB of size 3 pages and a program loops through 4. The first page access is a miss, then second is a miss, then the third, then the fourth one is not in the TLB, so we must evict the least recent one (first page). After that eviction, we repeat the TLB lookup to finally get the fourth page in it, giving a total of four misses, but now how does the Random eviction work? Let's have the same empty TLB and program. We miss one, two, three pages. Now the fourth ought to be in there, so we have to replace a random page this time. So we repeat a TLB lookup and now the fourth page could be in the first, second or third TLB entry. Doesn't this just lead to four misses as well?
Where in my assumptions and logic about how TLB and evictions work am I missing here that random is better than LRU with this corner case. Doesn't random also just miss the same amount?
A step by step explanation of each policy would be preferable. Thanks.
Upvotes: 0
Views: 443
Reputation: 37262
For example, take an empty TLB of size 3 pages and a program loops through 4. The first page access is a miss, then second is a miss, then the third, then the fourth one is not in the TLB, so we must evict the least recent one (first page). After that eviction, we repeat the TLB lookup to finally get the fourth page in it, giving a total of four misses, but now how does the Random eviction work?
In this case, after the first 3 misses the TLB will contain translations for pages 1, 2 and 3; then it will evict an entry to make room for the translation for page 4. There are 3 possibilities (chosen randomly):
If it evicts the translation for page 1 then you get a TLB miss for page 1.
If it evicts the translation for page 2 then you avoid a TLB miss for page 1.
If it evicts the translation for page 3 then you avoid a TLB miss for page 1.
In other words, there's a 66.66% chance that it will avoid a TLB miss when you need the translation for page 1 next. There's also a 33.333% chance that there will be a TLB miss when you need the translation for page 1 and that something else will be evicted to make room for it.
This probability will degrade. E.g. when you need the translation for page 2 again; there's a 33.33% chance it was evicted to make room for the translation of page 4, but there's also a chance that it was evicted to make room for the translation of page 1; so (guessing) there's might be a 50% chance that it wasn't evicted.
Doesn't this just lead to four misses as well?
I didn't do the maths (its messy and I haven't finished my morning coffee), but the chance of getting 4 misses is probably around 12%.
Upvotes: 1