1 From 3bccbe20f6d188ce7b00326e776b745cfd35b10a Mon Sep 17 00:00:00 2001
2 From: Adrian Hunter <adrian.hunter@intel.com>
3 Date: Wed, 28 Sep 2016 14:41:36 +0300
4 Subject: perf intel-pt: Fix MTC timestamp calculation for large MTC periods
6 From: Adrian Hunter <adrian.hunter@intel.com>
8 commit 3bccbe20f6d188ce7b00326e776b745cfd35b10a upstream.
10 The MTC packet provides a 8-bit slice of CTC which is related to TSC by
11 the TMA packet, however the TMA packet only provides the lower 16 bits
12 of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
13 provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
14 packet by copying the missing bits from the current MTC assuming the
15 least difference between the two, and that the current MTC comes after
18 Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
19 Cc: Jiri Olsa <jolsa@redhat.com>
20 Link: http://lkml.kernel.org/r/1475062896-22274-2-git-send-email-adrian.hunter@intel.com
21 Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
22 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
25 tools/perf/util/intel-pt-decoder/intel-pt-decoder.c | 36 ++++++++++++++++++++
26 1 file changed, 36 insertions(+)
28 --- a/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
29 +++ b/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
30 @@ -89,6 +89,7 @@ struct intel_pt_decoder {
34 + bool fixup_last_mtc;
38 @@ -584,10 +585,31 @@ struct intel_pt_calc_cyc_to_tsc_info {
39 uint64_t tsc_timestamp;
42 + bool fixup_last_mtc;
44 double cbr_cyc_to_tsc;
48 + * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
49 + * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
50 + * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
51 + * packet by copying the missing bits from the current MTC assuming the least
52 + * difference between the two, and that the current MTC comes after last_mtc.
54 +static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
57 + uint32_t first_missing_bit = 1U << (16 - mtc_shift);
58 + uint32_t mask = ~(first_missing_bit - 1);
60 + *last_mtc |= mtc & mask;
61 + if (*last_mtc >= mtc) {
62 + *last_mtc -= first_missing_bit;
67 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
69 struct intel_pt_decoder *decoder = pkt_info->decoder;
70 @@ -617,6 +639,11 @@ static int intel_pt_calc_cyc_cb(struct i
73 mtc = pkt_info->packet.payload;
74 + if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
75 + data->fixup_last_mtc = false;
76 + intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
79 if (mtc > data->last_mtc)
80 mtc_delta = mtc - data->last_mtc;
82 @@ -685,6 +712,7 @@ static int intel_pt_calc_cyc_cb(struct i
85 data->have_tma = true;
86 + data->fixup_last_mtc = true;
90 @@ -751,6 +779,7 @@ static void intel_pt_calc_cyc_to_tsc(str
91 .tsc_timestamp = decoder->tsc_timestamp,
92 .timestamp = decoder->timestamp,
93 .have_tma = decoder->have_tma,
94 + .fixup_last_mtc = decoder->fixup_last_mtc,
98 @@ -1241,6 +1270,7 @@ static void intel_pt_calc_tma(struct int
100 decoder->ctc_delta = 0;
101 decoder->have_tma = true;
102 + decoder->fixup_last_mtc = true;
103 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
104 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
106 @@ -1255,6 +1285,12 @@ static void intel_pt_calc_mtc_timestamp(
108 mtc = decoder->packet.payload;
110 + if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
111 + decoder->fixup_last_mtc = false;
112 + intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
113 + &decoder->last_mtc);
116 if (mtc > decoder->last_mtc)
117 mtc_delta = mtc - decoder->last_mtc;