perf report: Implement perf.data record decompression

zstd_init(, comp_level = 0) initializes decompression part of API only
hat now consists of zstd_decompress_stream() function.

The perf.data PERF_RECORD_COMPRESSED records are decompressed using
zstd_decompress_stream() function into a linked list of mmaped memory
regions of mmap_comp_len size (struct decomp).

After decompression of one COMPRESSED record its content is iterated and
fetched for usual processing. The mmaped memory regions with
decompressed events are kept in the linked list till the tool process
termination.

When dumping raw records (e.g., perf report -D --header) file offsets of
events from compressed records are printed as zero.

Committer notes:

Since now we have support for processing PERF_RECORD_COMPRESSED, we see
none, in raw form, like we saw in the previous patch commiter notes,
they were decompressed into the usual PERF_RECORD_{FORK,MMAP,COMM,etc}
records, we only see the stats for those PERF_RECORD_COMPRESSED events,
and since I used the file generated in the commiter notes for the
previous patch, there they are, 2 compressed records:

  $ perf report --header-only | grep cmdline
  # cmdline : /home/acme/bin/perf record -z2 sleep 1
  $ perf report -D | grep COMPRESS
        COMPRESSED events:          2
        COMPRESSED events:          0
  $ perf report --stdio
  # To display the perf.data header info, please use --header/--header-only options.
  #
  #
  # Total Lost Samples: 0
  #
  # Samples: 15  of event 'cycles:u'
  # Event count (approx.): 962227
  #
  # Overhead  Command  Shared Object     Symbol
  # ........  .......  ................  ...........................
  #
      46.99%  sleep    libc-2.28.so      [.] _dl_addr
      29.24%  sleep    [unknown]         [k] 0xffffffffaea00a67
      16.45%  sleep    libc-2.28.so      [.] __GI__IO_un_link.part.1
       5.92%  sleep    ld-2.28.so        [.] _dl_setup_hash
       1.40%  sleep    libc-2.28.so      [.] __nanosleep
       0.00%  sleep    [unknown]         [k] 0xffffffffaea00163

  #
  # (Tip: To see callchains in a more compact form: perf report -g folded)
  #
  $

Signed-off-by: Alexey Budankov <alexey.budankov@linux.intel.com>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/304b0a59-942c-3fe1-da02-aa749f87108b@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Alexey Budankov 2019-03-18 20:45:11 +03:00 коммит произвёл Arnaldo Carvalho de Melo
Родитель 504c1ad116
Коммит cb62c6f1f5
5 изменённых файлов: 181 добавлений и 2 удалений

Просмотреть файл

@ -1258,6 +1258,9 @@ repeat:
if (session == NULL) if (session == NULL)
return -1; return -1;
if (zstd_init(&(session->zstd_data), 0) < 0)
pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
if (report.queue_size) { if (report.queue_size) {
ordered_events__set_alloc_size(&session->ordered_events, ordered_events__set_alloc_size(&session->ordered_events,
report.queue_size); report.queue_size);
@ -1448,7 +1451,7 @@ repeat:
error: error:
if (report.ptime_range) if (report.ptime_range)
zfree(&report.ptime_range); zfree(&report.ptime_range);
zstd_fini(&(session->zstd_data));
perf_session__delete(session); perf_session__delete(session);
return ret; return ret;
} }

Просмотреть файл

