2011-06-17 13:37:24

by Joerg Roedel

[permalink] [raw]
Subject: [PATCH 0/5] perf support for amd guest/host-only bits v2

Hi,

this is the second version of the patch-set to support the AMD
guest-/host only bits in the performance counter MSRs. Due to lack of
time I havn't looked into emulating support for this feature on Intel or
other architectures, but the other comments should be worked in. The
changes to v1 include:

* Rebased patches to v3.0-rc3
* Allow exclude_guest and exclude_host set at the same time
* Reworked event-parse logic for the new exclude-bits
* Only count guest-events per default from perf-kvm

Thats it, unless I forgot something. Any feedback appreciated :-)

Thanks,

Joerg

Diffstat:

arch/x86/include/asm/perf_event.h | 3 +++
arch/x86/kernel/cpu/perf_event_amd.c | 13 +++++++++++++
include/linux/perf_event.h | 5 ++++-
tools/perf/builtin-kvm.c | 5 +++--
tools/perf/util/event.c | 8 ++++++++
tools/perf/util/event.h | 2 ++
tools/perf/util/evlist.c | 5 ++++-
tools/perf/util/parse-events.c | 15 +++++++++++++--
8 files changed, 50 insertions(+), 6 deletions(-)

Shortlog:

Joerg Roedel (5):
perf, core: Introduce attrs to count in either host or guest mode
perf, amd: Use GO/HO bits in perf-ctr
perf, tools: Add support for guest/host-only profiling
perf, tools: Fix copy&paste error in perf-kvm option description
perf, tools: Do guest-only counting in perf-kvm by default


2011-06-17 13:37:19

by Joerg Roedel

[permalink] [raw]
Subject: [PATCH 1/5] perf, core: Introduce attrs to count in either host or guest mode

The two new attributes exclude_guest and exclude_host can
bes used by user-space to tell the kernel to setup
performance counter to either only count while the CPU is in
guest or in host mode.
An additional check is also introduced to make sure
user-space does not try to exclude guest and host mode from
counting.

