Skip to content

Commit 60e2364

Browse files
Stephane EranianIngo Molnar
authored andcommitted
perf: Add ability to sample machine state on interrupt
Enable capture of interrupted machine state for each sample. Registers to sample are passed per event in the sample_regs_intr bitmask. To sample interrupt machine state, the PERF_SAMPLE_INTR_REGS must be passed in sample_type. The list of available registers is arch dependent and provided by asm/perf_regs.h Registers are laid out as u64 in the order of the bit order of sample_intr_regs. This patch also adds a new ABI version PERF_ATTR_SIZE_VER4 because we extend the perf_event_attr struct with a new u64 field. Reviewed-by: Jiri Olsa <[email protected]> Signed-off-by: Stephane Eranian <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: [email protected] Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent af4bdcf commit 60e2364

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

include/linux/perf_event.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct perf_branch_stack {
7979
struct perf_branch_entry entries[0];
8080
};
8181

82-
struct perf_regs_user {
82+
struct perf_regs {
8383
__u64 abi;
8484
struct pt_regs *regs;
8585
};
@@ -600,7 +600,8 @@ struct perf_sample_data {
600600
struct perf_callchain_entry *callchain;
601601
struct perf_raw_record *raw;
602602
struct perf_branch_stack *br_stack;
603-
struct perf_regs_user regs_user;
603+
struct perf_regs regs_user;
604+
struct perf_regs regs_intr;
604605
u64 stack_user_size;
605606
u64 weight;
606607
/*
@@ -630,6 +631,8 @@ static inline void perf_sample_data_init(struct perf_sample_data *data,
630631
data->weight = 0;
631632
data->data_src.val = PERF_MEM_NA;
632633
data->txn = 0;
634+
data->regs_intr.abi = PERF_SAMPLE_REGS_ABI_NONE;
635+
data->regs_intr.regs = NULL;
633636
}
634637

635638
extern void perf_output_sample(struct perf_output_handle *handle,

include/uapi/linux/perf_event.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ enum perf_event_sample_format {
137137
PERF_SAMPLE_DATA_SRC = 1U << 15,
138138
PERF_SAMPLE_IDENTIFIER = 1U << 16,
139139
PERF_SAMPLE_TRANSACTION = 1U << 17,
140+
PERF_SAMPLE_REGS_INTR = 1U << 18,
140141

141-
PERF_SAMPLE_MAX = 1U << 18, /* non-ABI */
142+
PERF_SAMPLE_MAX = 1U << 19, /* non-ABI */
142143
};
143144

144145
/*
@@ -238,6 +239,7 @@ enum perf_event_read_format {
238239
#define PERF_ATTR_SIZE_VER2 80 /* add: branch_sample_type */
239240
#define PERF_ATTR_SIZE_VER3 96 /* add: sample_regs_user */
240241
/* add: sample_stack_user */
242+
#define PERF_ATTR_SIZE_VER4 104 /* add: sample_regs_intr */
241243

242244
/*
243245
* Hardware event_id to monitor via a performance monitoring event:
@@ -334,6 +336,15 @@ struct perf_event_attr {
334336

335337
/* Align to u64. */
336338
__u32 __reserved_2;
339+
/*
340+
* Defines set of regs to dump for each sample
341+
* state captured on:
342+
* - precise = 0: PMU interrupt
343+
* - precise > 0: sampled instruction
344+
*
345+
* See asm/perf_regs.h for details.
346+
*/
347+
__u64 sample_regs_intr;
337348
};
338349

339350
#define perf_flags(attr) (*(&(attr)->read_format + 1))
@@ -686,6 +697,8 @@ enum perf_event_type {
686697
* { u64 weight; } && PERF_SAMPLE_WEIGHT
687698
* { u64 data_src; } && PERF_SAMPLE_DATA_SRC
688699
* { u64 transaction; } && PERF_SAMPLE_TRANSACTION
700+
* { u64 abi; # enum perf_sample_regs_abi
701+
* u64 regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
689702
* };
690703
*/
691704
PERF_RECORD_SAMPLE = 9,

kernel/events/core.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4460,7 +4460,7 @@ perf_output_sample_regs(struct perf_output_handle *handle,
44604460
}
44614461
}
44624462

4463-
static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4463+
static void perf_sample_regs_user(struct perf_regs *regs_user,
44644464
struct pt_regs *regs)
44654465
{
44664466
if (!user_mode(regs)) {
@@ -4476,6 +4476,14 @@ static void perf_sample_regs_user(struct perf_regs_user *regs_user,
44764476
}
44774477
}
44784478

4479+
static void perf_sample_regs_intr(struct perf_regs *regs_intr,
4480+
struct pt_regs *regs)
4481+
{
4482+
regs_intr->regs = regs;
4483+
regs_intr->abi = perf_reg_abi(current);
4484+
}
4485+
4486+
44794487
/*
44804488
* Get remaining task size from user stack pointer.
44814489
*
@@ -4857,6 +4865,23 @@ void perf_output_sample(struct perf_output_handle *handle,
48574865
if (sample_type & PERF_SAMPLE_TRANSACTION)
48584866
perf_output_put(handle, data->txn);
48594867

4868+
if (sample_type & PERF_SAMPLE_REGS_INTR) {
4869+
u64 abi = data->regs_intr.abi;
4870+
/*
4871+
* If there are no regs to dump, notice it through
4872+
* first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4873+
*/
4874+
perf_output_put(handle, abi);
4875+
4876+
if (abi) {
4877+
u64 mask = event->attr.sample_regs_intr;
4878+
4879+
perf_output_sample_regs(handle,
4880+
data->regs_intr.regs,
4881+
mask);
4882+
}
4883+
}
4884+
48604885
if (!event->attr.watermark) {
48614886
int wakeup_events = event->attr.wakeup_events;
48624887

@@ -4943,7 +4968,7 @@ void perf_prepare_sample(struct perf_event_header *header,
49434968
* in case new sample type is added, because we could eat
49444969
* up the rest of the sample size.
49454970
*/
4946-
struct perf_regs_user *uregs = &data->regs_user;
4971+
struct perf_regs *uregs = &data->regs_user;
49474972
u16 stack_size = event->attr.sample_stack_user;
49484973
u16 size = sizeof(u64);
49494974

@@ -4964,6 +4989,21 @@ void perf_prepare_sample(struct perf_event_header *header,
49644989
data->stack_user_size = stack_size;
49654990
header->size += size;
49664991
}
4992+
4993+
if (sample_type & PERF_SAMPLE_REGS_INTR) {
4994+
/* regs dump ABI info */
4995+
int size = sizeof(u64);
4996+
4997+
perf_sample_regs_intr(&data->regs_intr, regs);
4998+
4999+
if (data->regs_intr.regs) {
5000+
u64 mask = event->attr.sample_regs_intr;
5001+
5002+
size += hweight64(mask) * sizeof(u64);
5003+
}
5004+
5005+
header->size += size;
5006+
}
49675007
}
49685008

49695009
static void perf_event_output(struct perf_event *event,
@@ -7151,6 +7191,8 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
71517191
ret = -EINVAL;
71527192
}
71537193

7194+
if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
7195+
ret = perf_reg_validate(attr->sample_regs_intr);
71547196
out:
71557197
return ret;
71567198

0 commit comments

Comments
 (0)