Skip to contentSkip to conversation

ara virt interface of perf to support kvm guest os statistics collection in guest os

PATCH In review Zhang, Yanmin 2 participants 6 messages Jun 21, 2010 – Jun 22, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 2/5 ara virt interface of perf to support kvm guest os statistics collection in guest os

The 2nd patch is to change the definition of perf_event to facilitate perf attr copy when a hypercall happens.

---

include/linux/perf_event.h +42 -7
@@ -188,7 +188,10 @@ struct perf_event_attr {
 	__u64			sample_type;
 	__u64			read_format;
 
-	__u64			disabled       :  1, /* off by default        */
+	union {
+		__u64		flags;
+		struct {
+			__u64	disabled       :  1, /* off by default        */
 				inherit	       :  1, /* children inherit it   */
 				pinned	       :  1, /* must always be on PMU */
 				exclusive      :  1, /* only group on PMU     */
@@ -217,6 +220,8 @@ struct perf_event_attr {
 				mmap_data      :  1, /* non-exec mmap data    */
 
 				__reserved_1   : 46;
+		};
+	};
 
 	union {
 		__u32		wakeup_events;	  /* wakeup every n events */
@@ -465,12 +470,6 @@ enum perf_callchain_context {
 # include <asm/local64.h>
 #endif
 
-struct perf_guest_info_callbacks {
-	int (*is_in_guest) (void);
-	int (*is_user_mode) (void);
-	unsigned long (*get_guest_ip) (void);
-};
-
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
 #include <asm/hw_breakpoint.h>
 #endif
@@ -753,6 +752,20 @@ struct perf_event {
 
 	perf_overflow_handler_t		overflow_handler;
 
+	/*
+	 * pointers used by kvm perf paravirt interface.
+	 *
+	 * 1) Used in host kernel and points to host_perf_shadow which
+	 * has information about guest perf_event
+	 */
+	void				*host_perf_shadow;
+	/*
+	 * 2) Used in guest kernel and points to guest_perf_shadow which
+	 * is used as a communication area with host kernel. Host kernel
+	 * copies overflow data to it when an event overflows.
+	 */
+	void				*guest_perf_shadow;
+
 #ifdef CONFIG_EVENT_TRACING
 	struct ftrace_event_call	*tp_event;
 	struct event_filter		*filter;
@@ -838,6 +851,16 @@ struct perf_output_handle {
 	int				sample;
 };
 
+struct perf_guest_info_callbacks {
+	/* Support collect guest statistics from host side */
+	int (*is_in_guest) (void);
+	int (*is_user_mode) (void);
+	unsigned long (*get_guest_ip) (void);
+
+	/* Support paravirt interface */
+	void (*copy_event_to_shadow) (struct perf_event *event, int overflows);
+};
+
 #ifdef CONFIG_PERF_EVENTS
 
 /*
@@ -871,6 +894,10 @@ perf_event_create_kernel_counter(struct 
 				perf_overflow_handler_t callback);
 extern u64 perf_event_read_value(struct perf_event *event,
 				 u64 *enabled, u64 *running);
+extern void perf_event_output(struct perf_event *event, int nmi,
+		struct perf_sample_data *data, struct pt_regs *regs);
+void perf_event_attach(struct perf_event *event);
+void perf_event_detach(struct perf_event *event);
 
 struct perf_sample_data {
 	u64				type;
@@ -1023,6 +1050,14 @@ perf_event_task_sched_in(struct task_str
 static inline void
 perf_event_task_sched_out(struct task_struct *task,
 			    struct task_struct *next)			{ }
+
+static inline void
+perf_event_output(struct perf_event *event, int nmi,
+		struct perf_sample_data *data, struct pt_regs *regs)	{ }
+
+static inline void perf_event_attach(struct perf_event *event)		{ }
+static inline void perf_event_detach(struct perf_event *event)		{ }
+
 static inline void
 perf_event_task_tick(struct task_struct *task)				{ }
 static inline int perf_event_init_task(struct task_struct *child)	{ return 0; }
kernel/watchdog.c +2 -2
@@ -197,8 +197,6 @@ static struct perf_event_attr wd_hw_attr
 	.type		= PERF_TYPE_HARDWARE,
 	.config		= PERF_COUNT_HW_CPU_CYCLES,
 	.size		= sizeof(struct perf_event_attr),
-	.pinned		= 1,
-	.disabled	= 1,
 };
 
 /* Callback function for perf event subsystem */
@@ -361,6 +359,8 @@ static int watchdog_nmi_enable(int cpu)
 	/* Try to register using hardware perf events */
 	wd_attr = &wd_hw_attr;
 	wd_attr->sample_period = hw_nmi_get_sample_period();
+	wd_attr->pinned = 1;
+	wd_attr->disabled = 1;
 	event = perf_event_create_kernel_counter(wd_attr, cpu, -1, watchdog_overflow_callback);
 	if (!IS_ERR(event)) {
 		printk(KERN_INFO "NMI watchdog enabled, takes one hw-pmu counter.\n");
kernel/perf_event.c +61 -4

101 lines not on this page · raw message

#1 lore raw
Quoted 10 lines · Yanmin

The 2nd patch is to change the definition of perf_event to facilitate perf attr copy when a hypercall happens.

---

include/linux/perf_event.h struct perf_event_attr { · line 191

Quoted 4 lines
include/linux/perf_event.h +0 -0
@@ -188,7 +188,10 @@ struct perf_event_attr {
  	__u64			sample_type;
  	__u64			read_format;
 
 

Assuming these flags are available to the guest?

include/linux/perf_event.h struct perf_event_attr {

Quoted 7 lines
include/linux/perf_event.h +4 -1
-	__u64			disabled       :  1, /* off by default        */
+	union {
+		__u64		flags;
+		struct {
+			__u64	disabled       :  1, /* off by default        */
  				inherit	       :  1, /* children inherit it   */
 

inherit is meaningless for a guest.

Quoted 2 lines

pinned : 1, /* must always be on PMU */

We cannot allow a guest to pin a counter.

The other flags are also problematic. I'd like to see virt-specific flags (probably we'll only need kernel/user and nested_hv for nested virtualization).

Something that is worrying is that we don't expose group information. perf will multiplex the events for us, but there will be a loss in accuracy.

Quoted 5 lines
  #ifdef CONFIG_HAVE_HW_BREAKPOINT
  #include<asm/hw_breakpoint.h>
  #endif
include/linux/perf_event.h +0 -0
@@ -753,6 +752,20 @@ struct perf_event {
 

include/linux/perf_event.h struct perf_event { · line 762

Quoted 10 lines
include/linux/perf_event.h +7 -0
  	perf_overflow_handler_t		overflow_handler;
 
+	/*
+	 * pointers used by kvm perf paravirt interface.
+	 *
+	 * 1) Used in host kernel and points to host_perf_shadow which
+	 * has information about guest perf_event
+	 */
+	void				*host_perf_shadow;
 

Can we have real types instead of void pointers?

include/linux/perf_event.h struct perf_event {

Quoted 7 lines
include/linux/perf_event.h +6 -0
+	/*
+	 * 2) Used in guest kernel and points to guest_perf_shadow which
+	 * is used as a communication area with host kernel. Host kernel
+	 * copies overflow data to it when an event overflows.
+	 */
+	void				*guest_perf_shadow;
 

It's strange to see both guest and host parts in the same patch. Splitting to separate patches will really help review.

Quoted 8 lines
include/linux/perf_event.h +1 -3
@@ -1626,9 +1629,22 @@ void perf_event_task_tick(struct task_st
  	if (ctx&&  ctx->nr_events&&  ctx->nr_events != ctx->nr_active)
  		rotate = 1;
 
-	perf_ctx_adjust_freq(&cpuctx->ctx);
-	if (ctx)
-		perf_ctx_adjust_freq(ctx);
+#ifdef CONFIG_KVM_PERF

include/linux/perf_event.h void perf_event_task_tick(struct task_st · line 1642

Quoted 10 lines
include/linux/perf_event.h +9 -0
+	if (kvm_para_available()) {
+		/*
+		 * perf_ctx_adjust_freq causes lots of pmu->read which would
+		 * trigger too many vmexit to host kernel. We disable it
+		 * under para virt situation
+		 */
+		adjust_freq = 0;
+	}
+#endif
 

Perhaps we can have a batch read interface which will read many counters at once. This would reduce the number of exits. Also adjust the frequency less frequently.

Quoted 8 lines
include/linux/perf_event.h +6 -0
+
+	if (adjust_freq) {
+		perf_ctx_adjust_freq(&cpuctx->ctx);
+		if (ctx)
+			perf_ctx_adjust_freq(ctx);
+	}
 
 
Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Yanmin

The 2nd patch is to change the definition of perf_event to facilitate perf attr copy when a hypercall happens.

---

Quoted 9 lines

include/linux/perf_event.h struct perf_event_attr { · line 191

Quoted 7 lines
include/linux/perf_event.h +0 -0
@@ -188,7 +188,10 @@ struct perf_event_attr {
  	__u64			sample_type;
  	__u64			read_format;
 
 

Assuming these flags are available to the guest?

These flags are used by generic perf codes. To match with host kernel, we wish guest os also use the flags.

Quoted 10 lines

include/linux/perf_event.h struct perf_event_attr {

Quoted 8 lines
include/linux/perf_event.h +4 -1
-	__u64			disabled       :  1, /* off by default        */
+	union {
+		__u64		flags;
+		struct {
+			__u64	disabled       :  1, /* off by default        */
  				inherit	       :  1, /* children inherit it   */
 

inherit is meaningless for a guest.

Right. host kernel will reset it to 0 before create perf_event for guest os. Here we couldn't delete the flag as it's used by perf generic codes.
I need separate the patch a little better. All definitions in include/linux/perf_event.h are mostly perf generic code related. I'm very careful to change it.

Quoted 5 lines
Quoted 3 lines

pinned : 1, /* must always be on PMU */

We cannot allow a guest to pin a counter.

Ok. I will reset it in function kvm_pv_perf_op_open.

Quoted 4 lines

The other flags are also problematic. I'd like to see virt-specific flags (probably we'll only need kernel/user and nested_hv for nested virtualization).

How about to add more comments around struct guest_perf_attr->flags that guest os developers should take a look at include/linux/perf_event.h? BTW, I will reset more flags to 0 in kvm_pv_perf_op_open.

Quoted 11 lines

Something that is worrying is that we don't expose group information. perf will multiplex the events for us, but there will be a loss in accuracy.

Quoted 7 lines
  #ifdef CONFIG_HAVE_HW_BREAKPOINT
  #include<asm/hw_breakpoint.h>
  #endif
include/linux/perf_event.h +0 -0
@@ -753,6 +752,20 @@ struct perf_event {
 
  	perf_overflow_handler_t		overflow_handler;
 
Quoted 10 lines

include/linux/perf_event.h struct perf_event {

Quoted 8 lines
include/linux/perf_event.h +7 -0
+	/*
+	 * pointers used by kvm perf paravirt interface.
+	 *
+	 * 1) Used in host kernel and points to host_perf_shadow which
+	 * has information about guest perf_event
+	 */
+	void				*host_perf_shadow;
 

Can we have real types instead of void pointers?

I just want perf generic codes have less dependency on KVM codes.

Quoted 11 lines

include/linux/perf_event.h struct perf_event {

Quoted 8 lines
include/linux/perf_event.h +6 -0
+	/*
+	 * 2) Used in guest kernel and points to guest_perf_shadow which
+	 * is used as a communication area with host kernel. Host kernel
+	 * copies overflow data to it when an event overflows.
+	 */
+	void				*guest_perf_shadow;
 

It's strange to see both guest and host parts in the same patch. Splitting to separate patches will really help review.

It's a little hard to split the patches if they change the same file. Perhaps I could add more statements before the patch when I send it out.

Quoted 14 lines
Quoted 14 lines
include/linux/perf_event.h +6 -3
@@ -1626,9 +1629,22 @@ void perf_event_task_tick(struct task_st
  	if (ctx&&  ctx->nr_events&&  ctx->nr_events != ctx->nr_active)
  		rotate = 1;
 
-	perf_ctx_adjust_freq(&cpuctx->ctx);
-	if (ctx)
-		perf_ctx_adjust_freq(ctx);
+#ifdef CONFIG_KVM_PERF
+	if (kvm_para_available()) {
+		/*
+		 * perf_ctx_adjust_freq causes lots of pmu->read which would
+		 * trigger too many vmexit to host kernel. We disable it
+		 * under para virt situation
Quoted 8 lines

include/linux/perf_event.h void perf_event_task_tick(struct task_st · line 1642

Quoted 5 lines
include/linux/perf_event.h +4 -0
+		 */
+		adjust_freq = 0;
+	}
+#endif
 

Perhaps we can have a batch read interface which will read many counters at once.

It's a good idea. But that will touch many perf generic codes which causes it's hard to maintain or follow future changes.

Quoted 2 lines

This would reduce the number of exits. Also adjust the
frequency less frequently.

Here it depends on process scheduler frequency, CONFIG_HZ.

Quoted 10 lines
Quoted 9 lines
include/linux/perf_event.h +6 -0
+
+	if (adjust_freq) {
+		perf_ctx_adjust_freq(&cpuctx->ctx);
+		if (ctx)
+			perf_ctx_adjust_freq(ctx);
+	}
 
 
#3 lore raw
Quoted 23 lines · Yanmin
Quoted 12 lines

Something that is worrying is that we don't expose group information. perf will multiplex the events for us, but there will be a loss in accuracy.

Quoted 7 lines
   #ifdef CONFIG_HAVE_HW_BREAKPOINT
   #include<asm/hw_breakpoint.h>
   #endif
include/linux/perf_event.h +0 -0
@@ -753,6 +752,20 @@ struct perf_event {
 
   	perf_overflow_handler_t		overflow_handler;
 
Quoted 11 lines

include/linux/perf_event.h struct perf_event {

Quoted 9 lines
include/linux/perf_event.h +7 -0
+	/*
+	 * pointers used by kvm perf paravirt interface.
+	 *
+	 * 1) Used in host kernel and points to host_perf_shadow which
+	 * has information about guest perf_event
+	 */
+	void				*host_perf_shadow;
 
 

Can we have real types instead of void pointers?

Quoted 2 lines

I just want perf generic codes have less dependency on KVM codes.

One way to do that and retain type safety is to have

    struct perf_client {
          struct perf_client_ops *ops;
          ...
    }

The client (kvm) can do

   struct kvm_perf_client {
         struct perf_client pc;
         // kvm specific stuff
   };

the callbacks receive struct perf_client and use container_of to reach the kvm_perf_client that contains it.

Quoted 11 lines
Quoted 11 lines

include/linux/perf_event.h struct perf_event {

Quoted 8 lines
include/linux/perf_event.h +6 -0
+	/*
+	 * 2) Used in guest kernel and points to guest_perf_shadow which
+	 * is used as a communication area with host kernel. Host kernel
+	 * copies overflow data to it when an event overflows.
+	 */
+	void				*guest_perf_shadow;
 

It's strange to see both guest and host parts in the same patch. Splitting to separate patches will really help review.

Quoted 3 lines

It's a little hard to split the patches if they change the same file. Perhaps I could add more statements before the patch when I send it out.

With git, it's easy (once you're used to it):

  # go back one commit:
  git reset HEAD^
  # selectively add bits:
  git add -p
  # commit first patch
  git commit -s
  # selectively add bits:
  git add -p
  # commit second patch
  git commit -s
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
kernel/perf_event.c +6 -3
@@ -1626,9 +1629,22 @@ void perf_event_task_tick(struct task_st
   	if (ctx&&   ctx->nr_events&&   ctx->nr_events != ctx->nr_active)
   		rotate = 1;
 
-	perf_ctx_adjust_freq(&cpuctx->ctx);
-	if (ctx)
-		perf_ctx_adjust_freq(ctx);
+#ifdef CONFIG_KVM_PERF
+	if (kvm_para_available()) {
+		/*
+		 * perf_ctx_adjust_freq causes lots of pmu->read which would
+		 * trigger too many vmexit to host kernel. We disable it
+		 * under para virt situation
Quoted 9 lines

kernel/perf_event.c void perf_event_task_tick(struct task_st

Quoted 6 lines
kernel/perf_event.c +4 -0
+		 */
+		adjust_freq = 0;
+	}
+#endif
 
 

Perhaps we can have a batch read interface which will read many counters at once.

Quoted 3 lines

It's a good idea. But that will touch many perf generic codes which causes it's hard to maintain or follow future changes.

I'm talking about the guest/host interface. So you have one vmexit and many host perf calls.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
Quoted 36 lines · Avi Kivity
Quoted 23 lines · Yanmin
Quoted 12 lines

Something that is worrying is that we don't expose group information. perf will multiplex the events for us, but there will be a loss in accuracy.

Quoted 7 lines
   #ifdef CONFIG_HAVE_HW_BREAKPOINT
   #include<asm/hw_breakpoint.h>
   #endif
include/linux/perf_event.h +0 -0
@@ -753,6 +752,20 @@ struct perf_event {
 
   	perf_overflow_handler_t		overflow_handler;
 
Quoted 11 lines

include/linux/perf_event.h struct perf_event {

Quoted 9 lines
include/linux/perf_event.h +7 -0
+	/*
+	 * pointers used by kvm perf paravirt interface.
+	 *
+	 * 1) Used in host kernel and points to host_perf_shadow which
+	 * has information about guest perf_event
+	 */
+	void				*host_perf_shadow;
 
 

Can we have real types instead of void pointers?

Quoted 2 lines

I just want perf generic codes have less dependency on KVM codes.

One way to do that and retain type safety is to have

     struct perf_client {
           struct perf_client_ops *ops;
           ...
     }

The client (kvm) can do

Quoted 7 lines
    struct kvm_perf_client {
          struct perf_client pc;
          // kvm specific stuff
    };

the callbacks receive struct perf_client and use container_of to reach the kvm_perf_client that contains it.

Let me double check it.

Quoted 18 lines
Quoted 12 lines
Quoted 12 lines

include/linux/perf_event.h struct perf_event {

Quoted 9 lines
include/linux/perf_event.h +6 -0
+	/*
+	 * 2) Used in guest kernel and points to guest_perf_shadow which
+	 * is used as a communication area with host kernel. Host kernel
+	 * copies overflow data to it when an event overflows.
+	 */
+	void				*guest_perf_shadow;
 

It's strange to see both guest and host parts in the same patch. Splitting to separate patches will really help review.

Quoted 3 lines

It's a little hard to split the patches if they change the same file. Perhaps I could add more statements before the patch when I send it out.

With git, it's easy (once you're used to it):

Quoted 10 lines
   # go back one commit:
   git reset HEAD^
   # selectively add bits:
   git add -p
   # commit first patch
   git commit -s
   # selectively add bits:
   git add -p
   # commit second patch
   git commit -s

Thanks for your teaching.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
kernel/perf_event.c +2 -3
@@ -1626,9 +1629,22 @@ void perf_event_task_tick(struct task_st
   	if (ctx&&   ctx->nr_events&&   ctx->nr_events != ctx->nr_active)
   		rotate = 1;
 
-	perf_ctx_adjust_freq(&cpuctx->ctx);
-	if (ctx)
-		perf_ctx_adjust_freq(ctx);
+#ifdef CONFIG_KVM_PERF
+	if (kvm_para_available()) {

kernel/perf_event.c void perf_event_task_tick(struct task_st · line 1643

Quoted 10 lines
kernel/perf_event.c +8 -0
+		/*
+		 * perf_ctx_adjust_freq causes lots of pmu->read which would
+		 * trigger too many vmexit to host kernel. We disable it
+		 * under para virt situation
+		 */
+		adjust_freq = 0;
+	}
+#endif
 
 

Perhaps we can have a batch read interface which will read many counters at once.

Quoted 6 lines
Quoted 3 lines

It's a good idea. But that will touch many perf generic codes which causes it's hard to maintain or follow future changes.

I'm talking about the guest/host interface. So you have one vmexit and many host perf calls.

I understood what you were speaking. I mean, perf generic codes operate perf_event one by one. At low layer, we just know one perf_event before calling hypercall to vmexit to host kernel.

#5 lore raw
Quoted 8 lines · Yanmin
Quoted 4 lines

I'm talking about the guest/host interface. So you have one vmexit and many host perf calls.

I understood what you were speaking. I mean, perf generic codes operate perf_event one by one. At low layer, we just know one perf_event before calling hypercall to vmexit to host kernel.

Ah.

We might fix that by having the perf ops work on guest memory, and add a commit that transfers them to the host. But this is complicated and can be left for later.

Signature · 1 line
error compiling committee.c: too many arguments to function