Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp3388140pxy; Tue, 4 May 2021 00:33:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYvDkanYiidWLtCxXh9qJziekedeSD7iuPNGYkR5e3Zn4caD4rbKMzfXscvMO2tS9MLuRG X-Received: by 2002:a17:90a:410e:: with SMTP id u14mr3483618pjf.45.1620113606944; Tue, 04 May 2021 00:33:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620113606; cv=none; d=google.com; s=arc-20160816; b=iRPWPeiNIQJr379wz53PBdql2+ao+SP2dJoYYzOVooFJzWIaH2IqQ4nCHArA29ywIZ yVqsBZYwUwtbsGwuZIPv2zz+0aXKr0Df6FKURFDHEQpE0pgFNg+E5U3CmnDDJrb/5tzh BhnUSZIQQr1WUjXlJ5KQQDDdh7rumbSq5zdrJSWM8LLH/r59JlLEmPayWuWfK7WvXzqD p1lJkxY6S8x20+x6LnyOHNlGtZjPzUOvjv6KRfSkvipG4ojc93+mAiqzEFBXuFG/hPu3 BBjoj0omQ48zZZabBWdfw/CYeNl8y76Ejd1mxkEztX11X0gsN67Gikoz4OYcIZR4iK7C VDIQ== 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=d8455Y7T7/6uAFDCX85Psv6oAaYT3X4kZulDtvJB59Q=; b=02ZEsmQrLrbGjuZ7TgFHIMWoENVQBgvDyXuwwTxWdZip+tjxaKaoJgJGNbRvNTJCwL U/G++iv2D0PlFi35GgEpRTgA2oeFxOKYkuAWKc9OhWWxpxsEhvTJdE58AYri8uNgTwEL WVr4dlhRHWuFb6q9tMQNd4hv3Cs0UMPM/qcV41hYOA9i6IDGKBQOshQGvnjq6AeimIEW hV1Rb4+YCop8pDMX77SHgTQi39KH5DVZO91R6UjZ2xNK4q6J92CEXcTc36L+qQ6j5DBh 38VgJ0npGXbIe+7+p0SkW9fsqAIK9FMtKjQKft406Dk7Uxu/9Kn6dDD2kAkwQVvTowYQ t9kQ== 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 n10si2934367plc.81.2021.05.04.00.33.14; Tue, 04 May 2021 00:33:26 -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 S229911AbhEDHGE (ORCPT + 99 others); Tue, 4 May 2021 03:06:04 -0400 Received: from mga07.intel.com ([134.134.136.100]:36026 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229890AbhEDHGB (ORCPT ); Tue, 4 May 2021 03:06:01 -0400 IronPort-SDR: FVoYgEArmvkeDtqNFWTPVdkvES5jx8opPNI6I+mTO5dw89n4QygsDgXZ2V4yvnLLecZxTT0gUG 61/6GL8D0nbA== X-IronPort-AV: E=McAfee;i="6200,9189,9973"; a="261865873" X-IronPort-AV: E=Sophos;i="5.82,271,1613462400"; d="scan'208";a="261865873" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 May 2021 00:05:06 -0700 IronPort-SDR: w1CcQPG0XrtHE+n21dbIPy8kYE/l6pdbHByKPC0ZuzA9j5NZaFQZCIZcgmBdP+rjTvrybNcl3q COXxo5CDmPBA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,271,1613462400"; d="scan'208";a="450895202" Received: from nntpat99-84.inn.intel.com ([10.125.99.84]) by fmsmga004.fm.intel.com with ESMTP; 04 May 2021 00:05:02 -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 Subject: [PATCH v5 01/20] perf record: introduce thread affinity and mmap masks Date: Tue, 4 May 2021 10:04:36 +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 | 117 ++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 3337b5f93336..4701aec4279c 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; @@ -2210,6 +2217,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) { @@ -2645,6 +2691,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; @@ -2887,6 +2997,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); @@ -2905,6 +3021,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