File: Timer.sml

package info (click to toggle)
polyml 5.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 40,616 kB
  • sloc: cpp: 44,142; ansic: 26,963; sh: 22,002; asm: 13,486; makefile: 602; exp: 525; python: 253; awk: 91
file content (95 lines) | stat: -rw-r--r-- 3,496 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(*
    Title:      Standard Basis Library: Timer Signature and structure.
    Author:     David Matthews
    Copyright   David Matthews 2000, 2005, 2008, 2017

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License version 2.1 as published by the Free Software Foundation.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*)

(* G&R 2004 status: updated.  Added checkCPUTimes. *)

signature TIMER =
sig
    type cpu_timer
    type real_timer
    val startCPUTimer : unit -> cpu_timer
    val checkCPUTimer : cpu_timer -> {usr : Time.time, sys : Time.time}
    val checkGCTime : cpu_timer -> Time.time
    val totalCPUTimer : unit -> cpu_timer
    val startRealTimer : unit -> real_timer
    val checkRealTimer : real_timer -> Time.time
    val totalRealTimer : unit -> real_timer
    
    val checkCPUTimes : cpu_timer ->
        {
            nongc: { usr : Time.time, sys : Time.time},
            gc: { usr : Time.time, sys : Time.time}
        }
end

structure Timer :> TIMER =
struct
    type cpu_timer = {userTime: Time.time, sysTime: Time.time, gcUTime: Time.time, gcSTime: Time.time }
    type real_timer = Time.time

    local
        open Time

        local
            val timingGeneralCall = RunCall.rtsCallFull2 "PolyTimingGeneral"
        in
            fun doCall(code: int, arg:'a):'b =
                RunCall.unsafeCast(timingGeneralCall(RunCall.unsafeCast(code, arg)))
        end
        fun getUserTime() = doCall(7, ())
        and getSysTime() = doCall(8, ())
        and getGCUTime() = doCall(9, ())
        and getGCSTime() = doCall(13, ())
    in
        fun startCPUTimer () =
            {userTime=getUserTime(),
             sysTime=getSysTime(),
             gcUTime=getGCUTime(),
             gcSTime=getGCSTime() }
        and checkCPUTimer ({ userTime, sysTime, ...}: cpu_timer) =
            { usr = getUserTime() - userTime, sys = getSysTime() - sysTime}
        and checkGCTime ({ gcUTime, ...}: cpu_timer) = getGCUTime() - gcUTime
        and totalCPUTimer () =
            { userTime=Time.zeroTime, sysTime=Time.zeroTime, gcUTime=Time.zeroTime, gcSTime=Time.zeroTime }

        fun checkCPUTimes (timer as { gcUTime, gcSTime, ... }) =
            let
                val { usr, sys } = checkCPUTimer timer
                val gc_usr = getGCUTime() - gcUTime and gc_sys = getGCSTime() - gcSTime 
            in
                { gc = { usr = gc_usr, sys = gc_sys },
                  nongc = { usr = usr-gc_usr, sys = sys-gc_sys } }
            end

        fun totalRealTimer() = Time.zeroTime
        and startRealTimer() = doCall(10, ())
        and checkRealTimer t = startRealTimer() - t
    end

end;

(* Override the default printer so they're abstract. *)
local
    fun prettyCPUTimer _ _ (_: Timer.cpu_timer) = PolyML.PrettyString "?"
    and prettyRealTimer _ _ (_: Timer.real_timer) = PolyML.PrettyString "?"
in
    val () = PolyML.addPrettyPrinter prettyCPUTimer
    and () = PolyML.addPrettyPrinter prettyRealTimer
end;