Signed-off-by: Joerg Roedel <[email protected]>
---
include/linux/perf_event.h | 5 ++++-
1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index e0786e3..9a27b5d 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -219,7 +219,10 @@ struct perf_event_attr {
mmap_data : 1, /* non-exec mmap data */
sample_id_all : 1, /* sample_type all events */

- __reserved_1 : 45;
+ exclude_host : 1, /* don't count in host */
+ exclude_guest : 1, /* don't count in guest */
+
+ __reserved_1 : 43;

union {
__u32 wakeup_events; /* wakeup every n events */
--
1.7.4.1

2011-06-17 13:37:29

by Joerg Roedel

[permalink] [raw]
Subject: [PATCH 2/5] perf, amd: Use GO/HO bits in perf-ctr

The AMD perf-counters support counting in guest or host-mode
only. Make use of that feature when user-space specified
guest/host-mode only counting.

Signed-off-by: Joerg Roedel <[email protected]>
---
arch/x86/include/asm/perf_event.h | 3 +++
arch/x86/kernel/cpu/perf_event_amd.c | 13 +++++++++++++
2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index d9d4dae..34047c2 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -29,6 +29,9 @@
#define ARCH_PERFMON_EVENTSEL_INV (1ULL << 23)
#define ARCH_PERFMON_EVENTSEL_CMASK 0xFF000000ULL

+#define AMD_PERFMON_EVENTSEL_GUESTONLY (1ULL << 40)
+#define AMD_PERFMON_EVENTSEL_HOSTONLY (1ULL << 41)
+
#define AMD64_EVENTSEL_EVENT \
(ARCH_PERFMON_EVENTSEL_EVENT | (0x0FULL << 32))
#define INTEL_ARCH_EVENT_MASK \
diff --git a/arch/x86/kernel/cpu/perf_event_amd.c b/arch/x86/kernel/cpu/perf_event_amd.c
index fe29c1d..66809e7 100644
--- a/arch/x86/kernel/cpu/perf_event_amd.c
+++ b/arch/x86/kernel/cpu/perf_event_amd.c
@@ -118,6 +118,19 @@ static int amd_pmu_hw_config(struct perf_event *event)
if (ret)
return ret;

+ if (event->attr.exclude_host && event->attr.exclude_guest)
+ /*
+ * When HO == GO == 1 the hardware treats that as GO == HO == 0
+ * and will count in both modes. We don't want to count in that
+ * case so we emulate no-counting by setting US = OS = 0.
+ */
+ event->hw.config &= ~(ARCH_PERFMON_EVENTSEL_USR |
+ ARCH_PERFMON_EVENTSEL_OS);
+ else if (event->attr.exclude_host)
+ event->hw.config |= AMD_PERFMON_EVENTSEL_GUESTONLY;
+ else if (event->attr.exclude_guest)
+ event->hw.config |= AMD_PERFMON_EVENTSEL_HOSTONLY;
+
if (event->attr.type != PERF_TYPE_RAW)
return 0;

--
1.7.4.1

2011-06-17 13:37:15

by Joerg Roedel

[permalink] [raw]
Subject: [PATCH 3/5] perf, tools: Add support for guest/host-only profiling

To restrict a counter to either host or guest mode this
patch introduces two new event modifiers: G and H.
With G the counter is configured in guest-only mode and with
H in host-only mode.

Signed-off-by: Joerg Roedel <[email protected]>
---
tools/perf/util/parse-events.c | 14 ++++++++++++--
1 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 41982c3..8bfb8de 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -728,8 +728,8 @@ static int
parse_event_modifier(const char **strp, struct perf_event_attr *attr)
{
const char *str = *strp;
- int exclude = 0;
- int eu = 0, ek = 0, eh = 0, precise = 0;
+ int exclude = 0, exclude_GH = 0;
+ int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;

if (!*str)
return 0;
@@ -753,6 +753,14 @@ parse_event_modifier(const char **strp, struct perf_event_attr *attr)
if (!exclude)
exclude = eu = ek = eh = 1;
eh = 0;
+ } else if (*str == 'G') {
+ if (!exclude_GH)
+ exclude_GH = eG = eH = 1;
+ eG = 0;
+ } else if (*str == 'H') {
+ if (!exclude_GH)
+ exclude_GH = eG = eH = 1;
+ eH = 0;
} else if (*str == 'p') {
precise++;
} else
@@ -769,6 +777,8 @@ parse_event_modifier(const char **strp, struct perf_event_attr *attr)
attr->exclude_kernel = ek;
attr->exclude_hv = eh;
attr->precise_ip = precise;
+ attr->exclude_host = eH;
+ attr->exclude_guest = eG;

return 0;
}
--
1.7.4.1

2011-06-17 13:38:26

by Joerg Roedel

[permalink] [raw]
Subject: [PATCH 4/5] perf, tools: Fix copy&paste error in perf-kvm option description

The --host option certainly enables host-data collection.

Signed-off-by: Joerg Roedel <[email protected]>
---
tools/perf/builtin-kvm.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index 34d1e85..032324a 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -38,7 +38,7 @@ static const struct option kvm_options[] = {
OPT_BOOLEAN(0, "guest", &perf_guest,
"Collect guest os data"),
OPT_BOOLEAN(0, "host", &perf_host,
- "Collect guest os data"),
+ "Collect host os data"),
OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
"guest mount directory under which every guest os"
" instance has a subdir"),
--
1.7.4.1

2011-06-17 13:37:10

by Joerg Roedel

[permalink] [raw]
Subject: [PATCH 5/5] perf, tools: Do guest-only counting in perf-kvm by default

Make use of exclude_guest and exlude_host in perf-kvm to do
only guest-only counting by default.

Signed-off-by: Joerg Roedel <[email protected]>
---
tools/perf/builtin-kvm.c | 3 ++-
tools/perf/util/event.c | 8 ++++++++
tools/perf/util/event.h | 2 ++
tools/perf/util/evlist.c | 5 ++++-
tools/perf/util/parse-events.c | 1 +
5 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index 032324a..9b05afa 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -107,7 +107,8 @@ static int __cmd_buildid_list(int argc, const char **argv)

int cmd_kvm(int argc, const char **argv, const char *prefix __used)
{
- perf_host = perf_guest = 0;
+ perf_host = 0;
+ perf_guest = 1;

argc = parse_options(argc, argv, kvm_options, kvm_usage,
PARSE_OPT_STOP_AT_NON_OPTION);
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 3c1b8a6..8262819 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -44,6 +44,14 @@ static struct perf_sample synth_sample = {
.period = 1,
};

+void event_attr_init(struct perf_event_attr *attr)
+{
+ if (!perf_host)
+ attr->exclude_host = 1;
+ if (!perf_guest)
+ attr->exclude_guest = 1;
+}
+
static pid_t perf_event__synthesize_comm(union perf_event *event, pid_t pid,
int full, perf_event__handler_t process,
struct perf_session *session)
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index 1d7f664..376f67e 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -150,6 +150,8 @@ typedef int (*perf_event__handler_t)(union perf_event *event,
struct perf_sample *sample,
struct perf_session *session);

+void event_attr_init(struct perf_event_attr *attr);
+
int perf_event__synthesize_thread_map(struct thread_map *threads,
perf_event__handler_t process,
struct perf_session *session);
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index b021ea9..46d4ca7 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -82,8 +82,11 @@ int perf_evlist__add_default(struct perf_evlist *evlist)
.type = PERF_TYPE_HARDWARE,
.config = PERF_COUNT_HW_CPU_CYCLES,
};
- struct perf_evsel *evsel = perf_evsel__new(&attr, 0);
+ struct perf_evsel *evsel;
+
+ event_attr_init(&attr);

