1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Program to transform `memory.log` files produced by `nitc --trace-memory` into a csv file
21 # An aggregated time-slice
23 # The start time of the slice
26 # Number of allocations
27 var acpt
= new Counter[String]
29 # Number of allocated bytes
30 var asiz
= new Counter[String]
32 # Number of deallocations
33 var dcpt
= new Counter[String]
35 # Number of deallocated bytes
36 var dsiz
= new Counter[String]
38 # Total number of allocations since the beginning
39 var cpttot
= new Counter[String]
41 # Total number of allocated bytes since the beginning
42 var siztot
= new Counter[String]
44 # Map of all the various counters.
45 var fields
= new Map[String, Counter[String]]
52 fields
["cpttot"] = cpttot
53 fields
["siztot"] = siztot
57 # Main class that does the job
59 # The `memory.log` file.
62 # The delay of an aggregation
65 # Total number of events
68 # List a all time aggregations
69 var aggregs
= new Array[Aggreg]
71 # Total number of allocations
72 var cpttot
= new Counter[String]
74 # Total number of allocated bytes
75 var siztot
= new Counter[String]
77 # Parse the log file `filepath` and fill `aggregs`.
79 # Current lines (not yet put in an aggreg)
80 var lines
= new Counter[String]
85 for l
in "memory.log".to_path
.each_line
do
87 var t
= l
.substring_from
(2).to_f
89 while t
> time
+ dt
do
93 #if time > 1000.0 then break
102 # Create and register a new aggregation
103 fun aggreg
(lines
: Counter[String], t1
: Float) do
104 var aggreg
= new Aggreg(t1
)
106 print
"events:{events} aggregs:{aggregs.length} {t1}ms"
108 # Process each distinct line
110 var a
= l
.split
('\t')
111 if a
.length
!= 3 then
112 print
"Error {a.length}. {l}"
120 aggreg
.asiz
[e
] += v
* s
121 else if c
== "-" then
123 aggreg
.dsiz
[e
] += v
* s
128 # Sum all information
129 for e
, v
in aggreg
.acpt
do cpttot
[e
] += v
130 for e
, v
in aggreg
.asiz
do siztot
[e
] += v
131 for e
, v
in aggreg
.dcpt
do cpttot
[e
] -= v
132 for e
, v
in aggreg
.dsiz
do siztot
[e
] -= v
135 aggreg
.cpttot
.add_all cpttot
136 aggreg
.siztot
.add_all siztot
138 cpttot
.print_elements
(2)
141 # Generate a *long* CVS file, to use with statistical tools
144 var res
= new Template
147 res
.add
"time, class"
148 for f
, c
in aggregs
.first
.fields
do
154 # Collect the largest tags, add add an `other` tag.
155 var elts
= siztot
.sort
.reversed
.sub
(0,10).reversed
161 # For each field compute the value of `other`
162 for f
, c
in a
.fields
do
165 if e
== "other" then continue
171 # For each tag (incl. other) produce a line
177 for f
, v
in a
.fields
do
188 var m
= new MemProg("memory.log", 100.0)
191 m
.cpttot
.print_summary
192 m
.siztot
.print_summary
194 m
.tolong
.write_to_file
"memory.csv"
196 if "memplot.r".file_exists
then
197 system
("r memplot.r")