Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4200843pxf; Tue, 6 Apr 2021 10:13:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzPvqiUT1O1Z/4Ac0hJuJqNN399eZ/xQTY2q453r8W1K3OWoFy5Cvh+jO5adqzo23gJNtlk X-Received: by 2002:a05:6402:280c:: with SMTP id h12mr25447046ede.332.1617729179969; Tue, 06 Apr 2021 10:12:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617729179; cv=none; d=google.com; s=arc-20160816; b=bKnyYuRxhMf74vPBtzRoUIe3VWrM497Wp7kENp9GUyX6QrCloOAMFdMQZoFZ2UJBEa JLMuTkfnhtBZbwc6Ot40g2uCotyH9wrthebfU8TR4tnf0P+O5NY0JurVlkQjvakaJKBg Cvyb07wPhbTNwkBtuqic3fBhhqP/CgIckqD3NrKDUyiuwaHaFp3Q2pDjZwE4es39n3bG 1lDLfRci/GBFP6TEl2w7rtCnE15a0PCSiTIzcJ/RuVUmmqmE+re3BeGMPnhyxIKcC0SV Qu9MAeHbjRjIWB5MpsEHF0asY/VRnDNJ+DQSLr/ICETBoxgfsVXEJP/hIooMzRLrN7TE 1Ksg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:organization :references:cc:to:from:subject:ironport-sdr:ironport-sdr; bh=UIDp8qLCzJWwQyWV1fQcLxXDgrvWzTJI9e1cdq/R98Q=; b=esdOhkKNk3qmr3UONOmAd95kztZCO0c9ErxF8xoY9Ubw25hQRkfJMGMrbCxdw2abcF yB8jxBvUAVub9OoB5X+Zbf19QkS3+/lEjr2uNfqBHYZ8nEDxia3VuPKioOSDd4r2fOJW A6WpdlP3bguizRqwus+hNP07zQK5QjLWirqZ/G2JrQAKKZu5WSPjHTDzFCAcgrQMNVim nnth7KaPzpCYpM+m1My2YXZDqhfZ1xAcfeNCA6E+MLZYR7R6C1XeWJ2yvNQxRGewQQCB XW9L/537mdbD+UNht9s+gZAT3Z22DhlAJ/wrEONhxBTFEtwSXt4kSae2ySQ0sgJM62Lt LeOw== 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 lk24si6671792ejb.40.2021.04.06.10.12.36; Tue, 06 Apr 2021 10:12:59 -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 S233460AbhDFIjF (ORCPT + 99 others); Tue, 6 Apr 2021 04:39:05 -0400 Received: from mga02.intel.com ([134.134.136.20]:12795 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232951AbhDFIjE (ORCPT ); Tue, 6 Apr 2021 04:39:04 -0400 IronPort-SDR: Q5iDOWDkQrltxRad4zcmwWkQ9+jcHM8bOG9vDdefr1+mWJ4lxNWEbMJPlcxTp+sPVnR8x5tZ9d 84X8LIZ5PVow== X-IronPort-AV: E=McAfee;i="6000,8403,9945"; a="180151352" X-IronPort-AV: E=Sophos;i="5.81,308,1610438400"; d="scan'208";a="180151352" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Apr 2021 01:38:53 -0700 IronPort-SDR: RZU9tEN2JbNemmyFJVLp3HA1cRbBwhKP9oY7R7A+N6jW7CJHFeEEJFrQShVxZk69CrfWAxsp22 E07vY1Lhodtg== X-IronPort-AV: E=Sophos;i="5.81,308,1610438400"; d="scan'208";a="421119381" Received: from abaydur-mobl1.ccr.corp.intel.com (HELO [10.249.228.164]) ([10.249.228.164]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Apr 2021 01:38:49 -0700 Subject: [PATCH v4 01/12] perf record: introduce thread affinity and mmap masks From: "Bayduraev, Alexey V" To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Ingo Molnar , linux-kernel , Andi Kleen , Adrian Hunter , Alexei Budankov , Alexander Antonov References: <6c15adcb-6a9d-320e-70b5-957c4c8b6ff2@linux.intel.com> Organization: Intel Corporation Message-ID: <74656230-fa8c-3263-1a55-8dd74c66b79f@linux.intel.com> Date: Tue, 6 Apr 2021 11:38:46 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: <6c15adcb-6a9d-320e-70b5-957c4c8b6ff2@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit 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. Signed-off-by: Alexey Bayduraev --- tools/perf/builtin-record.c | 117 ++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 35465d1db6dd..e0cbf05d255c 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -85,6 +85,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; @@ -109,6 +114,8 @@ struct record { unsigned long long samples; struct mmap_cpu_mask affinity_mask; unsigned long output_max_size; /* = 0: unlimited */ + struct thread_mask *thread_masks; + int nr_threads; }; static volatile int done; @@ -2176,6 +2183,45 @@ 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) +{ + if (record__mmap_cpu_mask_alloc(&mask->maps, nr_bits) || + record__mmap_cpu_mask_alloc(&mask->affinity, nr_bits)) + return -ENOMEM; + + return 0; +} + +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) { @@ -2611,6 +2657,70 @@ 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 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) + return ret; + record__thread_mask_clear(&rec->thread_masks[t]); + } + + return 0; +} +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 int record__fini_thread_masks(struct record *rec) +{ + int t; + + if (rec->thread_masks) + for (t = 0; t < rec->nr_threads; t++) + record__thread_mask_free(&rec->thread_masks[t]); + + zfree(&rec->thread_masks); + + rec->nr_threads = 0; + + return 0; +} + int cmd_record(int argc, const char **argv) { int err; @@ -2844,6 +2954,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); @@ -2862,6 +2978,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