-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathperf_decode.ml
More file actions
818 lines (758 loc) · 30.5 KB
/
perf_decode.ml
File metadata and controls
818 lines (758 loc) · 30.5 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
open! Core
open! Async
let saturating_sub_i64 a b =
match Int64.(to_int (a - b)) with
| None -> Int.max_value
| Some offset -> offset
;;
let perf_event_header_re =
Re.Perl.re
{|^ *([0-9]+)/([0-9]+) +([0-9]+)\.([0-9]+): +([0-9]+) +([a-z\-]+)(/[a-z=0-9]+)?(/[a-zA-Z]*)?:([a-zA-Z]+:)?(.*)$|}
|> Re.compile
;;
let perf_extra_sampled_event_re =
Re.Perl.re {|^ *([0-9]+) +([0-9a-f]+) (.*)$|} |> Re.compile
;;
let perf_callstack_entry_re = Re.Perl.re "^\t *([0-9a-f]+) (.*)$" |> Re.compile
let perf_branches_event_re =
Re.Perl.re
{|^ *(call|return|tr strt|syscall|sysret|hw int|iret|int|tx abrt|tr end|tr strt tr end|tr end (?:async|call|return|syscall|sysret|iret)|jmp|jcc) +(\(x\) +)?([0-9a-f]+) (.*) => +([0-9a-f]+) (.*)$|}
|> Re.compile
;;
let perf_cbr_event_re =
Re.Perl.re {|^ *([a-z )(]*)? +cbr: +([0-9]+ +freq: +([0-9]+) MHz)?(.*)$|} |> Re.compile
;;
let perf_ptwrite_event_re =
Re.Perl.re
{|^ *(call|return|tr strt|syscall|sysret|hw int|iret|int|tx abrt|tr end|tr strt tr end|tr end (?:async|call|return|syscall|sysret|iret)|jmp|jcc)? +IP: +([0-9a-f]+) +payload: +(0x[0-9a-f]+) (.*) ([0-9]+) +([0-9a-f]+) (.*)$|}
|> Re.compile
;;
let trace_error_re =
Re.Posix.re
{|^ instruction trace error type [0-9]+ (time ([0-9]+)\.([0-9]+) )?cpu [\-0-9]+ pid ([\-0-9]+) tid ([\-0-9]+) ip (0x[0-9a-fA-F]+|0) code [0-9]+: (.*)$|}
|> Re.compile
;;
let symbol_and_offset_re = Re.Perl.re {|^(.*)\+(0x[0-9a-f]+)\s+\(.*\)$|} |> Re.compile
let unknown_symbol_dso_re = Re.Perl.re {|^\[unknown\]\s+\((.*)\)|} |> Re.compile
type header =
| Trace_error
| Event of
{ thread : Event.Thread.t
; time : Time_ns.Span.t
; period : int
; event :
[ `Branches
| `Cbr
| `Psb
| `Cycles
| `Branch_misses
| `Cache_misses
| `Ptwrite
]
; remaining_line : string
}
let maybe_pid_of_string = function
| "0" -> None
| pid -> Some (Pid.of_string pid)
;;
let parse_time ~time_hi ~time_lo =
let time_lo =
(* In practice, [time_lo] seems to always be 9 decimal places, but it seems
good to guard against other possibilities. *)
let num_decimal_places = String.length time_lo in
match Ordering.of_int (Int.compare num_decimal_places 9) with
| Less -> Int.of_string time_lo * Int.pow 10 (9 - num_decimal_places)
| Equal -> Int.of_string time_lo
| Greater -> Int.of_string (String.prefix time_lo 9)
in
let time_hi = Int.of_string time_hi in
time_lo + (time_hi * 1_000_000_000) |> Time_ns.Span.of_int_ns
;;
let parse_event_header line =
if String.is_prefix line ~prefix:" instruction trace error"
then Trace_error
else (
match Re.Group.all (Re.exec perf_event_header_re line) with
| [| _
; pid
; tid
; time_hi
; time_lo
; period
; event_name
; _event_config
; _event_selector
; _selector
; remaining_line
|] ->
let pid = maybe_pid_of_string pid in
let tid = maybe_pid_of_string tid in
let time = parse_time ~time_hi ~time_lo in
let period = Int.of_string period in
let event =
match event_name with
| "branches" -> `Branches
| "cbr" -> `Cbr
| "psb" -> `Psb
| "cycles" -> `Cycles
| "branch-misses" -> `Branch_misses
| "cache-misses" -> `Cache_misses
| "ptwrite" -> `Ptwrite
| _ ->
raise_s
[%message
"Unexpected event type when parsing perf output" (event_name : string)]
in
Event { thread = { pid; tid }; time; period; event; remaining_line }
| results ->
Core.print_endline "[failed]";
raise_s
[%message
"Regex of perf output did not match expected fields" (results : string array)])
;;
let parse_symbol_and_offset ?perf_maps pid str ~addr : Symbol.t * int =
match Re.Group.all (Re.exec symbol_and_offset_re str) with
| [| _; symbol; offset |] ->
let offset =
(* Sometimes [perf] reports symbols and offsets like
[memcpy@plt+0xffffffffff22f000], which are definitely wrong (the implied
execution address lies in kernel space, but we're in userspace).
This is a [perf] bug, but we need to be resililent to it.
[int_trunc_of_hex_string] will drop the leading 1 bit, resulting in a differently
wrong offset, but won't crash. We don't want to use [int64_of_hex_string] here to
avoid the extra allocation. *)
Util.int_trunc_of_hex_string ~remove_hex_prefix:true offset
in
From_perf symbol, offset
| _ | (exception _) ->
let failed = Symbol.Unknown, 0 in
(match perf_maps, pid with
| None, _ | _, None ->
(match Re.Group.all (Re.exec unknown_symbol_dso_re str) with
| [| _; dso |] ->
(* CR-someday tbrindus: ideally, we would subtract the DSO base offset
from [offset] here. *)
From_perf [%string "[unknown @ %{addr#Int64.Hex} (%{dso})]"], 0
| _ | (exception _) -> failed)
| Some perf_map, Some pid ->
(match Perf_map.Table.symbol ~pid perf_map ~addr with
| None -> failed
| Some location ->
(* It's strange that perf isn't resolving these symbols. It says on the
tin that it supports perf map files! *)
let offset = saturating_sub_i64 addr location.start_addr in
From_perf_map location, offset))
;;
let trace_error_to_event line : Event.Decode_error.t =
match Re.Group.all (Re.exec trace_error_re line) with
| [| _; _; time_hi; time_lo; pid; tid; ip; message |] ->
let pid = maybe_pid_of_string pid in
let tid = maybe_pid_of_string tid in
let instruction_pointer =
if String.( = ) ip "0"
then None
else Some (Util.int64_of_hex_string ~remove_hex_prefix:true ip)
in
let time =
if String.is_empty time_hi && String.is_empty time_lo
then Time_ns_unix.Span.Option.none
else Time_ns_unix.Span.Option.some (parse_time ~time_hi ~time_lo)
in
{ thread = { pid; tid }; instruction_pointer; message; time }
| results ->
raise_s
[%message
"Regex of trace error did not match expected fields" (results : string array)]
;;
let parse_perf_cbr_event thread time line : Event.t =
match Re.Group.all (Re.exec perf_cbr_event_re line) with
| [| _; _; _; freq; _ |] ->
Ok
{ thread; time; data = Power { freq = Int.of_string freq }; in_transaction = false }
| results ->
raise_s
[%message
"Regex of perf cbr event did not match expected fields" (results : string array)]
;;
let parse_location ?perf_maps ~pid instruction_pointer symbol_and_offset
: Event.Location.t
=
let instruction_pointer = Util.int64_of_hex_string instruction_pointer in
let symbol, symbol_offset =
parse_symbol_and_offset ?perf_maps pid symbol_and_offset ~addr:instruction_pointer
in
{ instruction_pointer; symbol; symbol_offset }
;;
let parse_callstack_entry ?perf_maps (thread : Event.Thread.t) line : Event.Location.t =
match Re.Group.all (Re.exec perf_callstack_entry_re line) with
| [| _; instruction_pointer; symbol_and_offset |] ->
parse_location ?perf_maps ~pid:thread.pid instruction_pointer symbol_and_offset
| results ->
raise_s
[%message
"perf output did not match expected regex when parsing callstack entry"
(results : string array)]
;;
let parse_perf_cycles_event ?perf_maps (thread : Event.Thread.t) time lines : Event.t =
let callstack =
List.map lines ~f:(parse_callstack_entry ?perf_maps thread) |> List.rev
in
Ok { thread; time; data = Stacktrace_sample { callstack }; in_transaction = false }
;;
let parse_perf_branches_event ?perf_maps (thread : Event.Thread.t) time line : Event.t =
match Re.Group.all (Re.exec perf_branches_event_re line) with
| [| _
; kind
; aux_flags
; src_instruction_pointer
; src_symbol_and_offset
; dst_instruction_pointer
; dst_symbol_and_offset
|] ->
let src_instruction_pointer = Util.int64_of_hex_string src_instruction_pointer in
let dst_instruction_pointer = Util.int64_of_hex_string dst_instruction_pointer in
let src_symbol, src_symbol_offset =
parse_symbol_and_offset
?perf_maps
thread.pid
src_symbol_and_offset
~addr:src_instruction_pointer
in
let dst_symbol, dst_symbol_offset =
parse_symbol_and_offset
?perf_maps
thread.pid
dst_symbol_and_offset
~addr:dst_instruction_pointer
in
let starts_trace, kind =
match String.chop_prefix kind ~prefix:"tr strt" with
| None -> false, kind
| Some rest -> true, String.lstrip ~drop:Char.is_whitespace rest
in
let ends_trace, kind =
match String.chop_prefix kind ~prefix:"tr end" with
| None -> false, kind
| Some rest -> true, String.lstrip ~drop:Char.is_whitespace rest
in
let trace_state_change : Trace_state_change.t option =
match starts_trace, ends_trace with
| true, false -> Some Start
| false, true -> Some End
| false, false
(* "tr strt tr end" happens when someone `go run`s ./demo/demo.go. But
that trace is pretty broken for other reasons, so it's hard to say if
this is truly necessary. Regardless, it's slightly more user friendly
to show a broken trace instead of crashing here. *)
| true, true -> None
in
(* record the flag indicating we're within a transaction *)
let in_transaction = String.contains aux_flags 'x' in
let kind : Event.Kind.t option =
match String.strip kind with
| "call" -> Some Call
| "return" -> Some Return
| "int" -> Some Interrupt
| "jmp" -> Some Jump
| "jcc" -> Some Jump
| "syscall" -> Some Syscall
| "hw int" -> Some Hardware_interrupt
| "iret" -> Some Iret
| "sysret" -> Some Sysret
| "async" -> Some Async
| "tx abrt" -> Some Tx_abort
| "" -> None
| _ ->
printf "Warning: skipping unrecognized perf output: %s\n%!" line;
None
in
Ok
{ thread
; time
; data =
Trace
{ trace_state_change
; kind
; src =
{ instruction_pointer = src_instruction_pointer
; symbol = src_symbol
; symbol_offset = src_symbol_offset
}
; dst =
{ instruction_pointer = dst_instruction_pointer
; symbol = dst_symbol
; symbol_offset = dst_symbol_offset
}
}
; in_transaction
}
| results ->
raise_s
[%message "Regex of expected perf output did not match." (results : string array)]
;;
let parse_perf_extra_sampled_event
?perf_maps
(thread : Event.Thread.t)
time
period
line
lines
name
: Event.t
=
let (location : Event.Location.t) =
match lines with
| [] ->
(match Re.Group.all (Re.exec perf_extra_sampled_event_re line) with
| [| _str; _; instruction_pointer; symbol_and_offset |] ->
parse_location ?perf_maps ~pid:thread.pid instruction_pointer symbol_and_offset
| results ->
raise_s
[%message
"Regex of perf event did not match expected fields" (results : string array)])
| lines -> List.hd_exn lines |> parse_callstack_entry ?perf_maps thread
in
Ok
{ thread
; time
; data = Event_sample { location; count = period; name }
; in_transaction = false
}
;;
let parse_perf_ptwrite_event ?perf_maps (thread : Event.Thread.t) time line : Event.t =
match Re.Group.all (Re.exec perf_ptwrite_event_re line) with
| [| _
; _branch_kind
; _fup_ip
; payload
; _payload_ascii
; _unknown
; instruction_pointer
; symbol_and_offset
|] ->
let location =
parse_location ?perf_maps ~pid:thread.pid instruction_pointer symbol_and_offset
in
Ok
{ thread
; time
; data = Ptwrite { location; data = payload }
; in_transaction = false
}
| results ->
raise_s
[%message
"Regex of perf ptwrite event did not match expected fields"
(results : string array)]
;;
let to_event ?perf_maps lines : Event.t option =
try
match lines with
| [] -> raise_s [%message "Unexpected line while parsing perf output."]
| first_line :: lines ->
let header = parse_event_header first_line in
(match header with
| Trace_error -> Some (Error (trace_error_to_event first_line))
| Event { thread; time; period; event; remaining_line } ->
(match event with
| `Branches ->
Some (parse_perf_branches_event ?perf_maps thread time remaining_line)
| `Cbr ->
(* cbr (core-to-bus ratio) are events which show frequency changes. *)
Some (parse_perf_cbr_event thread time remaining_line)
| `Psb -> (* Ignore psb (packet stream boundary) packets *) None
| `Cycles -> Some (parse_perf_cycles_event ?perf_maps thread time lines)
| `Branch_misses ->
Some
(parse_perf_extra_sampled_event
?perf_maps
thread
time
period
remaining_line
lines
Branch_misses)
| `Cache_misses ->
Some
(parse_perf_extra_sampled_event
?perf_maps
thread
time
period
remaining_line
lines
Cache_misses)
| `Ptwrite ->
Some (parse_perf_ptwrite_event ?perf_maps thread time remaining_line)))
with
| exn ->
raise_s
[%message
"BUG: exception raised while parsing perf output. Please report this to \
https://github.com/janestreet/magic-trace/issues/"
(exn : exn)
~perf_output:(lines : string list)]
;;
let split_line_pipe pipe : string list Pipe.Reader.t =
let reader, writer = Pipe.create () in
don't_wait_for
(let%bind acc =
Pipe.fold pipe ~init:[] ~f:(fun acc line ->
let should_acc = not String.(line = "") in
let should_write =
String.(line = "") || not (Char.equal (String.get line 0) '\t')
in
let%map () =
if List.length acc > 0 && should_write
then Pipe.write writer (List.rev acc)
else Deferred.return ()
in
let prev_acc = if should_write then [] else acc in
if should_acc then line :: prev_acc else prev_acc)
in
let%map () =
if List.length acc > 0
then Pipe.write writer (List.rev acc)
else Deferred.return ()
in
Pipe.close writer);
reader
;;
let to_events ?perf_maps pipe =
let pipe = split_line_pipe pipe in
(* Every route of filtering on streams in an async way seems to be deprecated,
including converting to pipes which says that the stream creation should be
switched to a pipe creation. Changing Async_shell is out-of-scope, and I also
can't see a reason why filter_map would lead to memory leaks. *)
Pipe.map pipe ~f:(to_event ?perf_maps) |> Pipe.filter_map ~f:Fn.id
;;
let%test_module _ =
(module struct
open Core
let check s =
to_event (String.split ~on:'\n' s) |> [%sexp_of: Event.t option] |> print_s
;;
let%expect_test "C symbol" =
check
{| 25375/25375 4509191.343298468: 1 branches:uH: call 7f6fce0b71f4 __clock_gettime+0x24 (foo.so) => 7ffd193838e0 __vdso_clock_gettime+0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (25375)) (tid (25375)))) (time 52d4h33m11.343298468s)
(data (Trace (kind Call) (src 0x7f6fce0b71f4) (dst 0x7ffd193838e0)))))) |}]
;;
let%expect_test "C symbol trace start" =
check
{| 25375/25375 4509191.343298468: 1 branches:uH: tr strt 0 [unknown] (foo.so) => 7f6fce0b71d0 __clock_gettime+0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (25375)) (tid (25375)))) (time 52d4h33m11.343298468s)
(data (Trace (trace_state_change Start) (src 0x0) (dst 0x7f6fce0b71d0)))))) |}]
;;
let%expect_test "C++ symbol" =
check
{| 7166/7166 4512623.871133092: 1 branches:uH: call 9bc6db a::B<a::C, a::D<a::E>, a::F, a::F, G::H, a::I>::run+0x1eb (foo.so) => 9f68b0 J::K<int, std::string>+0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (7166)) (tid (7166)))) (time 52d5h30m23.871133092s)
(data (Trace (kind Call) (src 0x9bc6db) (dst 0x9f68b0)))))) |}]
;;
let%expect_test "OCaml symbol" =
check
{|2017001/2017001 761439.053336670: 1 branches:uH: call 56234f77576b Base.Comparable.=_2352+0xb (foo.so) => 56234f4bc7a0 caml_apply2+0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (2017001)) (tid (2017001)))) (time 8d19h30m39.05333667s)
(data (Trace (kind Call) (src 0x56234f77576b) (dst 0x56234f4bc7a0)))))) |}]
;;
(* CR-someday wduff: Leaving this concrete example here for when we support this. See my
comment above as well.
{[
let%expect_test "Unknown Go symbol" =
check
{|2118573/2118573 770614.599007116: branches:uH: tr strt tr end 0 [unknown] (foo.so) => 4591e1 [unknown] (foo.so)|};
[%expect]
;;
]}
*)
let%expect_test "manufactured example 1" =
check
{|2017001/2017001 761439.053336670: 1 branches:uH: call 56234f77576b x => +0xb (foo.so) => 56234f4bc7a0 caml_apply2+0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (2017001)) (tid (2017001)))) (time 8d19h30m39.05333667s)
(data (Trace (kind Call) (src 0x56234f77576b) (dst 0x56234f4bc7a0)))))) |}]
;;
let%expect_test "manufactured example 2" =
check
{|2017001/2017001 761439.053336670: 1 branches:uH: call 56234f77576b x => +0xb (foo.so) => 56234f4bc7a0 => +0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (2017001)) (tid (2017001)))) (time 8d19h30m39.05333667s)
(data (Trace (kind Call) (src 0x56234f77576b) (dst 0x56234f4bc7a0)))))) |}]
;;
let%expect_test "manufactured example 3" =
check
{|2017001/2017001 761439.053336670: 1 branches:uH: call 56234f77576b + +0xb (foo.so) => 56234f4bc7a0 caml_apply2+0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (2017001)) (tid (2017001)))) (time 8d19h30m39.05333667s)
(data (Trace (kind Call) (src 0x56234f77576b) (dst 0x56234f4bc7a0)))))) |}]
;;
let%expect_test "unknown symbol in DSO" =
check
{|2017001/2017001 761439.053336670: 1 branches:uH: call 56234f77576b [unknown] (foo.so) => 56234f4bc7a0 caml_apply2+0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (2017001)) (tid (2017001)))) (time 8d19h30m39.05333667s)
(data (Trace (kind Call) (src 0x56234f77576b) (dst 0x56234f4bc7a0)))))) |}]
;;
let%expect_test "DSO with spaces in it" =
check
{|2017001/2017001 761439.053336670: 1 branches:uH: call 56234f77576b [unknown] (this is a spaced dso.so) => 56234f4bc7a0 caml_apply2+0x0 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (2017001)) (tid (2017001)))) (time 8d19h30m39.05333667s)
(data (Trace (kind Call) (src 0x56234f77576b) (dst 0x56234f4bc7a0)))))) |}]
;;
let%expect_test "software interrupts" =
check
"1907478/1909463 457407.880965552: 1 \
branches:uH: int 564aa58813d4 \
Builtins_RunMicrotasks+0x554 (/usr/local/bin/workload) => 564aa584fa00 \
Builtins_Call_ReceiverIsNotNullOrUndefined+0x0 (/usr/local/bin/workload)";
[%expect
{|
((Ok
((thread ((pid (1907478)) (tid (1909463)))) (time 5d7h3m27.880965552s)
(data (Trace (kind Interrupt) (src 0x564aa58813d4) (dst 0x564aa584fa00)))))) |}]
;;
let%expect_test "decode error with a timestamp" =
check
" instruction trace error type 1 time 47170.086912826 cpu -1 pid 293415 tid \
293415 ip 0x7ffff7327730 code 7: Overflow packet";
[%expect
{|
((Error
((thread ((pid (293415)) (tid (293415)))) (time (13h6m10.086912826s))
(instruction_pointer (0x7ffff7327730)) (message "Overflow packet")))) |}]
;;
let%expect_test "decode error without a timestamp" =
check
" instruction trace error type 1 cpu -1 pid 293415 tid 293415 ip 0x7ffff7327730 \
code 7: Overflow packet";
[%expect
{|
((Error
((thread ((pid (293415)) (tid (293415)))) (time ())
(instruction_pointer (0x7ffff7327730)) (message "Overflow packet")))) |}]
;;
let%expect_test "lost trace data" =
check
" instruction trace error type 1 time 2651115.104731379 cpu -1 pid 1801680 tid \
1801680 ip 0 code 8: Lost trace data";
[%expect
{|
((Error
((thread ((pid (1801680)) (tid (1801680)))) (time (30d16h25m15.104731379s))
(instruction_pointer ()) (message "Lost trace data")))) |}]
;;
let%expect_test "never-ending loop" =
check
" instruction trace error type 1 time 406036.830210719 cpu -1 pid 114362 tid \
114362 ip 0xffffffffb0999ed5 code 10: Never-ending loop (refer perf config \
intel-pt.max-loops)";
[%expect
{|
((Error
((thread ((pid (114362)) (tid (114362)))) (time (4d16h47m16.830210719s))
(instruction_pointer (-0x4f66612b))
(message "Never-ending loop (refer perf config intel-pt.max-loops)")))) |}]
;;
let%expect_test "power event cbr" =
check
"2937048/2937048 448556.689322817: 1 \
cbr: cbr: 46 freq: 4606 MHz (159%) \
0 0 [unknown] ([unknown])";
[%expect
{|
((Ok
((thread ((pid (2937048)) (tid (2937048)))) (time 5d4h35m56.689322817s)
(data (Power (freq 4606)))))) |}]
;;
(* Perf seems to change spacing when frequency is small and our regex was
crashing on this case. *)
let%expect_test "cbr event with double spaces" =
check
"2420596/2420596 525062.244538101: \
1 cbr: syscall cbr: 8 \
freq: 801 MHz ( 28%) 0 7f77dc9f4646 __nanosleep+0x16 \
(/usr/lib64/libc-2.28.so)";
[%expect
{|
((Ok
((thread ((pid (2420596)) (tid (2420596)))) (time 6d1h51m2.244538101s)
(data (Power (freq 801)))))) |}]
;;
let%expect_test "cbr event with tr end" =
check
"21302/21302 82318.700445693: 1 cbr: tr end \
cbr: 45 freq: 4500 MHz (118%) 0 5368e58 \
__symbol+0x168 (/dev/foo.exe)";
[%expect
{|
((Ok
((thread ((pid (21302)) (tid (21302)))) (time 22h51m58.700445693s)
(data (Power (freq 4500)))))) |}]
;;
(* Expected [None] because we ignore these events currently. *)
let%expect_test "power event psb offs" =
check
"2937048/2937048 448556.689403475: 1 \
psb: psb offs: 0x4be8 \
0 7f068fbfd330 mmap64+0x50 (/usr/lib64/ld-2.28.so)";
[%expect {|
() |}]
;;
let%expect_test "sampled callstack" =
check
"2060126/2060126 178090.391624068: 555555 cycles:u:\n\
\tffffffff97201100 [unknown] ([unknown])\n\
\t7f9bd48c1d80 _dl_setup_hash+0x0 (/usr/lib64/ld-2.28.so)\n\
\t7f9bd48bd18f _dl_map_object_from_fd+0xb8f (/usr/lib64/ld-2.28.so)\n\
\t7f9bd48bf6b0 _dl_map_object+0x1e0 (/usr/lib64/ld-2.28.so)\n\
\t7f9bd48ca184 dl_open_worker_begin+0xa4 (/usr/lib64/ld-2.28.so)\n\
\t7f9bd44521a2 _dl_catch_exception+0x82 (/usr/lib64/libc-2.28.so)\n\
\t7f9bd48c9ac2 dl_open_worker+0x32 (/usr/lib64/ld-2.28.so)\n\
\t7f9bd44521a2 _dl_catch_exception+0x82 (/usr/lib64/libc-2.28.so)\n\
\t7f9bd48c9d0c _dl_open+0xac (/usr/lib64/ld-2.28.so)\n\
\t7f9bd46ae1e8 dlopen_doit+0x58 (/usr/lib64/libdl-2.28.so)\n\
\t7f9bd44521a2 _dl_catch_exception+0x82 (/usr/lib64/libc-2.28.so)\n\
\t7f9bd445225e _dl_catch_error+0x2e (/usr/lib64/libc-2.28.so)\n\
\t7f9bd46ae964 _dlerror_run+0x64 (/usr/lib64/libdl-2.28.so)\n\
\t7f9bd46ae285 dlopen@@GLIBC_2.2.5+0x45 (/usr/lib64/libdl-2.28.so)\n\
\t4008de main+0x87 (/home/demo)";
[%expect
{|
((Ok
((thread ((pid (2060126)) (tid (2060126)))) (time 2d1h28m10.391624068s)
(data
(Stacktrace_sample
(callstack
(0x4008de 0x7f9bd46ae285 0x7f9bd46ae964 0x7f9bd445225e 0x7f9bd44521a2
0x7f9bd46ae1e8 0x7f9bd48c9d0c 0x7f9bd44521a2 0x7f9bd48c9ac2
0x7f9bd44521a2 0x7f9bd48ca184 0x7f9bd48bf6b0 0x7f9bd48bd18f
0x7f9bd48c1d80 -0x68dfef00))))))) |}]
;;
let%expect_test "cache-misses event with ipt" =
check
"3871580/3871580 430720.265503976: 50 \
cache-misses/period=50/u: 0 \
7fca9945c595 __sleep+0x55 (/usr/lib64/libc-2.28.so)";
[%expect
{|
((Ok
((thread ((pid (3871580)) (tid (3871580)))) (time 4d23h38m40.265503976s)
(data
(Event_sample (location 0x7fca9945c595) (count 50) (name Cache_misses)))))) |}]
;;
let%expect_test "cache-misses event with sampling" =
check
"3871580/3871580 431043.387175119: 50 cache-misses/period=50/u: \n\
\t7fca999481a0 _dl_unmap+0x0 (/usr/lib64/ld-2.28.so)\n\
\t7fca999454cc _dl_close_worker+0x83c (/usr/lib64/ld-2.28.so)\n\
\t7fca99945dbd _dl_close+0x2d (/usr/lib64/ld-2.28.so)\n\
\t7fca994cc1a2 _dl_catch_exception+0x82 (/usr/lib64/libc-2.28.so)\n\
\t7fca994cc25e _dl_catch_error+0x2e (/usr/lib64/libc-2.28.so)\n\
\t7fca99728964 _dlerror_run+0x64 (/usr/lib64/libdl-2.28.so)\n\
\t7fca99728313 dlclose+0x23 (/usr/lib64/libdl-2.28.so)\n\
\t4009b7 main+0x160 (/usr/local/home/demo)\n";
[%expect
{|
((Ok
((thread ((pid (3871580)) (tid (3871580)))) (time 4d23h44m3.387175119s)
(data
(Event_sample (location 0x7fca999481a0) (count 50) (name Cache_misses)))))) |}]
;;
let%expect_test "branch-misses event with ipt" =
check
"3871580/3871580 431228.526799230: 50 \
branch-misses/period=50/u: 0 \
7fca99943c60 _dl_open+0x0 (/usr/lib64/ld-2.28.so)";
[%expect
{|
((Ok
((thread ((pid (3871580)) (tid (3871580)))) (time 4d23h47m8.52679923s)
(data
(Event_sample (location 0x7fca99943c60) (count 50) (name Branch_misses)))))) |}]
;;
let%expect_test "perf reports a garbage symbol offset" =
check
{| 25375/25375 4509191.343298468: 1 branches:uH: call 7f6fce0b71f4 [unknown] (foo.so) => 7ffd193838e0 memcpy@plt+0xffffffffff22f000 (foo.so)|};
[%expect
{|
((Ok
((thread ((pid (25375)) (tid (25375)))) (time 52d4h33m11.343298468s)
(data (Trace (kind Call) (src 0x7f6fce0b71f4) (dst 0x7ffd193838e0)))))) |}]
;;
let%expect_test "tr end async" =
check
{| 25375/25375 4509191.343298468: 1 branches:uH: tr end async 7f6fce0b71f4 [unknown] (foo.so) => 0 [unknown] ([unknown])|};
[%expect
{|
((Ok
((thread ((pid (25375)) (tid (25375)))) (time 52d4h33m11.343298468s)
(data
(Trace (trace_state_change End) (kind Async) (src 0x7f6fce0b71f4)
(dst 0x0)))))) |}]
;;
let%expect_test "perf ptwrite event" =
check
{| 2769074/2769293 2592496.569782106: 1 ptwrite: call IP: 0 payload: 0x1b5 0 55c7952ad2d0 [unknown] (foo.bin)|};
[%expect
{|
((Ok
((thread ((pid (2769074)) (tid (2769293)))) (time 30d8m16.569782106s)
(data (Ptwrite (location 0x55c7952ad2d0) (data 0x1b5)))))) |}]
;;
let%expect_test "perf ptwrite event with printable payload" =
check
{|2817453/2817483 2596547.813541321: 1 ptwrite: call IP: 0 payload: 0x62 b 0 613d946b42d0 [unknown] (foo.bin)|};
[%expect
{|
((Ok
((thread ((pid (2817453)) (tid (2817483)))) (time 30d1h15m47.813541321s)
(data (Ptwrite (location 0x613d946b42d0) (data 0x62)))))) |}]
;;
let%expect_test "perf ptwrite event with jcc instruction" =
check
{|3256341/3256760 2632746.069047397: 1 ptwrite: jcc IP: 0 payload: 0x5000000000061b2 0 577bcad80555 [unknown] (foo.bin)|};
[%expect
{|
((Ok
((thread ((pid (3256341)) (tid (3256760)))) (time 30d11h19m6.069047397s)
(data (Ptwrite (location 0x577bcad80555) (data 0x5000000000061b2)))))) |}]
;;
let%expect_test "perf ptwrite event without instruction" =
check
{|3285832/3286108 2634666.172476558: 1 ptwrite: IP: 0 payload: 0xa00000000000000 0 64d20fb10432 [unknown] (foo.bin)|};
[%expect
{|
((Ok
((thread ((pid (3285832)) (tid (3286108)))) (time 30d11h51m6.172476558s)
(data (Ptwrite (location 0x64d20fb10432) (data 0xa00000000000000)))))) |}]
;;
end)
;;
module For_testing = struct
let to_event = to_event
let split_line_pipe = split_line_pipe
end