Skip to content

Commit fe17420

Browse files
KAGA-KOKOacmel
authored andcommitted
perf session: Keep file mmaped instead of malloc/memcpy
Profiling perf with perf revealed that a large part of the processing time is spent in malloc/memcpy/free in the sample ordering code. That code copies the data from the mmap into malloc'ed memory. That's silly. We can keep the mmap and just store the pointer in the queuing data structure. For 64 bit this is not a problem as we map the whole file anyway. On 32bit we keep 8 maps around and unmap the oldest before mmaping the next chunk of the file. Performance gain: 2.95s -> 1.23s (Faktor 2.4) Cc: Ingo Molnar <mingo@elte.hu> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> LKML-Reference: <20101130163820.278787719@linutronix.de> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 55b4462 commit fe17420

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

‎tools/perf/util/session.c‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ static void flush_sample_queue(struct perf_session *s,
418418

419419
os->last_flush = iter->timestamp;
420420
list_del(&iter->list);
421-
free(iter->event);
422421
free(iter);
423422
}
424423

@@ -531,7 +530,6 @@ static int queue_sample_event(event_t *event, struct sample_data *data,
531530
u64 timestamp = data->time;
532531
struct sample_queue *new;
533532

534-
535533
if (timestamp < s->ordered_samples.last_flush) {
536534
printf("Warning: Timestamp below last timeslice flush\n");
537535
return -EINVAL;
@@ -542,14 +540,7 @@ static int queue_sample_event(event_t *event, struct sample_data *data,
542540
return -ENOMEM;
543541

544542
new->timestamp = timestamp;
545-
546-
new->event = malloc(event->header.size);
547-
if (!new->event) {
548-
free(new);
549-
return -ENOMEM;
550-
}
551-
552-
memcpy(new->event, event, event->header.size);
543+
new->event = event;
553544

554545
__queue_sample_event(new, s);
555546

@@ -747,12 +738,12 @@ int __perf_session__process_events(struct perf_session *session,
747738
u64 file_size, struct perf_event_ops *ops)
748739
{
749740
u64 head, page_offset, file_offset, file_pos, progress_next;
750-
int err, mmap_prot, mmap_flags;
741+
int err, mmap_prot, mmap_flags, map_idx = 0;
751742
struct ui_progress *progress;
752743
size_t page_size, mmap_size;
744+
char *buf, *mmaps[8];
753745
event_t *event;
754746
uint32_t size;
755-
char *buf;
756747

757748
perf_event_ops__fill_defaults(ops);
758749

@@ -774,6 +765,8 @@ int __perf_session__process_events(struct perf_session *session,
774765
if (mmap_size > file_size)
775766
mmap_size = file_size;
776767

768+
memset(mmaps, 0, sizeof(mmaps));
769+
777770
mmap_prot = PROT_READ;
778771
mmap_flags = MAP_SHARED;
779772

@@ -789,6 +782,8 @@ int __perf_session__process_events(struct perf_session *session,
789782
err = -errno;
790783
goto out_err;
791784
}
785+
mmaps[map_idx] = buf;
786+
map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
792787
file_pos = file_offset + head;
793788

794789
more:
@@ -801,10 +796,10 @@ int __perf_session__process_events(struct perf_session *session,
801796
size = 8;
802797

803798
if (head + event->header.size >= mmap_size) {
804-
int munmap_ret;
805-
806-
munmap_ret = munmap(buf, mmap_size);
807-
assert(munmap_ret == 0);
799+
if (mmaps[map_idx]) {
800+
munmap(mmaps[map_idx], mmap_size);
801+
mmaps[map_idx] = NULL;
802+
}
808803

809804
page_offset = page_size * (head / page_size);
810805
file_offset += page_offset;

0 commit comments

Comments
 (0)