Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp634714img; Mon, 18 Mar 2019 10:44:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqwzhbf6uhXc04oCnDHmMetcF85Dzymrrly0g9HTI8B+k0sJY4y1wUdGV84ViFmHvNJTTv2Q X-Received: by 2002:a17:902:694b:: with SMTP id k11mr21186471plt.288.1552931057219; Mon, 18 Mar 2019 10:44:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552931057; cv=none; d=google.com; s=arc-20160816; b=Adc0Xr1nXCkomUVuGgI157Q/+cOhACeAyeMahj613st0x6qvH6qJUnrVqRaDPtdCir YQAJlfNNEThgKzGGtU1km6ucay2lgugXWWS1mgm0VzgYEReA59gjTQFru9pwnyOQH6rh hJJkH3AZmmTKRSBd9S0tCI9cbgngRisFvRgt5Yo308jiMirSrHjWuF4pYq7ymd4KLbjM q4VICYC/JthLHCT+3cLmioinCaaJsQjpUrvAmmzJbpV5BYYPotFpdQj4FJ/uUg8jeQ8+ 9jFNRMRJshoGxXBGiyiH5xpoaJiy7zMlnuJy/6kUAtYJuNvt3TtSuuqV2wZjtO5Z9SMZ TE9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:references:cc:to:from:subject; bh=7jk5bATNH7bDmgPNyI+lukExh8gLOse8j6hGKRRhBcY=; b=csyQF8BmrkNXv4EENfLAWKvTfM1jpZs71Q+RR9rcQqgqqA/Xh/rlSS0Z1e2vgrJhqw 2KP2ePWYN1Q3lUl3RaU/dGX5gtt1zPSeBJDje48PDbql6Fejl4+lmqApSWLxYAxEypZZ fguxbpd6nA2ytGFUXbJ0cHec9U+i4L3+sDhQPvEV6Cy/WB8ZEyqU//qwlMYaIqFwP9MR yzRtEure6R0gwWuiEkEkOWcD5nwFBpPRTqIBwoS8Q5h58QYkNJs405/h060XgN4Ro0n7 OqEsnULi0uOQtqS6+F2aEatl+r5sfekgGkr6pO13shl0cQQI541k7t3I/hBTwNASOVLk 70mQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id cb3si10242602plb.254.2019.03.18.10.44.01; Mon, 18 Mar 2019 10:44:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728043AbfCRRnE (ORCPT + 99 others); Mon, 18 Mar 2019 13:43:04 -0400 Received: from mga05.intel.com ([192.55.52.43]:47938 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728029AbfCRRm7 (ORCPT ); Mon, 18 Mar 2019 13:42:59 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Mar 2019 10:42:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,494,1544515200"; d="scan'208";a="283714944" Received: from linux.intel.com ([10.54.29.200]) by orsmga004.jf.intel.com with ESMTP; 18 Mar 2019 10:42:58 -0700 Received: from [10.251.91.19] (abudanko-mobl.ccr.corp.intel.com [10.251.91.19]) by linux.intel.com (Postfix) with ESMTP id 966E6580238; Mon, 18 Mar 2019 10:42:56 -0700 (PDT) Subject: [PATCH v10 06/12] perf util: introduce Zstd streaming based compression API From: Alexey Budankov To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Ingo Molnar , Andi Kleen , linux-kernel References: <12cce142-6238-475b-b9aa-236531c12c2b@linux.intel.com> Organization: Intel Corp. Message-ID: <18bf36f3-b85a-1fe2-dd83-10e0c6069568@linux.intel.com> Date: Mon, 18 Mar 2019 20:42:55 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: <12cce142-6238-475b-b9aa-236531c12c2b@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implemented functions are based on Zstd streaming compression API. The functions are used in runtime to compress data that come from mmaped kernel buffer. zstd_init(), zstd_fini() are used for initialization and finalization to allocate and deallocate internal zstd objects. zstd_compress_stream_to_records() is used to convert parts of mmaped kernel buffer into an array of PERF_RECORD_COMPRESSED records. Signed-off-by: Alexey Budankov --- tools/perf/util/Build | 2 ++ tools/perf/util/compress.h | 43 +++++++++++++++++++++++ tools/perf/util/zstd.c | 71 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 tools/perf/util/zstd.c diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 8dd3102301ea..6d5bbc8b589b 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -145,6 +145,8 @@ perf-y += scripting-engines/ perf-$(CONFIG_ZLIB) += zlib.o perf-$(CONFIG_LZMA) += lzma.o +perf-$(CONFIG_ZSTD) += zstd.o + perf-y += demangle-java.o perf-y += demangle-rust.o diff --git a/tools/perf/util/compress.h b/tools/perf/util/compress.h index 892e92e7e7fc..d00d7cb095aa 100644 --- a/tools/perf/util/compress.h +++ b/tools/perf/util/compress.h @@ -2,6 +2,11 @@ #ifndef PERF_COMPRESS_H #define PERF_COMPRESS_H +#include +#ifdef HAVE_ZSTD_SUPPORT +#include +#endif + #ifdef HAVE_ZLIB_SUPPORT int gzip_decompress_to_file(const char *input, int output_fd); bool gzip_is_compressed(const char *input); @@ -12,4 +17,42 @@ int lzma_decompress_to_file(const char *input, int output_fd); bool lzma_is_compressed(const char *input); #endif +struct zstd_data { +#ifdef HAVE_ZSTD_SUPPORT + ZSTD_CStream *cstream; +#endif +}; + +#ifdef HAVE_ZSTD_SUPPORT + +int zstd_init(struct zstd_data *data, int level); +int zstd_fini(struct zstd_data *data); + +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, + size_t process_header(void *record, size_t increment)); + +#else /* !HAVE_ZSTD_SUPPORT */ + +static inline int zstd_init(struct zstd_data *data __maybe_unused, int level __maybe_unused) +{ + return 0; +} + +static inline int zstd_fini(struct zstd_data *data __maybe_unused) +{ + return 0; +} + +static inline size_t zstd_compress_stream_to_records(struct zstd_data *data __maybe_unused, + void *dst __maybe_unused, size_t dst_size __maybe_unused, + void *src __maybe_unused, size_t src_size __maybe_unused, + size_t max_record_size __maybe_unused, + size_t process_header(void *record, size_t increment) __maybe_unused) +{ + return 0; +} + +#endif + #endif /* PERF_COMPRESS_H */ diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c new file mode 100644 index 000000000000..6d4f69d57567 --- /dev/null +++ b/tools/perf/util/zstd.c @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +#include "util/compress.h" +#include "util/debug.h" + +int zstd_init(struct zstd_data *data, int level) +{ + size_t ret; + + data->cstream = ZSTD_createCStream(); + if (data->cstream == NULL) { + pr_err("Couldn't create compression stream.\n"); + return -1; + } + + ret = ZSTD_initCStream(data->cstream, level); + if (ZSTD_isError(ret)) { + pr_err("Failed to initialize compression stream: %s\n", ZSTD_getErrorName(ret)); + return -1; + } + + return 0; +} + +int zstd_fini(struct zstd_data *data) +{ + if (data->cstream) { + ZSTD_freeCStream(data->cstream); + data->cstream = NULL; + } + + return 0; +} + +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, + size_t process_header(void *record, size_t increment)) +{ + size_t ret, size, compressed = 0; + ZSTD_inBuffer input = { src, src_size, 0 }; + ZSTD_outBuffer output; + void *record; + + while (input.pos < input.size) { + record = dst; + size = process_header(record, 0); + compressed += size; + dst += size; + dst_size -= size; + output = (ZSTD_outBuffer){ dst, (dst_size > max_record_size) ? + max_record_size : dst_size, 0 }; + ret = ZSTD_compressStream(data->cstream, &output, &input); + ZSTD_flushStream(data->cstream, &output); + if (ZSTD_isError(ret)) { + pr_err("failed to compress %ld bytes: %s\n", + (long)src_size, ZSTD_getErrorName(ret)); + memcpy(dst, src, src_size); + return src_size; + } + size = output.pos; + size = process_header(record, size); + compressed += size; + dst += size; + dst_size -= size; + } + + return compressed; +} + -- 2.20.1