1 #include <linux/list.h>
2 #include "ordered-events.h"
8 static void queue_event(struct ordered_events *oe, struct ordered_event *new)
10 struct ordered_event *last = oe->last;
11 u64 timestamp = new->timestamp;
18 list_add(&new->list, &oe->events);
19 oe->max_timestamp = timestamp;
24 * last event might point to some random place in the list as it's
25 * the last queued event. We expect that the new event is close to
28 if (last->timestamp <= timestamp) {
29 while (last->timestamp <= timestamp) {
31 if (p == &oe->events) {
32 list_add_tail(&new->list, &oe->events);
33 oe->max_timestamp = timestamp;
36 last = list_entry(p, struct ordered_event, list);
38 list_add_tail(&new->list, &last->list);
40 while (last->timestamp > timestamp) {
42 if (p == &oe->events) {
43 list_add(&new->list, &oe->events);
46 last = list_entry(p, struct ordered_event, list);
48 list_add(&new->list, &last->list);
52 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event))
53 static struct ordered_event *alloc_event(struct ordered_events *oe)
55 struct list_head *cache = &oe->cache;
56 struct ordered_event *new = NULL;
58 if (!list_empty(cache)) {
59 new = list_entry(cache->next, struct ordered_event, list);
61 } else if (oe->buffer) {
62 new = oe->buffer + oe->buffer_idx;
63 if (++oe->buffer_idx == MAX_SAMPLE_BUFFER)
65 } else if (oe->cur_alloc_size < oe->max_alloc_size) {
66 size_t size = MAX_SAMPLE_BUFFER * sizeof(*new);
68 oe->buffer = malloc(size);
72 oe->cur_alloc_size += size;
73 list_add(&oe->buffer->list, &oe->to_free);
75 /* First entry is abused to maintain the to_free list. */
83 struct ordered_event *
84 ordered_events__new(struct ordered_events *oe, u64 timestamp)
86 struct ordered_event *new;
88 new = alloc_event(oe);
90 new->timestamp = timestamp;
97 void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event)
99 list_move(&event->list, &oe->cache);
103 static int __ordered_events__flush(struct perf_session *s,
104 struct perf_tool *tool)
106 struct ordered_events *oe = &s->ordered_events;
107 struct list_head *head = &oe->events;
108 struct ordered_event *tmp, *iter;
109 struct perf_sample sample;
110 u64 limit = oe->next_flush;
111 u64 last_ts = oe->last ? oe->last->timestamp : 0ULL;
112 bool show_progress = limit == ULLONG_MAX;
113 struct ui_progress prog;
116 if (!tool->ordered_events || !limit)
120 ui_progress__init(&prog, oe->nr_events, "Processing time ordered events...");
122 list_for_each_entry_safe(iter, tmp, head, list) {
126 if (iter->timestamp > limit)
129 ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample);
131 pr_err("Can't parse sample, err = %d\n", ret);
133 ret = perf_session__deliver_event(s, iter->event, &sample, tool,
139 ordered_events__delete(oe, iter);
140 oe->last_flush = iter->timestamp;
143 ui_progress__update(&prog, 1);
146 if (list_empty(head))
148 else if (last_ts <= limit)
149 oe->last = list_entry(head->prev, struct ordered_event, list);
154 int ordered_events__flush(struct perf_session *s, struct perf_tool *tool,
157 struct ordered_events *oe = &s->ordered_events;
161 case OE_FLUSH__FINAL:
162 oe->next_flush = ULLONG_MAX;
167 struct ordered_event *first, *last;
168 struct list_head *head = &oe->events;
170 first = list_entry(head->next, struct ordered_event, list);
173 /* Warn if we are called before any event got allocated. */
174 if (WARN_ONCE(!last || list_empty(head), "empty queue"))
177 oe->next_flush = first->timestamp;
178 oe->next_flush += (last->timestamp - first->timestamp) / 2;
182 case OE_FLUSH__ROUND:
187 err = __ordered_events__flush(s, tool);
190 if (how == OE_FLUSH__ROUND)
191 oe->next_flush = oe->max_timestamp;