@ -20,6 +20,7 @@ bool lzma_is_compressed(const char *input);
struct zstd_data { struct zstd_data {
#ifdef HAVE_ZSTD_SUPPORT #ifdef HAVE_ZSTD_SUPPORT
ZSTD_CStream *cstream; ZSTD_CStream *cstream;
ZSTD_DStream *dstream;
#endif #endif
}; };
@ -31,6 +32,9 @@ int zstd_fini(struct zstd_data *data);
size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size, size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t dst_size,
void *src, size_t src_size, size_t max_record_size, void *src, size_t src_size, size_t max_record_size,
size_t process_header(void *record, size_t increment)); size_t process_header(void *record, size_t increment));
size_t zstd_decompress_stream(struct zstd_data *data, void *src, size_t src_size,
void *dst, size_t dst_size);
#else /* !HAVE_ZSTD_SUPPORT */ #else /* !HAVE_ZSTD_SUPPORT */
static inline int zstd_init(struct zstd_data *data __maybe_unused, int level __maybe_unused) static inline int zstd_init(struct zstd_data *data __maybe_unused, int level __maybe_unused)
@ -52,6 +56,13 @@ size_t zstd_compress_stream_to_records(struct zstd_data *data __maybe_unused,
{ {
return 0; return 0;
} }
static inline size_t zstd_decompress_stream(struct zstd_data *data __maybe_unused, void *src __maybe_unused,
size_t src_size __maybe_unused, void *dst __maybe_unused,
size_t dst_size __maybe_unused)
{
return 0;
}
#endif #endif
#endif /* PERF_COMPRESS_H */ #endif /* PERF_COMPRESS_H */

Просмотреть файл

@ -29,6 +29,61 @@
#include "stat.h" #include "stat.h"
#include "arch/common.h" #include "arch/common.h"
#ifdef HAVE_ZSTD_SUPPORT
static int perf_session__process_compressed_event(struct perf_session *session,
union perf_event *event, u64 file_offset)
{
void *src;
size_t decomp_size, src_size;
u64 decomp_last_rem = 0;
size_t decomp_len = session->header.env.comp_mmap_len;
struct decomp *decomp, *decomp_last = session->decomp_last;
decomp = mmap(NULL, sizeof(struct decomp) + decomp_len, PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (decomp == MAP_FAILED) {
pr_err("Couldn't allocate memory for decompression\n");
return -1;
}
decomp->file_pos = file_offset;
decomp->head = 0;
if (decomp_last) {
decomp_last_rem = decomp_last->size - decomp_last->head;
memcpy(decomp->data, &(decomp_last->data[decomp_last->head]), decomp_last_rem);
decomp->size = decomp_last_rem;
}
src = (void *)event + sizeof(struct compressed_event);
src_size = event->pack.header.size - sizeof(struct compressed_event);
decomp_size = zstd_decompress_stream(&(session->zstd_data), src, src_size,
&(decomp->data[decomp_last_rem]), decomp_len - decomp_last_rem);
if (!decomp_size) {
munmap(decomp, sizeof(struct decomp) + decomp_len);
pr_err("Couldn't decompress data\n");
return -1;
}
decomp->size += decomp_size;
if (session->decomp == NULL) {
session->decomp = decomp;
session->decomp_last = decomp;
} else {
session->decomp_last->next = decomp;
session->decomp_last = decomp;
}
pr_debug("decomp (B): %ld to %ld\n", src_size, decomp_size);
return 0;
}
#else /* !HAVE_ZSTD_SUPPORT */
#define perf_session__process_compressed_event perf_session__process_compressed_event_stub
#endif
static int perf_session__deliver_event(struct perf_session *session, static int perf_session__deliver_event(struct perf_session *session,
union perf_event *event, union perf_event *event,
struct perf_tool *tool, struct perf_tool *tool,
@ -197,6 +252,21 @@ static void perf_session__delete_threads(struct perf_session *session)
machine__delete_threads(&session->machines.host); machine__delete_threads(&session->machines.host);
} }
static void perf_session__release_decomp_events(struct perf_session *session)
{
struct decomp *next, *decomp;
size_t decomp_len;
next = session->decomp;
decomp_len = session->header.env.comp_mmap_len;
do {
decomp = next;
if (decomp == NULL)
break;
next = decomp->next;
munmap(decomp, decomp_len + sizeof(struct decomp));
} while (1);
}
void perf_session__delete(struct perf_session *session) void perf_session__delete(struct perf_session *session)
{ {
if (session == NULL) if (session == NULL)
@ -205,6 +275,7 @@ void perf_session__delete(struct perf_session *session)
auxtrace_index__free(&session->auxtrace_index); auxtrace_index__free(&session->auxtrace_index);
perf_session__destroy_kernel_maps(session); perf_session__destroy_kernel_maps(session);
perf_session__delete_threads(session); perf_session__delete_threads(session);
perf_session__release_decomp_events(session);
perf_env__exit(&session->header.env); perf_env__exit(&session->header.env);
machines__exit(&session->machines); machines__exit(&session->machines);
if (session->data) if (session->data)
@ -439,7 +510,7 @@ void perf_tool__fill_defaults(struct perf_tool *tool)
if (tool->feature == NULL) if (tool->feature == NULL)
tool->feature = process_event_op2_stub; tool->feature = process_event_op2_stub;
if (tool->compressed == NULL) if (tool->compressed == NULL)
tool->compressed = perf_session__process_compressed_event_stub; tool->compressed = perf_session__process_compressed_event;
} }
static void swap_sample_id_all(union perf_event *event, void *data) static void swap_sample_id_all(union perf_event *event, void *data)
@ -1725,6 +1796,8 @@ static int perf_session__flush_thread_stacks(struct perf_session *session)
volatile int session_done; volatile int session_done;
static int __perf_session__process_decomp_events(struct perf_session *session);
static int __perf_session__process_pipe_events(struct perf_session *session) static int __perf_session__process_pipe_events(struct perf_session *session)
{ {
struct ordered_events *oe = &session->ordered_events; struct ordered_events *oe = &session->ordered_events;
@ -1805,6 +1878,10 @@ more:
if (skip > 0) if (skip > 0)
head += skip; head += skip;
err = __perf_session__process_decomp_events(session);
if (err)
goto out_err;
if (!session_done()) if (!session_done())
goto more; goto more;
done: done:
@ -1853,6 +1930,39 @@ fetch_mmaped_event(struct perf_session *session,
return event; return event;
} }
static int __perf_session__process_decomp_events(struct perf_session *session)
{
s64 skip;
u64 size, file_pos = 0;
struct decomp *decomp = session->decomp_last;
if (!decomp)
return 0;
while (decomp->head < decomp->size && !session_done()) {
union perf_event *event = fetch_mmaped_event(session, decomp->head, decomp->size, decomp->data);
if (!event)
break;
size = event->header.size;
if (size < sizeof(struct perf_event_header) ||
(skip = perf_session__process_event(session, event, file_pos)) < 0) {
pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
decomp->file_pos + decomp->head, event->header.size, event->header.type);
return -EINVAL;
}
if (skip)
size += skip;
decomp->head += size;
}
return 0;
}
/* /*
* On 64bit we can mmap the data file in one go. No need for tiny mmap * On 64bit we can mmap the data file in one go. No need for tiny mmap
* slices. On 32bit we use 32MB. * slices. On 32bit we use 32MB.
@ -1962,6 +2072,10 @@ more:
head += size; head += size;
file_pos += size; file_pos += size;
err = __perf_session__process_decomp_events(session);
if (err)
goto out;
ui_progress__update(prog, size); ui_progress__update(prog, size);
if (session_done()) if (session_done())

Просмотреть файл

@ -39,6 +39,16 @@ struct perf_session {
u64 bytes_transferred; u64 bytes_transferred;
u64 bytes_compressed; u64 bytes_compressed;
struct zstd_data zstd_data; struct zstd_data zstd_data;
struct decomp *decomp;
struct decomp *decomp_last;
};
struct decomp {
struct decomp *next;
u64 file_pos;
u64 head;
size_t size;
char data[];
}; };
struct perf_tool; struct perf_tool;

Просмотреть файл

@ -9,6 +9,21 @@ int zstd_init(struct zstd_data *data, int level)
{ {
size_t ret; size_t ret;
data->dstream = ZSTD_createDStream();
if (data->dstream == NULL) {
pr_err("Couldn't create decompression stream.\n");
return -1;
}
ret = ZSTD_initDStream(data->dstream);
if (ZSTD_isError(ret)) {
pr_err("Failed to initialize decompression stream: %s\n", ZSTD_getErrorName(ret));
return -1;
}
if (!level)
return 0;
data->cstream = ZSTD_createCStream(); data->cstream = ZSTD_createCStream();
if (data->cstream == NULL) { if (data->cstream == NULL) {
pr_err("Couldn't create compression stream.\n"); pr_err("Couldn't create compression stream.\n");
@ -26,6 +41,11 @@ int zstd_init(struct zstd_data *data, int level)
int zstd_fini(struct zstd_data *data) int zstd_fini(struct zstd_data *data)
{ {
if (data->dstream) {
ZSTD_freeDStream(data->dstream);
data->dstream = NULL;
}
if (data->cstream) { if (data->cstream) {
ZSTD_freeCStream(data->cstream); ZSTD_freeCStream(data->cstream);
data->cstream = NULL; data->cstream = NULL;
@ -68,3 +88,24 @@ size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t
return compressed; return compressed;
} }
size_t zstd_decompress_stream(struct zstd_data *data, void *src, size_t src_size,
void *dst, size_t dst_size)
{
size_t ret;
ZSTD_inBuffer input = { src, src_size, 0 };
ZSTD_outBuffer output = { dst, dst_size, 0 };
while (input.pos < input.size) {
ret = ZSTD_decompressStream(data->dstream, &output, &input);
if (ZSTD_isError(ret)) {
pr_err("failed to decompress (B): %ld -> %ld : %s\n",
src_size, output.size, ZSTD_getErrorName(ret));
break;
}
output.dst = dst + output.pos;
output.size = dst_size - output.pos;
}
return output.pos;
}