Skip to content

Commit a1225de

Browse files
KAGA-KOKOacmel
authored andcommitted
perf session: Fix list sort algorithm
The homebrewn sort algorithm fails to sort in time order. One of the problem spots is that it fails to deal with equal timestamps correctly. My first gut reaction was to replace the fancy list with an rbtree, but the performance is 3 times worse. Rewrite it so it works. Cc: Ingo Molnar <mingo@elte.hu> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> LKML-Reference: <20101130163819.908482530@linutronix.de> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent c320c7b commit a1225de

2 files changed

Lines changed: 49 additions & 68 deletions

File tree

‎tools/perf/util/session.c‎

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct perf_session *perf_session__new(const char *filename, int mode, bool forc
104104
self->mmap_window = 32;
105105
self->machines = RB_ROOT;
106106
self->repipe = repipe;
107-
INIT_LIST_HEAD(&self->ordered_samples.samples_head);
107+
INIT_LIST_HEAD(&self->ordered_samples.samples);
108108
machine__init(&self->host_machine, "", HOST_KERNEL_ID);
109109

110110
if (mode == O_RDONLY) {
@@ -393,27 +393,33 @@ struct sample_queue {
393393
static void flush_sample_queue(struct perf_session *s,
394394
struct perf_event_ops *ops)
395395
{
396-
struct list_head *head = &s->ordered_samples.samples_head;
397-
u64 limit = s->ordered_samples.next_flush;
396+
struct ordered_samples *os = &s->ordered_samples;
397+
struct list_head *head = &os->samples;
398398
struct sample_queue *tmp, *iter;
399+
u64 limit = os->next_flush;
400+
u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
399401

400402
if (!ops->ordered_samples || !limit)
401403
return;
402404

403405
list_for_each_entry_safe(iter, tmp, head, list) {
404406
if (iter->timestamp > limit)
405-
return;
406-
407-
if (iter == s->ordered_samples.last_inserted)
408-
s->ordered_samples.last_inserted = NULL;
407+
break;
409408

410409
ops->sample((event_t *)iter->event, s);
411410

412-
s->ordered_samples.last_flush = iter->timestamp;
411+
os->last_flush = iter->timestamp;
413412
list_del(&iter->list);
414413
free(iter->event);
415414
free(iter);
416415
}
416+
417+
if (list_empty(head)) {
418+
os->last_sample = NULL;
419+
} else if (last_ts <= limit) {
420+
os->last_sample =
421+
list_entry(head->prev, struct sample_queue, list);
422+
}
417423
}
418424

419425
/*
@@ -465,71 +471,50 @@ static int process_finished_round(event_t *event __used,
465471
return 0;
466472
}
467473

468-
static void __queue_sample_end(struct sample_queue *new, struct list_head *head)
469-
{
470-
struct sample_queue *iter;
471-
472-
list_for_each_entry_reverse(iter, head, list) {
473-
if (iter->timestamp < new->timestamp) {
474-
list_add(&new->list, &iter->list);
475-
return;
476-
}
477-
}
478-
479-
list_add(&new->list, head);
480-
}
481-
482-
static void __queue_sample_before(struct sample_queue *new,
483-
struct sample_queue *iter,
484-
struct list_head *head)
485-
{
486-
list_for_each_entry_continue_reverse(iter, head, list) {
487-
if (iter->timestamp < new->timestamp) {
488-
list_add(&new->list, &iter->list);
489-
return;
490-
}
491-
}
492-
493-
list_add(&new->list, head);
494-
}
495-
496-
static void __queue_sample_after(struct sample_queue *new,
497-
struct sample_queue *iter,
498-
struct list_head *head)
499-
{
500-
list_for_each_entry_continue(iter, head, list) {
501-
if (iter->timestamp > new->timestamp) {
502-
list_add_tail(&new->list, &iter->list);
503-
return;
504-
}
505-
}
506-
list_add_tail(&new->list, head);
507-
}
508-
509474
/* The queue is ordered by time */
510475
static void __queue_sample_event(struct sample_queue *new,
511476
struct perf_session *s)
512477
{
513-
struct sample_queue *last_inserted = s->ordered_samples.last_inserted;
514-
struct list_head *head = &s->ordered_samples.samples_head;
478+
struct ordered_samples *os = &s->ordered_samples;
479+
struct sample_queue *sample = os->last_sample;
480+
u64 timestamp = new->timestamp;
481+
struct list_head *p;
515482

483+
os->last_sample = new;
516484

517-
if (!last_inserted) {
518-
__queue_sample_end(new, head);
485+
if (!sample) {
486+
list_add(&new->list, &os->samples);
487+
os->max_timestamp = timestamp;
519488
return;
520489
}
521490

522491
/*
523-
* Most of the time the current event has a timestamp
524-
* very close to the last event inserted, unless we just switched
525-
* to another event buffer. Having a sorting based on a list and
526-
* on the last inserted event that is close to the current one is
527-
* probably more efficient than an rbtree based sorting.
492+
* last_sample might point to some random place in the list as it's
493+
* the last queued event. We expect that the new event is close to
494+
* this.
528495
*/
529-
if (last_inserted->timestamp >= new->timestamp)
530-
__queue_sample_before(new, last_inserted, head);
531-
else
532-
__queue_sample_after(new, last_inserted, head);
496+
if (sample->timestamp <= timestamp) {
497+
while (sample->timestamp <= timestamp) {
498+
p = sample->list.next;
499+
if (p == &os->samples) {
500+
list_add_tail(&new->list, &os->samples);
501+
os->max_timestamp = timestamp;
502+
return;
503+
}
504+
sample = list_entry(p, struct sample_queue, list);
505+
}
506+
list_add_tail(&new->list, &sample->list);
507+
} else {
508+
while (sample->timestamp > timestamp) {
509+
p = sample->list.prev;
510+
if (p == &os->samples) {
511+
list_add(&new->list, &os->samples);
512+
return;
513+
}
514+
sample = list_entry(p, struct sample_queue, list);
515+
}
516+
list_add(&new->list, &sample->list);
517+
}
533518
}
534519

535520
static int queue_sample_event(event_t *event, struct sample_data *data,
@@ -559,10 +544,6 @@ static int queue_sample_event(event_t *event, struct sample_data *data,
559544
memcpy(new->event, event, event->header.size);
560545

561546
__queue_sample_event(new, s);
562-
s->ordered_samples.last_inserted = new;
563-
564-
if (new->timestamp > s->ordered_samples.max_timestamp)
565-
s->ordered_samples.max_timestamp = new->timestamp;
566547

567548
return 0;
568549
}

‎tools/perf/util/session.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ struct ordered_samples {
1717
u64 last_flush;
1818
u64 next_flush;
1919
u64 max_timestamp;
20-
struct list_head samples_head;
21-
struct sample_queue *last_inserted;
20+
struct list_head samples;
21+
struct sample_queue *last_sample;
2222
};
2323

2424
struct perf_session {

0 commit comments

Comments
 (0)