From patchwork Wed Jan 26 09:48:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12724831 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75522C63682 for ; Wed, 26 Jan 2022 09:49:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239367AbiAZJtX (ORCPT ); Wed, 26 Jan 2022 04:49:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232719AbiAZJtW (ORCPT ); Wed, 26 Jan 2022 04:49:22 -0500 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F00EC06161C for ; Wed, 26 Jan 2022 01:49:22 -0800 (PST) Received: by mail-ej1-x630.google.com with SMTP id s13so37722643ejy.3 for ; Wed, 26 Jan 2022 01:49:21 -0800 (PST) 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=fw08kuUCH//FlN6KbBP9jNrMN4LiivjmxluSf6wyNak=; b=EX+uNMHeO/daxI8dl09PBLVsvaazEEKnWZoIuPVqhDyBrxip8BnmsE40qC6sKZu60P ALnEVQFovM41QmwHcglK6FLw5bXkJvFaEzzOQo1ZGpUnBhyy7FXKA3X2zOOyTAVrmnlR A5gOp28thHtrRKYOS6IEo5lcvwOnt/OC+Mwn7QyBgQMf19FvfZtEgdA/BveEiS0xeb3Z Dook2XDoSUjbJfK4JEeK5Z8BIGKUbuVdq5PDSoFNnDO5tWEcKEq3ES9rZEUV8X5dTFiJ ugkhdPMDBf4bGSJ9brVLa26KDjnFt5+5JYZrR5U/8YYvxXjaHtj79GRF5wBvGjP0TivB VLEg== 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=fw08kuUCH//FlN6KbBP9jNrMN4LiivjmxluSf6wyNak=; b=oYyq5pG1rZp5Xvo+fCjKgGjvJA0b8TdtV25p/FzOJoRziUe1+DINnx7v9SyMkRhwOz elDrn/s0hovZ0VFfR+/Uv0gv2UnNC3axyMavI1ZJAn88UTtoMfBdv0aOUfZBoYT2L2if Oo6QQw/32NT0aRKfayTOkxgjPZsIKldV8ZNvB9xgiNs/u9hnp6Os9HB+V6OFX7/hNrSJ akP4jm2bL2czUH9YBhyfVXQZNv69E/9hqbsVRCifjG90Atu2bYFYjUL4QM5MaBj6uHvA 610hzjfPkUJ/qG64L+9hT0l++SHRq5XggQshM1r32Y5bobat7LXd7tYCtaN8juWdWFUj Xf1Q== X-Gm-Message-State: AOAM532Bld7Cfv62f9oW6/oD8lhdEgPwDrsNY+hGPQAfCIslWzNUqd+q 1V9faJMX36Gt8UNqSus4lhd74glQY78= X-Google-Smtp-Source: ABdhPJw19qXAj7CEcYdhT07x/3GWQxftgaMAq7pgUCb46QOJXQGjiz4MhnxSR9RSPTQQoW9r94p9Xw== X-Received: by 2002:a17:906:2890:: with SMTP id o16mr19151932ejd.99.1643190560644; Wed, 26 Jan 2022 01:49:20 -0800 (PST) Received: from oberon.zico.biz ([151.251.254.11]) by smtp.gmail.com with ESMTPSA id e17sm7119155eje.218.2022.01.26.01.49.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 01:49:20 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v8 08/20] trace-cmd library: Add local helper function for data compression Date: Wed, 26 Jan 2022 11:48:54 +0200 Message-Id: <20220126094906.570451-9-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126094906.570451-1-tz.stoyanov@gmail.com> References: <20220126094906.570451-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 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 3ee8d8b4..82f3b816 100644 --- a/lib/trace-cmd/trace-output.c +++ b/lib/trace-cmd/trace-output.c @@ -331,18 +331,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); @@ -360,12 +369,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. @@ -612,7 +671,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); @@ -638,7 +697,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); @@ -2317,7 +2376,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",