Commit 39447b38 authored by Zhang, Yanmin's avatar Zhang, Yanmin Committed by Avi Kivity

perf: Enhance perf to allow for guest statistic collection from host

Below patch introduces perf_guest_info_callbacks and related
register/unregister functions. Add more PERF_RECORD_MISC_XXX bits
meaning guest kernel and guest user space.
Signed-off-by: default avatarZhang Yanmin <yanmin_zhang@linux.intel.com>
Signed-off-by: default avatarAvi Kivity <avi@redhat.com>
parent b5a80b7e
...@@ -135,17 +135,10 @@ extern void perf_events_lapic_init(void); ...@@ -135,17 +135,10 @@ extern void perf_events_lapic_init(void);
*/ */
#define PERF_EFLAGS_EXACT (1UL << 3) #define PERF_EFLAGS_EXACT (1UL << 3)
#define perf_misc_flags(regs) \ struct pt_regs;
({ int misc = 0; \ extern unsigned long perf_instruction_pointer(struct pt_regs *regs);
if (user_mode(regs)) \ extern unsigned long perf_misc_flags(struct pt_regs *regs);
misc |= PERF_RECORD_MISC_USER; \ #define perf_misc_flags(regs) perf_misc_flags(regs)
else \
misc |= PERF_RECORD_MISC_KERNEL; \
if (regs->flags & PERF_EFLAGS_EXACT) \
misc |= PERF_RECORD_MISC_EXACT; \
misc; })
#define perf_instruction_pointer(regs) ((regs)->ip)
#else #else
static inline void init_hw_perf_events(void) { } static inline void init_hw_perf_events(void) { }
......
...@@ -1720,6 +1720,11 @@ struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) ...@@ -1720,6 +1720,11 @@ struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
{ {
struct perf_callchain_entry *entry; struct perf_callchain_entry *entry;
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
/* TODO: We don't support guest os callchain now */
return NULL;
}
if (in_nmi()) if (in_nmi())
entry = &__get_cpu_var(pmc_nmi_entry); entry = &__get_cpu_var(pmc_nmi_entry);
else else
...@@ -1743,3 +1748,29 @@ void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int ski ...@@ -1743,3 +1748,29 @@ void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int ski
regs->cs = __KERNEL_CS; regs->cs = __KERNEL_CS;
local_save_flags(regs->flags); local_save_flags(regs->flags);
} }
unsigned long perf_instruction_pointer(struct pt_regs *regs)
{
unsigned long ip;
if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
ip = perf_guest_cbs->get_guest_ip();
else
ip = instruction_pointer(regs);
return ip;
}
unsigned long perf_misc_flags(struct pt_regs *regs)
{
int misc = 0;
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
misc |= perf_guest_cbs->is_user_mode() ?
PERF_RECORD_MISC_GUEST_USER :
PERF_RECORD_MISC_GUEST_KERNEL;
} else
misc |= user_mode(regs) ? PERF_RECORD_MISC_USER :
PERF_RECORD_MISC_KERNEL;
if (regs->flags & PERF_EFLAGS_EXACT)
misc |= PERF_RECORD_MISC_EXACT;
return misc;
}
...@@ -288,11 +288,13 @@ struct perf_event_mmap_page { ...@@ -288,11 +288,13 @@ struct perf_event_mmap_page {
__u64 data_tail; /* user-space written tail */ __u64 data_tail; /* user-space written tail */
}; };
#define PERF_RECORD_MISC_CPUMODE_MASK (3 << 0) #define PERF_RECORD_MISC_CPUMODE_MASK (7 << 0)
#define PERF_RECORD_MISC_CPUMODE_UNKNOWN (0 << 0) #define PERF_RECORD_MISC_CPUMODE_UNKNOWN (0 << 0)
#define PERF_RECORD_MISC_KERNEL (1 << 0) #define PERF_RECORD_MISC_KERNEL (1 << 0)
#define PERF_RECORD_MISC_USER (2 << 0) #define PERF_RECORD_MISC_USER (2 << 0)
#define PERF_RECORD_MISC_HYPERVISOR (3 << 0) #define PERF_RECORD_MISC_HYPERVISOR (3 << 0)
#define PERF_RECORD_MISC_GUEST_KERNEL (4 << 0)
#define PERF_RECORD_MISC_GUEST_USER (5 << 0)
#define PERF_RECORD_MISC_EXACT (1 << 14) #define PERF_RECORD_MISC_EXACT (1 << 14)
/* /*
...@@ -446,6 +448,12 @@ enum perf_callchain_context { ...@@ -446,6 +448,12 @@ enum perf_callchain_context {
# include <asm/perf_event.h> # include <asm/perf_event.h>
#endif #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 #ifdef CONFIG_HAVE_HW_BREAKPOINT
#include <asm/hw_breakpoint.h> #include <asm/hw_breakpoint.h>
#endif #endif
...@@ -932,6 +940,12 @@ static inline void perf_event_mmap(struct vm_area_struct *vma) ...@@ -932,6 +940,12 @@ static inline void perf_event_mmap(struct vm_area_struct *vma)
__perf_event_mmap(vma); __perf_event_mmap(vma);
} }
extern struct perf_guest_info_callbacks *perf_guest_cbs;
extern int perf_register_guest_info_callbacks(
struct perf_guest_info_callbacks *);
extern int perf_unregister_guest_info_callbacks(
struct perf_guest_info_callbacks *);
extern void perf_event_comm(struct task_struct *tsk); extern void perf_event_comm(struct task_struct *tsk);
extern void perf_event_fork(struct task_struct *tsk); extern void perf_event_fork(struct task_struct *tsk);
...@@ -1001,6 +1015,11 @@ perf_sw_event(u32 event_id, u64 nr, int nmi, ...@@ -1001,6 +1015,11 @@ perf_sw_event(u32 event_id, u64 nr, int nmi,
static inline void static inline void
perf_bp_event(struct perf_event *event, void *data) { } perf_bp_event(struct perf_event *event, void *data) { }
static inline int perf_register_guest_info_callbacks
(struct perf_guest_info_callbacks *) {return 0; }
static inline int perf_unregister_guest_info_callbacks
(struct perf_guest_info_callbacks *) {return 0; }
static inline void perf_event_mmap(struct vm_area_struct *vma) { } static inline void perf_event_mmap(struct vm_area_struct *vma) { }
static inline void perf_event_comm(struct task_struct *tsk) { } static inline void perf_event_comm(struct task_struct *tsk) { }
static inline void perf_event_fork(struct task_struct *tsk) { } static inline void perf_event_fork(struct task_struct *tsk) { }
......
...@@ -2797,6 +2797,27 @@ void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int ski ...@@ -2797,6 +2797,27 @@ void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int ski
} }
/*
* We assume there is only KVM supporting the callbacks.
* Later on, we might change it to a list if there is
* another virtualization implementation supporting the callbacks.
*/
struct perf_guest_info_callbacks *perf_guest_cbs;
int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
{
perf_guest_cbs = cbs;
return 0;
}
EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
{
perf_guest_cbs = NULL;
return 0;
}
EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
/* /*
* Output * Output
*/ */
...@@ -3749,7 +3770,7 @@ void __perf_event_mmap(struct vm_area_struct *vma) ...@@ -3749,7 +3770,7 @@ void __perf_event_mmap(struct vm_area_struct *vma)
.event_id = { .event_id = {
.header = { .header = {
.type = PERF_RECORD_MMAP, .type = PERF_RECORD_MMAP,
.misc = 0, .misc = PERF_RECORD_MISC_USER,
/* .size */ /* .size */
}, },
/* .pid */ /* .pid */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment