Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3525310pxb; Mon, 16 Nov 2020 17:49:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJxr0h5CUYWZc7NlXRXMPjkhQo6O0+3AnNfY2kseVY0vZnvGo3EnPzhWnfb6OFwG837v886q X-Received: by 2002:a50:ab15:: with SMTP id s21mr18445900edc.88.1605577799669; Mon, 16 Nov 2020 17:49:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605577799; cv=none; d=google.com; s=arc-20160816; b=tYkyQ7MQxGfg/zUcmjsVrngYw+g6vnXftL++BuFN44dTDxFkhiXq+rv62D6vTq1cI3 /hpATuxwctGfdpule3XEjOYL628punke208R1H7pp1lbKbspHQB03MAYZfTc5np5O6Gh BxJbhGxStHqh6ynGSmoODmvbJ45d6j8hNgbAIKqk7MRO4yb8XM3YQ1dcruaptQ++A6H2 OmsIy2+KAhhCJuIrjHg7aU5oWOnzvYcvsMUGAJdixCpn/4puNNa9tyEsVNxRB7SFU98z uBunVA8oOMk6lhlMk/SQpS1Ye0jzp8G+89xVd5zKUPm4V+ONVvGLy7BhIl8M+jD+jtPd 7ijw== 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=VpmMr0B85aRVqrhaIW26Wgpf6garYDh0ljY4NjTGbD8=; b=SCw+KpTKvlTzPy5fkYm8kO3eiv4Y0wIexk71kyiBGLbORbl8E4LGevTj4bgSr/+u4g IWKvWI5qF+lLV/hrvXHGYMZr87OqbIBnOf/j29E3gt6hUz7I+yaRWsc/V2vM8WYyhjOT bSMcI+N0VuS8So/B5SOCzhTfzL5Klr9VtvUFf5Bbpk0G53g2N1zk3Miqt/Sn9tH6tCW0 f0L2XHo2Fswxx6TU9XdnjiUO/ai/qQKvcdo+iKyGJV208YvtBtrekVXG6z2LTHhN1xGN qkIx7AgXVE9n5jG79riVk624i6AM00l5oaeJX80f8AUk3PH8K9C9fCkolG5B0PqXJcYw XwyQ== 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 i24si12382170eja.728.2020.11.16.17.49.37; Mon, 16 Nov 2020 17:49:59 -0800 (PST) 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 S1729902AbgKPMO5 (ORCPT + 99 others); Mon, 16 Nov 2020 07:14:57 -0500 Received: from mga07.intel.com ([134.134.136.100]:32675 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727895AbgKPMO5 (ORCPT ); Mon, 16 Nov 2020 07:14:57 -0500 IronPort-SDR: voSIYMugpXKQtIK4VUEV7E1cpsHft+qPBi6MFalRijbhkVvjkgZu04qjWnqPf2bhpbzqeLKQK1 ndRgHYWC/6dA== X-IronPort-AV: E=McAfee;i="6000,8403,9806"; a="234889752" X-IronPort-AV: E=Sophos;i="5.77,482,1596524400"; d="scan'208";a="234889752" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Nov 2020 04:14:55 -0800 IronPort-SDR: B+3mx4uj/VQNno/47KSRH4PCCVE6HiU6RqcRiXd/e82TH9gIXI0Z49ZqfS5kBLwTC6XV5KVxT/ PFPijW79kdew== X-IronPort-AV: E=Sophos;i="5.77,482,1596524400"; d="scan'208";a="543582249" Received: from abudanko-mobl.ccr.corp.intel.com (HELO [10.249.228.209]) ([10.249.228.209]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Nov 2020 04:14:52 -0800 Subject: [PATCH v3 01/12] perf record: introduce thread affinity and mmap masks From: Alexey Budankov To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Ingo Molnar , linux-kernel , Andi Kleen , Adrian Hunter , Alexey Bayduraev , Alexander Antonov References: <7d197a2d-56e2-896d-bf96-6de0a4db1fb8@linux.intel.com> Organization: Intel Corp. Message-ID: Date: Mon, 16 Nov 2020 15:14:50 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.4.3 MIME-Version: 1.0 In-Reply-To: <7d197a2d-56e2-896d-bf96-6de0a4db1fb8@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US 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 Budankov --- tools/perf/builtin-record.c | 116 ++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index adf311d15d3d..82f009703ad7 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; @@ -108,6 +113,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; @@ -2174,6 +2181,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) { @@ -2603,6 +2649,69 @@ 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; + + 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; @@ -2821,6 +2930,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); @@ -2839,6 +2954,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.24.1