aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorAlexander Yarygin <yarygin@linux.vnet.ibm.com>2014-10-03 10:40:11 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2014-10-15 16:39:03 -0400
commit54bf53b1cb9150b894213a705c562d52388376ef (patch)
treeaa92017a60b2257c88d6b2da41a7cb562f341439 /tools
parent96355f2cfb61bb057df9bc0712cd7323dba28080 (diff)
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 <dsahern@gmail.com> Signed-off-by: Alexander Yarygin <yarygin@linux.vnet.ibm.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Namhyung Kim <namhyung.kim@lge.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/r/1412347212-28237-2-git-send-email-yarygin@linux.vnet.ibm.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/util/ordered-events.c49
-rw-r--r--tools/perf/util/ordered-events.h10
-rw-r--r--tools/perf/util/session.c5
3 files changed, 56 insertions, 8 deletions
diff --git a/tools/perf/util/ordered-events.c b/tools/perf/util/ordered-events.c
index 706ce1a66169..fd4be94125fb 100644
--- a/tools/perf/util/ordered-events.c
+++ b/tools/perf/util/ordered-events.c
@@ -1,5 +1,6 @@
1#include <linux/list.h> 1#include <linux/list.h>
2#include <linux/compiler.h> 2#include <linux/compiler.h>
3#include <linux/string.h>
3#include "ordered-events.h" 4#include "ordered-events.h"
4#include "evlist.h" 5#include "evlist.h"
5#include "session.h" 6#include "session.h"
@@ -57,11 +58,45 @@ static void queue_event(struct ordered_events *oe, struct ordered_event *new)
57 } 58 }
58} 59}
59 60
61static 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
75static 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
81static 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
60#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event)) 89#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event))
61static struct ordered_event *alloc_event(struct ordered_events *oe) 90static struct ordered_event *alloc_event(struct ordered_events *oe,
91 union perf_event *event)
62{ 92{
63 struct list_head *cache = &oe->cache; 93 struct list_head *cache = &oe->cache;
64 struct ordered_event *new = NULL; 94 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;
65 100
66 if (!list_empty(cache)) { 101 if (!list_empty(cache)) {
67 new = list_entry(cache->next, struct ordered_event, list); 102 new = list_entry(cache->next, struct ordered_event, list);
@@ -74,8 +109,10 @@ static struct ordered_event *alloc_event(struct ordered_events *oe)
74 size_t size = MAX_SAMPLE_BUFFER * sizeof(*new); 109 size_t size = MAX_SAMPLE_BUFFER * sizeof(*new);
75 110
76 oe->buffer = malloc(size); 111 oe->buffer = malloc(size);
77 if (!oe->buffer) 112 if (!oe->buffer) {
113 free_dup_event(oe, new_event);
78 return NULL; 114 return NULL;
115 }
79 116
80 pr("alloc size %" PRIu64 "B (+%zu), max %" PRIu64 "B\n", 117 pr("alloc size %" PRIu64 "B (+%zu), max %" PRIu64 "B\n",
81 oe->cur_alloc_size, size, oe->max_alloc_size); 118 oe->cur_alloc_size, size, oe->max_alloc_size);
@@ -90,15 +127,17 @@ static struct ordered_event *alloc_event(struct ordered_events *oe)
90 pr("allocation limit reached %" PRIu64 "B\n", oe->max_alloc_size); 127 pr("allocation limit reached %" PRIu64 "B\n", oe->max_alloc_size);
91 } 128 }
92 129
130 new->event = new_event;
93 return new; 131 return new;
94} 132}
95 133
96struct ordered_event * 134struct ordered_event *
97ordered_events__new(struct ordered_events *oe, u64 timestamp) 135ordered_events__new(struct ordered_events *oe, u64 timestamp,
136 union perf_event *event)
98{ 137{
99 struct ordered_event *new; 138 struct ordered_event *new;
100 139
101 new = alloc_event(oe); 140 new = alloc_event(oe, event);
102 if (new) { 141 if (new) {
103 new->timestamp = timestamp; 142 new->timestamp = timestamp;
104 queue_event(oe, new); 143 queue_event(oe, new);
@@ -111,6 +150,7 @@ void ordered_events__delete(struct ordered_events *oe, struct ordered_event *eve
111{ 150{
112 list_move(&event->list, &oe->cache); 151 list_move(&event->list, &oe->cache);
113 oe->nr_events--; 152 oe->nr_events--;
153 free_dup_event(oe, event->event);
114} 154}
115 155
116static int __ordered_events__flush(struct perf_session *s, 156static int __ordered_events__flush(struct perf_session *s,
@@ -240,6 +280,7 @@ void ordered_events__free(struct ordered_events *oe)
240 280
241 event = list_entry(oe->to_free.next, struct ordered_event, list); 281 event = list_entry(oe->to_free.next, struct ordered_event, list);
242 list_del(&event->list); 282 list_del(&event->list);
283 free_dup_event(oe, event->event);
243 free(event); 284 free(event);
244 } 285 }
245} 286}
diff --git a/tools/perf/util/ordered-events.h b/tools/perf/util/ordered-events.h
index 3b2f20542a01..7b8f9b011f38 100644
--- a/tools/perf/util/ordered-events.h
+++ b/tools/perf/util/ordered-events.h
@@ -34,9 +34,11 @@ struct ordered_events {
34 int buffer_idx; 34 int buffer_idx;
35 unsigned int nr_events; 35 unsigned int nr_events;
36 enum oe_flush last_flush_type; 36 enum oe_flush last_flush_type;
37 bool copy_on_queue;
37}; 38};
38 39
39struct ordered_event *ordered_events__new(struct ordered_events *oe, u64 timestamp); 40struct ordered_event *ordered_events__new(struct ordered_events *oe, u64 timestamp,
41 union perf_event *event);
40void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event); 42void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event);
41int ordered_events__flush(struct perf_session *s, struct perf_tool *tool, 43int ordered_events__flush(struct perf_session *s, struct perf_tool *tool,
42 enum oe_flush how); 44 enum oe_flush how);
@@ -48,4 +50,10 @@ void ordered_events__set_alloc_size(struct ordered_events *oe, u64 size)
48{ 50{
49 oe->max_alloc_size = size; 51 oe->max_alloc_size = size;
50} 52}
53
54static inline
55void ordered_events__set_copy_on_queue(struct ordered_events *oe, bool copy)
56{
57 oe->copy_on_queue = copy;
58}
51#endif /* __ORDERED_EVENTS_H */ 59#endif /* __ORDERED_EVENTS_H */
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 896bac73ea08..6702ac28754b 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -532,17 +532,16 @@ int perf_session_queue_event(struct perf_session *s, union perf_event *event,
532 return -EINVAL; 532 return -EINVAL;
533 } 533 }
534 534
535 new = ordered_events__new(oe, timestamp); 535 new = ordered_events__new(oe, timestamp, event);
536 if (!new) { 536 if (!new) {
537 ordered_events__flush(s, tool, OE_FLUSH__HALF); 537 ordered_events__flush(s, tool, OE_FLUSH__HALF);
538 new = ordered_events__new(oe, timestamp); 538 new = ordered_events__new(oe, timestamp, event);
539 } 539 }
540 540
541 if (!new) 541 if (!new)
542 return -ENOMEM; 542 return -ENOMEM;
543 543
544 new->file_offset = file_offset; 544 new->file_offset = file_offset;
545 new->event = event;
546 return 0; 545 return 0;
547} 546}
548 547