Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add combined locking support for MMContainer #182

Closed
wants to merge 1 commit into from

Conversation

igchor
Copy link
Contributor

@igchor igchor commented Dec 13, 2022

through withEvictionIterator function.

Also, expose the config option to enable and disable combined locking.
withEvictionIterator is implemented as an extra function, getEvictionIterator() is still there and it's behavior hasn't changed.

This is a subset of changes from: #172

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 13, 2022
@haowu14
Copy link
Contributor

haowu14 commented Dec 13, 2022

I was reading your comment in #172 (comment). In single-tier version, without the further change, would using combined_locking for iterator cause reduction in eviction success rate and why?

Copy link
Contributor

@haowu14 haowu14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good. Once you address the qestions I will import it for internal review.

@@ -68,6 +68,7 @@ class MM2Q {
enum LruType { Warm, WarmTail, Hot, Cold, ColdTail, NumTypes };

// Config class for MM2Q
// TODO: implement support for useCombinedLockForIterators
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the code you have, implementing this for 2Q is just adding the if statement you have for MMLRU right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the if statement + extending the MM2Q config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I just realized that TinyLFU uses a spin lock, which does not support combined locking, so there is no option to enable it.

@@ -343,5 +348,26 @@ size_t MMTypeTest<MMType>::getListSize(const Container& c,
}
throw std::invalid_argument("LruType not existing");
}

template <typename MMType>
void MMTypeTest<MMType>::verifyIterationVariants(Container& c) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment on this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@igchor
Copy link
Contributor Author

igchor commented Dec 14, 2022

I was reading your comment in #172 (comment). In single-tier version, without the further change, would using combined_locking for iterator cause reduction in eviction success rate and why?

There are two issues with using withEvictionIterator with current design:

  1. Current implementation of findEviction makes it pretty tricky to use withEvictionIterator - the iterator is passed from one function to another.
  2. The length of the critical section (under MMContainer lock) inside findEviction is pretty big. Combined locking makes the p99 latency very high (especially for readers).

The second issue is actually what started our efforts to optimize findEviction and shorten the Critical Section. We observed a very big performance drop in our initial multi-tier implementation (that followed the current findEviction logic). Our first attempt to shorten the Critical Section was this PR: #132 where we just wanted to mark the item as exclusive under the lock and do all other operations outside. The problem there was that the eviction success rate suffered leading us to #172 where performance is much better (for both single and multi-tier).

Lower eviction rate in PR 132 was due to higher chance of collision: after we marked the item and exited the critical section, someone else could have increased the ref count on that item causing us to fail. In the new design, this cannot happen because after item is marked, it's inaccessible by others.

@facebook-github-bot
Copy link
Contributor

@haowu14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@@ -195,6 +200,9 @@ class MMLru {
// lruInsertionPointSpec = 2, we insert at a point 1/4th from tail
uint8_t lruInsertionPointSpec{0};

// Whether to use combined locking for withEvictionIterator.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this after mmReconfigureIntervalSecs. Meta internal build failed on the initialization order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@facebook-github-bot
Copy link
Contributor

@igchor has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@haowu14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@@ -213,7 +216,8 @@ class MM2Q {
bool rebalanceOnRecordAccs,
size_t hotPercent,
size_t coldPercent,
uint32_t mmReconfigureInterval)
uint32_t mmReconfigureInterval,
bool useCombinedLockForIterators)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this optional.
The way we typically do is create a new constructor with all the parameters while keeping the constructor of Config(uint32_t time, double ratio, bool updateOnW, bool updateOnR, bool tryLockU, bool rebalanceOnRecordAccs, size_t hotPercent, size_t coldPercent, uint32_t mmReconfigureInterval)
And have it call the constructor with all the parameters with default value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done.

}

size_t i = 0;
c.withEvictionIterator([&c, &nodes, &i](auto&& iter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have to capture c here since it is not used inside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@facebook-github-bot
Copy link
Contributor

@igchor has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@haowu14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

through withEvictionIterator function.

Also, expose config option to enable and disable
combined locking.

withEvictionIterator is implemented as en extra function,
getEvictionIterator() is still there and it's behavior hasn't
changed.
@@ -456,6 +444,40 @@ class MMTinyLFU {
// Tiny and main cache iterators
ListIterator tIter_;
ListIterator mIter_;
};

class LockedIterator : public Iterator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@igchor: is this necessary? TinyLFU doesn't support combined locking. It seems like we can just rename Iterator to LockIterator and avoid introducing this new code here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, I forgot to revert these changes after realizing TinyLFU does not support combined locking. Done.

@facebook-github-bot
Copy link
Contributor

@igchor has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@haowu14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@haowu14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@haowu14 merged this pull request in d5b5bea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants