Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/timers/darwin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ struct RUsage
ru_nivcsw::Clong
end

@inline function maketime(ru::RUsage)
user = ru.ru_utime.tv_sec * 1e9 + ru.ru_utime.tv_usec *1e3
kernel = ru.ru_stime.tv_sec * 1e9 + ru.ru_stime.tv_usec *1e3
@inline function maketime(utime::TimeVal, stime::TimeVal)
user = utime.tv_sec * 1e9 + utime.tv_usec *1e3
kernel = stime.tv_sec * 1e9 + stime.tv_usec *1e3
return user+kernel
end

Expand All @@ -48,5 +48,31 @@ end
@inline function cputime()
ru = Ref{RUsage}()
ccall(:getRUsage, Cint, (Cint, Ref{RUsage}), RUSAGE_SELF, ru)
return maketime(ru[])
return maketime(ru[].ru_utime, ru[].ru_stime)
end

struct Measurement
realtime::UInt64
uime::TimeVal
stime::TimeVal
function Measurement()
rtime = time_ns()
ru = Ref{RUsage}()
ccall(:getRUsage, Cint, (Cint, Ref{RUsage}), RUSAGE_SELF, ru)
return new(rtime, ru[].ru_utime, ru[].ru_stime)
end
end

struct MeasurementDelta
realtime::Float64
cpuratio::Float64
function MeasurementDelta(t1::Measurement, t0::Measurement)
rt0 = Float64(t0.realtime)
ct0 = maketime(t0.utime, t0.stime)
rt1 = Float64(t1.realtime)
ct1 = maketime(t1.utime, t1.stime)
realtime = rt1 - rt0
cputime = ct1 - ct0
return new(realtime, cputime/realtime)
end
end
17 changes: 17 additions & 0 deletions src/timers/timers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
module Timers
import Compat

###
# For each supported operating system, we define a struct:
# struct Measurement end
# -(a::Measurement, b::Measurement) -> MeasurmentDelta
#
# struct MeasurmentDelta end
# isless
# time
# cpuratio
###

const ACCURATE_CPUTIME = Compat.Sys.iswindows() ? haskey(ENV, "BT_FORCE_CPUTIME") : true

"""
Expand Down Expand Up @@ -36,4 +47,10 @@ elseif Compat.Sys.iswindows()
else
error("$(Sys.KERNEL) is not supported please file an issue")
end

Base.isless(a::MeasurementDelta, b::MeasurementDelta) = isless(time(a), time(b))
Base.:(-)(t1::Measurement, t0::Measurement) = MeasurementDelta(t1, t0)
time(a::MeasurementDelta) = a.realtime
cpuratio(a::MeasurementDelta) = a.cpuratio

end # module
24 changes: 24 additions & 0 deletions src/timers/unix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,27 @@ end
@inline function cputime()
maketime(clock_gettime(CLOCK_PROCESS_CPUTIME_ID))
end

struct Measurement
realtime::TimeSpec
cputime::TimeSpec
function Measurement()
rtime = clock_gettime(CLOCK_MONOTONIC)
ctime = clock_gettime(CLOCK_PROCESS_CPUTIME_ID)
return new(rtime, ctime)
end
end

struct MeasurementDelta
realtime::Float64
cpuratio::Float64
function MeasurementDelta(t1::Measurement, t0::Measurement)
rt0 = maketime(t0.realtime)
ct0 = maketime(t0.cputime)
rt1 = maketime(t1.realtime)
ct1 = maketime(t1.cputime)
realtime = rt1 - rt0
cputime = ct1 - ct0
return new(realtime, cputime/realtime)
end
end
49 changes: 49 additions & 0 deletions src/timers/windows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,52 @@ end
proc, creation_time, exit_time, kernel_time, user_time)
return maketime(kernel_time[], user_time[])
end

@inline function frequency()
freq = Ref{UInt64}()
ccall(:QueryPerformanceFrequency, Cint, (Ref{UInt64},), freq)
return freq[]
end

@inline function currentprocess()
return ccall(:GetCurrentProcess, HANDLE, ())
end

@inline function cpucycles(proc::HANDLE)
cycles = Ref{UInt64}()
ccall(:QueryProcessCycleTime, Cint, (HANDLE, Ref{UInt64}), proc, cycles)
return cycles[]
end

@inline function perfcounter()
counter = Ref{UInt64}()
ccall(:QueryPerformanceCounter, Cint, (Ref{UInt64},), counter)
return counter[]
end

struct Measurement
time::UInt64
cpu::UInt64
function Measurement()
proc = currentprocess()
time = perfcounter()
cpu = cpucycles()
Copy link
Member Author

Choose a reason for hiding this comment

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

cpucyles(proc)

return new(time, cnu)
end
end

struct MeasurementDelta
realtime::Float64
cpuratio::Float64
function MeasurementDelta(t1::Measurement, t0::Measurement)
freq = frequency()
rt0 = t0.time
ct0 = t0.cpu
rt1 = t1.time
ct1 = t1.cpu
realcycles = rt1 - rt0
realtime = realcycles / freq
cpucycles = ct1 - ct0
new(realtime, cpucycles/realcycles)
end
end