-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
436 lines (352 loc) · 9.64 KB
/
main.go
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"html"
"os"
"runtime/debug"
"runtime/pprof"
"strings"
)
const usage = `Usage: dhatless [FLAGS] DHAT_FILE
Generate a report with all allocations recorded in the given DHAT output file.
By default, the generated report will be written to STDOUT as regular text.
Use -html to generate a HTML report.
Specific allocations can be ignored by using a ignore file.
A ignore file contains keywords(e.g. my_function) which will be searched in the
frame stack of all allocations.
If the frame stack of an allocation contains one of the keywords, that allocation
will not be added to the generated report.
The ignore file must contain a list of keywords separated by newline('\n').
Whitespaces(' ' and '\t') are trimmed from the start and end of the lines.
Empty lines and comment lines(which start with '#') are ignored.
FLAGS:
`
func main() {
if err := mainErr(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
func mainErr(args []string) error {
fset := flag.NewFlagSet("root", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, usage)
fset.PrintDefaults()
fmt.Fprintln(os.Stderr, "")
}
ignoreFile := fset.String("i", "", "`File` with keywords to ignored, one per line")
outputHtml := fset.Bool("html", false, "Generate HTML output")
printVersion := fset.Bool("version", false, "Print version")
cpuProfile := fset.Bool("profile-cpu", false, "Write CPU profile")
memProfile := fset.Bool("profile-mem", false, "Write memory profile")
if err := fset.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
if *printVersion {
version()
return nil
}
if fset.NArg() != 1 {
fset.Usage()
return fmt.Errorf("need DHAT file")
}
if *cpuProfile {
f, err := os.Create("profile.cpu")
if err != nil {
return err
}
_ = pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
defer func() {
if *memProfile {
f, err := os.Create("profile.mem")
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
}
_ = pprof.WriteHeapProfile(f)
f.Close()
}
}()
ignoreList, err := parseIgnoreFile(*ignoreFile)
if err != nil {
return err
}
report, err := parseReport(fset.Arg(0))
if err != nil {
return err
}
const dhatVersion = 2
if report.Version != dhatVersion {
return fmt.Errorf(
"DHAT report version %d is not supported, only version %d is supported",
report.Version, dhatVersion,
)
}
if *outputHtml {
fmt.Print(htmlHeader)
}
if *outputHtml {
fmt.Printf("<br><pre>\n")
}
fmt.Printf("Command: %s\n", report.Cmd)
fmt.Printf("PID: %d\n", report.PID)
fmt.Printf("Mode: %s\n", report.InvocationMode)
fmt.Printf("t-end: %d %s\n", report.TimeAtEnd, report.TimeUnit)
if *outputHtml {
fmt.Printf("</pre><br><hr><br>\n")
}
allocCount := 1
for i, pp := range report.ProgramPoints {
if shouldIgnore(*report, i, ignoreList) {
continue
}
if *outputHtml {
fmt.Printf("<details><summary>Allocation #%d</summary><br><p>\n", allocCount)
} else {
fmt.Printf("\n==== Allocation #%d ====\n", allocCount)
}
fmt.Printf("%d bytes in %d blocks\n", pp.TotalBytes, pp.TotalBlocks)
allocCount++
if *outputHtml {
fmt.Println("</p><pre>")
}
for j := len(pp.Frames) - 1; j >= 0; j-- {
frame := report.GetFrame(pp.Frames[j])
if *outputHtml {
frame = html.EscapeString(frame)
}
fmt.Printf("%s\n", frame)
}
if *outputHtml {
fmt.Println("</pre></details><br>")
}
}
if *outputHtml {
fmt.Print(`
</body>
</html>
`)
}
return nil
}
func version() {
bi, _ := debug.ReadBuildInfo()
g := func(k string) string {
for _, v := range bi.Settings {
if v.Key == k {
return v.Value
}
}
return ""
}
fmt.Println("go ", bi.GoVersion)
fmt.Println("main ", bi.Main.Version)
if v := g("vcs.revision"); v != "" {
fmt.Println("commit ", g("vcs.revision"))
}
if v := g("vcs.time"); v != "" {
fmt.Println("time ", g("vcs.time"))
}
if v := g("vcs.modified"); v != "" {
fmt.Println("dirty ", g("vcs.modified"))
}
}
const htmlHeader = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-size: 15px;
}
pre {
overflow-x: auto;
}
details > summary {
padding: 2px 6px;
width: 15em;
background-color: #ddd;
border: none;
box-shadow: 3px 3px 4px black;
cursor: pointer;
}
pre {
border-radius: 0 0 10px 10px;
background-color: #ddd;
padding: 2px 6px;
margin: 0;
box-shadow: 3px 3px 4px black;
}
details[open] > summary {
background-color: #ccf;
}
button {
background-color: #ddd;
font-size: 15px;
width: 10%;
}
</style>
<title>DHAT allocations report</title>
</head>
<body>
<button id="btn-openall">Open All</button>
<button id="btn-closeall">Close All</button>
<hr>
<script>
document.getElementById("btn-openall").addEventListener("click", function(event) {
const elements = document.querySelectorAll('details');
elements.forEach(element => {
element.open = true;
});
});
document.getElementById("btn-closeall").addEventListener("click", function(event) {
const elements = document.querySelectorAll('details');
elements.forEach(element => {
element.open = false;
});
});
</script>
`
func parseReport(file string) (*Report, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
var report Report
if err := json.NewDecoder(f).Decode(&report); err != nil {
return nil, err
}
return &report, nil
}
func parseIgnoreFile(file string) ([]string, error) {
if file == "" {
return nil, nil
}
ignoreList := make([]string, 0, 32)
content, err := os.ReadFile(file)
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(content), "\n") {
line := strings.Trim(line, " \t")
if line == "" {
continue
}
if line[0] == '#' {
continue
}
ignoreList = append(ignoreList, line)
}
return ignoreList, nil
}
func shouldIgnore(r Report, frame int, ignoreList []string) bool {
for _, s := range ignoreList {
if r.ProgramPointHasFrame(frame, s) {
return true
}
}
return false
}
type Report struct {
// Version number of the format. Incremented on each
// backwards-incompatible change. A mandatory integer.
Version int `json:"dhatFileVersion"`
// The invocation mode. A mandatory, free-form string.
InvocationMode string `json:"mode"`
// The verb used before above stack frames, i.e. "<verb> at {". A
// mandatory string.
StackFrameVerb string `json:"verb"`
// Are block lifetimes recorded? Affects whether some other fields are
// present. A mandatory boolean.
BlockLifetimesRecorded bool `json:"bklt"`
// Are block accesses recorded? Affects whether some other fields are
// present. A mandatory boolean.
BlockAccessesRecorded bool `json:"bkacc"`
// Byte/bytes/blocks-position units. Optional strings. "byte", "bytes",
// and "blocks" are the values used if these fields are omitted.
ByteUnit string `json:"bu,omitempty"`
BytesUnit string `json:"bsu,omitempty"`
BlocksUnit string `json:"bksu,omitempty"`
// Time units (individual and 1,000,000x). Mandatory strings.
TimeUnit string `json:"tu,omitempty"`
MilTimeUnit string `json:"mtu,omitempty"`
// The "short-lived" time threshold, measures in "tu"s.
// - bklt=true: a mandatory integer.
// - bklt=false: omitted.
ShortLivedTimeThreshold int `json:"tuth"`
// The executed command. A mandatory string.
Cmd string `json:"cmd"`
// The process ID. A mandatory integer.
PID int `json:"pid"`
// The time at the end of execution (t-end). A mandatory integer.
TimeAtEnd int `json:"te"`
// The time of the global max (t-gmax).
// - bklt=true: a mandatory integer.
// - bklt=false: omitted.
TimeAtGlobalMax int `json:"tg"`
// The program points. A mandatory array.
ProgramPoints []ProgramPoint `json:"pps"`
// Frame table. A mandatory array of strings.
FramesTable []string `json:"ftbl"`
}
func (r Report) ProgramPointHasFrame(i int, s string) bool {
for _, frame := range r.ProgramPoints[i].Frames {
sym := strings.Split(r.FramesTable[frame], ": ")[1]
if strings.Contains(sym, s) {
return true
}
}
return false
}
func (r Report) GetFrame(i int) string {
return strings.Split(r.FramesTable[i], ": ")[1]
}
type ProgramPoint struct {
// Total bytes and blocks. Mandatory integers.
TotalBytes int `json:"tb"`
TotalBlocks int `json:"tbk"`
// Total lifetimes of all blocks allocated at this PP.
// - bklt=true: a mandatory integer.
// - bklt=false: omitted.
TotalLifetimesOfBlocks int `json:"tl"`
// The maximum bytes and blocks for this PP.
// - bklt=true: mandatory integers.
// - bklt=false: omitted.
MaxBytes int `json:"mb"`
MaxBlocks int `json:"mbk"`
// The bytes and blocks at t-gmax for this PP.
// - bklt=true: mandatory integers.
// - bklt=false: omitted.
BytesAtTgmax int `json:"gb"`
BlocksAtTgmax int `json:"gbk"`
// The bytes and blocks at t-end for this PP.
// - bklt=true: mandatory integers.
// - bklt=false: omitted.
BytesAtTend int `json:"eb"`
BlocksAtTend int `json:"ebk"`
// The reads and writes of blocks for this PP.
// - bkacc=true: mandatory integers.
// - bkacc=false: omitted.
ReadsOfBlocks int `json:"rb"`
WritesOfBlocks int `json:"wb"`
// The exact accesses of blocks for this PP. Only used when all
// allocations are the same size and sufficiently small. A negative
// element indicates run-length encoding of the following integer.
// E.g. `-3, 4` means "three 4s in a row".
// - bkacc=true: an optional array of integers.
// - bkacc=false: omitted.
BlockAccesses []int `json:"acc"`
// Frames. Each element is an index into the "ftbl" array below.
// - All modes: A mandatory array of integers.
Frames []int `json:"fs"`
}