Skip to content

Commit 4871f11

Browse files
committed
std: change timeval to ns resolution timespec
This lets us use the more precise clock_gettime on posix machines.
1 parent 7aae732 commit 4871f11

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

Diff for: src/libstd/time.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
#[abi = "cdecl"]
22
native mod rustrt {
3-
fn get_time(&sec: i64, &usec: i32);
3+
fn get_time(&sec: i64, &nsec: i32);
44
fn precise_time_ns(&ns: u64);
55
}
66

77
#[doc = "A record specifying a time value in seconds and microseconds."]
8-
type timeval = {sec: i64, usec: i32};
8+
type timespec = {sec: i64, nsec: i32};
99

1010
#[doc = "
11-
Returns the current time as a `timeval` containing the seconds and
11+
Returns the current time as a `timespec` containing the seconds and
1212
microseconds since 1970-01-01T00:00:00Z.
1313
"]
14-
fn get_time() -> timeval {
14+
fn get_time() -> timespec {
1515
let mut sec = 0i64;
16-
let mut usec = 0i32;
17-
rustrt::get_time(sec, usec);
18-
ret {sec: sec, usec: usec};
16+
let mut nsec = 0i32;
17+
rustrt::get_time(sec, nsec);
18+
ret {sec: sec, nsec: nsec};
1919
}
2020

2121
#[doc = "
@@ -47,20 +47,20 @@ mod tests {
4747

4848
let tv1 = get_time();
4949
log(debug, "tv1=" + uint::str(tv1.sec as uint) + " sec + "
50-
+ uint::str(tv1.usec as uint) + " usec");
50+
+ uint::str(tv1.nsec as uint) + " nsec");
5151

5252
assert tv1.sec > some_recent_date;
53-
assert tv1.usec < 1000000i32;
53+
assert tv1.nsec < 1000000000i32;
5454

5555
let tv2 = get_time();
5656
log(debug, "tv2=" + uint::str(tv2.sec as uint) + " sec + "
57-
+ uint::str(tv2.usec as uint) + " usec");
57+
+ uint::str(tv2.nsec as uint) + " nsec");
5858

5959
assert tv2.sec >= tv1.sec;
6060
assert tv2.sec < some_future_date;
61-
assert tv2.usec < 1000000i32;
61+
assert tv2.nsec < 1000000000i32;
6262
if tv2.sec == tv1.sec {
63-
assert tv2.usec >= tv1.usec;
63+
assert tv2.nsec >= tv1.nsec;
6464
}
6565
}
6666

Diff for: src/rt/rust_builtin.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ rust_ptr_eq(type_desc *t, rust_box *a, rust_box *b) {
408408

409409
#if defined(__WIN32__)
410410
extern "C" CDECL void
411-
get_time(int64_t *sec, int32_t *usec) {
411+
get_time(int64_t *sec, int32_t *nsec) {
412412
FILETIME fileTime;
413413
GetSystemTimeAsFileTime(&fileTime);
414414

@@ -423,15 +423,22 @@ get_time(int64_t *sec, int32_t *usec) {
423423
const uint64_t NANOSECONDS_FROM_1601_TO_1970 = 11644473600000000u;
424424
uint64_t ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
425425
*sec = ns_since_1970 / 1000000;
426-
*usec = ns_since_1970 % 1000000;
426+
*nsec = (ns_since_1970 % 1000000) * 1000;
427427
}
428428
#else
429429
extern "C" CDECL void
430-
get_time(int64_t *sec, int32_t *usec) {
430+
get_time(int64_t *sec, int32_t *nsec) {
431+
#ifdef __APPLE__
431432
struct timeval tv;
432433
gettimeofday(&tv, NULL);
433434
*sec = tv.tv_sec;
434-
*usec = tv.tv_usec;
435+
*nsec = tv.tv_usec * 1000;
436+
#else
437+
timespec ts;
438+
clock_gettime(CLOCK_REALTIME, &ts);
439+
*sec = ts.tv_sec;
440+
*nsec = ts.tv_nsec;
441+
#endif
435442
}
436443
#endif
437444

Diff for: src/rustc/middle/trans/base.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ fn get_dest_addr(dest: dest) -> ValueRef {
155155
}
156156
}
157157

158-
fn log_fn_time(ccx: @crate_ctxt, name: str, start: time::timeval,
159-
end: time::timeval) {
158+
fn log_fn_time(ccx: @crate_ctxt, name: str, start: time::timespec,
159+
end: time::timespec) {
160160
let elapsed = 1000 * ((end.sec - start.sec) as int) +
161-
((end.usec as int) - (start.usec as int)) / 1000;
161+
((end.nsec as int) - (start.nsec as int)) / 1000000;
162162
*ccx.stats.fn_times += [{ident: name, time: elapsed}];
163163
}
164164

@@ -4056,7 +4056,7 @@ fn trans_fn(ccx: @crate_ctxt,
40564056
id: ast::node_id) {
40574057
let do_time = ccx.sess.opts.stats;
40584058
let start = if do_time { time::get_time() }
4059-
else { {sec: 0i64, usec: 0i32} };
4059+
else { {sec: 0i64, nsec: 0i32} };
40604060
let _icx = ccx.insn_ctxt("trans_fn");
40614061
trans_closure(ccx, path, decl, body, llfndecl, ty_self,
40624062
param_substs, id, {|fcx|

0 commit comments

Comments
 (0)