Re: [PATCH 11/18] perf metric: Add referenced metrics to hash data

From: Jiri Olsa
Date: Fri Jul 17 2020 - 09:51:52 EST


On Wed, Jul 15, 2020 at 11:36:09PM +0200, Jiri Olsa wrote:
> On Wed, Jul 15, 2020 at 11:25:14AM -0700, Ian Rogers wrote:
> > On Sun, Jul 12, 2020 at 6:27 AM Jiri Olsa <jolsa@xxxxxxxxxx> wrote:
>
> SNIP
>
> > > +int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
> > > +{
> > > + struct expr_id_data *data_ptr = NULL, *old_data = NULL;
> > > + char *old_key = NULL;
> > > + char *name;
> > > + int ret;
> > > +
> > > + data_ptr = zalloc(sizeof(*data_ptr));
> > > + if (!data_ptr)
> > > + return -ENOMEM;
> > > +
> > > + name = strdup(ref->metric_name);
> > > + if (!name) {
> > > + free(data_ptr);
> > > + return -ENOMEM;
> > > + }
> > > +
> > > + data_ptr->ref.metric_name = ref->metric_name;
> > > + data_ptr->ref.metric_expr = ref->metric_expr;
> >
> > Having one owned string and one unowned makes the memory management
> > here somewhat complicated. Perhaps dupe both?
>
> right, will check on this

actualy, both of them are pointers to const char strings from struct pmu_event,
so they don't need to be freed

the journey starts at __add_metric function, where we
get the struct pmu_event pointers:

ref->metric_name = pe->metric_name;
ref->metric_expr = pe->metric_expr;

then it's passed to struct metric_ref in metricgroup__setup_events:

metric_refs[i].metric_name = ref->metric_name;
metric_refs[i].metric_expr = ref->metric_expr;

still 'const char*'.. and ending up as part of the value in
expr__add_ref function:

data_ptr->ref.metric_name = ref->metric_name;
data_ptr->ref.metric_expr = ref->metric_expr;

I'll put comments in all those places so it's evident that it's intentional

jirka