Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4509416pxj; Tue, 22 Jun 2021 01:43:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZyucPUFiP2JAN0BnQbXW/L6LAAVHT3klAcLlrvapwbhG3GslQHSIKVAkjx1t3PWC9gIuj X-Received: by 2002:a17:906:d288:: with SMTP id ay8mr2742444ejb.230.1624351432882; Tue, 22 Jun 2021 01:43:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624351432; cv=none; d=google.com; s=arc-20160816; b=Emxd0IWCEbL8Fzvfudyswwz+jeyTf4BaP0J1wOdMNPNISU1CxoJOBsnSkLN2vLPAIo lfMPtc27PCxqI+qEkbRt7voMDqVWieFlWaZDH8oAIlQsVYT726PNzJ9nr0b61HmiLhzk ZSF0BiA68QS1WnB+xKFeDaUvCZsfMMIlvcLF7q6rDeVguQX9d0sr6QGQvj9i73IpwupV Y93Ms0XRvk/f1ZZyFwe4CQPJyRenFa1pDN9R2BJK7MJwiaXT+uEfVeqBkLbDn6uUjkD+ XPj3t+hmhAk3HYCkkiHGutepM15/fUCrmj0GKpusOld/96DR9YMWFN4yYa212URIwWye X0rg== 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 :ironport-sdr:ironport-sdr; bh=Vbd5+cV8989DRAQIxizasUqCHtl57yGipAoQSkZuVhM=; b=TlQUoU5P+6FUvvqco4kB/4MJEvJ12BRI854RPUNAlLf5HzvWAgfP/0Jm7nVSUp+jgK B24tTZUY7Em+JDU4eAMfpqKlL89iJD8mJBT+EaYFoP0me7665w7JbW7EdMhNd8Dl1K7Q Q/7xQF92MTpk414BvT8HsA5KtLUNDH84SQyAfTEGmyq8AYgJgJxwXk7GXXCDX3svNzcj 9snzPcTVQ5gv1+JJuiFVSm3vvMO4vR3OM7Gzj3MZRjQ2D3idk4UzJfznjTxhn5mDagSa O0xaNXmSfp1PvgN+a34Xx+pntd3JRk8LXgQQA4Xh+dnGN+IRDNaI22DwbQ9PKCqOBQFi IGjA== 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 hz14si9472983ejc.533.2021.06.22.01.43.30; Tue, 22 Jun 2021 01:43:52 -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 S230409AbhFVIow (ORCPT + 99 others); Tue, 22 Jun 2021 04:44:52 -0400 Received: from mga05.intel.com ([192.55.52.43]:57595 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230151AbhFVIov (ORCPT ); Tue, 22 Jun 2021 04:44:51 -0400 IronPort-SDR: tvZTAmGg62PEiTC6r4oWb0WTWG11TCIdUTUBsWGenbw9ByPS9y2j0YbYYRPS/Iz5zOv+D62O58 vQifFl/aGD3w== X-IronPort-AV: E=McAfee;i="6200,9189,10022"; a="292641501" X-IronPort-AV: E=Sophos;i="5.83,291,1616482800"; d="scan'208";a="292641501" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jun 2021 01:42:36 -0700 IronPort-SDR: K0HNODcoJWymAr21isLlzcF4xRhFqxZ1D6atuZOTTrj/JOXKdx8ne/duTxXq4fzlDlXxOlVLDe +C9et9CR95gQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,291,1616482800"; d="scan'208";a="417332455" Received: from nntpat99-84.inn.intel.com ([10.125.99.84]) by fmsmga007.fm.intel.com with ESMTP; 22 Jun 2021 01:42:33 -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 v7 01/20] perf record: Introduce thread affinity and mmap masks Date: Tue, 22 Jun 2021 11:42:10 +0300 Message-Id: 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 Signed-off-by: Alexey Bayduraev --- tools/perf/builtin-record.c | 127 ++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index bc3dd379eb67..bad8c3dae068 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 */ + struct thread_mask *thread_masks; + int nr_threads; }; static volatile int done; @@ -2216,6 +2223,55 @@ 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; + + 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) { @@ -2664,6 +2720,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; @@ -2906,6 +3026,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); @@ -2924,6 +3050,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