Skip to content

Commit 54bf53b

Browse files
zpp0acmel
authored andcommitted
perf session: Add option to copy events when queueing
When processing events the session code has an ordered samples queue which is used to time-sort events coming in across multiple mmaps. At a later point in time samples on the queue are flushed up to some timestamp at which point the event is actually processed. When analyzing events live (ie., record/analysis path in the same command) there is a race that leads to corrupted events and parse errors which cause perf to terminate. The problem is that when the event is placed in the ordered samples queue it is only a reference to the event which is really sitting in the mmap buffer. Even though the event is queued for later processing the mmap tail pointer is updated which indicates to the kernel that the event has been processed. The race is flushing the event from the queue before it gets overwritten by some other event. For commands trying to process events live (versus just writing to a file) and processing a high rate of events this leads to parse failures and perf terminates. Examples hitting this problem are 'perf kvm stat live', especially with nested VMs which generate 100,000+ traces per second, and a command processing scheduling events with a high rate of context switching -- e.g., running 'perf bench sched pipe'. This patch offers live commands an option to copy the event when it is placed in the ordered samples queue. Based on a patch from David Ahern <[email protected]> Signed-off-by: Alexander Yarygin <[email protected]> Acked-by: Jiri Olsa <[email protected]> Cc: Christian Borntraeger <[email protected]> Cc: David Ahern <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 96355f2 commit 54bf53b

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

tools/perf/util/ordered-events.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <linux/list.h>
22
#include <linux/compiler.h>
3+
#include <linux/string.h>
34
#include "ordered-events.h"
45
#include "evlist.h"
56
#include "session.h"
@@ -57,11 +58,45 @@ static void queue_event(struct ordered_events *oe, struct ordered_event *new)
5758
}
5859
}
5960

61+
static union perf_event *__dup_event(struct ordered_events *oe,
62+
union perf_event *event)
63+
{
64+
union perf_event *new_event = NULL;
65+
66+
if (oe->cur_alloc_size < oe->max_alloc_size) {
67+
new_event = memdup(event, event->header.size);
68+
if (new_event)
69+
oe->cur_alloc_size += event->header.size;
70+
}
71+
72+
return new_event;
73+
}
74+
75+
static union perf_event *dup_event(struct ordered_events *oe,
76+
union perf_event *event)
77+
{
78+
return oe->copy_on_queue ? __dup_event(oe, event) : event;
79+
}
80+
81+
static void free_dup_event(struct ordered_events *oe, union perf_event *event)
82+
{
83+
if (oe->copy_on_queue) {
84+
oe->cur_alloc_size -= event->header.size;
85+
free(event);
86+
}
87+
}
88+
6089
#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event))
61-
static struct ordered_event *alloc_event(struct ordered_events *oe)
90+
static struct ordered_event *alloc_event(struct ordered_events *oe,
91+
union perf_event *event)
6292
{
6393
struct list_head *cache = &oe->cache;
6494
struct ordered_event *new = NULL;
95+
union perf_event *new_event;
96+
97+
new_event = dup_event(oe, event);
98+
if (!new_event)
99+
return NULL;
65100

66101
if (!list_empty(cache)) {
67102
new = list_entry(cache->next, struct ordered_event, list);
@@ -74,8 +109,10 @@ static struct ordered_event *alloc_event(struct ordered_events *oe)
74109
size_t size = MAX_SAMPLE_BUFFER * sizeof(*new);
75110

76111
oe->buffer = malloc(size);
77-
if (!oe->buffer)
112+
if (!oe->buffer) {
113+
free_dup_event(oe, new_event);
78114
return NULL;
115+
}
79116

80117
pr("alloc size %" PRIu64 "B (+%zu), max %" PRIu64 "B\n",
81118
oe->cur_alloc_size, size, oe->max_alloc_size);
@@ -90,15 +127,17 @@ static struct ordered_event *alloc_event(struct ordered_events *oe)
90127
pr("allocation limit reached %" PRIu64 "B\n", oe->max_alloc_size);
91128
}
92129

130+
new->event = new_event;
93131
return new;
94132
}
95133

96134
struct ordered_event *
97-
ordered_events__new(struct ordered_events *oe, u64 timestamp)
135+
ordered_events__new(struct ordered_events *oe, u64 timestamp,
136+
union perf_event *event)
98137
{
99138
struct ordered_event *new;
100139

101-
new = alloc_event(oe);
140+
new = alloc_event(oe, event);
102141
if (new) {
103142
new->timestamp = timestamp;
104143
queue_event(oe, new);
@@ -111,6 +150,7 @@ void ordered_events__delete(struct ordered_events *oe, struct ordered_event *eve
111150
{
112151
list_move(&event->list, &oe->cache);
113152
oe->nr_events--;
153+
free_dup_event(oe, event->event);
114154
}
115155

116156
static int __ordered_events__flush(struct perf_session *s,
@@ -240,6 +280,7 @@ void ordered_events__free(struct ordered_events *oe)
240280

241281
event = list_entry(oe->to_free.next, struct ordered_event, list);
242282
list_del(&event->list);
283+
free_dup_event(oe, event->event);
243284
free(event);
244285
}
245286
}

tools/perf/util/ordered-events.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ struct ordered_events {
3434
int buffer_idx;
3535
unsigned int nr_events;
3636
enum oe_flush last_flush_type;
37+
bool copy_on_queue;
3738
};
3839

39-
struct ordered_event *ordered_events__new(struct ordered_events *oe, u64 timestamp);
40+
struct ordered_event *ordered_events__new(struct ordered_events *oe, u64 timestamp,
41+
union perf_event *event);
4042
void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event);
4143
int ordered_events__flush(struct perf_session *s, struct perf_tool *tool,
4244
enum oe_flush how);
@@ -48,4 +50,10 @@ void ordered_events__set_alloc_size(struct ordered_events *oe, u64 size)
4850
{
4951
oe->max_alloc_size = size;
5052
}
53+
54+
static inline
55+
void ordered_events__set_copy_on_queue(struct ordered_events *oe, bool copy)
56+
{
57+
oe->copy_on_queue = copy;
58+
}
5159
#endif /* __ORDERED_EVENTS_H */

tools/perf/util/session.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,17 +532,16 @@ int perf_session_queue_event(struct perf_session *s, union perf_event *event,
532532
return -EINVAL;
533533
}
534534

535-
new = ordered_events__new(oe, timestamp);
535+
new = ordered_events__new(oe, timestamp, event);
536536
if (!new) {
537537
ordered_events__flush(s, tool, OE_FLUSH__HALF);
538-
new = ordered_events__new(oe, timestamp);
538+
new = ordered_events__new(oe, timestamp, event);
539539
}
540540

541541
if (!new)
542542
return -ENOMEM;
543543

544544
new->file_offset = file_offset;
545-
new->event = event;
546545
return 0;
547546
}
548547

0 commit comments

Comments
 (0)