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

Reduce number of syscalls in rand #53725

Merged
merged 3 commits into from
Sep 2, 2018
Merged

Conversation

tbu-
Copy link
Contributor

@tbu- tbu- commented Aug 26, 2018

This skips the initial zero-length getrandom call and
directly hands the user buffer to the operating system, saving one
getrandom syscall.

In case that it is statically known that the OS doesn't support
`getrandom` (non-Linux) or becomes clear at runtime that `getrandom`
isn't available (`ENOSYS`), the opened fd ("/dev/urandom") isn't closed
after the function, so that future calls can reuse it. This saves
repeated `open`/`close` system calls at the cost of one permanently open
fd.

Additionally, this skips the initial zero-length `getrandom` call and
directly hands the user buffer to the operating system, saving one
`getrandom` syscall.
@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 26, 2018
@Mark-Simulacrum
Copy link
Member

r? @alexcrichton

@tbu-
Copy link
Contributor Author

tbu- commented Aug 27, 2018

Program:

use std::collections::HashMap;

fn main() {
    let mut m = HashMap::new();
    m.insert("abc", "def");
}

Without this PR:

[…]
sigaltstack(NULL, {ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f072df95000
sigaltstack({ss_sp=0x7f072df95000, ss_flags=0, ss_size=8192}, NULL) = 0
getrandom("", 0, GRND_NONBLOCK)         = 0
getrandom("\x26\x53\x69\xb8\x75\xd4\xe3\x9a\x32\x11\x10\x58\xa7\x8a\x47\x4b", 16, GRND_NONBLOCK) = 16
sigaltstack({ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=8192}, NULL) = 0
munmap(0x7f072df95000, 8192)            = 0
exit_group(0)                           = ?
    exited with 0    

With this PR:

[…]
sigaltstack(NULL, {ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa8d3862000
sigaltstack({ss_sp=0x7fa8d3862000, ss_flags=0, ss_size=8192}, NULL) = 0
getrandom("\x05\x08\x66\x91\x2a\xf0\x05\xf9\x44\x34\x7c\x27\xf3\xfe\x06\x4c", 16, GRND_NONBLOCK) = 16
sigaltstack({ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=8192}, NULL) = 0
munmap(0x7fa8d3862000, 8192)            = 0
exit_group(0)                           = ?
    exited with 0    

The initial getrandom("", 0, GRND_NONBLOCK) = 0 gets skipped.

@alexcrichton
Copy link
Member

Thanks for the PR! The changes to getrandom seem good to me, but I'm not super comfortable with effectively leaking a permanent file descriptor. This isn't really a performance sensitive piece of code, right? (It's only used sparingly on each thread I think?) In that sense is it really necessary to get the minor boost from leaving it open rather than closing and reopening?

@tbu-
Copy link
Contributor Author

tbu- commented Aug 29, 2018

Done.

@alexcrichton
Copy link
Member

@bors: r

@bors
Copy link
Contributor

bors commented Aug 29, 2018

📌 Commit 6d47737 has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 29, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Aug 29, 2018
…richton

Reduce number of syscalls in `rand`

This skips the initial zero-length `getrandom` call and
directly hands the user buffer to the operating system, saving one
`getrandom` syscall.
@pietroalbini
Copy link
Member

@bors r-

[00:25:39] error: unused import: `libc`
[00:25:39]   --> libstd/sys/unix/rand.rs:32:9
[00:25:39]    |
[00:25:39] 32 |     use libc;
[00:25:39]    |         ^^^^
[00:25:39]    |
[00:25:39]    = note: `-D unused-imports` implied by `-D warnings`
[00:25:39] 
[00:25:39] error: unused import: `sys::os::errno`
[00:25:39]   --> libstd/sys/unix/rand.rs:33:9
[00:25:39]    |
[00:25:39] 33 |     use sys::os::errno;
[00:25:39]    |         ^^^^^^^^^^^^^^
[00:25:39] 
[00:25:43] error: aborting due to 2 previous errors

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 29, 2018
let mut read = 0;
while read < v.len() {
let result = getrandom(&mut v[read..]);
if result == -1 {
let err = errno() as libc::c_int;
if err == libc::EINTR {
continue;
} else if err == libc::ENOSYS {
GETRANDOM_UNAVAILABLE.store(true, Ordering::Relaxed);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this not loop infinitely in the ENOSYS case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh my...

@alexcrichton
Copy link
Member

@bors: r-

oh dear, good eyes @goffrie!

@alexcrichton
Copy link
Member

@bors: r

@bors
Copy link
Contributor

bors commented Sep 2, 2018

📌 Commit b95c491 has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 2, 2018
@bors
Copy link
Contributor

bors commented Sep 2, 2018

⌛ Testing commit b95c491 with merge 9395f0a...

bors added a commit that referenced this pull request Sep 2, 2018
Reduce number of syscalls in `rand`

This skips the initial zero-length `getrandom` call and
directly hands the user buffer to the operating system, saving one
`getrandom` syscall.
@bors
Copy link
Contributor

bors commented Sep 2, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing 9395f0a to master...

@bors bors merged commit b95c491 into rust-lang:master Sep 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants