From patchwork Fri Mar 4 05:56: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: 12768493 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 4C55BC433F5 for ; Fri, 4 Mar 2022 05:57:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236196AbiCDF5x (ORCPT ); Fri, 4 Mar 2022 00:57:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235836AbiCDF5v (ORCPT ); Fri, 4 Mar 2022 00:57:51 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B56BBEC5DC for ; Thu, 3 Mar 2022 21:57:03 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id u10so9408026wra.9 for ; Thu, 03 Mar 2022 21:57:03 -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=MC4hoUBaVwlPPqWyjN+arvEm9A5b+U/dTvzidU7cZec=; b=atbN12J3q1/l+kX+u8LOV0Dk/vvMPsPfOnFAEV4pPgFQivCc8Qb1qQaVDGfQzA2/6p YfYVTqO/SSGh+XMdI5giqnlZfCFKOvb6XZJnlCPaLMI2heZs5DWwhSfRoNBJLu46keC5 1dQzEet3226IvtmR77yMCN/gmMSdyB61W3AogGya3uv8UGv8jT3UFvb/WJE1NH81iKTJ YOCUy3+0V4bxtLqeMt3u7klVwhZ9sehnDGLWrawA4/UBVc1sYhAEyPq8JtNuMub3JON6 HbO4saxOESvkJdtwnI88nKHGglOLk0xxeqRYerCI9d2JnlWM1PfZmJ9udnJ7oqEc35wT t8Rg== 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=MC4hoUBaVwlPPqWyjN+arvEm9A5b+U/dTvzidU7cZec=; b=dPTGgFmYnQboJ0SVWF1FMwxyhfu4XrimgFDaPIXQ4aZPM6LB+6Un2psc3jWCmo6jVN S+dT184uOmZhngDLsV5awT33iPAisjT4gOv7K8sqgR5RnPPBroL8NMUKwLY6VpZeuPxD H5xHUWgK4Avy1zOIppW/3u0S1Jc/Z7yufNo4gL1x0Bm47izYo+w2cVsyOVwzA+P0BW6w RTQiaBvpJEPFuOpN6Nh+AM783SkxZeUVFUsAb+B0YhYrCbUz2grKDQUX/cnVM+k2uuex 0K3N5bedN2O9/AXop2x7rw+yqQXTvlv1OsP2L277UX+Sv7xjfipRirx4zMbPBanvdU+L UFKA== X-Gm-Message-State: AOAM531McTXb6ISh6bk71MUF/RjhspOPQGeeEl59mJ3sanCgRzS5FMNC 0+RusPKFT591du/ouMbki/QfEIMmrWg= X-Google-Smtp-Source: ABdhPJyrieslXKQ0Hv0dOrgDRf37kFNPC3aSNd414xIkWnUXkDTwZGxqoWqTx3Bf6MR6dLIpRQJ+aw== X-Received: by 2002:adf:e4cb:0:b0:1f0:3429:8386 with SMTP id v11-20020adfe4cb000000b001f034298386mr7750536wrm.177.1646373422259; Thu, 03 Mar 2022 21:57:02 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r10-20020a05600c35ca00b0038981d85ae8sm505074wmq.33.2022.03.03.21.57.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 21:57:01 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH v2 1/5] trace-cmd: Use a structure to describe a compression protocol Date: Fri, 4 Mar 2022 07:56:54 +0200 Message-Id: <20220304055658.440904-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220304055658.440904-1-tz.stoyanov@gmail.com> References: <20220304055658.440904-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Changed the tracecmd_compress_proto_register() function to use a structure instead of list of arguments to describe new compression protocol. That approach is more flexible and allows to extend the API in the future without changing the already implemented protocols. Suggested-by: Sebastian Andrzej Siewior Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 18 ++++++++----- lib/trace-cmd/trace-compress-zlib.c | 15 ++++++++--- lib/trace-cmd/trace-compress-zstd.c | 18 ++++++++----- lib/trace-cmd/trace-compress.c | 26 +++++++------------ 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index d229b264..0ea37abc 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -514,6 +514,16 @@ struct tracecmd_compress_chunk { off64_t offset; }; struct tracecmd_compression; +struct tracecmd_compression_proto { + int weight; + const char *name; + const char *version; + int (*compress)(const char *in, unsigned int in_bytes, char *out, unsigned int *out_bytes); + int (*uncompress)(const char *in, unsigned int in_bytes, char *out, unsigned int *out_bytes); + unsigned int (*compress_size)(unsigned int bytes); + bool (*is_supported)(const char *name, const char *version); +}; + struct tracecmd_compression *tracecmd_compress_alloc(const char *name, const char *version, int fd, struct tep_handle *tep, struct tracecmd_msg_handle *msg_handle); @@ -530,13 +540,7 @@ int tracecmd_compress_proto_get_name(struct tracecmd_compression *compress, const char **name, const char **version); bool tracecmd_compress_is_supported(const char *name, const char *version); int tracecmd_compress_protos_get(char ***names, char ***versions); -int tracecmd_compress_proto_register(const char *name, const char *version, int weight, - int (*compress)(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes), - int (*uncompress)(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes), - unsigned int (*comress_size)(unsigned int bytes), - bool (*is_supported)(const char *name, const char *version)); +int tracecmd_compress_proto_register(struct tracecmd_compression_proto *proto); int tracecmd_compress_copy_from(struct tracecmd_compression *handle, int fd, int chunk_size, unsigned long long *read_size, unsigned long long *write_size); int tracecmd_uncompress_copy_to(struct tracecmd_compression *handle, int fd, diff --git a/lib/trace-cmd/trace-compress-zlib.c b/lib/trace-cmd/trace-compress-zlib.c index a697cc61..8b9758c9 100644 --- a/lib/trace-cmd/trace-compress-zlib.c +++ b/lib/trace-cmd/trace-compress-zlib.c @@ -103,7 +103,16 @@ static bool zlib_is_supported(const char *name, const char *version) int tracecmd_zlib_init(void) { - return tracecmd_compress_proto_register(__ZLIB_NAME, zlibVersion(), __ZLIB_WEIGTH, - zlib_compress, zlib_decompress, - zlib_compress_bound, zlib_is_supported); + struct tracecmd_compression_proto proto; + + memset(&proto, 0, sizeof(proto)); + proto.name = __ZLIB_NAME; + proto.version = zlibVersion(); + proto.weight = __ZLIB_WEIGTH; + proto.compress = zlib_compress; + proto.uncompress = zlib_decompress; + proto.is_supported = zlib_is_supported; + proto.compress_size = zlib_compress_bound; + + return tracecmd_compress_proto_register(&proto); } diff --git a/lib/trace-cmd/trace-compress-zstd.c b/lib/trace-cmd/trace-compress-zstd.c index fc5e350f..f99ad312 100644 --- a/lib/trace-cmd/trace-compress-zstd.c +++ b/lib/trace-cmd/trace-compress-zstd.c @@ -59,9 +59,19 @@ static bool zstd_is_supported(const char *name, const char *version) int tracecmd_zstd_init(void) { + struct tracecmd_compression_proto proto; int ret = 0; size_t r; + memset(&proto, 0, sizeof(proto)); + proto.name = __ZSTD_NAME; + proto.version = ZSTD_versionString(); + proto.weight = __ZSTD_WEIGTH; + proto.compress = zstd_compress; + proto.uncompress = zstd_decompress; + proto.is_supported = zstd_is_supported; + proto.compress_size = zstd_compress_bound; + ctx_c = ZSTD_createCCtx(); ctx_d = ZSTD_createDCtx(); if (!ctx_c || !ctx_d) @@ -71,13 +81,7 @@ int tracecmd_zstd_init(void) if (ZSTD_isError(r)) goto err; - ret = tracecmd_compress_proto_register(__ZSTD_NAME, - ZSTD_versionString(), - __ZSTD_WEIGTH, - zstd_compress, - zstd_decompress, - zstd_compress_bound, - zstd_is_supported); + ret = tracecmd_compress_proto_register(&proto); if (!ret) return 0; err: diff --git a/lib/trace-cmd/trace-compress.c b/lib/trace-cmd/trace-compress.c index 4fca7019..66bfc356 100644 --- a/lib/trace-cmd/trace-compress.c +++ b/lib/trace-cmd/trace-compress.c @@ -522,39 +522,33 @@ int tracecmd_compress_proto_get_name(struct tracecmd_compression *compress, * Returns 0 on success, or -1 in case of an error. If algorithm with given name * and version is already registered, -1 is returned. */ -int tracecmd_compress_proto_register(const char *name, const char *version, int weight, - int (*compress)(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes), - int (*uncompress)(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes), - unsigned int (*compress_size)(unsigned int bytes), - bool (*is_supported)(const char *name, const char *version)) +int tracecmd_compress_proto_register(struct tracecmd_compression_proto *proto) { struct compress_proto *new; - if (!name || !compress || !uncompress) + if (!proto || !proto->name || !proto->compress || !proto->uncompress) return -1; - if (tracecmd_compress_is_supported(name, version)) + if (tracecmd_compress_is_supported(proto->name, proto->version)) return -1; new = calloc(1, sizeof(*new)); if (!new) return -1; - new->proto_name = strdup(name); + new->proto_name = strdup(proto->name); if (!new->proto_name) goto error; - new->proto_version = strdup(version); + new->proto_version = strdup(proto->version); if (!new->proto_version) goto error; - new->compress_block = compress; - new->uncompress_block = uncompress; - new->compress_size = compress_size; - new->is_supported = is_supported; - new->weight = weight; + new->compress_block = proto->compress; + new->uncompress_block = proto->uncompress; + new->compress_size = proto->compress_size; + new->is_supported = proto->is_supported; + new->weight = proto->weight; new->next = proto_list; proto_list = new; return 0; From patchwork Fri Mar 4 05:56:55 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: 12768494 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 78241C433EF for ; Fri, 4 Mar 2022 05:57:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235836AbiCDF5y (ORCPT ); Fri, 4 Mar 2022 00:57:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229789AbiCDF5w (ORCPT ); Fri, 4 Mar 2022 00:57:52 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2FABEC5EA for ; Thu, 3 Mar 2022 21:57:04 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id j17so11122944wrc.0 for ; Thu, 03 Mar 2022 21:57:04 -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=bHD1J2VTUF2CyUdXN6vH9kOhGNENn+EXQmTCEV5ycyM=; b=d0XSCrYM1tKgfPGe5rh/fF7XWXw1D0YhEy9ZvuA3iOKYDpi8/0zIckDnWmdfrGllCN elSeEKE2bruBwnYR1eHJQVVp9BRY4frq97+WjxCJ5fn6mDif6meJoMdodSWs41n8FsO/ lM2MiD+QhR5mBZGGj5Saa3uWAfa4a48Sv1LTxVYlEzfWsF56R+jvfuv40dNGaGr6T40N yzRAHapTIFV+jAyh8AU/yJSvCVrpg4cdoMRB5qiFHpxAKlNxx2Vdot6Rz0QUCUEJo1w2 HuWP+OE2QUJoJ17vO+6iOMCbCblM8CBgjhfJbLm2YTUHxCtxoyROeaDrwZW31wALoy1/ 3liA== 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=bHD1J2VTUF2CyUdXN6vH9kOhGNENn+EXQmTCEV5ycyM=; b=aXz2Lr1mzIjxR/7goDxrVFsMla+BHZEso9fHAEh3sCUhd7GFIB2ziDMjDhjX3WUdks tocxA2kF8lmpuSqSFVJE2LW/e6qrE0/u2wFDzkvoYFflaF76AV//swFBbUOw+oBeWu/9 mB4YIYfbpy4kpaoF9zAdI6W87WpQ7c/K9xAGvlMQrkQ0in8dERE5TajLoKBgSA6MrZWB IL7qK9vt3T4C2D/T4Iw4t17Tq5T0QOlD2/XVXQrAQMP2kULahGWp0xYF458/TaoE1rJ+ 8kkQKBKGZYVZR4tskJBVsSvFR/vrmkPoL+R4UIgDFzpgNCpvBxBplXRLKBO+2JlkaOwd wd3g== X-Gm-Message-State: AOAM530ouIOcvFE1BVtVnEd4j/cMtkYNIyk7DJJfezaoLWtiEfm5WWdM NVj2vOA9SeK4ZCoH85hesgc= X-Google-Smtp-Source: ABdhPJwDOpkrJW1eWrnBrBUYZi/okb+NKxMNd5rcf8wIYCiXFfVrLfgz5TViYIHvyoYO4tAP8z6bAw== X-Received: by 2002:a05:6000:2a7:b0:1f0:b26a:38b with SMTP id l7-20020a05600002a700b001f0b26a038bmr430088wry.23.1646373423455; Thu, 03 Mar 2022 21:57:03 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r10-20020a05600c35ca00b0038981d85ae8sm505074wmq.33.2022.03.03.21.57.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 21:57:02 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH v2 2/5] trace-cmd: Make internal compression hooks more generic Date: Fri, 4 Mar 2022 07:56:55 +0200 Message-Id: <20220304055658.440904-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220304055658.440904-1-tz.stoyanov@gmail.com> References: <20220304055658.440904-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Changed the prototypes of trace-cmd internal compression API to be more generic. Suggested-by: Sebastian Andrzej Siewior Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 4 +-- lib/trace-cmd/trace-compress-zlib.c | 20 +++++------ lib/trace-cmd/trace-compress-zstd.c | 18 +++++----- lib/trace-cmd/trace-compress.c | 33 ++++++++----------- 4 files changed, 32 insertions(+), 43 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 0ea37abc..45d89270 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -518,8 +518,8 @@ struct tracecmd_compression_proto { int weight; const char *name; const char *version; - int (*compress)(const char *in, unsigned int in_bytes, char *out, unsigned int *out_bytes); - int (*uncompress)(const char *in, unsigned int in_bytes, char *out, unsigned int *out_bytes); + int (*compress)(const void *in, int in_bytes, void *out, int out_bytes); + int (*uncompress)(const void *in, int in_bytes, void *out, int out_bytes); unsigned int (*compress_size)(unsigned int bytes); bool (*is_supported)(const char *name, const char *version); }; diff --git a/lib/trace-cmd/trace-compress-zlib.c b/lib/trace-cmd/trace-compress-zlib.c index 8b9758c9..41342597 100644 --- a/lib/trace-cmd/trace-compress-zlib.c +++ b/lib/trace-cmd/trace-compress-zlib.c @@ -13,19 +13,17 @@ #define __ZLIB_NAME "zlib" #define __ZLIB_WEIGTH 10 -static int zlib_compress(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes) +static int zlib_compress(const void *in, int in_bytes, void *out, int out_bytes) { - unsigned long out_size = *out_bytes; + unsigned long obytes = out_bytes; int ret; - ret = compress2((unsigned char *)out, &out_size, + ret = compress2((unsigned char *)out, &obytes, (unsigned char *)in, (unsigned long)in_bytes, Z_BEST_COMPRESSION); - *out_bytes = out_size; errno = 0; switch (ret) { case Z_OK: - return 0; + return obytes; case Z_BUF_ERROR: errno = -ENOBUFS; break; @@ -43,19 +41,17 @@ static int zlib_compress(const char *in, unsigned int in_bytes, return -1; } -static int zlib_decompress(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes) +static int zlib_decompress(const void *in, int in_bytes, void *out, int out_bytes) { - unsigned long out_size = *out_bytes; + unsigned long obytes = out_bytes; int ret; - ret = uncompress((unsigned char *)out, &out_size, + ret = uncompress((unsigned char *)out, &obytes, (unsigned char *)in, (unsigned long)in_bytes); - *out_bytes = out_size; errno = 0; switch (ret) { case Z_OK: - return 0; + return obytes; case Z_BUF_ERROR: errno = -ENOBUFS; break; diff --git a/lib/trace-cmd/trace-compress-zstd.c b/lib/trace-cmd/trace-compress-zstd.c index f99ad312..eee4e5d6 100644 --- a/lib/trace-cmd/trace-compress-zstd.c +++ b/lib/trace-cmd/trace-compress-zstd.c @@ -15,31 +15,29 @@ static ZSTD_CCtx *ctx_c; static ZSTD_DCtx *ctx_d; -static int zstd_compress(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes) +static int zstd_compress(const void *in, int in_bytes, void *out, int out_bytes) { size_t ret; - ret = ZSTD_compress2(ctx_c, out, *out_bytes, in, in_bytes); + ret = ZSTD_compress2(ctx_c, out, out_bytes, in, in_bytes); if (ZSTD_isError(ret)) return -1; - *out_bytes = ret; - return 0; + + return ret; } -static int zstd_decompress(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes) +static int zstd_decompress(const void *in, int in_bytes, void *out, int out_bytes) { size_t ret; - ret = ZSTD_decompressDCtx(ctx_d, out, *out_bytes, in, in_bytes); + ret = ZSTD_decompressDCtx(ctx_d, out, out_bytes, in, in_bytes); if (ZSTD_isError(ret)) { errno = -EINVAL; return -1; } - *out_bytes = ret; + errno = 0; - return 0; + return ret; } static unsigned int zstd_compress_bound(unsigned int in_bytes) diff --git a/lib/trace-cmd/trace-compress.c b/lib/trace-cmd/trace-compress.c index 66bfc356..6263439c 100644 --- a/lib/trace-cmd/trace-compress.c +++ b/lib/trace-cmd/trace-compress.c @@ -18,10 +18,8 @@ struct compress_proto { char *proto_version; int weight; - int (*compress_block)(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes); - int (*uncompress_block)(const char *in, unsigned int in_bytes, - char *out, unsigned int *out_bytes); + int (*compress_block)(const void *in, int in_bytes, void *out, int out_bytes); + int (*uncompress_block)(const void *in, int in_bytes, void *out, int out_bytes); unsigned int (*compress_size)(unsigned int bytes); bool (*is_supported)(const char *name, const char *version); }; @@ -273,15 +271,13 @@ int tracecmd_uncompress_block(struct tracecmd_compression *handle) if (read_fd(handle->fd, bytes, s_compressed) < 0) goto error; - s_uncompressed = size; - ret = handle->proto->uncompress_block(bytes, s_compressed, - handle->buffer, &s_uncompressed); - if (ret) + ret = handle->proto->uncompress_block(bytes, s_compressed, handle->buffer, size); + if (ret < 0) goto error; free(bytes); handle->pointer = 0; - handle->capacity_read = s_uncompressed; + handle->capacity_read = ret; handle->capacity = size; return 0; error: @@ -318,12 +314,12 @@ int tracecmd_compress_block(struct tracecmd_compression *handle) if (!buf) return -1; - ret = handle->proto->compress_block(handle->buffer, handle->pointer, buf, &size); + ret = handle->proto->compress_block(handle->buffer, handle->pointer, buf, size); if (ret < 0) goto out; /* Write compressed data size */ - endian4 = tep_read_number(handle->tep, &size, 4); + endian4 = tep_read_number(handle->tep, &ret, 4); ret = do_write(handle, &endian4, 4); if (ret != 4) goto out; @@ -710,12 +706,13 @@ int tracecmd_compress_copy_from(struct tracecmd_compression *handle, int fd, int rsize += all; size = csize; if (all > 0) { - ret = handle->proto->compress_block(buf_from, all, buf_to, &size); + ret = handle->proto->compress_block(buf_from, all, buf_to, size); if (ret < 0) { if (errno == EINTR) continue; break; } + size = ret; /* Write compressed data size */ endian4 = tep_read_number(handle->tep, &size, 4); ret = write_fd(handle->fd, &endian4, 4); @@ -851,7 +848,6 @@ int tracecmd_uncompress_chunk(struct tracecmd_compression *handle, struct tracecmd_compress_chunk *chunk, char *data) { char *bytes_in = NULL; - unsigned int size; int ret = -1; if (!handle || !handle->proto || !handle->proto->uncompress_block || !chunk || !data) @@ -867,8 +863,7 @@ int tracecmd_uncompress_chunk(struct tracecmd_compression *handle, if (read_fd(handle->fd, bytes_in, chunk->zsize) < 0) goto out; - size = chunk->size; - if (handle->proto->uncompress_block(bytes_in, chunk->zsize, data, &size)) + if (handle->proto->uncompress_block(bytes_in, chunk->zsize, data, chunk->size) < 0) goto out; ret = 0; @@ -954,12 +949,12 @@ int tracecmd_uncompress_copy_to(struct tracecmd_compression *handle, int fd, rsize += s_compressed; ret = handle->proto->uncompress_block(bytes_in, s_compressed, - bytes_out, &s_uncompressed); - if (ret) + bytes_out, s_uncompressed); + if (ret < 0) break; - write_fd(fd, bytes_out, s_uncompressed); - wsize += s_uncompressed; + write_fd(fd, bytes_out, ret); + wsize += ret; chunks--; } free(bytes_in); From patchwork Fri Mar 4 05:56:56 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: 12768495 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 507E4C433EF for ; Fri, 4 Mar 2022 05:57:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229789AbiCDF5y (ORCPT ); Fri, 4 Mar 2022 00:57:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236045AbiCDF5w (ORCPT ); Fri, 4 Mar 2022 00:57:52 -0500 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4B43EC5D7 for ; Thu, 3 Mar 2022 21:57:05 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id j26so682200wrb.1 for ; Thu, 03 Mar 2022 21:57:05 -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=FWAEPmib45XN5vMBOEv5P8Eu5JsJutcrBsuYJjzpe3c=; b=fpSvP/thLGi9a7/r5dq34uKQY4pmNGvPjpEZ6BLD/2yaMbv3nCtqOIGZS9nL/er2Ke SG66wUzntuEztJOsbs2JZfOqWqHjmJhoOkzYigTNOh4aun1ma+QZsHeJv521HWPsruhI uY9ywOPnF3JYMSuSUMia5fhkfgDT8e4A6M6GKQs5jaFrtR6j91r6h7paiqTxYtXpBVLI AGG78MdghrxWXAeppisUy/GDFBRZKOGVrrHZ4e15jljyG3n1U0vWg5MN6vETqpK1G6iV CKvjI+zdl/MzRSmDWRXex6eNs3kURb9byFV4UR1hI20SlUdWJy2oaVOF5bw5ki2hepN5 9cUw== 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=FWAEPmib45XN5vMBOEv5P8Eu5JsJutcrBsuYJjzpe3c=; b=qt9L/l4c9c4pFTduw/rdCLBOz4lIvSSmgYow7Ewo/DrJemWL+OK/3RoLXXPPQ1N7K1 1ubC5f6i8wZ+ogmZzmdq7ErcUgCb+Njo9oAB4j0OUqEoHMxuOS4rJLMr5ZPHOW2yj1TP bOMbShwFzRX7UjBC3DpI5jRFvNqYXMvHIHWRmACJCpgVBqHWdY2bkGHy2oAgzMrdYS53 rmgaLFwUDRmqGCBOqLXHbGNMgpSHknKcoISJgr4NsY5lk30wE5MnKrI9l0d/95PgliE7 8DA1e5DY4ors/v8MP2jAmlkbyM4pyDTAukFnNiYfQOzT6VC6zUuUjQh29OXFVzbeGihl 4igw== X-Gm-Message-State: AOAM531zuMmipbm3jFB3JYeRKd8Nd9+dWggufXdoQUWWdAxoGIEo8iBL WBQPZtC37AKgXC6GUODrMTiLrk7S0kY= X-Google-Smtp-Source: ABdhPJwZkmATZhZLaozefkNmdCqrLksC5DVOreK/csgHbM/bVlKqWyl5nEPHiYzqTBua4PNLYsrYNQ== X-Received: by 2002:a05:6000:178f:b0:1f0:4d60:1628 with SMTP id e15-20020a056000178f00b001f04d601628mr4681923wrg.40.1646373424445; Thu, 03 Mar 2022 21:57:04 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r10-20020a05600c35ca00b0038981d85ae8sm505074wmq.33.2022.03.03.21.57.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 21:57:03 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH v2 3/5] trace-cmd: Use errno from zlib, if available Date: Fri, 4 Mar 2022 07:56:56 +0200 Message-Id: <20220304055658.440904-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220304055658.440904-1-tz.stoyanov@gmail.com> References: <20220304055658.440904-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Some zlib APIs set the errno in case of an error and return Z_ERRNO. In these cases, errno should not be overwritten by the tarce-cmd zlib wrappers. Suggested-by: Sebastian Andrzej Siewior Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-compress-zlib.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/trace-cmd/trace-compress-zlib.c b/lib/trace-cmd/trace-compress-zlib.c index 41342597..fd43d879 100644 --- a/lib/trace-cmd/trace-compress-zlib.c +++ b/lib/trace-cmd/trace-compress-zlib.c @@ -20,7 +20,6 @@ static int zlib_compress(const void *in, int in_bytes, void *out, int out_bytes) ret = compress2((unsigned char *)out, &obytes, (unsigned char *)in, (unsigned long)in_bytes, Z_BEST_COMPRESSION); - errno = 0; switch (ret) { case Z_OK: return obytes; @@ -33,6 +32,8 @@ static int zlib_compress(const void *in, int in_bytes, void *out, int out_bytes) case Z_STREAM_ERROR: errno = -EINVAL; break; + case Z_ERRNO: + break; default: errno = -EFAULT; break; @@ -48,7 +49,6 @@ static int zlib_decompress(const void *in, int in_bytes, void *out, int out_byte ret = uncompress((unsigned char *)out, &obytes, (unsigned char *)in, (unsigned long)in_bytes); - errno = 0; switch (ret) { case Z_OK: return obytes; @@ -61,6 +61,8 @@ static int zlib_decompress(const void *in, int in_bytes, void *out, int out_byte case Z_DATA_ERROR: errno = -EINVAL; break; + case Z_ERRNO: + break; default: errno = -EFAULT; break; From patchwork Fri Mar 4 05:56:57 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: 12768497 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 B90A9C433F5 for ; Fri, 4 Mar 2022 05:57:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236425AbiCDF57 (ORCPT ); Fri, 4 Mar 2022 00:57:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236265AbiCDF5y (ORCPT ); Fri, 4 Mar 2022 00:57:54 -0500 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F8A1EC5DC for ; Thu, 3 Mar 2022 21:57:07 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id i9-20020a1c3b09000000b00380f6f473b0so232288wma.1 for ; Thu, 03 Mar 2022 21:57:07 -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=71i7Wft610lYhRSTxxvgqEXn/8Ru22/+yKbd1RBssnQ=; b=JHetttTNycAGzds3Ov831jjUpHE5gwzCEx3oNAiLTzO4cu51cCXHQEwsj/yVtasrey QYaXJfgQOMcv8I0uCR6C25iBaLsesk+XiCgRMUVlycAl7zbvkQCwGEOKZLcHMyDy8jq6 afdwl6B0cAb3G7MV8oM7H+HsfcvHjZGnsSwyrYtFd8RfdiA1/wXWQ90PvbjxBF9IpcMN mU7BbtXw4x5yVOZFE5CUZp4C+QklOac5p2MHpet/Od8QpMUbqBmX/RjBDH2kpOz0q1jE N+mIivnXd0kqbXUHLpfrT3rZ+E1lxc8B/3jG9EMelz7MlrS1/9jutjWU6nSit3BVKQ0x J0mw== 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=71i7Wft610lYhRSTxxvgqEXn/8Ru22/+yKbd1RBssnQ=; b=adoE/RGCcYuGPDAaAAEMD0XgaFcq9tUnpTts8Qwax/rWU7TjmeNv6hSS1G//HZ2jfx Wowj4OIfHKyOB65rtK54H0tuoy8jCVd0ifL3WMik8e9QPd3sMWK05nM4DwZ7V8fCIkUC lgwPMXMdR+FZGxfOVzVW1J+TEqxweK0hkzB25fPGzEvGFMi1OEOGLcoXCN3OLrVejbDf awISp/2H7SHg/s9sFjNiiPFR2X+/1CAimgEjzfTcZRJ0ymJsJrOo78XHHH5a9kJX8I1i Y5gOeJ1CJHxhI34HyOJl+dvTd9ix8TDVNI6MnOl05RTj7mlFEQOaKzDXAQ6GWhwZ8LqQ ZF2w== X-Gm-Message-State: AOAM532kM9vLlAzeUGgrUnwai26ycT6s+hk80gKvPMeoyb4yWaFygG53 TpmVjqM5zvzB7FquPUwLnsy1V6LOALY= X-Google-Smtp-Source: ABdhPJwYRa8gcVCdl6+49VHBz09oc388BdCunnerTQ6t9kWbb8vBJsrB6N7d1GKvI/viU2uPZ3NIOQ== X-Received: by 2002:a1c:ed1a:0:b0:37f:a539:5d5e with SMTP id l26-20020a1ced1a000000b0037fa5395d5emr6393249wmh.37.1646373425668; Thu, 03 Mar 2022 21:57:05 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r10-20020a05600c35ca00b0038981d85ae8sm505074wmq.33.2022.03.03.21.57.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 21:57:05 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH v2 4/5] trace-cmd: Add context to compression hooks Date: Fri, 4 Mar 2022 07:56:57 +0200 Message-Id: <20220304055658.440904-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220304055658.440904-1-tz.stoyanov@gmail.com> References: <20220304055658.440904-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Some compression libraries require a context for compression and decompression. Currently the internal compression API does not have such context. That could cause problems in the future, if a multithread compression logic is implemented. Suggested-by: Sebastian Andrzej Siewior Signed-off-by: Tzvetomir Stoyanov (VMware) --- .../include/private/trace-cmd-private.h | 8 ++-- lib/trace-cmd/trace-compress-zlib.c | 6 +-- lib/trace-cmd/trace-compress-zstd.c | 6 +-- lib/trace-cmd/trace-compress.c | 38 ++++++++++++++----- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 45d89270..69343765 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -518,10 +518,12 @@ struct tracecmd_compression_proto { int weight; const char *name; const char *version; - int (*compress)(const void *in, int in_bytes, void *out, int out_bytes); - int (*uncompress)(const void *in, int in_bytes, void *out, int out_bytes); - unsigned int (*compress_size)(unsigned int bytes); + int (*compress)(void *ctx, const void *in, int in_bytes, void *out, int out_bytes); + int (*uncompress)(void *ctx, const void *in, int in_bytes, void *out, int out_bytes); + unsigned int (*compress_size)(void *ctx, unsigned int bytes); bool (*is_supported)(const char *name, const char *version); + void *(*new_context)(void); + void (*free_context)(void *ctx); }; struct tracecmd_compression *tracecmd_compress_alloc(const char *name, const char *version, diff --git a/lib/trace-cmd/trace-compress-zlib.c b/lib/trace-cmd/trace-compress-zlib.c index fd43d879..413a0764 100644 --- a/lib/trace-cmd/trace-compress-zlib.c +++ b/lib/trace-cmd/trace-compress-zlib.c @@ -13,7 +13,7 @@ #define __ZLIB_NAME "zlib" #define __ZLIB_WEIGTH 10 -static int zlib_compress(const void *in, int in_bytes, void *out, int out_bytes) +static int zlib_compress(void *ctx, const void *in, int in_bytes, void *out, int out_bytes) { unsigned long obytes = out_bytes; int ret; @@ -42,7 +42,7 @@ static int zlib_compress(const void *in, int in_bytes, void *out, int out_bytes) return -1; } -static int zlib_decompress(const void *in, int in_bytes, void *out, int out_bytes) +static int zlib_decompress(void *ctx, const void *in, int in_bytes, void *out, int out_bytes) { unsigned long obytes = out_bytes; int ret; @@ -71,7 +71,7 @@ static int zlib_decompress(const void *in, int in_bytes, void *out, int out_byte return -1; } -static unsigned int zlib_compress_bound(unsigned int in_bytes) +static unsigned int zlib_compress_bound(void *ctx, unsigned int in_bytes) { return compressBound(in_bytes); } diff --git a/lib/trace-cmd/trace-compress-zstd.c b/lib/trace-cmd/trace-compress-zstd.c index eee4e5d6..98eaac00 100644 --- a/lib/trace-cmd/trace-compress-zstd.c +++ b/lib/trace-cmd/trace-compress-zstd.c @@ -15,7 +15,7 @@ static ZSTD_CCtx *ctx_c; static ZSTD_DCtx *ctx_d; -static int zstd_compress(const void *in, int in_bytes, void *out, int out_bytes) +static int zstd_compress(void *ctx, const void *in, int in_bytes, void *out, int out_bytes) { size_t ret; @@ -26,7 +26,7 @@ static int zstd_compress(const void *in, int in_bytes, void *out, int out_bytes) return ret; } -static int zstd_decompress(const void *in, int in_bytes, void *out, int out_bytes) +static int zstd_decompress(void *ctx, const void *in, int in_bytes, void *out, int out_bytes) { size_t ret; @@ -40,7 +40,7 @@ static int zstd_decompress(const void *in, int in_bytes, void *out, int out_byte return ret; } -static unsigned int zstd_compress_bound(unsigned int in_bytes) +static unsigned int zstd_compress_bound(void *ctx, unsigned int in_bytes) { return ZSTD_compressBound(in_bytes); } diff --git a/lib/trace-cmd/trace-compress.c b/lib/trace-cmd/trace-compress.c index 6263439c..aa03d17c 100644 --- a/lib/trace-cmd/trace-compress.c +++ b/lib/trace-cmd/trace-compress.c @@ -18,10 +18,12 @@ struct compress_proto { char *proto_version; int weight; - int (*compress_block)(const void *in, int in_bytes, void *out, int out_bytes); - int (*uncompress_block)(const void *in, int in_bytes, void *out, int out_bytes); - unsigned int (*compress_size)(unsigned int bytes); + int (*compress_block)(void *ctx, const void *in, int in_bytes, void *out, int out_bytes); + int (*uncompress_block)(void *ctx, const void *in, int in_bytes, void *out, int out_bytes); + unsigned int (*compress_size)(void *ctx, unsigned int bytes); bool (*is_supported)(const char *name, const char *version); + void *(*new_context)(void); + void (*free_context)(void *ctx); }; static struct compress_proto *proto_list; @@ -35,6 +37,7 @@ struct tracecmd_compression { struct compress_proto *proto; struct tep_handle *tep; struct tracecmd_msg_handle *msg_handle; + void *context; }; static int read_fd(int fd, char *dst, int len) @@ -271,7 +274,8 @@ int tracecmd_uncompress_block(struct tracecmd_compression *handle) if (read_fd(handle->fd, bytes, s_compressed) < 0) goto error; - ret = handle->proto->uncompress_block(bytes, s_compressed, handle->buffer, size); + ret = handle->proto->uncompress_block(handle->context, + bytes, s_compressed, handle->buffer, size); if (ret < 0) goto error; @@ -308,13 +312,13 @@ int tracecmd_compress_block(struct tracecmd_compression *handle) !handle->proto->compress_size || !handle->proto->compress_block) return -1; - size = handle->proto->compress_size(handle->pointer); + size = handle->proto->compress_size(handle->context, handle->pointer); buf = malloc(size); if (!buf) return -1; - ret = handle->proto->compress_block(handle->buffer, handle->pointer, buf, size); + ret = handle->proto->compress_block(handle->context, handle->buffer, handle->pointer, buf, size); if (ret < 0) goto out; @@ -443,6 +447,9 @@ struct tracecmd_compression *tracecmd_compress_alloc(const char *name, const cha new->tep = tep; new->msg_handle = msg_handle; new->proto = proto; + if (proto->new_context) + new->context = proto->new_context(); + return new; } @@ -452,7 +459,14 @@ struct tracecmd_compression *tracecmd_compress_alloc(const char *name, const cha */ void tracecmd_compress_destroy(struct tracecmd_compression *handle) { + if (!handle) + return; + tracecmd_compress_reset(handle); + + if (handle->proto && handle->proto->free_context) + handle->proto->free_context(handle->context); + free(handle); } @@ -546,6 +560,8 @@ int tracecmd_compress_proto_register(struct tracecmd_compression_proto *proto) new->is_supported = proto->is_supported; new->weight = proto->weight; new->next = proto_list; + new->new_context = proto->new_context; + new->free_context = proto->free_context; proto_list = new; return 0; @@ -672,7 +688,7 @@ int tracecmd_compress_copy_from(struct tracecmd_compression *handle, int fd, int if (read_size) rmax = *read_size; - csize = handle->proto->compress_size(chunk_size); + csize = handle->proto->compress_size(handle->context, chunk_size); buf_from = malloc(chunk_size); if (!buf_from) return -1; @@ -706,7 +722,8 @@ int tracecmd_compress_copy_from(struct tracecmd_compression *handle, int fd, int rsize += all; size = csize; if (all > 0) { - ret = handle->proto->compress_block(buf_from, all, buf_to, size); + ret = handle->proto->compress_block(handle->context, + buf_from, all, buf_to, size); if (ret < 0) { if (errno == EINTR) continue; @@ -863,7 +880,8 @@ int tracecmd_uncompress_chunk(struct tracecmd_compression *handle, if (read_fd(handle->fd, bytes_in, chunk->zsize) < 0) goto out; - if (handle->proto->uncompress_block(bytes_in, chunk->zsize, data, chunk->size) < 0) + if (handle->proto->uncompress_block(handle->context, + bytes_in, chunk->zsize, data, chunk->size) < 0) goto out; ret = 0; @@ -948,7 +966,7 @@ int tracecmd_uncompress_copy_to(struct tracecmd_compression *handle, int fd, break; rsize += s_compressed; - ret = handle->proto->uncompress_block(bytes_in, s_compressed, + ret = handle->proto->uncompress_block(handle->context, bytes_in, s_compressed, bytes_out, s_uncompressed); if (ret < 0) break; From patchwork Fri Mar 4 05:56:58 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: 12768496 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 5361EC433FE for ; Fri, 4 Mar 2022 05:57:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236045AbiCDF56 (ORCPT ); Fri, 4 Mar 2022 00:57:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236425AbiCDF5z (ORCPT ); Fri, 4 Mar 2022 00:57:55 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12EE6EC5D7 for ; Thu, 3 Mar 2022 21:57:08 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id r10so11058730wrp.3 for ; Thu, 03 Mar 2022 21:57:08 -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=8FxFLGX1BEOBUVRtvIrxUM+uHMEWlXhooVRPrdsAHi4=; b=KUfhPDHSwqg2zPwf/kYUbWPBjl5rCjzyp+R9OctcYtKypSbkvDgx69SQf2IHL+E8mV RlFDXLXuu44JUje3BQDXQidDALUI3Z7oHKgKVfAT/L0uelnY4SrMp6wTB5wk5PCS2NRI mySaqB7dUhpqWtHCbPSNDOykPhY9AlAGT4tvX32AcXa4Hv4hulMqISqRE5AFe41qfRo5 GuIEzwSv6AEv7kVeFl1rvoHzrgiAo57CMRWwUyatzWcfHHqt70nvQSCIFnTuNMg6or3Y MaguHt8F/uWIeCJHCB9FX5nTm5JhW7hmyisqSf+l9Hw2s7s0KmF5MrjkE5d1PFQQBnhR O40A== 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=8FxFLGX1BEOBUVRtvIrxUM+uHMEWlXhooVRPrdsAHi4=; b=n96G8Cqa8p+n4/dzoUtwAJDFdaztqChA92/HqQ099agb2Akeo9QQskc74cvEFTE9+6 Zq193zOMV3CWCkAoJjorqSHO58+VYP3nTcUMge9XIQj+ChwX0RFbig8L4BRYVp3K7hrn eptBW5FpCggzP62ifsX3AkdliTWT0kZf6sbRLOu7e7DDIyuEJFWODokMFdEGIO9MVLSG VKf1vbCXiIY48klfjlJjeD7C+ypwdmTNtKzmvxgDZnxUUndR05d1I8hBo9TsAJrpurqu QC5I4WioAQcuMkh3jguEpEnfAf2y5KQ3nluKjDY9WXzp0/0ZkQ6STMWhfefHXUwei7Rl lu3w== X-Gm-Message-State: AOAM5336RMhNMeVeglrLlRhWIk+4b0nQla+oRmDYeQzBmQ4MRVdtzl3g aAMJO7oA231QIsWxV7OX/y7CTA9eKuI= X-Google-Smtp-Source: ABdhPJxq6UDHg9oUSFk8Yr733Uaw7vRe6VWWr/NUU+1rVoAsw+iMmo4KP6CRuXNBaxZheOo+0wwc6Q== X-Received: by 2002:adf:e74a:0:b0:1f0:25cb:3ad5 with SMTP id c10-20020adfe74a000000b001f025cb3ad5mr8425339wrn.231.1646373426711; Thu, 03 Mar 2022 21:57:06 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id r10-20020a05600c35ca00b0038981d85ae8sm505074wmq.33.2022.03.03.21.57.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 21:57:06 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH v2 5/5] trace-cmd: Use context hooks in zstd Date: Fri, 4 Mar 2022 07:56:58 +0200 Message-Id: <20220304055658.440904-6-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220304055658.440904-1-tz.stoyanov@gmail.com> References: <20220304055658.440904-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Using global compression context could be a limitation in multithreaded use cases. Use context hooks instead of global compression context. Suggested-by: Sebastian Andrzej Siewior Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/trace-compress-zstd.c | 80 +++++++++++++++++++---------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/lib/trace-cmd/trace-compress-zstd.c b/lib/trace-cmd/trace-compress-zstd.c index 98eaac00..3e42fc79 100644 --- a/lib/trace-cmd/trace-compress-zstd.c +++ b/lib/trace-cmd/trace-compress-zstd.c @@ -12,14 +12,20 @@ #define __ZSTD_NAME "zstd" #define __ZSTD_WEIGTH 5 -static ZSTD_CCtx *ctx_c; -static ZSTD_DCtx *ctx_d; +struct zstd_context { + ZSTD_CCtx *ctx_c; + ZSTD_DCtx *ctx_d; +}; static int zstd_compress(void *ctx, const void *in, int in_bytes, void *out, int out_bytes) { + struct zstd_context *context = ctx; size_t ret; - ret = ZSTD_compress2(ctx_c, out, out_bytes, in, in_bytes); + if (!ctx) + return -1; + + ret = ZSTD_compress2(context->ctx_c, out, out_bytes, in, in_bytes); if (ZSTD_isError(ret)) return -1; @@ -28,9 +34,13 @@ static int zstd_compress(void *ctx, const void *in, int in_bytes, void *out, int static int zstd_decompress(void *ctx, const void *in, int in_bytes, void *out, int out_bytes) { + struct zstd_context *context = ctx; size_t ret; - ret = ZSTD_decompressDCtx(ctx_d, out, out_bytes, in, in_bytes); + if (!ctx) + return -1; + + ret = ZSTD_decompressDCtx(context->ctx_d, out, out_bytes, in, in_bytes); if (ZSTD_isError(ret)) { errno = -EINVAL; return -1; @@ -55,11 +65,46 @@ static bool zstd_is_supported(const char *name, const char *version) return true; } +static void *new_zstd_context(void) +{ + struct zstd_context *context; + size_t r; + + context = calloc(1, sizeof(*context)); + if (!context) + return NULL; + + context->ctx_c = ZSTD_createCCtx(); + context->ctx_d = ZSTD_createDCtx(); + if (!context->ctx_c || !context->ctx_d) + goto err; + + r = ZSTD_CCtx_setParameter(context->ctx_c, ZSTD_c_contentSizeFlag, 0); + if (ZSTD_isError(r)) + goto err; + + return context; +err: + ZSTD_freeCCtx(context->ctx_c); + ZSTD_freeDCtx(context->ctx_d); + free(context); + return NULL; +} +static void free_zstd_context(void *ctx) +{ + struct zstd_context *context = ctx; + + if (!ctx) + return; + + ZSTD_freeCCtx(context->ctx_c); + ZSTD_freeDCtx(context->ctx_d); + free(context); +} + int tracecmd_zstd_init(void) { struct tracecmd_compression_proto proto; - int ret = 0; - size_t r; memset(&proto, 0, sizeof(proto)); proto.name = __ZSTD_NAME; @@ -69,25 +114,8 @@ int tracecmd_zstd_init(void) proto.uncompress = zstd_decompress; proto.is_supported = zstd_is_supported; proto.compress_size = zstd_compress_bound; + proto.new_context = new_zstd_context; + proto.free_context = free_zstd_context; - ctx_c = ZSTD_createCCtx(); - ctx_d = ZSTD_createDCtx(); - if (!ctx_c || !ctx_d) - goto err; - - r = ZSTD_CCtx_setParameter(ctx_c, ZSTD_c_contentSizeFlag, 0); - if (ZSTD_isError(r)) - goto err; - - ret = tracecmd_compress_proto_register(&proto); - if (!ret) - return 0; -err: - ZSTD_freeCCtx(ctx_c); - ZSTD_freeDCtx(ctx_d); - ctx_c = NULL; - ctx_d = NULL; - if (ret < 0) - return ret; - return -1; + return tracecmd_compress_proto_register(&proto); }