blob: 48c8f609441b4c16b85f14645a8fef917d1a9ca3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Jiri Olsa5f86b802014-08-01 13:02:58 -03004#include <linux/list.h>
Jiri Olsacee3ab92014-07-11 14:49:54 +02005#include <linux/compiler.h>
Alexander Yarygin54bf53b2014-10-03 18:40:11 +04006#include <linux/string.h>
Jiri Olsa5f86b802014-08-01 13:02:58 -03007#include "ordered-events.h"
Jiri Olsa5f86b802014-08-01 13:02:58 -03008#include "session.h"
9#include "asm/bug.h"
10#include "debug.h"
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030011#include "ui/progress.h"
Jiri Olsa5f86b802014-08-01 13:02:58 -030012
Jiri Olsacee3ab92014-07-11 14:49:54 +020013#define pr_N(n, fmt, ...) \
14 eprintf(n, debug_ordered_events, fmt, ##__VA_ARGS__)
15
16#define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
17
Jiri Olsa5f86b802014-08-01 13:02:58 -030018static void queue_event(struct ordered_events *oe, struct ordered_event *new)
19{
20 struct ordered_event *last = oe->last;
21 u64 timestamp = new->timestamp;
22 struct list_head *p;
23
24 ++oe->nr_events;
25 oe->last = new;
26
Jiri Olsacee3ab92014-07-11 14:49:54 +020027 pr_oe_time2(timestamp, "queue_event nr_events %u\n", oe->nr_events);
28
Jiri Olsa5f86b802014-08-01 13:02:58 -030029 if (!last) {
30 list_add(&new->list, &oe->events);
31 oe->max_timestamp = timestamp;
32 return;
33 }
34
35 /*
36 * last event might point to some random place in the list as it's
37 * the last queued event. We expect that the new event is close to
38 * this.
39 */
40 if (last->timestamp <= timestamp) {
41 while (last->timestamp <= timestamp) {
42 p = last->list.next;
43 if (p == &oe->events) {
44 list_add_tail(&new->list, &oe->events);
45 oe->max_timestamp = timestamp;
46 return;
47 }
48 last = list_entry(p, struct ordered_event, list);
49 }
50 list_add_tail(&new->list, &last->list);
51 } else {
52 while (last->timestamp > timestamp) {
53 p = last->list.prev;
54 if (p == &oe->events) {
55 list_add(&new->list, &oe->events);
56 return;
57 }
58 last = list_entry(p, struct ordered_event, list);
59 }
60 list_add(&new->list, &last->list);
61 }
62}
63
Alexander Yarygin54bf53b2014-10-03 18:40:11 +040064static union perf_event *__dup_event(struct ordered_events *oe,
65 union perf_event *event)
66{
67 union perf_event *new_event = NULL;
68
69 if (oe->cur_alloc_size < oe->max_alloc_size) {
70 new_event = memdup(event, event->header.size);
71 if (new_event)
72 oe->cur_alloc_size += event->header.size;
73 }
74
75 return new_event;
76}
77
78static union perf_event *dup_event(struct ordered_events *oe,
79 union perf_event *event)
80{
81 return oe->copy_on_queue ? __dup_event(oe, event) : event;
82}
83
Jiri Olsad5ceb622018-09-07 12:24:54 +020084static void __free_dup_event(struct ordered_events *oe, union perf_event *event)
Alexander Yarygin54bf53b2014-10-03 18:40:11 +040085{
Jiri Olsad5ceb622018-09-07 12:24:54 +020086 if (event) {
Alexander Yarygin54bf53b2014-10-03 18:40:11 +040087 oe->cur_alloc_size -= event->header.size;
88 free(event);
89 }
90}
91
Jiri Olsad5ceb622018-09-07 12:24:54 +020092static void free_dup_event(struct ordered_events *oe, union perf_event *event)
93{
94 if (oe->copy_on_queue)
95 __free_dup_event(oe, event);
96}
97
Jiri Olsa5f86b802014-08-01 13:02:58 -030098#define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event))
Alexander Yarygin54bf53b2014-10-03 18:40:11 +040099static struct ordered_event *alloc_event(struct ordered_events *oe,
100 union perf_event *event)
Jiri Olsa5f86b802014-08-01 13:02:58 -0300101{
102 struct list_head *cache = &oe->cache;
103 struct ordered_event *new = NULL;
Alexander Yarygin54bf53b2014-10-03 18:40:11 +0400104 union perf_event *new_event;
Jiri Olsa53da12e2018-09-07 12:24:55 +0200105 size_t size;
Alexander Yarygin54bf53b2014-10-03 18:40:11 +0400106
107 new_event = dup_event(oe, event);
108 if (!new_event)
109 return NULL;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300110
Jiri Olsad5ceb622018-09-07 12:24:54 +0200111 /*
112 * We maintain the following scheme of buffers for ordered
113 * event allocation:
114 *
115 * to_free list -> buffer1 (64K)
116 * buffer2 (64K)
117 * ...
118 *
119 * Each buffer keeps an array of ordered events objects:
120 * buffer -> event[0]
121 * event[1]
122 * ...
123 *
124 * Each allocated ordered event is linked to one of
125 * following lists:
126 * - time ordered list 'events'
127 * - list of currently removed events 'cache'
128 *
129 * Allocation of the ordered event uses the following order
130 * to get the memory:
131 * - use recently removed object from 'cache' list
132 * - use available object in current allocation buffer
133 * - allocate new buffer if the current buffer is full
134 *
135 * Removal of ordered event object moves it from events to
136 * the cache list.
137 */
Jiri Olsa53da12e2018-09-07 12:24:55 +0200138 size = sizeof(*oe->buffer) + MAX_SAMPLE_BUFFER * sizeof(*new);
139
Jiri Olsa5f86b802014-08-01 13:02:58 -0300140 if (!list_empty(cache)) {
141 new = list_entry(cache->next, struct ordered_event, list);
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300142 list_del_init(&new->list);
Jiri Olsa5f86b802014-08-01 13:02:58 -0300143 } else if (oe->buffer) {
Jiri Olsad5ceb622018-09-07 12:24:54 +0200144 new = &oe->buffer->event[oe->buffer_idx];
Jiri Olsa5f86b802014-08-01 13:02:58 -0300145 if (++oe->buffer_idx == MAX_SAMPLE_BUFFER)
146 oe->buffer = NULL;
Jiri Olsa53da12e2018-09-07 12:24:55 +0200147 } else if ((oe->cur_alloc_size + size) < oe->max_alloc_size) {
Jiri Olsa5f86b802014-08-01 13:02:58 -0300148 oe->buffer = malloc(size);
Alexander Yarygin54bf53b2014-10-03 18:40:11 +0400149 if (!oe->buffer) {
150 free_dup_event(oe, new_event);
Jiri Olsa5f86b802014-08-01 13:02:58 -0300151 return NULL;
Alexander Yarygin54bf53b2014-10-03 18:40:11 +0400152 }
Jiri Olsa5f86b802014-08-01 13:02:58 -0300153
Jiri Olsacee3ab92014-07-11 14:49:54 +0200154 pr("alloc size %" PRIu64 "B (+%zu), max %" PRIu64 "B\n",
155 oe->cur_alloc_size, size, oe->max_alloc_size);
156
Jiri Olsa5f86b802014-08-01 13:02:58 -0300157 oe->cur_alloc_size += size;
158 list_add(&oe->buffer->list, &oe->to_free);
159
Jiri Olsad5ceb622018-09-07 12:24:54 +0200160 oe->buffer_idx = 1;
161 new = &oe->buffer->event[0];
Jiri Olsacee3ab92014-07-11 14:49:54 +0200162 } else {
163 pr("allocation limit reached %" PRIu64 "B\n", oe->max_alloc_size);
Jiri Olsad5ceb622018-09-07 12:24:54 +0200164 return NULL;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300165 }
166
Alexander Yarygin54bf53b2014-10-03 18:40:11 +0400167 new->event = new_event;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300168 return new;
169}
170
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300171static struct ordered_event *
172ordered_events__new_event(struct ordered_events *oe, u64 timestamp,
Alexander Yarygin54bf53b2014-10-03 18:40:11 +0400173 union perf_event *event)
Jiri Olsa5f86b802014-08-01 13:02:58 -0300174{
175 struct ordered_event *new;
176
Alexander Yarygin54bf53b2014-10-03 18:40:11 +0400177 new = alloc_event(oe, event);
Jiri Olsa5f86b802014-08-01 13:02:58 -0300178 if (new) {
179 new->timestamp = timestamp;
180 queue_event(oe, new);
181 }
182
183 return new;
184}
185
186void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event)
187{
Jiri Olsafa4e5c62014-06-15 19:46:08 +0200188 list_move(&event->list, &oe->cache);
Jiri Olsa5f86b802014-08-01 13:02:58 -0300189 oe->nr_events--;
Alexander Yarygin54bf53b2014-10-03 18:40:11 +0400190 free_dup_event(oe, event->event);
David Carrillo-Cisneros1e0d4f02017-04-10 13:14:27 -0700191 event->event = NULL;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300192}
193
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300194int ordered_events__queue(struct ordered_events *oe, union perf_event *event,
Jiri Olsadc83e132017-08-03 13:24:33 +0200195 u64 timestamp, u64 file_offset)
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300196{
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300197 struct ordered_event *oevent;
198
199 if (!timestamp || timestamp == ~0ULL)
200 return -ETIME;
201
202 if (timestamp < oe->last_flush) {
203 pr_oe_time(timestamp, "out of order event\n");
204 pr_oe_time(oe->last_flush, "last flush, last_flush_type %d\n",
205 oe->last_flush_type);
206
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300207 oe->nr_unordered_events++;
Arnaldo Carvalho de Melo4a6b3622015-03-03 13:02:24 -0300208 }
209
210 oevent = ordered_events__new_event(oe, timestamp, event);
211 if (!oevent) {
212 ordered_events__flush(oe, OE_FLUSH__HALF);
213 oevent = ordered_events__new_event(oe, timestamp, event);
214 }
215
216 if (!oevent)
217 return -ENOMEM;
218
219 oevent->file_offset = file_offset;
220 return 0;
221}
222
Jiri Olsa68ca5d02018-12-05 17:05:07 +0100223static int do_flush(struct ordered_events *oe, bool show_progress)
Jiri Olsa5f86b802014-08-01 13:02:58 -0300224{
Jiri Olsa5f86b802014-08-01 13:02:58 -0300225 struct list_head *head = &oe->events;
226 struct ordered_event *tmp, *iter;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300227 u64 limit = oe->next_flush;
228 u64 last_ts = oe->last ? oe->last->timestamp : 0ULL;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300229 struct ui_progress prog;
230 int ret;
231
Arnaldo Carvalho de Melo28083682015-02-22 13:52:47 -0800232 if (!limit)
Jiri Olsa5f86b802014-08-01 13:02:58 -0300233 return 0;
234
235 if (show_progress)
236 ui_progress__init(&prog, oe->nr_events, "Processing time ordered events...");
237
238 list_for_each_entry_safe(iter, tmp, head, list) {
239 if (session_done())
240 return 0;
241
242 if (iter->timestamp > limit)
243 break;
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300244 ret = oe->deliver(oe, iter);
Jiri Olsa5f86b802014-08-01 13:02:58 -0300245 if (ret)
Arnaldo Carvalho de Melo9870d782015-03-31 12:48:16 -0300246 return ret;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300247
248 ordered_events__delete(oe, iter);
249 oe->last_flush = iter->timestamp;
250
251 if (show_progress)
252 ui_progress__update(&prog, 1);
253 }
254
255 if (list_empty(head))
256 oe->last = NULL;
257 else if (last_ts <= limit)
258 oe->last = list_entry(head->prev, struct ordered_event, list);
259
Arnaldo Carvalho de Melo5c9ce1e2015-08-24 17:16:22 -0300260 if (show_progress)
261 ui_progress__finish();
262
Jiri Olsa5f86b802014-08-01 13:02:58 -0300263 return 0;
264}
265
Jiri Olsa68ca5d02018-12-05 17:05:07 +0100266static int __ordered_events__flush(struct ordered_events *oe, enum oe_flush how,
267 u64 timestamp)
Jiri Olsa5f86b802014-08-01 13:02:58 -0300268{
Jiri Olsacee3ab92014-07-11 14:49:54 +0200269 static const char * const str[] = {
Jiri Olsab0a45202014-06-12 09:50:11 +0200270 "NONE",
Jiri Olsacee3ab92014-07-11 14:49:54 +0200271 "FINAL",
272 "ROUND",
273 "HALF ",
Changbin Du1e5b0cf2019-03-16 16:05:52 +0800274 "TOP ",
275 "TIME ",
Jiri Olsacee3ab92014-07-11 14:49:54 +0200276 };
Jiri Olsa5f86b802014-08-01 13:02:58 -0300277 int err;
Jiri Olsab8494f12018-11-07 16:46:47 +0100278 bool show_progress = false;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300279
Arnaldo Carvalho de Melo28083682015-02-22 13:52:47 -0800280 if (oe->nr_events == 0)
281 return 0;
282
Jiri Olsa5f86b802014-08-01 13:02:58 -0300283 switch (how) {
284 case OE_FLUSH__FINAL:
Jiri Olsab8494f12018-11-07 16:46:47 +0100285 show_progress = true;
Jiri Olsa16c66bc2018-11-05 13:24:55 +0100286 __fallthrough;
287 case OE_FLUSH__TOP:
288 oe->next_flush = ULLONG_MAX;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300289 break;
290
291 case OE_FLUSH__HALF:
292 {
293 struct ordered_event *first, *last;
294 struct list_head *head = &oe->events;
295
296 first = list_entry(head->next, struct ordered_event, list);
297 last = oe->last;
298
299 /* Warn if we are called before any event got allocated. */
300 if (WARN_ONCE(!last || list_empty(head), "empty queue"))
301 return 0;
302
303 oe->next_flush = first->timestamp;
304 oe->next_flush += (last->timestamp - first->timestamp) / 2;
305 break;
306 }
307
Jiri Olsa68ca5d02018-12-05 17:05:07 +0100308 case OE_FLUSH__TIME:
309 oe->next_flush = timestamp;
310 show_progress = false;
311 break;
312
Jiri Olsa5f86b802014-08-01 13:02:58 -0300313 case OE_FLUSH__ROUND:
Jiri Olsab0a45202014-06-12 09:50:11 +0200314 case OE_FLUSH__NONE:
Jiri Olsa5f86b802014-08-01 13:02:58 -0300315 default:
316 break;
Zou Wei8284bbe2020-04-28 17:18:43 +0800317 }
Jiri Olsa5f86b802014-08-01 13:02:58 -0300318
Jiri Olsacee3ab92014-07-11 14:49:54 +0200319 pr_oe_time(oe->next_flush, "next_flush - ordered_events__flush PRE %s, nr_events %u\n",
320 str[how], oe->nr_events);
321 pr_oe_time(oe->max_timestamp, "max_timestamp\n");
322
Jiri Olsa68ca5d02018-12-05 17:05:07 +0100323 err = do_flush(oe, show_progress);
Jiri Olsa5f86b802014-08-01 13:02:58 -0300324
325 if (!err) {
326 if (how == OE_FLUSH__ROUND)
327 oe->next_flush = oe->max_timestamp;
Jiri Olsab0a45202014-06-12 09:50:11 +0200328
329 oe->last_flush_type = how;
Jiri Olsa5f86b802014-08-01 13:02:58 -0300330 }
331
Jiri Olsacee3ab92014-07-11 14:49:54 +0200332 pr_oe_time(oe->next_flush, "next_flush - ordered_events__flush POST %s, nr_events %u\n",
333 str[how], oe->nr_events);
334 pr_oe_time(oe->last_flush, "last_flush\n");
335
Jiri Olsa5f86b802014-08-01 13:02:58 -0300336 return err;
337}
Jiri Olsa36522f52014-06-10 22:47:40 +0200338
Jiri Olsa68ca5d02018-12-05 17:05:07 +0100339int ordered_events__flush(struct ordered_events *oe, enum oe_flush how)
340{
341 return __ordered_events__flush(oe, how, 0);
342}
343
344int ordered_events__flush_time(struct ordered_events *oe, u64 timestamp)
345{
346 return __ordered_events__flush(oe, OE_FLUSH__TIME, timestamp);
347}
348
Jiri Olsa83356b32018-12-06 14:38:52 -0300349u64 ordered_events__first_time(struct ordered_events *oe)
350{
351 struct ordered_event *event;
352
353 if (list_empty(&oe->events))
354 return 0;
355
356 event = list_first_entry(&oe->events, struct ordered_event, list);
357 return event->timestamp;
358}
359
Jiri Olsaa4a66682018-11-07 16:58:36 +0100360void ordered_events__init(struct ordered_events *oe, ordered_events__deliver_t deliver,
361 void *data)
Jiri Olsa36522f52014-06-10 22:47:40 +0200362{
363 INIT_LIST_HEAD(&oe->events);
364 INIT_LIST_HEAD(&oe->cache);
365 INIT_LIST_HEAD(&oe->to_free);
366 oe->max_alloc_size = (u64) -1;
367 oe->cur_alloc_size = 0;
Arnaldo Carvalho de Melod10eb1e2015-03-03 12:20:38 -0300368 oe->deliver = deliver;
Jiri Olsaa4a66682018-11-07 16:58:36 +0100369 oe->data = data;
Jiri Olsa36522f52014-06-10 22:47:40 +0200370}
Jiri Olsaadc56ed2014-06-10 22:50:03 +0200371
Jiri Olsad5ceb622018-09-07 12:24:54 +0200372static void
373ordered_events_buffer__free(struct ordered_events_buffer *buffer,
374 unsigned int max, struct ordered_events *oe)
375{
376 if (oe->copy_on_queue) {
377 unsigned int i;
378
379 for (i = 0; i < max; i++)
380 __free_dup_event(oe, buffer->event[i].event);
381 }
382
383 free(buffer);
384}
385
Jiri Olsaadc56ed2014-06-10 22:50:03 +0200386void ordered_events__free(struct ordered_events *oe)
387{
Jiri Olsad5ceb622018-09-07 12:24:54 +0200388 struct ordered_events_buffer *buffer, *tmp;
Jiri Olsaadc56ed2014-06-10 22:50:03 +0200389
Jiri Olsad5ceb622018-09-07 12:24:54 +0200390 if (list_empty(&oe->to_free))
391 return;
392
393 /*
394 * Current buffer might not have all the events allocated
395 * yet, we need to free only allocated ones ...
396 */
Jiri Olsa99d86c8b2019-01-17 12:30:17 +0100397 if (oe->buffer) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300398 list_del_init(&oe->buffer->list);
Jiri Olsa99d86c8b2019-01-17 12:30:17 +0100399 ordered_events_buffer__free(oe->buffer, oe->buffer_idx, oe);
400 }
Jiri Olsad5ceb622018-09-07 12:24:54 +0200401
402 /* ... and continue with the rest */
403 list_for_each_entry_safe(buffer, tmp, &oe->to_free, list) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300404 list_del_init(&buffer->list);
Jiri Olsad5ceb622018-09-07 12:24:54 +0200405 ordered_events_buffer__free(buffer, MAX_SAMPLE_BUFFER, oe);
Jiri Olsaadc56ed2014-06-10 22:50:03 +0200406 }
407}
Wang Nan4532f642016-04-13 08:21:04 +0000408
409void ordered_events__reinit(struct ordered_events *oe)
410{
411 ordered_events__deliver_t old_deliver = oe->deliver;
412
413 ordered_events__free(oe);
414 memset(oe, '\0', sizeof(*oe));
Jiri Olsaa4a66682018-11-07 16:58:36 +0100415 ordered_events__init(oe, old_deliver, oe->data);
Wang Nan4532f642016-04-13 08:21:04 +0000416}