File: Statistics.ML

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 (200 lines) | stat: -rw-r--r-- 9,099 bytes parent folder | download | duplicates (3)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
(*
    Title:      Poly/ML Statistics parser.
    Author:     David Matthews
    Copyright   David Matthews 2013, 2015-17

    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
*)

(*
    This is an interface to the statistics provided by the run-time system.
    For machine-independence and backwards compatibility they are encoded
    using ASN1 binary encodeing
*)

local
    open Asn1

    datatype statistic =
        UnknownStat
    |   CounterStat of { identifier: int, name: string, count: int }
    |   SizeStat of { identifier: int, name: string, size: int }
    |   TimeStat of { identifier: int, name: string, time: Time.time }
    |   UserStat of { identifier: int, name: string, count: int }

    datatype component =
        CounterValue of int
    |   ByteCount of int
    |   Time of Time.time
    |   UnknownComponent

    val emptySlice = Word8VectorSlice.full(Word8Vector.fromList [])

    fun convStats(v: Word8Vector.vector) =
    let
        fun parseStatistic p =
            case decodeItem p of
                SOME {tag = Application(0x1, Constructed), data, remainder} =>
                    (
                        case parseComponents({identifier=0, name="", value=UnknownComponent}, data) of
                            {identifier, name, value=CounterValue v} =>
                                (CounterStat{identifier=identifier, name=name, count=v}, remainder)
                        |   _ => (UnknownStat, remainder)
                    )

            |   SOME {tag = Application(0x2, Constructed), data, remainder} =>
                    (
                        case parseComponents({identifier=0, name="", value=UnknownComponent}, data) of
                            {identifier, name, value=ByteCount s} =>
                                (SizeStat{identifier=identifier, name=name, size=s}, remainder)
                        |   _ => (UnknownStat, remainder)
                    )

            |   SOME {tag = Application(0x3, Constructed), data, remainder} =>
                    (
                        case parseComponents({identifier=0, name="", value=UnknownComponent}, data) of
                            {identifier, name, value=Time t} =>
                                (TimeStat{identifier=identifier, name=name, time=t}, remainder)
                        |   _ => (UnknownStat, remainder)
                    )

            |   SOME {tag = Application(0xb, Constructed), data, remainder} =>
                    (
                        case parseComponents({identifier=0, name="", value=UnknownComponent}, data) of
                            {identifier, name, value=CounterValue c} =>
                                (UserStat{identifier=identifier, name=name, count=c}, remainder)
                        |   _ => (UnknownStat, remainder)
                    )

            |   SOME {remainder, ...} => (UnknownStat, remainder)

            |   NONE => (UnknownStat, emptySlice)


        and parseComponents(result as {identifier, name, value }, p) =
            if Word8VectorSlice.length p = 0
            then result
            else
            (
                case decodeItem p of
                    SOME {tag = Application(0x4, Primitive), data, remainder} =>
                        parseComponents({identifier=decodeInt data, name=name, value=value}, remainder)
                |   SOME {tag = Application(0x5, Primitive), data, remainder} =>
                        parseComponents({name=decodeString data, identifier=identifier, value=value}, remainder)
                |   SOME {tag = Application(0x6, Primitive), data, remainder} =>
                        parseComponents({identifier=identifier, name=name, value=CounterValue(decodeInt data)}, remainder)
                |   SOME {tag = Application(0x7, Primitive), data, remainder} =>
                        parseComponents({identifier=identifier, name=name, value=ByteCount(decodeInt data)}, remainder)
                |   SOME {tag = Application(0x8, Constructed), data, remainder} =>
                        let
                            fun parseTime (t, p) =
                                if Word8VectorSlice.length p = 0
                                then t
                                else
                                (
                                    case decodeItem p of
                                        SOME {tag = Application(0x9, Primitive), data, remainder} =>
                                            parseTime(t + Time.fromSeconds(LargeInt.fromInt(decodeInt data)), remainder)
                                    |   SOME {tag = Application(0xa, Primitive), data, remainder} =>
                                            parseTime(t + Time.fromMicroseconds(LargeInt.fromInt(decodeInt data)), remainder)
                                    |   SOME {remainder, ...} => parseTime(t, remainder) (* Unknown *)
                                    |   NONE => t
                                )
                        in
                            parseComponents({identifier=identifier, name=name,
                                    value=Time(parseTime(Time.zeroTime, data))}, remainder)
                        end
                |   SOME {remainder, ...} => parseComponents(result, remainder)

                |   NONE => result
            )

        fun parseStatistics l =
            if Word8VectorSlice.length l = 0
            then []
            else
            let
                val (item, rest) = parseStatistic l
                val items = parseStatistics rest
            in
                item :: items
            end

        val stats =
            case decodeItem (Word8VectorSlice.full v) of
                SOME {tag = Application(0x0, Constructed), data, ...} => parseStatistics data
            |   _ => raise Fail "Statistics not available"
        
        fun extractCounter(n, l) =
            case List.find (fn CounterStat{identifier, ...} => identifier = n | _ => false) l of
                SOME(CounterStat{ count, ...}) => count
            |   _ => 0
        and extractSize(n, l) =
            case List.find (fn SizeStat{identifier, ...} => identifier = n | _ => false) l of
                SOME(SizeStat{ size, ...}) => size
            |   _ => 0
        and extractTime(n, l) =
            case List.find (fn TimeStat{identifier, ...} => identifier = n | _ => false) l of
                SOME(TimeStat{ time, ...}) => time
            |   _ => Time.zeroTime
        and extractUser(n, l) =
            case List.find (fn UserStat{identifier, ...} => identifier = n | _ => false) l of
                SOME(UserStat{ count, ...}) => count
            |   _ => 0
    in
        {
            threadsTotal = extractCounter(1, stats),
            threadsInML = extractCounter(2, stats),
            threadsWaitIO = extractCounter(3, stats),
            threadsWaitMutex = extractCounter(4, stats),
            threadsWaitCondVar = extractCounter(5, stats),
            threadsWaitSignal = extractCounter(6, stats),
            gcFullGCs = extractCounter(7, stats),
            gcPartialGCs = extractCounter(8, stats),
            sizeHeap = extractSize(9, stats),
            sizeHeapFreeLastGC = extractSize(10, stats),
            sizeHeapFreeLastFullGC = extractSize(11, stats),
            sizeAllocation = extractSize(12, stats),
            sizeAllocationFree = extractSize(13, stats),
            timeNonGCUser = extractTime(14, stats),
            timeNonGCSystem = extractTime(15, stats),
            timeGCUser = extractTime(16, stats),
            timeGCSystem = extractTime(17, stats),
            userCounters = Vector.tabulate(8, fn n => extractUser(n+18, stats))
        }
    end

    open RunCall
    
    local
        val polySpecificGeneralCall = RunCall.rtsCallFull2 "PolySpecificGeneral"
    in
        fun polySpecificGeneral(code: int, arg:'a):'b = RunCall.unsafeCast(polySpecificGeneralCall(RunCall.unsafeCast(code, arg)))
    end

in
    structure PolyML =
    struct
        open PolyML
        structure Statistics =
        struct
            fun getLocalStats() =
                convStats(polySpecificGeneral (29, ()))
            and getRemoteStats(pid: int) =
                convStats(polySpecificGeneral (30, pid))
            and numUserCounters(): int = polySpecificGeneral (27, ())
            and setUserCounter(which: int, value: int): unit = polySpecificGeneral (28, (which, value))
        end
    end
end;