All of lore.kernel.org
 help / color / mirror / Atom feed
From: James Morse <james.morse@arm.com>
To: Jan Bolke <jan.bolke@synopsys.com>,
	Andrew Murray <andrew.murray@arm.com>
Cc: "kvmarm@lists.cs.columbia.edu" <kvmarm@lists.cs.columbia.edu>
Subject: Re: Instruction/Cycle Counting in Guest Using the Kvm PMU
Date: Fri, 23 Nov 2018 17:36:22 +0000	[thread overview]
Message-ID: <fff7692d-117d-9119-1ff7-8b9bf5369c24@arm.com> (raw)
In-Reply-To: <0e9adc4e-20e3-5648-3ef6-64c9e16f780d@arm.com>

Hi Jan,

On 23/11/2018 12:29, James Morse wrote:
> On 23/11/2018 09:36, Jan Bolke wrote:
>> I am using the Kvm Api and try to integrate it as an instruction set simulator
>> in a SystemC environment.
> 
> 
>> I need some mechanism to count executed instructions in the guest (or cycles).
>>
>> Currently I am trying to use the emulated PMU cycle counter in the guest to get
>> the number of executed cycles in the guest.
>>
>> I am working on Arm64 and use Linux Kernel 4.14.33.
>>
>> I create the PMU device without creating a in-kernel vgic.
> 
>> I configure the counter, then start the counter, execute 3 or 4 dummy
>> instructions and read the counter again and then exit the guest with an exit_mmio.
>>
>> I assumed the value should be a very small number, as the guest only executed a
>> few instructions.
> 
> (some of which are system register writes, which can take a long time)
> 
>> The thing is as I read the counter, the value is something like 2970 or 0
>> (changes in each run).

> You are missing some barriers in your assembly snippet. 0 is a good indication
> that the code you wanted to measure escaped the measurement-window!

This would be true on bare-metal, but for KVM I'm talking rubbish. Turns out KVM
traps all these registers, and emulates the lot.

kvm_arm_setup_debug() sets MDCR_EL2.TPM so almost all these accesses trap,
effectively putting an ISB after each one.


>> So to me it looks like the counter is also counting the cycles for instruction
>> emulation in the host, am I right?
> 
> I'd assume not, but I don't know anything about the PMU.
> Andrew Murray posted a series[0] that did some stuff with starting/stopping the
> the counters around the guest, but I think that was just for the host making
> measurements of itself, or the guest.

> It might be easier to count instructions from outside the guest using perf. I
> think Andrew's series is making that more reliable.

>> Is it possible to just count the cycles in the guest from the guests’s point of
>> view?

This is what KVM is doing, by emulating the PMU with perf.
kvm_pmu_get_counter_value() looks like it should be doing the right thing...

As a sanity check, does the cycle counter work on your test machine?:
| perf stat -e cycles -- ls



Thanks,

James

      parent reply	other threads:[~2018-11-23 17:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-23  9:36 Instruction/Cycle Counting in Guest Using the Kvm PMU Jan Bolke
2018-11-23 12:29 ` James Morse
2018-11-23 13:27   ` Andrew Murray
2018-11-29 11:30     ` Jan Bolke
2018-11-30  0:23       ` Andrew Murray
2018-11-23 17:36   ` James Morse [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fff7692d-117d-9119-1ff7-8b9bf5369c24@arm.com \
    --to=james.morse@arm.com \
    --cc=andrew.murray@arm.com \
    --cc=jan.bolke@synopsys.com \
    --cc=kvmarm@lists.cs.columbia.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.