From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Jiri Olsa <jolsa@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>, lkml <linux-kernel@vger.kernel.org>,
Ingo Molnar <mingo@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
David Ahern <dsahern@gmail.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: Re: [PATCH 08/19] perf tools: Add mem2node object
Date: Thu, 8 Mar 2018 09:58:49 -0300 [thread overview]
Message-ID: <20180308125849.GA3701@kernel.org> (raw)
In-Reply-To: <20180308110306.GB13693@krava>
Em Thu, Mar 08, 2018 at 12:03:07PM +0100, Jiri Olsa escreveu:
> On Wed, Mar 07, 2018 at 04:27:36PM -0300, Arnaldo Carvalho de Melo wrote:
>
> SNIP
>
> > > diff --git a/tools/perf/util/mem2node.c b/tools/perf/util/mem2node.c
> > > new file mode 100644
> > > index 000000000000..6da8ddbb1182
> > > --- /dev/null
> > > +++ b/tools/perf/util/mem2node.c
> > > @@ -0,0 +1,129 @@
> > > +#include <errno.h>
> > > +#include <inttypes.h>
> > > +#include <linux/bitmap.h>
> > > +#include "mem2node.h"
> > > +#include "util.h"
> > > +
> > > +struct entry {
> > > + struct rb_node rb_node;
> > > + u64 start;
> > > + u64 end;
> > > + u64 node;
> > > +};
> >
> > Hey, this name is way too generic, please rename it to something more
> > descriptive
>
> ok, will change
>
> >
> > > +
> > > +static void entry__insert(struct entry *entry, struct rb_root *root)
> > > +{
> > > + struct rb_node **p = &root->rb_node;
> > > + struct rb_node *parent = NULL;
> > > + struct entry *e;
> > > +
> > > + while (*p != NULL) {
> > > + parent = *p;
> > > + e = rb_entry(parent, struct entry, rb_node);
> > > +
> > > + if (entry->start < e->start)
> > > + p = &(*p)->rb_left;
> > > + else
> > > + p = &(*p)->rb_right;
> > > + }
> > > +
> > > + rb_link_node(&entry->rb_node, parent, p);
> > > + rb_insert_color(&entry->rb_node, root);
> > > +}
> > > +
> > > +int mem2node__init(struct mem2node *map, struct perf_env *env)
> > > +{
> > > + struct memory_node *n, *nodes = &env->memory_nodes[0];
> > > + u64 bsize = env->memory_bsize;
> > > + struct entry *entry;
> > > + int i, j = 0, max = 0;
> > > +
> > > + memset(map, 0x0, sizeof(*map));
> > > + map->root = RB_ROOT;
> > > +
> > > + for (i = 0; i < env->nr_memory_nodes; i++) {
> > > + n = &nodes[i];
> > > + max += bitmap_weight(n->set, n->size);
> > > + }
> > > +
> > > + entry = zalloc(sizeof(*entry) * max);
> > > + if (!entry)
> > > + return -ENOMEM;
> >
> > Also this is not an 'entry', but max ones, please rename this variable
> > to 'entries'.
>
> ok
>
>
> SNIP
>
> > > +
> > > + entry[j].start = start;
> > > + entry[j].end = start + bsize;
> > > + entry[j].node = n->node;
> > > + RB_CLEAR_NODE(&entry[j].rb_node);
> >
> > The previous four line could be done via the usual:
> >
> > krava_entry__init(&entries[j], start, bsize, n->node);
>
> ook
>
> >
> > > + j++;
> > > + }
> > > + }
> > > +
> > > + /* Cut unused entries, due to merging. */
> > > + entry = realloc(entry, sizeof(*entry) * j);
> > > + if (!entry)
> > > + return -ENOMEM;
> >
> >
> > Humm, so you lose it when not able to cut it short? Why not:
>
> just shortening the memory, because some entries merge together
>
>
> >
> > nentries = realloc(entries, sizeof(entries[0) * j);
> > if (nentries)
> > entries = nentries;
>
> I don't think we need nentries.. AFAIK realloc works ok over single variable
So:
1) you alloc entries with a max number of entries
2) you go on populating it
3) there are some left, lets shrink it:
entries = realloc(entries, nr_entries * sizeof(entries[0]);
Here it will probably not fail, but you check it anyway, and that is
right, what happens if this returns NULL? entries gets set to NULL,
we lose the reference to the allocated memory and you return -ENOMEM,
right?
We end up leaking entries when what I'm suggesting you to do is to
not clobber entries with the return of realloc() (doing it this way most
of the time leads to bugs), but instead store it to a temp var
(nentries), and if it succeeds, then you know that you can
set nentries to entries and go ahead with your nicely shrunk block of
memory.
If it fails, then you continue with the original block of memory, that
continues to have what you just set up, etc.
Lemme look a third time to your original patch, I must be missing
something...
- Arnaldo
next prev parent reply other threads:[~2018-03-08 12:58 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-07 15:50 [PATCH 00/19] perf tools: Assorted fixes Jiri Olsa
2018-03-07 15:50 ` [PATCH 01/19] perf report: Fix the output for stdio events list Jiri Olsa
2018-03-09 8:51 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 02/19] perf report: Display perf.data header info Jiri Olsa
2018-03-09 8:52 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 03/19] perf record: Move machine variable down the function Jiri Olsa
2018-03-09 8:52 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 04/19] perf record: Remove progname from struct record Jiri Olsa
2018-03-09 8:53 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 05/19] perf tools: Add refcnt into struct mem_info Jiri Olsa
2018-03-07 18:56 ` Arnaldo Carvalho de Melo
2018-03-08 10:59 ` Jiri Olsa
2018-03-09 8:53 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 06/19] perf c2c: Use mem_info refcnt logic Jiri Olsa
2018-03-09 8:54 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 07/19] perf tools: Add MEM_TOPOLOGY feature to perf data file Jiri Olsa
2018-03-07 19:28 ` Arnaldo Carvalho de Melo
2018-03-09 8:54 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 08/19] perf tools: Add mem2node object Jiri Olsa
2018-03-07 19:27 ` Arnaldo Carvalho de Melo
2018-03-08 11:03 ` Jiri Olsa
2018-03-08 12:58 ` Arnaldo Carvalho de Melo [this message]
2018-03-08 13:00 ` Arnaldo Carvalho de Melo
2018-03-08 13:18 ` Jiri Olsa
2018-03-07 15:50 ` [PATCH 09/19] perf tests: Add mem2node object test Jiri Olsa
2018-03-07 15:50 ` [PATCH 10/19] perf c2c record: Record physical addresses in samples Jiri Olsa
2018-03-07 15:50 ` [PATCH 11/19] perf c2c report: Make calc_width work with struct c2c_hist_entry Jiri Olsa
2018-03-07 15:50 ` [PATCH 12/19] perf c2c report: Call calc_width only for displayed entries Jiri Olsa
2018-03-07 15:50 ` [PATCH 13/19] perf c2c report: Display node for cacheline address Jiri Olsa
2018-03-07 15:50 ` [PATCH 14/19] perf c2c report: Add span header over cacheline data Jiri Olsa
2018-03-07 15:50 ` [PATCH 15/19] perf c2c report: Add cacheline address count column Jiri Olsa
2018-03-07 15:50 ` [PATCH 16/19] perf tools: Update tags with .cpp files Jiri Olsa
2018-03-07 19:28 ` Arnaldo Carvalho de Melo
2018-03-09 8:55 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 17/19] perf build: Add llvm/clang/cxx make tests into FEATURE_TESTS_EXTRA Jiri Olsa
2018-03-09 8:55 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 18/19] perf build: Add llvm/clang make targets to FILES Jiri Olsa
2018-03-09 8:56 ` [tip:perf/core] " tip-bot for Jiri Olsa
2018-03-07 15:50 ` [PATCH 19/19] perf build: Force llvm/clang test compile output to .make.output Jiri Olsa
2018-03-07 19:30 ` Arnaldo Carvalho de Melo
2018-03-09 8:56 ` [tip:perf/core] " tip-bot for Jiri Olsa
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=20180308125849.GA3701@kernel.org \
--to=acme@kernel.org \
--cc=a.p.zijlstra@chello.nl \
--cc=alexander.shishkin@linux.intel.com \
--cc=dsahern@gmail.com \
--cc=jolsa@kernel.org \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=namhyung@kernel.org \
/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).