Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1013081pxb; Tue, 17 Aug 2021 01:26:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+hxTfvkI1hlmJ1DULqqKu6Uhm6K4ByKIx6pPbHNYexmLZ8aZ3K9wyTsuWLXrLmbrV1RhT X-Received: by 2002:a17:907:9705:: with SMTP id jg5mr2722196ejc.462.1629188790724; Tue, 17 Aug 2021 01:26:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629188790; cv=none; d=google.com; s=arc-20160816; b=syWsruHsoVpITAVEv3a31XSndnOjhC6RvfhI8dbs18pQxo0e0kSyG21hR6OWeSqLcf 6H52QeA39LxionrBjkMAjZXtLTo9ulXfCk9pHYwhvZFSl6OvagfSuI9IaRI6RpoWD9yh rUbRrJ+WXJwU3we6kccxRt0XciCFaJwJoS4V7dIQ6bhyuB8nzwqUqK+UwW0W5ECAm3h2 ZbLViRYNoTjoIiDWvAR6k5TPemckwXYgfnRNINW3myaHv6kZgvdRjmVCLBIoYvtuTZx3 49/FLUicvhzoJVOaitQu4dhqvrRdGDRL3uIhBi+WwvuYOG5ZpnuCXUckxOspwxAhpUfB 4znQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=RTZOR6rqW/Ta36/QVWlPkDfOViAsXgMWsUDA5bEHc1g=; b=yB53D8INO9utsWkpWtewkvdIro7lrr+H8PEhhoXRv3JM/alFGNEu7mgVjoUlP+0IEq sczK4PHs44UjtKsn2zJa8Qd8R7qFaLEr4kMV12sqhq4y6upHv4E4uReuUzHYK/Z/UkBf M4VycMvciwe6gDjG1VVprXe0HyXhTOihlDbkTR+tkGXVjbWLSuqBHBBuvK/LKAdEnm2w aiYOSUnyfKObC5Z//9loWQOFxm3tHxD0MKxu4Kc5tw8Ak23dgyEQT2Z7K3y5gbJRwvyy i0ukvW+LeUe3Ts7UfXnwNP83WtHJd39By3eFiwEKJxgrdyYVFRrmn8F1FqdduhVQpntO MoKQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hh2si1647063ejb.191.2021.08.17.01.26.08; Tue, 17 Aug 2021 01:26:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239121AbhHQIYJ (ORCPT + 99 others); Tue, 17 Aug 2021 04:24:09 -0400 Received: from mga07.intel.com ([134.134.136.100]:11537 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235120AbhHQIYH (ORCPT ); Tue, 17 Aug 2021 04:24:07 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10078"; a="279763165" X-IronPort-AV: E=Sophos;i="5.84,328,1620716400"; d="scan'208";a="279763165" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2021 01:23:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.84,328,1620716400"; d="scan'208";a="471080175" Received: from nntpat99-84.inn.intel.com ([10.125.99.84]) by orsmga008.jf.intel.com with ESMTP; 17 Aug 2021 01:23:31 -0700 From: Alexey Bayduraev To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Ingo Molnar , linux-kernel , Andi Kleen , Adrian Hunter , Alexander Antonov , Alexei Budankov , Riccardo Mancini Subject: [PATCH v11 01/24] perf record: Introduce thread affinity and mmap masks Date: Tue, 17 Aug 2021 11:23:04 +0300 Message-Id: <328c6ae8904139d1fba882a2daa75405a086e5cf.1629186429.git.alexey.v.bayduraev@linux.intel.com> X-Mailer: git-send-email 2.19.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Introduce affinity and mmap thread masks. Thread affinity mask defines cpus that a thread is allowed to run on. Thread maps mask defines mmap data buffers the thread serves to stream profiling data from. Acked-by: Andi Kleen Acked-by: Namhyung Kim Reviewed-by: Riccardo Mancini Tested-by: Riccardo Mancini Signed-off-by: Alexey Bayduraev --- tools/perf/builtin-record.c | 135 ++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 548c1dbde6c5..021a607510b5 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -87,6 +87,11 @@ struct switch_output { int cur_file; }; +struct thread_mask { + struct mmap_cpu_mask maps; + struct mmap_cpu_mask affinity; +}; + struct record { struct perf_tool tool; struct record_opts opts; @@ -111,6 +116,8 @@ struct record { 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; }; static volatile int done; @@ -2187,6 +2194,54 @@ static int record__parse_affinity(const struct option *opt, const char *str, int return 0; } +static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits) +{ + mask->nbits = nr_bits; + mask->bits = bitmap_alloc(mask->nbits); + if (!mask->bits) { + pr_err("Failed to allocate mmap_cpu mask\n"); + return -ENOMEM; + } + + return 0; +} + +static void record__mmap_cpu_mask_free(struct mmap_cpu_mask *mask) +{ + bitmap_free(mask->bits); + mask->nbits = 0; +} + +static void record__thread_mask_clear(struct thread_mask *mask) +{ + bitmap_zero(mask->maps.bits, mask->maps.nbits); + bitmap_zero(mask->affinity.bits, mask->affinity.nbits); +} + +static int record__thread_mask_alloc(struct thread_mask *mask, int nr_bits) +{ + int ret = record__mmap_cpu_mask_alloc(&mask->maps, nr_bits); + + if (ret) { + mask->affinity.bits = NULL; + return ret; + } + + ret = record__mmap_cpu_mask_alloc(&mask->affinity, nr_bits); + if (ret) { + record__mmap_cpu_mask_free(&mask->maps); + mask->maps.bits = NULL; + } + + return ret; +} + +static void record__thread_mask_free(struct thread_mask *mask) +{ + record__mmap_cpu_mask_free(&mask->maps); + record__mmap_cpu_mask_free(&mask->affinity); +} + static int parse_output_max_size(const struct option *opt, const char *str, int unset) { @@ -2635,6 +2690,79 @@ static struct option __record_options[] = { struct option *record_options = __record_options; +static void record__mmap_cpu_mask_init(struct mmap_cpu_mask *mask, struct perf_cpu_map *cpus) +{ + int c; + + for (c = 0; c < cpus->nr; c++) + set_bit(cpus->map[c], mask->bits); +} + +static void record__free_thread_masks(struct record *rec, int nr_threads) +{ + int t; + + if (rec->thread_masks) + for (t = 0; t < nr_threads; t++) + record__thread_mask_free(&rec->thread_masks[t]); + + zfree(&rec->thread_masks); +} + +static int record__alloc_thread_masks(struct record *rec, int nr_threads, int nr_bits) +{ + int t, ret; + + rec->thread_masks = zalloc(nr_threads * sizeof(*(rec->thread_masks))); + if (!rec->thread_masks) { + pr_err("Failed to allocate thread masks\n"); + return -ENOMEM; + } + + for (t = 0; t < nr_threads; t++) { + ret = record__thread_mask_alloc(&rec->thread_masks[t], nr_bits); + if (ret) + goto out_free; + record__thread_mask_clear(&rec->thread_masks[t]); + } + + return 0; + +out_free: + record__free_thread_masks(rec, nr_threads); + + return ret; +} + +static int record__init_thread_default_masks(struct record *rec, struct perf_cpu_map *cpus) +{ + int ret; + + ret = record__alloc_thread_masks(rec, 1, cpu__max_cpu()); + if (ret) + return ret; + + record__mmap_cpu_mask_init(&rec->thread_masks->maps, cpus); + + rec->nr_threads = 1; + + return 0; +} + +static int record__init_thread_masks(struct record *rec) +{ + struct perf_cpu_map *cpus = rec->evlist->core.cpus; + + return record__init_thread_default_masks(rec, cpus); +} + +static void record__fini_thread_masks(struct record *rec) +{ + record__free_thread_masks(rec, rec->nr_threads); + + rec->nr_threads = 0; +} + int cmd_record(int argc, const char **argv) { int err; @@ -2883,6 +3011,12 @@ int cmd_record(int argc, const char **argv) goto out; } + err = record__init_thread_masks(rec); + if (err) { + pr_err("record__init_thread_masks failed, error %d\n", err); + goto out; + } + if (rec->opts.nr_cblocks > nr_cblocks_max) rec->opts.nr_cblocks = nr_cblocks_max; pr_debug("nr_cblocks: %d\n", rec->opts.nr_cblocks); @@ -2901,6 +3035,7 @@ int cmd_record(int argc, const char **argv) symbol__exit(); auxtrace_record__free(rec->itr); out_opts: + record__fini_thread_masks(rec); evlist__close_control(rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close); return err; } -- 2.19.0