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
Avoid taking a write lock in RAND_get_rand_method() #20929
Conversation
The function RAND_get_rand_method() is called every time RAND_bytes() or RAND_priv_bytes() is called. We were obtaining a write lock in order to find the default random method - even though we rarely write. We change this to a read lock and only fallback to a write lock if we need to. Partial fix for openssl#20286
This pull request is ready to merge |
I have run some performance tests for this using the This basically runs The numbers are timings in us for a set of 100
There are some anomalies in here. The timings for 100 threads in 3.1 and master seem unexpectedly high. Having master with just this PR shows an improvement but still not to 1.1.1 levels. Having master and #20970 also show slightly better and marked improvement. Having both this PR and #20970 seems to have a dramatic improvement (better than 1.1.1 even). |
I have no idea how 10 concurrent threads can give better timing than 1. Could you please explain? |
If you have more CPU cores they definitely can. |
The timings are calculated based on the total time taken divided by the number of RAND_bytes() calls done. Each thread adds a fixed number of RAND_bytes() calls to be done. Based on the number of cores in the machine with 10 threads you can get more RAND_bytes() calls done completely in parallel to each other - so the total time doesn't go up by much, but the number of RAND_bytes() calls made goes up. Once you exceed the core count in the machine that effect goes away. At least that is my guess as to what is happening. |
OTC: as there is little benefit without #20970, master only. |
Pushed. Thanks. |
The function RAND_get_rand_method() is called every time RAND_bytes() or RAND_priv_bytes() is called. We were obtaining a write lock in order to find the default random method - even though we rarely write. We change this to a read lock and only fallback to a write lock if we need to. Partial fix for #20286 Reviewed-by: Hugo Landau <hlandau@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from #20929)
The function RAND_get_rand_method() is called every time RAND_bytes() or RAND_priv_bytes() is called. We were obtaining a write lock in order to find the default random method - even though we rarely write. We change this to a read lock and only fallback to a write lock if we need to. Partial fix for #20286 Reviewed-by: Hugo Landau <hlandau@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from openssl/openssl#20929)
The function RAND_get_rand_method() is called every time RAND_bytes() or RAND_priv_bytes() is called. We were obtaining a write lock in order to find the default random method - even though we rarely write. We change this to a read lock and only fallback to a write lock if we need to.
Partial fix for #20286