File: Timer.sml

package info (click to toggle)
polyml 5.6-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 31,892 kB
  • ctags: 34,453
  • sloc: cpp: 44,983; ansic: 24,520; asm: 14,850; sh: 11,730; makefile: 551; exp: 484; python: 253; awk: 91; sed: 9
file content (91 lines) | stat: -rw-r--r-- 3,422 bytes parent folder | download
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
(*
    Title:      Standard Basis Library: Timer Signature and structure.
    Author:     David Matthews
    Copyright   David Matthews 2000, 2005, 2008

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    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
        val doCall: int*unit -> Time.time
             = RunCall.run_call2 RuntimeCalls.POLY_SYS_timing_dispatch
        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;