Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 1 | #include <linux/list.h> |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 2 | #include <linux/compiler.h> |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 3 | #include <linux/string.h> |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 4 | #include "ordered-events.h" |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 5 | #include "session.h" |
| 6 | #include "asm/bug.h" |
| 7 | #include "debug.h" |
| 8 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 9 | #define pr_N(n, fmt, ...) \ |
| 10 | eprintf(n, debug_ordered_events, fmt, ##__VA_ARGS__) |
| 11 | |
| 12 | #define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__) |
| 13 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 14 | static void queue_event(struct ordered_events *oe, struct ordered_event *new) |
| 15 | { |
| 16 | struct ordered_event *last = oe->last; |
| 17 | u64 timestamp = new->timestamp; |
| 18 | struct list_head *p; |
| 19 | |
| 20 | ++oe->nr_events; |
| 21 | oe->last = new; |
| 22 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 23 | pr_oe_time2(timestamp, "queue_event nr_events %u\n", oe->nr_events); |
| 24 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 25 | if (!last) { |
| 26 | list_add(&new->list, &oe->events); |
| 27 | oe->max_timestamp = timestamp; |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * last event might point to some random place in the list as it's |
| 33 | * the last queued event. We expect that the new event is close to |
| 34 | * this. |
| 35 | */ |
| 36 | if (last->timestamp <= timestamp) { |
| 37 | while (last->timestamp <= timestamp) { |
| 38 | p = last->list.next; |
| 39 | if (p == &oe->events) { |
| 40 | list_add_tail(&new->list, &oe->events); |
| 41 | oe->max_timestamp = timestamp; |
| 42 | return; |
| 43 | } |
| 44 | last = list_entry(p, struct ordered_event, list); |
| 45 | } |
| 46 | list_add_tail(&new->list, &last->list); |
| 47 | } else { |
| 48 | while (last->timestamp > timestamp) { |
| 49 | p = last->list.prev; |
| 50 | if (p == &oe->events) { |
| 51 | list_add(&new->list, &oe->events); |
| 52 | return; |
| 53 | } |
| 54 | last = list_entry(p, struct ordered_event, list); |
| 55 | } |
| 56 | list_add(&new->list, &last->list); |
| 57 | } |
| 58 | } |
| 59 | |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 60 | static union perf_event *__dup_event(struct ordered_events *oe, |
| 61 | union perf_event *event) |
| 62 | { |
| 63 | union perf_event *new_event = NULL; |
| 64 | |
| 65 | if (oe->cur_alloc_size < oe->max_alloc_size) { |
| 66 | new_event = memdup(event, event->header.size); |
| 67 | if (new_event) |
| 68 | oe->cur_alloc_size += event->header.size; |
| 69 | } |
| 70 | |
| 71 | return new_event; |
| 72 | } |
| 73 | |
| 74 | static union perf_event *dup_event(struct ordered_events *oe, |
| 75 | union perf_event *event) |
| 76 | { |
| 77 | return oe->copy_on_queue ? __dup_event(oe, event) : event; |
| 78 | } |
| 79 | |
| 80 | static void free_dup_event(struct ordered_events *oe, union perf_event *event) |
| 81 | { |
| 82 | if (oe->copy_on_queue) { |
| 83 | oe->cur_alloc_size -= event->header.size; |
| 84 | free(event); |
| 85 | } |
| 86 | } |
| 87 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 88 | #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event)) |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 89 | static struct ordered_event *alloc_event(struct ordered_events *oe, |
| 90 | union perf_event *event) |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 91 | { |
| 92 | struct list_head *cache = &oe->cache; |
| 93 | struct ordered_event *new = NULL; |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 94 | union perf_event *new_event; |
| 95 | |
| 96 | new_event = dup_event(oe, event); |
| 97 | if (!new_event) |
| 98 | return NULL; |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 99 | |
| 100 | if (!list_empty(cache)) { |
| 101 | new = list_entry(cache->next, struct ordered_event, list); |
| 102 | list_del(&new->list); |
| 103 | } else if (oe->buffer) { |
| 104 | new = oe->buffer + oe->buffer_idx; |
| 105 | if (++oe->buffer_idx == MAX_SAMPLE_BUFFER) |
| 106 | oe->buffer = NULL; |
| 107 | } else if (oe->cur_alloc_size < oe->max_alloc_size) { |
| 108 | size_t size = MAX_SAMPLE_BUFFER * sizeof(*new); |
| 109 | |
| 110 | oe->buffer = malloc(size); |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 111 | if (!oe->buffer) { |
| 112 | free_dup_event(oe, new_event); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 113 | return NULL; |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 114 | } |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 115 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 116 | pr("alloc size %" PRIu64 "B (+%zu), max %" PRIu64 "B\n", |
| 117 | oe->cur_alloc_size, size, oe->max_alloc_size); |
| 118 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 119 | oe->cur_alloc_size += size; |
| 120 | list_add(&oe->buffer->list, &oe->to_free); |
| 121 | |
| 122 | /* First entry is abused to maintain the to_free list. */ |
| 123 | oe->buffer_idx = 2; |
| 124 | new = oe->buffer + 1; |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 125 | } else { |
| 126 | pr("allocation limit reached %" PRIu64 "B\n", oe->max_alloc_size); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 127 | } |
| 128 | |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 129 | new->event = new_event; |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 130 | return new; |
| 131 | } |
| 132 | |
Arnaldo Carvalho de Melo | 4a6b362 | 2015-03-03 13:02:24 -0300 | [diff] [blame] | 133 | static struct ordered_event * |
| 134 | ordered_events__new_event(struct ordered_events *oe, u64 timestamp, |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 135 | union perf_event *event) |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 136 | { |
| 137 | struct ordered_event *new; |
| 138 | |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 139 | new = alloc_event(oe, event); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 140 | if (new) { |
| 141 | new->timestamp = timestamp; |
| 142 | queue_event(oe, new); |
| 143 | } |
| 144 | |
| 145 | return new; |
| 146 | } |
| 147 | |
| 148 | void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event) |
| 149 | { |
Jiri Olsa | fa4e5c6 | 2014-06-15 19:46:08 +0200 | [diff] [blame] | 150 | list_move(&event->list, &oe->cache); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 151 | oe->nr_events--; |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 152 | free_dup_event(oe, event->event); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 153 | } |
| 154 | |
Arnaldo Carvalho de Melo | 4a6b362 | 2015-03-03 13:02:24 -0300 | [diff] [blame] | 155 | int ordered_events__queue(struct ordered_events *oe, union perf_event *event, |
| 156 | struct perf_sample *sample, u64 file_offset) |
| 157 | { |
| 158 | u64 timestamp = sample->time; |
| 159 | struct ordered_event *oevent; |
| 160 | |
| 161 | if (!timestamp || timestamp == ~0ULL) |
| 162 | return -ETIME; |
| 163 | |
| 164 | if (timestamp < oe->last_flush) { |
| 165 | pr_oe_time(timestamp, "out of order event\n"); |
| 166 | pr_oe_time(oe->last_flush, "last flush, last_flush_type %d\n", |
| 167 | oe->last_flush_type); |
| 168 | |
Arnaldo Carvalho de Melo | 9870d78 | 2015-03-31 12:48:16 -0300 | [diff] [blame] | 169 | oe->nr_unordered_events++; |
Arnaldo Carvalho de Melo | 4a6b362 | 2015-03-03 13:02:24 -0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | oevent = ordered_events__new_event(oe, timestamp, event); |
| 173 | if (!oevent) { |
| 174 | ordered_events__flush(oe, OE_FLUSH__HALF); |
| 175 | oevent = ordered_events__new_event(oe, timestamp, event); |
| 176 | } |
| 177 | |
| 178 | if (!oevent) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | oevent->file_offset = file_offset; |
| 182 | return 0; |
| 183 | } |
| 184 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 185 | static int __ordered_events__flush(struct ordered_events *oe) |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 186 | { |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 187 | struct list_head *head = &oe->events; |
| 188 | struct ordered_event *tmp, *iter; |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 189 | u64 limit = oe->next_flush; |
| 190 | u64 last_ts = oe->last ? oe->last->timestamp : 0ULL; |
| 191 | bool show_progress = limit == ULLONG_MAX; |
| 192 | struct ui_progress prog; |
| 193 | int ret; |
| 194 | |
Arnaldo Carvalho de Melo | 2808368 | 2015-02-22 13:52:47 -0800 | [diff] [blame] | 195 | if (!limit) |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 196 | return 0; |
| 197 | |
| 198 | if (show_progress) |
| 199 | ui_progress__init(&prog, oe->nr_events, "Processing time ordered events..."); |
| 200 | |
| 201 | list_for_each_entry_safe(iter, tmp, head, list) { |
| 202 | if (session_done()) |
| 203 | return 0; |
| 204 | |
| 205 | if (iter->timestamp > limit) |
| 206 | break; |
Arnaldo Carvalho de Melo | 9870d78 | 2015-03-31 12:48:16 -0300 | [diff] [blame] | 207 | ret = oe->deliver(oe, iter); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 208 | if (ret) |
Arnaldo Carvalho de Melo | 9870d78 | 2015-03-31 12:48:16 -0300 | [diff] [blame] | 209 | return ret; |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 210 | |
| 211 | ordered_events__delete(oe, iter); |
| 212 | oe->last_flush = iter->timestamp; |
| 213 | |
| 214 | if (show_progress) |
| 215 | ui_progress__update(&prog, 1); |
| 216 | } |
| 217 | |
| 218 | if (list_empty(head)) |
| 219 | oe->last = NULL; |
| 220 | else if (last_ts <= limit) |
| 221 | oe->last = list_entry(head->prev, struct ordered_event, list); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 226 | int ordered_events__flush(struct ordered_events *oe, enum oe_flush how) |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 227 | { |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 228 | static const char * const str[] = { |
Jiri Olsa | b0a4520 | 2014-06-12 09:50:11 +0200 | [diff] [blame] | 229 | "NONE", |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 230 | "FINAL", |
| 231 | "ROUND", |
| 232 | "HALF ", |
| 233 | }; |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 234 | int err; |
| 235 | |
Arnaldo Carvalho de Melo | 2808368 | 2015-02-22 13:52:47 -0800 | [diff] [blame] | 236 | if (oe->nr_events == 0) |
| 237 | return 0; |
| 238 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 239 | switch (how) { |
| 240 | case OE_FLUSH__FINAL: |
| 241 | oe->next_flush = ULLONG_MAX; |
| 242 | break; |
| 243 | |
| 244 | case OE_FLUSH__HALF: |
| 245 | { |
| 246 | struct ordered_event *first, *last; |
| 247 | struct list_head *head = &oe->events; |
| 248 | |
| 249 | first = list_entry(head->next, struct ordered_event, list); |
| 250 | last = oe->last; |
| 251 | |
| 252 | /* Warn if we are called before any event got allocated. */ |
| 253 | if (WARN_ONCE(!last || list_empty(head), "empty queue")) |
| 254 | return 0; |
| 255 | |
| 256 | oe->next_flush = first->timestamp; |
| 257 | oe->next_flush += (last->timestamp - first->timestamp) / 2; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | case OE_FLUSH__ROUND: |
Jiri Olsa | b0a4520 | 2014-06-12 09:50:11 +0200 | [diff] [blame] | 262 | case OE_FLUSH__NONE: |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 263 | default: |
| 264 | break; |
| 265 | }; |
| 266 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 267 | pr_oe_time(oe->next_flush, "next_flush - ordered_events__flush PRE %s, nr_events %u\n", |
| 268 | str[how], oe->nr_events); |
| 269 | pr_oe_time(oe->max_timestamp, "max_timestamp\n"); |
| 270 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 271 | err = __ordered_events__flush(oe); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 272 | |
| 273 | if (!err) { |
| 274 | if (how == OE_FLUSH__ROUND) |
| 275 | oe->next_flush = oe->max_timestamp; |
Jiri Olsa | b0a4520 | 2014-06-12 09:50:11 +0200 | [diff] [blame] | 276 | |
| 277 | oe->last_flush_type = how; |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 278 | } |
| 279 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 280 | pr_oe_time(oe->next_flush, "next_flush - ordered_events__flush POST %s, nr_events %u\n", |
| 281 | str[how], oe->nr_events); |
| 282 | pr_oe_time(oe->last_flush, "last_flush\n"); |
| 283 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 284 | return err; |
| 285 | } |
Jiri Olsa | 36522f5 | 2014-06-10 22:47:40 +0200 | [diff] [blame] | 286 | |
Arnaldo Carvalho de Melo | 9870d78 | 2015-03-31 12:48:16 -0300 | [diff] [blame] | 287 | void ordered_events__init(struct ordered_events *oe, ordered_events__deliver_t deliver) |
Jiri Olsa | 36522f5 | 2014-06-10 22:47:40 +0200 | [diff] [blame] | 288 | { |
| 289 | INIT_LIST_HEAD(&oe->events); |
| 290 | INIT_LIST_HEAD(&oe->cache); |
| 291 | INIT_LIST_HEAD(&oe->to_free); |
| 292 | oe->max_alloc_size = (u64) -1; |
| 293 | oe->cur_alloc_size = 0; |
Arnaldo Carvalho de Melo | d10eb1e | 2015-03-03 12:20:38 -0300 | [diff] [blame] | 294 | oe->deliver = deliver; |
Jiri Olsa | 36522f5 | 2014-06-10 22:47:40 +0200 | [diff] [blame] | 295 | } |
Jiri Olsa | adc56ed | 2014-06-10 22:50:03 +0200 | [diff] [blame] | 296 | |
| 297 | void ordered_events__free(struct ordered_events *oe) |
| 298 | { |
| 299 | while (!list_empty(&oe->to_free)) { |
| 300 | struct ordered_event *event; |
| 301 | |
| 302 | event = list_entry(oe->to_free.next, struct ordered_event, list); |
| 303 | list_del(&event->list); |
Alexander Yarygin | 54bf53b | 2014-10-03 18:40:11 +0400 | [diff] [blame] | 304 | free_dup_event(oe, event->event); |
Jiri Olsa | adc56ed | 2014-06-10 22:50:03 +0200 | [diff] [blame] | 305 | free(event); |
| 306 | } |
| 307 | } |