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> |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 3 | #include "ordered-events.h" |
| 4 | #include "evlist.h" |
| 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 | |
| 60 | #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event)) |
| 61 | static struct ordered_event *alloc_event(struct ordered_events *oe) |
| 62 | { |
| 63 | struct list_head *cache = &oe->cache; |
| 64 | struct ordered_event *new = NULL; |
| 65 | |
| 66 | if (!list_empty(cache)) { |
| 67 | new = list_entry(cache->next, struct ordered_event, list); |
| 68 | list_del(&new->list); |
| 69 | } else if (oe->buffer) { |
| 70 | new = oe->buffer + oe->buffer_idx; |
| 71 | if (++oe->buffer_idx == MAX_SAMPLE_BUFFER) |
| 72 | oe->buffer = NULL; |
| 73 | } else if (oe->cur_alloc_size < oe->max_alloc_size) { |
| 74 | size_t size = MAX_SAMPLE_BUFFER * sizeof(*new); |
| 75 | |
| 76 | oe->buffer = malloc(size); |
| 77 | if (!oe->buffer) |
| 78 | return NULL; |
| 79 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 80 | pr("alloc size %" PRIu64 "B (+%zu), max %" PRIu64 "B\n", |
| 81 | oe->cur_alloc_size, size, oe->max_alloc_size); |
| 82 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 83 | oe->cur_alloc_size += size; |
| 84 | list_add(&oe->buffer->list, &oe->to_free); |
| 85 | |
| 86 | /* First entry is abused to maintain the to_free list. */ |
| 87 | oe->buffer_idx = 2; |
| 88 | new = oe->buffer + 1; |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 89 | } else { |
| 90 | pr("allocation limit reached %" PRIu64 "B\n", oe->max_alloc_size); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return new; |
| 94 | } |
| 95 | |
| 96 | struct ordered_event * |
| 97 | ordered_events__new(struct ordered_events *oe, u64 timestamp) |
| 98 | { |
| 99 | struct ordered_event *new; |
| 100 | |
| 101 | new = alloc_event(oe); |
| 102 | if (new) { |
| 103 | new->timestamp = timestamp; |
| 104 | queue_event(oe, new); |
| 105 | } |
| 106 | |
| 107 | return new; |
| 108 | } |
| 109 | |
| 110 | void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event) |
| 111 | { |
Jiri Olsa | fa4e5c6 | 2014-06-15 19:46:08 +0200 | [diff] [blame] | 112 | list_move(&event->list, &oe->cache); |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 113 | oe->nr_events--; |
| 114 | } |
| 115 | |
| 116 | static int __ordered_events__flush(struct perf_session *s, |
| 117 | struct perf_tool *tool) |
| 118 | { |
| 119 | struct ordered_events *oe = &s->ordered_events; |
| 120 | struct list_head *head = &oe->events; |
| 121 | struct ordered_event *tmp, *iter; |
| 122 | struct perf_sample sample; |
| 123 | u64 limit = oe->next_flush; |
| 124 | u64 last_ts = oe->last ? oe->last->timestamp : 0ULL; |
| 125 | bool show_progress = limit == ULLONG_MAX; |
| 126 | struct ui_progress prog; |
| 127 | int ret; |
| 128 | |
| 129 | if (!tool->ordered_events || !limit) |
| 130 | return 0; |
| 131 | |
| 132 | if (show_progress) |
| 133 | ui_progress__init(&prog, oe->nr_events, "Processing time ordered events..."); |
| 134 | |
| 135 | list_for_each_entry_safe(iter, tmp, head, list) { |
| 136 | if (session_done()) |
| 137 | return 0; |
| 138 | |
| 139 | if (iter->timestamp > limit) |
| 140 | break; |
| 141 | |
| 142 | ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample); |
| 143 | if (ret) |
| 144 | pr_err("Can't parse sample, err = %d\n", ret); |
| 145 | else { |
| 146 | ret = perf_session__deliver_event(s, iter->event, &sample, tool, |
| 147 | iter->file_offset); |
| 148 | if (ret) |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | ordered_events__delete(oe, iter); |
| 153 | oe->last_flush = iter->timestamp; |
| 154 | |
| 155 | if (show_progress) |
| 156 | ui_progress__update(&prog, 1); |
| 157 | } |
| 158 | |
| 159 | if (list_empty(head)) |
| 160 | oe->last = NULL; |
| 161 | else if (last_ts <= limit) |
| 162 | oe->last = list_entry(head->prev, struct ordered_event, list); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int ordered_events__flush(struct perf_session *s, struct perf_tool *tool, |
| 168 | enum oe_flush how) |
| 169 | { |
| 170 | struct ordered_events *oe = &s->ordered_events; |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 171 | static const char * const str[] = { |
Jiri Olsa | b0a4520 | 2014-06-12 09:50:11 +0200 | [diff] [blame^] | 172 | "NONE", |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 173 | "FINAL", |
| 174 | "ROUND", |
| 175 | "HALF ", |
| 176 | }; |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 177 | int err; |
| 178 | |
| 179 | switch (how) { |
| 180 | case OE_FLUSH__FINAL: |
| 181 | oe->next_flush = ULLONG_MAX; |
| 182 | break; |
| 183 | |
| 184 | case OE_FLUSH__HALF: |
| 185 | { |
| 186 | struct ordered_event *first, *last; |
| 187 | struct list_head *head = &oe->events; |
| 188 | |
| 189 | first = list_entry(head->next, struct ordered_event, list); |
| 190 | last = oe->last; |
| 191 | |
| 192 | /* Warn if we are called before any event got allocated. */ |
| 193 | if (WARN_ONCE(!last || list_empty(head), "empty queue")) |
| 194 | return 0; |
| 195 | |
| 196 | oe->next_flush = first->timestamp; |
| 197 | oe->next_flush += (last->timestamp - first->timestamp) / 2; |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | case OE_FLUSH__ROUND: |
Jiri Olsa | b0a4520 | 2014-06-12 09:50:11 +0200 | [diff] [blame^] | 202 | case OE_FLUSH__NONE: |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 203 | default: |
| 204 | break; |
| 205 | }; |
| 206 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 207 | pr_oe_time(oe->next_flush, "next_flush - ordered_events__flush PRE %s, nr_events %u\n", |
| 208 | str[how], oe->nr_events); |
| 209 | pr_oe_time(oe->max_timestamp, "max_timestamp\n"); |
| 210 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 211 | err = __ordered_events__flush(s, tool); |
| 212 | |
| 213 | if (!err) { |
| 214 | if (how == OE_FLUSH__ROUND) |
| 215 | oe->next_flush = oe->max_timestamp; |
Jiri Olsa | b0a4520 | 2014-06-12 09:50:11 +0200 | [diff] [blame^] | 216 | |
| 217 | oe->last_flush_type = how; |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 218 | } |
| 219 | |
Jiri Olsa | cee3ab9 | 2014-07-11 14:49:54 +0200 | [diff] [blame] | 220 | pr_oe_time(oe->next_flush, "next_flush - ordered_events__flush POST %s, nr_events %u\n", |
| 221 | str[how], oe->nr_events); |
| 222 | pr_oe_time(oe->last_flush, "last_flush\n"); |
| 223 | |
Jiri Olsa | 5f86b80 | 2014-08-01 13:02:58 -0300 | [diff] [blame] | 224 | return err; |
| 225 | } |
Jiri Olsa | 36522f5 | 2014-06-10 22:47:40 +0200 | [diff] [blame] | 226 | |
| 227 | void ordered_events__init(struct ordered_events *oe) |
| 228 | { |
| 229 | INIT_LIST_HEAD(&oe->events); |
| 230 | INIT_LIST_HEAD(&oe->cache); |
| 231 | INIT_LIST_HEAD(&oe->to_free); |
| 232 | oe->max_alloc_size = (u64) -1; |
| 233 | oe->cur_alloc_size = 0; |
| 234 | } |
Jiri Olsa | adc56ed | 2014-06-10 22:50:03 +0200 | [diff] [blame] | 235 | |
| 236 | void ordered_events__free(struct ordered_events *oe) |
| 237 | { |
| 238 | while (!list_empty(&oe->to_free)) { |
| 239 | struct ordered_event *event; |
| 240 | |
| 241 | event = list_entry(oe->to_free.next, struct ordered_event, list); |
| 242 | list_del(&event->list); |
| 243 | free(event); |
| 244 | } |
| 245 | } |