Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2678593rdb; Mon, 12 Feb 2024 12:37:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXHrtMe1rv2hn6AIxhVdivO70FNVmZZ1mwu6fXjLb2E8V8lCDNMhMuGy3YZTIiC6nyKuBiqU+cUyUd4ldFLlnasaxIkkgjWGWSJyME2dA== X-Google-Smtp-Source: AGHT+IF/YayR2x3aXAP2oJ65cExA4Gy62+MiNCX6KZCSva3jXx0q/FURHLavpNhq0kGBGaEfK+3E X-Received: by 2002:a17:90a:df90:b0:28f:ee83:13cd with SMTP id p16-20020a17090adf9000b0028fee8313cdmr788912pjv.0.1707770262254; Mon, 12 Feb 2024 12:37:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707770262; cv=pass; d=google.com; s=arc-20160816; b=uQYmebZnjOtccMAMvlxgKJ28VMbT6dlwRbTjJ2alk1V8RCUcd7I+5Z5DHca2e5N7GU fU1VDznfOm72f6AMO8LG6m8zcd73STwuYp81x4vTrVe646LUekx3hZlAS7Av8aN8EuOB 5txEgKPi9KgUEHH7/dVyj1XvxwA/7l9N7UOEmZUODE7qTAmOyXSOPmFgZ1o9pqYLFaxu FSXcEgol0DWBq8RAAV4JDzPavwaW7IqL7tNzXynCsYRIgJyyk+XPEDufAQydXkVF5L4Y iPPIaFh9v+CHr+b45R47YtW2uFKnJMxHkwxEVmAz5sXoKR+w4HVDAdqrR1396QYbh3US m9yw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=wxAo8wnRkMugY5yAL0/bzNXwLaeP7jnxmRa56P+AWYU=; fh=uZtzIaszZxi4I9XLlcDhWUulwU/ncdfyJhUofAk7/aw=; b=pFQhDsOENkSbXySXzga9Q3LeMrnG8/U6g/8CCNDPI9IwAkVRj5zC4CK6mtpr7U0XmX R0Ch1OBYJ5NEl7T34CVWOLUgcJXwC8iI6P6Q8Ul0RyKsAU78IjG+xxwqxen/E9BoPK5D Z8baO9wB7uadmOIRdJP0Jmg/uCVpP19FNKXpSDr+1WXy+0RzC27NdliukltWxUmTkuyp TJ+0splHGCB8DMkGpFqMFojk8vp2DBYKp9hgdrDZ8d5To8lh8iSYmz9TdPWi4s+t0hra qCXymysnPZAjyPf+bo4HoGuLHmYG4x3YZL/ManaRvUynzzYkys8nU6cNNk+kJb+lfCrN 8wxA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="GDI/nHXU"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-62343-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62343-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUEiva0li0Y0DiN2NUu0JQb6/CACmoivwO6eyhLfGFQz6IGAw1VxPcQd3tyvSqLp2aSJYUvQQpro9knKEUsvt5ssWo/O2/q1y8P8INyEQ== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id y187-20020a6364c4000000b005cdf488ba0asi720887pgb.746.2024.02.12.12.37.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 12:37:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62343-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="GDI/nHXU"; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-62343-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62343-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id DE2F22831BC for ; Mon, 12 Feb 2024 20:37:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 662DE42AAF; Mon, 12 Feb 2024 20:37:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GDI/nHXU" Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A24B01EB20 for ; Mon, 12 Feb 2024 20:37:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707770253; cv=none; b=oT9XWL0lPfJOo3S6XD1QdYYlj1xQ2iwT/oZ8Ysk2XPyXmw9KMu9pcsfGOVTHIovlasNYF3UFjp//0oQvS6VAv6Vnfxo/T4lyCsVwr+4MGqTLjthLmk9OKu8C04kW9es3GpKEoui4y5CXJXv5Gb+eT/i+JNro6kpq/5amyAaqGfA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707770253; c=relaxed/simple; bh=0Xsn62CHAwZfzPgXua85w1HrUgM+rk5dPEmDrbkfjcE=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=Vme8o64VBxGPgm4SDBZVko8hfoOS9qMRJOTUSf2elwTGvhNCTVUhSvY5TX+CQi/ucqWnawze2f79BKLMNa9uoAe4lCkvUKkPeaQpTeBR3Un33nH0Dpp4pRHQarCK7oyJzEDrxeaqa1kA7GNANH0TtFhaesQRLQz3YwqFdPoHjHs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GDI/nHXU; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1d93b982761so1825ad.0 for ; Mon, 12 Feb 2024 12:37:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707770250; x=1708375050; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=wxAo8wnRkMugY5yAL0/bzNXwLaeP7jnxmRa56P+AWYU=; b=GDI/nHXUyCkPU+WL3CQJs4lztrv31b7i5c7vMkhGEGedJ3apYBd8nUjuXj9wNCwsIe tkZS0Ie7ktLy8Nj5gbJID9OQoWUjlKJSuc/B5vY8RmQzVGTxDVBgvUWXJV2UkTtJqIZX cA2uFFDmtWPnbLdnlXd+VbUgOAkTOQe8oyogBpj4XxkiWWSiH7EI9ChGjhRna7NC0hbE JyJ7hSYqLN8/HxiKtasjF1EkmtsVaGlOd6M2SCGoOr4ho6eeu9xlWuuixJNGMMjECh4w P90hS1GbVQkcMX3iX7BEznXW5WQ8NOYo/QLxa/WOYzngcs/bCQgHFQyPJkYGlraMioB5 vv0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707770250; x=1708375050; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wxAo8wnRkMugY5yAL0/bzNXwLaeP7jnxmRa56P+AWYU=; b=Ki0NSLgRYn8YGSUll/Kj4iEu83XwN1AucpaJo78nu+0KNfWTwLKTDDbAAGfT/ypKIw s3Rp3tsT+L1Uq5xPTgn3UajmKCyaS2ct8dwpP+s/Tta5hMXEB8RM7qD+xNhJNKZGxfXY rUpHF8q7HVd32IK+111Pwiioowax0cq4u5/0da7DcO0d1Ue92RdCgX5MdkiSDzuJ0kRE vId0KQhXTYvhyuUVV/pubvijDHGW8JpBJ5DjXy2tsFXdU2BI06wVgA4NOk7csjmHBjJm A0k0jEp6dweguY6u2rMOQHAeX+/jidYrnWgcuicVYHhwDbmszKw/GDZ0MvC6bQPjrN2j 6W9Q== X-Forwarded-Encrypted: i=1; AJvYcCWa1qO0KLmJVEFi9Hyq8dPpdt1HmOWJeBt0Om+p5uP4nrc5dogqcV8BmRHTytOpiz9qBKzKnRCI4+CDJD9r4xnkgVXIQxeWaIkhcQgL X-Gm-Message-State: AOJu0YxDEC6irI++duOBYOdm86YOON1NGAwEoWlzTSayM5iE+YG755vl LSPVB0mmTta5idnXK45J0a9bqfR3iV5OuNhWoA6xj5BcRi1QXycCBd57iobSipkLboUTJizAtF/ QY6Gdo4zObplsXE1WE0269j0fVislm6L8G58g X-Received: by 2002:a17:902:9a94:b0:1d9:3524:3db2 with SMTP id w20-20020a1709029a9400b001d935243db2mr340700plp.11.1707770249632; Mon, 12 Feb 2024 12:37:29 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240210031746.4057262-1-irogers@google.com> <20240210031746.4057262-2-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Mon, 12 Feb 2024 12:37:15 -0800 Message-ID: Subject: Re: [PATCH v3 1/6] perf maps: Switch from rbtree to lazily sorted array for addresses To: Namhyung Kim Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Song Liu , Colin Ian King , Liam Howlett , K Prateek Nayak , Artem Savkov , Changbin Du , Masami Hiramatsu , Athira Rajeev , Alexey Dobriyan , James Clark , Vincent Whitchurch , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, leo.yan@linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Feb 12, 2024 at 12:26=E2=80=AFPM Namhyung Kim = wrote: > > On Mon, Feb 12, 2024 at 12:19=E2=80=AFPM Ian Rogers = wrote: > > > > On Mon, Feb 12, 2024 at 12:15=E2=80=AFPM Namhyung Kim wrote: > > > > > > On Fri, Feb 9, 2024 at 7:18=E2=80=AFPM Ian Rogers wrote: > > > > > > > > Maps is a collection of maps primarily sorted by the starting addre= ss > > > > of the map. Prior to this change the maps were held in an rbtree > > > > requiring 4 pointers per node. Prior to reference count checking, t= he > > > > rbnode was embedded in the map so 3 pointers per node were > > > > necessary. This change switches the rbtree to an array lazily sorte= d > > > > by address, much as the array sorting nodes by name. 1 pointer is > > > > needed per node, but to avoid excessive resizing the backing array = may > > > > be twice the number of used elements. Meaning the memory overhead i= s > > > > roughly half that of the rbtree. For a perf record with > > > > "--no-bpf-event -g -a" of true, the memory overhead of perf inject = is > > > > reduce fom 3.3MB to 3MB, so 10% or 300KB is saved. > > > > > > > > Map inserts always happen at the end of the array. The code tracks > > > > whether the insertion violates the sorting property. O(log n) rb-tr= ee > > > > complexity is switched to O(1). > > > > > > > > Remove slides the array, so O(log n) rb-tree complexity is degraded= to > > > > O(n). > > > > > > > > A find may need to sort the array using qsort which is O(n*log n), = but > > > > in general the maps should be sorted and so average performance sho= uld > > > > be O(log n) as with the rbtree. > > > > > > > > An rbtree node consumes a cache line, but with the array 4 nodes fi= t > > > > on a cache line. Iteration is simplified to scanning an array rathe= r > > > > than pointer chasing. > > > > > > > > Overall it is expected the performance after the change should be > > > > comparable to before, but with half of the memory consumed. > > > > > > > > To avoid a list and repeated logic around splitting maps, > > > > maps__merge_in is rewritten in terms of > > > > maps__fixup_overlap_and_insert. maps_merge_in splits the given mapp= ing > > > > inserting remaining gaps. maps__fixup_overlap_and_insert splits the > > > > existing mappings, then adds the incoming mapping. By adding the ne= w > > > > mapping first, then re-inserting the existing mappings the splittin= g > > > > behavior matches. > > > > > > > > Signed-off-by: Ian Rogers > > > > Acked-by: Namhyung Kim > > > > --- > > > [SNIP] > > > > int maps__for_each_map(struct maps *maps, int (*cb)(struct map *ma= p, void *data), void *data) > > > > { > > > > - struct map_rb_node *pos; > > > > + bool done =3D false; > > > > int ret =3D 0; > > > > > > > > - down_read(maps__lock(maps)); > > > > - maps__for_each_entry(maps, pos) { > > > > - ret =3D cb(pos->map, data); > > > > - if (ret) > > > > - break; > > > > + /* See locking/sorting note. */ > > > > + while (!done) { > > > > + down_read(maps__lock(maps)); > > > > + if (maps__maps_by_address_sorted(maps)) { > > > > + /* > > > > + * maps__for_each_map callbacks may buggily= /unsafely > > > > + * insert into maps_by_address. Deliberatel= y reload > > > > + * maps__nr_maps and maps_by_address on eac= h iteration > > > > + * to avoid using memory freed by maps__ins= ert growing > > > > + * the array - this may cause maps to be sk= ipped or > > > > + * repeated. > > > > + */ > > > > + for (unsigned int i =3D 0; i < maps__nr_map= s(maps); i++) { > > > > + struct map **maps_by_address =3D ma= ps__maps_by_address(maps); > > > > > > Any chance they can move out of the loop? I guess not as they are > > > not marked to const/pure functions.. > > > > It's not because the cb(...) call below will potentially modify > > maps_by_address by inserting maps and reallocating the array. Having > > it outside the loop was what caused the original bug. > > Oh, I meant if compiler can move them automatically. The const (on the accessor) isn't sufficient for that, they'd perhaps need to be restrict. maps here is neither const or restrict which means the callback can modify it (even though it isn't an argument) and the function here needs to reload its value. Thanks, Ian > Thanks, > Namhyung > > > > > > > > > > > + struct map *map =3D maps_by_address= [i]; > > > > + > > > > + ret =3D cb(map, data); > > > > + if (ret) > > > > + break; > > > > + } > > > > + done =3D true; > > > > + } > > > > + up_read(maps__lock(maps)); > > > > + if (!done) > > > > + maps__sort_by_address(maps); > > > > } > > > > - up_read(maps__lock(maps)); > > > > return ret; > > > > }