From: Namhyung Kim <namhyung@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
Song Liu <song@kernel.org>,
Colin Ian King <colin.i.king@gmail.com>,
Liam Howlett <liam.howlett@oracle.com>,
K Prateek Nayak <kprateek.nayak@amd.com>,
Artem Savkov <asavkov@redhat.com>,
Changbin Du <changbin.du@huawei.com>,
Masami Hiramatsu <mhiramat@kernel.org>,
Athira Rajeev <atrajeev@linux.vnet.ibm.com>,
Alexey Dobriyan <adobriyan@gmail.com>,
James Clark <james.clark@arm.com>,
Vincent Whitchurch <vincent.whitchurch@axis.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
bpf@vger.kernel.org, leo.yan@linux.dev
Subject: Re: [PATCH v3 1/6] perf maps: Switch from rbtree to lazily sorted array for addresses
Date: Mon, 12 Feb 2024 12:15:41 -0800 [thread overview]
Message-ID: <CAM9d7chEKepmHY_Mgvq27CEcKB1e8bENwn2=pMe-yin30nfGLA@mail.gmail.com> (raw)
In-Reply-To: <20240210031746.4057262-2-irogers@google.com>
On Fri, Feb 9, 2024 at 7:18 PM Ian Rogers <irogers@google.com> wrote:
>
> 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 <irogers@google.com>
> Acked-by: Namhyung Kim <namhyung@kernel.org>
> ---
[SNIP]
> int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data)
> {
> - struct map_rb_node *pos;
> + bool done = false;
> int ret = 0;
>
> - down_read(maps__lock(maps));
> - maps__for_each_entry(maps, pos) {
> - ret = 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. Deliberately reload
> + * maps__nr_maps and maps_by_address on each iteration
> + * to avoid using memory freed by maps__insert growing
> + * the array - this may cause maps to be skipped or
> + * repeated.
> + */
> + for (unsigned int i = 0; i < maps__nr_maps(maps); i++) {
> + struct map **maps_by_address = 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..
Thanks,
Namhyung
> + struct map *map = maps_by_address[i];
> +
> + ret = cb(map, data);
> + if (ret)
> + break;
> + }
> + done = true;
> + }
> + up_read(maps__lock(maps));
> + if (!done)
> + maps__sort_by_address(maps);
> }
> - up_read(maps__lock(maps));
> return ret;
> }
next prev parent reply other threads:[~2024-02-12 20:15 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-10 3:17 [PATCH v3 0/6] maps memory improvements and fixes Ian Rogers
2024-02-10 3:17 ` [PATCH v3 1/6] perf maps: Switch from rbtree to lazily sorted array for addresses Ian Rogers
2024-02-12 20:15 ` Namhyung Kim [this message]
2024-02-12 20:19 ` Ian Rogers
2024-02-12 20:26 ` Namhyung Kim
2024-02-12 20:37 ` Ian Rogers
2024-02-10 3:17 ` [PATCH v3 2/6] perf maps: Get map before returning in maps__find Ian Rogers
2024-02-10 3:17 ` [PATCH v3 3/6] perf maps: Get map before returning in maps__find_by_name Ian Rogers
2024-02-10 3:17 ` [PATCH v3 4/6] perf maps: Get map before returning in maps__find_next_entry Ian Rogers
2024-02-10 3:17 ` [PATCH v3 5/6] perf maps: Hide maps internals Ian Rogers
2024-02-10 3:17 ` [PATCH v3 6/6] perf maps: Locking tidy up of nr_maps Ian Rogers
2024-02-13 18:09 ` [PATCH v3 0/6] maps memory improvements and fixes Namhyung Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAM9d7chEKepmHY_Mgvq27CEcKB1e8bENwn2=pMe-yin30nfGLA@mail.gmail.com' \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adobriyan@gmail.com \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=asavkov@redhat.com \
--cc=atrajeev@linux.vnet.ibm.com \
--cc=bpf@vger.kernel.org \
--cc=changbin.du@huawei.com \
--cc=colin.i.king@gmail.com \
--cc=irogers@google.com \
--cc=james.clark@arm.com \
--cc=jolsa@kernel.org \
--cc=kprateek.nayak@amd.com \
--cc=leo.yan@linux.dev \
--cc=liam.howlett@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=song@kernel.org \
--cc=vincent.whitchurch@axis.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).