std: change timeval to ns resolution timespec

This lets us use the more precise clock_gettime on posix
machines.
This commit is contained in:
Erick Tryzelaar 2012-04-02 21:41:24 -07:00
parent 7aae7320db
commit 4871f11439
3 changed files with 27 additions and 20 deletions

View File

@ -1,21 +1,21 @@
#[abi = "cdecl"]
native mod rustrt {
fn get_time(&sec: i64, &usec: i32);
fn get_time(&sec: i64, &nsec: i32);
fn precise_time_ns(&ns: u64);
}
#[doc = "A record specifying a time value in seconds and microseconds."]
type timeval = {sec: i64, usec: i32};
type timespec = {sec: i64, nsec: i32};
#[doc = "
Returns the current time as a `timeval` containing the seconds and
Returns the current time as a `timespec` containing the seconds and
microseconds since 1970-01-01T00:00:00Z.
"]
fn get_time() -> timeval {
fn get_time() -> timespec {
let mut sec = 0i64;
let mut usec = 0i32;
rustrt::get_time(sec, usec);
ret {sec: sec, usec: usec};
let mut nsec = 0i32;
rustrt::get_time(sec, nsec);
ret {sec: sec, nsec: nsec};
}
#[doc = "
@ -47,20 +47,20 @@ mod tests {
let tv1 = get_time();
log(debug, "tv1=" + uint::str(tv1.sec as uint) + " sec + "
+ uint::str(tv1.usec as uint) + " usec");
+ uint::str(tv1.nsec as uint) + " nsec");
assert tv1.sec > some_recent_date;
assert tv1.usec < 1000000i32;
assert tv1.nsec < 1000000000i32;
let tv2 = get_time();
log(debug, "tv2=" + uint::str(tv2.sec as uint) + " sec + "
+ uint::str(tv2.usec as uint) + " usec");
+ uint::str(tv2.nsec as uint) + " nsec");
assert tv2.sec >= tv1.sec;
assert tv2.sec < some_future_date;
assert tv2.usec < 1000000i32;
assert tv2.nsec < 1000000000i32;
if tv2.sec == tv1.sec {
assert tv2.usec >= tv1.usec;
assert tv2.nsec >= tv1.nsec;
}
}

View File

@ -408,7 +408,7 @@ rust_ptr_eq(type_desc *t, rust_box *a, rust_box *b) {
#if defined(__WIN32__)
extern "C" CDECL void
get_time(int64_t *sec, int32_t *usec) {
get_time(int64_t *sec, int32_t *nsec) {
FILETIME fileTime;
GetSystemTimeAsFileTime(&fileTime);
@ -423,15 +423,22 @@ get_time(int64_t *sec, int32_t *usec) {
const uint64_t NANOSECONDS_FROM_1601_TO_1970 = 11644473600000000u;
uint64_t ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
*sec = ns_since_1970 / 1000000;
*usec = ns_since_1970 % 1000000;
*nsec = (ns_since_1970 % 1000000) * 1000;
}
#else
extern "C" CDECL void
get_time(int64_t *sec, int32_t *usec) {
get_time(int64_t *sec, int32_t *nsec) {
#ifdef __APPLE__
struct timeval tv;
gettimeofday(&tv, NULL);
*sec = tv.tv_sec;
*usec = tv.tv_usec;
*nsec = tv.tv_usec * 1000;
#else
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
*sec = ts.tv_sec;
*nsec = ts.tv_nsec;
#endif
}
#endif

View File

@ -155,10 +155,10 @@ fn get_dest_addr(dest: dest) -> ValueRef {
}
}
fn log_fn_time(ccx: @crate_ctxt, name: str, start: time::timeval,
end: time::timeval) {
fn log_fn_time(ccx: @crate_ctxt, name: str, start: time::timespec,
end: time::timespec) {
let elapsed = 1000 * ((end.sec - start.sec) as int) +
((end.usec as int) - (start.usec as int)) / 1000;
((end.nsec as int) - (start.nsec as int)) / 1000000;
*ccx.stats.fn_times += [{ident: name, time: elapsed}];
}
@ -4056,7 +4056,7 @@ fn trans_fn(ccx: @crate_ctxt,
id: ast::node_id) {
let do_time = ccx.sess.opts.stats;
let start = if do_time { time::get_time() }
else { {sec: 0i64, usec: 0i32} };
else { {sec: 0i64, nsec: 0i32} };
let _icx = ccx.insn_ctxt("trans_fn");
trans_closure(ccx, path, decl, body, llfndecl, ty_self,
param_substs, id, {|fcx|