Re: [PATCH v10 05/24] perf record: Introduce thread local variable

Jiri Olsa
2021-09-12T20:46:00Z

On Mon, Jul 12, 2021 at 09:46:05AM +0300, Alexey Bayduraev wrote:
> Introduce thread local variable and use it for threaded trace streaming.
> Use thread affinity mask instead or record affinity mask in affinity
> modes.
>
> Acked-by: Andi Kleen <ak@linux.intel.com>
> Acked-by: Namhyung Kim <namhyung@gmail.com>
> Signed-off-by: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
> ---
> tools/perf/builtin-record.c | 139 ++++++++++++++++++++++++------------
> 1 file changed, 93 insertions(+), 46 deletions(-)
>
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index a25b23affc43..dfc5db24e037 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -110,6 +110,8 @@ struct record_thread {
> unsigned long waking;
> };
>
> +static __thread struct record_thread *thread;
> +
> struct record {
> struct perf_tool tool;
> struct record_opts opts;
> @@ -132,7 +134,6 @@ struct record {
> bool timestamp_boundary;
> struct switch_output switch_output;
> unsigned long long samples;
> - struct mmap_cpu_mask affinity_mask;
> unsigned long output_max_size; /* = 0: unlimited */
> int nr_threads;
> struct thread_mask *thread_masks;
> @@ -574,7 +575,7 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
> bf = map->data;
> }
>
> - rec->samples++;
> + thread->samples++;
> return record__write(rec, map, bf, size);
> }
>
> @@ -1317,19 +1318,29 @@ static struct perf_event_header finished_round_event = {
> .type = PERF_RECORD_FINISHED_ROUND,
> };
>
> -static void record__adjust_affinity(struct record *rec, struct mmap *map)
> +static int record__adjust_affinity(struct record *rec, struct mmap *map)

hm, why changing the return value if the caller does not check it?

jirka

> {
> + int ret = 0;
> +
> if (rec->opts.affinity != PERF_AFFINITY_SYS &&
> - !bitmap_equal(rec->affinity_mask.bits, map->affinity_mask.bits,
> - rec->affinity_mask.nbits)) {
> - bitmap_zero(rec->affinity_mask.bits, rec->affinity_mask.nbits);
> - bitmap_or(rec->affinity_mask.bits, rec->affinity_mask.bits,
> - map->affinity_mask.bits, rec->affinity_mask.nbits);
> - sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&rec->affinity_mask),
> - (cpu_set_t *)rec->affinity_mask.bits);
> - if (verbose == 2)
> - mmap_cpu_mask__scnprintf(&rec->affinity_mask, "thread");
> + !bitmap_equal(thread->mask->affinity.bits, map->affinity_mask.bits,
> + thread->mask->affinity.nbits)) {
> + bitmap_zero(thread->mask->affinity.bits, thread->mask->affinity.nbits);
> + bitmap_or(thread->mask->affinity.bits, thread->mask->affinity.bits,
> + map->affinity_mask.bits, thread->mask->affinity.nbits);
> + ret = sched_setaffinity(0, MMAP_CPU_MASK_BYTES(&thread->mask->affinity),
> + (cpu_set_t *)thread->mask->affinity.bits);
> + if (ret)
> + pr_err("threads[%d]: sched_setaffinity() call failed: %s\n",
> + thread->tid, strerror(errno));
> + if (verbose == 2) {
> + pr_debug("threads[%d]: addr=", thread->tid);
> + mmap_cpu_mask__scnprintf(&thread->mask->affinity, "thread");
> + pr_debug("threads[%d]: on cpu=%d\n", thread->tid, sched_getcpu());
> + }
> }
> +
> + return ret;
> }

SNIP