Re: [PATCH 37/48] perf record: Introduce struct record_thread

From: Namhyung Kim
Date: Mon Sep 17 2018 - 07:26:30 EST


Hi Jiri,

On Thu, Sep 13, 2018 at 02:54:39PM +0200, Jiri Olsa wrote:
> Adding struct record_thread to carry the single thread's maps.
>
> Link: http://lkml.kernel.org/n/tip-dsyi97xdc7ullvsisqmha0ca@xxxxxxxxxxxxxx
> Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
> ---
> tools/perf/builtin-record.c | 179 ++++++++++++++++++++++++++++++++++++
> 1 file changed, 179 insertions(+)
>
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 1b01cb4d06b8..5c6b56f164a9 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -65,6 +65,15 @@ struct switch_output {
> bool set;
> };
>
> +struct record_thread {
> + struct perf_mmap **mmap;
> + int mmap_nr;
> + struct perf_mmap **ovw_mmap;
> + int ovw_mmap_nr;
> + struct fdarray pollfd;
> + struct record *rec;
> +};
> +
> struct record {
> struct perf_tool tool;
> struct record_opts opts;
> @@ -83,6 +92,8 @@ struct record {
> bool timestamp_boundary;
> struct switch_output switch_output;
> unsigned long long samples;
> + struct record_thread *threads;
> + int threads_cnt;
> };
>
> static volatile int auxtrace_record__snapshot_started;
> @@ -967,6 +978,166 @@ static int record__synthesize(struct record *rec, bool tail)
> return err;
> }
>
> +static void
> +record_thread__clean(struct record_thread *th)
> +{
> + free(th->mmap);
> + free(th->ovw_mmap);
> +}
> +
> +static void
> +record__threads_clean(struct record *rec)
> +{
> + struct record_thread *threads = rec->threads;
> + int i;
> +
> + if (threads) {
> + for (i = 0; i < rec->threads_cnt; i++)
> + record_thread__clean(threads + i);
> + }
> +}
> +
> +static void record_thread__init(struct record_thread *th, struct record *rec)
> +{
> + memset(th, 0, sizeof(*th));
> + fdarray__init(&th->pollfd, 64);
> + th->rec = rec;
> +}
> +
> +static int
> +record_thread__mmap(struct record_thread *th, int nr, int nr_ovw)
> +{
> + struct perf_mmap **mmap;
> +
> + mmap = zalloc(sizeof(*mmap) * nr);
> + if (!mmap)
> + return -ENOMEM;
> +
> + th->mmap = mmap;
> + th->mmap_nr = nr;
> +
> + if (nr_ovw) {
> + mmap = zalloc(sizeof(*mmap) * nr_ovw);
> + if (!mmap)
> + return -ENOMEM;
> +
> + th->ovw_mmap = mmap;
> + th->ovw_mmap_nr = nr;

s/nr;/nr_ovw;/ ?


> + }
> +
> + return 0;
> +}
> +
> +static int
> +record__threads_assign(struct record *rec)
> +{
> + struct record_thread *threads = rec->threads;
> + struct record_thread *thread0 = threads;
> + struct perf_evlist *evlist = rec->evlist;
> + int i, j, nr, nr0, nr_ovw, nr_trk;
> + int ret = -ENOMEM;
> +
> + nr = evlist->mmap ? evlist->nr_mmaps : 0;
> + nr_trk = evlist->track_mmap ? evlist->nr_mmaps : 0;
> + nr_ovw = evlist->overwrite_mmap ? evlist->nr_mmaps : 0;
> +
> + nr0 = nr_trk;
> + nr0 += nr;
> +
> + if (record_thread__mmap(thread0, nr0, nr_ovw))
> + goto out_error;
> +
> + for (i = 0; i < nr_ovw; i++)
> + thread0->ovw_mmap[i] = &evlist->overwrite_mmap[i];
> +
> + for (i = 0; i < nr_trk; i++)
> + thread0->mmap[i] = &evlist->track_mmap[i];
> +
> + for (j = 0; i < nr0 && j < nr; i++, j++)
> + thread0->mmap[i] = &evlist->mmap[j];

I'm not sure it'll work with the overwrite mmap well..

Thanks,
Namhyung


> +
> + ret = 0;
> +
> +out_error:
> + return ret;
> +}