Skip to content

Commit c980d10

Browse files
committed
perf events: Make sample_type identity fields available in all PERF_RECORD_ events
If perf_event_attr.sample_id_all is set it will add the PERF_SAMPLE_ identity info: TID, TIME, ID, CPU, STREAM_ID As a trailer, so that older perf tools can process new files, just ignoring the extra payload. With this its possible to do further analysis on problems in the event stream, like detecting reordering of MMAP and FORK events, etc. V2: Fixup header size in comm, mmap and task processing, as we have to take into account different sample_types for each matching event, noticed by Thomas Gleixner. Thomas also noticed a problem in v2 where if we didn't had space in the buffer we wouldn't restore the header size. Tested-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Ian Munsie <imunsie@au1.ibm.com> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Acked-by: Thomas Gleixner <tglx@linutronix.de> Cc: Frédéric Weisbecker <fweisbec@gmail.com> Cc: Ian Munsie <imunsie@au1.ibm.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Paul Mackerras <paulus@samba.org> Cc: Stephane Eranian <eranian@google.com> Cc: Thomas Gleixner <tglx@linutronix.de> LKML-Reference: <new-submission> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 6844c09 commit c980d10

2 files changed

Lines changed: 102 additions & 18 deletions

File tree

‎include/linux/perf_event.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ struct perf_event_attr {
215215
*/
216216
precise_ip : 2, /* skid constraint */
217217
mmap_data : 1, /* non-exec mmap data */
218+
sample_id_all : 1, /* sample_type all events */
218219

219-
__reserved_1 : 46;
220+
__reserved_1 : 45;
220221

221222
union {
222223
__u32 wakeup_events; /* wakeup every n events */
@@ -327,6 +328,15 @@ struct perf_event_header {
327328
enum perf_event_type {
328329

329330
/*
331+
* If perf_event_attr.sample_id_all is set then all event types will
332+
* have the sample_type selected fields related to where/when
333+
* (identity) an event took place (TID, TIME, ID, CPU, STREAM_ID)
334+
* described in PERF_RECORD_SAMPLE below, it will be stashed just after
335+
* the perf_event_header and the fields already present for the existing
336+
* fields, i.e. at the end of the payload. That way a newer perf.data
337+
* file will be supported by older perf tools, with these new optional
338+
* fields being ignored.
339+
*
330340
* The MMAP events record the PROT_EXEC mappings so that we can
331341
* correlate userspace IPs to code. They have the following structure:
332342
*

‎kernel/perf_event.c‎

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,9 +3388,9 @@ __always_inline void perf_output_copy(struct perf_output_handle *handle,
33883388
} while (len);
33893389
}
33903390

3391-
static void perf_event_header__init_id(struct perf_event_header *header,
3392-
struct perf_sample_data *data,
3393-
struct perf_event *event)
3391+
static void __perf_event_header__init_id(struct perf_event_header *header,
3392+
struct perf_sample_data *data,
3393+
struct perf_event *event)
33943394
{
33953395
u64 sample_type = event->attr.sample_type;
33963396

@@ -3418,13 +3418,51 @@ static void perf_event_header__init_id(struct perf_event_header *header,
34183418
}
34193419
}
34203420

3421+
static void perf_event_header__init_id(struct perf_event_header *header,
3422+
struct perf_sample_data *data,
3423+
struct perf_event *event)
3424+
{
3425+
if (event->attr.sample_id_all)
3426+
__perf_event_header__init_id(header, data, event);
3427+
}
3428+
3429+
static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3430+
struct perf_sample_data *data)
3431+
{
3432+
u64 sample_type = data->type;
3433+
3434+
if (sample_type & PERF_SAMPLE_TID)
3435+
perf_output_put(handle, data->tid_entry);
3436+
3437+
if (sample_type & PERF_SAMPLE_TIME)
3438+
perf_output_put(handle, data->time);
3439+
3440+
if (sample_type & PERF_SAMPLE_ID)
3441+
perf_output_put(handle, data->id);
3442+
3443+
if (sample_type & PERF_SAMPLE_STREAM_ID)
3444+
perf_output_put(handle, data->stream_id);
3445+
3446+
if (sample_type & PERF_SAMPLE_CPU)
3447+
perf_output_put(handle, data->cpu_entry);
3448+
}
3449+
3450+
static void perf_event__output_id_sample(struct perf_event *event,
3451+
struct perf_output_handle *handle,
3452+
struct perf_sample_data *sample)
3453+
{
3454+
if (event->attr.sample_id_all)
3455+
__perf_event__output_id_sample(handle, sample);
3456+
}
3457+
34213458
int perf_output_begin(struct perf_output_handle *handle,
34223459
struct perf_event *event, unsigned int size,
34233460
int nmi, int sample)
34243461
{
34253462
struct perf_buffer *buffer;
34263463
unsigned long tail, offset, head;
34273464
int have_lost;
3465+
struct perf_sample_data sample_data;
34283466
struct {
34293467
struct perf_event_header header;
34303468
u64 id;
@@ -3451,8 +3489,12 @@ int perf_output_begin(struct perf_output_handle *handle,
34513489
goto out;
34523490

34533491
have_lost = local_read(&buffer->lost);
3454-
if (have_lost)
3455-
size += sizeof(lost_event);
3492+
if (have_lost) {
3493+
lost_event.header.size = sizeof(lost_event);
3494+
perf_event_header__init_id(&lost_event.header, &sample_data,
3495+
event);
3496+
size += lost_event.header.size;
3497+
}
34563498

34573499
perf_output_get_handle(handle);
34583500

@@ -3483,11 +3525,11 @@ int perf_output_begin(struct perf_output_handle *handle,
34833525
if (have_lost) {
34843526
lost_event.header.type = PERF_RECORD_LOST;
34853527
lost_event.header.misc = 0;
3486-
lost_event.header.size = sizeof(lost_event);
34873528
lost_event.id = event->id;
34883529
lost_event.lost = local_xchg(&buffer->lost, 0);
34893530

34903531
perf_output_put(handle, lost_event);
3532+
perf_event__output_id_sample(event, handle, &sample_data);
34913533
}
34923534

34933535
return 0;
@@ -3700,7 +3742,7 @@ void perf_prepare_sample(struct perf_event_header *header,
37003742
header->misc = 0;
37013743
header->misc |= perf_misc_flags(regs);
37023744

3703-
perf_event_header__init_id(header, data, event);
3745+
__perf_event_header__init_id(header, data, event);
37043746

37053747
if (sample_type & PERF_SAMPLE_IP)
37063748
data->ip = perf_instruction_pointer(regs);
@@ -3768,6 +3810,7 @@ perf_event_read_event(struct perf_event *event,
37683810
struct task_struct *task)
37693811
{
37703812
struct perf_output_handle handle;
3813+
struct perf_sample_data sample;
37713814
struct perf_read_event read_event = {
37723815
.header = {
37733816
.type = PERF_RECORD_READ,
@@ -3779,12 +3822,14 @@ perf_event_read_event(struct perf_event *event,
37793822
};
37803823
int ret;
37813824

3825+
perf_event_header__init_id(&read_event.header, &sample, event);
37823826
ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
37833827
if (ret)
37843828
return;
37853829

37863830
perf_output_put(&handle, read_event);
37873831
perf_output_read(&handle, event);
3832+
perf_event__output_id_sample(event, &handle, &sample);
37883833

37893834
perf_output_end(&handle);
37903835
}
@@ -3814,14 +3859,16 @@ static void perf_event_task_output(struct perf_event *event,
38143859
struct perf_task_event *task_event)
38153860
{
38163861
struct perf_output_handle handle;
3862+
struct perf_sample_data sample;
38173863
struct task_struct *task = task_event->task;
3818-
int size, ret;
3864+
int ret, size = task_event->event_id.header.size;
38193865

3820-
size = task_event->event_id.header.size;
3821-
ret = perf_output_begin(&handle, event, size, 0, 0);
3866+
perf_event_header__init_id(&task_event->event_id.header, &sample, event);
38223867

3868+
ret = perf_output_begin(&handle, event,
3869+
task_event->event_id.header.size, 0, 0);
38233870
if (ret)
3824-
return;
3871+
goto out;
38253872

38263873
task_event->event_id.pid = perf_event_pid(event, task);
38273874
task_event->event_id.ppid = perf_event_pid(event, current);
@@ -3831,7 +3878,11 @@ static void perf_event_task_output(struct perf_event *event,
38313878

38323879
perf_output_put(&handle, task_event->event_id);
38333880

3881+
perf_event__output_id_sample(event, &handle, &sample);
3882+
38343883
perf_output_end(&handle);
3884+
out:
3885+
task_event->event_id.header.size = size;
38353886
}
38363887

38373888
static int perf_event_task_match(struct perf_event *event)
@@ -3944,19 +3995,29 @@ static void perf_event_comm_output(struct perf_event *event,
39443995
struct perf_comm_event *comm_event)
39453996
{
39463997
struct perf_output_handle handle;
3998+
struct perf_sample_data sample;
39473999
int size = comm_event->event_id.header.size;
3948-
int ret = perf_output_begin(&handle, event, size, 0, 0);
4000+
int ret;
4001+
4002+
perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4003+
ret = perf_output_begin(&handle, event,
4004+
comm_event->event_id.header.size, 0, 0);
39494005

39504006
if (ret)
3951-
return;
4007+
goto out;
39524008

39534009
comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
39544010
comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
39554011

39564012
perf_output_put(&handle, comm_event->event_id);
39574013
perf_output_copy(&handle, comm_event->comm,
39584014
comm_event->comm_size);
4015+
4016+
perf_event__output_id_sample(event, &handle, &sample);
4017+
39594018
perf_output_end(&handle);
4019+
out:
4020+
comm_event->event_id.header.size = size;
39604021
}
39614022

39624023
static int perf_event_comm_match(struct perf_event *event)
@@ -4001,7 +4062,6 @@ static void perf_event_comm_event(struct perf_comm_event *comm_event)
40014062
comm_event->comm_size = size;
40024063

40034064
comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4004-
40054065
rcu_read_lock();
40064066
list_for_each_entry_rcu(pmu, &pmus, entry) {
40074067
cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
@@ -4080,19 +4140,28 @@ static void perf_event_mmap_output(struct perf_event *event,
40804140
struct perf_mmap_event *mmap_event)
40814141
{
40824142
struct perf_output_handle handle;
4143+
struct perf_sample_data sample;
40834144
int size = mmap_event->event_id.header.size;
4084-
int ret = perf_output_begin(&handle, event, size, 0, 0);
4145+
int ret;
40854146

4147+
perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4148+
ret = perf_output_begin(&handle, event,
4149+
mmap_event->event_id.header.size, 0, 0);
40864150
if (ret)
4087-
return;
4151+
goto out;
40884152

40894153
mmap_event->event_id.pid = perf_event_pid(event, current);
40904154
mmap_event->event_id.tid = perf_event_tid(event, current);
40914155

40924156
perf_output_put(&handle, mmap_event->event_id);
40934157
perf_output_copy(&handle, mmap_event->file_name,
40944158
mmap_event->file_size);
4159+
4160+
perf_event__output_id_sample(event, &handle, &sample);
4161+
40954162
perf_output_end(&handle);
4163+
out:
4164+
mmap_event->event_id.header.size = size;
40964165
}
40974166

40984167
static int perf_event_mmap_match(struct perf_event *event,
@@ -4245,6 +4314,7 @@ void perf_event_mmap(struct vm_area_struct *vma)
42454314
static void perf_log_throttle(struct perf_event *event, int enable)
42464315
{
42474316
struct perf_output_handle handle;
4317+
struct perf_sample_data sample;
42484318
int ret;
42494319

42504320
struct {
@@ -4266,11 +4336,15 @@ static void perf_log_throttle(struct perf_event *event, int enable)
42664336
if (enable)
42674337
throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
42684338

4269-
ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4339+
perf_event_header__init_id(&throttle_event.header, &sample, event);
4340+
4341+
ret = perf_output_begin(&handle, event,
4342+
throttle_event.header.size, 1, 0);
42704343
if (ret)
42714344
return;
42724345

42734346
perf_output_put(&handle, throttle_event);
4347+
perf_event__output_id_sample(event, &handle, &sample);
42744348
perf_output_end(&handle);
42754349
}
42764350

0 commit comments

Comments
 (0)