+ evsel = perf_evsel__new(&attr, 0);
if (evsel == NULL)
return -ENOMEM;

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 8bfb8de..1c97b4b 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -842,6 +842,7 @@ int parse_events(const struct option *opt, const char *str, int unset __used)
for (;;) {
ostr = str;
memset(&attr, 0, sizeof(attr));
+ event_attr_init(&attr);
ret = parse_event_symbols(opt, &str, &attr);
if (ret == EVT_FAILED)
return -1;
--
1.7.4.1

2011-06-19 09:02:16

by Avi Kivity

[permalink] [raw]
Subject: Re: [PATCH 0/5] perf support for amd guest/host-only bits v2

On 06/17/2011 04:37 PM, Joerg Roedel wrote:
> Hi,
>
> this is the second version of the patch-set to support the AMD
> guest-/host only bits in the performance counter MSRs. Due to lack of
> time I havn't looked into emulating support for this feature on Intel or
> other architectures, but the other comments should be worked in. The
> changes to v1 include:

I'll be happy to add Intel support, it's needed for my guest pmu
patchset anyway.

--
error compiling committee.c: too many arguments to function

2011-06-28 16:11:00

by Joerg Roedel

[permalink] [raw]
Subject: Re: [PATCH 0/5] perf support for amd guest/host-only bits v2

On Fri, Jun 17, 2011 at 03:37:29PM +0200, Joerg Roedel wrote:
> this is the second version of the patch-set to support the AMD
> guest-/host only bits in the performance counter MSRs. Due to lack of
> time I havn't looked into emulating support for this feature on Intel or
> other architectures, but the other comments should be worked in. The
> changes to v1 include:
>
> * Rebased patches to v3.0-rc3
> * Allow exclude_guest and exclude_host set at the same time
> * Reworked event-parse logic for the new exclude-bits
> * Only count guest-events per default from perf-kvm

Hi Peter, Ingo,

have you had a chance to look at this patch-set? Are any changes
required?

Regards,
Joerg

2011-06-29 09:04:26

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH 0/5] perf support for amd guest/host-only bits v2

On Tue, 2011-06-28 at 18:10 +0200, Joerg Roedel wrote:
> On Fri, Jun 17, 2011 at 03:37:29PM +0200, Joerg Roedel wrote:
> > this is the second version of the patch-set to support the AMD
> > guest-/host only bits in the performance counter MSRs. Due to lack of
> > time I havn't looked into emulating support for this feature on Intel or
> > other architectures, but the other comments should be worked in. The
> > changes to v1 include:
> >
> > * Rebased patches to v3.0-rc3
> > * Allow exclude_guest and exclude_host set at the same time
> > * Reworked event-parse logic for the new exclude-bits
> > * Only count guest-events per default from perf-kvm
>
> Hi Peter, Ingo,
>
> have you had a chance to look at this patch-set? Are any changes
> required?

I would feel a lot more comfortable by having it implemented on all of
x86 as well as at least one !x86 platform. Avi graciously volunteered
for the Intel bits.

Paulus, I hear from benh that you're also responsible for the ppc-kvm
bits, could you possibly find some time to implement this feature for
ppc?

2011-06-29 09:28:17

by Avi Kivity

[permalink] [raw]
Subject: Re: [PATCH 0/5] perf support for amd guest/host-only bits v2

On 06/29/2011 12:02 PM, Peter Zijlstra wrote:
> >
> > have you had a chance to look at this patch-set? Are any changes
> > required?
>
> I would feel a lot more comfortable by having it implemented on all of
> x86 as well as at least one !x86 platform. Avi graciously volunteered
> for the Intel bits.

Silly me. Joerg, can you post the git tree publicly please?

--
error compiling committee.c: too many arguments to function

2011-06-29 09:51:37

by Joerg Roedel

