Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2671369rdb; Mon, 12 Feb 2024 12:20:15 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVLlZRYETbAAeQvDMWUJVjsv7K7cEkRll2XyI/Hinpt7Z0mT1BYpDoO0KG0Q7oCqCQsvuNRfLR2sd9cpXG9Z9yylc5QPj6EHOt6raAB6A== X-Google-Smtp-Source: AGHT+IEchNCgmV2sQuH6GPMeOAp5iJn58a24nIowNw1fiIZYaC2Ns8yWo5d+hJMyBNG3cRPcJF0U X-Received: by 2002:a92:4b0b:0:b0:363:e034:47f1 with SMTP id m11-20020a924b0b000000b00363e03447f1mr9163653ilg.0.1707769214986; Mon, 12 Feb 2024 12:20:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707769214; cv=pass; d=google.com; s=arc-20160816; b=LzkwZPZoD3kx7Oa7L46e8G6xtWzUItuLBp6ZCHlZzSx2pqb/XIyQfj7x2Xwg8DDSZm CbohzfYtXaO7wGzH/hMJo/KIAvhG9vxdu2S9oOuMx01m6yYnmWikUaafQ12jGDx9FOfE txZ3flAszTt8vgK/j/mKrFLk1Fw+LDgdbMfkfbAenEnpRjz9w9M8bygYW/CMJdyUdupG osDmXlTbh57V6I6P3suN2iKthxwTL+Drgwdzs/eFVEKlK9ZC+Hrs3kyb1sGUMf3S04IC a1FnYweKjxROU93qf48mwaR0mcPOTlBD7yEQir0Kl+z+fMKf/yHXIytMRX/2k0LKkyDP xohw== 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=Icnm5yBA0aCxHiFxkqBy8LL31RhB/IB62tLF3Y2+Rh4=; fh=mYNmsOShIjcfpTQZDJ+G62h1Tpv3n1eKK30K5Tqy1BY=; b=aftHCze1kP9Aim4NmGDxRSSE4qWsAWyAGxl5iEfDlGyO4qB0A4DoLhUEM/sFQ6dRep odlrH4+sBU6TTkygAVeNsjQbMR72VbFUHTGFBYrCpBFAsU3r9m98kzJBpZLjAayy8V3G HQfLAXtuEiUey6iAPBb6FnKqLH7LbQQ59k/dX4HmYyCrYfSv2zGVgYKYid0vqummihJ+ r9ZA3xCiN4CLhsw27fUUd2ZG1iVV5p/SRyqplpkbfSuLtWrA3piPidVpuPHwfJ5wgKmU tkc6wlAGKwji5eZPPBTv6MfOCEghzH+LP7LvWA7Cd/D2luMoeHpt4k5xi728iNfwWNCT PQIg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=11FMG5XN; 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-62326-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62326-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; AJvYcCUnti16M5s7q1ntYsVZOnw2iane7pefL08JVH4VV5fNn8lweWwMLL8tcZHtUdzaD5QhzN4sVmAuRvf05zIFBbmYYueeHNrMu2X4UiIMRg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id h24-20020a633858000000b005cdfa102332si689007pgn.601.2024.02.12.12.20.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 12:20:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62326-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=11FMG5XN; 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-62326-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62326-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 EC178286905 for ; Mon, 12 Feb 2024 20:19:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3655E481AD; Mon, 12 Feb 2024 20:19:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="11FMG5XN" Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 B86F147A7A for ; Mon, 12 Feb 2024 20:19:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707769184; cv=none; b=qe8fuP8XrE0DQkRpNbV4iqTfd459e4mLVgQd4HcLiumnxPabW+hcJHsyf1zZc2ccz8sPnSejjjcZ+ocUlGx2fDYEr17kleFiFQd5CMXfx062N9zNh7DVG20jy+FE9RScP0/ZW5/M1V+LZ8Ic5UFobIGrDZ+vsU7rzPxuiOOtPJk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707769184; c=relaxed/simple; bh=uJODUvONYUlc20hpmSj0J1uA1bYdfMjYs2on9f3uMaU=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=dMOyMYcbEH5BFlWpomDO2WzwJbYFVfLhwaPPWmedbEyClAIJFFJuINeuKpJQIxaqSJ8+qvwD7t5aKrIVpPl5TNc75v4bE+G/FnXIzwHUmM++rWLYKA+xugS6JJB4GpU/L+eQ0S3hbGjtoIc7mOJLuoYNsoUwRHh5df6IWin/OG4= 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=11FMG5XN; arc=none smtp.client-ip=209.85.214.175 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-f175.google.com with SMTP id d9443c01a7336-1d93b982761so49205ad.0 for ; Mon, 12 Feb 2024 12:19:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707769182; x=1708373982; 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=Icnm5yBA0aCxHiFxkqBy8LL31RhB/IB62tLF3Y2+Rh4=; b=11FMG5XNHfIstKlv1zF3fyvh+Of51VOIQ2onsVY8flJARraF4gjA5LIN+TFj2G1O5W JWiTYiKZ+SEZLn/+eX1i9XDfHJE+fcfUbAclkhxjoN9r6sxui/mioNKfqwkLfYqQ+lHA H77yyFuuYc9HakPkptohUGOu4QJ7/e3wzSYQXPcQm6A3o6wGW2MciPyiWFpIvEBBCqjj TI/wYJ34DXgJ6R5fHY/mLEUAuhJXGEUn2yghX4Mj2RL82+ltrj89lcoxG57yLODw4jGD 0/dmZyZAP206IU2Bk/rFDz0iwPpsvL4FvZvmbQbPZDKX6ctgz3naW+Tl+PG6p3a/gQsg cRVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707769182; x=1708373982; 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=Icnm5yBA0aCxHiFxkqBy8LL31RhB/IB62tLF3Y2+Rh4=; b=NWZdR9xqVA3cbQYUjHN/t6yjfxVnWHDoBmcKyo8Ui36DGZoWOuv5AwDNQ1ieLa0N1f RGMeuVdETtfMcr0TitekF1kCB0HLRpnZrvM/Oi1Q5JxiW0yBhXZknaao2erE15ipUvMF BNI6UzR72zd8JH6vrUOcn/fgRyX2thr5K5gu5E1oih5OYC6mNIEt6JaZAIknz28Wnifp YHhKgnV3/pjmmPrVdu4T9aNtHKToTFshdEgV0q5XfaIWb6/eQYTPU+hTiJw63x5cDNo/ 6OHlNhf5h+bUy4SoJZHWwrqbKq9NAsWeaAQSBg8lXnK79tc4vchavd9HbzyDNXFrSOlc yTMQ== X-Forwarded-Encrypted: i=1; AJvYcCVAa9Q//jHF6iVEWXHcXSxHk+QpjwaFi9yo/mBmNQoCACDa34tyI+Jn2YdtegI5/yEUjTtMyDnFy9tUeNSYxIcmt7brBtCVWvTDN5NP X-Gm-Message-State: AOJu0YxXHPnqTRha+5Vbc9MOyUFcPJUrE46oIQrfkcZq3kCDs68sda7I CPJVMQYDM/NUI14/Li+UuCvT9sL/J9pD47uFTy48C1auuFQfcyFdJ1PfwrMcZlzZHzLOSuQPG/+ J/1aq6Yd7Hof5aA1ANBI+GgbPn2VW80k45IIh X-Received: by 2002:a17:902:c1d4:b0:1d8:e076:21f3 with SMTP id c20-20020a170902c1d400b001d8e07621f3mr550plc.2.1707769181746; Mon, 12 Feb 2024 12:19:41 -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:19:26 -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:15=E2=80=AFPM Namhyung Kim = wrote: > > On Fri, Feb 9, 2024 at 7:18=E2=80=AFPM Ian Rogers wr= ote: > > > > Maps is a collection of maps primarily sorted by the starting address > > of the map. Prior to this change the maps were held in an rbtree > > requiring 4 pointers per node. Prior to reference count checking, the > > rbnode was embedded in the map so 3 pointers per node were > > necessary. This change switches the rbtree to an array lazily sorted > > 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 is > > 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-tree > > 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 should > > be O(log n) as with the rbtree. > > > > An rbtree node consumes a cache line, but with the array 4 nodes fit > > on a cache line. Iteration is simplified to scanning an array rather > > 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 mapping > > inserting remaining gaps. maps__fixup_overlap_and_insert splits the > > existing mappings, then adds the incoming mapping. By adding the new > > mapping first, then re-inserting the existing mappings the splitting > > behavior matches. > > > > Signed-off-by: Ian Rogers > > Acked-by: Namhyung Kim > > --- > [SNIP] > > int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, v= oid *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/uns= afely > > + * insert into maps_by_address. Deliberately re= load > > + * maps__nr_maps and maps_by_address on each it= eration > > + * to avoid using memory freed by maps__insert = growing > > + * the array - this may cause maps to be skippe= d or > > + * repeated. > > + */ > > + for (unsigned int i =3D 0; i < maps__nr_maps(ma= ps); i++) { > > + struct map **maps_by_address =3D maps__= 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. 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; > > }