From ddb5a8e6e440c016e4472bf16e4e694827d68729 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 5 May 2020 17:19:25 +0300 Subject: [PATCH] time: add specialized vpc_now_darwin to fix -os cross --- vlib/time/time_darwin.c.v | 11 +++++++++++ vlib/time/time_nix.c.v | 14 +++----------- vlib/v/gen/profile.v | 7 ++++--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/vlib/time/time_darwin.c.v b/vlib/time/time_darwin.c.v index 11436cc186..a1e5c5b805 100644 --- a/vlib/time/time_darwin.c.v +++ b/vlib/time/time_darwin.c.v @@ -36,3 +36,14 @@ fn sys_mono_now_darwin() u64 { } return (tm - start_time) * time_base.numer / time_base.denom } + +// NB: vpc_now_darwin is used by `v -profile` . +// It should NOT call *any other v function*, just C functions and casts. +[inline] +fn vpc_now_darwin() u64 { + tm := C.mach_absolute_time() + if time_base.denom == 0 { + C.mach_timebase_info(&time_base) + } + return (tm - start_time) * time_base.numer / time_base.denom +} diff --git a/vlib/time/time_nix.c.v b/vlib/time/time_nix.c.v index 8d81ba568c..33b8bbfe90 100644 --- a/vlib/time/time_nix.c.v +++ b/vlib/time/time_nix.c.v @@ -43,15 +43,7 @@ fn sys_mono_now() u64 { // It should NOT call *any other v function*, just C functions and casts. [inline] fn vpc_now() u64 { - $if macos { - tm := C.mach_absolute_time() - if time_base.denom == 0 { - C.mach_timebase_info(&time_base) - } - return (tm - start_time) * time_base.numer / time_base.denom - } $else { - ts := C.timespec{} - C.clock_gettime(C.CLOCK_MONOTONIC, &ts) - return u64(ts.tv_sec) * 1_000_000_000 + u64(ts.tv_nsec) - } + ts := C.timespec{} + C.clock_gettime(C.CLOCK_MONOTONIC, &ts) + return u64(ts.tv_sec) * 1_000_000_000 + u64(ts.tv_nsec) } diff --git a/vlib/v/gen/profile.v b/vlib/v/gen/profile.v index a0f22192a1..35a5374a74 100644 --- a/vlib/v/gen/profile.v +++ b/vlib/v/gen/profile.v @@ -12,15 +12,16 @@ fn (mut g Gen) profile_fn(fn_name string, is_main bool){ g.writeln('\tatexit(vprint_profile_stats);') g.writeln('') } - if fn_name == 'time.vpc_now' { + if fn_name.starts_with('time.vpc_now') { g.defer_profile_code = '' } else { + measure_fn_name := if g.pref.os == .mac { 'time__vpc_now_darwin' } else { 'time__vpc_now' } fn_profile_counter_name := 'vpc_${g.last_fn_c_name}' fn_profile_counter_name_calls := '${fn_profile_counter_name}_calls' g.writeln('') - g.writeln('\tdouble _PROF_FN_START = time__vpc_now(); ${fn_profile_counter_name_calls}++; // $fn_name') + g.writeln('\tdouble _PROF_FN_START = ${measure_fn_name}(); ${fn_profile_counter_name_calls}++; // $fn_name') g.writeln('') - g.defer_profile_code = '\t${fn_profile_counter_name} += time__vpc_now() - _PROF_FN_START;' + g.defer_profile_code = '\t${fn_profile_counter_name} += ${measure_fn_name}() - _PROF_FN_START;' g.pcs_declarations.writeln('double ${fn_profile_counter_name} = 0.0; u64 ${fn_profile_counter_name_calls} = 0;') g.pcs << ProfileCounterMeta{ g.last_fn_c_name, fn_profile_counter_name, fn_profile_counter_name_calls } }