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

Fix overflow in precise_time_ns() on Windows, #22788

Merged
merged 1 commit into from
Feb 28, 2015

Conversation

vadimcn
Copy link
Contributor

@vadimcn vadimcn commented Feb 25, 2015

which starts happening after ~2 hours of machine uptime.
Closes #17845

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

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

huonw referenced this pull request Feb 25, 2015
This commit deprecates the entire libtime library in favor of the
externally-provided libtime in the rust-lang organization. Users of the
`libtime` crate as-is today should add this to their Cargo manifests:

    [dependencies.time]
    git = "https://github.com/rust-lang/time"

To implement this transition, a new function `Duration::span` was added to the
`std::time::Duration` time. This function takes a closure and then returns the
duration of time it took that closure to execute. This interface will likely
improve with `FnOnce` unboxed closures as moving in and out will be a little
easier.

Due to the deprecation of the in-tree crate, this is a:

[breaking-change]

cc #18855, some of the conversions in the `src/test/bench` area may have been a
little nicer with that implemented
@alexcrichton
Copy link
Member

I feel that this may not be the time to whip out asm!, this isn't really performance-related, right? Also, the non-x86_64 code should work on x86_64 as well?

@pnkfelix
Copy link
Member

@vadimcn you might consider taking the approach of splitting the u64 into the high- and low- order 32-bit parts, as I show here: pnkfelix@b6d2ccb

Of course the approach I show there is slower than yours, since it uses two divisions (and a modulo, but that should be folded into one of the divisions if the target architecture supports doing them together) and two multiples, in place of your one division and one multiply. But it preserves the low order bits without needing asm. (we can put in asm versions later if warranted, but I agree with @alexcrichton that we can avoid it for now.)

@pnkfelix
Copy link
Member

@vadimcn wait, sorry, @huonw has pointed out that I may have misread your code.

@vadimcn
Copy link
Contributor Author

vadimcn commented Feb 25, 2015

@pnkfelix: But it doesn't lose any bits! Had I wanted that, I'd have simply written value/denom*numer!

Now, it's true that my code will start losing low bits when (numer * denom) overflows, but that doesn't happen until we get into 20 GHz territory (10 GHz if using signed integers). These days multiprocessor machines don't seem to use RDTSC for perf counters, so the actual frequencies we are likely to see will be in MHz, not GHz.

BTW, your version seems to lose low bits too, and sooner than mine. I have not yet figured out why. Check this out: http://is.gd/uX6JH4

@pnkfelix
Copy link
Member

@vadimcn weird. I did not expect that. (Update: Ah, I am at least losing bits during (ag % f) << 32), since ag % f might take up more than 32 bits, I think.)

Anyway, I no longer object to your non-asm version.

(I still agree with @alexcrichton that we need not include the asm version right now.)

@vadimcn
Copy link
Contributor Author

vadimcn commented Feb 25, 2015

The asm version uses true 128-bit operations, so it doesn't lose low bits at all, which is nice, IMO.

@alexcrichton
Copy link
Member

True, but using unstable language features generally makes updating said features much tougher, for example. I'm also generally pretty wary of usage of asm! as it's tough to verify and I'm never quite sure if it's correct. The 32-bit version should suffice for now and we can always add back the asm! version at a later date.

@pnkfelix
Copy link
Member

@vadimcn by the way, I'm inclined to agree with your assessment on #17845 that this overflow seems like it could also hit the unix platforms as well. (I guess there's something special about windows that I only hit it there in the overflow work.)

We should definitely consider moving this portable routine that you have written out of the windows-specific code and into one of the core or std num modules, even if only for use within std alone...

@vadimcn
Copy link
Contributor Author

vadimcn commented Feb 25, 2015

@pnkfelix: I'd like to hear from Mac people that this is indeed a problem, before attempting to fix it.
From what I could find on the Internet, it appears that in recent OSX versions, 1 tick == 1 ns, so the ratio is 1/1 and thus this computation will never overflow.

which starts happening after ~2 hours of machine uptime.
@vadimcn
Copy link
Contributor Author

vadimcn commented Feb 26, 2015

@alexcrichton: Ok, I dropped the asm version.

@pnkfelix
Copy link
Member

@bors r+ 5dd001b rollup

steveklabnik added a commit to steveklabnik/rust that referenced this pull request Feb 26, 2015
…elix

which starts happening after ~2 hours of machine uptime.
Closes rust-lang#17845
Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 27, 2015
…elix

 which starts happening after ~2 hours of machine uptime.
Closes rust-lang#17845
@bors bors merged commit 5dd001b into rust-lang:master Feb 28, 2015
@vadimcn vadimcn deleted the fix-precise_time_ns branch March 5, 2015 19:59
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.

precise_time_ns can overflow on Windows
6 participants