From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B7DEC433F5 for ; Fri, 8 Oct 2021 04:22:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EECA2610CE for ; Fri, 8 Oct 2021 04:22:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237470AbhJHEYW (ORCPT ); Fri, 8 Oct 2021 00:24:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237272AbhJHEYV (ORCPT ); Fri, 8 Oct 2021 00:24:21 -0400 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 851D4C061570 for ; Thu, 7 Oct 2021 21:22:26 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id g10so30385998edj.1 for ; Thu, 07 Oct 2021 21:22:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FDbY+Q//wqmNyJEUVf6j9jClB8JKizMICldYbJnoFTw=; b=Z7/uW7mVfDY1aVxgQ6lbCsybJOgjcAC/jPsg5HXj/6qdsYtsQrxsKT7MNNVLYD0di1 pOQRffnC2O+4MMU6zWbafBykyK5UQLQYioi9rY68VcUaz/a6PMlAq5vdOB9uicBiEJcc BXhf3AmPcesiN6c9QShPMuGIajcCkgn3aFxY57MuDN0bLCblNKbFp1ifVOXGMQg9kVny DG9hMY8ioSZ3ysqd5GAWCrbAbU2yGWXt9PC8U29qYpGpYJRz3oMjOq8j9Y5B/dSbHsre mLXS5FjFmUOsh3exnyRiNMv0gXTXgKmYVNj6UlGg0H5L+Z79xHUR/mvm2gTzFz9dhhJx L4Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FDbY+Q//wqmNyJEUVf6j9jClB8JKizMICldYbJnoFTw=; b=jU8ruIyWZ49cmr1pq/lF3cXwk414SC2jzHDaG8GjXVqmJvJzeqcxmmZJw+tzoA5DBa lcfhJyKFbtHKY5AKAJd+TqrNOQnG0CeNVZzT0CNqfX02qWkcLnW3+XKvcH7PX66XOeL5 caj/HlGCg2kSqpr3wYat3KhiS+J+olcz7JlGPuVdB/ra4kmnAijQY9bx/zKVp6PYdyT6 mBteMBIxQSG3INWir/HOe7gjqPEEKmwV2Uxxew9sEBGACbjrv+hM/jf3o1g/TDzSVPHf ngYgaCzJZBGy+eVYW+2hhPSfmIBpGtvz7OpuLoGED7XgCtP8T3lpepNEQKwgTJOvJBLs Z3zA== X-Gm-Message-State: AOAM533ORPfKBIFqkkdNfv/X47oSVtWRKPwt6W1RY0n+xgfOyC6IPFko aU5td+fFvimZVNSF9SkKDRKiFrmsOXcPKg== X-Google-Smtp-Source: ABdhPJwIxZz6ynq7TgsrgCLyEKAfOHA+NHoVR4T+UQNirUcMKSmDAhEmSaOmDkQSh4vB6O9hGRBFEg== X-Received: by 2002:a17:906:b1d5:: with SMTP id bv21mr1298790ejb.346.1633666945164; Thu, 07 Oct 2021 21:22:25 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id g23sm517429edt.1.2021.10.07.21.22.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Oct 2021 21:22:24 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v3 08/20] trace-cmd library: Add local helper function for data compression Date: Fri, 8 Oct 2021 07:22:02 +0300 Message-Id: <20211008042214.977193-9-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211008042214.977193-1-tz.stoyanov@gmail.com> References: <20211008042214.977193-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org The newly added helper functions read data from a file and compress it, before writing into the trace file. The trace data is compressed in chunks, which are page aligned. A new local define is introduced: PAGES_IN_CHUNK which can be used to tune how big a compression chunk is. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-output.c | 71 +++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c index d15a5986..c46643f5 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -283,18 +283,27 @@ static unsigned long get_size(const char *file) return size; } -static tsize_t copy_file_fd(struct tracecmd_output *handle, int fd) +static tsize_t copy_file_fd(struct tracecmd_output *handle, int fd, unsigned long long max) { + tsize_t rsize = BUFSIZ; tsize_t size = 0; char buf[BUFSIZ]; stsize_t r; do { - r = read(fd, buf, BUFSIZ); + if (max && rsize > max) + rsize = max; + + r = read(fd, buf, rsize); if (r > 0) { size += r; if (do_write_check(handle, buf, r)) return 0; + if (max) { + max -= r; + if (!max) + break; + } } } while (r > 0); @@ -312,12 +321,62 @@ static tsize_t copy_file(struct tracecmd_output *handle, tracecmd_warning("Can't read '%s'", file); return 0; } - size = copy_file_fd(handle, fd); + size = copy_file_fd(handle, fd, 0); close(fd); return size; } +#define PAGES_IN_CHUNK 10 +__hidden unsigned long long out_copy_fd_compress(struct tracecmd_output *handle, + int fd, unsigned long long max, + unsigned long long *write_size) +{ + unsigned long long rsize = 0; + unsigned long long wsize = 0; + unsigned long long size; + int ret; + + if (handle->compress) { + rsize = max; + ret = tracecmd_compress_copy_from(handle->compress, fd, + PAGES_IN_CHUNK * handle->page_size, + &rsize, &wsize); + if (ret < 0) + return 0; + + size = rsize; + if (write_size) + *write_size = wsize; + } else { + size = copy_file_fd(handle, fd, max); + if (write_size) + *write_size = size; + } + + return size; +} + +static tsize_t copy_file_compress(struct tracecmd_output *handle, + const char *file, unsigned long long *write_size) +{ + int ret; + int fd; + + fd = open(file, O_RDONLY); + if (fd < 0) { + tracecmd_warning("Can't read '%s'", file); + return 0; + } + + ret = out_copy_fd_compress(handle, fd, 0, write_size); + if (!ret) + tracecmd_warning("Can't compress '%s'", file); + + close(fd); + return ret; +} + /* * Finds the path to the debugfs/tracing * Allocates the string and stores it. @@ -513,7 +572,7 @@ static int read_header_files(struct tracecmd_output *handle, bool compress) endian8 = convert_endian_8(handle, size); if (do_write_check(handle, &endian8, 8)) goto out_close; - check_size = copy_file_fd(handle, fd); + check_size = copy_file_fd(handle, fd, 0); close(fd); if (size != check_size) { tracecmd_warning("wrong size for '%s' size=%lld read=%lld", path, size, check_size); @@ -539,7 +598,7 @@ static int read_header_files(struct tracecmd_output *handle, bool compress) endian8 = convert_endian_8(handle, size); if (do_write_check(handle, &endian8, 8)) goto out_close; - check_size = copy_file_fd(handle, fd); + check_size = copy_file_fd(handle, fd, 0); close(fd); if (size != check_size) { tracecmd_warning("wrong size for '%s'", path); @@ -2181,7 +2240,7 @@ __hidden int out_write_cpu_data(struct tracecmd_output *handle, if (data[i].size) { if (lseek64(data[i].fd, data[i].offset, SEEK_SET) == (off64_t)-1) goto out_free; - read_size = copy_file_fd(handle, data[i].fd); + read_size = copy_file_fd(handle, data[i].fd, data[i].size); if (read_size != data_files[i].file_size) { errno = EINVAL; tracecmd_warning("did not match size of %lld to %lld", -- 2.31.1