From patchwork Wed Mar 2 04:51:27 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: 12765475 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 9F0E2C433EF for ; Wed, 2 Mar 2022 04:51:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233322AbiCBEwT (ORCPT ); Tue, 1 Mar 2022 23:52:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232215AbiCBEwT (ORCPT ); Tue, 1 Mar 2022 23:52:19 -0500 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82A5DB0EA2 for ; Tue, 1 Mar 2022 20:51:36 -0800 (PST) Received: by mail-ej1-x62a.google.com with SMTP id kt27so1295343ejb.0 for ; Tue, 01 Mar 2022 20:51:36 -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=Ccy6MU5C0RQqj0dWG7/lICfl72uE5FTBNsueAaL7lC35Tu4QZz/+t/NxloPoNKnuLW PCBcPsPpTL4TUdnWU9TmQFXCVnoFQOTOjtQfLCJ+R3xxMT80l2TZJcHwwADFx3fozhjP 1ArbrawteglpR46QZdcLOk957CtJ7/MQS2Le8upx13nuE+Sst654gDny6u3auNPd3J7j 3E7JyUaZaFdHEi4FngZa65EzET/t7fLS1jEYCD7L12KBgNXtwYtzSwWhqESBkPKCWyUd 5bduFxIp0TgUGCslgRQwKo56hq//MY1CmrGqPxdCJMwVOUM4ChycZEf3BujEaRZ1R9zp pfaA== 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=AyvWtlsEf3TbtScPDTIK1WZ7BgpYpQE6IwF5UyBZW1e+V+mzX2cpgPOV30obc5Piez h9hbyqN9ksfV0wdkGwae+zV0tv5Xr+db+IS5D/gg/VcOHkGxNaJ+BBadtuGJ3DI7n0xM qBYY/2Bq+AlxdHZ/ZlsYYQlJN5f5QkvSQD5JLjqxhDrLEGCe4j+BinaLSYprE3/N4eFU jrY5zo6FMg8LxzXyhMJFN4QhI3MC45fN3nzAKJF5z4LmbM3XDArA63iHBKj/qXUIdsAh qQtW75lPgvtXgHOA8pLIg56JOUmQMeHXmMzZaJK+ADG0DiPZPdpIp/WmbADKqzNo4Itp tUcg== X-Gm-Message-State: AOAM530C0nkhO+a17vltCn3gsu7GA5dAyErAU3V33UELgo2vrwINy5LS Rbnchxb5ZrIqtqa+Pf/NualDkyhibnM= X-Google-Smtp-Source: ABdhPJxGTWJ5m70K8Tyua0YuhmNwnTtno2x6OFa0zeHWCX2Dke1tdCOPs5hPvdHnobIsBAxFzTQQ+g== X-Received: by 2002:a17:907:6286:b0:6da:6e24:5e43 with SMTP id nd6-20020a170907628600b006da6e245e43mr36977ejc.449.1646196694979; Tue, 01 Mar 2022 20:51:34 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id ce7-20020a170906b24700b006cf095c2f5bsm5941036ejb.83.2022.03.01.20.51.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Mar 2022 20:51:34 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH 1/5] trace-cmd: Use a structure to describe a compression protocol Date: Wed, 2 Mar 2022 06:51:27 +0200 Message-Id: <20220302045131.387658-2-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220302045131.387658-1-tz.stoyanov@gmail.com> References: <20220302045131.387658-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) Acked-by: Sebastian Andrzej Siewior --- .../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 Wed Mar 2 04:51:28 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: 12765477 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 CC16FC433F5 for ; Wed, 2 Mar 2022 04:51:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234205AbiCBEwU (ORCPT ); Tue, 1 Mar 2022 23:52:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232215AbiCBEwT (ORCPT ); Tue, 1 Mar 2022 23:52:19 -0500 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BCFCB0EA1 for ; Tue, 1 Mar 2022 20:51:37 -0800 (PST) Received: by mail-ej1-x62e.google.com with SMTP id gb39so1252097ejc.1 for ; Tue, 01 Mar 2022 20:51:37 -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=mvLNlheELyfNJWEbH0QDTyLJPqNDrrn6JPnIuibccrva4Aulkya8TeHNdp5XYRWHel d2arXEaiF0ZqEv17TWGyFsQaAV/IgcRsq2rnEz3/hOoTLCKhtZbkutOxEX9mVd0gSkAq 1EL5O9VBIOcHqwWqsEhX6t1lcRTiXrzzcF/8xD7yBGCJeJW2/uQv8gqvpv4ecwdj4VGp IdAq9F0AJg/9a2pUHkOedL0zhW+ByuEZhRrjy1hTSQiP297aTKuK5/Kiy4LqsYkMKy/N G225o2WDD0TmAY3v2dsqL3LqzSXrWEX6peEMm1pbN75l/R1askHYUEjCcFD6oE/oypFq SjFw== 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=tu5HLuxDEXX/I+g/mS7Ekt5fJZsg7StQuc+zvdwfLW4FiYe7b0V4duJaDTdcquoP4W MoNJ1qfhk+bdOEkPVMWaW8B289pqPaF3GuGqBzQ19D5OkcAaSy3hGu1vC5bbkZ15ZHgk n+KTDvl4UY+UEJrSX4k4qn6+vkwb8mH3Z+D22qNnRo7Qc1MWysi16y/a15tkcT16Xm9o +8nrTFKTARPSSpQTv4KMvWn8jW2oQw/EW4boJZqCYfv4d+s5PBj/m1C1rcF/PxrGEPFk d1d2eSAKbcZi7j7tuiDMJBg5KHdqNXvYpfUq6EIskd8cUbj1AklEIIeuxU2+2mpfHecF 48pA== X-Gm-Message-State: AOAM5322mFZLvWhRmJIEMM1ZO7qa1DCOlHDbovk/FAmdUUA49KGpqlDt sT4FE+ztCjQU7YCvDgjFXyg= X-Google-Smtp-Source: ABdhPJxvKKZ1jBaghhFosMK7QMpDXja8DSAtp8fr0U2k7OJAZ3ix/mjnE2s3QkDvaChpwI78KJzHMg== X-Received: by 2002:a17:906:7287:b0:6d6:e4ff:b9fb with SMTP id b7-20020a170906728700b006d6e4ffb9fbmr4960292ejl.304.1646196695924; Tue, 01 Mar 2022 20:51:35 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id ce7-20020a170906b24700b006cf095c2f5bsm5941036ejb.83.2022.03.01.20.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Mar 2022 20:51:35 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH 2/5] trace-cmd: Make internal compression hooks more generic Date: Wed, 2 Mar 2022 06:51:28 +0200 Message-Id: <20220302045131.387658-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220302045131.387658-1-tz.stoyanov@gmail.com> References: <20220302045131.387658-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) Acked-by: Sebastian Andrzej Siewior --- .../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 Wed Mar 2 04:51:29 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: 12765476 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 524FDC4332F for ; Wed, 2 Mar 2022 04:51:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232215AbiCBEwV (ORCPT ); Tue, 1 Mar 2022 23:52:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233989AbiCBEwU (ORCPT ); Tue, 1 Mar 2022 23:52:20 -0500 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D5B0B0EA2 for ; Tue, 1 Mar 2022 20:51:38 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id qk11so1232851ejb.2 for ; Tue, 01 Mar 2022 20:51:38 -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=Dspl58zuLJZ2K9EAHhG8wMD1O9GENFwPkvT9SD+t70U=; b=qPBzgH4LTMZC217MTB3zygazsaYOK3IXj+V7gt6uD63eWUti7aQ06N7AJLfIX7YsDK hM42yvP4ORD6T3WR6McwhHN6nmnDDEBLmJuhaQSyUsYefDihqDIwBfxN1rbzKJXrjcYu ozstCXi54GuRL+vesSybSiSoOv2uAiSUEokDnf0YHQh1h+KiUu177mhXfZeThZM/M6lf rQBxV9SF/1+BOYRTxtQPpZg/lasu91W+HU7KgDPQMUAoyjo2riXodygf4dj9kZYCMdpm BxaZWDpDJSP3F3zMTffHl8rR4Q6pga22W4qIzurihK4GgFY6RiVuXLCuqQzXczSTo/bd tiXw== 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=Dspl58zuLJZ2K9EAHhG8wMD1O9GENFwPkvT9SD+t70U=; b=fq6HuAPcJhuYraD8a/MZ4lUXuszf9DEMQxMi05TkdlFZQAev2s3BZyUNIQsfZCU8MV vZ+KE88/B/acSofMrLIjcRON/2sbo3tQv62f+TY7l0mHEy3lei6YM3EZRNNR9kZt+UH2 SJkKz/R97leicL/lqycvommqx5+oW+ynNGd/6NcbR10ejnQ0Zh5apWKZtYEIIqZgwUWR GDbpZxxiEqhCgqIlmFm3n/DdyNhVQLFph32fd+Sg6Gss/DErbdGvoMAlzwUEkCE/Rkhf GuKtIvWwcCJDXqRAQ5LEPu9wsdx/y2YEXVTpLEbt8CHlXYG3hBlfq+Yy8F82Sq3f68LE SdjA== X-Gm-Message-State: AOAM530pbAHL99+LwRok4pVGX5yTw2EH43sNc1BUwpjg9RW3Uk8C/dlb Awg0UDrNsfa7tZQmem3woq4= X-Google-Smtp-Source: ABdhPJxAJklHJ27LXYsVp0HQoApGXB4y2yDsx19g/HIsFO8A7LcmtYorrsGqWZodjL2WHy7Ua5j8/w== X-Received: by 2002:a17:906:c405:b0:6ce:7100:8cf1 with SMTP id u5-20020a170906c40500b006ce71008cf1mr22759213ejz.722.1646196696806; Tue, 01 Mar 2022 20:51:36 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id ce7-20020a170906b24700b006cf095c2f5bsm5941036ejb.83.2022.03.01.20.51.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Mar 2022 20:51:36 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH 3/5] trace-cmd: Use errno from zlib, if available Date: Wed, 2 Mar 2022 06:51:29 +0200 Message-Id: <20220302045131.387658-4-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220302045131.387658-1-tz.stoyanov@gmail.com> References: <20220302045131.387658-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 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/trace-cmd/trace-compress-zlib.c b/lib/trace-cmd/trace-compress-zlib.c index 41342597..0dfd4f15 100644 --- a/lib/trace-cmd/trace-compress-zlib.c +++ b/lib/trace-cmd/trace-compress-zlib.c @@ -33,6 +33,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; @@ -61,6 +63,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 Wed Mar 2 04:51:30 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: 12765478 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 00A2BC433FE for ; Wed, 2 Mar 2022 04:51:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235122AbiCBEwW (ORCPT ); Tue, 1 Mar 2022 23:52:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233989AbiCBEwW (ORCPT ); Tue, 1 Mar 2022 23:52:22 -0500 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B5D9B0EA1 for ; Tue, 1 Mar 2022 20:51:39 -0800 (PST) Received: by mail-ej1-x62a.google.com with SMTP id dr20so1204940ejc.6 for ; Tue, 01 Mar 2022 20:51:39 -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=i2StLPfrNnVqBIJte/hCLed9gvRr/hCO6p+GYBj3TMk=; b=CWMrGbQxl/mGfZzjxVkgSquGBcvfeb7ENNIasTGotg9d22qheS5aORfubhn35+A/3e Ms6S94bKc/suThWSGmFyU7CuYjxlbggoz6zOeMlLLlA4D/xHik4Bv+5QwOsH1R/9gT5z Ai55swWG8ENmTkdv5leOv4I5i//N4RYzGqOaBkgJyC/ZgNQFrUVfAzRlzQFLDMoXfNuz QWcvI2hXTl+PPfx/9lZjAz7quMfZ/j9ZStNuTivkSfegiw7NSDAFOtqDgOgO5kK3+E34 kpNF469FsY9Lcxkkvb4Iu+VfDXMFk7NzAHEohrXhyJTU2v0DqgcO1nfBh4W6udHSZuLv 6OMg== 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=i2StLPfrNnVqBIJte/hCLed9gvRr/hCO6p+GYBj3TMk=; b=XKw5rcXsWrmBADowr6/ApaX5ZfmkzK24YzdYxyXruNHLXmGM6ts/AYRbeeI9G6PDhD Wqhv+WkhPU2OaLncyLrt0xmY//usUxaE+0Fhvl6nk3TrQld78EKO4NtmoQMeKbPbqV+a meMvve+VaG4uGTy7wJZDvVtIOedZI21k7Mmi9GBq8uB6BN8kT4pNxbx7aui9G9RZg/q6 bioGPAzH/RElcKrGe8BQBPOgeDQwAXb+bMqSJLgkYo6QPf4CIRLObi2XLzPxLIaaHBT1 FMCDuYZak6+En57nuVBGZKR8/gOUBJaabteO4MuxHiLJod9tEEFNig49NCeS3n1AkbMp 6ooA== X-Gm-Message-State: AOAM530y/mAcKkWMosX+fbS1eBnRPFkaRR8JVc9YPHMXYlIsiOvrauiR z2RD/hqOcQJ39AkBjD4tXGw= X-Google-Smtp-Source: ABdhPJwrq00n42McEh/jHkQaUDzCXImBn8IKAixXMW23B1cYF+r5LoIiBjhPR5RJ2o7MyHLzG7Trzw== X-Received: by 2002:a17:906:d94:b0:6d6:e113:2768 with SMTP id m20-20020a1709060d9400b006d6e1132768mr6252390eji.193.1646196697935; Tue, 01 Mar 2022 20:51:37 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id ce7-20020a170906b24700b006cf095c2f5bsm5941036ejb.83.2022.03.01.20.51.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Mar 2022 20:51:37 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH 4/5] trace-cmd: Add context to compression hooks Date: Wed, 2 Mar 2022 06:51:30 +0200 Message-Id: <20220302045131.387658-5-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220302045131.387658-1-tz.stoyanov@gmail.com> References: <20220302045131.387658-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) Acked-by: Sebastian Andrzej Siewior --- .../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 | 33 +++++++++++++------ 4 files changed, 34 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 0dfd4f15..819a739d 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; @@ -43,7 +43,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; @@ -73,7 +73,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..9371d3cc 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; } @@ -453,6 +460,8 @@ struct tracecmd_compression *tracecmd_compress_alloc(const char *name, const cha void tracecmd_compress_destroy(struct tracecmd_compression *handle) { tracecmd_compress_reset(handle); + if (handle->proto->free_context) + handle->proto->free_context(handle->context); free(handle); } @@ -546,6 +555,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 +683,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 +717,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 +875,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 +961,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 Wed Mar 2 04:51:31 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: 12765479 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 7F1F0C433EF for ; Wed, 2 Mar 2022 04:51:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233989AbiCBEwX (ORCPT ); Tue, 1 Mar 2022 23:52:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234768AbiCBEwW (ORCPT ); Tue, 1 Mar 2022 23:52:22 -0500 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 490D5B0EA2 for ; Tue, 1 Mar 2022 20:51:40 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id qx21so1146594ejb.13 for ; Tue, 01 Mar 2022 20:51:40 -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=GFllhzEiVfekNSt4psgymBJ9B8phQhEDWBQXaTHwtA3UHGqSkSNfcJT87p0YvFwb6G +qzv1tXt776cAKU5QXBlqC2o2/VPYWYV2bYIOo4DDff7MRbf79AKlvl2h6q2Nn4ybLqg pNQsOSZ35Tzf0+O2YC37P9Q2s7QQnfQi13bcZ9AKWdr/Ayu8omzLHTD8PuWMyI5rkwEq gNwn0immFLiflVe4ls/e0wnXAI4u7ZjqT4I7guOGmzcEyNjHys8sXtSIk+lR013voTOP xzbXy/3Op/uY5k04RKx+auLAAa1oLz+uVlNCpP+6lHPw5pqTzLj6+fdnT43YjkIMQKnC 2jSA== 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=TL+b558QMpEg9Qu7LdHbhazpbytNO6FBrLF4KZQMu+/SFFE2nMzZAH5uOpLtFRvFp8 k9cFwr44KEuborDzs+MXoI9atumaGuwcmhYEyEoL39fDn034Syqn/AG98MlPKsi5f/bb 6zWjJD2l8f22Ii+cz+qLdVGQe5xVlyrwnUCnK1tJLJKQfm7k+LbLTcav3LplDXXvT1Fu 06WaN0OLiL+P5apBUNFQRztZsje6oTvWtWFw/N1P9Nz3aHQhKv91VXogZMtVGCT3djVV Z/mcFmnIBwvfUxxVuO3IpNzGSG6Ug93lFGi9XZJ2DUsmoIEaOuaLYC47JTGu38i1fuBN hOXw== X-Gm-Message-State: AOAM5337PcRTqwPPizbG9PId33Hba9C+ujxEu1yJuQsQqL8jXN1PMLtE Fbs4RY82B1jRIsxY7iRJMEI= X-Google-Smtp-Source: ABdhPJzUT0syvsur3tFPqYAyp2VOh5t9K5G8uK+PQRx3MglZGL7jSTUS38E3pu+sZghGMuf8X+vNXA== X-Received: by 2002:a17:906:30d1:b0:6cf:c116:c9d3 with SMTP id b17-20020a17090630d100b006cfc116c9d3mr20369344ejb.245.1646196698855; Tue, 01 Mar 2022 20:51:38 -0800 (PST) Received: from oberon.com ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id ce7-20020a170906b24700b006cf095c2f5bsm5941036ejb.83.2022.03.01.20.51.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Mar 2022 20:51:38 -0800 (PST) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: sebastian@breakpoint.cc, linux-trace-devel@vger.kernel.org Subject: [PATCH 5/5] trace-cmd: Use context hooks in zstd Date: Wed, 2 Mar 2022 06:51:31 +0200 Message-Id: <20220302045131.387658-6-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220302045131.387658-1-tz.stoyanov@gmail.com> References: <20220302045131.387658-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) Acked-by: Sebastian Andrzej Siewior --- 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); }