Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp834723ybk; Fri, 15 May 2020 15:07:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw7TQuSEdVo0tTv8ysKTVYc0eaqjjHyj8IvS/ulmdrVEZoPktF0DC9gX4sTNUy6qhf2Hj2K X-Received: by 2002:a17:906:970e:: with SMTP id k14mr4963534ejx.202.1589580462355; Fri, 15 May 2020 15:07:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589580462; cv=none; d=google.com; s=arc-20160816; b=Yjhc0lfTxfa7PGQQ4XMWigmr1+w3EtrM8MS5jmb/jJJZd88WHfVjQYnk8XTifp3BBu UpuMNzktHaIk7mu06wpTJN0Ck+nZVUE5pPwfH+5JmFvROQ9QczCCD1VgmD03tcBJYDW/ lp0n2fta2n2VGG6lUhnMkIQtq9q+mOT2R0O7Rlc8AHCTZ0nUVtQ+gzCtTi4Tc39om062 mh0a7XJTsq5yZEUrCrCswa0Sqr7i/qLZv5QPQwpzDbN2EULahi7xXTHwHS6h39SgRyT2 /p9Il7+7LGtW6kvguBmLgCXbNaEPA3/bm5cYw72ebGCYS06p++dZRmpMexhvyMNgGQjC BYSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=hAuns5l+V0vwIv3z0kLgsyRcBseJuPIzou+9SjbnNYk=; b=Y89SR1c9HJBB//NhvNSZPBMy5KyxJjmOPZCPXDMm+GzIXJsdRL8kpXbtIjPQJ0JSrT 0fnDsVHleI4CG3opZ9in7BeEkQXFS7gnIN1XGOXwG62FzobGjHbrl8dQQuqUv5Og8NtY lTeIeD03O+t+YO5+KlQsjg8HK6RwXvV1baEzKK925ZI6cc8wk9mPPeCiIYn/OOYDEcjT Daw80er62kUB5YkqTHyZgwgddBLDWQ4p6wGrUM0V4QFJ+tfjHFZN3UvOIy3bX7re3awl i1LG2ZmZjAuc3RWH+6Ah7l0AzIZqUDoD9K5hR9Nw6bg3SvH6AIQRfL3LbbjJUPQDI1JA 6M2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Q41c+n8y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y31si2218274ede.507.2020.05.15.15.07.17; Fri, 15 May 2020 15:07:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Q41c+n8y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727825AbgEOWD3 (ORCPT + 99 others); Fri, 15 May 2020 18:03:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726183AbgEOWD2 (ORCPT ); Fri, 15 May 2020 18:03:28 -0400 Received: from mail-yb1-xb43.google.com (mail-yb1-xb43.google.com [IPv6:2607:f8b0:4864:20::b43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78D22C061A0C for ; Fri, 15 May 2020 15:03:28 -0700 (PDT) Received: by mail-yb1-xb43.google.com with SMTP id i16so1876552ybq.9 for ; Fri, 15 May 2020 15:03:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=hAuns5l+V0vwIv3z0kLgsyRcBseJuPIzou+9SjbnNYk=; b=Q41c+n8yK3nkE3KDJdYhiZiAf04uT8mHpZRDb8LE9n2aTAq1FORypaxmgE+gDKOUQw XgmE1rrLddyYi6BvRhTpUZGcbSG9WZ4laYZ/IsOQ5McL2z5nhvUo8LoOPaVLULfkSJxV fHfq0IjeQjp9CpDRbkwM790U4XVnPDGPrI4p21eO5Ib59s5h8FpNgx4YwRUOJ4N2qnUk vTTYOJLfNG5qTjOyAP+P5zN0ehyw6s3ofnQpoyTPIHB8LfKqimn3eokLjj4qQ/sdZ4Ib /uU60ACLgOtg2gvO4J5Gke8xnpFW5a9XAfQK2Q65DgzFRHtelZU1+bGJjM9/dUnDoOws Ldvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=hAuns5l+V0vwIv3z0kLgsyRcBseJuPIzou+9SjbnNYk=; b=WGXUbEU9R/WuFHYXAmas37LVPc34lnNPqsD1zMp3u4/YOJ1BaBwvX+UVo1dhdZAoLc w6q+0VDJ7IlmX0MTN5NvezqtsgwsltG/474tCA4k+4FyQgfPA2C7/6/OoUlvhZ41rzSi ib4IqWUoKvcW7UBkRrIYD8UpMdjuINT5rJIVznpebL+hXQ3h1s+EyqjFeh3RBxodyFzA dK1i9P1bYPetY8VW2JvACvISfx9DAgB0Vp0aWWgkRv+D5tKdfAkwUOCHWbZKGkgDMibF Xii2RTctLGjtzE2C1Rp+4tg+OwnJ0mzqDpW9MkorMpDUglfYLF4tnavtiKBeg8YMHZoP TH1w== X-Gm-Message-State: AOAM531jrsPBYhKgaoWGRLiavKvKENl+c95ETNRtEhINfRRhxtzX9o9Y CJ3Yeoear4KguzDjZnAfveA9gVriKUa10ZGaFN5Iig== X-Received: by 2002:a25:5387:: with SMTP id h129mr8404945ybb.47.1589580207492; Fri, 15 May 2020 15:03:27 -0700 (PDT) MIME-Version: 1.0 References: <20200515165007.217120-1-irogers@google.com> <20200515165007.217120-8-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Fri, 15 May 2020 15:03:16 -0700 Message-ID: Subject: Re: [PATCH v2 7/7] perf expr: Migrate expr ids table to a hashmap To: Andrii Nakryiko Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Kajol Jain , Andi Kleen , John Garry , Jin Yao , Kan Liang , Cong Wang , Kim Phillips , Adrian Hunter , Leo Yan , open list , Networking , bpf , Stephane Eranian Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 15, 2020 at 12:39 PM Andrii Nakryiko wrote: > > On Fri, May 15, 2020 at 9:51 AM Ian Rogers wrote: > > > > Use a hashmap between a char* string and a double* value. While bpf's > > hashmap entries are size_t in size, we can't guarantee sizeof(size_t) >= > > sizeof(double). Avoid a memory allocation when gathering ids by making 0.0 > > a special value encoded as NULL. > > > > Original map suggestion by Andi Kleen: > > https://lore.kernel.org/lkml/20200224210308.GQ160988@tassilo.jf.intel.com/ > > and seconded by Jiri Olsa: > > https://lore.kernel.org/lkml/20200423112915.GH1136647@krava/ > > > > Signed-off-by: Ian Rogers > > --- > > tools/perf/tests/expr.c | 40 ++++++----- > > tools/perf/tests/pmu-events.c | 25 +++---- > > tools/perf/util/expr.c | 129 +++++++++++++++++++--------------- > > tools/perf/util/expr.h | 26 +++---- > > tools/perf/util/expr.y | 22 +----- > > tools/perf/util/metricgroup.c | 87 +++++++++++------------ > > tools/perf/util/stat-shadow.c | 49 ++++++++----- > > 7 files changed, 197 insertions(+), 181 deletions(-) > > > > diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c > > index 3f742612776a..5e606fd5a2c6 100644 > > --- a/tools/perf/tests/expr.c > > +++ b/tools/perf/tests/expr.c > > @@ -19,11 +19,9 @@ static int test(struct expr_parse_ctx *ctx, const char *e, double val2) > > int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) > > { > > const char *p; > > - const char **other; > > - double val; > > - int i, ret; > > + double val, *val_ptr; > > + int ret; > > struct expr_parse_ctx ctx; > > - int num_other; > > > > expr__ctx_init(&ctx); > > expr__add_id(&ctx, "FOO", 1); > > @@ -52,25 +50,29 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) > > ret = expr__parse(&val, &ctx, p, 1); > > TEST_ASSERT_VAL("missing operand", ret == -1); > > > > + hashmap__clear(&ctx.ids); > > hashmap__clear() will free up memory allocated for hashmap itself and > hash entries, but not keys/values. Unless it's happening somewhere > else, you'll need to do something similar to expr__ctx_clear() below? In this case the const char* keys come from the literals added on lines 27 and 28 and so didn't need free-ing - which is what expr__ctx_clear() does. I've made these literals strdups and switched to expr__ctx_clear() as you suggest, as this is more reflective of the real use. > Same below for another "lone" hashmap_clear() call. This was a memory leak, thanks! Ian > > TEST_ASSERT_VAL("find other", > > - expr__find_other("FOO + BAR + BAZ + BOZO", "FOO", &other, &num_other, 1) == 0); > > [...]