Skip to content

Commit f6acb4a

Browse files
Leo Yangregkh
authored andcommitted
perf cs-etm: Correct synthesizing instruction samples
[ Upstream commit c9f5baa ] When 'etm->instructions_sample_period' is less than 'tidq->period_instructions', the function cs_etm__sample() cannot handle this case properly with its logic. Let's see below flow as an example: - If we set itrace option '--itrace=i4', then function cs_etm__sample() has variables with initialized values: tidq->period_instructions = 0 etm->instructions_sample_period = 4 - When the first packet is coming: packet->instr_count = 10; the number of instructions executed in this packet is 10, thus update period_instructions as below: tidq->period_instructions = 0 + 10 = 10 instrs_over = 10 - 4 = 6 offset = 10 - 6 - 1 = 3 tidq->period_instructions = instrs_over = 6 - When the second packet is coming: packet->instr_count = 10; in the second pass, assume 10 instructions in the trace sample again: tidq->period_instructions = 6 + 10 = 16 instrs_over = 16 - 4 = 12 offset = 10 - 12 - 1 = -3 -> the negative value tidq->period_instructions = instrs_over = 12 So after handle these two packets, there have below issues: The first issue is that cs_etm__instr_addr() returns the address within the current trace sample of the instruction related to offset, so the offset is supposed to be always unsigned value. But in fact, function cs_etm__sample() might calculate a negative offset value (in handling the second packet, the offset is -3) and pass to cs_etm__instr_addr() with u64 type with a big positive integer. The second issue is it only synthesizes 2 samples for sample period = 4. In theory, every packet has 10 instructions so the two packets have total 20 instructions, 20 instructions should generate 5 samples (4 x 5 = 20). This is because cs_etm__sample() only calls once cs_etm__synth_instruction_sample() to generate instruction sample per range packet. This patch fixes the logic in function cs_etm__sample(); the basic idea for handling coming packet is: - To synthesize the first instruction sample, it combines the left instructions from the previous packet and the head of the new packet; then generate continuous samples with sample period; - At the tail of the new packet, if it has the rest instructions, these instructions will be left for the sequential sample. Suggested-by: Mike Leach <mike.leach@linaro.org> Signed-off-by: Leo Yan <leo.yan@linaro.org> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org> Reviewed-by: Mike Leach <mike.leach@linaro.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Robert Walker <robert.walker@arm.com> Cc: Suzuki Poulouse <suzuki.poulose@arm.com> Cc: coresight ml <coresight@lists.linaro.org> Cc: linux-arm-kernel@lists.infradead.org Link: http://lore.kernel.org/lkml/20200219021811.20067-4-leo.yan@linaro.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f7ba2ee commit f6acb4a

1 file changed

Lines changed: 70 additions & 17 deletions

File tree

tools/perf/util/cs-etm.c

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,9 +1359,12 @@ static int cs_etm__sample(struct cs_etm_queue *etmq,
13591359
struct cs_etm_auxtrace *etm = etmq->etm;
13601360
int ret;
13611361
u8 trace_chan_id = tidq->trace_chan_id;
1362-
u64 instrs_executed = tidq->packet->instr_count;
1362+
u64 instrs_prev;
13631363

1364-
tidq->period_instructions += instrs_executed;
1364+
/* Get instructions remainder from previous packet */
1365+
instrs_prev = tidq->period_instructions;
1366+
1367+
tidq->period_instructions += tidq->packet->instr_count;
13651368

13661369
/*
13671370
* Record a branch when the last instruction in
@@ -1379,26 +1382,76 @@ static int cs_etm__sample(struct cs_etm_queue *etmq,
13791382
* TODO: allow period to be defined in cycles and clock time
13801383
*/
13811384

1382-
/* Get number of instructions executed after the sample point */
1383-
u64 instrs_over = tidq->period_instructions -
1384-
etm->instructions_sample_period;
1385+
/*
1386+
* Below diagram demonstrates the instruction samples
1387+
* generation flows:
1388+
*
1389+
* Instrs Instrs Instrs Instrs
1390+
* Sample(n) Sample(n+1) Sample(n+2) Sample(n+3)
1391+
* | | | |
1392+
* V V V V
1393+
* --------------------------------------------------
1394+
* ^ ^
1395+
* | |
1396+
* Period Period
1397+
* instructions(Pi) instructions(Pi')
1398+
*
1399+
* | |
1400+
* \---------------- -----------------/
1401+
* V
1402+
* tidq->packet->instr_count
1403+
*
1404+
* Instrs Sample(n...) are the synthesised samples occurring
1405+
* every etm->instructions_sample_period instructions - as
1406+
* defined on the perf command line. Sample(n) is being the
1407+
* last sample before the current etm packet, n+1 to n+3
1408+
* samples are generated from the current etm packet.
1409+
*
1410+
* tidq->packet->instr_count represents the number of
1411+
* instructions in the current etm packet.
1412+
*
1413+
* Period instructions (Pi) contains the the number of
1414+
* instructions executed after the sample point(n) from the
1415+
* previous etm packet. This will always be less than
1416+
* etm->instructions_sample_period.
1417+
*
1418+
* When generate new samples, it combines with two parts
1419+
* instructions, one is the tail of the old packet and another
1420+
* is the head of the new coming packet, to generate
1421+
* sample(n+1); sample(n+2) and sample(n+3) consume the
1422+
* instructions with sample period. After sample(n+3), the rest
1423+
* instructions will be used by later packet and it is assigned
1424+
* to tidq->period_instructions for next round calculation.
1425+
*/
13851426

13861427
/*
1387-
* Calculate the address of the sampled instruction (-1 as
1388-
* sample is reported as though instruction has just been
1389-
* executed, but PC has not advanced to next instruction)
1428+
* Get the initial offset into the current packet instructions;
1429+
* entry conditions ensure that instrs_prev is less than
1430+
* etm->instructions_sample_period.
13901431
*/
1391-
u64 offset = (instrs_executed - instrs_over - 1);
1392-
u64 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1393-
tidq->packet, offset);
1432+
u64 offset = etm->instructions_sample_period - instrs_prev;
1433+
u64 addr;
13941434

1395-
ret = cs_etm__synth_instruction_sample(
1396-
etmq, tidq, addr, etm->instructions_sample_period);
1397-
if (ret)
1398-
return ret;
1435+
while (tidq->period_instructions >=
1436+
etm->instructions_sample_period) {
1437+
/*
1438+
* Calculate the address of the sampled instruction (-1
1439+
* as sample is reported as though instruction has just
1440+
* been executed, but PC has not advanced to next
1441+
* instruction)
1442+
*/
1443+
addr = cs_etm__instr_addr(etmq, trace_chan_id,
1444+
tidq->packet, offset - 1);
1445+
ret = cs_etm__synth_instruction_sample(
1446+
etmq, tidq, addr,
1447+
etm->instructions_sample_period);
1448+
if (ret)
1449+
return ret;
13991450

1400-
/* Carry remaining instructions into next sample period */
1401-
tidq->period_instructions = instrs_over;
1451+
offset += etm->instructions_sample_period;
1452+
tidq->period_instructions -=
1453+
etm->instructions_sample_period;
1454+
}
14021455
}
14031456

14041457
if (etm->sample_branches) {

0 commit comments

Comments
 (0)