| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Measuring time |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --guardedness #-} |
| 8 |
|
| 9 |
module System.Clock where |
| 10 |
|
| 11 |
open import Level using (Level; 0ℓ; Lift; lower) |
| 12 |
open import Data.Bool.Base using (if_then_else_) |
| 13 |
open import Data.Fin.Base using (Fin; toℕ) |
| 14 |
open import Data.Nat.Base as ℕ using (ℕ; zero; suc; _+_; _∸_; _^_; _<ᵇ_) |
| 15 |
import Data.Nat.Show as ℕ using (show) |
| 16 |
open import Data.Nat.DivMod using (_/_) |
| 17 |
import Data.Nat.Properties as ℕ |
| 18 |
open import Data.String.Base using (String; _++_; padLeft) |
| 19 |
|
| 20 |
open import IO.Base |
| 21 |
open import Function.Base using (_$_; _∘′_) |
| 22 |
open import Foreign.Haskell using (_,_) |
| 23 |
|
| 24 |
open import Relation.Nullary.Decidable using (False; fromWitnessFalse; toWitnessFalse) |
| 25 |
|
| 26 |
private |
| 27 |
variable |
| 28 |
a : Level |
| 29 |
A : Set a |
| 30 |
|
| 31 |
------------------------------------------------------------------------ |
| 32 |
-- Re-exporting the Clock data structure |
| 33 |
|
| 34 |
open import System.Clock.Primitive as Prim |
| 35 |
public |
| 36 |
using ( Clock |
| 37 |
-- System-wide monotonic time since an arbitrary point in the past |
| 38 |
; monotonic |
| 39 |
-- System-wide real time since the Epoch |
| 40 |
; realTime |
| 41 |
-- Amount of execution time of the current process |
| 42 |
; processCPUTime |
| 43 |
-- Amount of execution time of the current OS thread |
| 44 |
; threadCPUTime |
| 45 |
-- A raw hardware version of Monotonic ignoring adjustments |
| 46 |
; monotonicRaw |
| 47 |
-- Linux-specific clocks |
| 48 |
-- Similar to Monotonic, includes time spent suspended |
| 49 |
; bootTime |
| 50 |
-- Faster but less precise alternative to Monotonic |
| 51 |
; monotonicCoarse |
| 52 |
-- Faster but less precise alternative to RealTime |
| 53 |
; realTimeCoarse |
| 54 |
) |
| 55 |
|
| 56 |
------------------------------------------------------------------------ |
| 57 |
-- Defining a more convenient representation |
| 58 |
|
| 59 |
record Time : Set where |
| 60 |
constructor mkTime |
| 61 |
field seconds : ℕ |
| 62 |
nanoseconds : ℕ |
| 63 |
open Time public |
| 64 |
|
| 65 |
------------------------------------------------------------------------ |
| 66 |
-- Reading the clock |
| 67 |
|
| 68 |
getTime : Clock → IO Time |
| 69 |
getTime c = do |
| 70 |
(a , b) ← lift (Prim.getTime c) |
| 71 |
pure $ mkTime a b |
| 72 |
|
| 73 |
------------------------------------------------------------------------ |
| 74 |
-- Measuring time periods |
| 75 |
|
| 76 |
diff : Time → Time → Time |
| 77 |
diff (mkTime ss sns) (mkTime es ens) = |
| 78 |
if ens <ᵇ sns |
| 79 |
then mkTime (es ∸ suc ss) ((1000000000 + ens) ∸ sns) |
| 80 |
else mkTime (es ∸ ss) (ens ∸ sns) |
| 81 |
|
| 82 |
record Timed (A : Set a) : Set a where |
| 83 |
constructor mkTimed |
| 84 |
field value : A |
| 85 |
time : Time |
| 86 |
|
| 87 |
time : IO A → IO (Timed A) |
| 88 |
time io = do |
| 89 |
start ← lift! $ getTime realTime |
| 90 |
a ← io |
| 91 |
end ← lift! $ getTime realTime |
| 92 |
pure $ mkTimed a $ diff (lower start) (lower end) |
| 93 |
|
| 94 |
time′ : IO {0ℓ} A → IO Time |
| 95 |
time′ io = Timed.time <$> time io |
| 96 |
|
| 97 |
------------------------------------------------------------------------ |
| 98 |
-- Showing time |
| 99 |
|
| 100 |
show : Time → -- Time in seconds and nanoseconds |
| 101 |
Fin 10 → -- Number of decimals to show |
| 102 |
-- (in [0,9] because we are using nanoseconds) |
| 103 |
String |
| 104 |
show (mkTime s ns) prec = secs ++ "s" ++ padLeft '0' decimals nsecs where |
| 105 |
decimals = toℕ prec |
| 106 |
secs = ℕ.show s |
| 107 |
prf = ℕ.m^n≢0 10 (9 ∸ decimals) |
| 108 |
nsecs = ℕ.show ((ns / (10 ^ (9 ∸ decimals))) {{prf}}) |