Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp106927rdg; Wed, 11 Oct 2023 23:26:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFBPwyimEK0vQDJBujWNYQ5ODlgUoo/c3tTCAhXBfyOQaNKfGBEMM1sMfVENqyv7zqPfo9+ X-Received: by 2002:a17:90b:4a0c:b0:274:b4ce:7049 with SMTP id kk12-20020a17090b4a0c00b00274b4ce7049mr20399442pjb.34.1697091965725; Wed, 11 Oct 2023 23:26:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697091965; cv=none; d=google.com; s=arc-20160816; b=bzHpv3K+mxYfqnnXU9Rp3U4G5NOzVj8vIMCNdBSZra2neRq2D0zW8H6WdxJNi64Rxy VSijGwzFUTVIW6vh/VbDGCz/AzfBS1GBcNCEfsRUY9KDJQQdKPt/0Wabm4z0M8A3n2Um sMHHdOqiH07x0oRpEOqe7hKXDS+Ur5LYvuB6PCt2qUYv1AtVy8Oq91pmrBqFFJQgbK2y OM1rvvH8e4s0Joc6LnH5lWBQp1d64TGCG95gdnANyx0yT90iauByhttxSKXruMMhFTsH pQ/VJ21uUrse0Ww5x7UJv+xfBuyi8qJS64Fu0XWYmz4uK8HMtd4wWk6+duWiXDyRQ8M5 l5Gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=7ROvqMh8j+zKvWbIwAMh3UVBHyePHtFkWdSWTbYfkrk=; fh=d2c3GDvCFnEMjeV5/UdzkMgvIdbP06sIBDmJP5CjgvI=; b=ouf4ryt941mzo/QKpojr1FvkR9iG1oqI4DNW+I4ugjQPNRucmsOX7DmgA9t3rjF4kS seK/soYebFoPW8YUKV5ZPXpn0NUuTam2/gnufFtusZxyBYse4xtubmSkUQetFSAT328l k4xlGnhCtT6fmdl8baYcao2wXpWnQlpWHQKhfP64Czd/dv+HlkOwYob8nE75fKdnPuhe dIw91F6NRjsN+2dV/OGrqKCgIj5mbmeyffSZm9GfrDibu19eU6UP1V24RmTbRf9LB+C4 1POQSWl5tm7Z+nMjtAGyUM5h93xo58E18aINkNc2AzkzJLQAkRishRAhWFxUBQt0omyF uTig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qEsMd4mP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id oj11-20020a17090b4d8b00b0026822221502si1698587pjb.163.2023.10.11.23.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 23:26:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qEsMd4mP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 1CDA681140E3; Wed, 11 Oct 2023 23:26:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377520AbjJLGZO (ORCPT + 99 others); Thu, 12 Oct 2023 02:25:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377695AbjJLGYv (ORCPT ); Thu, 12 Oct 2023 02:24:51 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3F20D48 for ; Wed, 11 Oct 2023 23:24:35 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9a60a104b6so1502752276.1 for ; Wed, 11 Oct 2023 23:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697091875; x=1697696675; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=7ROvqMh8j+zKvWbIwAMh3UVBHyePHtFkWdSWTbYfkrk=; b=qEsMd4mP5hXoihm5RuLvG3X4V8L8ImJ4jmvEBU/Q6Q/kwQptfvKbz3xp+sL9IDDaPF /18gUKoKZdbPON7uuYB4VY5mMRHmRONyl9wJ8QHT9mlQh/0DUYuYKiw1uGhtsBcZjYXV 5DGuNPbT8fA++tS+XbFWvhMULlpfS8jJLdn63L0gupfKTlaU7AOx/CERFD/DkvqBKup/ TzcIhWzmxg6B2hYOBT1JsGjbg1O04i5XQ0p8PUV9/iQxjqewgd6USXxohPZVyV7wYvjo 6BhjUUsj6uvsaB1EiCjxkUD5Sk340LLePRbFzHHl6Je3qFJRHOIWjrgJaeayjltmWQAE NL2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697091875; x=1697696675; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7ROvqMh8j+zKvWbIwAMh3UVBHyePHtFkWdSWTbYfkrk=; b=HaqjKU5Lr/tO37S6nJ2m7QN7cUCRM56msWYv+FiNe88DiOY39mc+OYkzNq6YKIX1O5 N86rqbXi+R006yWHY14ptb/MARQu86grnGPRD9bVNZvEi0t7prlwNxioXBObiwgND+H9 59J5yKPYWwEF7AYYj8CqEbS/WRx1E0/hkNSWOFnOag0FSHuCuQIVRv88O/E3DynTiY+o dR6GRsEadlwAhAWlaFIVSvwNKWSNIrA8gvyajnhsE780wc95gehlazq4aC25jOW0+zbt oGa+0HnuiBhIIh+H8kRym3gVrjhodGaqlzsSzWceu97ihGWKCaXDBGypjoFaXhlYhNQ1 IVAA== X-Gm-Message-State: AOJu0Yz2AJWDXxMBcvkQrPQxjehP/XJp18moFnS/v8+HO+6I3rpvxvu7 V300DBPOzw9rheI0vpoqdtHpMUvH3fda X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7be5:14d2:880b:c5c9]) (user=irogers job=sendgmr) by 2002:a25:1f45:0:b0:d8a:fcdb:d670 with SMTP id f66-20020a251f45000000b00d8afcdbd670mr489117ybf.1.1697091875173; Wed, 11 Oct 2023 23:24:35 -0700 (PDT) Date: Wed, 11 Oct 2023 23:23:58 -0700 In-Reply-To: <20231012062359.1616786-1-irogers@google.com> Message-Id: <20231012062359.1616786-13-irogers@google.com> Mime-Version: 1.0 References: <20231012062359.1616786-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Subject: [PATCH v2 12/13] perf mmap: Lazily initialize zstd streams From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Song Liu , Sandipan Das , Anshuman Khandual , James Clark , Liam Howlett , Miguel Ojeda , Leo Yan , German Gomez , Ravi Bangoria , Artem Savkov , Athira Rajeev , Andi Kleen , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 11 Oct 2023 23:26:02 -0700 (PDT) Zstd streams create dictionaries that can require significant RAM, especially when there is one per-CPU. Tools like perf record won't use the streams without the -z option, and so the creation of the streams is pure overhead. Switch to creating the streams on first use. Signed-off-by: Ian Rogers --- tools/perf/util/compress.h | 1 + tools/perf/util/mmap.c | 5 ++-- tools/perf/util/mmap.h | 1 - tools/perf/util/zstd.c | 61 ++++++++++++++++++++------------------ 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/tools/perf/util/compress.h b/tools/perf/util/compress.h index 0cd3369af2a4..9391850f1a7e 100644 --- a/tools/perf/util/compress.h +++ b/tools/perf/util/compress.h @@ -21,6 +21,7 @@ struct zstd_data { #ifdef HAVE_ZSTD_SUPPORT ZSTD_CStream *cstream; ZSTD_DStream *dstream; + int comp_level; #endif }; diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c index 49093b21ee2d..122ee198a86e 100644 --- a/tools/perf/util/mmap.c +++ b/tools/perf/util/mmap.c @@ -295,15 +295,14 @@ int mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, struct perf_cpu map->core.flush = mp->flush; - map->comp_level = mp->comp_level; #ifndef PYTHON_PERF - if (zstd_init(&map->zstd_data, map->comp_level)) { + if (zstd_init(&map->zstd_data, mp->comp_level)) { pr_debug2("failed to init mmap compressor, error %d\n", errno); return -1; } #endif - if (map->comp_level && !perf_mmap__aio_enabled(map)) { + if (mp->comp_level && !perf_mmap__aio_enabled(map)) { map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); if (map->data == MAP_FAILED) { diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h index f944c3cd5efa..0df6e1621c7e 100644 --- a/tools/perf/util/mmap.h +++ b/tools/perf/util/mmap.h @@ -39,7 +39,6 @@ struct mmap { #endif struct mmap_cpu_mask affinity_mask; void *data; - int comp_level; struct perf_data_file *file; struct zstd_data zstd_data; }; diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c index 48dd2b018c47..60f2d749b1c0 100644 --- a/tools/perf/util/zstd.c +++ b/tools/perf/util/zstd.c @@ -7,35 +7,9 @@ int zstd_init(struct zstd_data *data, int level) { - size_t ret; - - data->dstream = ZSTD_createDStream(); - if (data->dstream == NULL) { - pr_err("Couldn't create decompression stream.\n"); - return -1; - } - - ret = ZSTD_initDStream(data->dstream); - if (ZSTD_isError(ret)) { - pr_err("Failed to initialize decompression stream: %s\n", ZSTD_getErrorName(ret)); - return -1; - } - - if (!level) - return 0; - - data->cstream = ZSTD_createCStream(); - if (data->cstream == NULL) { - pr_err("Couldn't create compression stream.\n"); - return -1; - } - - ret = ZSTD_initCStream(data->cstream, level); - if (ZSTD_isError(ret)) { - pr_err("Failed to initialize compression stream: %s\n", ZSTD_getErrorName(ret)); - return -1; - } - + data->comp_level = level; + data->dstream = NULL; + data->cstream = NULL; return 0; } @@ -63,6 +37,21 @@ size_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_t ZSTD_outBuffer output; void *record; + if (!data->cstream) { + data->cstream = ZSTD_createCStream(); + if (data->cstream == NULL) { + pr_err("Couldn't create compression stream.\n"); + return -1; + } + + ret = ZSTD_initCStream(data->cstream, data->comp_level); + if (ZSTD_isError(ret)) { + pr_err("Failed to initialize compression stream: %s\n", + ZSTD_getErrorName(ret)); + return -1; + } + } + while (input.pos < input.size) { record = dst; size = process_header(record, 0); @@ -96,6 +85,20 @@ size_t zstd_decompress_stream(struct zstd_data *data, void *src, size_t src_size ZSTD_inBuffer input = { src, src_size, 0 }; ZSTD_outBuffer output = { dst, dst_size, 0 }; + if (!data->dstream) { + data->dstream = ZSTD_createDStream(); + if (data->dstream == NULL) { + pr_err("Couldn't create decompression stream.\n"); + return -1; + } + + ret = ZSTD_initDStream(data->dstream); + if (ZSTD_isError(ret)) { + pr_err("Failed to initialize decompression stream: %s\n", + ZSTD_getErrorName(ret)); + return -1; + } + } while (input.pos < input.size) { ret = ZSTD_decompressStream(data->dstream, &output, &input); if (ZSTD_isError(ret)) { -- 2.42.0.609.gbb76f46606-goog