1 #include <linux/list.h>
2 #include <linux/compiler.h>
3 #include <linux/string.h>
4 #include "ordered-events.h"
10 #define pr_N(n, fmt, ...) \
11 eprintf(n, debug_ordered_events, fmt, ##__VA_ARGS__)
13 #define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
15 static void queue_event(struct ordered_events *oe, struct ordered_event *new)
17 struct ordered_event *last = oe->last;
18 u64 timestamp = new->timestamp;
24 pr_oe_time2(timestamp, "queue_event nr_events %u\n", oe->nr_events);
27 list_add(&new->list, &oe->events);
28 oe->max_timestamp = timestamp;
33 * last event might point to some random place in the list as it's
34 * the last queued event. We expect that the new event is close to
37 if (last->timestamp <= timestamp) {
38 while (last->timestamp <= timestamp) {
40 if (p == &oe->events) {
41 list_add_tail(&new->list, &oe->events);
42 oe->max_timestamp = timestamp;
45 last = list_entry(p, struct ordered_event, list);
47 list_add_tail(&new->list, &last->list);
49 while (last->timestamp > timestamp) {
51 if (p == &oe->events) {
52 list_add(&new->list, &oe->events);
55 last = list_entry(p, struct ordered_event, list);
57 list_add(&new->list, &last->list);
61 static union perf_event *__dup_event(struct ordered_events *oe,
62 union perf_event *event)
64 union perf_event *new_event = NULL;
66 if (oe->cur_alloc_size < oe->max_alloc_size) {
67 new_event = memdup(event, event->header.size);
69 oe->cur_alloc_size += event->header.size;
75 static union perf_event *dup_event(struct ordered_events *oe,
76 union perf_event *event)
78 return oe->copy_on_queue ? __dup_event(oe, event) : event;
81 static void free_dup_event(struct ordered_events *oe, union perf_event *event)
83 if (oe->copy_on_queue) {
84 oe->cur_alloc_size -= event->header.size;
89 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event))
90 static struct ordered_event *alloc_event(struct ordered_events *oe,
91 union perf_event *event)
93 struct list_head *cache = &oe->cache;
94 struct ordered_event *new = NULL;
95 union perf_event *new_event;
97 new_event = dup_event(oe, event);
101 if (!list_empty(cache)) {
102 new = list_entry(cache->next, struct ordered_event, list);
103 list_del(&new->list);
104 } else if (oe->buffer) {
105 new = oe->buffer + oe->buffer_idx;
106 if (++oe->buffer_idx == MAX_SAMPLE_BUFFER)
108 } else if (oe->cur_alloc_size < oe->max_alloc_size) {
109 size_t size = MAX_SAMPLE_BUFFER * sizeof(*new);
111 oe->buffer = malloc(size);
113 free_dup_event(oe, new_event);
117 pr("alloc size %" PRIu64 "B (+%zu), max %" PRIu64 "B\n",
118 oe->cur_alloc_size, size, oe->max_alloc_size);
120 oe->cur_alloc_size += size;
121 list_add(&oe->buffer->list, &oe->to_free);
123 /* First entry is abused to maintain the to_free list. */
125 new = oe->buffer + 1;
127 pr("allocation limit reached %" PRIu64 "B\n", oe->max_alloc_size);
130 new->event = new_event;
134 struct ordered_event *
135 ordered_events__new(struct ordered_events *oe, u64 timestamp,
136 union perf_event *event)
138 struct ordered_event *new;
140 new = alloc_event(oe, event);
142 new->timestamp = timestamp;
143 queue_event(oe, new);
149 void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event)
151 list_move(&event->list, &oe->cache);
153 free_dup_event(oe, event->event);
156 static int __ordered_events__flush(struct perf_session *s,
157 struct perf_tool *tool)
159 struct ordered_events *oe = &s->ordered_events;
160 struct list_head *head = &oe->events;
161 struct ordered_event *tmp, *iter;
162 struct perf_sample sample;
163 u64 limit = oe->next_flush;
164 u64 last_ts = oe->last ? oe->last->timestamp : 0ULL;
165 bool show_progress = limit == ULLONG_MAX;
166 struct ui_progress prog;
169 if (!tool->ordered_events || !limit)
173 ui_progress__init(&prog, oe->nr_events, "Processing time ordered events...");
175 list_for_each_entry_safe(iter, tmp, head, list) {
179 if (iter->timestamp > limit)
182 ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample);
184 pr_err("Can't parse sample, err = %d\n", ret);
186 ret = perf_session__deliver_event(s, iter->event, &sample, tool,
192 ordered_events__delete(oe, iter);
193 oe->last_flush = iter->timestamp;
196 ui_progress__update(&prog, 1);
199 if (list_empty(head))
201 else if (last_ts <= limit)
202 oe->last = list_entry(head->prev, struct ordered_event, list);
207 int ordered_events__flush(struct perf_session *s, struct perf_tool *tool,
210 struct ordered_events *oe = &s->ordered_events;
211 static const char * const str[] = {
220 case OE_FLUSH__FINAL:
221 oe->next_flush = ULLONG_MAX;
226 struct ordered_event *first, *last;
227 struct list_head *head = &oe->events;
229 first = list_entry(head->next, struct ordered_event, list);
232 /* Warn if we are called before any event got allocated. */
233 if (WARN_ONCE(!last || list_empty(head), "empty queue"))
236 oe->next_flush = first->timestamp;
237 oe->next_flush += (last->timestamp - first->timestamp) / 2;
241 case OE_FLUSH__ROUND:
247 pr_oe_time(oe->next_flush, "next_flush - ordered_events__flush PRE %s, nr_events %u\n",
248 str[how], oe->nr_events);
249 pr_oe_time(oe->max_timestamp, "max_timestamp\n");
251 err = __ordered_events__flush(s, tool);
254 if (how == OE_FLUSH__ROUND)
255 oe->next_flush = oe->max_timestamp;
257 oe->last_flush_type = how;
260 pr_oe_time(oe->next_flush, "next_flush - ordered_events__flush POST %s, nr_events %u\n",
261 str[how], oe->nr_events);
262 pr_oe_time(oe->last_flush, "last_flush\n");
267 void ordered_events__init(struct ordered_events *oe)
269 INIT_LIST_HEAD(&oe->events);
270 INIT_LIST_HEAD(&oe->cache);
271 INIT_LIST_HEAD(&oe->to_free);
272 oe->max_alloc_size = (u64) -1;
273 oe->cur_alloc_size = 0;
276 void ordered_events__free(struct ordered_events *oe)
278 while (!list_empty(&oe->to_free)) {
279 struct ordered_event *event;
281 event = list_entry(oe->to_free.next, struct ordered_event, list);
282 list_del(&event->list);
283 free_dup_event(oe, event->event);