[permalink] [raw]
Subject: Re: [PATCH 0/5] perf support for amd guest/host-only bits v2

On Wed, Jun 29, 2011 at 05:02:54AM -0400, Peter Zijlstra wrote:
> On Tue, 2011-06-28 at 18:10 +0200, Joerg Roedel wrote:
> > On Fri, Jun 17, 2011 at 03:37:29PM +0200, Joerg Roedel wrote:
> > > this is the second version of the patch-set to support the AMD
> > > guest-/host only bits in the performance counter MSRs. Due to lack of
> > > time I havn't looked into emulating support for this feature on Intel or
> > > other architectures, but the other comments should be worked in. The
> > > changes to v1 include:
> > >
> > > * Rebased patches to v3.0-rc3
> > > * Allow exclude_guest and exclude_host set at the same time
> > > * Reworked event-parse logic for the new exclude-bits
> > > * Only count guest-events per default from perf-kvm
> >
> > Hi Peter, Ingo,
> >
> > have you had a chance to look at this patch-set? Are any changes
> > required?
>
> I would feel a lot more comfortable by having it implemented on all of
> x86 as well as at least one !x86 platform. Avi graciously volunteered
> for the Intel bits.

Ok, since no changes are required from my side then, how about adding
support for more hardware successively like it was done for perf-kvm?

Joerg

--
AMD Operating System Research Center

Advanced Micro Devices GmbH Einsteinring 24 85609 Dornach
General Managers: Alberto Bozzo, Andrew Bowd
Registration: Dornach, Landkr. Muenchen; Registerger. Muenchen, HRB Nr. 43632

2011-06-29 09:48:05

by Joerg Roedel

[permalink] [raw]
Subject: Re: [PATCH 0/5] perf support for amd guest/host-only bits v2

On Wed, Jun 29, 2011 at 12:27:48PM +0300, Avi Kivity wrote:
> On 06/29/2011 12:02 PM, Peter Zijlstra wrote:
>> >
>> > have you had a chance to look at this patch-set? Are any changes
>> > required?
>>
>> I would feel a lot more comfortable by having it implemented on all of
>> x86 as well as at least one !x86 platform. Avi graciously volunteered
>> for the Intel bits.
>
> Silly me. Joerg, can you post the git tree publicly please?

Okay, I pushed it to

git://git.kernel.org/pub/scm/linux/kernel/git/joro/linux-2.6-kvm.git perf-guest-counting

It probably takes some time until it appears on the mirrors.

Thanks,

Joerg

2011-06-29 12:06:04

by Paul Mackerras

[permalink] [raw]
Subject: Re: [PATCH 0/5] perf support for amd guest/host-only bits v2

On Wed, Jun 29, 2011 at 11:02:54AM +0200, Peter Zijlstra wrote:
> On Tue, 2011-06-28 at 18:10 +0200, Joerg Roedel wrote:
> > On Fri, Jun 17, 2011 at 03:37:29PM +0200, Joerg Roedel wrote:
> > > this is the second version of the patch-set to support the AMD
> > > guest-/host only bits in the performance counter MSRs. Due to lack of
> > > time I havn't looked into emulating support for this feature on Intel or
> > > other architectures, but the other comments should be worked in. The
> > > changes to v1 include:
> > >
> > > * Rebased patches to v3.0-rc3
> > > * Allow exclude_guest and exclude_host set at the same time
> > > * Reworked event-parse logic for the new exclude-bits
> > > * Only count guest-events per default from perf-kvm
> >
> > Hi Peter, Ingo,
> >
> > have you had a chance to look at this patch-set? Are any changes
> > required?
>
> I would feel a lot more comfortable by having it implemented on all of
> x86 as well as at least one !x86 platform. Avi graciously volunteered
> for the Intel bits.
>
> Paulus, I hear from benh that you're also responsible for the ppc-kvm
> bits, could you possibly find some time to implement this feature for
> ppc?

I'll have a look at it, but I don't know how quickly I'll be able to
produce a patch.

We have two styles of KVM on PowerPC (at least as far as server
processors are concerned), one where the guest runs entirely in
usermode and the privileged facilities are emulated, and another that
uses hypervisor mode in the host and can allow the guest to use
supervisor mode. In the latter case, the PMU is considered a guest
resource, that is, the hardware allows the guest to manipulate the PMU
directly, and PMU interrupts go directly to the guest. In that mode
it's not really possible to count or profile guest activity from the
host. There are some hypervisor-only counters in the PMU but they
have limited event selection compared to the counters available to the
guest.

Paul.