Skip to content

calculate appropriate level 0 chance#35

Open
sticnarf wants to merge 3 commits intotikv:masterfrom
sticnarf:calculate-chance
Open

calculate appropriate level 0 chance#35
sticnarf wants to merge 3 commits intotikv:masterfrom
sticnarf:calculate-chance

Conversation

@sticnarf
Copy link
Contributor

Problem

Currently the maximum level 0 chance is 0.98. Sometimes we still fail to achieve our target even with this maximum value. For example, if we have plenty of tasks of all levels to run and the average time spent in handling a level 0 task once is 50us while the average time spent in handling other tasks once is 1500us. We can calculate the final time proportion of level 0 is (50 * 0.98) / (50 * 0.98 + 1500 * 0.02) = 0.62, which is far lower than we expected.

Solution

Firstly, this PR raises the maximum level 0 chance to 0.9999, which should suffice in most cases. And the way we adjust the chance is improved. The new chance is now calculated based on the assumption that we have plenty of tasks in all levels. (It is OK when the assumption is false because then we actually don't need to balance tasks of different levels.

Formula

Say the average time spent in handling level 1 and 2 tasks once is k times of that for level 0 task, the current time proportion of level 0 tasks is m, the target proportion is m0 and the chance of level 0 tasks is p. We want to get an appropriate new chance of level 0 tasks, p'.

So, we have simultaneous equations below:

p/(p+k(1-p)) = m
p'/(p'+k(1-p')) = m0

Solving the equations we can get p' = m0(1-m)p/(m0(1-m0)-(m-m0)(p-(1-m0))).

In our implementation, we ensure 0 < p < 1, 0 <= m <= 1, 0 < m0 < 1. The denominator can be proved to be greater than zero so we needn't check it in our code.

@sticnarf sticnarf requested a review from BusyJay April 15, 2020 06:23
Signed-off-by: Yilin Chen <sticnarf@gmail.com>
@BusyJay
Copy link
Member

BusyJay commented Apr 20, 2020

the denominator can be proved to be greater than zero

How?

@sticnarf
Copy link
Contributor Author

sticnarf commented Jun 10, 2020

the denominator can be proved to be greater than zero

How?

if
image

then
image

Because m and m_0p are all > 0, p-(1-m0) > 0.
So we have
image

And also, we know m <= 1, then,

image

Multiply the denominator in both side, we get
image

But both (1-m0) and (1-p) > 0, so we get a contradiction.
QED

BusyJay
BusyJay previously approved these changes Jun 10, 2020
Copy link
Member

@BusyJay BusyJay left a comment

Choose a reason for hiding this comment

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

Cool.

Signed-off-by: Yilin Chen <sticnarf@gmail.com>
Signed-off-by: Yilin Chen <sticnarf@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants