From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,
	SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id AC145C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:49 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 851E4611EF
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:49 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233420AbhHUJU1 (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:27 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34008 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233240AbhHUJU1 (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:27 -0400
Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6813C061575;
        Sat, 21 Aug 2021 02:19:47 -0700 (PDT)
Received: by mail-ej1-x62d.google.com with SMTP id z20so25180779ejf.5;
        Sat, 21 Aug 2021 02:19:47 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:mime-version
         :content-transfer-encoding;
        bh=jNhwlYCFxIsxfxHGWTfCyvwhYhQYYLG4sGsEIO4+KlA=;
        b=RFeY+DOrcj8Ia/6gfRHvGUE0vHQdbmAxU1Upyc3xFBBrJdm/c+tNgC85qHVc22AMbA
         koS+zqBeSaYszwkuj4Ai5LYSlPNy7KqMNog2WKOd+zdnPsh4q3jsnzaYYi3aigGZC/MD
         3Y5wEVG/Om/9FUlWAVF+lb5n66YBOrJWUSWwr1jkVrIcDz4y5NbXmTQKYFE+NlCj/fp6
         AliTa8HvhGLBu1Sr9eFXdooEMDpvi9qJe3YjY0+mLUhF7HjLMZvbQV0wfFGBdb2VZzBz
         XtrmNEr3SYpoZI2d35y0iJGBuAJdQw6sWVRY32jgTy+hnLPIWPwdD7qGy1Fu8mK+Apqo
         2a5Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version
         :content-transfer-encoding;
        bh=jNhwlYCFxIsxfxHGWTfCyvwhYhQYYLG4sGsEIO4+KlA=;
        b=j6Qjt5bydGjsijA6Brb5Tkqze0Y5eFMVeke8TLNrvkY+Yj1IYk6WFRZYyu96Ict9ad
         B4oaD9XyvHnTVnv5S9AsE352EoPGevvxPyBhrtOCL/HtBdvsEp+c69TjcEFvb4tQiMCA
         7t2ucMLZO5fPPKMDen0HtDFUyyqYDIXOUKGWGFRVe96aEV5ps4kT3qkErRn6Kpg7ZsPX
         7Ieg4AGOFkIMHbORsAgM2QXcteQJPnF4fsWf7lq90tRnugCuMs0TXUbjvEzEbQ4T1nYw
         qGVNHYzrALhgcaQaMOtjOf1+970VebeP49FqMnk2fhrjNVtAJxNFyDJBhgpzrrdkKIjC
         rC/w==
X-Gm-Message-State: AOAM532Qvui3jEDEMwxyq/UiSjdN3nqzU48FpBRcw5HlEVxd90uyZrZR
        8dqViaMa8Yzvs6s8qRXH5GQ=
X-Google-Smtp-Source: ABdhPJzebR0NMfXrkQQS/6YrsPl3gnHV9MyEoPbavjZHhosYvUzvfySz2bwpyWQJjjoC61nQ8U9sxg==
X-Received: by 2002:a17:906:cd10:: with SMTP id oz16mr25148015ejb.428.1629537586318;
        Sat, 21 Aug 2021 02:19:46 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.44
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:45 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 00/37] perf: use workqueue for evlist operations
Date:   Sat, 21 Aug 2021 11:19:06 +0200
Message-Id: <cover.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Hi,

this patchset adds multithreading through the workqueue to the most
important evlist operations: enable, disable, close, and open.
Their multithreaded implementation is then used in perf-record through
a new option '--threads'.
It is dependent on the workqueue patchset (v3):
https://lore.kernel.org/lkml/cover.1629454773.git.rickyman7@gmail.com/

In each operation, each worker is assigned a cpu and pins itself to that
cpu to perform the operation on all evsels. In case enough threads are
provided, the underlying threads are pinned to the cpus, otherwise, they
just change their affinity temporarily.

Parallelization of enable, disable, and close is pretty straightforward,
while open requires more work to separate the actual open from all 
fallback mechanisms.
In the multithreaded implementation of open, each thread will run until it
finishes or gets an error. When all threads finish, the main process
checks for errors. If it finds one, it applies a fallback and resumes the
open on all cpus from where they encountered the error.

I have tested the main fallback mechanisms (precise_ip, ignore missing
thread,fd limit increase), but not all of the missing feature ones.
I also ran perf test, with no errors. Below you can find the skipped 
tests (I ommitted successfull results for brevity):
$ sudo ./perf test
23: Watchpoint                                                      :
23.1: Read Only Watchpoint                                          : Skip (missing hardware support)
58: builtin clang support                                           : Skip (not compiled in)
63: Test libpfm4 support                                            : Skip (not compiled in)
89: perf stat --bpf-counters test                                   : Skip
90: Check Arm CoreSight trace data recording and synthesized samples: Skip

I know the patchset is huge, but I didn't have time to split it (and 
my time is running out). In any case, I tried to keep related patches
close together. It is organized as follows:
 -  1 -  3: remove the cpu iterator inside evsel to simplify
            parallelization. In the doing, cpumap idx and max methods are
            improved based on the assumption that the cpumap is ordered.
 -  4 -  5: preparation patches for adding affinities to threadpool.
 -  6 -  8: add affinity support to threadpool and workqueue (preparation
            for adding workqueue to evsel).
 -       9: preparation for adding workqueue to evsel.
 - 10 - 13: add multithreading to evlist enable, disable, and close.
 - 14 - 27: preparation for adding multithreading to evlist__open.
 -      28: add multithreading to evlist__open.
 - 29 - 34: use multithreaded evlist operations in perf-record.
 - 35 - 38: improve evlist-open-close benchmark, adding multithreading
            and detailed output.

I'll be happy to split it if necessary in the future, with the goal of
merging my GSoC work, but, for now, I need to send it as is to include it
in my final report.

Below are some experimental results of evlist-open-close benchmark run on:
 - laptop (2 cores + hyperthreading)
 - vm (16 vCPUs)

The command line was:
$ ./perf bench internals evlist-open-close [-j] <options>

where [-j] was included only in the specified rows and <options> refers
to the column label:
 - "" (dummy): open one dummy event on all cpus.
 - "-n 100": open 100 dummy events on all cpus.
 - "-e '{cs,cycles}'": open the "{cs,cycles}" group on all cpus.
 - "-u 0": open one dummy event on all cpus for all processes of root user
           (~300 on my laptop; ~360 on my VM).

Results:
machine   configuration   (dummy)       -n 100     -e '{cs,cycles}'      -u 0
laptop    perf/core     980 +- 130   10514 +- 313   31950 +- 526    14529 +- 241
laptop    this w/o -j   698 +- 102   11302 +- 283   31807 +- 448    13885 +- 143
laptop    this w/ -j   2233 +- 261    5434 +- 386   13586 +- 443     9465 +- 568
vm        perf/core    9818 +-  94   89993 +- 941        N/A       266414 +-1431
vm        this w/o -j  5245 +-  88   82806 +- 922        N/A       260416 +-1563
vm        this w -j   37787 +- 748   54844 +-1089        N/A       101088 +-1900

Comments:
 - opening one dummy event in single threaded mode is faster than 
   perf/core, probably due to the changes in how evsel cpus are iterated.
 - opening one event in multithreaded mode is not worth it.
 - in all other cases, multithreaded mode is between 1.5x and 2.5x faster.

Time breakdown on my laptop:
One dummy event per cpu:
$  ./perf bench internals evlist-open-close -d
# Running 'internals/evlist-open-close' benchmark:
  Number of workers:    1
  Number of cpus:       4
  Number of threads:    1
  Number of events:     1 (4 fds)
  Number of iterations: 100
  Average open-close took:      879.250 usec (+-      103.238 usec)
                init took:        0.040 usec (+-        0.020 usec)
                open took:       28.900 usec (+-        6.675 usec)
                mmap took:      415.870 usec (+-       17.391 usec)
              enable took:      240.950 usec (+-       92.641 usec)
             disable took:       64.670 usec (+-        9.722 usec)
              munmap took:       16.450 usec (+-        3.571 usec)
               close took:      112.100 usec (+-       25.465 usec)
                fini took:        0.220 usec (+-        0.042 usec)
$ ./perf bench internals evlist-open-close -j -d
# Running 'internals/evlist-open-close' benchmark:
  Number of workers:    4
  Number of cpus:       4
  Number of threads:    1
  Number of events:     1 (4 fds)
  Number of iterations: 100
  Average open-close took:     1979.670 usec (+-      271.772 usec)
                init took:       30.860 usec (+-        1.190 usec)
                open took:      552.040 usec (+-       96.166 usec)
                mmap took:      428.970 usec (+-        9.492 usec)
              enable took:      222.740 usec (+-       56.112 usec)
             disable took:      191.990 usec (+-       55.029 usec)
              munmap took:       13.670 usec (+-        0.754 usec)
               close took:      155.660 usec (+-       44.079 usec)
                fini took:      383.520 usec (+-       87.476 usec)

Comments: 
 - the overhead comes from open (spinnig up threads) and fini 
   (terminating threads). There could be some improvements there (e.g.
   not waiting for the thread to spawn).
 - enable, disable, and close also take longer due to the overhead of
   assigning the tasks to the workers.

One dummy event per process per cpu:
$ ./perf bench internals evlist-open-close -d -u0
# Running 'internals/evlist-open-close' benchmark:
  Number of workers:    1
  Number of cpus:       4
  Number of threads:    295
  Number of events:     1 (1180 fds)
  Number of iterations: 100
  Average open-close took:    15101.380 usec (+-      247.959 usec)
                init took:        0.010 usec (+-        0.010 usec)
                open took:     4224.460 usec (+-      119.028 usec)
                mmap took:     3235.210 usec (+-       55.867 usec)
              enable took:     2359.570 usec (+-       44.923 usec)
             disable took:     2321.100 usec (+-       80.779 usec)
              munmap took:      304.440 usec (+-       11.558 usec)
               close took:     2655.920 usec (+-       59.089 usec)
                fini took:        0.380 usec (+-        0.051 usec)
$ ./perf bench internals evlist-open-close -j -d -u0
# Running 'internals/evlist-open-close' benchmark:
  Number of workers:    4
  Number of cpus:       4
  Number of threads:    298
  Number of events:     1 (1192 fds)
  Number of iterations: 100
  Average open-close took:    10321.060 usec (+-      771.875 usec)
                init took:       31.530 usec (+-        0.721 usec)
                open took:     2849.870 usec (+-      533.019 usec)
                mmap took:     3267.810 usec (+-       87.465 usec)
              enable took:     1041.160 usec (+-       66.324 usec)
             disable took:     1176.970 usec (+-      134.291 usec)
              munmap took:      253.680 usec (+-        4.525 usec)
               close took:     1204.550 usec (+-      101.284 usec)
                fini took:      495.260 usec (+-      136.661 usec)
Comments:
 - mmap/munmap are not parallelized and account for 20% of the time.
 - open time is reduced by 33% (due to overhead of spinning up threads,
   which is around half ms).
 - enable, disable, and close times are halved.

It is not always worth using multithreading in evlist operations, but
in the good cases the improvements can be significant.
For this reason, we could include an heuristic to decide whether to
use it or not (e.g. use it only if there are less than X event fds to open).

It'd be great to see more experiments like these on a bigger machine, and
on a more realistic scenario (instead of dummy events).

Thanks,
Riccardo

Riccardo Mancini (37):
  libperf cpumap: improve idx function
  libperf cpumap: improve max function
  perf evlist: replace evsel__cpu_iter* functions with evsel__find_cpu
  perf util: add mmap_cpu_mask__duplicate function
  perf util/mmap: add missing bitops.h header
  perf workqueue: add affinities to threadpool
  perf workqueue: add support for setting affinities to workers
  perf workqueue: add method to execute work on specific CPU
  perf python: add workqueue dependency
  perf evlist: add multithreading helper
  perf evlist: add multithreading to evlist__disable
  perf evlist: add multithreading to evlist__enable
  perf evlist: add multithreading to evlist__close
  perf evsel: remove retry_sample_id goto label
  perf evsel: separate open preparation from open itself
  perf evsel: save open flags in evsel
  perf evsel: separate missing feature disabling from evsel__open_cpu
  perf evsel: add evsel__prepare_open function
  perf evsel: separate missing feature detection from evsel__open_cpu
  perf evsel: separate rlimit increase from evsel__open_cpu
  perf evsel: move ignore_missing_thread to fallback code
  perf evsel: move test_attr__open to success path in evsel__open_cpu
  perf evsel: move bpf_counter__install_pe to success path in
    evsel__open_cpu
  perf evsel: handle precise_ip fallback in evsel__open_cpu
  perf evsel: move event open in evsel__open_cpu to separate function
  perf evsel: add evsel__open_per_cpu_no_fallback function
  perf evlist: add evlist__for_each_entry_from macro
  perf evlist: add multithreading to evlist__open
  perf evlist: add custom fallback to evlist__open
  perf record: use evlist__open_custom
  tools lib/subcmd: add OPT_UINTEGER_OPTARG option type
  perf record: add --threads option
  perf record: pin threads to monitored cpus if enough threads available
  perf record: apply multithreading in init and fini phases
  perf test/evlist-open-close: add multithreading
  perf test/evlist-open-close: use inline func to convert timeval to
    usec
  perf test/evlist-open-close: add detailed output mode

 tools/lib/perf/cpumap.c                  |  27 +-
 tools/lib/subcmd/parse-options.h         |   1 +
 tools/perf/Documentation/perf-record.txt |   9 +
 tools/perf/bench/evlist-open-close.c     | 116 +++++-
 tools/perf/builtin-record.c              | 128 ++++--
 tools/perf/builtin-stat.c                |  24 +-
 tools/perf/tests/workqueue.c             |  87 ++++-
 tools/perf/util/evlist.c                 | 464 +++++++++++++++++-----
 tools/perf/util/evlist.h                 |  44 ++-
 tools/perf/util/evsel.c                  | 473 ++++++++++++++---------
 tools/perf/util/evsel.h                  |  36 +-
 tools/perf/util/mmap.c                   |  12 +
 tools/perf/util/mmap.h                   |   4 +
 tools/perf/util/python-ext-sources       |   2 +
 tools/perf/util/record.h                 |   3 +
 tools/perf/util/workqueue/threadpool.c   |  70 ++++
 tools/perf/util/workqueue/threadpool.h   |   7 +
 tools/perf/util/workqueue/workqueue.c    | 154 +++++++-
 tools/perf/util/workqueue/workqueue.h    |  18 +
 19 files changed, 1334 insertions(+), 345 deletions(-)

-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 5F8CDC4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:51 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 442B261206
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:51 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233240AbhHUJU3 (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:29 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34018 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233478AbhHUJU2 (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:28 -0400
Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B071C061575;
        Sat, 21 Aug 2021 02:19:49 -0700 (PDT)
Received: by mail-ed1-x535.google.com with SMTP id q3so17660657edt.5;
        Sat, 21 Aug 2021 02:19:49 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=GDP1MLL1Qg0dnIIFehlblVPtQewpoe7l+lrwKCe2/Qk=;
        b=gvuvUP4K8VAsRQXs8iaxCIOLZ2hgDYsfWfsFAsaNSwAEYkWQ/cbp6CmgbOKpmL2ba0
         qfXF3SdKmYWLx0LYICMPcwDD7p1QXq+WqYJnKFZmnj3p/sj2Deb9OsDClZsi1Gr+isrj
         VFkDg0lJowsuvRFG6vFfTJcAOd23umQ2YDWQ1rMDIplTmMigt0ldWgftZYKQJ5fdZKp8
         na5uCnJtZ/OAkJxRHJpkWEz+2xKafbrqsFpI3unVYP3VKVQAj0pH2rJ94XpCYIeieFdI
         75YM0lqXc0FW9zOA5OWHRzuk6es8IIShY/jqjWi6/qs3Vmk12c5tJAPk4g5h/LTlc9HJ
         09lA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=GDP1MLL1Qg0dnIIFehlblVPtQewpoe7l+lrwKCe2/Qk=;
        b=QuNv9qp/gPwKUrDn7gI+ms0ryFsPEqq5mbVWoOiXAm9MY4g3a7XR1RhccmY0tLszQT
         5j3wMUCflyIGaReVoTHTYZnYPel9VY7j+4nDShxXVeM/M7+rN8D2R0bCLSFLe+khqpf7
         lagQa1c4Kjyqp8SXHIcYndWlncFwC3vDXF+h5ho1dfSaiVQ2Ww1ypfcrBr/X/U1Rks8b
         0s1wlYtRKmVcweNh+AqTsn2n6YOzAs6Mi8CdtyFhh8AgjsHU/FVJBUJ26lBpNkSGUjUz
         UH4/xIBZLmMEqDy8FoYaCV+qGb56+OoBzr28o5eqg7BOk1X+WcaBADai+9bnBt9MepK8
         Kocg==
X-Gm-Message-State: AOAM530TzPWXfDPlXzrsM8dqYJIhhV3WcTJPob+iH9nfIcciufuaoUoY
        q+UOprtaYJHh4jnR9L46lZ8=
X-Google-Smtp-Source: ABdhPJwSs6s3PTZbEpUrjrXyqO3HTocUUwGhf7ReTxWPVyE0PPLIXqAfGHQ9vx3SmqQLDrL2TtgKNQ==
X-Received: by 2002:a05:6402:1694:: with SMTP id a20mr26812801edv.99.1629537587766;
        Sat, 21 Aug 2021 02:19:47 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.46
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:47 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 01/37] libperf cpumap: improve idx function
Date:   Sat, 21 Aug 2021 11:19:07 +0200
Message-Id: <f1543c15797169c21e8b205a4a6751159180580d.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

>From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered
and without dups"), perf_cpu_map elements are sorted in ascending order.

This patch improves the perf_cpu_map__idx function by using a binary
search.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/lib/perf/cpumap.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
index ca0215047c326af4..fb633272be3aaed9 100644
--- a/tools/lib/perf/cpumap.c
+++ b/tools/lib/perf/cpumap.c
@@ -265,11 +265,18 @@ bool perf_cpu_map__empty(const struct perf_cpu_map *map)
 
 int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
 {
-	int i;
+	int low = 0, high = cpus->nr, idx, cpu_at_idx;
 
-	for (i = 0; i < cpus->nr; ++i) {
-		if (cpus->map[i] == cpu)
-			return i;
+	while (low < high) {
+		idx = (low + high) / 2;
+		cpu_at_idx = cpus->map[idx];
+
+		if (cpu_at_idx == cpu)
+			return idx;
+		else if (cpu_at_idx > cpu)
+			high = idx;
+		else
+			low = idx+1;
 	}
 
 	return -1;
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id AAD48C4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:56 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 8C81961156
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:56 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233597AbhHUJUb (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:31 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34022 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233524AbhHUJUa (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:30 -0400
Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A63EEC061575;
        Sat, 21 Aug 2021 02:19:50 -0700 (PDT)
Received: by mail-ej1-x62e.google.com with SMTP id mf2so6671166ejb.9;
        Sat, 21 Aug 2021 02:19:50 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=mbPZDp/xkJGnPgN5FrjhXWQn3CwvReLDj7zmHU7fpVM=;
        b=Q4cdAiRLpALsYo/BGM1e4/Df3zq8rBBfAO6wg/DJ7kxr5ekEoVLYHJHMt4ye1YZzw9
         50RWVjwqc86ZX2sWywYTM4z8YL8egBA5ZkfA0u88cittsY0xVyrdlcEwDMxmV49JEuJR
         Qnb17XXmligG5zNmGsEf/Kn7bhTnhASE7QnRiWgBW+3G06cI6p7Tpyb9SwZ4DCQOjuk/
         cxdtXV/gsgBQnc3fOGty4aP8QPmL7fBR2daZ3OzX5JITg1eOROUBgPy6L8wlQ98Ys5uD
         xjas/+qU8KloLKPJhGacXcVQVzG9U5YyuegS+005sigqFUGEB73OdcoenbSAW3jHtbe9
         2N5Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=mbPZDp/xkJGnPgN5FrjhXWQn3CwvReLDj7zmHU7fpVM=;
        b=OebV9gypuPZsUfJfBvLWa6GZqZRTHKosPnT3Z1nTDg1hzZ4Uh9JIVpfQutBZwb3KLJ
         FQR2bypNLS2Ed/yodBLJz9LopZDXokRBlEva0KRCpkibO9UZi2P83lRAClIYOrfWWqw9
         c6rqMUa+AQPRQx1w8TlnteVHTFSZCZftpH4UZH6jvqVOZbO9roExV9Lh9nMD3jO53Eh1
         TMBkZmgxMtMM4oQ4GdRYu9OqvqnKu6F8uDgr7EpoGb2SjpdkuxG5GOQ7QsLYs2JLCdER
         /FupAVB9RUFap3IYginjBrf9RJU6HJOfnI0ZtKizXKlJM3VgoJHuvSzKeAoxKIVx96pK
         GKog==
X-Gm-Message-State: AOAM530xwLsxhiaEU5wNJmeu6G/IH60nSL00LTqvNw8jQw8zZfEt7UkW
        kchy86JeHGTM4hYfV+7C79U=
X-Google-Smtp-Source: ABdhPJwnXCQolY4RrQa1SsJmvAPCgz0c5ncMp/XmzMtNZc2mg1+lNkceGOFyikTiuyHLZLUkYTQ1sA==
X-Received: by 2002:a17:906:3b54:: with SMTP id h20mr22119315ejf.436.1629537589196;
        Sat, 21 Aug 2021 02:19:49 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.47
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:48 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 02/37] libperf cpumap: improve max function
Date:   Sat, 21 Aug 2021 11:19:08 +0200
Message-Id: <fb79f02e7b86ea8044d563adb1e9890c906f982f.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

>From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered and
without dups"), perf_cpu_map elements are sorted in ascending order.

This patch improves the perf_cpu_map__max function by returning the last
element.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/lib/perf/cpumap.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
index fb633272be3aaed9..80e03b2f0c60cce7 100644
--- a/tools/lib/perf/cpumap.c
+++ b/tools/lib/perf/cpumap.c
@@ -284,14 +284,10 @@ int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
 
 int perf_cpu_map__max(struct perf_cpu_map *map)
 {
-	int i, max = -1;
-
-	for (i = 0; i < map->nr; i++) {
-		if (map->map[i] > max)
-			max = map->map[i];
-	}
-
-	return max;
+	if (map->nr > 0)
+		return map->map[map->nr-1];
+	else
+		return -1;
 }
 
 /*
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id CB768C4320E
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:56 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id B1F9461156
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:56 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233524AbhHUJUd (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:33 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34036 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233478AbhHUJUb (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:31 -0400
Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30C8CC061575;
        Sat, 21 Aug 2021 02:19:52 -0700 (PDT)
Received: by mail-ej1-x62d.google.com with SMTP id gr13so25223178ejb.6;
        Sat, 21 Aug 2021 02:19:52 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=BbrLjcxucq8UgbwP3JKE4YkklXE9jxKK4CeYaVITJ/4=;
        b=tdt/+7Rm7hYfFYB0kQ3E87nZQrVKKTWICbxWGUjCT4AEn9gaBCylm+b7HFhDpuFA86
         LPgUZ2DOZiorpbodNh8d2ByS5uj7Tw4ijxs/OT2NQ+IVjsgbzhxK3QsjioU9GECFeQAu
         XoD3FIgHtU0LlD++FlAcO5b+nGg79NuNCTLjINBgN/1q8kOLOrQa3Ztml66CC6TN9E2i
         nznKy/TXMHpO1QePEgrARHueB7Jd/eQcaUYD9Vvd/0zQZL8JguymK1LQWOp8xJZNe4gQ
         lf4EjJJr063WGApzcf5LkFrQI5AC9vQSq5UyAy03TUkNwsPb6mhJJmVSoDrjBtnoiIDZ
         MhsQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=BbrLjcxucq8UgbwP3JKE4YkklXE9jxKK4CeYaVITJ/4=;
        b=CF2/wkmRzWyM+GcfQhJkvB/aw4jkpIpMTTQ3mdAOCjfTmCCPZWk4bzRTbo8o8DmgB8
         FWPoMmmSMwSZV+zLcnquAwtpmUWmohJrpmoSNX2ARL7xUGY5/TGJ82dtzFF4UuBiholJ
         APF4RMDS58Q0FG6QRib4NPLyPqPT2uHGtpllQJ6TG/VPuNmN82ZtCksV4a7kpCFeM6T6
         6gzk/l+XWkDj3HVCC3YL3WXplfNGebIArhwMtU5vIVRGraGejSihz8z1lB8vFxIq0Lhr
         jz3m2wOYpJ6g2FraqjM6pUo28rCQKxbzynStAiB0g6SKxM/pYLWN3z1xR15kBpkcQ/0+
         cKWQ==
X-Gm-Message-State: AOAM533lWtDzQNvqfDT6CNjTHkYe3vloAGO9nMSHugR7wbbnFefMw+FN
        MLwSe74cSh80vTHmdizcF8q7/tOjRJUsd89o1JA=
X-Google-Smtp-Source: ABdhPJyztVfLHDdgCbq2BBtskFQRRN0DJlhA1ZSs3RRshobNarv529xnoCHLWvw/vHMOueKN4FbUwA==
X-Received: by 2002:a17:907:b09:: with SMTP id h9mr26199233ejl.278.1629537590632;
        Sat, 21 Aug 2021 02:19:50 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.49
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:50 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>,
        Andi Kleen <ak@linux.intel.com>
Subject: [RFC PATCH v1 03/37] perf evlist: replace evsel__cpu_iter* functions with evsel__find_cpu
Date:   Sat, 21 Aug 2021 11:19:09 +0200
Message-Id: <5bfe13dd0985611285bb697987816ddc36e93e76.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Commit a8cbe40fe9f4ba49 ("perf evsel: Add iterator to iterate over events
ordered by CPU") introduced an iterator for evsel.core.cpus inside evsel
which is used to iterate over evlist one CPU at a time.

However, this solution is hacky since it involves a mutable state in the
evsel which is supposed to be unmodified during iteration.
Sice checking that a CPU is within the evsel can be done quickly in
O(logn) time, this patch replaces the aforementioned iterator with a
simple check.

Cc: Andi Kleen <ak@linux.intel.com>
Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/builtin-stat.c | 24 +++++++++--------
 tools/perf/util/evlist.c  | 54 +++++++++++----------------------------
 tools/perf/util/evlist.h  |  5 +---
 tools/perf/util/evsel.h   |  1 -
 4 files changed, 30 insertions(+), 54 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 84de61795e67bbb9..62dcc001c8f0a78b 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -407,7 +407,7 @@ static int read_affinity_counters(struct timespec *rs)
 {
 	struct evsel *counter;
 	struct affinity affinity;
-	int i, ncpus, cpu;
+	int i, ncpus, cpu, cpu_idx;
 
 	if (all_counters_use_bpf)
 		return 0;
@@ -424,13 +424,14 @@ static int read_affinity_counters(struct timespec *rs)
 		affinity__set(&affinity, cpu);
 
 		evlist__for_each_entry(evsel_list, counter) {
-			if (evsel__cpu_iter_skip(counter, cpu))
+			cpu_idx = evsel__find_cpu(counter, cpu);
+			if (cpu_idx < 0)
 				continue;
 			if (evsel__is_bpf(counter))
 				continue;
 			if (!counter->err) {
 				counter->err = read_counter_cpu(counter, rs,
-								counter->cpu_iter - 1);
+								cpu_idx);
 			}
 		}
 	}
@@ -789,7 +790,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 	const bool forks = (argc > 0);
 	bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
 	struct affinity affinity;
-	int i, cpu, err;
+	int i, cpu, cpu_idx, err;
 	bool second_pass = false;
 
 	if (forks) {
@@ -823,7 +824,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 		affinity__set(&affinity, cpu);
 
 		evlist__for_each_entry(evsel_list, counter) {
-			if (evsel__cpu_iter_skip(counter, cpu))
+			cpu_idx = evsel__find_cpu(counter, cpu);
+			if (cpu_idx < 0)
 				continue;
 			if (counter->reset_group || counter->errored)
 				continue;
@@ -831,7 +833,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 				continue;
 try_again:
 			if (create_perf_stat_counter(counter, &stat_config, &target,
-						     counter->cpu_iter - 1) < 0) {
+						     cpu_idx) < 0) {
 
 				/*
 				 * Weak group failed. We cannot just undo this here
@@ -877,22 +879,24 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
 			evlist__for_each_entry(evsel_list, counter) {
 				if (!counter->reset_group && !counter->errored)
 					continue;
-				if (evsel__cpu_iter_skip_no_inc(counter, cpu))
+				cpu_idx = evsel__find_cpu(counter, cpu);
+				if (cpu_idx < 0)
 					continue;
-				perf_evsel__close_cpu(&counter->core, counter->cpu_iter);
+				perf_evsel__close_cpu(&counter->core, cpu_idx);
 			}
 			/* Now reopen weak */
 			evlist__for_each_entry(evsel_list, counter) {
 				if (!counter->reset_group && !counter->errored)
 					continue;
-				if (evsel__cpu_iter_skip(counter, cpu))
+				cpu_idx = evsel__find_cpu(counter, cpu);
+				if (cpu_idx < 0)
 					continue;
 				if (!counter->reset_group)
 					continue;
 try_again_reset:
 				pr_debug2("reopening weak %s\n", evsel__name(counter));
 				if (create_perf_stat_counter(counter, &stat_config, &target,
-							     counter->cpu_iter - 1) < 0) {
+							     cpu_idx) < 0) {
 
 					switch (stat_handle_error(counter)) {
 					case COUNTER_FATAL:
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 47581a237c7a7848..3d55d9a53b9f4875 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -341,36 +341,9 @@ static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
 		return perf_thread_map__nr(evlist->core.threads);
 }
 
-void evlist__cpu_iter_start(struct evlist *evlist)
+int evsel__find_cpu(struct evsel *ev, int cpu)
 {
-	struct evsel *pos;
-
-	/*
-	 * Reset the per evsel cpu_iter. This is needed because
-	 * each evsel's cpumap may have a different index space,
-	 * and some operations need the index to modify
-	 * the FD xyarray (e.g. open, close)
-	 */
-	evlist__for_each_entry(evlist, pos)
-		pos->cpu_iter = 0;
-}
-
-bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
-{
-	if (ev->cpu_iter >= ev->core.cpus->nr)
-		return true;
-	if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
-		return true;
-	return false;
-}
-
-bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
-{
-	if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
-		ev->cpu_iter++;
-		return false;
-	}
-	return true;
+	return perf_cpu_map__idx(ev->core.cpus, cpu);
 }
 
 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
@@ -400,7 +373,7 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
 {
 	struct evsel *pos;
 	struct affinity affinity;
-	int cpu, i, imm = 0;
+	int cpu, i, imm = 0, cpu_idx;
 	bool has_imm = false;
 
 	if (affinity__setup(&affinity) < 0)
@@ -414,7 +387,8 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
 			evlist__for_each_entry(evlist, pos) {
 				if (evsel__strcmp(pos, evsel_name))
 					continue;
-				if (evsel__cpu_iter_skip(pos, cpu))
+				cpu_idx = evsel__find_cpu(pos, cpu);
+				if (cpu_idx < 0)
 					continue;
 				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
 					continue;
@@ -422,7 +396,7 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
 					has_imm = true;
 				if (pos->immediate != imm)
 					continue;
-				evsel__disable_cpu(pos, pos->cpu_iter - 1);
+				evsel__disable_cpu(pos, cpu_idx);
 			}
 		}
 		if (!has_imm)
@@ -462,7 +436,7 @@ static void __evlist__enable(struct evlist *evlist, char *evsel_name)
 {
 	struct evsel *pos;
 	struct affinity affinity;
-	int cpu, i;
+	int cpu, i, cpu_idx;
 
 	if (affinity__setup(&affinity) < 0)
 		return;
@@ -473,11 +447,12 @@ static void __evlist__enable(struct evlist *evlist, char *evsel_name)
 		evlist__for_each_entry(evlist, pos) {
 			if (evsel__strcmp(pos, evsel_name))
 				continue;
-			if (evsel__cpu_iter_skip(pos, cpu))
+			cpu_idx = evsel__find_cpu(pos, cpu);
+			if (cpu_idx < 0)
 				continue;
 			if (!evsel__is_group_leader(pos) || !pos->core.fd)
 				continue;
-			evsel__enable_cpu(pos, pos->cpu_iter - 1);
+			evsel__enable_cpu(pos, cpu_idx);
 		}
 	}
 	affinity__cleanup(&affinity);
@@ -1264,7 +1239,7 @@ void evlist__close(struct evlist *evlist)
 {
 	struct evsel *evsel;
 	struct affinity affinity;
-	int cpu, i;
+	int cpu, i, cpu_idx;
 
 	/*
 	 * With perf record core.cpus is usually NULL.
@@ -1282,9 +1257,10 @@ void evlist__close(struct evlist *evlist)
 		affinity__set(&affinity, cpu);
 
 		evlist__for_each_entry_reverse(evlist, evsel) {
-			if (evsel__cpu_iter_skip(evsel, cpu))
-			    continue;
-			perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
+			cpu_idx = evsel__find_cpu(evsel, cpu);
+			if (cpu_idx < 0)
+				continue;
+			perf_evsel__close_cpu(&evsel->core, cpu_idx);
 		}
 	}
 	affinity__cleanup(&affinity);
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 5c22383489ae4905..fde893170c7ba6d2 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -310,15 +310,12 @@ void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
 	__evlist__for_each_entry_safe(&(evlist)->core.entries, tmp, evsel)
 
 #define evlist__for_each_cpu(evlist, index, cpu)	\
-	evlist__cpu_iter_start(evlist);			\
 	perf_cpu_map__for_each_cpu (cpu, index, (evlist)->core.all_cpus)
 
 struct evsel *evlist__get_tracking_event(struct evlist *evlist);
 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel);
 
-void evlist__cpu_iter_start(struct evlist *evlist);
-bool evsel__cpu_iter_skip(struct evsel *ev, int cpu);
-bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu);
+int evsel__find_cpu(struct evsel *ev, int cpu);
 
 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
 
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 80383096d51c5f00..eabccce406886320 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -119,7 +119,6 @@ struct evsel {
 	bool			errored;
 	struct hashmap		*per_pkg_mask;
 	int			err;
-	int			cpu_iter;
 	struct {
 		evsel__sb_cb_t	*cb;
 		void		*data;
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 22572C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:00 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 02FAD61156
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:19:59 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233827AbhHUJUf (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:35 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34044 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233736AbhHUJUc (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:32 -0400
Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 648ABC061575;
        Sat, 21 Aug 2021 02:19:53 -0700 (PDT)
Received: by mail-ed1-x52a.google.com with SMTP id dj8so17662535edb.2;
        Sat, 21 Aug 2021 02:19:53 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=N+nB96fRRhC53bAnF/MrlsVfe1VX/CDYmRy1kac/YlM=;
        b=UeWjhOKTJ6BHy7tzxslYFw7fz+/yWLcuxUdQmQimsGL+iLQ3RVtyUdWpUmXVZ9gt+s
         2QYwCffKwklia9A73rJMbGdpvH/gImOT3KNGnuVQ1VozjMdXrzVGH1/2DpNhf+VGeoju
         XeFWkjXjvud6NqzjKlHa4OiS3oCYbjTPLWm5MXbfDuxV2oTT4O2sNcvLjBcoy3CmY1uX
         +bYnvjwJkZf5/O3H8XA4avMZ9tjlOO3cI/awoviknHU8/KYlr1AlJJqO7QCWslzTrJyB
         5fT9ueXPCwSgr4knh/t+dLdnISxSwDnsVnhB3neq+hXd0OgZFfCeGJ2d1+L/BoJVO+iV
         6pQw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=N+nB96fRRhC53bAnF/MrlsVfe1VX/CDYmRy1kac/YlM=;
        b=CKJmyCAFlOUqdE53+eUxuXncCokX68KLFAyUYfu2eiZEd5bK9G/kaXZfJPb5sObvCj
         cCYgEq7hcIjxXMMWGZP4QU4mTjB+grgwrF7an4gfI07iSQ/iA2E5ZxPrwQkuHHLmVGU0
         8iAr+tYJf1lLI0NAfDwaG9E0+RqROx5GKFsVcLLe0OhVWOVtjh4fulcZKAQvGKVpr4bn
         rf+ceTdY+9VcE3qdgJ1ccknUpv6SWkEFfrM9Lg6wQnR/gXjLqfm/F0cHHBlrhMjScrwl
         3nhiPPi+oVxYMJQuxr37xCm820UNv/E9ADg4RsZbnXGxOVuWaxac3qE4wUz7qR7CuRea
         aY1w==
X-Gm-Message-State: AOAM533ZLzv7IoyT38Ov82Iq/U27k4ASQxxlgFuampheiYUV3vmFM3bR
        /mPQAkIoEhwL/vrtSeKmIY0=
X-Google-Smtp-Source: ABdhPJxp2/wUJQzfbLz6rIo+2jvkaIAe6L9SpBwIrcWcrvhvRFPIylffTp+VR6GlhdvMSGxjllpihw==
X-Received: by 2002:a05:6402:7ce:: with SMTP id u14mr26542422edy.61.1629537591966;
        Sat, 21 Aug 2021 02:19:51 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.50
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:51 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 04/37] perf util: add mmap_cpu_mask__duplicate function
Date:   Sat, 21 Aug 2021 11:19:10 +0200
Message-Id: <8943a548ef7a3dd3e015095afad7e9a8b2154c05.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds a new function in util/mmap.c to duplicate a mmap_cpu_mask.

This new function will be used in the following patches.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/mmap.c | 12 ++++++++++++
 tools/perf/util/mmap.h |  3 +++
 2 files changed, 15 insertions(+)

diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c
index ab7108d22428b527..9e9a447682cc962c 100644
--- a/tools/perf/util/mmap.c
+++ b/tools/perf/util/mmap.c
@@ -350,3 +350,15 @@ int perf_mmap__push(struct mmap *md, void *to,
 out:
 	return rc;
 }
+
+int mmap_cpu_mask__duplicate(struct mmap_cpu_mask *original,
+				struct mmap_cpu_mask *clone)
+{
+	clone->nbits = original->nbits;
+	clone->bits = bitmap_alloc(original->nbits);
+	if (!clone->bits)
+		return -ENOMEM;
+
+	memcpy(clone->bits, original->bits, MMAP_CPU_MASK_BYTES(original));
+	return 0;
+}
diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h
index 9d5f589f02ae70e1..b4923e587fd7749c 100644
--- a/tools/perf/util/mmap.h
+++ b/tools/perf/util/mmap.h
@@ -63,4 +63,7 @@ size_t mmap__mmap_len(struct mmap *map);
 
 void mmap_cpu_mask__scnprintf(struct mmap_cpu_mask *mask, const char *tag);
 
+int mmap_cpu_mask__duplicate(struct mmap_cpu_mask *original,
+				struct mmap_cpu_mask *clone);
+
 #endif /*__PERF_MMAP_H */
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 0993FC4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:01 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id E5CF061156
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:00 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233856AbhHUJUg (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:36 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34054 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233806AbhHUJUe (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:34 -0400
Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A136C061575;
        Sat, 21 Aug 2021 02:19:55 -0700 (PDT)
Received: by mail-ed1-x530.google.com with SMTP id s25so4932649edw.0;
        Sat, 21 Aug 2021 02:19:55 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=ztxai8Rgcye1ZeVdGSiAJDplMB1xLc+6qAy/h0sNGyQ=;
        b=ry5UiqSSViLUEQhMPbeM21Qc07fR3RQphsgTnszjsYqIV8K8CHVbWMCmRAed5yqJIC
         R8QCFtOvUeuMDhaUeL5NbpnKQ1GbJ9Yn9IWnm77rw/+4zoUegax9qCvCwTISmjwQrf+8
         BC0QyYoqKNjE7YBoMPKBf2w8hlLJvZXJIqc5zG5Kgf/9INffANI5klEzpR/BdUMZfNpR
         fWvU7qzSdnRQ7JIjCukRzKYjpaLr/EF5Y076ZjJpp+LuZv3K60dvFBjO+QJxkqYTo9Wr
         Wmwh2ndTpwoDjPeS4LZguWSzvilEm9n7Cj++Lav4Gw25HAxkA+wIiC3Xq2TxbM7P+9B7
         7W+A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=ztxai8Rgcye1ZeVdGSiAJDplMB1xLc+6qAy/h0sNGyQ=;
        b=cThlETs7AiyBvu/Yut0v2pf+kjtG3CinaXIG6bOHIYqPGLzZcJvyeNmy3vnFzW/kFH
         MrYazrEdRZOq+ZjcM1xSkUflAoYZlgjCqSGFyheFHSoLDWDpvlH2HsK0Vw2pweKxc2gj
         1O0EnuE+6TyVPjzJt5vZQ4e2a5nuTuU2uhmyfP8iyefyGXxpTXjwytVgw78duKlDKTTu
         WXdw5ww+nv4R1j0ma/faucW0oywg3eCjKelVWz3UptP7VBp7of+TNZOY0hGGs8mQDJ9J
         neflLmX0SK1doXV/QFZNZ2dR1Xr1693gAeKHdPnY1eYaV1DwDNE0YMph1bTzRfAB6RkT
         lI8g==
X-Gm-Message-State: AOAM533KIBOaGQIc3OiOTMHb2erQKBJXPxbq1MyP0O1HPoWkW4LiyGzn
        z4wHyIJJTxt/XV/OUn3RKlE=
X-Google-Smtp-Source: ABdhPJy9UE3YTgazmcyHnwa2Vj5VYh2+1ThgjYgmGDAk1K9iCbZc1ZpR9/3NBra+XcGOcl2CTc4Cpw==
X-Received: by 2002:a05:6402:220f:: with SMTP id cq15mr5376364edb.142.1629537593755;
        Sat, 21 Aug 2021 02:19:53 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.52
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:53 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 05/37] perf util/mmap: add missing bitops.h header
Date:   Sat, 21 Aug 2021 11:19:11 +0200
Message-Id: <c5b91ee432a2e28e7f16337c740b43b4d0b0e86c.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

MMAP_CPU_MASK_BYTES uses the BITS_TO_LONGS macro, which is defined in
linux/bitops.h.
However, this header is not included directly, but gets imported
indirectly in files using the macro.
This patch adds the missing include.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/mmap.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h
index b4923e587fd7749c..8e259b9610f83c96 100644
--- a/tools/perf/util/mmap.h
+++ b/tools/perf/util/mmap.h
@@ -6,6 +6,7 @@
 #include <linux/refcount.h>
 #include <linux/types.h>
 #include <linux/ring_buffer.h>
+#include <linux/bitops.h>
 #include <stdbool.h>
 #include <pthread.h> // for cpu_set_t
 #ifdef HAVE_AIO_SUPPORT
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 4B1C9C4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:05 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 332DE61156
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:05 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233955AbhHUJUl (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:41 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34068 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233851AbhHUJUg (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:36 -0400
Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A690CC06175F;
        Sat, 21 Aug 2021 02:19:56 -0700 (PDT)
Received: by mail-ed1-x533.google.com with SMTP id s25so4932712edw.0;
        Sat, 21 Aug 2021 02:19:56 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=UPweOBnT2woI7Uc+uUSBbPPvUccUzrnaZjXAycOrk2c=;
        b=c5h/KerRO8qgqBGB1q6+xGs+jr1EsKylgxTYMDYY0tQU46xKJ4A1Wdr+ryZk4khWBW
         T/QiqhIU4JBfxa0wIXK+hTSubr4tJjAGxxMNm064v9tfOqU3Ezgye7M0V3vcSEBR9K4i
         Rnz/7kvxev+MF7Xd85rOQC59mtOIKs21+Kl3BCKbCMZX15zg2LWmm3sqwjbyle9SBTxu
         iSnmgyWJa0ATuCAmJxgI2fME6RshD2jUh3VtNECWFax0Ki60RoFI3mbc4koWSJ7CWhhm
         v2W8X0dzn1Li0pCZmp8KYtoNNkieC/JJaaw1brbEgO2WzH05s99iLLEob6xCOMkaOeeU
         Q/LA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=UPweOBnT2woI7Uc+uUSBbPPvUccUzrnaZjXAycOrk2c=;
        b=G6VQf60Xe4wbbT08p3JtkeeFrq7rCDk+PKxsHt6iuoM5ueX0kRiZZ1zyNmZXDinq2P
         dMSuW9LH+tWoGA1qJPFC/gYw3AoJZ//4mzsHHkkr/W5f6M0a8/E/O981ZhhaO21GQ9oO
         nvXCWBxH2O+g3EUfaH3dXD/r5ygrknY7nhIvNBghzRQAW4xNOoRPhzDDABfMwFd85ZLF
         c3tdxdgBAvIFcqYXY8e2vcH50GJihkPVDskcE2h923Qk6KYyUeujOYNFf7d+XsdMmfBx
         qt3Uf05TNguAJTeUr+MI0x4bBNQhBJuSY6TDle/PWXDoIwF+Qe3KgBlCuD9Rl2f0SggC
         h71A==
X-Gm-Message-State: AOAM532oFnn0DV9QAOtfNMclZ5jJFDliTHGxa+RXm4zHXF1etId5Nylo
        THPWhXyJdzJkkgz9nQZeqZ4=
X-Google-Smtp-Source: ABdhPJzbq3JVrJCSs4Or1NUPXCNyQpTloBLqI53+S+iY5qYs58EXSrhZssmRgw4Fal/BvTmXIRvSgg==
X-Received: by 2002:a05:6402:1011:: with SMTP id c17mr26253123edu.144.1629537595194;
        Sat, 21 Aug 2021 02:19:55 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.53
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:54 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 06/37] perf workqueue: add affinities to threadpool
Date:   Sat, 21 Aug 2021 11:19:12 +0200
Message-Id: <f2994f2e63e9b825a8fc483d7795bbdea02eef15.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds the possibility to set affinities to the threads in the
threadpool.
An usage of the new functions is added to the workqueue test.

This patch concludes the patches regarding the threadpool.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/tests/workqueue.c           | 87 +++++++++++++++++++++++---
 tools/perf/util/workqueue/threadpool.c | 70 +++++++++++++++++++++
 tools/perf/util/workqueue/threadpool.h |  7 +++
 3 files changed, 157 insertions(+), 7 deletions(-)

diff --git a/tools/perf/tests/workqueue.c b/tools/perf/tests/workqueue.c
index 4eb14a75b6c0a3aa..738b56ec3004f742 100644
--- a/tools/perf/tests/workqueue.c
+++ b/tools/perf/tests/workqueue.c
@@ -4,6 +4,8 @@
 #include <linux/kernel.h>
 #include <linux/err.h>
 #include <linux/zalloc.h>
+#include <linux/bitmap.h>
+#include <perf/cpumap.h>
 #include "tests.h"
 #include "util/debug.h"
 #include "util/workqueue/threadpool.h"
@@ -31,6 +33,7 @@ struct test_task {
 	struct task_struct task;
 	int n_threads;
 	int *array;
+	struct mmap_cpu_mask *affinity_masks;
 };
 
 /**
@@ -54,9 +57,36 @@ static void dummy_work(int idx)
 static void test_task_fn1(int tidx, struct task_struct *task)
 {
 	struct test_task *mtask = container_of(task, struct test_task, task);
+	struct mmap_cpu_mask real_affinity_mask, *set_affinity_mask;
+	int ret;
+
+	set_affinity_mask = &mtask->affinity_masks[tidx];
+	real_affinity_mask.nbits = set_affinity_mask->nbits;
+	real_affinity_mask.bits = bitmap_alloc(real_affinity_mask.nbits);
+	if (!real_affinity_mask.bits) {
+		pr_err("ENOMEM in malloc real_affinity_mask.bits\n");
+		goto out;
+	}
+
+	ret = pthread_getaffinity_np(pthread_self(), real_affinity_mask.nbits,
+				(cpu_set_t *)real_affinity_mask.bits);
+	if (ret) {
+		pr_err("Error in pthread_getaffinity_np: %s\n", strerror(ret));
+		goto out;
+	}
+
+	if (!bitmap_equal(real_affinity_mask.bits, set_affinity_mask->bits,
+			real_affinity_mask.nbits)) {
+		pr_err("affinity mismatch!\n");
+		mmap_cpu_mask__scnprintf(set_affinity_mask, "set affinity");
+		mmap_cpu_mask__scnprintf(&real_affinity_mask, "real affinity");
+		goto out;
+	}
 
 	dummy_work(tidx);
 	mtask->array[tidx] = tidx+1;
+out:
+	bitmap_free(real_affinity_mask.bits);
 }
 
 static void test_task_fn2(int tidx, struct task_struct *task)
@@ -116,21 +146,58 @@ static int __test__threadpool(void *_args)
 {
 	struct threadpool_test_args_t *args = _args;
 	struct threadpool *pool;
+	int ret, i, nr_cpus, nr_bits, cpu;
 	struct test_task task;
 	int pool_size = args->pool_size ?: sysconf(_SC_NPROCESSORS_ONLN);
-	int i, ret = __threadpool__prepare(&pool, pool_size);
+	struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
+	struct mmap_cpu_mask *affinity_masks;
 
-	if (ret)
-		goto out;
+	if (!cpumap) {
+		pr_err("ENOMEM in perf_cpu_map__new\n");
+		return TEST_FAIL;
+	}
+
+	nr_cpus = perf_cpu_map__nr(cpumap);
+	nr_bits = BITS_TO_LONGS(nr_cpus) * sizeof(unsigned long);
+
+	affinity_masks = calloc(pool_size, sizeof(*affinity_masks));
+	if (!affinity_masks) {
+		pr_err("ENOMEM in calloc affinity_masks\n");
+		ret = TEST_FAIL;
+		goto out_put_cpumap;
+	}
+
+	for (i = 0; i < pool_size; i++) {
+		affinity_masks[i].nbits = nr_bits;
+		affinity_masks[i].bits = bitmap_alloc(nr_cpus);
+		if (!affinity_masks[i].bits) {
+			ret = TEST_FAIL;
+			goto out_free_affinity_masks;
+		}
+		bitmap_zero(affinity_masks[i].bits, affinity_masks[i].nbits);
+		cpu = perf_cpu_map__cpu(cpumap, i % nr_cpus);
+		test_and_set_bit(cpu, affinity_masks[i].bits);
+	}
 
 	task.task.fn = test_task_fn1;
 	task.n_threads = pool_size;
+	task.affinity_masks = affinity_masks;
 	task.array = calloc(pool_size, sizeof(*task.array));
 	TEST_ASSERT_VAL("calloc failure", task.array);
 
+	ret = __threadpool__prepare(&pool, pool_size);
+	if (ret)
+		goto out_free_tasks;
+
+	ret = threadpool__set_affinities(pool, task.affinity_masks);
+	if (ret) {
+		ret = TEST_FAIL;
+		goto out_free_tasks;
+	}
+
 	ret = __threadpool__exec_wait(pool, &task.task);
 	if (ret)
-		goto out;
+		goto out_free_tasks;
 
 	for (i = 0; i < pool_size; i++)
 		TEST_ASSERT_VAL("failed array check (1)", task.array[i] == i+1);
@@ -139,17 +206,23 @@ static int __test__threadpool(void *_args)
 
 	ret = __threadpool__exec_wait(pool, &task.task);
 	if (ret)
-		goto out;
+		goto out_free_tasks;
 
 	for (i = 0; i < pool_size; i++)
 		TEST_ASSERT_VAL("failed array check (2)", task.array[i] == 2*i);
 
 	ret = __threadpool__teardown(pool);
 	if (ret)
-		goto out;
+		goto out_free_tasks;
 
-out:
+out_free_tasks:
 	free(task.array);
+out_free_affinity_masks:
+	for (i = 0; i < pool_size; i++)
+		bitmap_free(affinity_masks[i].bits);
+	free(affinity_masks);
+out_put_cpumap:
+	perf_cpu_map__put(cpumap);
 	return ret;
 }
 
diff --git a/tools/perf/util/workqueue/threadpool.c b/tools/perf/util/workqueue/threadpool.c
index 44bcbe4fa3d2d026..bf4ebc394b9bc705 100644
--- a/tools/perf/util/workqueue/threadpool.c
+++ b/tools/perf/util/workqueue/threadpool.c
@@ -13,7 +13,9 @@
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <pthread.h>
+#include <linux/bitmap.h>
 #include <internal/lib.h>
+#include "util/mmap.h"
 #include "threadpool.h"
 
 #ifndef HAVE_GETTID
@@ -39,6 +41,7 @@ struct threadpool_entry {
 		int cmd[2];			/* messages to thread (commands) */
 	} pipes;
 	bool				running; /* has this thread been started? */
+	struct mmap_cpu_mask		affinity_mask;
 };
 
 enum threadpool_msg {
@@ -255,6 +258,16 @@ static int threadpool_entry__recv_cmd(struct threadpool_entry *thread,
 	return 0;
 }
 
+/**
+ * threadpool_entry__apply_affinity - apply @thread->affinity
+ */
+static int threadpool_entry__apply_affinity(struct threadpool_entry *thread)
+{
+	return -pthread_setaffinity_np(thread->ptid,
+			MMAP_CPU_MASK_BYTES(&thread->affinity_mask),
+			(cpu_set_t *)(thread->affinity_mask.bits));
+}
+
 /**
  * threadpool_entry__function - function running on thread
  *
@@ -339,6 +352,7 @@ struct threadpool *threadpool__new(int n_threads)
 		pool->threads[t].ptid = 0;
 		pool->threads[t].pool = pool;
 		pool->threads[t].running = false;
+		// affinity is set to zero due to calloc
 		threadpool_entry__init_pipes(&pool->threads[t]);
 	}
 
@@ -414,6 +428,7 @@ void threadpool__delete(struct threadpool *pool)
 	for (t = 0; t < pool->nr_threads; t++) {
 		thread = &pool->threads[t];
 		threadpool_entry__close_pipes(thread);
+		bitmap_free(thread->affinity_mask.bits);
 	}
 
 	zfree(&pool->threads);
@@ -455,6 +470,16 @@ int threadpool__start_thread(struct threadpool *pool, int tidx)
 	pthread_attr_init(&attrs);
 	pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
 
+	if (thread->affinity_mask.bits) {
+		ret = pthread_attr_setaffinity_np(&attrs,
+				MMAP_CPU_MASK_BYTES(&thread->affinity_mask),
+				(cpu_set_t *)(thread->affinity_mask.bits));
+		if (ret) {
+			err = -ret;
+			goto out;
+		}
+	}
+
 	ret = pthread_create(&thread->ptid, &attrs, threadpool_entry__function, thread);
 	if (ret) {
 		err = -ret;
@@ -617,3 +642,48 @@ bool threadpool__is_busy(struct threadpool *pool)
 {
 	return pool->current_task;
 }
+
+/**
+ * threadpool__set_affinity - set @affinity of the @tid thread in @pool
+ *
+ * If threadpool is not running affinity will be set on start.
+ * If threadpool is running, affinity is immediately set.
+ *
+ * This function can be called from any thread.
+ */
+int threadpool__set_affinity(struct threadpool *pool, int tid,
+				struct mmap_cpu_mask *affinity)
+{
+	struct threadpool_entry *thread = &pool->threads[tid];
+	int ret = mmap_cpu_mask__duplicate(affinity, &thread->affinity_mask);
+
+	if (ret)
+		return ret;
+
+	if (thread->running)
+		return threadpool_entry__apply_affinity(thread);
+	else
+		return 0;
+}
+
+/**
+ * threadpool__set_affinities - set @affinities of all threads in @pool
+ *
+ * If threadpool is not running, affinities will be set on start.
+ * If threadpool is running, affinities are immediately set.
+ *
+ * This function can be called from any thread.
+ */
+int threadpool__set_affinities(struct threadpool *pool,
+				struct mmap_cpu_mask *affinities)
+{
+	int i, ret;
+
+	for (i = 0; i < pool->nr_threads; i++) {
+		ret = threadpool__set_affinity(pool, i, &affinities[i]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
diff --git a/tools/perf/util/workqueue/threadpool.h b/tools/perf/util/workqueue/threadpool.h
index 9a6081cef8af95e0..21fbfb57b00e2906 100644
--- a/tools/perf/util/workqueue/threadpool.h
+++ b/tools/perf/util/workqueue/threadpool.h
@@ -2,6 +2,8 @@
 #ifndef __WORKQUEUE_THREADPOOL_H
 #define __WORKQUEUE_THREADPOOL_H
 
+#include "util/mmap.h"
+
 struct threadpool;
 struct task_struct;
 
@@ -25,6 +27,11 @@ extern int threadpool__size(struct threadpool *pool);
 extern bool threadpool__is_running(struct threadpool *pool);
 extern bool threadpool__is_busy(struct threadpool *pool);
 
+extern int threadpool__set_affinities(struct threadpool *pool,
+				struct mmap_cpu_mask *affinities);
+extern int threadpool__set_affinity(struct threadpool *pool, int tid,
+				struct mmap_cpu_mask *affinity);
+
 /* Error management */
 #define THREADPOOL_STRERR_BUFSIZE (128+STRERR_BUFSIZE)
 #define THREADPOOL_ERROR__OFFSET 512
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 1BE79C4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:12 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 0491C61101
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:12 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233249AbhHUJUt (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:49 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34088 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233882AbhHUJUj (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:39 -0400
Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 260A5C0613CF;
        Sat, 21 Aug 2021 02:19:58 -0700 (PDT)
Received: by mail-ej1-x62f.google.com with SMTP id lo4so25140207ejb.7;
        Sat, 21 Aug 2021 02:19:58 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=+U3YOtDAf1VM4NAaGzCL3EE9rWzGI06v0q8Jd39E39o=;
        b=ktuNHDddlmv6ZpVQ6Ty9173/Y6XHmN51si50enIjm1Ed4YGAMA5TuZj47K/myzqHsq
         tcAuUMs9HoEna3lCx95+iKh+zXi0IwjCcP6HDYoA4TbgTbZX7kbSYoU9r2GcC/Zk+k30
         3G7mnYNquwD4KHREHOZSa5xx1HS0C8fj8e/tKiK0E2qJBn0tBvVxHehblAkqOiA3S0ht
         u+Q8D4rT/AtIW7XmnMGBBex03dR3O3DGGs4g/tVj+90ep6DKKSbbi2d8nxO5O3nZ03rU
         NAb2Hu5m1mDXSbd3oq3V2qJ2QMu0zwjZX4lQoStmKbyTYzhRVkFxtvwrOoze+zfRnXQt
         5VnQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=+U3YOtDAf1VM4NAaGzCL3EE9rWzGI06v0q8Jd39E39o=;
        b=NAerN8LjWS/Lu4wf9D7sjphgenE2z6T1fNQWdWylU6c+c7fiDb2WL1QIcu0t/O8HxC
         rwHyZCrOLm91foOws/KMX5ZG7RyclO3bejFhjxSF2GNDkoC4z1ERpUn0v5xdWmRxLN8q
         UHC5buVERb6onX0FaV2GdgVPSMaCHMQTIJgbnkpCaZntmigBamHZf8b2L9Tr+xYYUbMd
         9GkjHvFWC8FCMqJp15xRiisDSsUm53AZKEcwFf5x42UZ3aWvRMvd6mO4aNPcMOr3u4CT
         eLuBm6cAdwwZqneNBcUMRMyDNuW4TdvmxnV5qAV/GqjBkvvvTcg2yonDfsSInKMCDEgL
         a/rw==
X-Gm-Message-State: AOAM532m4e5aXwXXwlcMvUy7P26M0HC1PgbtnmxXcNCZ7N4vx9qmU0c5
        gGdzhlYrPBcQ9s4xoBHSj9o=
X-Google-Smtp-Source: ABdhPJzWz4zqSl4Wxjq1eHtKykn4YK5Yhk7fNzB2Mp2qMU2rPertjE2XHx3rd4ZyjyzqgfMhtT5Y9w==
X-Received: by 2002:a17:906:c34c:: with SMTP id ci12mr25899953ejb.247.1629537596662;
        Sat, 21 Aug 2021 02:19:56 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.55
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:56 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 07/37] perf workqueue: add support for setting affinities to workers
Date:   Sat, 21 Aug 2021 11:19:13 +0200
Message-Id: <5e564cefbf2612ff492a77f25ba4289ad3efcee0.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds an interface to workqueue to set affinities to the
underlying threadpool threads.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/workqueue/workqueue.c | 21 +++++++++++++++++++++
 tools/perf/util/workqueue/workqueue.h |  6 ++++++
 2 files changed, 27 insertions(+)

diff --git a/tools/perf/util/workqueue/workqueue.c b/tools/perf/util/workqueue/workqueue.c
index 7daac65abb5d57d1..61f1b6c41deba031 100644
--- a/tools/perf/util/workqueue/workqueue.c
+++ b/tools/perf/util/workqueue/workqueue.c
@@ -50,6 +50,7 @@ static const char * const workqueue_errno_str[] = {
 	"Error executing function in threadpool",
 	"Error stopping the threadpool",
 	"Error starting thread in the threadpool",
+	"Error setting affinity in threadpool",
 	"Error sending message to worker",
 	"Error receiving message from worker",
 	"Received unexpected message from worker",
@@ -758,6 +759,26 @@ int flush_workqueue(struct workqueue_struct *wq)
 	return err;
 }
 
+/**
+ * workqueue_set_affinities - set affinities to all threads in @wq->pool
+ */
+int workqueue_set_affinities(struct workqueue_struct *wq,
+				struct mmap_cpu_mask *affinities)
+{
+	wq->pool_errno = threadpool__set_affinities(wq->pool, affinities);
+	return wq->pool_errno ? -WORKQUEUE_ERROR__POOLAFFINITY : 0;
+}
+
+/**
+ * workqueue_set_affinities - set affinity to thread @tidx in @wq->pool
+ */
+int workqueue_set_affinity(struct workqueue_struct *wq, int tidx,
+				struct mmap_cpu_mask *affinity)
+{
+	wq->pool_errno = threadpool__set_affinity(wq->pool, tidx, affinity);
+	return wq->pool_errno ? -WORKQUEUE_ERROR__POOLAFFINITY : 0;
+}
+
 /**
  * init_work - initialize the @work struct
  */
diff --git a/tools/perf/util/workqueue/workqueue.h b/tools/perf/util/workqueue/workqueue.h
index 7a0eda923df25d85..dc6baee138b22ab2 100644
--- a/tools/perf/util/workqueue/workqueue.h
+++ b/tools/perf/util/workqueue/workqueue.h
@@ -28,6 +28,11 @@ extern int queue_work_on_worker(int tidx, struct workqueue_struct *wq, struct wo
 
 extern int flush_workqueue(struct workqueue_struct *wq);
 
+extern int workqueue_set_affinities(struct workqueue_struct *wq,
+				struct mmap_cpu_mask *affinities);
+extern int workqueue_set_affinity(struct workqueue_struct *wq, int tidx,
+				struct mmap_cpu_mask *affinity);
+
 extern void init_work(struct work_struct *work);
 
 /* parallel_for utility */
@@ -92,6 +97,7 @@ enum {
 	WORKQUEUE_ERROR__POOLEXE,
 	WORKQUEUE_ERROR__POOLSTOP,
 	WORKQUEUE_ERROR__POOLSTARTTHREAD,
+	WORKQUEUE_ERROR__POOLAFFINITY,
 	WORKQUEUE_ERROR__WRITEPIPE,
 	WORKQUEUE_ERROR__READPIPE,
 	WORKQUEUE_ERROR__INVALIDMSG,
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id AA88AC432BE
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:14 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 94A0B61156
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:14 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234282AbhHUJUw (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:52 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34068 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233834AbhHUJUk (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:40 -0400
Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C786C0617AE;
        Sat, 21 Aug 2021 02:19:59 -0700 (PDT)
Received: by mail-ed1-x52a.google.com with SMTP id bo19so17594840edb.9;
        Sat, 21 Aug 2021 02:19:59 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=cjZ7uJNpZLE3J25Q91QvoDOYEkRc1avEONuCIWmN21s=;
        b=k7f6w18PjhT3tPUNqSh13BKbvmzhsemBAn2Yv31MYOaF1P09K+9A5g9grlZJ3GgeXm
         FOvin/FqkPFeWlWH6FbtwbaX3FDcLvIkGOlkCgldeyhiMMZ/lQremK/bjpN8vUepBLcq
         W0ejf7EGc5goNcUqLa20UrLSG90CKQeAVhzehICzFuRcMFbZUbgPGU+K5SsDt9EQAcOM
         9P/nIkDQpl5YAUSBYyoyFNO3mZIB3P+lLVnzdsywopM/mki2jUDyRWg/cX/V++ZEcVP4
         z5gJLP4nMCe01meDXDdQ8zDaH/P/9QRSeiiokPCjCIyXGoKeBMWK1JhYyf5yzMioDVze
         n1mg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=cjZ7uJNpZLE3J25Q91QvoDOYEkRc1avEONuCIWmN21s=;
        b=XzxQtyDI6nsdA8d3d+pKfM4ip3HN/Gr5fhNuum31XmGA/4OWZWe2i6GOEk1Di7rb13
         a+5kaU32SNRmsNqUfKsgsKZwO7eMh8+hrKlW1Kg24DJO4dEUUfRozQRp125d2qPG3P9x
         Cqn9z6+QQFyD4HxX32kTqeg+RGcdgT1PMHy1gbtczde/5jOWHTZ8Par6upxXW4JbeUDq
         qORf3vPjbvCavmuvt3CL0lIszbVfPqTe3TL93T14BaIFnm0QsMdDiw48O8/nHM+tbNQ9
         CWbYAAxNqbmMqp51aT0ZiqlMpZ62eHvLYPOal2hyCd1sYobC2ksg4QRHqSiZLzs/1DVE
         dVHg==
X-Gm-Message-State: AOAM5308mSU3U+DxSlFN+B3pb2z6W41UN1JHARhhFWCqOKM+GBGkmWxP
        1RZnjHg4hMOdK5EXDXFpQLE=
X-Google-Smtp-Source: ABdhPJwk6TExhFuuAKA+/Mj774+d3h051lTRiB9fbjHzh6HqntG9xKsFHTnbK0v17l/ePBCXcsYayw==
X-Received: by 2002:a05:6402:52:: with SMTP id f18mr26541861edu.338.1629537597915;
        Sat, 21 Aug 2021 02:19:57 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.56
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:57 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 08/37] perf workqueue: add method to execute work on specific CPU
Date:   Sat, 21 Aug 2021 11:19:14 +0200
Message-Id: <fc51bfc206cd5976eec14541fc5ee50ce6788818.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds the possibility to schedule a work item on a specific
CPU.
There are 2 possibilities:
 - threads are pinned to a CPU using the new functions
   workqueue_set_affinity_cpu and workqueue_set_affinities_cpu
 - no thread is pinned to the requested cpu. In this case, affinity will
   be set before (and cleared after) executing the work.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/workqueue/workqueue.c | 133 +++++++++++++++++++++++++-
 tools/perf/util/workqueue/workqueue.h |  12 +++
 2 files changed, 144 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/workqueue/workqueue.c b/tools/perf/util/workqueue/workqueue.c
index 61f1b6c41deba031..650170a6a11f56bd 100644
--- a/tools/perf/util/workqueue/workqueue.c
+++ b/tools/perf/util/workqueue/workqueue.c
@@ -10,9 +10,12 @@
 #include <linux/string.h>
 #include <linux/zalloc.h>
 #include <linux/kernel.h>
+#include <linux/bitmap.h>
 #include "debug.h"
 #include <internal/lib.h>
 #include "workqueue.h"
+#include <perf/cpumap.h>
+#include "util/affinity.h"
 
 struct workqueue_struct *global_wq;
 
@@ -43,6 +46,10 @@ struct workqueue_struct {
 	struct worker		**workers;	/* array of all workers */
 	struct worker		*next_worker;	/* next worker to choose (round robin) */
 	int			first_stopped_worker; /* next worker to start if needed */
+	struct {
+		int		*map;		/* maps cpu to thread idx */
+		int		size;		/* size of the map array */
+	} cpu_to_tidx_map;
 };
 
 static const char * const workqueue_errno_str[] = {
@@ -429,6 +436,7 @@ static void worker_thread(int tidx, struct task_struct *task)
 struct workqueue_struct *create_workqueue(int nr_threads)
 {
 	int ret, err = 0;
+	int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 	struct workqueue_struct *wq = zalloc(sizeof(struct workqueue_struct));
 
 	if (!wq) {
@@ -449,10 +457,18 @@ struct workqueue_struct *create_workqueue(int nr_threads)
 		goto out_delete_pool;
 	}
 
+	wq->cpu_to_tidx_map.size = nr_cpus;
+	wq->cpu_to_tidx_map.map = calloc(nr_cpus, sizeof(*wq->cpu_to_tidx_map.map));
+	if (!wq->workers) {
+		err = -ENOMEM;
+		goto out_free_workers;
+	}
+	memset(wq->cpu_to_tidx_map.map, -1, nr_cpus * sizeof(*wq->cpu_to_tidx_map.map));
+
 	ret = pthread_mutex_init(&wq->lock, NULL);
 	if (ret) {
 		err = -ret;
-		goto out_free_workers;
+		goto out_free_cpu_to_idx_map;
 	}
 
 	ret = pthread_cond_init(&wq->idle_cond, NULL);
@@ -494,6 +510,8 @@ struct workqueue_struct *create_workqueue(int nr_threads)
 	pthread_mutex_destroy(&wq->lock);
 out_free_workers:
 	free(wq->workers);
+out_free_cpu_to_idx_map:
+	free(wq->cpu_to_tidx_map.map);
 out_delete_pool:
 	threadpool__delete(wq->pool);
 out_free_wq:
@@ -552,6 +570,7 @@ int destroy_workqueue(struct workqueue_struct *wq)
 	wq->msg_pipe[1] = -1;
 
 	zfree(&wq->workers);
+	zfree(&wq->cpu_to_tidx_map.map);
 	free(wq);
 	return err;
 }
@@ -779,6 +798,118 @@ int workqueue_set_affinity(struct workqueue_struct *wq, int tidx,
 	return wq->pool_errno ? -WORKQUEUE_ERROR__POOLAFFINITY : 0;
 }
 
+/**
+ * workqueue_set_affinity_cpu - set affinity to @cpu to thread @tidx in @wq->pool
+ *
+ * If cpu is -1, then affinity is set to all online processors.
+ */
+int workqueue_set_affinity_cpu(struct workqueue_struct *wq, int tidx, int cpu)
+{
+	struct mmap_cpu_mask affinity;
+	int i, err;
+
+	if (cpu >= 0)
+		affinity.nbits = cpu+1;
+	else
+		affinity.nbits = wq->cpu_to_tidx_map.size;
+
+	affinity.bits = bitmap_alloc(affinity.nbits);
+	if (!affinity.bits) {
+		pr_debug2("Failed allocation of bitmapset\n");
+		return -ENOMEM;
+	}
+
+	if (cpu >= 0)
+		test_and_set_bit(cpu, affinity.bits);
+	else
+		bitmap_fill(affinity.bits, affinity.nbits);
+
+	err = workqueue_set_affinity(wq, tidx, &affinity);
+	if (err)
+		goto out;
+
+	// find and unset this thread from the map
+	for (i = 0; i < wq->cpu_to_tidx_map.size; i++) {
+		if (wq->cpu_to_tidx_map.map[i] == tidx)
+			wq->cpu_to_tidx_map.map[i] = -1;
+	}
+
+	if (cpu >= 0)
+		wq->cpu_to_tidx_map.map[cpu] = tidx;
+
+out:
+	bitmap_free(affinity.bits);
+	return err;
+}
+
+/**
+ * workqueue_set_affinities_cpu - set single-cpu affinities to all threads in @wq->pool
+ */
+int workqueue_set_affinities_cpu(struct workqueue_struct *wq,
+				struct perf_cpu_map *cpus)
+{
+	int cpu, idx, err;
+
+	if (perf_cpu_map__nr(cpus) > threadpool__size(wq->pool))
+		return -EINVAL;
+
+
+	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
+		err = workqueue_set_affinity_cpu(wq, idx, cpu);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
+struct cpu_bound_work {
+	struct work_struct work;
+	int cpu;
+	struct work_struct *original_work;
+};
+
+static void set_affinity_and_execute(struct work_struct *work)
+{
+	struct cpu_bound_work *cpu_bound_work = container_of(work, struct cpu_bound_work, work);
+	struct affinity affinity;
+
+	if (affinity__setup(&affinity) < 0)
+		goto out;
+
+	affinity__set(&affinity, cpu_bound_work->cpu);
+	cpu_bound_work->original_work->func(cpu_bound_work->original_work);
+	affinity__cleanup(&affinity);
+
+out:
+	free(cpu_bound_work);
+}
+
+/**
+ * queue_work_on - execute @work on @cpu
+ *
+ * The work is assigned to the worker pinned to @cpu, if any.
+ * Otherwise, affinity is set before running the work and unset after.
+ */
+int queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
+{
+	struct cpu_bound_work *cpu_bound_work;
+	int tidx = wq->cpu_to_tidx_map.map[cpu];
+
+	if (tidx >= 0)
+		return queue_work_on_worker(tidx, wq, work);
+
+	cpu_bound_work = malloc(sizeof(*cpu_bound_work));
+	if (!cpu_bound_work)
+		return -ENOMEM;
+
+	init_work(&cpu_bound_work->work);
+	cpu_bound_work->work.func = set_affinity_and_execute;
+	cpu_bound_work->cpu = cpu;
+	cpu_bound_work->original_work = work;
+	return queue_work(wq, &cpu_bound_work->work);
+}
+
 /**
  * init_work - initialize the @work struct
  */
diff --git a/tools/perf/util/workqueue/workqueue.h b/tools/perf/util/workqueue/workqueue.h
index dc6baee138b22ab2..a91a37e367b62d02 100644
--- a/tools/perf/util/workqueue/workqueue.h
+++ b/tools/perf/util/workqueue/workqueue.h
@@ -25,6 +25,7 @@ extern int workqueue_nr_threads(struct workqueue_struct *wq);
 
 extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
 extern int queue_work_on_worker(int tidx, struct workqueue_struct *wq, struct work_struct *work);
+extern int queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work);
 
 extern int flush_workqueue(struct workqueue_struct *wq);
 
@@ -32,6 +33,9 @@ extern int workqueue_set_affinities(struct workqueue_struct *wq,
 				struct mmap_cpu_mask *affinities);
 extern int workqueue_set_affinity(struct workqueue_struct *wq, int tidx,
 				struct mmap_cpu_mask *affinity);
+extern int workqueue_set_affinity_cpu(struct workqueue_struct *wq, int tidx, int cpu);
+extern int workqueue_set_affinities_cpu(struct workqueue_struct *wq,
+					struct perf_cpu_map *cpus);
 
 extern void init_work(struct work_struct *work);
 
@@ -82,6 +86,14 @@ static inline int schedule_work_on_worker(int tidx, struct work_struct *work)
 	return queue_work_on_worker(tidx, global_wq, work);
 }
 
+/**
+ * schedule_work_on - queue @work to be executed on @cpu by global_wq
+ */
+static inline int schedule_work_on(int cpu, struct work_struct *work)
+{
+	return queue_work_on(cpu, global_wq, work);
+}
+
 /**
  * flush_scheduled_work - ensure that any scheduled work in global_wq has run to completion
  */
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 08086C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:16 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id E050761101
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:15 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233744AbhHUJUx (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:20:53 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34110 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233945AbhHUJUl (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:41 -0400
Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC30EC0613A3;
        Sat, 21 Aug 2021 02:20:00 -0700 (PDT)
Received: by mail-ej1-x635.google.com with SMTP id bq25so25121389ejb.11;
        Sat, 21 Aug 2021 02:20:00 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=CmrhphcsjFgBuYDW3B3fapu6bDNA5VX52WO9R+24v9s=;
        b=m2Viv4XboyvSZ5ruFP2UcLsLcQlJdXhMk3zU5fb+39f8d7ebSn4ZdCh4T8UX14mQ2z
         W4ID/VE0LvTD2JctHUBQ8vkdFSgXeHBuWEoMl2rGCgD8NrcqsUdultrrHN5K8XPp4RCk
         S1lCRsEg57Ev8dqjmhSIFQL0XJ2xSuyREDfgswxtTZrdR9X4mZP9xg5whV58VA78gTdS
         ZSam1S1hAdd8UR/3I20u71XPVpmzLPDOCWKrnATGSk5U2ffqhb/ZAW3srPEyfZsJGJWj
         ErHTWSt5Ab4nh8OCKS0cYmZbHO2GszTzOVAuKNHDhRFFGjiL7qVxSpyzfVrOsLvyBn8y
         e8xg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=CmrhphcsjFgBuYDW3B3fapu6bDNA5VX52WO9R+24v9s=;
        b=nbxA2nrm3K6M2X/+1ut8dbAwZ7qKMjGMYMHHpHrKk1gAf1V1qHTV7KlxoVfaSmV21R
         pu8b0aHBjbl5WcbVONKqqQMSUMj9Zr75s/c2p39U0Z3v0FOx2CM7kLBn0SeCcVC7lf6Y
         /n8MJ2CMu4pjiVYGiAYOyZF/EDcUOInmyhpHUsK6O79bjkI1e5PiLUi6w/foxXSplGo1
         bfsqBanYiXViiXpYHhNF+jOXdjQS9tvd94e/Gu17XkCZtWO5vZb6f8CSMMtk3LhylABX
         DPuyRXAdFzkbeava3nvaAhRl7H+dElrfTH/xvBExG6ahVQbwyEa8AhA7VFEipDkXlIjU
         VhwQ==
X-Gm-Message-State: AOAM531w9CTthtUEJymj3lThSfiZebWOpCCGKSRUirfEOXZdG439W81d
        4yVKVbT68kU06Ai0hE5oceY=
X-Google-Smtp-Source: ABdhPJyVyJQVRW66l7EIYPAWWjnr3xxvDU7hN6mBExB62tdaMgYjtYKgB4xTi6fcRR+NL8HxLaCA3w==
X-Received: by 2002:a17:906:c4c:: with SMTP id t12mr25950039ejf.217.1629537599242;
        Sat, 21 Aug 2021 02:19:59 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.58
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:19:59 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 09/37] perf python: add workqueue dependency
Date:   Sat, 21 Aug 2021 11:19:15 +0200
Message-Id: <6fd8b091eb4765fde55a1d35a273c4b3765b0c3d.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds util/workqueue/*.c to python-ext-sources, which is
needed for the following patch, where workqueue will be used in
util/evlist.c.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/python-ext-sources | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources
index d7c976671e3a131a..7453f65c6f7a4396 100644
--- a/tools/perf/util/python-ext-sources
+++ b/tools/perf/util/python-ext-sources
@@ -39,3 +39,5 @@ util/rwsem.c
 util/hashmap.c
 util/pmu-hybrid.c
 util/fncache.c
+util/workqueue/threadpool.c
+util/workqueue/workqueue.c
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id D1BB7C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:37 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id BADED61186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:37 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234077AbhHUJVQ (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:16 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34094 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234134AbhHUJUs (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:48 -0400
Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B729C0612E7;
        Sat, 21 Aug 2021 02:20:02 -0700 (PDT)
Received: by mail-ej1-x62d.google.com with SMTP id w5so25224867ejq.2;
        Sat, 21 Aug 2021 02:20:01 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=nYCZWFJIc++fa6HHlc4YfaHaR0UFJ1cN64ckPkTe/mE=;
        b=YnreBb68LiGNZPOjvwGEyR6jPj/i+sVhr1Z7zn2xSsRcwjbhgJN+AXLDnUEbfIHyJK
         Mm+RFqaxbHNsKJbj0rP/6inIPRCoJInpdOcV3tHEWWaV/BiywMyIBlG+TbIjp0e3FaLb
         VUZ+twcHjZnYLHiD6CQxRVjvFFct6zJkUFiX5T8bX6LTPNngQJHhdq+wNlApAYxA8BPn
         ckEHKjIBcA51FJG9ODsna2tkPVCNvsD1IA3Tx8igZJ8stXn5tvF+CJOEU2viagGGigkT
         l5iEjyQ3ZzYaLfkfwjaKjs08ScoC4hC4ClAvWenZfPwd5nZ/J8dffIDweqXnccbMYskQ
         oXzg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=nYCZWFJIc++fa6HHlc4YfaHaR0UFJ1cN64ckPkTe/mE=;
        b=q8lXm4qas2wK8DfdtdpE/mUqEcioWbeZDFFPneJGNbWOIHZTkPmY3J3xiD+cHzZMVC
         f172eznvD9ZAqpHrBX2BFIlFvn24FOotrBuT71zLoo3OjtrJ5h8vo0ufKzUsmtRPvSPE
         CRAxZh/7nJUVO6FEdlyAXB1DzHOxO3xiUUp9VDAL532i0/Etbxv067DGUsb7k3tNqcrJ
         B0/IMvpVQFl7vAcuu27Rj3OU0OrbOIL3lYujWa7dXKAX1WuOI1M5tdXZHudU+ibHnsFn
         Cv2/BRP20l0SphLDHxS6TbhXMvl5xZSYPh+zxtxL4xmIS2PKYuDCHdNE4lVg7KFaxBuO
         IVlQ==
X-Gm-Message-State: AOAM530V56Nya0ecnwCkTQheSOvbMC8rOmbS73OXzZyD66rIwheGroOs
        gt+0I4d32aziUmlMc+1XtXY=
X-Google-Smtp-Source: ABdhPJyce9hWXTkxRNdvnxtjxseU74yk9B1zgj+Ux05n1XUE6rDGvaA9arGsiHRn6W4IjKsc0RnpfQ==
X-Received: by 2002:a17:906:408d:: with SMTP id u13mr26070763ejj.344.1629537600562;
        Sat, 21 Aug 2021 02:20:00 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.19.59
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:00 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 10/37] perf evlist: add multithreading helper
Date:   Sat, 21 Aug 2021 11:19:16 +0200
Message-Id: <34d8ec75acb56fbbbef08fee8be8404015e682cd.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds the function evlist__for_each_evsel_cpu, which executes
the given function on each evsel, for each cpu.

If perf_singlethreaded is unset, this function will use a workqueue to
execute the function.

This helper function will be used in the following patches.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evlist.c | 117 +++++++++++++++++++++++++++++++++++++++
 tools/perf/util/evlist.h |  14 +++++
 2 files changed, 131 insertions(+)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 3d55d9a53b9f4875..f9fdbd85a163ee97 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -27,6 +27,8 @@
 #include "util/perf_api_probe.h"
 #include "util/evsel_fprintf.h"
 #include "util/evlist-hybrid.h"
+#include "util/util.h"
+#include "util/workqueue/workqueue.h"
 #include <signal.h>
 #include <unistd.h>
 #include <sched.h>
@@ -41,6 +43,7 @@
 #include <sys/prctl.h>
 
 #include <linux/bitops.h>
+#include <linux/kernel.h>
 #include <linux/hash.h>
 #include <linux/log2.h>
 #include <linux/err.h>
@@ -369,6 +372,120 @@ static int evlist__is_enabled(struct evlist *evlist)
 	return false;
 }
 
+struct evlist_work {
+	struct work_struct work;
+	struct evlist *evlist;
+	int cpu;
+	evsel__cpu_func func;
+	void *args;
+	int ret;
+};
+
+static void evlist__for_each_evsel_cpu_thread_func(struct work_struct *_work)
+{
+	struct evlist_work *work = container_of(_work, struct evlist_work, work);
+	int cpu_idx, ret, err = 0;
+	struct evsel *pos;
+
+	work->ret = 0;
+	evlist__for_each_entry(work->evlist, pos) {
+		cpu_idx = evsel__find_cpu(pos, work->cpu);
+		if (cpu_idx < 0)
+			continue;
+		ret = work->func(work->evlist, pos, cpu_idx, work->args);
+		if (ret) {
+			work->ret = ret;
+			if (err < 0)		// error
+				return;
+		}
+	}
+}
+
+static int evlist__for_each_evsel_cpu_multithreaded(struct evlist *evlist,
+					evsel__cpu_func func, void *args)
+{
+	int i, cpu, ret;
+	struct evlist_work *works;
+	char errbuf[WORKQUEUE_STRERR_BUFSIZE];
+
+	works = calloc(perf_cpu_map__nr(evlist->core.all_cpus), sizeof(*works));
+	perf_cpu_map__for_each_cpu(cpu, i, evlist->core.all_cpus) {
+		init_work(&works[i].work);
+		works[i].evlist = evlist;
+		works[i].work.func = evlist__for_each_evsel_cpu_thread_func;
+		works[i].cpu = cpu;
+		works[i].func = func;
+		works[i].args = args;
+		works[i].ret = 0;
+
+		ret = schedule_work_on(cpu, &works[i].work);
+		if (ret) {
+			workqueue_strerror(global_wq, ret, errbuf, sizeof(errbuf));
+			pr_debug("schedule_work: %s\n", errbuf);
+			break;
+		}
+	}
+
+	ret = flush_scheduled_work();
+	if (ret) {
+		workqueue_strerror(global_wq, ret, errbuf, sizeof(errbuf));
+		pr_debug("flush_scheduled_work: %s\n", errbuf);
+		goto out;
+	}
+
+	perf_cpu_map__for_each_cpu(cpu, i, evlist->core.all_cpus) {
+		if (works[i].ret) {
+			ret = works[i].ret;
+			if (works[i].ret < 0)		// error
+				goto out;
+		}
+	}
+out:
+	free(works);
+
+	return ret;
+}
+
+static int evlist__for_each_evsel_cpu_singlethreaded(struct evlist *evlist,
+					evsel__cpu_func func, void *args)
+{
+	int ret, err = 0, i, cpu, cpu_idx;
+	struct affinity affinity;
+	struct evsel *pos;
+
+	if (affinity__setup(&affinity) < 0)
+		return -1;
+
+	evlist__for_each_cpu(evlist, i, cpu) {
+		affinity__set(&affinity, cpu);
+
+		evlist__for_each_entry(evlist, pos) {
+			cpu_idx = evsel__find_cpu(pos, cpu);
+			if (cpu_idx < 0)
+				continue;
+			ret = func(evlist, pos, cpu_idx, args);
+			if (ret) {
+				err = ret;
+				if (err < 0)		// error
+					goto out;
+			}
+		}
+	}
+
+out:
+	affinity__cleanup(&affinity);
+	return err;
+}
+
+int evlist__for_each_evsel_cpu(struct evlist *evlist, evsel__cpu_func func, void *args)
+{
+	if (perf_singlethreaded)
+		return evlist__for_each_evsel_cpu_singlethreaded(evlist, func, args);
+	else
+		return evlist__for_each_evsel_cpu_multithreaded(evlist, func, args);
+
+}
+
 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
 {
 	struct evsel *pos;
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index fde893170c7ba6d2..5f24a45d4e3cf30a 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -363,4 +363,18 @@ struct evsel *evlist__find_evsel(struct evlist *evlist, int idx);
 
 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf);
 void evlist__check_mem_load_aux(struct evlist *evlist);
+
+/**
+ * evsel__cpu_func - function to run on each evsel for each cpu
+ * @evlist: the parent evlist
+ * @evsel: the processed evsel
+ * @cpu: index of the cpu in evsel->core.cpus
+ * @args: additional custom arguments
+ *
+ * Returns:
+ * A negative value is considered as an error.
+ * A positive value will be propagated to evlist__for_each_evsel_cpu.
+ */
+typedef int (*evsel__cpu_func)(struct evlist *evlist, struct evsel *evsel, int cpu, void *args);
+int evlist__for_each_evsel_cpu(struct evlist *evlist, evsel__cpu_func func, void *args);
 #endif /* __PERF_EVLIST_H */
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id AF940C432BE
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:38 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 97CC961186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:38 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234150AbhHUJVQ (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:16 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34142 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234166AbhHUJUs (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:48 -0400
Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47953C06129D;
        Sat, 21 Aug 2021 02:20:03 -0700 (PDT)
Received: by mail-ed1-x534.google.com with SMTP id r19so17598085eds.13;
        Sat, 21 Aug 2021 02:20:03 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=KwlR07ut3cqVZEyAKALk9RBIbvWirqxmVWSTPfvdu3o=;
        b=QsLXNogpiol/dymbdXO361CG8GbHjA0WP8xm+E1uGPSU/GjZ/vgkJ2pV0bv6XAy4T4
         7QeXOlMUjeocaa3f42Q0WqBgERiWgVjzTiYgCfjPfhveSdgKuEoBluG0ienHIDXFOKrC
         TPeyoNKHs5skJ8tH/qG7Pm0XXGSFnxG5FQWnIbK6O1rz0Q2bNLopdrwkVZtlyNLbOaEY
         sQAJ1obJBGks6RaHuNMleqraIraSPRoSbW5c2xXmMHZxf4NcBVpi+lqBihCzwdMRAvwk
         pL5hMhzzHH/GLGglDrNA/jr3b/Lw9ZGGNiKQYrdc01K+AV8CARi+3ND+5O2fGgYNzYZ1
         UcOw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=KwlR07ut3cqVZEyAKALk9RBIbvWirqxmVWSTPfvdu3o=;
        b=S04e1UQ9FzVh7ewqi3xbdng/9wBexg+i0hm+DSb/Vlj13MDeJ/ZsXSEjfiu7OchEaE
         /BGirC+oMnQU1utWFoVReuzh59NvK7KatZxJV+QFxeJ4owDhQZ8Wh8+scWMq7f9jKdYZ
         +3+0bG7d/wU83uIEY/VZmXWHUKnuDrSJFEuOhaS4xnU7t4gIn97fV5uxsNJHUqhfbP4w
         09fwtmkSm94TwRhp/akKP2ZEU+OCLioPmD4dukwW58bRoIlGN1lA7T73tl8JFMZHfcoi
         bQKU0Wo5Pd5hW12Fib/yzWLuJ/pKWPd7MXkZr49H3DtVhBxWW4IEa9adQzunwmS/Gyvb
         g3oA==
X-Gm-Message-State: AOAM530O3LxJ5L/oz31RcR0R//Z5l6seSayCzC2OONiYZFJNXXDecYXj
        D36QycWuBEiWhQpFjmq/06A=
X-Google-Smtp-Source: ABdhPJxrV3Ysw75iDG3KpLGT2qVE0SchiiZ1Q9MgRlbXXOR1V9j9M1AQI7VvO3Oy29GWH4teQQvEQg==
X-Received: by 2002:a05:6402:84e:: with SMTP id b14mr26513634edz.227.1629537601871;
        Sat, 21 Aug 2021 02:20:01 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.00
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:01 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 11/37] perf evlist: add multithreading to evlist__disable
Date:   Sat, 21 Aug 2021 11:19:17 +0200
Message-Id: <7dd234195436b7f6db850719292f6d9d63fdd6ff.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

In this patch, evlist__for_each_evsel_cpu is used in evlist__disable to
allow it to run in parallel.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evlist.c | 57 +++++++++++++++++++++-------------------
 1 file changed, 30 insertions(+), 27 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index f9fdbd85a163ee97..5c82246813c51c51 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -486,41 +486,44 @@ int evlist__for_each_evsel_cpu(struct evlist *evlist, evsel__cpu_func func, void
 
 }
 
+struct evlist_disable_args {
+	char *evsel_name;
+	int imm;
+};
+
+static int __evlist__disable_evsel_cpu_func(struct evlist *evlist __maybe_unused,
+					struct evsel *pos, int cpu, void *_args)
+{
+	int ret = 0;
+	struct evlist_disable_args *args = _args;
+
+	if (evsel__strcmp(pos, args->evsel_name))
+		return 0;
+	if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
+		return 0;
+	if (pos->immediate)
+		ret = 1;
+	if (pos->immediate != args->imm)
+		return ret;
+	evsel__disable_cpu(pos, cpu);
+
+	return ret;
+}
+
+
 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
 {
 	struct evsel *pos;
-	struct affinity affinity;
-	int cpu, i, imm = 0, cpu_idx;
-	bool has_imm = false;
-
-	if (affinity__setup(&affinity) < 0)
-		return;
+	int ret;
+	struct evlist_disable_args args = { .evsel_name = evsel_name };
 
 	/* Disable 'immediate' events last */
-	for (imm = 0; imm <= 1; imm++) {
-		evlist__for_each_cpu(evlist, i, cpu) {
-			affinity__set(&affinity, cpu);
-
-			evlist__for_each_entry(evlist, pos) {
-				if (evsel__strcmp(pos, evsel_name))
-					continue;
-				cpu_idx = evsel__find_cpu(pos, cpu);
-				if (cpu_idx < 0)
-					continue;
-				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
-					continue;
-				if (pos->immediate)
-					has_imm = true;
-				if (pos->immediate != imm)
-					continue;
-				evsel__disable_cpu(pos, cpu_idx);
-			}
-		}
-		if (!has_imm)
+	for (args.imm = 0; args.imm <= 1; args.imm++) {
+		ret = evlist__for_each_evsel_cpu(evlist, __evlist__disable_evsel_cpu_func, &args);
+		if (!ret)	// does not have immediate evsels
 			break;
 	}
 
-	affinity__cleanup(&affinity);
 	evlist__for_each_entry(evlist, pos) {
 		if (evsel__strcmp(pos, evsel_name))
 			continue;
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 0ACD2C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:40 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id E6DCF61215
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:39 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234425AbhHUJVR (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:17 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34146 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234183AbhHUJUt (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:49 -0400
Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E870C06129E;
        Sat, 21 Aug 2021 02:20:04 -0700 (PDT)
Received: by mail-ej1-x631.google.com with SMTP id z20so25181598ejf.5;
        Sat, 21 Aug 2021 02:20:04 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=y0qmvJhChn9arrJ/rGJFD9sMX4D0tlR5THiYSB7Y93g=;
        b=NclQhA3IMlz6bJJsu2lP34LymOrK3ekgCnfc8WMbX22Oc01p1iMkDmJyjnJlZ+uuT3
         KlgaIqLFalY9I70N8GnqNaW3qZQUSLa4oX2ci7lONyXJOXgRT4UyQCrtQQjhwPe67Xh7
         lgddfj25StczUQQVOyl2hRh0RWy3Ivt80op8w5qOfHhpThhM9pQoY5DvhM2F3QO1X2Q7
         CEmFcgANeoE5l5MC9TFz8PKGOe/MMOI869UFKCOfRYzTYB/pndNr/NtiaSpjlVwIZ2G8
         2vOulYtlZiWIuPbVaf3sYHtIKVkZXCPkAqxzn+VYl01gwpvE5gTYlytA/bBgBn6ncDa7
         oC3Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=y0qmvJhChn9arrJ/rGJFD9sMX4D0tlR5THiYSB7Y93g=;
        b=rdU1xJhOOGGOL8fN+rAqJKRwl/zu/umiTQN5utolnu4DqKeht0YeenfWdLF0o9YpSL
         74NgMYi8hEee/HNpCt1hZDWtvsuZe0DuYx+32UiDatwHgBGcPbk4uR8BM1824Sf1KR9a
         97xG7LrPePt7c3p1hQP9nSh4jOKr4dANRo4goI4OvZgMkGP5tcB1HG6B9Za+8+rb2M7O
         zQY6awAmJcSkLFOE/gZ3TdZdneeE4QpO2e3lc+XJkskBqkaggPkqW0CbEcR4RMqxzYim
         asGEjNHuFVpJNMvWLoqrlfXvcCWbZBigyHBxpN/0AU+kgr9Q1lnot/i+1+IRzyZ5Su4h
         TcIA==
X-Gm-Message-State: AOAM533H+cPwatu3ezbhgKIXTclySr57jf6jQV0G0c3IjhOO/f2GXtGw
        dyo+wgen9S4UQw2U7z50I6I8FrSXLerDs5dIeQQ=
X-Google-Smtp-Source: ABdhPJzaClLRRYQ+cz6kMXL05+dfo/J4U+WeVY/rQE/+tLGc/Eh1S//eTsFpb6Fce6aNxwuCi6aaog==
X-Received: by 2002:a17:907:7291:: with SMTP id dt17mr26449106ejc.162.1629537603165;
        Sat, 21 Aug 2021 02:20:03 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.01
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:02 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 12/37] perf evlist: add multithreading to evlist__enable
Date:   Sat, 21 Aug 2021 11:19:18 +0200
Message-Id: <9a3eda98548b5db42d5a6677ed3c607928d29af9.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

In this patch, evlist__for_each_evsel_cpu is used in evlist__enable to
allow it to run in parallel.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evlist.c | 41 +++++++++++++---------------------------
 1 file changed, 13 insertions(+), 28 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 5c82246813c51c51..fbe2d6ed8ecc8f21 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -552,37 +552,22 @@ void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
 	__evlist__disable(evlist, evsel_name);
 }
 
-static void __evlist__enable(struct evlist *evlist, char *evsel_name)
+static int __evlist__enable_evsel_cpu_fun(struct evlist *evlist __maybe_unused,
+					struct evsel *pos, int cpu, void *args)
 {
-	struct evsel *pos;
-	struct affinity affinity;
-	int cpu, i, cpu_idx;
+	char *evsel_name = args;
 
-	if (affinity__setup(&affinity) < 0)
-		return;
-
-	evlist__for_each_cpu(evlist, i, cpu) {
-		affinity__set(&affinity, cpu);
+	if (evsel__strcmp(pos, evsel_name))
+		return 0;
+	if (!evsel__is_group_leader(pos) || !pos->core.fd)
+		return 0;
+	evsel__enable_cpu(pos, cpu);
+	return 0;
+}
 
-		evlist__for_each_entry(evlist, pos) {
-			if (evsel__strcmp(pos, evsel_name))
-				continue;
-			cpu_idx = evsel__find_cpu(pos, cpu);
-			if (cpu_idx < 0)
-				continue;
-			if (!evsel__is_group_leader(pos) || !pos->core.fd)
-				continue;
-			evsel__enable_cpu(pos, cpu_idx);
-		}
-	}
-	affinity__cleanup(&affinity);
-	evlist__for_each_entry(evlist, pos) {
-		if (evsel__strcmp(pos, evsel_name))
-			continue;
-		if (!evsel__is_group_leader(pos) || !pos->core.fd)
-			continue;
-		pos->disabled = false;
-	}
+static void __evlist__enable(struct evlist *evlist, char *evsel_name)
+{
+	evlist__for_each_evsel_cpu(evlist, __evlist__enable_evsel_cpu_fun, evsel_name);
 
 	/*
 	 * Even single event sets the 'enabled' for evlist,
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id B2857C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:42 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 9AB3B61101
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:42 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233851AbhHUJVT (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:19 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34154 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234216AbhHUJUv (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:51 -0400
Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C733AC0612A3;
        Sat, 21 Aug 2021 02:20:05 -0700 (PDT)
Received: by mail-ed1-x52b.google.com with SMTP id g21so17634290edw.4;
        Sat, 21 Aug 2021 02:20:05 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=GdLOUQfMiSvyr/krR4mQnx8RhZT43Ru8R5OO/VTr+4g=;
        b=oex+PMhEWJc4qwt8Ggjweiaj75IMXWNcxKWGsglVkLTpLuRsDVhhn9Wih1vYhRL6uf
         LAC+2aZENq3Dtc+KwR9Lt7gGQSGV+pDfGS8r+J3PeoTRCvpV5XXrjquIgDFvqAeMrAS3
         9oiakVOvaZiFkni2m8E1FK2cu60iW+4wLCIBem/tQVDSffnmj8La6mCuj/UftextcpKu
         LAVtX8rKzltLdnPy4zAtpMvZb9yJTWhkmOdSc3zZKkbXO1IZ+Wh9BY/CXeoUfqdub1VJ
         zgpzgEUvNjZcdKNbCfyJI/ZBaRChM35Hh2Ls4pfU0hWowUWGSlInw6IjIqt2ovkU5IbJ
         tnDg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=GdLOUQfMiSvyr/krR4mQnx8RhZT43Ru8R5OO/VTr+4g=;
        b=TS3NuNSexre3XEqxfRLfyX0xuqC88AAW3NAr3kwuTSl4k5Yw6DWNTPZlj+3+2b1RMK
         IneIbaVrOXjCK29Whe28ttRK4pZLuVl/ZDYz6SVXiydHw6FbWhOhO1JwEh0Q9ZaEhR6X
         9XbY8A+Tn8ryMBcsDdE9aIR6RBMG//yRGVBrmTvS3c9GUrJ/zfjxr1XGg7bclyOKG0Uy
         dAQcYr2jHeqUjJg2mOcSRk1Do0qZclgZ9NSDPso3s4HSWhz85y8sLR0a6OiKZhHgs3h9
         nk480nE0eQ05nVGYY7+wsT6mDIF5Il5P7baEDdBHalr4C2h+OabV2ie05cQYFxUocVUO
         ONbQ==
X-Gm-Message-State: AOAM532mcYlrkj43o7cnxBryE6c/JQsOoLQqww/CiJGNbs5osflqA4MA
        /WGlHeU75PmFoN/mhu439WI=
X-Google-Smtp-Source: ABdhPJwv43GnPtCNdOqMO5P90nO8K3zv3Bm0H/mwUHij9kQ5Xq0T+kmsYbhkxKklIBPKKLg9Oq7N8Q==
X-Received: by 2002:aa7:ce87:: with SMTP id y7mr26928301edv.306.1629537604387;
        Sat, 21 Aug 2021 02:20:04 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.03
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:04 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 13/37] perf evlist: add multithreading to evlist__close
Date:   Sat, 21 Aug 2021 11:19:19 +0200
Message-Id: <5473d8d537e30b3fd5333038dc5fdd25ba027d06.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

In this patch, evlist__for_each_evsel_cpu is used in evlist__close to
allow it to run in parallel.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evlist.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index fbe2d6ed8ecc8f21..f0a839107e8849bf 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1340,11 +1340,17 @@ void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
 	evlist->selected = evsel;
 }
 
+static int __evlist__close_evsel_cpu_func(struct evlist *evlist __maybe_unused,
+					struct evsel *evsel, int cpu,
+					void *args __maybe_unused)
+{
+	perf_evsel__close_cpu(&evsel->core, cpu);
+	return 0;
+}
+
 void evlist__close(struct evlist *evlist)
 {
 	struct evsel *evsel;
-	struct affinity affinity;
-	int cpu, i, cpu_idx;
 
 	/*
 	 * With perf record core.cpus is usually NULL.
@@ -1356,19 +1362,8 @@ void evlist__close(struct evlist *evlist)
 		return;
 	}
 
-	if (affinity__setup(&affinity) < 0)
-		return;
-	evlist__for_each_cpu(evlist, i, cpu) {
-		affinity__set(&affinity, cpu);
+	evlist__for_each_evsel_cpu(evlist, __evlist__close_evsel_cpu_func, NULL);
 
-		evlist__for_each_entry_reverse(evlist, evsel) {
-			cpu_idx = evsel__find_cpu(evsel, cpu);
-			if (cpu_idx < 0)
-				continue;
-			perf_evsel__close_cpu(&evsel->core, cpu_idx);
-		}
-	}
-	affinity__cleanup(&affinity);
 	evlist__for_each_entry_reverse(evlist, evsel) {
 		perf_evsel__free_fd(&evsel->core);
 		perf_evsel__free_id(&evsel->core);
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id F0123C432BE
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:49 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id D9DDD61209
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:20:49 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234216AbhHUJV0 (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:26 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34110 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234295AbhHUJUw (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:20:52 -0400
Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 408A5C0612AC;
        Sat, 21 Aug 2021 02:20:07 -0700 (PDT)
Received: by mail-ej1-x62c.google.com with SMTP id h9so25216803ejs.4;
        Sat, 21 Aug 2021 02:20:07 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=TQW9vFf14CSQ8/UctEQPnfJzPFIegK8ITMd5pacLu0s=;
        b=oPSURToHwIk6vpDnWikcvdiqa3NRcqB5AjozmrGNhz5bHbGqpeygl/kT/OUJcVSx60
         8RaU/BSf0v4e317mZGJ+rBF71qk1Gvd6Vfqsu3Nt3AW4IlX9IjNMGx9/8TeqVMHUxNwv
         4XKpBPmyQqWC4FihySXcEAO0lyt5Ym8DzU9ESlL63+KJyisG4VQtWJKSOQ1NWL6OuKeE
         05/lNgfDdCmTzL3tsDg73k+gLEF0X4stcJUg7TxWDxS5VEcoOPAse9qiZ+5nawd9KzDx
         +qDZBj/2OggNBISVQAdHuwUsJZA7HfRa9iuIUBJs13N9+3XBd+nnoh9eAcv9UqFF2yxP
         crTg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=TQW9vFf14CSQ8/UctEQPnfJzPFIegK8ITMd5pacLu0s=;
        b=ZV+eDjWT2ZCNaQ+t/xq1H7RRdmxd/UjqSgf0prM8BMYlq9dbWYPtgJczh8Wunt0kZS
         ZPJz/XkjGosjaPcJmLmcIY0jVcXk9bCt5XIwcCMYp3TGhMy8z3VZ+w5kfILeE0efi/+k
         +VD0IG5rid5N/2/jDvI7RlUDqjeDwqlvcfLcEHVpXYrkgGKZzpRRmmOEzmVAZ+JKWKLk
         NAhG4zfFenbOM7Vkm6Yj38Z+1iipGblezFuYtUoJSfhG8/hz4oa0tdwsHcfR4ImoPi2a
         SuTDAGglUYMV8j4TKvx9/4tuCbXn9fQfsDFReWd+NyBee3awHhIihT0gB/DDl/1PyW4p
         gSCA==
X-Gm-Message-State: AOAM533E5rwMd/uSKJa5QIAwgw0UEtwuZQppJofPNA1awOiQrK+Ou56H
        R9RV1+rvUE5yd8BCENgGxko=
X-Google-Smtp-Source: ABdhPJzIRHMGLNKiGZP2u9AbH/A1Whdva6pfc1wbHv+YzGJR33ApWIVgLpHZjiauuoji7WBa0ybsPQ==
X-Received: by 2002:a17:906:eda3:: with SMTP id sa3mr26444608ejb.451.1629537605773;
        Sat, 21 Aug 2021 02:20:05 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.04
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:05 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 14/37] perf evsel: remove retry_sample_id goto label
Date:   Sat, 21 Aug 2021 11:19:20 +0200
Message-Id: <340af0d03408d6621fd9c742e311db18b3585b3b.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

As far as I can tell, there is no good reason, apart from optimization
to have the retry_sample_id separate from fallback_missing_features.
Probably, this label was added to avoid reapplying patches for missing
features that had already been applied.
However, missing features that have been added later have not used this
optimization, always jumping to fallback_missing_features and reapplying
all missing features.

This patch removes that label, replacing it with
fallback_missing_features.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index f61e5dd53f5d2859..7b4bb3229a16524e 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1825,7 +1825,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		evsel->core.attr.bpf_event = 0;
 	if (perf_missing_features.branch_hw_idx)
 		evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX;
-retry_sample_id:
 	if (perf_missing_features.sample_id_all)
 		evsel->core.attr.sample_id_all = 0;
 
@@ -2006,7 +2005,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	} else if (!perf_missing_features.sample_id_all) {
 		perf_missing_features.sample_id_all = true;
 		pr_debug2_peo("switching off sample_id_all\n");
-		goto retry_sample_id;
+		goto fallback_missing_features;
 	} else if (!perf_missing_features.lbr_flags &&
 			(evsel->core.attr.branch_sample_type &
 			 (PERF_SAMPLE_BRANCH_NO_CYCLES |
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 06974C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:02 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id DDCA561101
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:01 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234321AbhHUJVk (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:40 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34088 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233806AbhHUJVP (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:15 -0400
Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A42CC06124C;
        Sat, 21 Aug 2021 02:20:08 -0700 (PDT)
Received: by mail-ej1-x633.google.com with SMTP id ia27so2990907ejc.10;
        Sat, 21 Aug 2021 02:20:08 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=BJSUm8Y9WnxzFwDFjQxHNxBZXFxJQPdQmlVoLoTFeKI=;
        b=F3uSEingtZ8e2chaZYz9uYo2cTkvJWXNwlgxioO9kGhbAhoJKJhNipWw0o9rWStTzV
         RpZZJE1XvmbaVlHEWfapDpAg24tCNWhNNQJnBhDaBtgndh8XFnTvIwCTM2EWyMewMkPo
         /igCQT7wVgYn0BE1aJWEzqNOEHjQEYtxkz52VbnjiMx5At0Nlf9doLbgFzFmvrbEjoo+
         c+WZtAKgwiM+PUkfu0YwmUgf4zbqmgN7Zrg8xjvUTd1NypFWJsfWVw6k8ZqowqeT11u+
         BidrcuPhZ74fpLlBRtAz2D2jRnABTuckS12vIdwQaoy0T76sy3H//rE3ci3fbGNKNZdd
         Q3Sg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=BJSUm8Y9WnxzFwDFjQxHNxBZXFxJQPdQmlVoLoTFeKI=;
        b=WhOntWXsy9wEBSoib79oc/2Uyg+9HlTrFzVaXMEEuD35g4SnaAdZ/+CT+hsMmaG+wu
         WgY3ngy6DaBIBmum1tbr6PywvzHwyENYsplg4ccYGonkbxk1W5oh0P6jLDkVaPKpCJJP
         qWBDE46RgteHe2YnuFFsiCw7n+CX2jfEZPm0/sYglJhYl2gcErGRJNIXCrW0Xrz1FDVG
         TI3brDmUVfOZCsbSS/3w4JPqDi9E1756OHwWelUMWyvwB8JA6ix+E8qYOP7U83Aix8T4
         JQburLmPlqFwYVvDV7DdJBYaUn6zSBPdXysqfB8WqOXI310eO/amcCyNXC9btyGG8Xk+
         oN6A==
X-Gm-Message-State: AOAM533iI6IVhrwVqLiD/opclEb/yLJQFjzYrsz9eSgf2hrpPK75SwaH
        Q5zG5G0+Uf+dBCt7+KcCCow=
X-Google-Smtp-Source: ABdhPJwOSjD4YnXLgM3rfVn2z0sRLUryjEgikOKbTOTa/LMfKaA5kNIsoN/FfqFjZuYHxvvjdl7rng==
X-Received: by 2002:a17:906:405:: with SMTP id d5mr25680829eja.189.1629537606974;
        Sat, 21 Aug 2021 02:20:06 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.05
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:06 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 15/37] perf evsel: separate open preparation from open itself
Date:   Sat, 21 Aug 2021 11:19:21 +0200
Message-Id: <e14118b934c338dbbf68b8677f20d0d7dbf9359a.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This is a preparatory patch for the following patches with the goal to
separate in evlist__open_cpu the actual perf_event_open, which could be
performed in parallel, from the existing fallback mechanisms, which
should be handled sequentially.

This patch separates the first lines of evsel__open_cpu into a new
__evsel__prepare_open function.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 45 +++++++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 11 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 7b4bb3229a16524e..ddf324e2e17a0951 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1746,22 +1746,20 @@ static int perf_event_open(struct evsel *evsel,
 	return fd;
 }
 
-static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
-		struct perf_thread_map *threads,
-		int start_cpu, int end_cpu)
+
+static struct perf_cpu_map *empty_cpu_map;
+static struct perf_thread_map *empty_thread_map;
+
+static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
+		struct perf_thread_map *threads)
 {
-	int cpu, thread, nthreads;
-	unsigned long flags = PERF_FLAG_FD_CLOEXEC;
-	int pid = -1, err, old_errno;
-	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
+	int nthreads;
 
 	if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
 	    (perf_missing_features.aux_output     && evsel->core.attr.aux_output))
 		return -EINVAL;
 
 	if (cpus == NULL) {
-		static struct perf_cpu_map *empty_cpu_map;
-
 		if (empty_cpu_map == NULL) {
 			empty_cpu_map = perf_cpu_map__dummy_new();
 			if (empty_cpu_map == NULL)
@@ -1772,8 +1770,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	}
 
 	if (threads == NULL) {
-		static struct perf_thread_map *empty_thread_map;
-
 		if (empty_thread_map == NULL) {
 			empty_thread_map = thread_map__new_by_tid(-1);
 			if (empty_thread_map == NULL)
@@ -1792,6 +1788,33 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	    perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0)
 		return -ENOMEM;
 
+	return 0;
+}
+
+static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
+		struct perf_thread_map *threads,
+		int start_cpu, int end_cpu)
+{
+	int cpu, thread, nthreads;
+	unsigned long flags = PERF_FLAG_FD_CLOEXEC;
+	int pid = -1, err, old_errno;
+	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
+
+	err = __evsel__prepare_open(evsel, cpus, threads);
+	if (err)
+		return err;
+
+	if (cpus == NULL)
+		cpus = empty_cpu_map;
+
+	if (threads == NULL)
+		threads = empty_thread_map;
+
+	if (evsel->core.system_wide)
+		nthreads = 1;
+	else
+		nthreads = threads->nr;
+
 	if (evsel->cgrp) {
 		flags |= PERF_FLAG_PID_CGROUP;
 		pid = evsel->cgrp->fd;
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id B6E67C4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:02 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 9E34461101
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:02 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234385AbhHUJVk (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:40 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34140 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234073AbhHUJVQ (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:16 -0400
Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDD6BC061226;
        Sat, 21 Aug 2021 02:20:09 -0700 (PDT)
Received: by mail-ej1-x633.google.com with SMTP id bt14so25229171ejb.3;
        Sat, 21 Aug 2021 02:20:09 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=K06lN51zooeraOtXEnuTstuj3NqHpgaRejWVBk4KyOk=;
        b=PGI+h7CA1GqZcp4I1rAnRgvjggxCainerJwwcK2u4W8y/JqKLtiE4yBNPAF1aLnLI2
         mzYHhOBesppnvlNDBLvrlBtZ3HtN5yViQa1qAhxvbIYbTExwoHbvwS+TzGTObAXknsuY
         I3dEfvBhLmiHg8Du7CugiOSdi0Nl7CXb1lka+gP37UJ/9muyKhKZtzg3E0saCHlLm0Pv
         bB+ljE9Ctzqllh1W08dZLQRS04IihAx7K1P0gHrJLAQFoQobCe/lr6Ku1ihCabX/KJq/
         QMe01khZWUY1Krhr75Redrn73SPJ9k7bQZplB/CkG7HPHEWStw+4Ttl990zuXQBLAjD1
         UG2g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=K06lN51zooeraOtXEnuTstuj3NqHpgaRejWVBk4KyOk=;
        b=odLnZPLCIletJhoICxpxwB8qKKwKLve8HwzLShih8XjbbqhGnZjl/vZOXMeS5elk6i
         K5LRfaxhYE3AAUDIaEIqiozvsc5AVQJg4kNcksDzQ3NXzc3Qwiab8OUbymeuCs3hcVZD
         ZTt6n2jTMtvWdGPBpkT4lepwRdRNQ/lhQS35/uwBKqrffnqzE9cug05RjumTrYjDTym9
         wuFSFOZzxdE/tY0tjwJXqwcXS9Oqq31XGb+1pjycwnExbIKqyrU7JTCVTYXEFKeH+0TW
         LYdmM7qbV44w0Y394ubyyCeppOiXZBS9HwjPB5kBUAmpjytyceME9d9prFT9InReVbxP
         sbSg==
X-Gm-Message-State: AOAM5316VM9VVWzLMs+xh0j3YKqOUGSLtc83j4iclYafIGhEDGasXffc
        UdZalCPI80+qyM6UQ7vDFWU=
X-Google-Smtp-Source: ABdhPJwt8uU784n9hXFhioDFuLC0yISYbl4g86peqol/mnB6pyTeYBtayK+NLczWBOODuS4ItMlpYQ==
X-Received: by 2002:a17:906:e57:: with SMTP id q23mr25767665eji.483.1629537608295;
        Sat, 21 Aug 2021 02:20:08 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.07
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:08 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 16/37] perf evsel: save open flags in evsel
Date:   Sat, 21 Aug 2021 11:19:22 +0200
Message-Id: <d9f63159098e56fa518eecf25171d72e6f74df37.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch caches the flags used in perf_event_open inside evsel, so
that they can be set in __evsel__prepare_open (this will be useful
in following patches, when the fallback mechanisms will be handled
outside the open itself).
This also optimizes the code, by not having to recompute them everytime.

Since flags are now saved in evsel, the flags argument in
perf_event_open is removed.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 24 ++++++++++++------------
 tools/perf/util/evsel.h |  1 +
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index ddf324e2e17a0951..509a2970a94b3142 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1710,17 +1710,16 @@ static void display_attr(struct perf_event_attr *attr)
 }
 
 static int perf_event_open(struct evsel *evsel,
-			   pid_t pid, int cpu, int group_fd,
-			   unsigned long flags)
+			   pid_t pid, int cpu, int group_fd)
 {
 	int precise_ip = evsel->core.attr.precise_ip;
 	int fd;
 
 	while (1) {
 		pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
-			  pid, cpu, group_fd, flags);
+			  pid, cpu, group_fd, evsel->open_flags);
 
-		fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, flags);
+		fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, evsel->open_flags);
 		if (fd >= 0)
 			break;
 
@@ -1788,6 +1787,10 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	    perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0)
 		return -ENOMEM;
 
+	evsel->open_flags = PERF_FLAG_FD_CLOEXEC;
+	if (evsel->cgrp)
+		evsel->open_flags |= PERF_FLAG_PID_CGROUP;
+
 	return 0;
 }
 
@@ -1796,7 +1799,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		int start_cpu, int end_cpu)
 {
 	int cpu, thread, nthreads;
-	unsigned long flags = PERF_FLAG_FD_CLOEXEC;
 	int pid = -1, err, old_errno;
 	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
 
@@ -1815,10 +1817,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	else
 		nthreads = threads->nr;
 
-	if (evsel->cgrp) {
-		flags |= PERF_FLAG_PID_CGROUP;
+	if (evsel->cgrp)
 		pid = evsel->cgrp->fd;
-	}
 
 fallback_missing_features:
 	if (perf_missing_features.weight_struct) {
@@ -1832,7 +1832,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		evsel->core.attr.clockid = 0;
 	}
 	if (perf_missing_features.cloexec)
-		flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
+		evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
 	if (perf_missing_features.mmap2)
 		evsel->core.attr.mmap2 = 0;
 	if (perf_missing_features.exclude_guest)
@@ -1866,7 +1866,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 			test_attr__ready();
 
 			fd = perf_event_open(evsel, pid, cpus->map[cpu],
-					     group_fd, flags);
+					     group_fd);
 
 			FD(evsel, cpu, thread) = fd;
 
@@ -1874,7 +1874,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 			if (unlikely(test_attr__enabled)) {
 				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
-						fd, group_fd, flags);
+						fd, group_fd, evsel->open_flags);
 			}
 
 			if (fd < 0) {
@@ -2012,7 +2012,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		perf_missing_features.clockid = true;
 		pr_debug2_peo("switching off use_clockid\n");
 		goto fallback_missing_features;
-	} else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
+	} else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) {
 		perf_missing_features.cloexec = true;
 		pr_debug2_peo("switching off cloexec flag\n");
 		goto fallback_missing_features;
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index eabccce406886320..1c0057e80d080f2f 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -149,6 +149,7 @@ struct evsel {
 		struct bperf_leader_bpf *leader_skel;
 		struct bperf_follower_bpf *follower_skel;
 	};
+	unsigned long		open_flags;
 };
 
 struct perf_missing_features {
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id D862CC4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:06 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id BFE4D611EF
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:06 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233765AbhHUJVl (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:41 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34142 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234117AbhHUJVQ (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:16 -0400
Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 144EAC0613CF;
        Sat, 21 Aug 2021 02:20:11 -0700 (PDT)
Received: by mail-ej1-x631.google.com with SMTP id d11so25134679eja.8;
        Sat, 21 Aug 2021 02:20:11 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=2bmu3Fw9zmgDwWJcJ4+5MOZxCKRd0DdpkeYuxgu64FQ=;
        b=BZ0kBqFERbWnZHrCk4EMplslRN4zRsOzoj7eeWeGHoHcNqT56Rk2D1nu1rPk+O/8n2
         EGh8oS9XuM8H3wlXq6MAJxukf5fLXy867ewCIqIe5RU3lujHHBK0JtFt05UsZGipYLQR
         97qp9japXOXLaLq0RT72RMnc/Zsp62qtyBiYswImys9IhtLKpT6NW8HER4K5NEy7gJYE
         PF6sZxVcM0Il6hdY/kyF2LeLAk4VpN7LIxXk2RlhKszkDA3crwwLn+/tRJb3LsxkvtS2
         YuviLTREkjyHRm2ElVPdXU55R1v/r/uVzgpxfcWL4Mxb7P4Vzm2zM1FwHjd582U5RG7I
         4wxQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=2bmu3Fw9zmgDwWJcJ4+5MOZxCKRd0DdpkeYuxgu64FQ=;
        b=ZQfJEI2z3ouzksPRfrsexX36a/ekb2rl0n/fgJcg7m8hQ/nvaQRTUFyaDsZIRhd1YT
         Drc4g7q8e792ZL9zIfQBzMcTdkE8zZstrXvPfb8X0zIF+WHFbyT1zOoJryyfNg+rLR9t
         RS5ar/hCCIbxwixhuXzLN2s7DKLmddNJijo37XWCzJSHs3Fi0ChIALhqUdlsB6hybqLo
         EVtvb/8IV8ezic0OlQxcpwfrTHf1Ywi4u3jnAVqblCQSbA29Wy3J0HyVfXa1zbF1F/Jm
         13SzIgqrfA66Vz86QHq+919cieBUnW/yFY4UKn4/fAaBlUW+NTF9x1kQN17LgyEm9NRM
         m4Og==
X-Gm-Message-State: AOAM531RqyahX3J2rBbIuoqQOaYkkucWrnCpKH7Osb18wdtUrSDWSYhj
        Qs3j4EAl25tnjsxNGli/dvLkEL9cL8GVsL4wDxM=
X-Google-Smtp-Source: ABdhPJzWtsFt2DdMrGwdN1/CTaXlQRRznR3ZMo/bJErIajlOkyiWCXGK6K3D0vzqnjtr5+FCSX+jTQ==
X-Received: by 2002:a17:906:dc51:: with SMTP id yz17mr2036023ejb.498.1629537609566;
        Sat, 21 Aug 2021 02:20:09 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.08
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:09 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 17/37] perf evsel: separate missing feature disabling from evsel__open_cpu
Date:   Sat, 21 Aug 2021 11:19:23 +0200
Message-Id: <48138bd2932646dde315505da733c2ca635ad2ee.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This is a preparatory patch for the following patches with the goal to
separate in evlist__open_cpu the actual opening, which could be
performed in parallel, from the existing fallback mechanisms, which
should be handled sequentially.

This patch separates the disabling of missing features from
evlist__open_cpu into a new function evsel__disable_missing_features.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 57 ++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 26 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 509a2970a94b3142..f0bc89f743915800 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1794,33 +1794,8 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	return 0;
 }
 
-static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
-		struct perf_thread_map *threads,
-		int start_cpu, int end_cpu)
+static void evsel__disable_missing_features(struct evsel *evsel)
 {
-	int cpu, thread, nthreads;
-	int pid = -1, err, old_errno;
-	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
-
-	err = __evsel__prepare_open(evsel, cpus, threads);
-	if (err)
-		return err;
-
-	if (cpus == NULL)
-		cpus = empty_cpu_map;
-
-	if (threads == NULL)
-		threads = empty_thread_map;
-
-	if (evsel->core.system_wide)
-		nthreads = 1;
-	else
-		nthreads = threads->nr;
-
-	if (evsel->cgrp)
-		pid = evsel->cgrp->fd;
-
-fallback_missing_features:
 	if (perf_missing_features.weight_struct) {
 		evsel__set_sample_bit(evsel, WEIGHT);
 		evsel__reset_sample_bit(evsel, WEIGHT_STRUCT);
@@ -1850,6 +1825,36 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX;
 	if (perf_missing_features.sample_id_all)
 		evsel->core.attr.sample_id_all = 0;
+}
+
+static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
+		struct perf_thread_map *threads,
+		int start_cpu, int end_cpu)
+{
+	int cpu, thread, nthreads;
+	int pid = -1, err, old_errno;
+	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
+
+	err = __evsel__prepare_open(evsel, cpus, threads);
+	if (err)
+		return err;
+
+	if (cpus == NULL)
+		cpus = empty_cpu_map;
+
+	if (threads == NULL)
+		threads = empty_thread_map;
+
+	if (evsel->core.system_wide)
+		nthreads = 1;
+	else
+		nthreads = threads->nr;
+
+	if (evsel->cgrp)
+		pid = evsel->cgrp->fd;
+
+fallback_missing_features:
+	evsel__disable_missing_features(evsel);
 
 	display_attr(&evsel->core.attr);
 
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id C27D0C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:12 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id AB11761101
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:12 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234447AbhHUJVr (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:47 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34070 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234431AbhHUJVS (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:18 -0400
Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36482C06179A;
        Sat, 21 Aug 2021 02:20:12 -0700 (PDT)
Received: by mail-ed1-x52c.google.com with SMTP id s25so4933431edw.0;
        Sat, 21 Aug 2021 02:20:12 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=/cXCUrZyJrSXhlY2buOOLYVNdgfOW9khyRbxBZB9LKU=;
        b=NKl0D4hS5TgUd757ZzzuL6NG8v3TvbZa1fdRJuOXQsnhL8YWP+PBmfc7Pa3/umlC8n
         ZFozMkcSYSTW05ngDMNqMASPmiQWXMwAUVrC0F42r9qCCs5V1nPikMsnDkT0/9Hvq7cK
         h+D63O9VduzhNF3uY5Tf/SBEefx06Wc6SFV2smWwtHBDG+wtyqid7QUOhFyAf5J3mt0P
         yWsn/ZKuEiaQ2a9yG/bKlyq9fmaK+qmmzwnw7f2n8HB5hxysJrXHbQYP9HEpSiuwY/9K
         fpJm2Bsk3sWwkTYef8YGMCJGdWlXQprwJ68v5xylCX1c9JMWTnXFrswS38Mg1kkUgk0y
         DOWg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=/cXCUrZyJrSXhlY2buOOLYVNdgfOW9khyRbxBZB9LKU=;
        b=o5HPEGFvBDWltriq3lYy8sdYgxGfczkCJo6IJ2TD5mPSe8OiqjYU8gZ9PvEfS7/QQR
         WmVN6sbTuLjb6HQzSfoLSVYIMH924LsZerv1zRCeCggKzC2zBQV0Fopw/W2Y/Zjz7n2G
         k+Xvc1bmbKdmx1fmaVv1IJR7kJ0Xb7W8SxbXLB4oAz/3w+Rnk+Qr5wazlfkHde8XWU58
         e6pKWpMspj+YuIgG8QOppAtGuqP3HeJbDYBSrVxJAmSIs8FFTrVMwCkGp3HZ+JtVbAE7
         YjndRH/NJQDrSAHrWKRpyYVZcFiKj85nvCVbRAhiwTjS8xk9zw1EKTOYzsq/qiVuG+of
         7wjQ==
X-Gm-Message-State: AOAM533fWkbr0JxsuhzcdNUiBlrhMET5ITmaLa/ysye0fs2ns9LqnlID
        9PIPakKf55OkrdUZQTID0jc=
X-Google-Smtp-Source: ABdhPJyrmCVUVKDCSeUyrd72rE8kDtU2kEI8A+skQ5Ur4cGEy4U7C0/OtaTP8ki/jiRGJRGbewaNxQ==
X-Received: by 2002:a50:cc0b:: with SMTP id m11mr26358893edi.96.1629537610818;
        Sat, 21 Aug 2021 02:20:10 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.09
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:10 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 18/37] perf evsel: add evsel__prepare_open function
Date:   Sat, 21 Aug 2021 11:19:24 +0200
Message-Id: <fa5e78bbb92c848226f044278fdcf777b3ce4583.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This function will prepare the evsel and disable the missing features.
It will be used in one of the following patches.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 14 ++++++++++++++
 tools/perf/util/evsel.h |  2 ++
 2 files changed, 16 insertions(+)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index f0bc89f743915800..4e9a3e62075305f1 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1827,6 +1827,20 @@ static void evsel__disable_missing_features(struct evsel *evsel)
 		evsel->core.attr.sample_id_all = 0;
 }
 
+int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
+		struct perf_thread_map *threads)
+{
+	int err;
+
+	err = __evsel__prepare_open(evsel, cpus, threads);
+	if (err)
+		return err;
+
+	evsel__disable_missing_features(evsel);
+
+	return err;
+}
+
 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads,
 		int start_cpu, int end_cpu)
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 1c0057e80d080f2f..58aa998e1814ac9e 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -286,6 +286,8 @@ int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads)
 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads);
 void evsel__close(struct evsel *evsel);
+int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
+		struct perf_thread_map *threads);
 
 struct perf_sample;
 
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 4DF1FC4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:21 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 328B1611EF
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:21 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234183AbhHUJV7 (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:59 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34160 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234540AbhHUJVZ (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:25 -0400
Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB050C061756;
        Sat, 21 Aug 2021 02:20:13 -0700 (PDT)
Received: by mail-ed1-x52b.google.com with SMTP id i6so17675032edu.1;
        Sat, 21 Aug 2021 02:20:13 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=Ll29t2ph8DtISGKSm4m+CMcxpNqDMVgepL0wvoiS6hU=;
        b=CgzTartnOriqkGirMWqliPXDGH5PGCpv+D/qdUW1i9fR2s1C9BtC2cF/XoXNLyrryq
         8ZHAbf1hyn8sZnmVeVIOyUMYuIAg4gAh6efjV9jYVmw3GfYQKQwkHj3/k5q9Vl28Be4m
         rEeIVkBRbliXGfTxH1OV3TJTJrLEPK+/0kcIZn6SvUaybQ0Q9kjZhcbujeOaAvF+U3nq
         ZYiGujReVBp/1x/7VcahP4s4zMPAR4yIN30QFAQ/HEzr/Qdnhht2mLkf63aoQS/tsrZg
         fCjtrmDk3pXap01DO9QDvSYmxn0+O0Z8BQ1p9H8lSeTMIk6MuCPXuRmmZC2XwK82Fu27
         VK1w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=Ll29t2ph8DtISGKSm4m+CMcxpNqDMVgepL0wvoiS6hU=;
        b=iCRqDeoeI0/Fi/GKbw4PYu7K+J5rQNe9WosSGlW0SICdwHqVbDHQ4Bd9WjrEg449yH
         pIN15h1OeKWX5NvJIdmL7qUReQKJn0bggXfHtndjuiGbfgGfPdyfJOv969j/V4DQpAfx
         AOlFwoxUKSQN8xFVUmed88KyWigcBXO1ZWiR8Ze6L3Ec4y/8DXxaa9+JlIXMAaE7wq7M
         OrTRmTBdAh3cFJRIGe7JaUeEJz9D5WuDMlppVvZWvgRvYFEeUc26UsLc4XNjxzU6EFNW
         3agF4d9jwIaZWFHqBrDsP6OtnfH5db+nJ7peG/KhLk8eilyK+YQCKUh1TPk6qBObBn+R
         h/RA==
X-Gm-Message-State: AOAM5325KySwxinFzUQZOh7yOac5uEEauosUZApt+fqPNBqaMp43Sk0G
        Z9miFWhpku9iPoXmzEV/jYUWWb8u8IIe4AuWleY=
X-Google-Smtp-Source: ABdhPJykrvcRa/raNWIGISNIyfzOPR8QcM4B5ud6L6Py1INPYHktiIl58OUXtTtS6tgdLBe/JypO9g==
X-Received: by 2002:aa7:c2da:: with SMTP id m26mr7579123edp.351.1629537612166;
        Sat, 21 Aug 2021 02:20:12 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.10
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:11 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 19/37] perf evsel: separate missing feature detection from evsel__open_cpu
Date:   Sat, 21 Aug 2021 11:19:25 +0200
Message-Id: <cba0b7d939862473662adeedb0f9c9b69566ee9a.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This is a preparatory patch for the following patches with the goal to
separate in evlist__open_cpu the actual opening, which could be
performed in parallel, from the existing fallback mechanisms, which
should be handled sequentially.

This patch separates the missing feature detection in evsel__open_cpu
into a new evsel__detect_missing_features function.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 174 +++++++++++++++++++++-------------------
 tools/perf/util/evsel.h |   1 +
 2 files changed, 92 insertions(+), 83 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 4e9a3e62075305f1..c393bd992322d925 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1841,6 +1841,96 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	return err;
 }
 
+bool evsel__detect_missing_features(struct evsel *evsel)
+{
+	/*
+	 * Must probe features in the order they were added to the
+	 * perf_event_attr interface.
+	 */
+	if (!perf_missing_features.weight_struct &&
+	    (evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) {
+		perf_missing_features.weight_struct = true;
+		pr_debug2("switching off weight struct support\n");
+		return true;
+	} else if (!perf_missing_features.code_page_size &&
+	    (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)) {
+		perf_missing_features.code_page_size = true;
+		pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support, bailing out\n");
+		return false;
+	} else if (!perf_missing_features.data_page_size &&
+	    (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)) {
+		perf_missing_features.data_page_size = true;
+		pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support, bailing out\n");
+		return false;
+	} else if (!perf_missing_features.cgroup && evsel->core.attr.cgroup) {
+		perf_missing_features.cgroup = true;
+		pr_debug2_peo("Kernel has no cgroup sampling support, bailing out\n");
+		return false;
+	} else if (!perf_missing_features.branch_hw_idx &&
+	    (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX)) {
+		perf_missing_features.branch_hw_idx = true;
+		pr_debug2("switching off branch HW index support\n");
+		return true;
+	} else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
+		perf_missing_features.aux_output = true;
+		pr_debug2_peo("Kernel has no attr.aux_output support, bailing out\n");
+		return false;
+	} else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) {
+		perf_missing_features.bpf = true;
+		pr_debug2_peo("switching off bpf_event\n");
+		return true;
+	} else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
+		perf_missing_features.ksymbol = true;
+		pr_debug2_peo("switching off ksymbol\n");
+		return true;
+	} else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
+		perf_missing_features.write_backward = true;
+		pr_debug2_peo("switching off write_backward\n");
+		return false;
+	} else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
+		perf_missing_features.clockid_wrong = true;
+		pr_debug2_peo("switching off clockid\n");
+		return true;
+	} else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
+		perf_missing_features.clockid = true;
+		pr_debug2_peo("switching off use_clockid\n");
+		return true;
+	} else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) {
+		perf_missing_features.cloexec = true;
+		pr_debug2_peo("switching off cloexec flag\n");
+		return true;
+	} else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
+		perf_missing_features.mmap2 = true;
+		pr_debug2_peo("switching off mmap2\n");
+		return true;
+	} else if (!perf_missing_features.exclude_guest &&
+		   (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
+		perf_missing_features.exclude_guest = true;
+		pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+		return true;
+	} else if (!perf_missing_features.sample_id_all) {
+		perf_missing_features.sample_id_all = true;
+		pr_debug2_peo("switching off sample_id_all\n");
+		return true;
+	} else if (!perf_missing_features.lbr_flags &&
+			(evsel->core.attr.branch_sample_type &
+			 (PERF_SAMPLE_BRANCH_NO_CYCLES |
+			  PERF_SAMPLE_BRANCH_NO_FLAGS))) {
+		perf_missing_features.lbr_flags = true;
+		pr_debug2_peo("switching off branch sample type no (cycles/flags)\n");
+		return true;
+	} else if (!perf_missing_features.group_read &&
+		    evsel->core.attr.inherit &&
+		   (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
+		   evsel__is_group_leader(evsel)) {
+		perf_missing_features.group_read = true;
+		pr_debug2_peo("switching off group read\n");
+		return true;
+	} else {
+		return false;
+	}
+}
+
 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads,
 		int start_cpu, int end_cpu)
@@ -1979,90 +2069,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	if (err != -EINVAL || cpu > 0 || thread > 0)
 		goto out_close;
 
-	/*
-	 * Must probe features in the order they were added to the
-	 * perf_event_attr interface.
-	 */
-	if (!perf_missing_features.weight_struct &&
-	    (evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) {
-		perf_missing_features.weight_struct = true;
-		pr_debug2("switching off weight struct support\n");
+	if (evsel__detect_missing_features(evsel))
 		goto fallback_missing_features;
-	} else if (!perf_missing_features.code_page_size &&
-	    (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)) {
-		perf_missing_features.code_page_size = true;
-		pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support, bailing out\n");
-		goto out_close;
-	} else if (!perf_missing_features.data_page_size &&
-	    (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)) {
-		perf_missing_features.data_page_size = true;
-		pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support, bailing out\n");
-		goto out_close;
-	} else if (!perf_missing_features.cgroup && evsel->core.attr.cgroup) {
-		perf_missing_features.cgroup = true;
-		pr_debug2_peo("Kernel has no cgroup sampling support, bailing out\n");
-		goto out_close;
-        } else if (!perf_missing_features.branch_hw_idx &&
-	    (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX)) {
-		perf_missing_features.branch_hw_idx = true;
-		pr_debug2("switching off branch HW index support\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
-		perf_missing_features.aux_output = true;
-		pr_debug2_peo("Kernel has no attr.aux_output support, bailing out\n");
-		goto out_close;
-	} else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) {
-		perf_missing_features.bpf = true;
-		pr_debug2_peo("switching off bpf_event\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
-		perf_missing_features.ksymbol = true;
-		pr_debug2_peo("switching off ksymbol\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
-		perf_missing_features.write_backward = true;
-		pr_debug2_peo("switching off write_backward\n");
-		goto out_close;
-	} else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
-		perf_missing_features.clockid_wrong = true;
-		pr_debug2_peo("switching off clockid\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
-		perf_missing_features.clockid = true;
-		pr_debug2_peo("switching off use_clockid\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) {
-		perf_missing_features.cloexec = true;
-		pr_debug2_peo("switching off cloexec flag\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
-		perf_missing_features.mmap2 = true;
-		pr_debug2_peo("switching off mmap2\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.exclude_guest &&
-		   (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
-		perf_missing_features.exclude_guest = true;
-		pr_debug2_peo("switching off exclude_guest, exclude_host\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.sample_id_all) {
-		perf_missing_features.sample_id_all = true;
-		pr_debug2_peo("switching off sample_id_all\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.lbr_flags &&
-			(evsel->core.attr.branch_sample_type &
-			 (PERF_SAMPLE_BRANCH_NO_CYCLES |
-			  PERF_SAMPLE_BRANCH_NO_FLAGS))) {
-		perf_missing_features.lbr_flags = true;
-		pr_debug2_peo("switching off branch sample type no (cycles/flags)\n");
-		goto fallback_missing_features;
-	} else if (!perf_missing_features.group_read &&
-		    evsel->core.attr.inherit &&
-		   (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
-		   evsel__is_group_leader(evsel)) {
-		perf_missing_features.group_read = true;
-		pr_debug2_peo("switching off group read\n");
-		goto fallback_missing_features;
-	}
 out_close:
 	if (err)
 		threads->err_thread = thread;
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 58aa998e1814ac9e..a83fb7f69b1ead73 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -288,6 +288,7 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
 void evsel__close(struct evsel *evsel);
 int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads);
+bool evsel__detect_missing_features(struct evsel *evsel);
 
 struct perf_sample;
 
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 27FF5C4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:22 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 166A161156
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:22 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234559AbhHUJV7 (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:21:59 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34106 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233924AbhHUJVZ (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:25 -0400
Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFFE3C061757;
        Sat, 21 Aug 2021 02:20:14 -0700 (PDT)
Received: by mail-ed1-x531.google.com with SMTP id i6so17675105edu.1;
        Sat, 21 Aug 2021 02:20:14 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=MOotWiEv+98ro2y3QGt/u7+sbNifTqAd54gUTUaSOVY=;
        b=WQwOgvKCgIfHmLnxQIvPieXmBZruWPCXgYrrC/NGtaFxT2VW5+VaGhuugXhZ/YjR+T
         kEarLJfNc3dOEIJM72h9fyK0clcpw/YclDgb7lRUpeGhCSVGfMmA6+PGzGLn+V2+5Lyx
         LKIeAXEqlW9I8//kwEpErGT9AolOUHfs9RYDCkfpIEfzHlbnkTBtvYXkwTwSfy61xfxu
         kbTreXMXdxqHSS32ZpMW8eD8X3rJ/m3/btXID3L147HN0XnMfs8UDRk/45mcr1U75zMc
         wSsfqIHvEW9cyDL38rYV9Khaa6MOD6F7uKeliwPSowNFL5K1Q5/vFiFLVt9iunMgzX9t
         cAqQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=MOotWiEv+98ro2y3QGt/u7+sbNifTqAd54gUTUaSOVY=;
        b=iEk5UVM0BBDvoSYWVYWfRD+eUWupbpT9vZfEIZSg1c6mICdK3+FJepKVzkQY3w/hHg
         vQbuLb6Ud43ZmKCJOcELr/btfiNByuDyALM1stsNkYO1vXMYklskrHZzR3q3xoMFF69e
         zxfHLMftLFRmx6dim6TcPLmgmQ5YJ9THDLT78SRAuYOUHx9e3VvV3GX1LssCEycsiOim
         sK7nONe/BX/XgKDtjMuovaHSPRFCX65h1D/C3KpA3kTt4zlkp6Ny1vy/hv3PjUlh8K8r
         SFlnNDMdnYV8oc9N3B2UjxFFUHBdj6MW0tKlKRiJ1VzyaHr9+xMm0YtNMFqk1IaX7Tv8
         sEHA==
X-Gm-Message-State: AOAM532ejhQbKzpcpRYzTvh8yIkF5IWgZaEZ6wQQEh8DS1wP6DT1j0ak
        cBiEzXFlCPaSOyxZPEwvfKg=
X-Google-Smtp-Source: ABdhPJyW5yl0n572PbavDjuw61RHEFo3Jr1zPq0cWqyh+WG1+bF2jDt5++WL9keCJy/THQJi5sHnVw==
X-Received: by 2002:a05:6402:344f:: with SMTP id l15mr6771949edc.56.1629537613394;
        Sat, 21 Aug 2021 02:20:13 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.12
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:13 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 20/37] perf evsel: separate rlimit increase from evsel__open_cpu
Date:   Sat, 21 Aug 2021 11:19:26 +0200
Message-Id: <2f256de8ec37b9809a5cef73c2fa7bce416af5d3.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This is a preparatory patch for the following patches with the goal to
separate from evlist__open_cpu the actual opening (which could be
performed in parallel), from the existing fallback mechanisms, which
should be handled sequentially.

This patch separates the rlimit increase from evsel__open_cpu.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 50 ++++++++++++++++++++++++-----------------
 tools/perf/util/evsel.h |  3 +++
 2 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index c393bd992322d925..916930ea31450265 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1931,13 +1931,40 @@ bool evsel__detect_missing_features(struct evsel *evsel)
 	}
 }
 
+bool evsel__increase_rlimit(enum rlimit_action *set_rlimit)
+{
+	int old_errno;
+	struct rlimit l;
+
+	if (*set_rlimit < INCREASED_MAX) {
+
+		old_errno = errno;
+		if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
+			if (*set_rlimit == NO_CHANGE)
+				l.rlim_cur = l.rlim_max;
+			else {
+				l.rlim_cur = l.rlim_max + 1000;
+				l.rlim_max = l.rlim_cur;
+			}
+			if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
+				(*set_rlimit) += 1;
+				errno = old_errno;
+				return true;
+			}
+		}
+		errno = old_errno;
+	}
+
+	return false;
+}
+
 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads,
 		int start_cpu, int end_cpu)
 {
 	int cpu, thread, nthreads;
 	int pid = -1, err, old_errno;
-	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
+	enum rlimit_action set_rlimit = NO_CHANGE;
 
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
@@ -2046,25 +2073,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	 * perf stat needs between 5 and 22 fds per CPU. When we run out
 	 * of them try to increase the limits.
 	 */
-	if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
-		struct rlimit l;
-
-		old_errno = errno;
-		if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
-			if (set_rlimit == NO_CHANGE)
-				l.rlim_cur = l.rlim_max;
-			else {
-				l.rlim_cur = l.rlim_max + 1000;
-				l.rlim_max = l.rlim_cur;
-			}
-			if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
-				set_rlimit++;
-				errno = old_errno;
-				goto retry_open;
-			}
-		}
-		errno = old_errno;
-	}
+	if (err == -EMFILE && evsel__increase_rlimit(&set_rlimit))
+		goto retry_open;
 
 	if (err != -EINVAL || cpu > 0 || thread > 0)
 		goto out_close;
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index a83fb7f69b1ead73..bf9abd9a5cbf9852 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -290,6 +290,9 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads);
 bool evsel__detect_missing_features(struct evsel *evsel);
 
+enum rlimit_action { NO_CHANGE, SET_TO_MAX, INCREASED_MAX };
+bool evsel__increase_rlimit(enum rlimit_action *set_rlimit);
+
 struct perf_sample;
 
 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name);
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 27B5AC4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:35 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 0D7A461186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:35 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234666AbhHUJWM (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:12 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34090 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233594AbhHUJVj (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:39 -0400
Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F08AC0611C1;
        Sat, 21 Aug 2021 02:20:16 -0700 (PDT)
Received: by mail-ed1-x529.google.com with SMTP id n12so17621850edx.8;
        Sat, 21 Aug 2021 02:20:16 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=Kt1ytqrFQ3gxDFuU2/KO3OHIat7YiLJUy+EkQoB0jDg=;
        b=VQUzkOsgyk04CQmPGLMYJKdX92MbEcNeSP7aW98PvA8QFuZP3rFpJykMWn30t1FYky
         byLp9t4MJOgoMxmlBmGRnlNhwoSxVQKPj1GCtHp3f2AjnJIL8vnofjYqngZIbMK/58Sv
         Oot81y+bSDY21kzySyVJcJnXkyYYhDOPGN8V/DHGtwHSidIJyaR/TT1TM/Wx8BK+ekAk
         R+XTlxe0JkL0yEt0HpGTS7zDALlMbNOOSA6BbhkJy5Mc1NmifjjJnpXTra9nZHiEve8e
         6HeGnAE3cYjvb0bjoxsrriN4j01pOB9v+thg5srLjvPNsx43oh8MmpF9MxI4fOQcpPMH
         haXA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=Kt1ytqrFQ3gxDFuU2/KO3OHIat7YiLJUy+EkQoB0jDg=;
        b=AEf5K1eTVFPWSiAD9jHR4w+NxCIll+Vztf/ZrR80Uzikbt+1k+OJI2UUQ5KgHlbMMJ
         2phSrXTl+VjBC/dbwBAFN1A2x58pRoZBAAIqjmg6E8AA33HEgXiGOWx3+tvC5thsb1xo
         mCDg9/ayDR0AcouMOrFQ13xg6TVLh/L8qEWDHG2UCAQdhgNh4sfI/9mD3qzJDG2b249U
         KHyYeAer1hAruY3kTTR7PY/LNDm+FlTyyi6gBHf9YqVCTtAB03wOeo04PwQIx24pWCz8
         15oxhf+AYOAAuGL4qWH3RYT2+F6Mil3drur7JNSfM4aVaDHfxiqa46SMnynxlCE6kb0l
         fJ+Q==
X-Gm-Message-State: AOAM533Da09PY4DEe/zxMakau4OgOiaB9E76fAsOR87vkbhixMXPwV84
        6HOjzfY1KpdFroLlvZYiT5U=
X-Google-Smtp-Source: ABdhPJzlgSjmm/lF0FgQ7+bskzwpSSZrrLGycbNOtdp017Sig4/W+nBL5b4RWVJoxp2k9By5mmSPKg==
X-Received: by 2002:aa7:c0c6:: with SMTP id j6mr15646714edp.146.1629537614656;
        Sat, 21 Aug 2021 02:20:14 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.13
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:14 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 21/37] perf evsel: move ignore_missing_thread to fallback code
Date:   Sat, 21 Aug 2021 11:19:27 +0200
Message-Id: <4eca51443c786baaf6811b7cd8e73aafd97f7606.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch moves ignore_missing_thread outside the perf_event_open loop.
Doing so, we need to move the retry_open flag a few places higher, with
minimal impact. Furthermore, thread need not be decreased since it won't
get increased by the for loop (since we're jumping back inside), but we
need to check that the nthreads decrease didn't put thread out of range.

The goal is to have fallbacks handled in one place only, since in the
future parallel code, these would be handled separately.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 29 +++++++++++++----------------
 tools/perf/util/evsel.h |  5 +++++
 2 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 916930ea31450265..a1a88607fd59efcb 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1656,7 +1656,7 @@ static int update_fds(struct evsel *evsel,
 	return 0;
 }
 
-static bool ignore_missing_thread(struct evsel *evsel,
+bool evsel__ignore_missing_thread(struct evsel *evsel,
 				  int nr_cpus, int cpu,
 				  struct perf_thread_map *threads,
 				  int thread, int err)
@@ -1993,12 +1993,15 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 		for (thread = 0; thread < nthreads; thread++) {
 			int fd, group_fd;
+retry_open:
+			if (thread >= nthreads)
+				break;
 
 			if (!evsel->cgrp && !evsel->core.system_wide)
 				pid = perf_thread_map__pid(threads, thread);
 
 			group_fd = get_group_fd(evsel, cpu, thread);
-retry_open:
+
 			test_attr__ready();
 
 			fd = perf_event_open(evsel, pid, cpus->map[cpu],
@@ -2016,20 +2019,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 			if (fd < 0) {
 				err = -errno;
 
-				if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
-					/*
-					 * We just removed 1 thread, so take a step
-					 * back on thread index and lower the upper
-					 * nthreads limit.
-					 */
-					nthreads--;
-					thread--;
-
-					/* ... and pretend like nothing have happened. */
-					err = 0;
-					continue;
-				}
-
 				pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
 					  err);
 				goto try_fallback;
@@ -2069,6 +2058,14 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	return 0;
 
 try_fallback:
+	if (evsel__ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
+		/* We just removed 1 thread, so lower the upper nthreads limit. */
+		nthreads--;
+
+		/* ... and pretend like nothing have happened. */
+		err = 0;
+		goto retry_open;
+	}
 	/*
 	 * perf stat needs between 5 and 22 fds per CPU. When we run out
 	 * of them try to increase the limits.
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index bf9abd9a5cbf9852..661d2677a19b6248 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -293,6 +293,11 @@ bool evsel__detect_missing_features(struct evsel *evsel);
 enum rlimit_action { NO_CHANGE, SET_TO_MAX, INCREASED_MAX };
 bool evsel__increase_rlimit(enum rlimit_action *set_rlimit);
 
+bool evsel__ignore_missing_thread(struct evsel *evsel,
+				  int nr_cpus, int cpu,
+				  struct perf_thread_map *threads,
+				  int thread, int err);
+
 struct perf_sample;
 
 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name);
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id E34CDC4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:35 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id CD02461186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:35 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S237918AbhHUJWN (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:13 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34088 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234017AbhHUJVj (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:39 -0400
Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76E89C0611C2;
        Sat, 21 Aug 2021 02:20:17 -0700 (PDT)
Received: by mail-ed1-x52b.google.com with SMTP id bo19so17595501edb.9;
        Sat, 21 Aug 2021 02:20:17 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=2DKoXbLqb5JEZfckQbGfo0eY1oqTs4gsyzCuCjXFWuM=;
        b=goOtNRccV4Ca5TUodnLlBiWwZbJJo4TWXbcTtsYNzoCukYgdjdCx6uHLXg7h/LJgv2
         hpvzqKh1Y90enpyN442g5a02CC7kxb3+1ZZr3s3UUWs5P6Ff5Nb3S3RKom7p9B8LaWtN
         a2cvV78I+6Gfmyxot7CJZ1rX5XInkZIPFu0IcYrDd2UvA8aNhQXtLmJNyPIUuhUrcV0+
         Hnrwlw3WnPCb7bVevf8eYsyqZaNfb98SvFcbZtuU6fBba42PnnUlMsxBCPvWBCpecezw
         tBqJawlPnEfMQwPsRiTXvYoFnkX5JBlNjI9zAcTtTnw8kWHaLj+Ent6B961Cxb4/2eNU
         iU+Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=2DKoXbLqb5JEZfckQbGfo0eY1oqTs4gsyzCuCjXFWuM=;
        b=lvmzHL/3T6Jm+fgDI4sjVLinIbqGCOTTaWa9gGvSQLVJtYSNCUnDcQdUw+7QZTwNMJ
         ZbWG/isQRzZ9IChEcPD0S53a41NHVUg1vyoiyEDB5vW7MAnsFxs2md1+tGRisZpXpHa7
         SVeI8rpEokze/32l7C2izMq9kwL7rM0hjR5EHFhkqApEowKKC9uvNKD3ZQEFFlZQDWVm
         Bcy4jJ54JEmgo1wF372ieQblHm0NKKOhpsNnW/IgPC9i3+bqPWFYLNApRAiWFmZgmERO
         K5f9E4/PTuyzQRi/uPWQ+3oaa+mwmvtvhAMaoPfPhVIvzmzU57GVhbXSudfuPX/re3je
         0zGg==
X-Gm-Message-State: AOAM5320LRb8BCLCdpPKwOacw5rD4MM3615KVpGZppdpOMZwH8VwXCGT
        u8I2d1DQWqXLEp6N9P6jpEQ=
X-Google-Smtp-Source: ABdhPJxFTsrC49EPVHSw351D2Z7eBxtPfwGJL/PK7IyX79wNXB0WsrJNUsHn5g7qgqGDHYDba62n8Q==
X-Received: by 2002:a05:6402:214d:: with SMTP id bq13mr26632573edb.263.1629537615955;
        Sat, 21 Aug 2021 02:20:15 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.14
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:15 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 22/37] perf evsel: move test_attr__open to success path in evsel__open_cpu
Date:   Sat, 21 Aug 2021 11:19:28 +0200
Message-Id: <b3baf11360ca96541c9631730614fd7d217496fc.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

test_attr__open ignores the fd if -1, therefore it is safe to move it to
the success path (fd >= 0).

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index a1a88607fd59efcb..d2b0391569286080 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -2011,11 +2011,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 			bpf_counter__install_pe(evsel, cpu, fd);
 
-			if (unlikely(test_attr__enabled)) {
-				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
-						fd, group_fd, evsel->open_flags);
-			}
-
 			if (fd < 0) {
 				err = -errno;
 
@@ -2024,6 +2019,11 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 				goto try_fallback;
 			}
 
+			if (unlikely(test_attr__enabled)) {
+				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
+						fd, group_fd, evsel->open_flags);
+			}
+
 			pr_debug2_peo(" = %d\n", fd);
 
 			if (evsel->bpf_fd >= 0) {
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 759F2C432BE
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:38 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 5EDF66121E
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:38 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234034AbhHUJWP (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:15 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34144 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234384AbhHUJVk (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:40 -0400
Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C03A3C0611F9;
        Sat, 21 Aug 2021 02:20:18 -0700 (PDT)
Received: by mail-ed1-x52d.google.com with SMTP id b7so17672936edu.3;
        Sat, 21 Aug 2021 02:20:18 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=hSVEhq3LjLu21uthNLRpY++hxTR94OXd9+gS0oH2CrE=;
        b=G8amz1JObX3GRuSFcCEiYiCvwXSjKBNuwgNbi39x6nCiZTXag+QSolTMaj1Kx67Ql5
         JfdLV43x4cDZqjHCqR9jkyJHHCD+bKVf/0bUENiYi8MJa9hUDJW/X3kpGiXq/SAhP9DV
         kMF4W+g8Nxv7AlK2iUX1vXciV+vmaJX/uQVi5StttjMOu20XzHSgQej4DC7JCo8FE68/
         BP5DT1TVZmE/oxNjuNbfXlpvXUgN/jwrDtzb7Y052JNDz4iwaEkYEoesC2edgzZb8oLg
         BIGmjGLO0HKDanfjrIJ1qmj7kXRQIu4jb8r+yeWpPKv1pT464TAhRHxFKpI2OSKkRQHz
         Jqug==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=hSVEhq3LjLu21uthNLRpY++hxTR94OXd9+gS0oH2CrE=;
        b=IMIFvwgye91jeV1NxUVXjh1qud4rD7zmKHawLd0SbePj3OAxa18tZQQ5PmY9Gc2t7h
         pfXNMSn69Llv3ScraMN6SHFkpvVROo1B4Y2w6LL9YlN5Ohphz2T5sfmkBD3UCfh2ln2c
         7k578s8v4F5p4f+W14Y+XsG7/9MsUiUvR0yYVcl6rssdY5kM+NWjuI2YBhkdh/G4HcCT
         OXSLn7KefsA7W4nGpdozpaT8VAP/mg5tm5nEyibo4HmWoxqXENJuhAhyJ4mcDbnmSkiU
         yzBaRAx6Iy10OOZpMmCANijaGK0nwom6YOYiNJuHnXiSzvI2VWxX7cOyUwXSb93vYm/i
         lS7A==
X-Gm-Message-State: AOAM530xBkQTNSvA8mb2POP3gmAI7O2U0Amlpmp8OdDYm9urqAva6Snv
        ORMbL7ANES9FHtye3ZnswuQ=
X-Google-Smtp-Source: ABdhPJy84gtsoCFiGxshU5Ik3OhmeQJqe3S2bHeBRTU237vjJQibWM/xI1V6+TWZ04pQHELxji/MkQ==
X-Received: by 2002:aa7:db82:: with SMTP id u2mr27502712edt.299.1629537617286;
        Sat, 21 Aug 2021 02:20:17 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.16
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:17 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 23/37] perf evsel: move bpf_counter__install_pe to success path in evsel__open_cpu
Date:   Sat, 21 Aug 2021 11:19:29 +0200
Message-Id: <64f8a1b0a838a6e6049cd43c1beafd432999ae57.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

I don't see why bpf_counter__install_pe should get called even if fd=-1,
so I'm moving it to the success path.

This will be useful in following patches to separate the actual open and
the related operations from the fallback mechanisms.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index d2b0391569286080..3e556afed8dd396c 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -2009,8 +2009,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 			FD(evsel, cpu, thread) = fd;
 
-			bpf_counter__install_pe(evsel, cpu, fd);
-
 			if (fd < 0) {
 				err = -errno;
 
@@ -2019,6 +2017,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 				goto try_fallback;
 			}
 
+			bpf_counter__install_pe(evsel, cpu, fd);
+
 			if (unlikely(test_attr__enabled)) {
 				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
 						fd, group_fd, evsel->open_flags);
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 20AAEC4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:56 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 01434611EF
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:55 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233809AbhHUJWe (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:34 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34160 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S235147AbhHUJV5 (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:57 -0400
Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40AF6C0611BD;
        Sat, 21 Aug 2021 02:20:20 -0700 (PDT)
Received: by mail-ed1-x536.google.com with SMTP id d6so17650276edt.7;
        Sat, 21 Aug 2021 02:20:20 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=bgPEK1IAfRbcf64sxtgFjOq9WczpOQefLSQknOJFNgw=;
        b=I+ilja/9mBtJaapuFwCjDpRGyqV9LZ0j1YOE1XhsqQh+VJfL1czIZpyz6YnvMGhy8F
         K2RKdt7PsrTVsWUe3QcRxSDN813QunrEfkmhL81CliwD3ub0Le2mCxTekPM3eiy56o7I
         3Mj6+5sO5anoZW4e0/swEnKy/ijORJ6nxj5S7zTNUFuD8ZNb0OwR1tqwLCKVftVPzPvf
         PK7/LD+Uwbok9vxdLX6/oAdwHiYruiywNUGrrGrjQvOPRpgvN2UGprYp3xrEqgpA2/St
         xBQqmgVmYIjkuFijUuQoMwDEYCKLkGWUvBTDM08RxDrrjU5NgZ13l5AAW093Ee6aSL6o
         aAfQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=bgPEK1IAfRbcf64sxtgFjOq9WczpOQefLSQknOJFNgw=;
        b=WP8dgJ7h38Zui2iSGMtgjjo8PKmBRUM08z1sz2fu0feH7/3ujD5nkToXAYDg1/kkgN
         4HI/G6y5ZQmKGhUtrJiwuiGcPAHUW/1pNhfj/xj2WKPdRbCv5m1KncD+/z2XlW9G5FEy
         LGuD2HsJXOQI2X6vFUXSPwS+Eaa07csh+tXmvOH5I8y+k6FkxpAHiO4LU9tS7VpjSRWo
         RdtzWkqDgof8/ApxYRUsro8eNuDjvAOgwbRKvAxruF6GFX6HVZ4KYP8iPR0YwaqmNw5H
         4BI8OnRVJCCwBDvVSQubfCWuRjWBdbmjpv+cjsgD1EyyM63z2bdkFRSZvIX0z4wM1A7X
         w6Qw==
X-Gm-Message-State: AOAM5300X6a8cLUttNGOwmy72x+vu+bnPa/Ti+NA4p4smB2wwtTOJi8b
        DafHAdWioZJf+R04Qb68XGY=
X-Google-Smtp-Source: ABdhPJwJ5CSBkK8BYON2RfmKUa50QoTJv2mHYsAVtFaW1wn56w+LImZ/DnlAKE6pqWouzfMZZ//fzA==
X-Received: by 2002:a05:6402:1cb9:: with SMTP id cz25mr190285edb.11.1629537618772;
        Sat, 21 Aug 2021 02:20:18 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.17
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:18 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 24/37] perf evsel: handle precise_ip fallback in evsel__open_cpu
Date:   Sat, 21 Aug 2021 11:19:30 +0200
Message-Id: <74208c433d2024a6c4af9c0b140b54ed6b5ea810.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This is another patch in the effort to separate the fallback mechanisms
from the open itself.

In case of precise_ip fallback, the original precise_ip will be stored
in the evsel (it was stored in a local variable) and the open will be
retried. Since the precise_ip fallback will be the first in the chain of
fallbacks, there should be no functional change with this patch.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 59 ++++++++++++++++++-----------------------
 tools/perf/util/evsel.h |  2 ++
 2 files changed, 28 insertions(+), 33 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 3e556afed8dd396c..2e95416b8320c6b9 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1709,42 +1709,29 @@ static void display_attr(struct perf_event_attr *attr)
 	}
 }
 
-static int perf_event_open(struct evsel *evsel,
-			   pid_t pid, int cpu, int group_fd)
+bool evsel__precise_ip_fallback(struct evsel *evsel)
 {
-	int precise_ip = evsel->core.attr.precise_ip;
-	int fd;
-
-	while (1) {
-		pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
-			  pid, cpu, group_fd, evsel->open_flags);
-
-		fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, evsel->open_flags);
-		if (fd >= 0)
-			break;
-
-		/* Do not try less precise if not requested. */
-		if (!evsel->precise_max)
-			break;
-
-		/*
-		 * We tried all the precise_ip values, and it's
-		 * still failing, so leave it to standard fallback.
-		 */
-		if (!evsel->core.attr.precise_ip) {
-			evsel->core.attr.precise_ip = precise_ip;
-			break;
-		}
+	/* Do not try less precise if not requested. */
+	if (!evsel->precise_max)
+		return false;
 
-		pr_debug2_peo("\nsys_perf_event_open failed, error %d\n", -ENOTSUP);
-		evsel->core.attr.precise_ip--;
-		pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
-		display_attr(&evsel->core.attr);
+	/*
+	 * We tried all the precise_ip values, and it's
+	 * still failing, so leave it to standard fallback.
+	 */
+	if (!evsel->core.attr.precise_ip) {
+		evsel->core.attr.precise_ip = evsel->precise_ip_original;
+		return false;
 	}
 
-	return fd;
-}
+	if (!evsel->precise_ip_original)
+		evsel->precise_ip_original = evsel->core.attr.precise_ip;
 
+	evsel->core.attr.precise_ip--;
+	pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
+	display_attr(&evsel->core.attr);
+	return true;
+}
 
 static struct perf_cpu_map *empty_cpu_map;
 static struct perf_thread_map *empty_thread_map;
@@ -2004,8 +1991,11 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 			test_attr__ready();
 
-			fd = perf_event_open(evsel, pid, cpus->map[cpu],
-					     group_fd);
+			pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
+				pid, cpus->map[cpu], group_fd, evsel->open_flags);
+
+			fd = sys_perf_event_open(&evsel->core.attr, pid, cpus->map[cpu],
+						group_fd, evsel->open_flags);
 
 			FD(evsel, cpu, thread) = fd;
 
@@ -2058,6 +2048,9 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	return 0;
 
 try_fallback:
+	if (evsel__precise_ip_fallback(evsel))
+		goto retry_open;
+
 	if (evsel__ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
 		/* We just removed 1 thread, so lower the upper nthreads limit. */
 		nthreads--;
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 661d2677a19b6248..0a245afab2d87d74 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -150,6 +150,7 @@ struct evsel {
 		struct bperf_follower_bpf *follower_skel;
 	};
 	unsigned long		open_flags;
+	int			precise_ip_original;
 };
 
 struct perf_missing_features {
@@ -297,6 +298,7 @@ bool evsel__ignore_missing_thread(struct evsel *evsel,
 				  int nr_cpus, int cpu,
 				  struct perf_thread_map *threads,
 				  int thread, int err);
+bool evsel__precise_ip_fallback(struct evsel *evsel);
 
 struct perf_sample;
 
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id C4B73C4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:56 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id AD8C461208
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:21:56 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234756AbhHUJWe (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:34 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34162 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S235162AbhHUJV5 (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:57 -0400
Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 756ECC0619F6;
        Sat, 21 Aug 2021 02:20:21 -0700 (PDT)
Received: by mail-ed1-x536.google.com with SMTP id cn28so17630741edb.6;
        Sat, 21 Aug 2021 02:20:21 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=MMAiSLL3U1jyvlQA9M+E1LVvAf3sYZBM8G7KKzsRxEw=;
        b=R/Nu8Hs7Nh8VZ5uWGXwqsgYSXL1QalbBBKbTuIa7JnSO9SxAkh03StMI4O01ReV9+G
         1WX8RFCrD+54OyISTKzPpq5oRzu0tLB6m5X3P53TqUpEWgpQoSWqvn+fhrMFAtwoInt+
         NuWRzqIWcHU3C0xN6lQEExxycTLzEgN821IIb7GDl1J0GZSSrMBbVfdaWE7oxCWevK3e
         D46mA55ycfKzpKkOteIlh0DDlj9NqGIKpSm0d2D+hsITN3R5qdIBDNxVYx5n+9uuOTuB
         0ipkplqBC5aMw7CpMoArrhOhpnbrtYIQ1d1lDRD1bkeYyK87qrA4LBlghVYfMuRxUVh1
         xGCg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=MMAiSLL3U1jyvlQA9M+E1LVvAf3sYZBM8G7KKzsRxEw=;
        b=WP1E3MWx6ew4QqIf8AEdX7tFqtcBJLwUftrWROFUiLrhz6ZHftwaK6xuFJ0H9C38JW
         6INZSKTr1FrotKRpEPkW22odT5UU3A2Pen47zHrOSlKwanB+tZNquS/HfKb5tUsvdEfw
         /K8LpUuw7ZK3MWooLsVVkLvFrEVRRB0sxI9oDcUHaASLS6/CRv9yNGNFt9UOwiOJ13Dj
         JjsneKjSOOWS26sGxkoBRO+F+/LgPszlw+O61/01MhXknf1GK13y8bNB7+r0ji0QwoKM
         aJtWH7ws6USOu2ueNI+5GlWYT4ioAqR1V/I24wfpvgvVRD7CVcNqoo+v2ag5pg94IYQq
         9F/Q==
X-Gm-Message-State: AOAM5332qFea4p6sIwMJGvyJ4NfyEpj2wmBFFpWK2ksegG2kUSg+TrwV
        3yVw1MwY4FFFYe/f2sYi4Bc=
X-Google-Smtp-Source: ABdhPJwmTRM2Neqq23Iu4eU7f7r0wc55lJAm3U2C4EOlBsf+qDotJ69e5MxiRXwNacrhjbv98/EYVA==
X-Received: by 2002:aa7:c78e:: with SMTP id n14mr26409224eds.381.1629537619936;
        Sat, 21 Aug 2021 02:20:19 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.18
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:19 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 25/37] perf evsel: move event open in evsel__open_cpu to separate function
Date:   Sat, 21 Aug 2021 11:19:31 +0200
Message-Id: <74ac2eea14f45b2cbecffb509dd5f3cd523d4a9b.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This is the final patch splitting evsel__open_cpu.
This patch moves the entire loop code to a separate function, to be
reused for the multithreaded code.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 142 ++++++++++++++++++++++++----------------
 tools/perf/util/evsel.h |  12 ++++
 2 files changed, 99 insertions(+), 55 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 2e95416b8320c6b9..e41f55a7a70ea630 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1945,6 +1945,82 @@ bool evsel__increase_rlimit(enum rlimit_action *set_rlimit)
 	return false;
 }
 
+static struct perf_event_open_result perf_event_open(struct evsel *evsel,
+					pid_t pid, int cpu, int thread,
+					struct perf_cpu_map *cpus,
+					struct perf_thread_map *threads)
+{
+	int fd, group_fd, rc;
+	struct perf_event_open_result res;
+
+	if (!evsel->cgrp && !evsel->core.system_wide)
+		pid = perf_thread_map__pid(threads, thread);
+
+	group_fd = get_group_fd(evsel, cpu, thread);
+
+	test_attr__ready();
+
+	pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
+			pid, cpus->map[cpu], group_fd, evsel->open_flags);
+
+	fd = sys_perf_event_open(&evsel->core.attr, pid, cpus->map[cpu],
+				group_fd, evsel->open_flags);
+
+	FD(evsel, cpu, thread) = fd;
+	res.fd = fd;
+
+	if (fd < 0) {
+		rc = -errno;
+
+		pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
+				rc);
+		res.rc = rc;
+		res.err = PEO_FALLBACK;
+		return res;
+	}
+
+	bpf_counter__install_pe(evsel, cpu, fd);
+
+	if (unlikely(test_attr__enabled)) {
+		test_attr__open(&evsel->core.attr, pid,
+			cpus->map[cpu], fd,
+			group_fd, evsel->open_flags);
+	}
+
+	pr_debug2_peo(" = %d\n", fd);
+
+	if (evsel->bpf_fd >= 0) {
+		int evt_fd = fd;
+		int bpf_fd = evsel->bpf_fd;
+
+		rc = ioctl(evt_fd,
+				PERF_EVENT_IOC_SET_BPF,
+				bpf_fd);
+		if (rc && errno != EEXIST) {
+			pr_err("failed to attach bpf fd %d: %s\n",
+				bpf_fd, strerror(errno));
+			res.rc = -EINVAL;
+			res.err = PEO_ERROR;
+			return res;
+		}
+	}
+
+	/*
+	 * If we succeeded but had to kill clockid, fail and
+	 * have evsel__open_strerror() print us a nice error.
+	 */
+	if (perf_missing_features.clockid ||
+		perf_missing_features.clockid_wrong) {
+		res.rc = -EINVAL;
+		res.err = PEO_ERROR;
+		return res;
+	}
+
+	res.rc = 0;
+	res.err = PEO_SUCCESS;
+	return res;
+}
+
 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads,
 		int start_cpu, int end_cpu)
@@ -1952,6 +2028,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	int cpu, thread, nthreads;
 	int pid = -1, err, old_errno;
 	enum rlimit_action set_rlimit = NO_CHANGE;
+	struct perf_event_open_result peo_res;
 
 	err = __evsel__prepare_open(evsel, cpus, threads);
 	if (err)
@@ -1979,67 +2056,22 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	for (cpu = start_cpu; cpu < end_cpu; cpu++) {
 
 		for (thread = 0; thread < nthreads; thread++) {
-			int fd, group_fd;
 retry_open:
 			if (thread >= nthreads)
 				break;
 
-			if (!evsel->cgrp && !evsel->core.system_wide)
-				pid = perf_thread_map__pid(threads, thread);
-
-			group_fd = get_group_fd(evsel, cpu, thread);
-
-			test_attr__ready();
-
-			pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
-				pid, cpus->map[cpu], group_fd, evsel->open_flags);
+			peo_res = perf_event_open(evsel, pid, cpu, thread, cpus,
+						threads);
 
-			fd = sys_perf_event_open(&evsel->core.attr, pid, cpus->map[cpu],
-						group_fd, evsel->open_flags);
-
-			FD(evsel, cpu, thread) = fd;
-
-			if (fd < 0) {
-				err = -errno;
-
-				pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
-					  err);
+			err = peo_res.rc;
+			switch (peo_res.err) {
+			case PEO_SUCCESS:
+				set_rlimit = NO_CHANGE;
+				continue;
+			case PEO_FALLBACK:
 				goto try_fallback;
-			}
-
-			bpf_counter__install_pe(evsel, cpu, fd);
-
-			if (unlikely(test_attr__enabled)) {
-				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
-						fd, group_fd, evsel->open_flags);
-			}
-
-			pr_debug2_peo(" = %d\n", fd);
-
-			if (evsel->bpf_fd >= 0) {
-				int evt_fd = fd;
-				int bpf_fd = evsel->bpf_fd;
-
-				err = ioctl(evt_fd,
-					    PERF_EVENT_IOC_SET_BPF,
-					    bpf_fd);
-				if (err && errno != EEXIST) {
-					pr_err("failed to attach bpf fd %d: %s\n",
-					       bpf_fd, strerror(errno));
-					err = -EINVAL;
-					goto out_close;
-				}
-			}
-
-			set_rlimit = NO_CHANGE;
-
-			/*
-			 * If we succeeded but had to kill clockid, fail and
-			 * have evsel__open_strerror() print us a nice error.
-			 */
-			if (perf_missing_features.clockid ||
-			    perf_missing_features.clockid_wrong) {
-				err = -EINVAL;
+			default:
+			case PEO_ERROR:
 				goto out_close;
 			}
 		}
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 0a245afab2d87d74..8c9827a93ac001a7 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -282,6 +282,18 @@ int evsel__enable(struct evsel *evsel);
 int evsel__disable(struct evsel *evsel);
 int evsel__disable_cpu(struct evsel *evsel, int cpu);
 
+enum perf_event_open_err {
+	PEO_SUCCESS,
+	PEO_FALLBACK,
+	PEO_ERROR
+};
+
+struct perf_event_open_result {
+	enum perf_event_open_err err;
+	int rc;
+	int fd;
+};
+
 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu);
 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads);
 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 3E9F3C4320E
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:00 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 2711E61208
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:00 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234812AbhHUJWh (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:37 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34116 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S235333AbhHUJV6 (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:21:58 -0400
Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85E1FC061144;
        Sat, 21 Aug 2021 02:20:22 -0700 (PDT)
Received: by mail-ed1-x533.google.com with SMTP id q3so17661996edt.5;
        Sat, 21 Aug 2021 02:20:22 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=54GMaFeo+0ii7XXdVN7OyGxlbAq4Jcyh9p53ycXRCvY=;
        b=f6FvLaETibhXox2YQ39Lc+SJfu9Vo/EqS1NZyGo8hGRMUepgc8jNljcjcnctclfV65
         SEDKK3ceXA4XW5N1Md5F8CFFKCrgmG3w1qsawwaYtri3Axe9qMWh163cTfGxcsnZENAA
         phtuhTyJgI7MW0QJeiKsmEcdFG6PfAflcTQjeXRdl/J7sCH5LPF3ccLftuywVbFMVES/
         QohdcKu4wyKM3YNrNny1mSSjeCe0BALohSzT77mbIq7jn1gwox5PKdoqvdUT3f9VS1/o
         Dbi7bBt7LkWPdEQWy8T3Qf4PGHKS2p/YMC6R2brwd77Pi72wM8FI3AdmCAj9uhCJJ99W
         k+fw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=54GMaFeo+0ii7XXdVN7OyGxlbAq4Jcyh9p53ycXRCvY=;
        b=iyTVp0oMo3BdcTz/LDMgoV5vGusRhfY7ql2WVtnhJOUwLoNksl19mu1lgylxat3DUO
         h+9fufJejPnMF5eaU3a7LcYQB543SP4asklI2AvpzOl0LYIBi2ndkeSbTooZP//hTBCT
         yY+TyPbLmcfrt9F8HB/+dqI+WEchDcWUNSl+q+W1KjRinZl2fAZcCTJ262lfgvhi3IbR
         c6tkuV0r1exgy5VGyYeY4ryr/jCCNTpp/GiTBPg+hho+avY045t5bbB909mc5/pTYIB5
         UeXBN1eImJ0Q4Am7zf/Nx1QICLZtZZkQcVbBL6igUAfAiNEMkM7lAzFpLw6h/62gOWov
         /bsQ==
X-Gm-Message-State: AOAM5319RfDxBJ495+9JUsnGHmzA8ubxRcBaC99MJW41WnkN3GIjHVwz
        esfjTKxbb7I5nPXK/P7gRNs=
X-Google-Smtp-Source: ABdhPJy2g+B62ZbkcrLfi6FK4ioITzqxZe5fXKbFOq1aQmJn46XwY+JUtgbIKCj1UF2UgiyCknlVlQ==
X-Received: by 2002:aa7:db8b:: with SMTP id u11mr26503283edt.362.1629537621120;
        Sat, 21 Aug 2021 02:20:21 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.20
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:20 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 26/37] perf evsel: add evsel__open_per_cpu_no_fallback function
Date:   Sat, 21 Aug 2021 11:19:32 +0200
Message-Id: <e4d366eab34d2427faded11de6ecb9efe8b781ef.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This function is equivalent to evsel__open, but without any fallback
mechanism, which should be handled separately.
It is also possible to a starting thread to be able to resume
a previous failing evsel__open_per_cpu_no_fallback.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evsel.c | 44 +++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/evsel.h |  9 +++++++++
 2 files changed, 53 insertions(+)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index e41f55a7a70ea630..74aad07f3c1102cb 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -2021,6 +2021,50 @@ static struct perf_event_open_result perf_event_open(struct evsel *evsel,
 	return res;
 }
 
+struct evsel_open_result evsel__open_per_cpu_no_fallback(struct evsel *evsel,
+					struct perf_cpu_map *cpus,
+					struct perf_thread_map *threads,
+					int cpu, int start_thread)
+{
+	int thread, nthreads, pid = -1;
+	struct evsel_open_result res = { .thread = start_thread };
+
+	if (cpus == NULL)
+		cpus = empty_cpu_map;
+
+	if (threads == NULL)
+		threads = empty_thread_map;
+
+	if (evsel->core.system_wide)
+		nthreads = 1;
+	else
+		nthreads = threads->nr;
+
+	if (evsel->cgrp)
+		pid = evsel->cgrp->fd;
+
+	display_attr(&evsel->core.attr);
+
+
+	for (thread = start_thread; thread < nthreads; thread++) {
+		res.peo_res = perf_event_open(evsel, pid, cpu, thread, cpus,
+					threads);
+
+		switch (res.peo_res.err) {
+		case PEO_SUCCESS:
+			continue;
+		case PEO_FALLBACK:
+			goto out;
+		default:
+		case PEO_ERROR:
+			goto out;
+		}
+	}
+out:
+	res.thread = thread;
+	return res;
+}
+
 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads,
 		int start_cpu, int end_cpu)
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 8c9827a93ac001a7..fa0d732d0a088016 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -294,10 +294,19 @@ struct perf_event_open_result {
 	int fd;
 };
 
+struct evsel_open_result {
+	int thread;
+	struct perf_event_open_result peo_res;
+};
+
 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu);
 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads);
 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads);
+struct evsel_open_result evsel__open_per_cpu_no_fallback(struct evsel *evsel,
+					struct perf_cpu_map *cpus,
+					struct perf_thread_map *threads,
+					int cpu, int start_thread);
 void evsel__close(struct evsel *evsel);
 int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		struct perf_thread_map *threads);
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id C7CBDC4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:12 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id AB25461186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:12 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233369AbhHUJWu (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:50 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34088 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234663AbhHUJWM (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:12 -0400
Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA7E6C061147;
        Sat, 21 Aug 2021 02:20:23 -0700 (PDT)
Received: by mail-ed1-x530.google.com with SMTP id dj8so17663773edb.2;
        Sat, 21 Aug 2021 02:20:23 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=9QtVrbhUZ3Hz139HgNIy3r2BPh97ZYhKrBaN0u3auT0=;
        b=DGc30yDbq10hT70Gm0uu0F0HMpNHf5J1ZCabb5/ql6pQ3LOwTIwaH9Ae86bdHHu8bm
         rlJTeCXr3NfO84mQ3Bb4iWo34tFO1MuV7V1TPqaK1Fh3j7IZt2Lx48xhzTo0wPesdiTO
         f//2AOdzNPmlGdSTIWcY3p9UuaeEsYSRaHWB+MHT0XOwsU97KnrBBdyfks/ob7Xlt1D8
         NO5E5wZpnzZEoRc/TRww20DlATvS8eGa5ccYibAK+YGWrlaAICTU1aT2FZMhW36/oAq7
         rr8MQiCGbWSaD69wb/uVHP0OcMZnUbU5vMf4xnkoT/vhKHxzPGHcwSBnY7EqQnTnwucf
         W7hA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=9QtVrbhUZ3Hz139HgNIy3r2BPh97ZYhKrBaN0u3auT0=;
        b=D3GOg+AItVYPLi3y/i7GEj5OzEU27WkK2u3Ql+uX2g1q2u4o8gDOP5OSY1LKQGQCWi
         8gI5FwAsC0Pr0acOhffPyfeMllOM/HZ0tH54BV7zwAFBUwx40QeN4mjTIelYKPvNVW5p
         PwctdrJhmx1rcQECQWgoYQCeXe2MBUQDBO9rTMomLW8xhoOxTOw88sIVi4f4MXE2k8ka
         3m6OYDr+YVTfktaXT1Dk7ZcHy54SiGq9EvllwNZscCjFWfuRXX5IMbj4GuCv8mF5atEY
         nAiK2Apj6N4xPl4GUPMHrisB1QLKRv46jA45u1Ubrv9GvuKPgKKpgf4fSnRY7h1ACkgg
         Ut2Q==
X-Gm-Message-State: AOAM5339Ml33353ZR7iEAdvHeEgfkyJQ0MWe65S5M984bOQN1uw7XYvx
        NvaBqP8aWcARRumqQwjH4pY=
X-Google-Smtp-Source: ABdhPJwN8y6ZGwYt+8hKntEco2UM8aUU8Q7M6zB5LuTrBbZ/bumj6BPvutj5FabQGsG40AiYOqN6XA==
X-Received: by 2002:a50:998f:: with SMTP id m15mr26362842edb.193.1629537622314;
        Sat, 21 Aug 2021 02:20:22 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.21
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:22 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 27/37] perf evlist: add evlist__for_each_entry_from macro
Date:   Sat, 21 Aug 2021 11:19:33 +0200
Message-Id: <2386505f8b598adf0dbcd04ec21804c6bcf00826.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds a new iteration macro for evlist that resumes iteration
from a given evsel in the evlist.

This macro will be used in the next patch

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evlist.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 5f24a45d4e3cf30a..b0c2da0f9755b2d1 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -275,6 +275,22 @@ void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
 #define evlist__for_each_entry_continue(evlist, evsel) \
 	__evlist__for_each_entry_continue(&(evlist)->core.entries, evsel)
 
+/**
+ * __evlist__for_each_entry_from - continue iteration from @evsel (included)
+ * @list: list_head instance to iterate
+ * @evsel: struct evsel iterator
+ */
+#define __evlist__for_each_entry_from(list, evsel) \
+	list_for_each_entry_from(evsel, list, core.node)
+
+/**
+ * evlist__for_each_entry_from - continue iteration from @evsel (included)
+ * @evlist: evlist instance to iterate
+ * @evsel: struct evsel iterator
+ */
+#define evlist__for_each_entry_from(evlist, evsel) \
+	__evlist__for_each_entry_from(&(evlist)->core.entries, evsel)
+
 /**
  * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
  * @list: list_head instance to iterate
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 7B64DC4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:13 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 629B1611EF
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:13 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234481AbhHUJWv (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:51 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34096 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233476AbhHUJWM (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:12 -0400
Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F4011C061149;
        Sat, 21 Aug 2021 02:20:24 -0700 (PDT)
Received: by mail-ed1-x52a.google.com with SMTP id v2so17613120edq.10;
        Sat, 21 Aug 2021 02:20:24 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=kbY+VGy/3mkjno4xnYKOjNQeJxgBCMAOKpqcP2zu5lg=;
        b=Aj7n8fZwGkeeVrdSL1o7mSpy2nVXdCldhQbgSyhea8KSahPrk4kMiVIbb2vBYt9Uvp
         A3fngXvTzPe8CqqAVUIbnIAXYrFtMxDVKPBK79qYJfXqk1SQuG5IyWCKxJ4C7inX8EIp
         vak3mYz9w2LRBWZ5zQGEE/qj9KjiwoP6PLCobSEKtJIRIM6gtFGBBGpv8hJSO4p+keFi
         8/4vn4GeXr7ZWFXWqs5JQWThpr9/Mp3wh8jyBa6NVmZQc2aXmv2Gcl3LMS7XBDwwfo7v
         +/Bo+PYW42Bd0hng8UrBNLR+V79Xy4lT9yJrUhdVI6uWmlT0wweFcwSpoVEaA8UL5k3Q
         fcVw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=kbY+VGy/3mkjno4xnYKOjNQeJxgBCMAOKpqcP2zu5lg=;
        b=QrmK9aZ/FfpUNRjIbLwE2f66k2pNncVZpFe1F26rzXoNaDRSolnfYCLc9eaQrdwu95
         Hc2Q6DPZ0xX5CThSPr1I6TdDrWqZ5EmGx6uP9BUtqWrNd7I1gDB3ILQSRBafy2quW+mM
         fuWTBrnYoYkxzJ0c2He4EkitWxJO4IFyYuwfrYb2QvniRVuq8k6oOw2a/qDymtQTWCTX
         aCTDD8RyAaI3DrrDkvCcuRznASEuzAfBMhDnHD+7ZqHKRhIeO+SybJN/BJUZ/u6hz9P8
         kaUo9b9CJJ3xr+Sb+My/WR0MjPGrV3POIjdn9ZPbjQWT1Dv2sq+l9A65Zs8lRW2CjS13
         qVRg==
X-Gm-Message-State: AOAM532mVUFo8jmq7cUXMAaSThhBfom0zXIIxv0UoFVBwIo6xVhK80MF
        OEHYynkLmqAFOuVQQNAYCVCZVIsLbvIdVCPuzJI=
X-Google-Smtp-Source: ABdhPJwmQgO+RqW9PZ4FGpV5+fnG8bHO/agqcKbQdGZt8GIeBhCtCSWdd1PrW0r3n49AaAL62V1Ivw==
X-Received: by 2002:a05:6402:7ce:: with SMTP id u14mr26544088edy.61.1629537623567;
        Sat, 21 Aug 2021 02:20:23 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.22
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:23 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 28/37] perf evlist: add multithreading to evlist__open
Date:   Sat, 21 Aug 2021 11:19:34 +0200
Message-Id: <313458a08272272b00efa6b5581a1cdbaca0ef84.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch enables multithreading in evlist__open using the new
evsel__open_per_cpu_no_fallback function.

The multithreaded version tries to open everything in parallel. Once
workers are done, it checks their result and, in case of error, it
tries the fallback mechanisms present in evsel__open_cpu and restarts
the workers from were they've left.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evlist.c | 189 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 183 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index f0a839107e8849bf..3472038d719ec7d4 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -19,6 +19,8 @@
 #include "units.h"
 #include "bpf_counter.h"
 #include <internal/lib.h> // page_size
+#include <internal/cpumap.h>
+#include <internal/threadmap.h>
 #include "affinity.h"
 #include "../perf.h"
 #include "asm/bug.h"
@@ -1403,11 +1405,184 @@ static int evlist__create_syswide_maps(struct evlist *evlist)
 	return err;
 }
 
-int evlist__open(struct evlist *evlist)
+static int evlist__open_singlethreaded(struct evlist *evlist)
 {
 	struct evsel *evsel;
 	int err;
 
+	evlist__for_each_entry(evlist, evsel) {
+		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
+		if (err < 0)
+			return err;
+	}
+
+	return 0;
+}
+
+struct evlist_open_work {
+	struct work_struct work;
+	struct evlist *evlist;
+	int cpu;
+	union {
+		int cpu_resume;
+		int cpu_err;
+	};
+	union {
+		struct evsel *evsel_resume;
+		struct evsel *evsel_err;
+	};
+	struct evsel_open_result res;	// this is also used to resume work
+	bool progress;			// did the worker do any progress?
+};
+
+static void evlist__open_multithreaded_func(struct work_struct *_work)
+{
+	struct evlist_open_work *work = container_of(_work, struct evlist_open_work, work);
+	struct evsel *evsel = work->evsel_resume;
+	int cpu_idx, thread_resume = work->res.thread;
+
+	work->res.peo_res.err = PEO_SUCCESS;
+	work->progress = false;
+
+	if (!evsel)	// nothing to do
+		return;
+
+	work->evsel_err = NULL;
+
+	evlist__for_each_entry_from(work->evlist, evsel) {
+		cpu_idx = evsel__find_cpu(evsel, work->cpu);
+		if (cpu_idx < work->cpu_resume)
+			continue;
+
+		work->res = evsel__open_per_cpu_no_fallback(evsel,
+					evsel->core.cpus,
+					evsel->core.threads,
+					cpu_idx, thread_resume);
+		work->progress |= work->res.thread != thread_resume;
+		if (work->res.peo_res.err != PEO_SUCCESS) {
+			work->evsel_err = evsel;
+			work->cpu_err = cpu_idx;
+			break;
+		}
+
+		thread_resume = 0;
+	}
+}
+
+static int evlist__open_multithreaded(struct evlist *evlist)
+{
+	int cpu, cpuid, cpuidx, thread, err;
+	struct evlist_open_work *works;
+	char errbuf[WORKQUEUE_STRERR_BUFSIZE];
+	struct perf_event_open_result peo_res;
+	struct evsel *evsel;
+	struct perf_cpu_map *cpus;
+	struct perf_thread_map *threads;
+	enum rlimit_action set_rlimit = NO_CHANGE;
+	bool progress;
+
+	works = calloc(perf_cpu_map__nr(evlist->core.all_cpus), sizeof(*works));
+	if (!works)
+		return -ENOMEM;
+
+	perf_cpu_map__for_each_cpu(cpuid, cpuidx, evlist->core.all_cpus) {
+		init_work(&works[cpuidx].work);
+		works[cpuidx].work.func = evlist__open_multithreaded_func;
+		works[cpuidx].evlist = evlist;
+		works[cpuidx].cpu = cpuid;
+		works[cpuidx].evsel_resume = evlist__first(evlist);
+	}
+
+reprepare:
+	evlist__for_each_entry(evlist, evsel) {
+		err = evsel__prepare_open(evsel, evsel->core.cpus,
+				evsel->core.threads);
+		if (err)
+			goto out;
+	}
+retry:
+	perf_cpu_map__for_each_cpu(cpuid, cpuidx, evlist->core.all_cpus) {
+		err = schedule_work_on(cpuid, &works[cpuidx].work);
+		if (err) {
+			workqueue_strerror(global_wq, err, errbuf, sizeof(errbuf));
+			pr_debug("schedule_work: %s\n", errbuf);
+			goto out;
+		}
+	}
+
+	err = flush_scheduled_work();
+	if (err) {
+		workqueue_strerror(global_wq, err, errbuf, sizeof(errbuf));
+		pr_debug("flush_scheduled_work: %s\n", errbuf);
+		goto out;
+	}
+
+	// check if any event was opened (progress = true)
+	progress = false;
+	perf_cpu_map__for_each_cpu(cpuid, cpuidx, evlist->core.all_cpus) {
+		if (works[cpuidx].progress) {
+			progress = true;
+			break;
+		}
+	}
+
+	perf_cpu_map__for_each_cpu(cpuid, cpuidx, evlist->core.all_cpus) {
+		peo_res = works[cpuidx].res.peo_res;
+
+		switch (peo_res.err) {
+		case PEO_SUCCESS:
+			continue;
+		case PEO_FALLBACK:
+			err = peo_res.rc;
+			break;
+		default:
+		case PEO_ERROR:
+			err = peo_res.rc;
+			goto out;
+		}
+
+		// fallback
+		evsel = works[cpuidx].evsel_err;
+		cpus = evsel->core.cpus;
+		cpu = works[cpuidx].cpu_err;
+		threads = evsel->core.threads;
+		thread = works[cpuidx].res.thread;
+
+		if (evsel__precise_ip_fallback(evsel))
+			goto retry;
+
+		if (evsel__ignore_missing_thread(evsel, cpus->nr, cpu,
+						threads, thread, err))
+			goto retry;
+
+		// increase rlimit only if no progress was made
+		if (progress)
+			set_rlimit = NO_CHANGE;
+		if (err == -EMFILE && evsel__increase_rlimit(&set_rlimit))
+			goto retry;
+
+		if (err != -EINVAL || cpu > 0 || thread > 0)
+			goto out;
+
+		if (evsel__detect_missing_features(evsel))
+			goto reprepare;
+
+		// no fallback worked, return the error
+		goto out;
+	}
+
+	err = 0;
+
+out:
+	free(works);
+
+	return err;
+}
+
+int evlist__open(struct evlist *evlist)
+{
+	int err;
+
 	/*
 	 * Default: one fd per CPU, all threads, aka systemwide
 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
@@ -1420,11 +1595,13 @@ int evlist__open(struct evlist *evlist)
 
 	evlist__update_id_pos(evlist);
 
-	evlist__for_each_entry(evlist, evsel) {
-		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
-		if (err < 0)
-			goto out_err;
-	}
+	if (perf_singlethreaded)
+		err = evlist__open_singlethreaded(evlist);
+	else
+		err = evlist__open_multithreaded(evlist);
+
+	if (err)
+		goto out_err;
 
 	return 0;
 out_err:
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id D9441C432BE
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:13 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id B88D461206
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:13 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240642AbhHUJWv (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:51 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34146 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234101AbhHUJWP (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:15 -0400
Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87A66C06114D;
        Sat, 21 Aug 2021 02:20:27 -0700 (PDT)
Received: by mail-ed1-x529.google.com with SMTP id i6so17675554edu.1;
        Sat, 21 Aug 2021 02:20:27 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=+Wxxt3YvhYd/zL8RWKEMmopxrwh/epV13mN5cuVdlwQ=;
        b=HvujytgwM4KQkb8ZI9Fpf6wYN7e/cUnmsjvoxkToQuW/QmtAVt7o9vZ2boBbGYOu8B
         LFXAvPRnnLLa2NHgTvfbHkeHY4NyhY9wvzUZoTHpdbGvcPwOpOGhDkTCpJc2pO42O4Yz
         FXBj4sd/9t5Rn3blSDcGnttBarREc0xo4a3RsuMsKQ+A5cf1eNfU2J13Si2I7Kn5bk1E
         d1XBFvzmMSjpehDRSQubuy2cFbtW74/0xCcASKK2VA58W6oLWStSRvYIbMk3n9JXyTAn
         EB5hXUMu+TNihx5JZB9Ew+9Y9B+o0diXjDawUlVc/loxu7Mz8jckW169ItxoS8CG0tV3
         0fVQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=+Wxxt3YvhYd/zL8RWKEMmopxrwh/epV13mN5cuVdlwQ=;
        b=DzArRZe6vOF7J3QGM1j3S8N+Dmo6/VFr01Ru62WVgIyGB6qJI45Zha/8sHC+QOS8IV
         TJHL82Hd1qqYwkSFMj3lpPO+jMgpkCYrGeg5RKXzZQwhS5nKwPIDXeXbXZUNNuBFA7LO
         1lf4i1H5FOE54vGt4FBZRkoduznJvf6Pzpmg3xZysWkQSdjJ9NXOBTpQOjjURdEYGCbU
         6Fq4uzQ6IY55B+06slsWyLbfCOdoTGg2Wl1sB5EFHGf9yfLRY03hg7ijo5QJLlBfYRur
         kYtK5E59GIKwxyDbsPu/V9lUK+Qb7+vvMcBB9EdY5HmNxyxYOPxonQGXUhKJwI2QNj7S
         592A==
X-Gm-Message-State: AOAM533sVFgiJMy0LVhqMFh52y/gs0dhA+BLNeIUxNBcRwQKm18vjKIF
        Ay22QrCq3PGoP9JjCievOEQ=
X-Google-Smtp-Source: ABdhPJznttDiEJ6KkNIBl23JXKkVU2T9HNvOPQgyOzYxqVW8+2RJdTp3KEbk5jSrUrLH7aFi8119/A==
X-Received: by 2002:a05:6402:518f:: with SMTP id q15mr26625616edd.226.1629537626130;
        Sat, 21 Aug 2021 02:20:26 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.25
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:25 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 30/37] perf record: use evlist__open_custom
Date:   Sat, 21 Aug 2021 11:19:36 +0200
Message-Id: <b05793fb68d8e0c50af9a6c600dc70a7f79fb806.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch replace the custom evlist opening implemented in record__open
with the new standard function evlist__open_custom.
Differently from before this patch, in case of a weak group all fds are
closed and then reopened (instead of just closing failing ones).

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/builtin-record.c | 63 ++++++++++++++++++++++++-------------
 1 file changed, 42 insertions(+), 21 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index cccc2d0f9977d5b3..dc9a814b2e7906fc 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -882,6 +882,37 @@ static int record__mmap(struct record *rec)
 	return record__mmap_evlist(rec, rec->evlist);
 }
 
+struct record_open_custom_fallback {
+	struct evlist_open_custom_fallback fallback;
+	struct record_opts *opts;
+	bool retry;
+};
+
+static bool record__open_fallback(struct evlist_open_custom_fallback *_fallback,
+				struct evlist *evlist, struct evsel *evsel, int err)
+{
+	char msg[BUFSIZ];
+	struct record_open_custom_fallback *fallback = container_of(_fallback,
+		struct record_open_custom_fallback, fallback);
+
+	if (evsel__fallback(evsel, -err, msg, sizeof(msg))) {
+		if (verbose > 0)
+			ui__warning("%s\n", msg);
+		return true;
+	}
+	if ((err == -EINVAL || err == -EBADF) &&
+		evsel->core.leader != &evsel->core &&
+		evsel->weak_group) {
+		evlist__reset_weak_group(evlist, evsel, true);
+		fallback->retry = true;
+		return false;
+	}
+
+	evsel__open_strerror(evsel, &fallback->opts->target, -err, msg, sizeof(msg));
+	ui__error("%s\n", msg);
+	return false;
+}
+
 static int record__open(struct record *rec)
 {
 	char msg[BUFSIZ];
@@ -890,6 +921,12 @@ static int record__open(struct record *rec)
 	struct perf_session *session = rec->session;
 	struct record_opts *opts = &rec->opts;
 	int rc = 0;
+	struct record_open_custom_fallback cust_fb = {
+		.fallback = {
+			.func = record__open_fallback
+		},
+		.opts = opts
+	};
 
 	/*
 	 * For initial_delay, system wide or a hybrid system, we need to add a
@@ -919,28 +956,12 @@ static int record__open(struct record *rec)
 
 	evlist__config(evlist, opts, &callchain_param);
 
-	evlist__for_each_entry(evlist, pos) {
-try_again:
-		if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
-			if (evsel__fallback(pos, errno, msg, sizeof(msg))) {
-				if (verbose > 0)
-					ui__warning("%s\n", msg);
-				goto try_again;
-			}
-			if ((errno == EINVAL || errno == EBADF) &&
-			    pos->core.leader != &pos->core &&
-			    pos->weak_group) {
-			        pos = evlist__reset_weak_group(evlist, pos, true);
-				goto try_again;
-			}
-			rc = -errno;
-			evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg));
-			ui__error("%s\n", msg);
-			goto out;
-		}
 
-		pos->supported = true;
-	}
+	do {
+		cust_fb.retry = false;
+		rc = evlist__open_custom(evlist, &cust_fb.fallback);
+	} while (cust_fb.retry);
+
 
 	if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) {
 		pr_warning(
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id A36E9C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:14 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 8A5A061186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:14 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S238468AbhHUJWw (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:52 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34144 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S238216AbhHUJWP (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:15 -0400
Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3ACD8C06114C;
        Sat, 21 Aug 2021 02:20:26 -0700 (PDT)
Received: by mail-ed1-x536.google.com with SMTP id g21so17635262edw.4;
        Sat, 21 Aug 2021 02:20:26 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=QInwCOuO2ckUJlld3UzzWnV+R3RPjh8yWuOELi0NyEQ=;
        b=f+GGTOdmQ56JFw+99caC9CxgbqDRllsngG7xH0A54iDf3Iggd+oPiexWzubco4j7Pa
         YzH4JuoSKdbMx2eLnvZGZTR2RGm9XrHeJuAjhXBJ9gAoGFpacA17ygDKum5PG+q8bzk5
         2sygZTXSFlWHO4PyvuUgmcn3OHmYZ7SMi53CwwfXaBvISEpkhelp39rUB90jeHQD0c4y
         8UQY/7CjM01f4CBMy08tYuFVmhUe8VmmL7tkGkCMM4ZwEsFb78WOcrCU62Rq6jRF+kxv
         Cy82xQot62x3/fU65XdjbNg6+Ma1azNJX6PJZEhzMFatzugymXwEjgLJzkikGoi5g3q4
         KObg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=QInwCOuO2ckUJlld3UzzWnV+R3RPjh8yWuOELi0NyEQ=;
        b=nWCjaV3JJYdjG0zydvC+jctgeg60sAFMRMsuGdD4C+9zMesAw4A86+nnGMzwQWJqZp
         zNBvxn0av3LE1UDp17Zd1A1ThUbfJN4xXZjTr1SX6yUWX7v9yBFTUkwoA/n9hvgDWpgJ
         1+VERGrVGdc12a9HgAlt2wg952iopNLncGUYiye9MbLhk95t6geSDecTbeIg6XYbCgHh
         /OGANaz8x+a1CZ5uQRSgio+qs+ZOSBhJciI6ZEYmgYk0wh4utvezN1GF7KbOnla49g66
         gklbSz4ysb3ytDvUY+z7iKz1mKdVqMJVpJ5a9EvydkisadhWhvVngNcOxzfNcNidqJ//
         Wm6g==
X-Gm-Message-State: AOAM530AdI7s5A88I9aRL9pBNsiVJMwg8bsfK6EstjosegBxhMYHMvVq
        i/fWL4y86BQFwB1+RJYT6YY=
X-Google-Smtp-Source: ABdhPJw1SvoUmyQKhMERoQNYecOdUDpzkTxbqNBSikW1lrmB4QjUyjajuRSr6cptZDKzh+So31wk7g==
X-Received: by 2002:a05:6402:10d9:: with SMTP id p25mr26451432edu.51.1629537624769;
        Sat, 21 Aug 2021 02:20:24 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.23
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:24 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 29/37] perf evlist: add custom fallback to evlist__open
Date:   Sat, 21 Aug 2021 11:19:35 +0200
Message-Id: <6109f52f83bbb9d6782969a5e6fc66f5cab7abd8.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds the function evlist__open_custom, which makes it possible
to provide a custom fallback mechanism to evsel__open.
This function will be used in record__open in the following patch.
This could also be used to adapt perf-stat way of opening to the new
multithreaded mechanism.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/evlist.c | 27 +++++++++++++++++++++------
 tools/perf/util/evlist.h |  9 +++++++++
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 3472038d719ec7d4..22b9607dcc637a2d 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1405,15 +1405,20 @@ static int evlist__create_syswide_maps(struct evlist *evlist)
 	return err;
 }
 
-static int evlist__open_singlethreaded(struct evlist *evlist)
+static int evlist__open_singlethreaded(struct evlist *evlist,
+					struct evlist_open_custom_fallback *cust_fb)
 {
 	struct evsel *evsel;
 	int err;
 
 	evlist__for_each_entry(evlist, evsel) {
+try_again:
 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
-		if (err < 0)
+		if (err < 0) {
+			if (cust_fb && cust_fb->func(cust_fb, evlist, evsel, err))
+				goto try_again;
 			return err;
+		}
 	}
 
 	return 0;
@@ -1469,7 +1474,8 @@ static void evlist__open_multithreaded_func(struct work_struct *_work)
 	}
 }
 
-static int evlist__open_multithreaded(struct evlist *evlist)
+static int evlist__open_multithreaded(struct evlist *evlist,
+				struct evlist_open_custom_fallback *cust_fb)
 {
 	int cpu, cpuid, cpuidx, thread, err;
 	struct evlist_open_work *works;
@@ -1567,6 +1573,9 @@ static int evlist__open_multithreaded(struct evlist *evlist)
 		if (evsel__detect_missing_features(evsel))
 			goto reprepare;
 
+		if (cust_fb && cust_fb->func(cust_fb, evlist, evsel, err))
+			goto reprepare;
+
 		// no fallback worked, return the error
 		goto out;
 	}
@@ -1579,7 +1588,8 @@ static int evlist__open_multithreaded(struct evlist *evlist)
 	return err;
 }
 
-int evlist__open(struct evlist *evlist)
+int evlist__open_custom(struct evlist *evlist,
+			struct evlist_open_custom_fallback *cust_fb)
 {
 	int err;
 
@@ -1596,9 +1606,9 @@ int evlist__open(struct evlist *evlist)
 	evlist__update_id_pos(evlist);
 
 	if (perf_singlethreaded)
-		err = evlist__open_singlethreaded(evlist);
+		err = evlist__open_singlethreaded(evlist, cust_fb);
 	else
-		err = evlist__open_multithreaded(evlist);
+		err = evlist__open_multithreaded(evlist, cust_fb);
 
 	if (err)
 		goto out_err;
@@ -1610,6 +1620,11 @@ int evlist__open(struct evlist *evlist)
 	return err;
 }
 
+int evlist__open(struct evlist *evlist)
+{
+	return evlist__open_custom(evlist, NULL);
+}
+
 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
 {
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index b0c2da0f9755b2d1..cb753b4c4e70b4ba 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -161,6 +161,15 @@ void evlist__mmap_consume(struct evlist *evlist, int idx);
 int evlist__open(struct evlist *evlist);
 void evlist__close(struct evlist *evlist);
 
+struct evlist_open_custom_fallback;
+typedef bool (*evlist_open_custom_fallback_fn)(struct evlist_open_custom_fallback *fallback,
+					struct evlist *evlist, struct evsel *evsel, int err);
+struct evlist_open_custom_fallback {
+	evlist_open_custom_fallback_fn func;
+};
+int evlist__open_custom(struct evlist *evlist, struct evlist_open_custom_fallback *cust_fb);
+
+
 struct callchain_param;
 
 void evlist__set_id_pos(struct evlist *evlist);
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id EC539C4320E
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:14 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id CD29361206
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:14 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234663AbhHUJWw (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:52 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34154 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233774AbhHUJWP (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:15 -0400
Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1D7EC06114F;
        Sat, 21 Aug 2021 02:20:28 -0700 (PDT)
Received: by mail-ed1-x52a.google.com with SMTP id i6so17675592edu.1;
        Sat, 21 Aug 2021 02:20:28 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=FRWPQ8HxhbjIOSVCF9fueneGQHgaEtyHF4yxY4sSzec=;
        b=JIHhV+40z3DGp/eALzG7aaet5twMq1a/vpBct8kXXSCGkw+XCFmERWJNER2GFveio/
         o0v9jX4CHSnNnHTKAaLukGGLDp5nxdG437LX+P2XTSMmDD4FLHlGq3MlDtZZQ5LwCqNu
         h5MM8t54E+8Hp8LRtBDgRjk+uopp2T0BAKJz5UMjtglzVwaHBBITdDWWHTDj9rVOZK9g
         5Tg/fIEXHBhA0YDvBe1aUDPliS/qD7LPuuWZlmkDmB6VCGcoYYkmhFxY8zRBZiRr9f0G
         RXU2fUxbSdKXMwZEvSlA+gN7CyjpG9SfpM8uJYVeqTn5rm5OaggXF6hRmQ/7FBRUO86d
         jV5w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=FRWPQ8HxhbjIOSVCF9fueneGQHgaEtyHF4yxY4sSzec=;
        b=lIyfXcLrBbv/h41p0lBUBw4l1JzJsVSYRyUJQWF78Jl369H0ujlKGts2aq0QhzIs9y
         /k7AcaMJKdAnhJmoesJXcJp6st1cUiuivM+olkEtx5lM+LfZFLePTHkpS5Rjkwsn8zlS
         w/YO3K8H2Io2NA4Yu/VILspK+kcNELKvtwmh7Evp0L9Lw8L8XM0QvDZIMUu+2clOHRdA
         p3kDONxyQaOIrZz+jdWGB3I+aPggpmGE6lW7lUPRY2YwjjGsJe+5OwKKBEGC4U6OR/3K
         /AFsKPoeu2l0vgyHBe1mDnaCM2qAasXgde4efXvAUhATykuYOEcA6sMcllI5aE5+l7SI
         +BPg==
X-Gm-Message-State: AOAM531Np01P85TtlhrH9X58aRkVWT1M4ENp7G4dbiuOzkwD7ffenQdx
        qYxjyyPvHiRI0IdH2tuoukE=
X-Google-Smtp-Source: ABdhPJzrnyo88qRgBmIhxwLlVU5nAHryT91dUbv2RLu+LkECJzI0/fIabuml8GZTahoyGQD+6eqbmw==
X-Received: by 2002:a05:6402:220f:: with SMTP id cq15mr5378154edb.142.1629537627317;
        Sat, 21 Aug 2021 02:20:27 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.26
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:27 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 31/37] tools lib/subcmd: add OPT_UINTEGER_OPTARG option type
Date:   Sat, 21 Aug 2021 11:19:37 +0200
Message-Id: <c46749b3dff796729078352ff164d363457a3587.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds OPT_UINTEGER_OPTARG, which is the same as OPT_UINTEGER,
but also makes it possible to use the option without any value, setting
the variable to a default value, d.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/lib/subcmd/parse-options.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/lib/subcmd/parse-options.h b/tools/lib/subcmd/parse-options.h
index d2414144eb8c9927..41b9b942504d398e 100644
--- a/tools/lib/subcmd/parse-options.h
+++ b/tools/lib/subcmd/parse-options.h
@@ -133,6 +133,7 @@ struct option {
 #define OPT_SET_PTR(s, l, v, h, p)  { .type = OPTION_SET_PTR, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (p) }
 #define OPT_INTEGER(s, l, v, h)     { .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .help = (h) }
 #define OPT_UINTEGER(s, l, v, h)    { .type = OPTION_UINTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned int *), .help = (h) }
+#define OPT_UINTEGER_OPTARG(s, l, v, d, h)    { .type = OPTION_UINTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned int *), .help = (h), .flags = PARSE_OPT_OPTARG, .defval = (intptr_t)(d) }
 #define OPT_LONG(s, l, v, h)        { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) }
 #define OPT_ULONG(s, l, v, h)        { .type = OPTION_ULONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned long *), .help = (h) }
 #define OPT_U64(s, l, v, h)         { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) }
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id AB43EC432BE
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:16 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 90DCA61186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:16 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234682AbhHUJWy (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:54 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34110 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233997AbhHUJWc (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:32 -0400
Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B341C061151;
        Sat, 21 Aug 2021 02:20:31 -0700 (PDT)
Received: by mail-ed1-x52b.google.com with SMTP id bo19so17596097edb.9;
        Sat, 21 Aug 2021 02:20:31 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=QZgLUS1D1n1+osF5QCdhScd9/ZmMYRc9ErCmnvjzL/o=;
        b=aXZ7T+Da/te6Uru6w9k13v1OvEQIVyjAlHMvzTX1OMB1TOzHXd+8FIUWDnImHdut8v
         l+7P9LQmIPxcWjI/fQRy70tNtZTXRVS2hjJ/2Wm9E2H3LQY9hw2lP+mzh/wwJYjGVx1V
         wUBLO4h6zs6iUTlaKnA5C9m1KAw2ry+zadZNW/rZA59ZBGltah2VSaO8fFfFUmQlP5DE
         32iOqmlLqady8+ykIaJ39vxA7U8Z1ua7WuGasHmSdqDVx35ADu1tjWt5Gc+/h4LJ9nBg
         eUUpBKLqvQHHulayj4Y9gpax7ySY36ciDJy1ftqJgK2PXlzpNpqhNE9ExgrqFCx30Ooh
         TRag==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=QZgLUS1D1n1+osF5QCdhScd9/ZmMYRc9ErCmnvjzL/o=;
        b=C5nOZxzdZWtnCu8ngwhwgBp6KngZMV2TRbs1Bk5Rp4O5ynMwJzIrPW3bG8aE4zcSQ6
         Cy4eyLuOvt82ZWsxhmShlwBSjK0kAG2MLbulTGHAb86uYuZ0E74F+E5Fco0VuOqU1HYn
         TIA0yhViMJiz0AQKiHYFNBtNv3HZFNPvtIquSeaOFabhSkRi81JxBhdwbHrTrh6DFdGU
         l9emkudzXp2D5mX0KfpZgNeR149+OZZwexA9YJu32Hti7xVe0Hcph0dzvPaTkTxxdlBP
         tP9RWBqIjyxjMp27d5xCMZaoLJWJe5qMqPJm8xUV/5hKFxrl/M2t1CvK5Fj2/ed8rAma
         m5qg==
X-Gm-Message-State: AOAM5321AXtLQcRJ+rE/VML2xraMTXIvw+jFAD6UtZ8NCgZgcMD38NBc
        Vk89AWaQxKmrf1vEa2ARVPYbIW6dO0vRamxe0Ck=
X-Google-Smtp-Source: ABdhPJxh7kN8bNFHQBGgpqoc5aZ+/dAV5K9rlk7/zXzEEDSfwpFCqGS1UFt2jkbnAsHgE0Y2v1LI3g==
X-Received: by 2002:a05:6402:1011:: with SMTP id c17mr26254970edu.144.1629537629951;
        Sat, 21 Aug 2021 02:20:29 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.28
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:29 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 33/37] perf record: pin threads to monitored cpus if enough threads available
Date:   Sat, 21 Aug 2021 11:19:39 +0200
Message-Id: <d2218d8fc69eb7c9e85454af5065523f86c37ff1.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch sets the affinity of the workqueue threads to pin them to
each monitored CPU in case the --threads option is set with enough
threads and evlist multithreading is enabled.

This yields a better performance for the evlist operations, since
affinity need not be sent by each thread everytime.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/builtin-record.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 7802a0e25f631fac..e2d2445e05d7c07a 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -2945,6 +2945,16 @@ int cmd_record(int argc, const char **argv)
 			pr_err("setup_global_workqueue: %s\n", errbuf);
 			goto out;
 		}
+
+		if ((int)rec->opts.nr_threads >= rec->evlist->core.all_cpus->nr
+				&& rec->opts.multithreaded_evlist) {
+			err = workqueue_set_affinities_cpu(global_wq, rec->evlist->core.all_cpus);
+			if (err) {
+				workqueue_strerror(global_wq, err, errbuf, sizeof(errbuf));
+				pr_err("workqueue_set_affinities_cpu: %s\n", errbuf);
+				goto out;
+			}
+		}
 	}
 
 	err = __cmd_record(&record, argc, argv);
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 58617C4338F
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:17 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 3D83C61207
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:17 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240968AbhHUJWz (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:55 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34104 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234073AbhHUJWc (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:32 -0400
Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5DAAC061152;
        Sat, 21 Aug 2021 02:20:32 -0700 (PDT)
Received: by mail-ej1-x629.google.com with SMTP id z20so25183200ejf.5;
        Sat, 21 Aug 2021 02:20:32 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=3Nz5cuYI+OBcCvgsxocGu8/AZ30FKdwuuYGkZjJe3RI=;
        b=XlfGwAjvHqfY+y4udXph83bMIIMSpq+LH04Tv1UnmEAhu+SI9fZUUDs8BQlQmhWdPn
         Y7cfA0zb+kADyU9y1OZPZ9t2mjEJQBD1OwjPxi8/76ohjBipKsx778JAaxcqaoVmQxLM
         maTYh5GJETMpfc91FdgA8MSnIw+7IyOEkLiehoCzgC7iQnRoQEC1wXDZ3zADk1mBfOgP
         dIGgtMI1P6+bhVPv0zCmRpO5ayN+CrHb8/nNFODRBnG+6uZy6aCkNli3/YJysAoJ5c2o
         3bgrIeA8j/3btQV+VNS4CJ/77cTlPqsbQWmq/WUfDMr2fR90HRF6MT1jI/KimY2zRw+M
         1YhA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=3Nz5cuYI+OBcCvgsxocGu8/AZ30FKdwuuYGkZjJe3RI=;
        b=JoI+5hcJi7GLEixgDoCqCkjM78MJUs7ZcPtpJLgyweEh2DuBx6fwdvzSw0S9gvvY0f
         1Wq3r+oB/6L2FF9iJqnyCrWzqYdvPTa2NNdA/MvGgRaWBI3EUGtrGO5Vo2RAqGubWCLG
         4/UQ9oOL4+wscAIcufWKNikSXB6XeAs7+yB5AduYGtSP+B/01bboDB+pvj1gyyQk+O5i
         cL/ty0sstoCzrhYwhbyAYGPkWXBocGQzFPik1W0/9WZ6jQHdvHNuYgkVIIdnTdrNYk6c
         OUfv2HRgKTSPU4x0XDysVh+j/dyg+9z9sN/CYONtfSE6T7PHT/ht0ScMECFEqNiD4mhR
         yjPQ==
X-Gm-Message-State: AOAM530jboCXGa/6u0KRQJxONLv8q4jfCGNtUjYC+5ipBrkGL8hcqwux
        Lmn6ci1AskNOn5LzAmwzCis=
X-Google-Smtp-Source: ABdhPJwEhzv1vtyyOdarPLAkKYHQELwvwBMh67mJUrR4i0LAMM810AeJvCpK5/M7CjiVNVh7D2OytQ==
X-Received: by 2002:a17:906:ac1:: with SMTP id z1mr25592170ejf.261.1629537631322;
        Sat, 21 Aug 2021 02:20:31 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.30
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:30 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 34/37] perf record: apply multithreading in init and fini phases
Date:   Sat, 21 Aug 2021 11:19:40 +0200
Message-Id: <f73e485eb0d29539722da396555d9fcbd0267642.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch grows the multithreaded portion of perf-record, marked by the
perf_set_multithreaded and perf_set_singlethreaded functions to the whole
init and fini part of __cmd_record.
By doing so, perf-record can take advantage of the parallelized evlist
operations (open, enable, disable, close).

This patch also needs to handle the case in which evlist and synthesis
multithreading are not enabled at the same time. Therefore, in
record__synthesize multithreading is enabled/disabled and then
disabled/renabled if needed.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/builtin-record.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index e2d2445e05d7c07a..db9ec08db672f994 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -1411,6 +1411,7 @@ static int record__synthesize(struct record *rec, bool tail)
 	struct perf_tool *tool = &rec->tool;
 	int err = 0;
 	event_op f = process_synthesized_event;
+	bool perf_was_singlethreaded = perf_singlethreaded;
 
 	if (rec->opts.tail_synthesize != tail)
 		return 0;
@@ -1499,12 +1500,16 @@ static int record__synthesize(struct record *rec, bool tail)
 	if (rec->opts.multithreaded_synthesis) {
 		perf_set_multithreaded();
 		f = process_locked_synthesized_event;
+	} else {
+		perf_set_singlethreaded();
 	}
 
 	err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads,
 					    f, opts->sample_address);
 
-	if (rec->opts.multithreaded_synthesis)
+	if (!perf_was_singlethreaded && perf_singlethreaded)
+		perf_set_multithreaded();
+	if (perf_was_singlethreaded && !perf_singlethreaded)
 		perf_set_singlethreaded();
 
 out:
@@ -1735,6 +1740,9 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 
 	record__uniquify_name(rec);
 
+	if (rec->opts.multithreaded_evlist)
+		perf_set_multithreaded();
+
 	if (record__open(rec) != 0) {
 		err = -1;
 		goto out_child;
@@ -1877,6 +1885,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 		}
 	}
 
+	// disable locks in the main thread since there is no multithreading
+	if (rec->opts.multithreaded_evlist)
+		perf_set_singlethreaded();
+
 	trigger_ready(&auxtrace_snapshot_trigger);
 	trigger_ready(&switch_output_trigger);
 	perf_hooks__invoke_record_start();
@@ -1998,6 +2010,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 		}
 	}
 
+	// reenable multithreading for evlist
+	if (rec->opts.multithreaded_evlist)
+		perf_set_multithreaded();
+
 	trigger_off(&auxtrace_snapshot_trigger);
 	trigger_off(&switch_output_trigger);
 
@@ -2099,6 +2115,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 
 	if (!opts->no_bpf_event)
 		evlist__stop_sb_thread(rec->sb_evlist);
+
+	// disable multithreaded mode on exit
+	if (rec->opts.multithreaded_evlist)
+		perf_set_singlethreaded();
 	return status;
 }
 
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 11F0DC43214
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:18 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id EA698611EF
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:17 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233721AbhHUJWz (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:55 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34068 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234127AbhHUJWQ (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:16 -0400
Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18A7EC061150;
        Sat, 21 Aug 2021 02:20:30 -0700 (PDT)
Received: by mail-ed1-x52c.google.com with SMTP id i6so17675618edu.1;
        Sat, 21 Aug 2021 02:20:30 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=7abl+gAZuH7R6YORSyN+O64mVlc0Mbp5zWJWP/oVwnA=;
        b=E9vTuDsNMixeT+DjzghClvuYFRyrQnKDWNDbM+PK/N/6dMdUEp9CHjj6xc/dp1EKmV
         y6p4+DVNc2CLvtA+2aY7n+7D+eP79qua6K6PGLZVFK5VB7EZZ7rQO9KfuNySBTPtqX/p
         IsIvGs0+5aooMqWlAudIkWE09LMuYugUYTXirkKMBv/65Owm8uPJgDRfEtZ0wKMnHWBU
         4KC+jS7fUm3tSeHmDKxH+z0u2lVOn7KEAvz7FnA3C7eaSIgmkQE4YZFCJxMWIdYdHB4G
         2oa4sdod8wJxKKmt3M8BDdofjf19FtSUrDTjeuI5adV7D+AyyhiV3PJzgJHmBqqBnZB0
         Qa5g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=7abl+gAZuH7R6YORSyN+O64mVlc0Mbp5zWJWP/oVwnA=;
        b=Y2l0pJ89Y0k6mtKDQaIEmfuGx4tazfN7dKMR5+LjJAfL/m6UhYfT5zCZ7wVYfcXnTO
         Dmy/CwQyMgkk9828Kyr0J5NY3t1qNxShuedAW2mwgg9W6zs7t541hTmk6W5bcvXOWypQ
         WKLKtf29miMouqrQ5ZQHl+0UlIQZ2mVNTm5T0XWEVI7qYdfDQD5+6ku7YvsgkLezEVmc
         44mW1wGHALb8YIJVtduWmLipWgC62uZrqcRcWLxQqPv/QOnOlAE1p644TQBFuetavBs5
         0XIaRx/REGcb/oE3ugym+MsgE4o3tS/hIU8+bqZBHFZa+/VVTUuyRgMyoDpCNQbHcklt
         QBYA==
X-Gm-Message-State: AOAM5310zQ6/xB+bne4YOdVUyHF+e7bOuGXgqQ0FIqFhQ7Aw8SvvMgIP
        L63mr/gmOKUXkym4xl9tkGI=
X-Google-Smtp-Source: ABdhPJzGKDaoAgL73DeMX395cKj5kss4APYUkRWJKasFLanE0ZJvqxWB38R52XdzUUIbIV/waiRqsg==
X-Received: by 2002:a50:ff03:: with SMTP id a3mr26553586edu.100.1629537628637;
        Sat, 21 Aug 2021 02:20:28 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.27
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:28 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 32/37] perf record: add --threads option
Date:   Sat, 21 Aug 2021 11:19:38 +0200
Message-Id: <fc333a628d66e23ac2af6ebd0f16f9b39f9563ba.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds a new --threads option to perf-record, which sets the
number of threads to use for multithreaded operations (synthesis and, in
following patches, evlist).

The new option will override the --num-thread-synthesize option if set.
By default, no thread will be used. The option can also be passed
without any argument, setting the number of threads to the number of
online cpus.

Furthermore, two new perf configs are added to selectively disable
multithreading in either synthesis and evlist.

To keep the same behaviour for --num-thread-synthesize, setting only that
option will cause multithreading to be enabled only in synthesis (by
overriding the perf config options for multithreaded synthesis and
evlist).

Examples:
$ ./perf record --threads
uses one thread per cpu for synthesis (and evlist in following patches)

$ ./perf record --threads 2 --num-thread-synthesize 4
the two options shouldn't be mixed, the behaviour would be using 2
threads for everything (4 is ignored)

$ ./perf record --num-thread-synthesize 4
same behaviour as before: 4 threads, but only for synthesis

$ ./perf config record.multithreaded_synthesis=no
$ ./perf record --threads
uses multithreading for everything but synthesis (i.e. evlist in
following patches)

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/Documentation/perf-record.txt |  9 ++++++
 tools/perf/builtin-record.c              | 35 +++++++++++++++++++-----
 tools/perf/util/record.h                 |  3 ++
 3 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index f1079ee7f2ecf4a8..f5525e3a36e0cf2a 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -629,6 +629,15 @@ appended unit character - B/K/M/G
 	The number of threads to run when synthesizing events for existing processes.
 	By default, the number of threads equals 1.
 
+--threads::
+	The number of threads to use for operations which have multithreaded
+	support (synthesize, evlist).
+	Setting this option overrides --num-thread-synthesize.
+	You can selectively disable any of the multithreaded operations through
+	perf-config record.multithreaded-{synthesis,evlist}.
+	By default, the number of threads equals 1.
+	Setting this option without any parameter sets it to the number of online cpus.
+
 ifdef::HAVE_LIBPFM[]
 --pfm-events events::
 Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index dc9a814b2e7906fc..7802a0e25f631fac 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -1496,7 +1496,7 @@ static int record__synthesize(struct record *rec, bool tail)
 	if (err < 0)
 		pr_warning("Couldn't synthesize cgroup events.\n");
 
-	if (rec->opts.nr_threads_synthesize > 1) {
+	if (rec->opts.multithreaded_synthesis) {
 		perf_set_multithreaded();
 		f = process_locked_synthesized_event;
 	}
@@ -1504,7 +1504,7 @@ static int record__synthesize(struct record *rec, bool tail)
 	err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->core.threads,
 					    f, opts->sample_address);
 
-	if (rec->opts.nr_threads_synthesize > 1)
+	if (rec->opts.multithreaded_synthesis)
 		perf_set_singlethreaded();
 
 out:
@@ -2188,6 +2188,12 @@ static int perf_record_config(const char *var, const char *value, void *cb)
 			rec->opts.nr_cblocks = nr_cblocks_default;
 	}
 #endif
+	if (!strcmp(var, "record.multithreaded-synthesis"))
+		rec->opts.multithreaded_synthesis = perf_config_bool(var, value);
+
+	if (!strcmp(var, "record.multithreaded-evlist"))
+		rec->opts.multithreaded_evlist = perf_config_bool(var, value);
+
 
 	return 0;
 }
@@ -2434,6 +2440,9 @@ static struct record record = {
 		},
 		.mmap_flush          = MMAP_FLUSH_DEFAULT,
 		.nr_threads_synthesize = 1,
+		.nr_threads          = 1,
+		.multithreaded_evlist = true,
+		.multithreaded_synthesis = true,
 		.ctl_fd              = -1,
 		.ctl_fd_ack          = -1,
 	},
@@ -2640,6 +2649,9 @@ static struct option __record_options[] = {
 	OPT_UINTEGER(0, "num-thread-synthesize",
 		     &record.opts.nr_threads_synthesize,
 		     "number of threads to run for event synthesis"),
+	OPT_UINTEGER_OPTARG(0, "threads",
+		     &record.opts.nr_threads, UINT_MAX,
+		     "number of threads to use"),
 #ifdef HAVE_LIBPFM
 	OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
 		"libpfm4 event selector. use 'perf list' to list available events",
@@ -2915,10 +2927,19 @@ int cmd_record(int argc, const char **argv)
 		rec->opts.comp_level = comp_level_max;
 	pr_debug("comp level: %d\n", rec->opts.comp_level);
 
-	if (rec->opts.nr_threads_synthesize == UINT_MAX)
-		rec->opts.nr_threads_synthesize = sysconf(_SC_NPROCESSORS_ONLN);
-	if (rec->opts.nr_threads_synthesize > 1) {
-		err = setup_global_workqueue(rec->opts.nr_threads_synthesize);
+	if (rec->opts.nr_threads <= 1) {
+		rec->opts.multithreaded_evlist = false;
+		if (rec->opts.nr_threads_synthesize > 1) {
+			rec->opts.multithreaded_synthesis = true;
+			rec->opts.nr_threads = rec->opts.nr_threads_synthesize;
+		} else {
+			rec->opts.multithreaded_synthesis = false;
+		}
+	}
+	if (rec->opts.nr_threads == UINT_MAX)
+		rec->opts.nr_threads = sysconf(_SC_NPROCESSORS_ONLN);
+	if (rec->opts.nr_threads > 1) {
+		err = setup_global_workqueue(rec->opts.nr_threads);
 		if (err) {
 			create_workqueue_strerror(global_wq, errbuf, sizeof(errbuf));
 			pr_err("setup_global_workqueue: %s\n", errbuf);
@@ -2928,7 +2949,7 @@ int cmd_record(int argc, const char **argv)
 
 	err = __cmd_record(&record, argc, argv);
 
-	if (rec->opts.nr_threads_synthesize > 1)
+	if (rec->opts.nr_threads > 1)
 		teardown_global_workqueue();
 out:
 	bitmap_free(rec->affinity_mask.bits);
diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h
index 68f471d9a88b2b36..9c47a7904a43ffc7 100644
--- a/tools/perf/util/record.h
+++ b/tools/perf/util/record.h
@@ -74,6 +74,9 @@ struct record_opts {
 	int	      mmap_flush;
 	unsigned int  comp_level;
 	unsigned int  nr_threads_synthesize;
+	unsigned int  nr_threads;
+	bool	      multithreaded_synthesis;
+	bool	      multithreaded_evlist;
 	int	      ctl_fd;
 	int	      ctl_fd_ack;
 	bool	      ctl_fd_close;
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY,
	USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 01E89C4320A
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:20 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id DF6BC61186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:19 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240984AbhHUJW5 (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:57 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34078 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234117AbhHUJWc (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:32 -0400
Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E4FCC061153;
        Sat, 21 Aug 2021 02:20:34 -0700 (PDT)
Received: by mail-ej1-x62b.google.com with SMTP id bt14so25230366ejb.3;
        Sat, 21 Aug 2021 02:20:34 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=a/3m71kP0tYYLOMpvrgSKthU0QKRYTlQxU81VqWCx9s=;
        b=h/o/tTy2sYra0Z5LDpF/y/DoixhjUiOfLebPC7Rky5s8foQj/crzoXJFtUSV7el98k
         kyXQWsGaFFhpPN9URIVO9wBzWmM/x9vPRFaK+PZE+fe5jTTJQy+js9eykztwU0rlcTmU
         QsGysI70+aY6WYjnaOvQC47YpgzbpwwD/oCtlJoeLdFEHXHL1NhXpH7EOgu/gp8Sg6Fu
         DSGYmZ+vSOImtYI6ImDViFuZNX40sq22PukcEY8NW3ZF4vDYxuB4PW8dbeb2YqqnxOom
         0F2lRj+FU9Rc1SRw8YwvmF1vAbqsgi6HPuvP0SGge1u15Ar60vEnxCXuM+F9ibpe3ToI
         2cJw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=a/3m71kP0tYYLOMpvrgSKthU0QKRYTlQxU81VqWCx9s=;
        b=KExSUBP3siULC2H8pZupoWITXwZRQhCitYdjeqPSViRCB//EmpvJZSkgxUsOxQHKtf
         CWVsbhwiuzYqEjBml7JSihtfOmK1iiNZSzzyh/ZjjFKaxIMFX0XR16/f16RQZen2h24b
         jgMLYeA7qdsPM+JJtF48dD4HiyuSUerKHUoTHmVh163RtmaiiHhT1rrd7lnY9vwoNqVQ
         A0jUfqWp0vSw/qNNQw/dlfGXXOFOOyYYZxaSHuDmvuw5/Ww6ZsAAJeRTQcwA64QyzQs8
         vFLVRhZiXpClYKgMHrmq83dJ0plOOmDRFiQuma//kBII7tLV9OgbWZaPHCQ1zjp4YmTc
         Tsug==
X-Gm-Message-State: AOAM533nSoKjJNF4+M2JKG3c/w4z2gn/jL6Fr3P8e4wCPbl44juCP8lF
        TB8OUnOKWnyQJFhe962hAQjEBAaEbrlj8RwaqxA=
X-Google-Smtp-Source: ABdhPJyvyfWAk6gfVyzKvye/3Td3EzcXlBFfZ38ePuBh4t2TM/Z2PKThH+yw05dhhZwEEwbDOQYGQA==
X-Received: by 2002:a17:906:b890:: with SMTP id hb16mr25194842ejb.383.1629537632804;
        Sat, 21 Aug 2021 02:20:32 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.31
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:32 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 35/37] perf test/evlist-open-close: add multithreading
Date:   Sat, 21 Aug 2021 11:19:41 +0200
Message-Id: <e01604581141e02aa29f1940879971c7e1ed791e.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds the new option -j/--threads to use multiple threads in
the evlist operations in the evlist-open-close benchmark.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/bench/evlist-open-close.c | 46 ++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 6 deletions(-)

diff --git a/tools/perf/bench/evlist-open-close.c b/tools/perf/bench/evlist-open-close.c
index 674cb14cbaa9ef2c..c18aa85725281795 100644
--- a/tools/perf/bench/evlist-open-close.c
+++ b/tools/perf/bench/evlist-open-close.c
@@ -12,6 +12,8 @@
 #include "../util/parse-events.h"
 #include "internal/threadmap.h"
 #include "internal/cpumap.h"
+#include "../util/util.h"
+#include "../util/workqueue/workqueue.h"
 #include <linux/perf_event.h>
 #include <linux/kernel.h>
 #include <linux/time64.h>
@@ -35,7 +37,8 @@ static struct record_opts opts = {
 		.default_per_cpu = true,
 	},
 	.mmap_flush          = MMAP_FLUSH_DEFAULT,
-	.nr_threads_synthesize = 1,
+	.nr_threads          = 1,
+	.multithreaded_evlist = true,
 	.ctl_fd              = -1,
 	.ctl_fd_ack          = -1,
 };
@@ -51,6 +54,7 @@ static const struct option options[] = {
 	OPT_STRING('t', "tid", &opts.target.tid, "tid", "record events on existing thread id"),
 	OPT_STRING('u', "uid", &opts.target.uid_str, "user", "user to profile"),
 	OPT_BOOLEAN(0, "per-thread", &opts.target.per_thread, "use per-thread mmaps"),
+	OPT_UINTEGER_OPTARG('j', "threads", &opts.nr_threads, UINT_MAX, "Number of threads to use"),
 	OPT_END()
 };
 
@@ -106,18 +110,32 @@ static struct evlist *bench__create_evlist(char *evstr)
 
 static int bench__do_evlist_open_close(struct evlist *evlist)
 {
-	char sbuf[STRERR_BUFSIZE];
-	int err = evlist__open(evlist);
+	char sbuf[WORKQUEUE_STRERR_BUFSIZE];
+	int err = -1, ret;
+
+	if (opts.nr_threads > 1) {
+		err = setup_global_workqueue(opts.nr_threads);
+		if (err) {
+			create_workqueue_strerror(global_wq, sbuf, sizeof(sbuf));
+			pr_err("setup_global_workqueue: %s\n", sbuf);
+			return err;
+		}
+		if (evlist->core.all_cpus->nr <= workqueue_nr_threads(global_wq))
+			workqueue_set_affinities_cpu(global_wq, evlist->core.all_cpus);
 
+		perf_set_multithreaded();
+	}
+
+	err = evlist__open(evlist);
 	if (err < 0) {
 		pr_err("evlist__open: %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
-		return err;
+		goto out;
 	}
 
 	err = evlist__mmap(evlist, opts.mmap_pages);
 	if (err < 0) {
 		pr_err("evlist__mmap: %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
-		return err;
+		goto out;
 	}
 
 	evlist__enable(evlist);
@@ -125,7 +143,19 @@ static int bench__do_evlist_open_close(struct evlist *evlist)
 	evlist__munmap(evlist);
 	evlist__close(evlist);
 
-	return 0;
+out:
+	if (opts.nr_threads > 1) {
+		ret = teardown_global_workqueue();
+		if (ret) {
+			destroy_workqueue_strerror(err, sbuf, sizeof(sbuf));
+			pr_err("teardown_global_workqueue: %s\n", sbuf);
+			err = ret;
+		}
+
+		perf_set_singlethreaded();
+	}
+
+	return err;
 }
 
 static int bench_evlist_open_close__run(char *evstr)
@@ -143,6 +173,7 @@ static int bench_evlist_open_close__run(char *evstr)
 
 	init_stats(&time_stats);
 
+	printf("  Number of workers:\t%u\n", opts.nr_threads);
 	printf("  Number of cpus:\t%d\n", evlist->core.cpus->nr);
 	printf("  Number of threads:\t%d\n", evlist->core.threads->nr);
 	printf("  Number of events:\t%d (%d fds)\n",
@@ -226,6 +257,9 @@ int bench_evlist_open_close(int argc, const char **argv)
 		exit(EXIT_FAILURE);
 	}
 
+	if (opts.nr_threads == UINT_MAX)
+		opts.nr_threads = sysconf(_SC_NPROCESSORS_ONLN);
+
 	err = target__validate(&opts.target);
 	if (err) {
 		target__strerror(&opts.target, err, errbuf, sizeof(errbuf));
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 3C154C432BE
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:20 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 2425C61205
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:20 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234273AbhHUJW5 (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:22:57 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34160 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S234680AbhHUJWd (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:33 -0400
Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DAC5C061156;
        Sat, 21 Aug 2021 02:20:35 -0700 (PDT)
Received: by mail-ej1-x632.google.com with SMTP id e21so9301183ejz.12;
        Sat, 21 Aug 2021 02:20:35 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=luODGGdTKMSVHQNhZwY0q6ykClJFkZ30Qtks/1RszYE=;
        b=rxguwcdIVN6PUc+2PVw9G6OIRfOCWxNfYnKXtvbbMX/eeIbd4pTQ4qofcRbBIv5DHY
         OYfZS9M0/CWVffCZoTtl0C3jwDS2QI43pmIEUTiD4Einub29D4MdUCGue8YE5NGUl5Gs
         6YMi5FqQhc4b8ZjpVBDrqTr0BtdTNZJ30WqotQcdgEV/xtRRDDhe/+ZVpUiJp2QpV2lz
         /dBjYvypxxQ8ECFJbBTNc7eNI9+60UzGkIVKZCGQClZRnetCgT6LEocF4BtLS9KeNYyv
         grsZksuoN2FRtK9HuAdfImNqFh78OeF3r3PAqBlAC3avq68QTabHhe+gTC7HT3mXFZgM
         pIfg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=luODGGdTKMSVHQNhZwY0q6ykClJFkZ30Qtks/1RszYE=;
        b=LPoNe4RZDvnnryRVy4CUGnMJo5wbmvxHwD9xIVc9GYgv7gRLAvvLfktptCX4eeGNdM
         UQaYffsmQFbHnLNfif2mjL4DDWsiyec8OcC0N7lqq83qaGHWyq+BB0eTMin4AnJYpTOe
         /snyi2cEPFcQHR6kNyu2yq3rT8H7np7sIT+ZtRDNjDE2fZr4DShasowW6/6Vr1RmTPNI
         oKne4Euupw09jg9e3O5cHzxRsql7o0rPl1p8gAdj0rK0PuTEhtCAfQIMDcoXehkOE2b9
         i1oz3qoCzJlmTIUVLZX6GiqzwqHAs+iaY6ZfKmD3Wd/qy1iaR8f6bRHMDK1MWjWbTr/u
         LPJA==
X-Gm-Message-State: AOAM5320rVgYPrcAtcgYf2qt6wiPsYm5ipPd0WnJ372YM1dm5XxFOt3i
        MGND+GmZMP4JjlcQ1nrtKKQ=
X-Google-Smtp-Source: ABdhPJzt48eQb0wDTCqRy8w5b0Kgi385W8LmXZDYo/2Jb9nbZOTl+g5B1eG+X9UW2bSfNrHZVd+pIA==
X-Received: by 2002:a17:906:fb91:: with SMTP id lr17mr25878506ejb.110.1629537633955;
        Sat, 21 Aug 2021 02:20:33 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.32
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:33 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 36/37] perf test/evlist-open-close: use inline func to convert timeval to usec
Date:   Sat, 21 Aug 2021 11:19:42 +0200
Message-Id: <b95035ec4a125355be8ea843f7275c4580da6398.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch introduces a new inline function to convert a timeval to
usec.
This function will be used also in the next patch.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/bench/evlist-open-close.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/perf/bench/evlist-open-close.c b/tools/perf/bench/evlist-open-close.c
index c18aa85725281795..00d0aef564f80d44 100644
--- a/tools/perf/bench/evlist-open-close.c
+++ b/tools/perf/bench/evlist-open-close.c
@@ -26,6 +26,11 @@ static int iterations = 100;
 static int nr_events = 1;
 static const char *event_string = "dummy";
 
+static inline u64 timeval2usec(struct timeval *tv)
+{
+	return tv->tv_sec * USEC_PER_SEC + tv->tv_usec;
+}
+
 static struct record_opts opts = {
 	.sample_time	     = true,
 	.mmap_pages	     = UINT_MAX,
@@ -197,7 +202,7 @@ static int bench_evlist_open_close__run(char *evstr)
 
 		gettimeofday(&end, NULL);
 		timersub(&end, &start, &diff);
-		runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
+		runtime_us = timeval2usec(&diff);
 		update_stats(&time_stats, runtime_us);
 
 		evlist__delete(evlist);
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham
	autolearn_force=no version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 78A17C432BE
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:47 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 6305E61186
	for <linux-perf-users@archiver.kernel.org>; Sat, 21 Aug 2021 09:22:47 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240955AbhHUJXZ (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 21 Aug 2021 05:23:25 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34088 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S233990AbhHUJWt (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 21 Aug 2021 05:22:49 -0400
Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C06BBC061760;
        Sat, 21 Aug 2021 02:20:36 -0700 (PDT)
Received: by mail-ej1-x635.google.com with SMTP id u3so25238447ejz.1;
        Sat, 21 Aug 2021 02:20:36 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=lzouR77OW9nooQQxrqAFj0oMxi+kbjqOBs0ytAr5v0w=;
        b=fVEDPE1qGrefc4fPgIGanm8A7JRLPKnb6uo0CT7JzDhhKiYLn20ovdpI9NovZZmKae
         FBoRoMXFXTMHqOEcALOD1lW7kXumebv9DMEJDEGr3H8CQ15z4DGNrx6gO8K7DT2MEpRi
         LtqcFwhSVP9Z5hiMWreLyM2O5F8Jph/rdEWX0x3gYfTa8Ax6V7W+HP2lMsOWTVl42YOH
         Xy2YAEYlAS0aGN7uHs+yMagCA+TAuk++WV2+fBC3MKoVAXbiQBVz8jp/7KTHJtXd6jzU
         z3+NjV+TqKILTgGpzU8aF2s7HlLdTSfo4AkOAmfkzFQN/eH0ETQELIieM3Juxa2rierB
         7kFA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=lzouR77OW9nooQQxrqAFj0oMxi+kbjqOBs0ytAr5v0w=;
        b=L43wp90yYnTWSlTARd62xZrO0sam7a3gOAB46uwqaTmCjeKHR/pQnBEF3LvvwYA1mn
         3sOTAeaR5AGpQLNBRLOIhDPTb7it/cWefWTHLmybwgZPylzW2wW4bvzCYcnYYPauOnD4
         i8dSwUqSgwprT8KsS9tn1a7wjCQEpcNcSJ0adpWDDEmg/RitWINuRO8Fc8KtyoYFu1mI
         5sRhQoLghx3btpWhRNIDB0Dk/F6IcO79p9e0JEvooipy2JHG3l1jARe16eapZHozqkcR
         uGic1dTl5IE8v5gBY8Go9UhHr3ZSNMx6BaXy/tF5o5u9ezjqCtsSEor0NL8aM+MGa3ej
         ELpg==
X-Gm-Message-State: AOAM5311KirUNuiKe9PnHxgn77zWmNpvs4eeiNYxIG/eMgcfi3PJzxKa
        4CGxNuWJijYFKA6t4icPJ2w=
X-Google-Smtp-Source: ABdhPJyAAzILA+hmxdZxcTLI9L83c0xsJcIEXV/1vhcSoDzY9Z2RexNuLcOWKzZLJAjQe6eJCqbI8g==
X-Received: by 2002:a17:906:3157:: with SMTP id e23mr26251426eje.29.1629537635274;
        Sat, 21 Aug 2021 02:20:35 -0700 (PDT)
Received: from honeypot.. (mob-176-243-254-204.net.vodafone.it. [176.243.254.204])
        by smtp.googlemail.com with ESMTPSA id a25sm4003878ejv.91.2021.08.21.02.20.34
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 21 Aug 2021 02:20:35 -0700 (PDT)
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <acme@kernel.org>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org,
        Riccardo Mancini <rickyman7@gmail.com>
Subject: [RFC PATCH v1 37/37] perf test/evlist-open-close: add detailed output mode
Date:   Sat, 21 Aug 2021 11:19:43 +0200
Message-Id: <fd3d0bdaf8dfa4eedd53fbdcf8088b8211a6f191.1629490974.git.rickyman7@gmail.com>
X-Mailer: git-send-email 2.31.1
In-Reply-To: <cover.1629490974.git.rickyman7@gmail.com>
References: <cover.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

This patch adds a detailed output mode in the perf-test
evlist-open-close. In this output mode, the time taken by each single
evlist function is computed.

Normal mode:
$ sudo ./perf bench internals evlist-open-close
  Number of workers:    1
  Number of cpus:       4
  Number of threads:    1
  Number of events:     1 (4 fds)
  Number of iterations: 100
  Average open-close took:     1199.300 usec (+-      289.699 usec)

Detailed mode:
$ sudo ./perf bench internals evlist-open-close -d
  Number of workers:    1
  Number of cpus:       4
  Number of threads:    1
  Number of events:     1 (4 fds)
  Number of iterations: 100
  Average open-close took:     1199.300 usec (+-      289.699 usec)
                init took:        0.000 usec (+-        0.000 usec)
                open took:       25.600 usec (+-        1.778 usec)
                mmap took:      532.000 usec (+-       58.133 usec)
              enable took:      337.300 usec (+-      194.160 usec)
             disable took:      181.700 usec (+-       85.307 usec)
              munmap took:       22.100 usec (+-        4.045 usec)
               close took:      100.300 usec (+-       21.329 usec)
                fini took:        0.200 usec (+-        0.133 usec)

* init and fini represent the time taken before and after the evlist
  operations (in this case the workqueue setup and teardown operations)

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/bench/evlist-open-close.c | 63 ++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 3 deletions(-)

diff --git a/tools/perf/bench/evlist-open-close.c b/tools/perf/bench/evlist-open-close.c
index 00d0aef564f80d44..8ba2799c66cafb3e 100644
--- a/tools/perf/bench/evlist-open-close.c
+++ b/tools/perf/bench/evlist-open-close.c
@@ -25,12 +25,18 @@
 static int iterations = 100;
 static int nr_events = 1;
 static const char *event_string = "dummy";
+static bool detail;
 
 static inline u64 timeval2usec(struct timeval *tv)
 {
 	return tv->tv_sec * USEC_PER_SEC + tv->tv_usec;
 }
 
+struct timers {
+	struct timeval start, end, diff;
+	struct stats init, open, mmap, enable, disable, munmap, close, fini;
+};
+
 static struct record_opts opts = {
 	.sample_time	     = true,
 	.mmap_pages	     = UINT_MAX,
@@ -60,6 +66,7 @@ static const struct option options[] = {
 	OPT_STRING('u', "uid", &opts.target.uid_str, "user", "user to profile"),
 	OPT_BOOLEAN(0, "per-thread", &opts.target.per_thread, "use per-thread mmaps"),
 	OPT_UINTEGER_OPTARG('j', "threads", &opts.nr_threads, UINT_MAX, "Number of threads to use"),
+	OPT_BOOLEAN('d', "detail", &detail, "compute time taken by single functions"),
 	OPT_END()
 };
 
@@ -113,11 +120,28 @@ static struct evlist *bench__create_evlist(char *evstr)
 	return NULL;
 }
 
-static int bench__do_evlist_open_close(struct evlist *evlist)
+#define START_TIMER(timers) do { \
+	if (detail) { \
+		gettimeofday(&(timers)->start, NULL); \
+	} \
+} while (0)
+
+#define RECORD_TIMER(timers, field) do { \
+	if (detail) { \
+		gettimeofday(&(timers)->end, NULL); \
+		timersub(&(timers)->end, &(timers)->start, &(timers)->diff); \
+		update_stats(&(timers)->field, timeval2usec(&(timers)->diff)); \
+		(timers)->start = (timers)->end; \
+	} \
+} while (0)
+
+static int bench__do_evlist_open_close(struct evlist *evlist, struct timers *timers)
 {
 	char sbuf[WORKQUEUE_STRERR_BUFSIZE];
 	int err = -1, ret;
 
+	START_TIMER(timers);
+
 	if (opts.nr_threads > 1) {
 		err = setup_global_workqueue(opts.nr_threads);
 		if (err) {
@@ -130,23 +154,30 @@ static int bench__do_evlist_open_close(struct evlist *evlist)
 
 		perf_set_multithreaded();
 	}
+	RECORD_TIMER(timers, init);
 
 	err = evlist__open(evlist);
 	if (err < 0) {
 		pr_err("evlist__open: %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
 		goto out;
 	}
+	RECORD_TIMER(timers, open);
 
 	err = evlist__mmap(evlist, opts.mmap_pages);
 	if (err < 0) {
 		pr_err("evlist__mmap: %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
 		goto out;
 	}
+	RECORD_TIMER(timers, mmap);
 
 	evlist__enable(evlist);
+	RECORD_TIMER(timers, enable);
 	evlist__disable(evlist);
+	RECORD_TIMER(timers, disable);
 	evlist__munmap(evlist);
+	RECORD_TIMER(timers, munmap);
 	evlist__close(evlist);
+	RECORD_TIMER(timers, close);
 
 out:
 	if (opts.nr_threads > 1) {
@@ -159,10 +190,15 @@ static int bench__do_evlist_open_close(struct evlist *evlist)
 
 		perf_set_singlethreaded();
 	}
+	RECORD_TIMER(timers, fini);
 
 	return err;
 }
 
+#define PRINT_TIMER(timers, field) \
+	printf("%20s took: %12.3f usec (+- %12.3f usec)\n", #field, \
+		avg_stats(&(timers)->field), stddev_stats(&(timers)->field))
+
 static int bench_evlist_open_close__run(char *evstr)
 {
 	// used to print statistics only
@@ -172,10 +208,21 @@ static int bench_evlist_open_close__run(char *evstr)
 	struct stats time_stats;
 	u64 runtime_us;
 	int i, err;
+	struct timers timers;
 
 	if (!evlist)
 		return -ENOMEM;
 
+	init_stats(&time_stats);
+	init_stats(&timers.init);
+	init_stats(&timers.open);
+	init_stats(&timers.mmap);
+	init_stats(&timers.enable);
+	init_stats(&timers.disable);
+	init_stats(&timers.munmap);
+	init_stats(&timers.close);
+	init_stats(&timers.fini);
+
 	init_stats(&time_stats);
 
 	printf("  Number of workers:\t%u\n", opts.nr_threads);
@@ -194,7 +241,7 @@ static int bench_evlist_open_close__run(char *evstr)
 			return -ENOMEM;
 
 		gettimeofday(&start, NULL);
-		err = bench__do_evlist_open_close(evlist);
+		err = bench__do_evlist_open_close(evlist, &timers);
 		if (err) {
 			evlist__delete(evlist);
 			return err;
@@ -211,7 +258,17 @@ static int bench_evlist_open_close__run(char *evstr)
 
 	time_average = avg_stats(&time_stats);
 	time_stddev = stddev_stats(&time_stats);
-	printf("  Average open-close took: %.3f usec (+- %.3f usec)\n", time_average, time_stddev);
+	printf("  Average open-close took: %12.3f usec (+- %12.3f usec)\n", time_average, time_stddev);
+	if (detail) {
+		PRINT_TIMER(&timers, init);
+		PRINT_TIMER(&timers, open);
+		PRINT_TIMER(&timers, mmap);
+		PRINT_TIMER(&timers, enable);
+		PRINT_TIMER(&timers, disable);
+		PRINT_TIMER(&timers, munmap);
+		PRINT_TIMER(&timers, close);
+		PRINT_TIMER(&timers, fini);
+	}
 
 	return 0;
 }
-- 
2.31.1


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 2D714C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 18:45:00 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 0314C61008
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 18:44:59 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S232968AbhHaSpy (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 14:45:54 -0400
Received: from mail.kernel.org ([198.145.29.99]:56106 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S229483AbhHaSpv (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 14:45:51 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 95D2D60FF2;
        Tue, 31 Aug 2021 18:44:55 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630435495;
        bh=IWw13YnK9rW1CKWgDL11yNR61oOp+iBIT8indqr3iGI=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=Bpk668iWJmNVqL5ZBx+tRbvzuKrwQ4OoASsSwqNTDThTxEYQ1+CKxW7pcCjiUyx3g
         +vDU+Z6pfmNrNV3yt9yOdCrW1BO5GtyU/5qYemUP+XE5d+lmzhMQFoOk6Eclv0FgXg
         kl+c6oadqi3XRfKGojpBYgP8bGLljNGwnmLWDCXSa7WswmBMlKqG9Yur8lGpS7vB6S
         b2IdR4rsOyUtLnw3cEQ8pic0dCF7lj1sjMji8RrqhzSGTwABRUPXQR0Mu7cS+X0ivT
         zxXqSmHKT39hddwGqHb631iWlC8/Upy9xCIvXjuUh7soJ04BFoi3JpzHiFjKPXMxBy
         qnovK6eHeKG4w==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id A4C7A4007E; Tue, 31 Aug 2021 15:44:52 -0300 (-03)
Date:   Tue, 31 Aug 2021 15:44:52 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 31/37] tools lib/subcmd: add OPT_UINTEGER_OPTARG
 option type
Message-ID: <YS54pArzp3mKKZzM@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <c46749b3dff796729078352ff164d363457a3587.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <c46749b3dff796729078352ff164d363457a3587.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:37AM +0200, Riccardo Mancini escreveu:
> This patch adds OPT_UINTEGER_OPTARG, which is the same as OPT_UINTEGER,
> but also makes it possible to use the option without any value, setting
> the variable to a default value, d.

Thanks, applied, just to erode a bit the patch kit.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/lib/subcmd/parse-options.h | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/tools/lib/subcmd/parse-options.h b/tools/lib/subcmd/parse-options.h
> index d2414144eb8c9927..41b9b942504d398e 100644
> --- a/tools/lib/subcmd/parse-options.h
> +++ b/tools/lib/subcmd/parse-options.h
> @@ -133,6 +133,7 @@ struct option {
>  #define OPT_SET_PTR(s, l, v, h, p)  { .type = OPTION_SET_PTR, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (p) }
>  #define OPT_INTEGER(s, l, v, h)     { .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, int *), .help = (h) }
>  #define OPT_UINTEGER(s, l, v, h)    { .type = OPTION_UINTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned int *), .help = (h) }
> +#define OPT_UINTEGER_OPTARG(s, l, v, d, h)    { .type = OPTION_UINTEGER, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned int *), .help = (h), .flags = PARSE_OPT_OPTARG, .defval = (intptr_t)(d) }
>  #define OPT_LONG(s, l, v, h)        { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) }
>  #define OPT_ULONG(s, l, v, h)        { .type = OPTION_ULONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, unsigned long *), .help = (h) }
>  #define OPT_U64(s, l, v, h)         { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) }
> -- 
> 2.31.1

-- 

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 7777DC432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 18:46:39 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 5E21B6103D
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 18:46:39 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S239327AbhHaSrc (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 14:47:32 -0400
Received: from mail.kernel.org ([198.145.29.99]:56616 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S239280AbhHaSrb (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 14:47:31 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 825176056B;
        Tue, 31 Aug 2021 18:46:35 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630435595;
        bh=kTXrNmPAOjCCJ87XuzHaMszrCV1Jfzu38Ezem81EJ/E=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=G6qXQvPARkrHm3Ca+RTu4DzO5nw5PbL2A+/CBzgbRuIPi8sW9f3Q/rXP3Ni6Wkbyp
         7++7YQ7jipWuNR2QgTxgUqVLlaQ9Ugv2u19BH+MV8f+7KI8FAR+YJAjp3aPGUklydD
         NM6ZP2wkxs53iin29VnoLs1fa68pX1RfJ0QXwDxy44wPB55NPi4B26CvHQEVYkECP9
         2wwt+4qkKBK465ZImQ4XcagvMzZGToTpyxLGY49xWfJQ4J1oOfdU5s7Ha+VygSmUEj
         0hcfuczwqtrPJmF5BNwCv934yh1CiBNPfGVM17b1+FoYNwQBfN98Xc9hTwre9wO2eD
         NaqIsWJxYIllA==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id EF4384007E; Tue, 31 Aug 2021 15:46:32 -0300 (-03)
Date:   Tue, 31 Aug 2021 15:46:32 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 01/37] libperf cpumap: improve idx function
Message-ID: <YS55COgKH5CRsYE0@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <f1543c15797169c21e8b205a4a6751159180580d.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <f1543c15797169c21e8b205a4a6751159180580d.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:07AM +0200, Riccardo Mancini escreveu:
> >From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered
> and without dups"), perf_cpu_map elements are sorted in ascending order.
> 
> This patch improves the perf_cpu_map__idx function by using a binary
> search.

Why not use bsearch()?  See 'man bsearch' and look at the example.

- Arnaldo
 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/lib/perf/cpumap.c | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
> index ca0215047c326af4..fb633272be3aaed9 100644
> --- a/tools/lib/perf/cpumap.c
> +++ b/tools/lib/perf/cpumap.c
> @@ -265,11 +265,18 @@ bool perf_cpu_map__empty(const struct perf_cpu_map *map)
>  
>  int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
>  {
> -	int i;
> +	int low = 0, high = cpus->nr, idx, cpu_at_idx;
>  
> -	for (i = 0; i < cpus->nr; ++i) {
> -		if (cpus->map[i] == cpu)
> -			return i;
> +	while (low < high) {
> +		idx = (low + high) / 2;
> +		cpu_at_idx = cpus->map[idx];
> +
> +		if (cpu_at_idx == cpu)
> +			return idx;
> +		else if (cpu_at_idx > cpu)
> +			high = idx;
> +		else
> +			low = idx+1;
>  	}
>  
>  	return -1;
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 1BFC0C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 18:47:15 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id F3FE96103D
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 18:47:14 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S239179AbhHaSsJ (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 14:48:09 -0400
Received: from mail.kernel.org ([198.145.29.99]:56824 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S239481AbhHaSsI (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 14:48:08 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 37BF161008;
        Tue, 31 Aug 2021 18:47:13 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630435633;
        bh=iT/iIdNBsTbGiFre6zz9wX0FMEwGiJRBo2nVuhGFLaE=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=mktDBhAO3/B5cmfypfEMomXqZX+lfO17FLVMw0r2aC6emyMFXQaxwJcErh4djdmsw
         upV+1TEuS+2cKvpyefH4tk8kN7iY06HKsUNayutcm/xrShONgV2qO3NXEHNFrqX0NO
         LAATfSaDO/cmqOQuXWTeJEXGOT9/rIKAX37a7Jw/tYhXGZB6SQJ0ZGL0QTaL+mIyZY
         AHt/Dy+Fnx+ppqA4V8+1c503hwM+Vj1DZQoUyZeNqj1E6FGYMeYIUhJ54RetrLYj78
         8NH7FK8sC8xbL854MXORHmN3AOw5mgH0syLTWfHShflq4eCbuxRCz8wowf9ZtNghN+
         9RDfWfJrSt+8g==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id BBE1C400FE; Tue, 31 Aug 2021 15:47:10 -0300 (-03)
Date:   Tue, 31 Aug 2021 15:47:10 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 02/37] libperf cpumap: improve max function
Message-ID: <YS55LhhuABId0ag7@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <fb79f02e7b86ea8044d563adb1e9890c906f982f.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <fb79f02e7b86ea8044d563adb1e9890c906f982f.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:08AM +0200, Riccardo Mancini escreveu:
> >From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered and
> without dups"), perf_cpu_map elements are sorted in ascending order.
> 
> This patch improves the perf_cpu_map__max function by returning the last
> element.
> 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/lib/perf/cpumap.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
> index fb633272be3aaed9..80e03b2f0c60cce7 100644
> --- a/tools/lib/perf/cpumap.c
> +++ b/tools/lib/perf/cpumap.c
> @@ -284,14 +284,10 @@ int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
>  
>  int perf_cpu_map__max(struct perf_cpu_map *map)
>  {
> -	int i, max = -1;
> -
> -	for (i = 0; i < map->nr; i++) {
> -		if (map->map[i] > max)
> -			max = map->map[i];
> -	}
> -
> -	return max;
> +	if (map->nr > 0)
> +		return map->map[map->nr-1];
> +	else
> +		return -1;

Applying, but adding spaces around the '-',

Thanks.

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id AA48BC432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:17:03 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 7D70A61074
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:17:03 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240509AbhHaTR5 (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:17:57 -0400
Received: from mail.kernel.org ([198.145.29.99]:38524 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S231938AbhHaTR5 (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:17:57 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 7682161057;
        Tue, 31 Aug 2021 19:17:01 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630437421;
        bh=bTQUHDephO2BwXAw6npExcZWHUGXMCAV4h/ZXp453S0=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=eoGIT/5ecE7FeDZWcHzNQ7gI5GmMz1XF7CYKFfnRfVVOrS2wCBNW2sUdKh9LoLy2V
         8nxfrCGlGpUZUYMwLFpKJyfmEiEoSxbfi25CFGA2n++CMyNT8va3Q37pmaDFIwbWIz
         +yfhL518zmwR7tyF1towo3fBL6sdXIVZYSJx+eb8E7LL4aR5zzQTCnJQvyQ4sOiRdQ
         w+VlSnS78/7NGBQteJpCbz7nzQGCFYFJ7BAr5FIIzAkZcZtIGPUjVxdpNGEyLKd4ia
         rPD9QI+6z+AhbiDrzU42I0TsS+mS5xrorRHZcsRjCQZbXjM54cUKd1nibOZqPww/uF
         clS2GhacneB+A==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id D664E4007E; Tue, 31 Aug 2021 16:16:57 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:16:57 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 02/37] libperf cpumap: improve max function
Message-ID: <YS6AKamBroQIgG7r@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <fb79f02e7b86ea8044d563adb1e9890c906f982f.1629490974.git.rickyman7@gmail.com>
 <YS55LhhuABId0ag7@kernel.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <YS55LhhuABId0ag7@kernel.org>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Tue, Aug 31, 2021 at 03:47:10PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Sat, Aug 21, 2021 at 11:19:08AM +0200, Riccardo Mancini escreveu:
> > >From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered and
> > without dups"), perf_cpu_map elements are sorted in ascending order.
> > 
> > This patch improves the perf_cpu_map__max function by returning the last
> > element.
> > 
> > Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> > ---
> >  tools/lib/perf/cpumap.c | 12 ++++--------
> >  1 file changed, 4 insertions(+), 8 deletions(-)
> > 
> > diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
> > index fb633272be3aaed9..80e03b2f0c60cce7 100644
> > --- a/tools/lib/perf/cpumap.c
> > +++ b/tools/lib/perf/cpumap.c
> > @@ -284,14 +284,10 @@ int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
> >  
> >  int perf_cpu_map__max(struct perf_cpu_map *map)
> >  {
> > -	int i, max = -1;
> > -
> > -	for (i = 0; i < map->nr; i++) {
> > -		if (map->map[i] > max)
> > -			max = map->map[i];
> > -	}
> > -
> > -	return max;
> > +	if (map->nr > 0)
> > +		return map->map[map->nr-1];
> > +	else
> > +		return -1;
> 
> Applying, but adding spaces around the '-',

I ended up with this, ok?

+++ b/tools/lib/perf/cpumap.c
@@ -282,10 +282,8 @@ int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)

 int perf_cpu_map__max(struct perf_cpu_map *map)
 {
-       if (map->nr > 0)
-               return map->map[map->nr-1];
-       else
-               return -1;
+       // cpu_map__trim_new() qsort()s it, cpu_map__default_new() sorts it as well.
+       return map->nr > 0 ? map->map[map->nr - 1] : -1;
 }


From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 3894EC432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:21:07 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 1DAC460234
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:21:07 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240564AbhHaTWB (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:22:01 -0400
Received: from mail.kernel.org ([198.145.29.99]:39960 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S231634AbhHaTV7 (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:21:59 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 8C885604AC;
        Tue, 31 Aug 2021 19:21:03 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630437663;
        bh=y8xsBEhjhKK04c0aoOkUN1HQirrD7nQfR2QpWd0Iw1E=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=DEtKVnssoyFoVRvb+4oy/vKxBHXxGxsQukxpGJJxJGCJJvCjDWeBYg3VVhivAtb/T
         L/vdns4/PYJv5Y/mTrUZ6FlU9VXyqgre2/XdkkpeLJQJibSepZvhDJ3kOHcpYfc+bc
         s6dlCc7BEsewjC8NaWi0XwZ6VCjnnB39j9/nIgxw+oEGh91Ovh3NsPb+zcsjJ5+3m0
         u6Vnw3eN/u8nWfs/sHGMft9lJqxoI/egEa4ev+Z7udubihP/y8CD3edHeaIwrqu9dp
         8GLV6WSR3gVqcSAJfzraYmXRixrpZb2HbmxVmOexoPPAo2b/WvdoJGLQWlD/7XbaZ6
         2by0hFRJWPpQw==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 0F23B4007E; Tue, 31 Aug 2021 16:21:01 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:21:01 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 04/37] perf util: add mmap_cpu_mask__duplicate
 function
Message-ID: <YS6BHW5lbvdPebTS@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <8943a548ef7a3dd3e015095afad7e9a8b2154c05.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <8943a548ef7a3dd3e015095afad7e9a8b2154c05.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:10AM +0200, Riccardo Mancini escreveu:
> This patch adds a new function in util/mmap.c to duplicate a mmap_cpu_mask.
> 
> This new function will be used in the following patches.
> 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/mmap.c | 12 ++++++++++++
>  tools/perf/util/mmap.h |  3 +++
>  2 files changed, 15 insertions(+)
> 
> diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c
> index ab7108d22428b527..9e9a447682cc962c 100644
> --- a/tools/perf/util/mmap.c
> +++ b/tools/perf/util/mmap.c
> @@ -350,3 +350,15 @@ int perf_mmap__push(struct mmap *md, void *to,
>  out:
>  	return rc;
>  }
> +
> +int mmap_cpu_mask__duplicate(struct mmap_cpu_mask *original,
> +				struct mmap_cpu_mask *clone)
> +{
> +	clone->nbits = original->nbits;
> +	clone->bits = bitmap_alloc(original->nbits);
> +	if (!clone->bits)
> +		return -ENOMEM;
> +
> +	memcpy(clone->bits, original->bits, MMAP_CPU_MASK_BYTES(original));
> +	return 0;
> +}
> diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h
> index 9d5f589f02ae70e1..b4923e587fd7749c 100644
> --- a/tools/perf/util/mmap.h
> +++ b/tools/perf/util/mmap.h
> @@ -63,4 +63,7 @@ size_t mmap__mmap_len(struct mmap *map);
>  
>  void mmap_cpu_mask__scnprintf(struct mmap_cpu_mask *mask, const char *tag);
>  
> +int mmap_cpu_mask__duplicate(struct mmap_cpu_mask *original,
> +				struct mmap_cpu_mask *clone);
> +
>  #endif /*__PERF_MMAP_H */

The jargon here is class__clone():

â¬¢[acme@toolbox perf]$ find tools/perf -name "*.[ch]" | xargs grep __clone
tools/perf/util/bpf-loader.c:bpf_map_op__clone(struct bpf_map_op *op)
tools/perf/util/bpf-loader.c:bpf_map_priv__clone(struct bpf_map_priv *priv)
tools/perf/util/bpf-loader.c:		newop = bpf_map_op__clone(pos);
tools/perf/util/bpf-loader.c:			priv = bpf_map_priv__clone(tmpl_priv);
tools/perf/util/cgroup.c:			evsel = evsel__clone(pos);
tools/perf/util/evsel.c: * evsel__clone - create a new evsel copied from @orig
tools/perf/util/evsel.c:struct evsel *evsel__clone(struct evsel *orig)
tools/perf/util/evsel.h:	 * Please check evsel__clone() to copy them properly so that
tools/perf/util/evsel.h:struct evsel *evsel__clone(struct evsel *orig);
tools/perf/util/map.h:struct map *map__clone(struct map *map);
tools/perf/util/maps.h:int maps__clone(struct thread *thread, struct maps *parent);
tools/perf/util/parse-events.c:int parse_events_term__clone(struct parse_events_term **new,
tools/perf/util/parse-events.c:		ret = parse_events_term__clone(&n, term);
tools/perf/util/parse-events.h:int parse_events_term__clone(struct parse_events_term **new,
tools/perf/util/symbol.c:				struct map *m = map__clone(new_map);
tools/perf/util/thread.c:static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
tools/perf/util/thread.c:	return do_maps_clone ? maps__clone(thread, parent->maps) : 0;
tools/perf/util/thread.c:	return thread__clone_maps(thread, parent, do_maps_clone);
tools/perf/util/map.c:struct map *map__clone(struct map *from)
tools/perf/util/map.c:			struct map *before = map__clone(pos);
tools/perf/util/map.c:			struct map *after = map__clone(pos);
tools/perf/util/map.c:int maps__clone(struct thread *thread, struct maps *parent)
tools/perf/util/map.c:		struct map *new = map__clone(map);
tools/perf/util/pmu.c:		ret = parse_events_term__clone(&cloned, term);
â¬¢[acme@toolbox perf]$ find tools/perf -name "*.[ch]" | xargs grep __duplicate
â¬¢[acme@toolbox perf]$

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 7337DC4320E
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:22:22 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 46D3A60F25
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:22:22 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240627AbhHaTXQ (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:23:16 -0400
Received: from mail.kernel.org ([198.145.29.99]:40446 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S231634AbhHaTXQ (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:23:16 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 449E160234;
        Tue, 31 Aug 2021 19:22:20 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630437740;
        bh=i1Y1hbT/RZr4nx6BdPpIbKxSIJvrWrIGnl09XjmbwrQ=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=BT9a29McinkaEPxJSYihIRCbncF/2ez4ztp1fuytYb1LpHiNyaEdYcYmcy3yRg1xf
         /30XDjHFoAh4Bx9kP90BULDT3FiPaTYhov+ExVCYV5tUbSMn8WhKRQqO+IB+jRJSF2
         u04d1g0/9hdavW6FocQWFJYhjnXW8R5aY9yJM4WYe8kTLv9nWx9Vrb+GN8ueu2emOR
         k9ZfZfONJWg6lK6VN8TiMlU6c7w9b7NjgBOlctI3GuzVRVyjrw3t5XLSnJCQ4TwyhU
         saSQ5aodm9AZ3J6SV3QHrjqERvAHFi8velP5wB60FoZetUp+hNrh0iAEyay1/LWpSb
         yKh6SBooUKhxQ==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 20C934007E; Tue, 31 Aug 2021 16:22:18 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:22:18 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 05/37] perf util/mmap: add missing bitops.h header
Message-ID: <YS6Baiyjlw7vpwp7@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <c5b91ee432a2e28e7f16337c740b43b4d0b0e86c.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <c5b91ee432a2e28e7f16337c740b43b4d0b0e86c.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:11AM +0200, Riccardo Mancini escreveu:
> MMAP_CPU_MASK_BYTES uses the BITS_TO_LONGS macro, which is defined in
> linux/bitops.h.
> However, this header is not included directly, but gets imported
> indirectly in files using the macro.
> This patch adds the missing include.

Thanks, applied.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/mmap.h | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h
> index b4923e587fd7749c..8e259b9610f83c96 100644
> --- a/tools/perf/util/mmap.h
> +++ b/tools/perf/util/mmap.h
> @@ -6,6 +6,7 @@
>  #include <linux/refcount.h>
>  #include <linux/types.h>
>  #include <linux/ring_buffer.h>
> +#include <linux/bitops.h>
>  #include <stdbool.h>
>  #include <pthread.h> // for cpu_set_t
>  #ifdef HAVE_AIO_SUPPORT
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id CC7D5C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:25:06 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id AAA3960234
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:25:06 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240748AbhHaT0A (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:26:00 -0400
Received: from mail.kernel.org ([198.145.29.99]:41216 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S231952AbhHaT0A (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:26:00 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 4D26F61041;
        Tue, 31 Aug 2021 19:25:04 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630437904;
        bh=LfTYo29aM8dEHYTzt+wGMSKyqaEeeay0p+2H1UYZ30c=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=Ib9VzZNveg+hsvzL/JD6x5/M7xGvAuvxiaLDW+1jn5cH2+jZnpQDMAZuSf9LfaNH/
         o4cQbgmANz+9SeYrQmA0/HUb9W/MUsJBN+AwTAme2tL9lWJoBzc22lq6/C501pR3MK
         r0pw3c2xAlfBLNCe//MLtNNTSe7FXVHHbpvWWWKpXMe89xdTFCECdumqfZUyfjavPZ
         I9Vl/Y9f7RpB1WRYG2MvuAn/8uKKMeFMb3PkxTrsgWFpZURNzQ80dJD7a4KBaNeYFd
         VWy5HobBZAqBuvFyAQg8F7yHKRkkFjCkMRXx3fVy68OTalDS3DU5sF8ni/lFls+S0M
         9ZOa6X5NDhowA==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id DE65B4007E; Tue, 31 Aug 2021 16:25:01 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:25:01 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 14/37] perf evsel: remove retry_sample_id goto
 label
Message-ID: <YS6CDRKiZ7CpVZYD@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <340af0d03408d6621fd9c742e311db18b3585b3b.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <340af0d03408d6621fd9c742e311db18b3585b3b.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:20AM +0200, Riccardo Mancini escreveu:
> As far as I can tell, there is no good reason, apart from optimization
> to have the retry_sample_id separate from fallback_missing_features.
> Probably, this label was added to avoid reapplying patches for missing
> features that had already been applied.
> However, missing features that have been added later have not used this
> optimization, always jumping to fallback_missing_features and reapplying
> all missing features.
> 
> This patch removes that label, replacing it with
> fallback_missing_features.

Fair enough, applied,

- Arnaldo
 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index f61e5dd53f5d2859..7b4bb3229a16524e 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1825,7 +1825,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		evsel->core.attr.bpf_event = 0;
>  	if (perf_missing_features.branch_hw_idx)
>  		evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX;
> -retry_sample_id:
>  	if (perf_missing_features.sample_id_all)
>  		evsel->core.attr.sample_id_all = 0;
>  
> @@ -2006,7 +2005,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	} else if (!perf_missing_features.sample_id_all) {
>  		perf_missing_features.sample_id_all = true;
>  		pr_debug2_peo("switching off sample_id_all\n");
> -		goto retry_sample_id;
> +		goto fallback_missing_features;
>  	} else if (!perf_missing_features.lbr_flags &&
>  			(evsel->core.attr.branch_sample_type &
>  			 (PERF_SAMPLE_BRANCH_NO_CYCLES |
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 48D46C43214
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:27:45 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 2BC5A61074
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:27:45 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240804AbhHaT2j (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:28:39 -0400
Received: from mail.kernel.org ([198.145.29.99]:42268 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S240813AbhHaT2h (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:28:37 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 05A5B61053;
        Tue, 31 Aug 2021 19:27:42 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630438062;
        bh=1hz+rLyf4P3BZ3OMt88NDe8EDolggy1OIHQDfSHHPc8=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=Drl5/ExfkJPnxheBzwCx81pEtdrxwdXzNuBv+1v2hTUn68ldoOmV22x/cSICSOb3y
         CvE7ijrYwvZEzK3I9ApsBqvcl054Ej0X0szhXsp879WYoQM8eMzpEa0XMzjHnmPDwq
         lNVkspNiciUAUcbCs8ZmAy+gQr6DUbq372RZHazVwcmgLJfDJ2QbEdFIUs+b3IHtVp
         hyF7v+J2jNvmtpXyg7p0EKlnMXgftfLe7HZ/QIzaFEsg4LObcEBQng7ExAuc3A4wo/
         0KgOA4kyJg2VS4FABGfJrgKJRAlfWQUKKmr0JIzzGe5omNEbU7XtyNbTrcd8vfzrT0
         C/rRWNbr1hFfQ==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id D73E74007E; Tue, 31 Aug 2021 16:27:39 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:27:39 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 15/37] perf evsel: separate open preparation from
 open itself
Message-ID: <YS6Cqw8oHJ72zIRK@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <e14118b934c338dbbf68b8677f20d0d7dbf9359a.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <e14118b934c338dbbf68b8677f20d0d7dbf9359a.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:21AM +0200, Riccardo Mancini escreveu:
> This is a preparatory patch for the following patches with the goal to
> separate in evlist__open_cpu the actual perf_event_open, which could be
> performed in parallel, from the existing fallback mechanisms, which
> should be handled sequentially.

Thanks, applied as the end result is equivalent and we erode this
patchkit a bit more.

- Arnaldo
 
> This patch separates the first lines of evsel__open_cpu into a new
> __evsel__prepare_open function.
> 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 45 +++++++++++++++++++++++++++++++----------
>  1 file changed, 34 insertions(+), 11 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 7b4bb3229a16524e..ddf324e2e17a0951 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1746,22 +1746,20 @@ static int perf_event_open(struct evsel *evsel,
>  	return fd;
>  }
>  
> -static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
> -		struct perf_thread_map *threads,
> -		int start_cpu, int end_cpu)
> +
> +static struct perf_cpu_map *empty_cpu_map;
> +static struct perf_thread_map *empty_thread_map;
> +
> +static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
> +		struct perf_thread_map *threads)
>  {
> -	int cpu, thread, nthreads;
> -	unsigned long flags = PERF_FLAG_FD_CLOEXEC;
> -	int pid = -1, err, old_errno;
> -	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
> +	int nthreads;
>  
>  	if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) ||
>  	    (perf_missing_features.aux_output     && evsel->core.attr.aux_output))
>  		return -EINVAL;
>  
>  	if (cpus == NULL) {
> -		static struct perf_cpu_map *empty_cpu_map;
> -
>  		if (empty_cpu_map == NULL) {
>  			empty_cpu_map = perf_cpu_map__dummy_new();
>  			if (empty_cpu_map == NULL)
> @@ -1772,8 +1770,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	}
>  
>  	if (threads == NULL) {
> -		static struct perf_thread_map *empty_thread_map;
> -
>  		if (empty_thread_map == NULL) {
>  			empty_thread_map = thread_map__new_by_tid(-1);
>  			if (empty_thread_map == NULL)
> @@ -1792,6 +1788,33 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	    perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0)
>  		return -ENOMEM;
>  
> +	return 0;
> +}
> +
> +static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
> +		struct perf_thread_map *threads,
> +		int start_cpu, int end_cpu)
> +{
> +	int cpu, thread, nthreads;
> +	unsigned long flags = PERF_FLAG_FD_CLOEXEC;
> +	int pid = -1, err, old_errno;
> +	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
> +
> +	err = __evsel__prepare_open(evsel, cpus, threads);
> +	if (err)
> +		return err;
> +
> +	if (cpus == NULL)
> +		cpus = empty_cpu_map;
> +
> +	if (threads == NULL)
> +		threads = empty_thread_map;
> +
> +	if (evsel->core.system_wide)
> +		nthreads = 1;
> +	else
> +		nthreads = threads->nr;
> +
>  	if (evsel->cgrp) {
>  		flags |= PERF_FLAG_PID_CGROUP;
>  		pid = evsel->cgrp->fd;
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 6DC42C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:31:09 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 4332D61057
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:31:09 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S230024AbhHaTcC (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:32:02 -0400
Received: from mail.kernel.org ([198.145.29.99]:43392 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S234139AbhHaTcB (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:32:01 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id D0F6260F91;
        Tue, 31 Aug 2021 19:31:05 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630438266;
        bh=NFwDSnpklMxKC8EPBXBrgV0olmRU2p/XdOiop04+OXY=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=ZmEuKQQCj4tfmsYyd5IsPjxYsRUf3ApRsmYdwQ6q3/qUi76aR0LmiwGmXuyVQPjdB
         CNGvHoIM5ZEqHo8f9lc31czbFDwjIY01Chxsghw6C9Q1LfFs/BUofJrztjHzJVun+j
         0Niqp6lNuaGceUIjD66H5UXzQ+OdNFltfUTCO+EmkcjcwyDFpeRYsN6lQTu1z3SfYw
         P3inIZDD0vBMtQSlToI4yXOvx228cHUkBtjLXLoa09050UQb3QIj9wOvAqkefAVeKu
         dokUAuH8DUqskf5u0K3XtWkXRxiKK0pRRVEiK4Kh1VFJqnTHwSwKjdLz1J4gRT0YPm
         tFM5+VinuW+BQ==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 720894007E; Tue, 31 Aug 2021 16:31:03 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:31:03 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 16/37] perf evsel: save open flags in evsel
Message-ID: <YS6Dd6lZoftrKGXl@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <d9f63159098e56fa518eecf25171d72e6f74df37.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <d9f63159098e56fa518eecf25171d72e6f74df37.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:22AM +0200, Riccardo Mancini escreveu:
> This patch caches the flags used in perf_event_open inside evsel, so
> that they can be set in __evsel__prepare_open (this will be useful
> in following patches, when the fallback mechanisms will be handled
> outside the open itself).
> This also optimizes the code, by not having to recompute them everytime.

Nice, thanks, applied, I'll make available what I have in tmp.perf/core
so that you can take a look before I push it all to Linus, this week.

- Arnaldo
 
> Since flags are now saved in evsel, the flags argument in
> perf_event_open is removed.
> 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 24 ++++++++++++------------
>  tools/perf/util/evsel.h |  1 +
>  2 files changed, 13 insertions(+), 12 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index ddf324e2e17a0951..509a2970a94b3142 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1710,17 +1710,16 @@ static void display_attr(struct perf_event_attr *attr)
>  }
>  
>  static int perf_event_open(struct evsel *evsel,
> -			   pid_t pid, int cpu, int group_fd,
> -			   unsigned long flags)
> +			   pid_t pid, int cpu, int group_fd)
>  {
>  	int precise_ip = evsel->core.attr.precise_ip;
>  	int fd;
>  
>  	while (1) {
>  		pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
> -			  pid, cpu, group_fd, flags);
> +			  pid, cpu, group_fd, evsel->open_flags);
>  
> -		fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, flags);
> +		fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, evsel->open_flags);
>  		if (fd >= 0)
>  			break;
>  
> @@ -1788,6 +1787,10 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	    perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0)
>  		return -ENOMEM;
>  
> +	evsel->open_flags = PERF_FLAG_FD_CLOEXEC;
> +	if (evsel->cgrp)
> +		evsel->open_flags |= PERF_FLAG_PID_CGROUP;
> +
>  	return 0;
>  }
>  
> @@ -1796,7 +1799,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		int start_cpu, int end_cpu)
>  {
>  	int cpu, thread, nthreads;
> -	unsigned long flags = PERF_FLAG_FD_CLOEXEC;
>  	int pid = -1, err, old_errno;
>  	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
>  
> @@ -1815,10 +1817,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	else
>  		nthreads = threads->nr;
>  
> -	if (evsel->cgrp) {
> -		flags |= PERF_FLAG_PID_CGROUP;
> +	if (evsel->cgrp)
>  		pid = evsel->cgrp->fd;
> -	}
>  
>  fallback_missing_features:
>  	if (perf_missing_features.weight_struct) {
> @@ -1832,7 +1832,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		evsel->core.attr.clockid = 0;
>  	}
>  	if (perf_missing_features.cloexec)
> -		flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
> +		evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
>  	if (perf_missing_features.mmap2)
>  		evsel->core.attr.mmap2 = 0;
>  	if (perf_missing_features.exclude_guest)
> @@ -1866,7 +1866,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  			test_attr__ready();
>  
>  			fd = perf_event_open(evsel, pid, cpus->map[cpu],
> -					     group_fd, flags);
> +					     group_fd);
>  
>  			FD(evsel, cpu, thread) = fd;
>  
> @@ -1874,7 +1874,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  
>  			if (unlikely(test_attr__enabled)) {
>  				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
> -						fd, group_fd, flags);
> +						fd, group_fd, evsel->open_flags);
>  			}
>  
>  			if (fd < 0) {
> @@ -2012,7 +2012,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		perf_missing_features.clockid = true;
>  		pr_debug2_peo("switching off use_clockid\n");
>  		goto fallback_missing_features;
> -	} else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
> +	} else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) {
>  		perf_missing_features.cloexec = true;
>  		pr_debug2_peo("switching off cloexec flag\n");
>  		goto fallback_missing_features;
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index eabccce406886320..1c0057e80d080f2f 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -149,6 +149,7 @@ struct evsel {
>  		struct bperf_leader_bpf *leader_skel;
>  		struct bperf_follower_bpf *follower_skel;
>  	};
> +	unsigned long		open_flags;
>  };
>  
>  struct perf_missing_features {
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 4B680C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:35:18 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 304BE60F91
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:35:18 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240857AbhHaTgK (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:36:10 -0400
Received: from mail.kernel.org ([198.145.29.99]:45912 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S240895AbhHaTgD (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:36:03 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id E479661059;
        Tue, 31 Aug 2021 19:35:07 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630438508;
        bh=lJl812jWZ9pp2L8z9pIZmOr/kh8QoAiHqxxqA2KL4To=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=neZ6ScwxRqrUqfakfaz1ENR+ujv8fVP+pl58AfTYWRRiXMVRDZwhDBB/HXYyw2E0b
         L74BHdggQHv6No/mtajI6bpihIe4mWs7AUgGZy7egETQEfEIShWcV/i0J2fAdU3KPx
         grw5oA6BWoYuTU4nxOq0FicgX7A7C37/tMKcrjvaj0/Lq6k5qXyp/3vbONv30whu90
         5e/4H8FoVmLDPDSrKodXDHftDRJkEQaFEsIVueUIHyXTEhS+SHuXS8SCXqYubmHldT
         M4sWhTzQZarzdnv0fn76DyAi8MSPZadZN7qG3grX0AxsmEtxkuhDfWTDemdqZMj2F9
         twhdvLAptB52g==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id C47304007E; Tue, 31 Aug 2021 16:35:04 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:35:04 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 17/37] perf evsel: separate missing feature
 disabling from evsel__open_cpu
Message-ID: <YS6EaLuwquCwiWqX@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <48138bd2932646dde315505da733c2ca635ad2ee.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <48138bd2932646dde315505da733c2ca635ad2ee.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:23AM +0200, Riccardo Mancini escreveu:
> This is a preparatory patch for the following patches with the goal to
> separate in evlist__open_cpu the actual opening, which could be
> performed in parallel, from the existing fallback mechanisms, which
> should be handled sequentially.
> 
> This patch separates the disabling of missing features from
> evlist__open_cpu into a new function evsel__disable_missing_features.


Thanks, applied as the end result is the same.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 57 ++++++++++++++++++++++-------------------
>  1 file changed, 31 insertions(+), 26 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 509a2970a94b3142..f0bc89f743915800 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1794,33 +1794,8 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	return 0;
>  }
>  
> -static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
> -		struct perf_thread_map *threads,
> -		int start_cpu, int end_cpu)
> +static void evsel__disable_missing_features(struct evsel *evsel)
>  {
> -	int cpu, thread, nthreads;
> -	int pid = -1, err, old_errno;
> -	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
> -
> -	err = __evsel__prepare_open(evsel, cpus, threads);
> -	if (err)
> -		return err;
> -
> -	if (cpus == NULL)
> -		cpus = empty_cpu_map;
> -
> -	if (threads == NULL)
> -		threads = empty_thread_map;
> -
> -	if (evsel->core.system_wide)
> -		nthreads = 1;
> -	else
> -		nthreads = threads->nr;
> -
> -	if (evsel->cgrp)
> -		pid = evsel->cgrp->fd;
> -
> -fallback_missing_features:
>  	if (perf_missing_features.weight_struct) {
>  		evsel__set_sample_bit(evsel, WEIGHT);
>  		evsel__reset_sample_bit(evsel, WEIGHT_STRUCT);
> @@ -1850,6 +1825,36 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX;
>  	if (perf_missing_features.sample_id_all)
>  		evsel->core.attr.sample_id_all = 0;
> +}
> +
> +static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
> +		struct perf_thread_map *threads,
> +		int start_cpu, int end_cpu)
> +{
> +	int cpu, thread, nthreads;
> +	int pid = -1, err, old_errno;
> +	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
> +
> +	err = __evsel__prepare_open(evsel, cpus, threads);
> +	if (err)
> +		return err;
> +
> +	if (cpus == NULL)
> +		cpus = empty_cpu_map;
> +
> +	if (threads == NULL)
> +		threads = empty_thread_map;
> +
> +	if (evsel->core.system_wide)
> +		nthreads = 1;
> +	else
> +		nthreads = threads->nr;
> +
> +	if (evsel->cgrp)
> +		pid = evsel->cgrp->fd;
> +
> +fallback_missing_features:
> +	evsel__disable_missing_features(evsel);
>  
>  	display_attr(&evsel->core.attr);
>  
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id BA189C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:36:42 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 924B261059
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:36:42 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240918AbhHaThg (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:37:36 -0400
Received: from mail.kernel.org ([198.145.29.99]:46740 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S240907AbhHaThf (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:37:35 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id D48E860FE8;
        Tue, 31 Aug 2021 19:36:39 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630438600;
        bh=Bdd2AUfHZRkDnkxyysbUypVEneghpA+reSd88K+srqk=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=JUV2gWTYfGktXIAjOfRBzWNrKiB4FZqdwoSeyCTguLsxhymvyj+cdiz77bHHKy7CS
         D1uMYw7CwNX9LUxk8E65djo1YhJzV86CYLClu9eYaMlA4yd//dhSMY36c52J7N/+cJ
         juxdIOS30IeIp/eTxC2OnmXIvsqXJFBzXWBoLoOJgCgvEtTXaeDFeuWqoEosMN+YSg
         bhvTHL3rVZSp4Q42/crpZU73Ar5/NJXr0Ylc+tibNj1S/S5TNoe6+0Vw2tnW9nD5FI
         XOIR4pthQJ77+bQYSX6CaC6QBHM/Bul8KjhdLtSwqFbJfAcmSFGW0qUEezDgVxMUzo
         gjOGprnWKAT8Q==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id A0E8F4007E; Tue, 31 Aug 2021 16:36:37 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:36:37 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 18/37] perf evsel: add evsel__prepare_open function
Message-ID: <YS6ExQdtIkejH46k@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <fa5e78bbb92c848226f044278fdcf777b3ce4583.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <fa5e78bbb92c848226f044278fdcf777b3ce4583.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:24AM +0200, Riccardo Mancini escreveu:
> This function will prepare the evsel and disable the missing features.
> It will be used in one of the following patches.

Applied, fixed up this:

int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
                        struct perf_thread_map *threads)

I.e. the alignment of the second line with parms, please use this form
in your next patches.

- Arnaldo
 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 14 ++++++++++++++
>  tools/perf/util/evsel.h |  2 ++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index f0bc89f743915800..4e9a3e62075305f1 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1827,6 +1827,20 @@ static void evsel__disable_missing_features(struct evsel *evsel)
>  		evsel->core.attr.sample_id_all = 0;
>  }
>  
> +int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
> +		struct perf_thread_map *threads)
> +{
> +	int err;
> +
> +	err = __evsel__prepare_open(evsel, cpus, threads);
> +	if (err)
> +		return err;
> +
> +	evsel__disable_missing_features(evsel);
> +
> +	return err;
> +}
> +
>  static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		struct perf_thread_map *threads,
>  		int start_cpu, int end_cpu)
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index 1c0057e80d080f2f..58aa998e1814ac9e 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -286,6 +286,8 @@ int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads)
>  int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		struct perf_thread_map *threads);
>  void evsel__close(struct evsel *evsel);
> +int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
> +		struct perf_thread_map *threads);
>  
>  struct perf_sample;
>  
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id E432EC432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:39:08 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id C8ED960FE8
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:39:08 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240909AbhHaTkD (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:40:03 -0400
Received: from mail.kernel.org ([198.145.29.99]:48948 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S240905AbhHaTkD (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:40:03 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 3ABAD60F9E;
        Tue, 31 Aug 2021 19:39:07 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630438747;
        bh=x273xtouPAcHm91AALzS9Mid9ylyvIGOadJdw0nv7zg=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=ciIY8fC97m41MryMUSIsYfTaPWh6rWkzlHFAmlPQ5NpPrkgYAaqftwuGHE3LkGZvV
         lJWjgjiSGGzfpoqFD6XerG2w21O301dBpb4Fefe/QWt7rti1CIkRpHNj65J+q8nOwd
         ZcqVVBxCLD0tZDMLyQ6hfSKPI9mPRssr+VNvABpIjJrT4nRMIyMoqKIPSZVET+E7hL
         xMpbMFkApJw3NPXoYW+Rdy647/h6vlZI6bvL3mkF0klY9pOhtjlGBkRmb2aLnlXRa0
         +AENoM906JBVtGdvShFDIB9ad4Pc/B60HktI3GZCsuwuDuNnD70L37kcqLiXrr5wDl
         txfIi4RAu7fAw==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 9FA794007E; Tue, 31 Aug 2021 16:39:04 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:39:04 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 19/37] perf evsel: separate missing feature
 detection from evsel__open_cpu
Message-ID: <YS6FWEf41ApoxBee@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <cba0b7d939862473662adeedb0f9c9b69566ee9a.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <cba0b7d939862473662adeedb0f9c9b69566ee9a.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:25AM +0200, Riccardo Mancini escreveu:
> This is a preparatory patch for the following patches with the goal to
> separate in evlist__open_cpu the actual opening, which could be
> performed in parallel, from the existing fallback mechanisms, which
> should be handled sequentially.
> 
> This patch separates the missing feature detection in evsel__open_cpu
> into a new evsel__detect_missing_features function.

Thanks, applied.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 174 +++++++++++++++++++++-------------------
>  tools/perf/util/evsel.h |   1 +
>  2 files changed, 92 insertions(+), 83 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 4e9a3e62075305f1..c393bd992322d925 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1841,6 +1841,96 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	return err;
>  }
>  
> +bool evsel__detect_missing_features(struct evsel *evsel)
> +{
> +	/*
> +	 * Must probe features in the order they were added to the
> +	 * perf_event_attr interface.
> +	 */
> +	if (!perf_missing_features.weight_struct &&
> +	    (evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) {
> +		perf_missing_features.weight_struct = true;
> +		pr_debug2("switching off weight struct support\n");
> +		return true;
> +	} else if (!perf_missing_features.code_page_size &&
> +	    (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)) {
> +		perf_missing_features.code_page_size = true;
> +		pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support, bailing out\n");
> +		return false;
> +	} else if (!perf_missing_features.data_page_size &&
> +	    (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)) {
> +		perf_missing_features.data_page_size = true;
> +		pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support, bailing out\n");
> +		return false;
> +	} else if (!perf_missing_features.cgroup && evsel->core.attr.cgroup) {
> +		perf_missing_features.cgroup = true;
> +		pr_debug2_peo("Kernel has no cgroup sampling support, bailing out\n");
> +		return false;
> +	} else if (!perf_missing_features.branch_hw_idx &&
> +	    (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX)) {
> +		perf_missing_features.branch_hw_idx = true;
> +		pr_debug2("switching off branch HW index support\n");
> +		return true;
> +	} else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
> +		perf_missing_features.aux_output = true;
> +		pr_debug2_peo("Kernel has no attr.aux_output support, bailing out\n");
> +		return false;
> +	} else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) {
> +		perf_missing_features.bpf = true;
> +		pr_debug2_peo("switching off bpf_event\n");
> +		return true;
> +	} else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
> +		perf_missing_features.ksymbol = true;
> +		pr_debug2_peo("switching off ksymbol\n");
> +		return true;
> +	} else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
> +		perf_missing_features.write_backward = true;
> +		pr_debug2_peo("switching off write_backward\n");
> +		return false;
> +	} else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
> +		perf_missing_features.clockid_wrong = true;
> +		pr_debug2_peo("switching off clockid\n");
> +		return true;
> +	} else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
> +		perf_missing_features.clockid = true;
> +		pr_debug2_peo("switching off use_clockid\n");
> +		return true;
> +	} else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) {
> +		perf_missing_features.cloexec = true;
> +		pr_debug2_peo("switching off cloexec flag\n");
> +		return true;
> +	} else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
> +		perf_missing_features.mmap2 = true;
> +		pr_debug2_peo("switching off mmap2\n");
> +		return true;
> +	} else if (!perf_missing_features.exclude_guest &&
> +		   (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
> +		perf_missing_features.exclude_guest = true;
> +		pr_debug2_peo("switching off exclude_guest, exclude_host\n");
> +		return true;
> +	} else if (!perf_missing_features.sample_id_all) {
> +		perf_missing_features.sample_id_all = true;
> +		pr_debug2_peo("switching off sample_id_all\n");
> +		return true;
> +	} else if (!perf_missing_features.lbr_flags &&
> +			(evsel->core.attr.branch_sample_type &
> +			 (PERF_SAMPLE_BRANCH_NO_CYCLES |
> +			  PERF_SAMPLE_BRANCH_NO_FLAGS))) {
> +		perf_missing_features.lbr_flags = true;
> +		pr_debug2_peo("switching off branch sample type no (cycles/flags)\n");
> +		return true;
> +	} else if (!perf_missing_features.group_read &&
> +		    evsel->core.attr.inherit &&
> +		   (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
> +		   evsel__is_group_leader(evsel)) {
> +		perf_missing_features.group_read = true;
> +		pr_debug2_peo("switching off group read\n");
> +		return true;
> +	} else {
> +		return false;
> +	}
> +}
> +
>  static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		struct perf_thread_map *threads,
>  		int start_cpu, int end_cpu)
> @@ -1979,90 +2069,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	if (err != -EINVAL || cpu > 0 || thread > 0)
>  		goto out_close;
>  
> -	/*
> -	 * Must probe features in the order they were added to the
> -	 * perf_event_attr interface.
> -	 */
> -	if (!perf_missing_features.weight_struct &&
> -	    (evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) {
> -		perf_missing_features.weight_struct = true;
> -		pr_debug2("switching off weight struct support\n");
> +	if (evsel__detect_missing_features(evsel))
>  		goto fallback_missing_features;
> -	} else if (!perf_missing_features.code_page_size &&
> -	    (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)) {
> -		perf_missing_features.code_page_size = true;
> -		pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support, bailing out\n");
> -		goto out_close;
> -	} else if (!perf_missing_features.data_page_size &&
> -	    (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)) {
> -		perf_missing_features.data_page_size = true;
> -		pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support, bailing out\n");
> -		goto out_close;
> -	} else if (!perf_missing_features.cgroup && evsel->core.attr.cgroup) {
> -		perf_missing_features.cgroup = true;
> -		pr_debug2_peo("Kernel has no cgroup sampling support, bailing out\n");
> -		goto out_close;
> -        } else if (!perf_missing_features.branch_hw_idx &&
> -	    (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX)) {
> -		perf_missing_features.branch_hw_idx = true;
> -		pr_debug2("switching off branch HW index support\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
> -		perf_missing_features.aux_output = true;
> -		pr_debug2_peo("Kernel has no attr.aux_output support, bailing out\n");
> -		goto out_close;
> -	} else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) {
> -		perf_missing_features.bpf = true;
> -		pr_debug2_peo("switching off bpf_event\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) {
> -		perf_missing_features.ksymbol = true;
> -		pr_debug2_peo("switching off ksymbol\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) {
> -		perf_missing_features.write_backward = true;
> -		pr_debug2_peo("switching off write_backward\n");
> -		goto out_close;
> -	} else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) {
> -		perf_missing_features.clockid_wrong = true;
> -		pr_debug2_peo("switching off clockid\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) {
> -		perf_missing_features.clockid = true;
> -		pr_debug2_peo("switching off use_clockid\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) {
> -		perf_missing_features.cloexec = true;
> -		pr_debug2_peo("switching off cloexec flag\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) {
> -		perf_missing_features.mmap2 = true;
> -		pr_debug2_peo("switching off mmap2\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.exclude_guest &&
> -		   (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
> -		perf_missing_features.exclude_guest = true;
> -		pr_debug2_peo("switching off exclude_guest, exclude_host\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.sample_id_all) {
> -		perf_missing_features.sample_id_all = true;
> -		pr_debug2_peo("switching off sample_id_all\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.lbr_flags &&
> -			(evsel->core.attr.branch_sample_type &
> -			 (PERF_SAMPLE_BRANCH_NO_CYCLES |
> -			  PERF_SAMPLE_BRANCH_NO_FLAGS))) {
> -		perf_missing_features.lbr_flags = true;
> -		pr_debug2_peo("switching off branch sample type no (cycles/flags)\n");
> -		goto fallback_missing_features;
> -	} else if (!perf_missing_features.group_read &&
> -		    evsel->core.attr.inherit &&
> -		   (evsel->core.attr.read_format & PERF_FORMAT_GROUP) &&
> -		   evsel__is_group_leader(evsel)) {
> -		perf_missing_features.group_read = true;
> -		pr_debug2_peo("switching off group read\n");
> -		goto fallback_missing_features;
> -	}
>  out_close:
>  	if (err)
>  		threads->err_thread = thread;
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index 58aa998e1814ac9e..a83fb7f69b1ead73 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -288,6 +288,7 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
>  void evsel__close(struct evsel *evsel);
>  int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		struct perf_thread_map *threads);
> +bool evsel__detect_missing_features(struct evsel *evsel);
>  
>  struct perf_sample;
>  
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 6D03EC432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:41:30 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 545F06102A
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:41:30 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240946AbhHaTmY (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:42:24 -0400
Received: from mail.kernel.org ([198.145.29.99]:51112 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S234715AbhHaTmV (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:42:21 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 09DD060F6C;
        Tue, 31 Aug 2021 19:41:26 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630438886;
        bh=Q/h/rcc7Azc7x48LlJctFK4Yk9KlVRAwRYtEyT2GKVU=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=acTIp6JqMSQqh/gLADlqOjJVw/uAm/cbra626tqWnkrhMK8idIn9lu3jmD3MwAooP
         gfngHiPFc+20P0ilC0lYES1zQMOtofGInvEdsozcC+KeKggrUHepI0vSoTO8qsqxQl
         FjDckiSmDp3VMBtS6VAC38e2eN/qq/F+sblYLKmve5FRNkr6ygdNpx8vqFpRKMswZ3
         wEt/qaT7YeJo6C0e2iCPR/9Es8Yx3FMa81nMSplYkAf8JYOobhuw8ibekPkSuwfXz+
         E+n6wdmJYWNVEweyp+XLEav6EYiqnfFApTps4OwLGaexCdowz7vm6vh7uy2DlZK7Uv
         DAwmpWJTDv6+A==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 939CF4007E; Tue, 31 Aug 2021 16:41:23 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:41:23 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 20/37] perf evsel: separate rlimit increase from
 evsel__open_cpu
Message-ID: <YS6F46/ONZzCWFnT@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <2f256de8ec37b9809a5cef73c2fa7bce416af5d3.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <2f256de8ec37b9809a5cef73c2fa7bce416af5d3.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:26AM +0200, Riccardo Mancini escreveu:
> This is a preparatory patch for the following patches with the goal to
> separate from evlist__open_cpu the actual opening (which could be
> performed in parallel), from the existing fallback mechanisms, which
> should be handled sequentially.
> 
> This patch separates the rlimit increase from evsel__open_cpu.
> 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 50 ++++++++++++++++++++++++-----------------
>  tools/perf/util/evsel.h |  3 +++
>  2 files changed, 33 insertions(+), 20 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index c393bd992322d925..916930ea31450265 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1931,13 +1931,40 @@ bool evsel__detect_missing_features(struct evsel *evsel)
>  	}
>  }
>  
> +bool evsel__increase_rlimit(enum rlimit_action *set_rlimit)
> +{
> +	int old_errno;
> +	struct rlimit l;
> +
> +	if (*set_rlimit < INCREASED_MAX) {
> +

The above blank line is not needed
> +		old_errno = errno;

And here it should be used, I'm recycling it for you :-)

> +		if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
> +			if (*set_rlimit == NO_CHANGE)
> +				l.rlim_cur = l.rlim_max;
> +			else {

Also when the else clause has {}, the if should have it too, I'm fixing
it.

I see that you just moved things around, but then this is a good time to
do these cosmetic changes.

Applying.

- Arnaldo

> +				l.rlim_cur = l.rlim_max + 1000;
> +				l.rlim_max = l.rlim_cur;
> +			}
> +			if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
> +				(*set_rlimit) += 1;
> +				errno = old_errno;
> +				return true;
> +			}
> +		}
> +		errno = old_errno;
> +	}
> +
> +	return false;
> +}
> +
>  static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		struct perf_thread_map *threads,
>  		int start_cpu, int end_cpu)
>  {
>  	int cpu, thread, nthreads;
>  	int pid = -1, err, old_errno;
> -	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
> +	enum rlimit_action set_rlimit = NO_CHANGE;
>  
>  	err = __evsel__prepare_open(evsel, cpus, threads);
>  	if (err)
> @@ -2046,25 +2073,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	 * perf stat needs between 5 and 22 fds per CPU. When we run out
>  	 * of them try to increase the limits.
>  	 */
> -	if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
> -		struct rlimit l;
> -
> -		old_errno = errno;
> -		if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
> -			if (set_rlimit == NO_CHANGE)
> -				l.rlim_cur = l.rlim_max;
> -			else {
> -				l.rlim_cur = l.rlim_max + 1000;
> -				l.rlim_max = l.rlim_cur;
> -			}
> -			if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
> -				set_rlimit++;
> -				errno = old_errno;
> -				goto retry_open;
> -			}
> -		}
> -		errno = old_errno;
> -	}
> +	if (err == -EMFILE && evsel__increase_rlimit(&set_rlimit))
> +		goto retry_open;
>  
>  	if (err != -EINVAL || cpu > 0 || thread > 0)
>  		goto out_close;
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index a83fb7f69b1ead73..bf9abd9a5cbf9852 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -290,6 +290,9 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		struct perf_thread_map *threads);
>  bool evsel__detect_missing_features(struct evsel *evsel);
>  
> +enum rlimit_action { NO_CHANGE, SET_TO_MAX, INCREASED_MAX };
> +bool evsel__increase_rlimit(enum rlimit_action *set_rlimit);
> +
>  struct perf_sample;
>  
>  void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name);
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 99DCAC432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:44:56 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 7EB496102A
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:44:56 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S234715AbhHaTpv (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:45:51 -0400
Received: from mail.kernel.org ([198.145.29.99]:54042 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S232016AbhHaTpu (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:45:50 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id CAE2160F6C;
        Tue, 31 Aug 2021 19:44:54 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630439095;
        bh=lTUh2DVu0SDf71xXMtCahB5YXku0JQOpZqCnumJpauU=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=PSpUpSaeyStxnRc3bwUi/cP1kzByp2fzzq47ippka8ErdVWlgjFDVnGjGw3XKJp1U
         QU2044pyd9KDZUizfIKBMLrzVX7yAJiN2MfFDh1ltSyZWlINsQHaSuFnqfwpeDzNnk
         6i7c7fesST/LydueI+UT6px2LLUgVRqrAqboPu1z+JRwvo/S0YisbFEAV2MBiVyJlz
         C6n8xY69p+k1YmrDGwnsUwod4Gp/D37TzvnHaDqtKfrLCYsJ7xuSKysb3zuq3wH0ib
         kjH9XfK80p3Laq4CAPK8PNYAxy/7SnQQ1WtC5hDzd1CzsbwthBYsHjlTyPWyjEawhg
         DBAvR+1pNsu+g==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 858CF4007E; Tue, 31 Aug 2021 16:44:52 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:44:52 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 21/37] perf evsel: move ignore_missing_thread to
 fallback code
Message-ID: <YS6GtNjIcyA4r8O8@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <4eca51443c786baaf6811b7cd8e73aafd97f7606.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <4eca51443c786baaf6811b7cd8e73aafd97f7606.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:27AM +0200, Riccardo Mancini escreveu:
> This patch moves ignore_missing_thread outside the perf_event_open loop.
> Doing so, we need to move the retry_open flag a few places higher, with
> minimal impact. Furthermore, thread need not be decreased since it won't
> get increased by the for loop (since we're jumping back inside), but we
> need to check that the nthreads decrease didn't put thread out of range.
> 
> The goal is to have fallbacks handled in one place only, since in the
> future parallel code, these would be handled separately.

Thanks, looks ok, applied.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 29 +++++++++++++----------------
>  tools/perf/util/evsel.h |  5 +++++
>  2 files changed, 18 insertions(+), 16 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 916930ea31450265..a1a88607fd59efcb 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1656,7 +1656,7 @@ static int update_fds(struct evsel *evsel,
>  	return 0;
>  }
>  
> -static bool ignore_missing_thread(struct evsel *evsel,
> +bool evsel__ignore_missing_thread(struct evsel *evsel,
>  				  int nr_cpus, int cpu,
>  				  struct perf_thread_map *threads,
>  				  int thread, int err)
> @@ -1993,12 +1993,15 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  
>  		for (thread = 0; thread < nthreads; thread++) {
>  			int fd, group_fd;
> +retry_open:
> +			if (thread >= nthreads)
> +				break;
>  
>  			if (!evsel->cgrp && !evsel->core.system_wide)
>  				pid = perf_thread_map__pid(threads, thread);
>  
>  			group_fd = get_group_fd(evsel, cpu, thread);
> -retry_open:
> +
>  			test_attr__ready();
>  
>  			fd = perf_event_open(evsel, pid, cpus->map[cpu],
> @@ -2016,20 +2019,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  			if (fd < 0) {
>  				err = -errno;
>  
> -				if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
> -					/*
> -					 * We just removed 1 thread, so take a step
> -					 * back on thread index and lower the upper
> -					 * nthreads limit.
> -					 */
> -					nthreads--;
> -					thread--;
> -
> -					/* ... and pretend like nothing have happened. */
> -					err = 0;
> -					continue;
> -				}
> -
>  				pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
>  					  err);
>  				goto try_fallback;
> @@ -2069,6 +2058,14 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	return 0;
>  
>  try_fallback:
> +	if (evsel__ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
> +		/* We just removed 1 thread, so lower the upper nthreads limit. */
> +		nthreads--;
> +
> +		/* ... and pretend like nothing have happened. */
> +		err = 0;
> +		goto retry_open;
> +	}
>  	/*
>  	 * perf stat needs between 5 and 22 fds per CPU. When we run out
>  	 * of them try to increase the limits.
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index bf9abd9a5cbf9852..661d2677a19b6248 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -293,6 +293,11 @@ bool evsel__detect_missing_features(struct evsel *evsel);
>  enum rlimit_action { NO_CHANGE, SET_TO_MAX, INCREASED_MAX };
>  bool evsel__increase_rlimit(enum rlimit_action *set_rlimit);
>  
> +bool evsel__ignore_missing_thread(struct evsel *evsel,
> +				  int nr_cpus, int cpu,
> +				  struct perf_thread_map *threads,
> +				  int thread, int err);
> +
>  struct perf_sample;
>  
>  void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name);
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 45B64C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:47:46 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 22B5C60FE8
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:47:46 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S240986AbhHaTsk (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:48:40 -0400
Received: from mail.kernel.org ([198.145.29.99]:56096 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S232016AbhHaTsj (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:48:39 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id ADC9D60FC3;
        Tue, 31 Aug 2021 19:47:43 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630439263;
        bh=6qWa4iDpk/xVe1Ft/BT0+AL0cYx8U0yvB8CfDL4WQfE=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=lyZ5Ln5LBqUbTkqlMgcgGVtZWzWO+JDaM6fVY/djKM2f7F+jDsm/7HRWLsOuhi/JI
         1T9udOjykm/dL/+ZSsPysnLu/bAbr+LJQjsHeJjfZzH1DKgnilKImOKgXOAClzRKNM
         xBGzMqet3qI0XD20LBWNneMR6PAeyueT+mJcGpY1bXwNDKyY94I2eTO5UjhjOoNRU1
         0++gaNdfRrAQCWwRocf2okajwLfynB58qc1MOjYuR/xeEHmkz6RM+BZQqgxkyUen2v
         swRUq6mJHx2XlXoH03a4NoXKrsyknOo1SfTVh6K/5t4mdo0cVUZfiCcMruON/RJXvA
         NDBSHIdqdYANA==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 329C24007E; Tue, 31 Aug 2021 16:47:41 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:47:41 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 22/37] perf evsel: move test_attr__open to success
 path in evsel__open_cpu
Message-ID: <YS6HXQ1DOA4uZVht@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <b3baf11360ca96541c9631730614fd7d217496fc.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <b3baf11360ca96541c9631730614fd7d217496fc.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:28AM +0200, Riccardo Mancini escreveu:
> test_attr__open ignores the fd if -1, therefore it is safe to move it to
> the success path (fd >= 0).

Nicely spotted, applied.

- Arnaldo
 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index a1a88607fd59efcb..d2b0391569286080 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -2011,11 +2011,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  
>  			bpf_counter__install_pe(evsel, cpu, fd);
>  
> -			if (unlikely(test_attr__enabled)) {
> -				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
> -						fd, group_fd, evsel->open_flags);
> -			}
> -
>  			if (fd < 0) {
>  				err = -errno;
>  
> @@ -2024,6 +2019,11 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  				goto try_fallback;
>  			}
>  
> +			if (unlikely(test_attr__enabled)) {
> +				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
> +						fd, group_fd, evsel->open_flags);
> +			}
> +
>  			pr_debug2_peo(" = %d\n", fd);
>  
>  			if (evsel->bpf_fd >= 0) {
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 0917AC432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:50:29 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id D7B6F61059
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:50:28 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S230217AbhHaTvW (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:51:22 -0400
Received: from mail.kernel.org ([198.145.29.99]:57746 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S230008AbhHaTvV (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:51:21 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 7075160FE6;
        Tue, 31 Aug 2021 19:50:25 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630439425;
        bh=ekHRx31xM7F0lGz1VDIoOTtbt1ej1vJeqD8rSC0v11U=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=XViDsM1Wyg0Ma+Qekrqx4vg4No3lgCD3GyUoAgVAeQy+FFTRUZVYnwrkfw63ua8nc
         apcjV4sJDHSWGkFhCfukmsjyLJwN5eOBMzrhHnkusyMKF9SDL332oTUk8qhtmE6Gii
         VRXx9TyipzApKPODqD+ir57qsMO4XEE7B12VI3wxSItc1tlsVhkxyeJf+Zz77e2DIx
         tu7ILb+mjvv3I/Roynx4Dg7ytD7HhZAkM1AJeZuW7yzdS9UcLR3SSBmiFQvID+1ynt
         YChg98MAiRff6OQv/+JfzIqmvAZXlKOAUZk6lXVb9zwfbjYDMYaXQ8liKStBdLv3ZV
         N7EWauc3U0G7g==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 2D9754007E; Tue, 31 Aug 2021 16:50:22 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:50:22 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>,
        Song Liu <songliubraving@fb.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 23/37] perf evsel: move bpf_counter__install_pe to
 success path in evsel__open_cpu
Message-ID: <YS6H/hiBHVMxWeQR@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <64f8a1b0a838a6e6049cd43c1beafd432999ae57.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <64f8a1b0a838a6e6049cd43c1beafd432999ae57.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:29AM +0200, Riccardo Mancini escreveu:
> I don't see why bpf_counter__install_pe should get called even if fd=-1,
> so I'm moving it to the success path.
> 
> This will be useful in following patches to separate the actual open and
> the related operations from the fallback mechanisms.

Looks sane, applied.

Next time please use git blame to find the author and add him to the CC
list, like I just did, so that the mistery can be unveiled or a duh!
uttered :-)

- Arnaldo
 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index d2b0391569286080..3e556afed8dd396c 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -2009,8 +2009,6 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  
>  			FD(evsel, cpu, thread) = fd;
>  
> -			bpf_counter__install_pe(evsel, cpu, fd);
> -
>  			if (fd < 0) {
>  				err = -errno;
>  
> @@ -2019,6 +2017,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  				goto try_fallback;
>  			}
>  
> +			bpf_counter__install_pe(evsel, cpu, fd);
> +
>  			if (unlikely(test_attr__enabled)) {
>  				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
>  						fd, group_fd, evsel->open_flags);
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 3FE62C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:52:59 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 224246102A
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:52:59 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S241049AbhHaTxu (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:53:50 -0400
Received: from mail.kernel.org ([198.145.29.99]:59462 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S241009AbhHaTxr (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:53:47 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 36CFD6108B;
        Tue, 31 Aug 2021 19:52:51 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630439571;
        bh=xNMN2u+WxIaMkCU2FbZtFtSdUVxdfHTvJJz6sJp62V0=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=Gvz1NPpyTnZ3uclqKuiMGhsk9g3YiKbrHAaH89MLILtmJMA9+T78PY33gyOWC0Wii
         b3/Ju0NFxf1LHBbtThBSwmnpcsFoXsGoHvtpBdUpkRxtXnzD7dolMAGckDy9mvPtmt
         yqqI123QuNFWRnpuV58AAhq/uBgwgLajmUIQ6biEzLLGjd0M9+RqMYUSH9g/2SWdzB
         WlNAvxahwweui4Lt1JpFA7ALfMQoYXwjpJ0LSly0f2WEW6c7JGLH4i0l8//fksHLd8
         OW6aBddmlVPVXNlQBic+eHqcrLagxcOEFl+mroJnVGC1fxNOhB6eY8YAB/qSzWBGYa
         57jB3BB3AHn0w==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 083AB4007E; Tue, 31 Aug 2021 16:52:49 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:52:48 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 24/37] perf evsel: handle precise_ip fallback in
 evsel__open_cpu
Message-ID: <YS6IkHuMDIGuOzO7@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <74208c433d2024a6c4af9c0b140b54ed6b5ea810.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <74208c433d2024a6c4af9c0b140b54ed6b5ea810.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:30AM +0200, Riccardo Mancini escreveu:
> This is another patch in the effort to separate the fallback mechanisms
> from the open itself.
> 
> In case of precise_ip fallback, the original precise_ip will be stored
> in the evsel (it was stored in a local variable) and the open will be
> retried. Since the precise_ip fallback will be the first in the chain of
> fallbacks, there should be no functional change with this patch.

Thanks, looks ok, applied.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 59 ++++++++++++++++++-----------------------
>  tools/perf/util/evsel.h |  2 ++
>  2 files changed, 28 insertions(+), 33 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 3e556afed8dd396c..2e95416b8320c6b9 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1709,42 +1709,29 @@ static void display_attr(struct perf_event_attr *attr)
>  	}
>  }
>  
> -static int perf_event_open(struct evsel *evsel,
> -			   pid_t pid, int cpu, int group_fd)
> +bool evsel__precise_ip_fallback(struct evsel *evsel)
>  {
> -	int precise_ip = evsel->core.attr.precise_ip;
> -	int fd;
> -
> -	while (1) {
> -		pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
> -			  pid, cpu, group_fd, evsel->open_flags);
> -
> -		fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, evsel->open_flags);
> -		if (fd >= 0)
> -			break;
> -
> -		/* Do not try less precise if not requested. */
> -		if (!evsel->precise_max)
> -			break;
> -
> -		/*
> -		 * We tried all the precise_ip values, and it's
> -		 * still failing, so leave it to standard fallback.
> -		 */
> -		if (!evsel->core.attr.precise_ip) {
> -			evsel->core.attr.precise_ip = precise_ip;
> -			break;
> -		}
> +	/* Do not try less precise if not requested. */
> +	if (!evsel->precise_max)
> +		return false;
>  
> -		pr_debug2_peo("\nsys_perf_event_open failed, error %d\n", -ENOTSUP);
> -		evsel->core.attr.precise_ip--;
> -		pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
> -		display_attr(&evsel->core.attr);
> +	/*
> +	 * We tried all the precise_ip values, and it's
> +	 * still failing, so leave it to standard fallback.
> +	 */
> +	if (!evsel->core.attr.precise_ip) {
> +		evsel->core.attr.precise_ip = evsel->precise_ip_original;
> +		return false;
>  	}
>  
> -	return fd;
> -}
> +	if (!evsel->precise_ip_original)
> +		evsel->precise_ip_original = evsel->core.attr.precise_ip;
>  
> +	evsel->core.attr.precise_ip--;
> +	pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip);
> +	display_attr(&evsel->core.attr);
> +	return true;
> +}
>  
>  static struct perf_cpu_map *empty_cpu_map;
>  static struct perf_thread_map *empty_thread_map;
> @@ -2004,8 +1991,11 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  
>  			test_attr__ready();
>  
> -			fd = perf_event_open(evsel, pid, cpus->map[cpu],
> -					     group_fd);
> +			pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
> +				pid, cpus->map[cpu], group_fd, evsel->open_flags);
> +
> +			fd = sys_perf_event_open(&evsel->core.attr, pid, cpus->map[cpu],
> +						group_fd, evsel->open_flags);
>  
>  			FD(evsel, cpu, thread) = fd;
>  
> @@ -2058,6 +2048,9 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	return 0;
>  
>  try_fallback:
> +	if (evsel__precise_ip_fallback(evsel))
> +		goto retry_open;
> +
>  	if (evsel__ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) {
>  		/* We just removed 1 thread, so lower the upper nthreads limit. */
>  		nthreads--;
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index 661d2677a19b6248..0a245afab2d87d74 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -150,6 +150,7 @@ struct evsel {
>  		struct bperf_follower_bpf *follower_skel;
>  	};
>  	unsigned long		open_flags;
> +	int			precise_ip_original;
>  };
>  
>  struct perf_missing_features {
> @@ -297,6 +298,7 @@ bool evsel__ignore_missing_thread(struct evsel *evsel,
>  				  int nr_cpus, int cpu,
>  				  struct perf_thread_map *threads,
>  				  int thread, int err);
> +bool evsel__precise_ip_fallback(struct evsel *evsel);
>  
>  struct perf_sample;
>  
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 3A44DC432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:54:48 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 154D461074
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 19:54:48 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S230224AbhHaTzm (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 15:55:42 -0400
Received: from mail.kernel.org ([198.145.29.99]:60778 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S229946AbhHaTzm (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 15:55:42 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 532D361053;
        Tue, 31 Aug 2021 19:54:46 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630439686;
        bh=aHloCrBXDAhdjVe5ypi8FjpqRvK46acnBPd5AxJF6jM=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=E3qHKfO7Z8IeYKyPGbJcLeS8l1mWQDSssd1OcC6/hXCS2hmu6PmJKz1qElx5b0lZL
         Izn9m3rhr8rK0Ezbxj2NZWOs+sVa2AavF7YbjlXGsJj66zYZtYlPS/OBiWi1y0kqJ/
         6RmT/YomRvQr/2lNYMi5XWRfiZ25B7h1Yf19/EO9Yj3R8TKCzxpv2z4IqSLa33sjVM
         ivS4LOo/YKmXuNIGoedWHj+3KFW3r/xJW499i+Mv1+kuPhpdDXvlsI0pzE7fFpauPf
         Ex/u4QP9M1bvQv5/gMknkkvQTaWKE3dksEWS2DMn2gq0K7v8gYNdtMc2ABRdbKqjM+
         vA8LfULOjyfEQ==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 444514007E; Tue, 31 Aug 2021 16:54:44 -0300 (-03)
Date:   Tue, 31 Aug 2021 16:54:44 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 25/37] perf evsel: move event open in
 evsel__open_cpu to separate function
Message-ID: <YS6JBBW3d4pmcy/U@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <74ac2eea14f45b2cbecffb509dd5f3cd523d4a9b.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <74ac2eea14f45b2cbecffb509dd5f3cd523d4a9b.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:31AM +0200, Riccardo Mancini escreveu:
> This is the final patch splitting evsel__open_cpu.
> This patch moves the entire loop code to a separate function, to be
> reused for the multithreaded code.

Are you going to use that 'enum perf_event_open_err' somewhere else?
I.e. is there a need to expose it in evsel.h?

I'm stopping at this patch to give the ones I merged so far some
testing, will now push it to tmp.perf/core.

- Arnaldo
 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evsel.c | 142 ++++++++++++++++++++++++----------------
>  tools/perf/util/evsel.h |  12 ++++
>  2 files changed, 99 insertions(+), 55 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 2e95416b8320c6b9..e41f55a7a70ea630 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1945,6 +1945,82 @@ bool evsel__increase_rlimit(enum rlimit_action *set_rlimit)
>  	return false;
>  }
>  
> +static struct perf_event_open_result perf_event_open(struct evsel *evsel,
> +					pid_t pid, int cpu, int thread,
> +					struct perf_cpu_map *cpus,
> +					struct perf_thread_map *threads)
> +{
> +	int fd, group_fd, rc;
> +	struct perf_event_open_result res;
> +
> +	if (!evsel->cgrp && !evsel->core.system_wide)
> +		pid = perf_thread_map__pid(threads, thread);
> +
> +	group_fd = get_group_fd(evsel, cpu, thread);
> +
> +	test_attr__ready();
> +
> +	pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
> +			pid, cpus->map[cpu], group_fd, evsel->open_flags);
> +
> +	fd = sys_perf_event_open(&evsel->core.attr, pid, cpus->map[cpu],
> +				group_fd, evsel->open_flags);
> +
> +	FD(evsel, cpu, thread) = fd;
> +	res.fd = fd;
> +
> +	if (fd < 0) {
> +		rc = -errno;
> +
> +		pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
> +				rc);
> +		res.rc = rc;
> +		res.err = PEO_FALLBACK;
> +		return res;
> +	}
> +
> +	bpf_counter__install_pe(evsel, cpu, fd);
> +
> +	if (unlikely(test_attr__enabled)) {
> +		test_attr__open(&evsel->core.attr, pid,
> +			cpus->map[cpu], fd,
> +			group_fd, evsel->open_flags);
> +	}
> +
> +	pr_debug2_peo(" = %d\n", fd);
> +
> +	if (evsel->bpf_fd >= 0) {
> +		int evt_fd = fd;
> +		int bpf_fd = evsel->bpf_fd;
> +
> +		rc = ioctl(evt_fd,
> +				PERF_EVENT_IOC_SET_BPF,
> +				bpf_fd);
> +		if (rc && errno != EEXIST) {
> +			pr_err("failed to attach bpf fd %d: %s\n",
> +				bpf_fd, strerror(errno));
> +			res.rc = -EINVAL;
> +			res.err = PEO_ERROR;
> +			return res;
> +		}
> +	}
> +
> +	/*
> +	 * If we succeeded but had to kill clockid, fail and
> +	 * have evsel__open_strerror() print us a nice error.
> +	 */
> +	if (perf_missing_features.clockid ||
> +		perf_missing_features.clockid_wrong) {
> +		res.rc = -EINVAL;
> +		res.err = PEO_ERROR;
> +		return res;
> +	}
> +
> +	res.rc = 0;
> +	res.err = PEO_SUCCESS;
> +	return res;
> +}
> +
>  static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  		struct perf_thread_map *threads,
>  		int start_cpu, int end_cpu)
> @@ -1952,6 +2028,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	int cpu, thread, nthreads;
>  	int pid = -1, err, old_errno;
>  	enum rlimit_action set_rlimit = NO_CHANGE;
> +	struct perf_event_open_result peo_res;
>  
>  	err = __evsel__prepare_open(evsel, cpus, threads);
>  	if (err)
> @@ -1979,67 +2056,22 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
>  	for (cpu = start_cpu; cpu < end_cpu; cpu++) {
>  
>  		for (thread = 0; thread < nthreads; thread++) {
> -			int fd, group_fd;
>  retry_open:
>  			if (thread >= nthreads)
>  				break;
>  
> -			if (!evsel->cgrp && !evsel->core.system_wide)
> -				pid = perf_thread_map__pid(threads, thread);
> -
> -			group_fd = get_group_fd(evsel, cpu, thread);
> -
> -			test_attr__ready();
> -
> -			pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx",
> -				pid, cpus->map[cpu], group_fd, evsel->open_flags);
> +			peo_res = perf_event_open(evsel, pid, cpu, thread, cpus,
> +						threads);
>  
> -			fd = sys_perf_event_open(&evsel->core.attr, pid, cpus->map[cpu],
> -						group_fd, evsel->open_flags);
> -
> -			FD(evsel, cpu, thread) = fd;
> -
> -			if (fd < 0) {
> -				err = -errno;
> -
> -				pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
> -					  err);
> +			err = peo_res.rc;
> +			switch (peo_res.err) {
> +			case PEO_SUCCESS:
> +				set_rlimit = NO_CHANGE;
> +				continue;
> +			case PEO_FALLBACK:
>  				goto try_fallback;
> -			}
> -
> -			bpf_counter__install_pe(evsel, cpu, fd);
> -
> -			if (unlikely(test_attr__enabled)) {
> -				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
> -						fd, group_fd, evsel->open_flags);
> -			}
> -
> -			pr_debug2_peo(" = %d\n", fd);
> -
> -			if (evsel->bpf_fd >= 0) {
> -				int evt_fd = fd;
> -				int bpf_fd = evsel->bpf_fd;
> -
> -				err = ioctl(evt_fd,
> -					    PERF_EVENT_IOC_SET_BPF,
> -					    bpf_fd);
> -				if (err && errno != EEXIST) {
> -					pr_err("failed to attach bpf fd %d: %s\n",
> -					       bpf_fd, strerror(errno));
> -					err = -EINVAL;
> -					goto out_close;
> -				}
> -			}
> -
> -			set_rlimit = NO_CHANGE;
> -
> -			/*
> -			 * If we succeeded but had to kill clockid, fail and
> -			 * have evsel__open_strerror() print us a nice error.
> -			 */
> -			if (perf_missing_features.clockid ||
> -			    perf_missing_features.clockid_wrong) {
> -				err = -EINVAL;
> +			default:
> +			case PEO_ERROR:
>  				goto out_close;
>  			}
>  		}
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index 0a245afab2d87d74..8c9827a93ac001a7 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -282,6 +282,18 @@ int evsel__enable(struct evsel *evsel);
>  int evsel__disable(struct evsel *evsel);
>  int evsel__disable_cpu(struct evsel *evsel, int cpu);
>  
> +enum perf_event_open_err {
> +	PEO_SUCCESS,
> +	PEO_FALLBACK,
> +	PEO_ERROR
> +};
> +
> +struct perf_event_open_result {
> +	enum perf_event_open_err err;
> +	int rc;
> +	int fd;
> +};
> +
>  int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu);
>  int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads);
>  int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 97490C432BE
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 20:06:14 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 747F161090
	for <linux-perf-users@archiver.kernel.org>; Tue, 31 Aug 2021 20:06:14 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S232034AbhHaUHJ (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Tue, 31 Aug 2021 16:07:09 -0400
Received: from mail.kernel.org ([198.145.29.99]:40008 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S229476AbhHaUHI (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Tue, 31 Aug 2021 16:07:08 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 256316108B;
        Tue, 31 Aug 2021 20:06:13 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1630440373;
        bh=xrvKiHXhYI03XkknWcnBRe0wu5iG2lTHButTzM1ZtwE=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=FQcjRDlduBYIPHCwtbtquYUWQAwyz1mnXHv1m813KLjFpOv3EAYfIEB01ZMzWLPAA
         vnga06UIcMK7xldwTnWdGSVXwFDXBmy21Nmo2CJsAKCegkmOIFh22CwWwLea4JL5X0
         1RhgjEWQRcOtRU7mL2Jv8gK0+Q1zbzu0o7GAMpw93RY2z2gaTzOsQDHLcnmd6wi2uP
         ltm4EgHsDc8WUY7QsmjuLeF7dJYUIAgESL4lRJ2D0VFGO7J+D5q/snaWuIYUwVb+vJ
         ia/TV/6Spg/6/flpaZIqCxG5sMrQVkqX83uzQ9mU3L0DeNQlug9rNz08j0jHSTwWDy
         WQYf2r2h1Hovg==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 03F884007E; Tue, 31 Aug 2021 17:06:09 -0300 (-03)
Date:   Tue, 31 Aug 2021 17:06:09 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 27/37] perf evlist: add
 evlist__for_each_entry_from macro
Message-ID: <YS6LsW1bYZpRyhY4@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <2386505f8b598adf0dbcd04ec21804c6bcf00826.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <2386505f8b598adf0dbcd04ec21804c6bcf00826.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:33AM +0200, Riccardo Mancini escreveu:
> This patch adds a new iteration macro for evlist that resumes iteration
> from a given evsel in the evlist.
> 
> This macro will be used in the next patch

Thanks, applied.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/evlist.h | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
> index 5f24a45d4e3cf30a..b0c2da0f9755b2d1 100644
> --- a/tools/perf/util/evlist.h
> +++ b/tools/perf/util/evlist.h
> @@ -275,6 +275,22 @@ void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
>  #define evlist__for_each_entry_continue(evlist, evsel) \
>  	__evlist__for_each_entry_continue(&(evlist)->core.entries, evsel)
>  
> +/**
> + * __evlist__for_each_entry_from - continue iteration from @evsel (included)
> + * @list: list_head instance to iterate
> + * @evsel: struct evsel iterator
> + */
> +#define __evlist__for_each_entry_from(list, evsel) \
> +	list_for_each_entry_from(evsel, list, core.node)
> +
> +/**
> + * evlist__for_each_entry_from - continue iteration from @evsel (included)
> + * @evlist: evlist instance to iterate
> + * @evsel: struct evsel iterator
> + */
> +#define evlist__for_each_entry_from(evlist, evsel) \
> +	__evlist__for_each_entry_from(&(evlist)->core.entries, evsel)
> +
>  /**
>   * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
>   * @list: list_head instance to iterate
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,
	HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 1D830C433FE
	for <linux-perf-users@archiver.kernel.org>; Fri,  3 Sep 2021 21:52:24 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 003FE60FD7
	for <linux-perf-users@archiver.kernel.org>; Fri,  3 Sep 2021 21:52:23 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S236098AbhICVxX (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Fri, 3 Sep 2021 17:53:23 -0400
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56256 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S230425AbhICVxW (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Fri, 3 Sep 2021 17:53:22 -0400
Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06DA0C061575;
        Fri,  3 Sep 2021 14:52:22 -0700 (PDT)
Received: by mail-wm1-x32c.google.com with SMTP id 192-20020a1c04c9000000b002f7a4ab0a49so373273wme.0;
        Fri, 03 Sep 2021 14:52:21 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20210112;
        h=message-id:subject:from:to:cc:date:in-reply-to:references
         :user-agent:mime-version:content-transfer-encoding;
        bh=4X569cMojqGCvJ6b5l8jpJFI25ifYXNTwbLVHX2EofM=;
        b=VWyhegPkhwjy9vsmRQA7vARMix2jDpDWJuWPtP6pzjR79F+TWr2inKlvFLd3GAmIVt
         vWVBcuRd4E+AaoJGcet+fwbY6r20LDvNu2i7wrBGAZrAjYmFF7QyNdAys6a7yF6PVyb+
         kYhMSj/s+SpXOcQJnpv0oUis/ioqyug+TDT5FHFt87MZj2sxzFWz+z6cNhSiV8MvYSRm
         +9KnBT/CpJZ1Ou3XT30s1GF1Ymj/rfZveaafkeAuir0uTkRKNu3I1nO2CzMRccggrJo5
         jN37tTmUaqwqp8mitEq9ylMtVyKvLmuD7Dw3PLtVzmlBDjdojqUOSCKhqzAxPeOocCpc
         U1vw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to
         :references:user-agent:mime-version:content-transfer-encoding;
        bh=4X569cMojqGCvJ6b5l8jpJFI25ifYXNTwbLVHX2EofM=;
        b=jeEnXqXC+R+XYPBn2eDqB8OOgyQpYKk2KjNQpGedhSiHOHypaeq7FyCZ7lF/+hhj2m
         EJu1WQNhOZ//Sx2IvEC3qwcLnWFkYAjk67HSkpLuPToKVkt6emEMmW6nNCQl1OYBzR+y
         ifDLXi7YqLB7YOn0ZegTaSlOO/HYIEUOoXNRjLyhmHCWRlIGDx6SB3Xy94EKaChUcV6F
         d/osfUv9NM8v5apfV71cYzRtGBJT5nDLmE3oytSxWULSS6av61fK1ieyC4Ezqf6jWbUQ
         TlosZaPYp95wBqxi4NkB0xzaj00sQKw/TbhdnbUkn3h91hMCM/RTePynojnLuJx1r6Gq
         XmxQ==
X-Gm-Message-State: AOAM533KO5n3jgksmR/E+KLENaE9Mmb0DlK7gA4Md6/62HFkHFsR5gpH
        AiwDXCqO5IGpU2x2qk6DXqDToN3P0fnJGFMC
X-Google-Smtp-Source: ABdhPJxWE1IXgzk2tIXdRbzsYwp2Bfj8DJqP5O8MwLtSDromiw2lGScrG2XMCPWNsFVbS+GyO6Zkng==
X-Received: by 2002:a1c:202:: with SMTP id 2mr669946wmc.122.1630705940287;
        Fri, 03 Sep 2021 14:52:20 -0700 (PDT)
Received: from [192.168.1.15] ([151.29.107.216])
        by smtp.gmail.com with ESMTPSA id c24sm409253wrb.57.2021.09.03.14.52.19
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Fri, 03 Sep 2021 14:52:19 -0700 (PDT)
Message-ID: <9506b14fe2965e4145c034715eb10e02f2137f7b.camel@gmail.com>
Subject: Re: [RFC PATCH v1 25/37] perf evsel: move event open in
 evsel__open_cpu to separate function
From:   Riccardo Mancini <rickyman7@gmail.com>
To:     Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Date:   Fri, 03 Sep 2021 23:52:18 +0200
In-Reply-To: <YS6JBBW3d4pmcy/U@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
         <74ac2eea14f45b2cbecffb509dd5f3cd523d4a9b.1629490974.git.rickyman7@gmail.com>
         <YS6JBBW3d4pmcy/U@kernel.org>
Content-Type: text/plain; charset="UTF-8"
User-Agent: Evolution 3.40.4 (3.40.4-1.fc34) 
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Hi Arnaldo,

thanks for yourÂ review and your suggestions, and also for the PRIu64 patch.

On Tue, 2021-08-31 at 16:54 -0300, Arnaldo Carvalho de Melo wrote:
> Em Sat, Aug 21, 2021 at 11:19:31AM +0200, Riccardo Mancini escreveu:
> > This is the final patch splitting evsel__open_cpu.
> > This patch moves the entire loop code to a separate function, to be
> > reused for the multithreaded code.
> 
> Are you going to use that 'enum perf_event_open_err' somewhere else?
> I.e. is there a need to expose it in evsel.h?

Yes, in the next patch (26/37). It's being used to expose a function that just
does the perf_event_open calls for an evsel. It needs to return such structure
to provide information about the error (which return code, at which thread).

> 
> I'm stopping at this patch to give the ones I merged so far some
> testing, will now push it to tmp.perf/core.

I checked tmp.perf/core and it looks good to me.
I also did some additional tests to check that fallback mechanisms where
working:

check missing pid being ignored (rerun until warning is shown)
$ sudo ./perf bench internals evlist-open-close -i10 -u $UID

check that weak group fallback is working
$ sudo ./perf record -e '{cycles,cache-misses,cache-
references,cpu_clk_unhalted.thread,cycles,cycles,cycles}:W' 

check that precision_ip fallback is working:
edited perf-sys.h to make sys_perf_event_open fail if precision_ip > 2
$ sudo ./perf record -e '{cycles,cs}:P'


I've also run perf-test on my machine and it's passing too.
I'm encounteirng oneÂ fail on the "BPF filter" test (42), which is present also
in perf/core, so it should not be related to this patch.

Thanks,
Riccardo

> 
> - Arnaldo
> Â 
> > Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> > ---
> > Â tools/perf/util/evsel.c | 142 ++++++++++++++++++++++++----------------
> > Â tools/perf/util/evsel.h |Â  12 ++++
> > Â 2 files changed, 99 insertions(+), 55 deletions(-)
> > 
> > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > index 2e95416b8320c6b9..e41f55a7a70ea630 100644
> > --- a/tools/perf/util/evsel.c
> > +++ b/tools/perf/util/evsel.c
> > @@ -1945,6 +1945,82 @@ bool evsel__increase_rlimit(enum rlimit_action
> > *set_rlimit)
> > Â Â Â Â Â Â Â Â return false;
> > Â }
> > Â 
> > +static struct perf_event_open_result perf_event_open(struct evsel *evsel,
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pid_t pid, int cpu, int thread,
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct perf_cpu_map *cpus,
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct perf_thread_map *threads)
> > +{
> > +Â Â Â Â Â Â Â int fd, group_fd, rc;
> > +Â Â Â Â Â Â Â struct perf_event_open_result res;
> > +
> > +Â Â Â Â Â Â Â if (!evsel->cgrp && !evsel->core.system_wide)
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pid = perf_thread_map__pid(threads, thread);
> > +
> > +Â Â Â Â Â Â Â group_fd = get_group_fd(evsel, cpu, thread);
> > +
> > +Â Â Â Â Â Â Â test_attr__ready();
> > +
> > +Â Â Â Â Â Â Â pr_debug2_peo("sys_perf_event_open: pid %dÂ  cpu %dÂ  group_fd %dÂ  flags
> > %#lx",
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pid, cpus->map[cpu], group_fd, evsel->open_flags);
> > +
> > +Â Â Â Â Â Â Â fd = sys_perf_event_open(&evsel->core.attr, pid, cpus->map[cpu],
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â group_fd, evsel->open_flags);
> > +
> > +Â Â Â Â Â Â Â FD(evsel, cpu, thread) = fd;
> > +Â Â Â Â Â Â Â res.fd = fd;
> > +
> > +Â Â Â Â Â Â Â if (fd < 0) {
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â rc = -errno;
> > +
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â rc);
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â res.rc = rc;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â res.err = PEO_FALLBACK;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return res;
> > +Â Â Â Â Â Â Â }
> > +
> > +Â Â Â Â Â Â Â bpf_counter__install_pe(evsel, cpu, fd);
> > +
> > +Â Â Â Â Â Â Â if (unlikely(test_attr__enabled)) {
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â test_attr__open(&evsel->core.attr, pid,
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â cpus->map[cpu], fd,
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â group_fd, evsel->open_flags);
> > +Â Â Â Â Â Â Â }
> > +
> > +Â Â Â Â Â Â Â pr_debug2_peo(" = %d\n", fd);
> > +
> > +Â Â Â Â Â Â Â if (evsel->bpf_fd >= 0) {
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int evt_fd = fd;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int bpf_fd = evsel->bpf_fd;
> > +
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â rc = ioctl(evt_fd,
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â PERF_EVENT_IOC_SET_BPF,
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â bpf_fd);
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (rc && errno != EEXIST) {
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pr_err("failed to attach bpf fd %d: %s\n",
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â bpf_fd, strerror(errno));
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â res.rc = -EINVAL;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â res.err = PEO_ERROR;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return res;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> > +Â Â Â Â Â Â Â }
> > +
> > +Â Â Â Â Â Â Â /*
> > +Â Â Â Â Â Â Â  * If we succeeded but had to kill clockid, fail and
> > +Â Â Â Â Â Â Â  * have evsel__open_strerror() print us a nice error.
> > +Â Â Â Â Â Â Â  */
> > +Â Â Â Â Â Â Â if (perf_missing_features.clockid ||
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â perf_missing_features.clockid_wrong) {
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â res.rc = -EINVAL;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â res.err = PEO_ERROR;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return res;
> > +Â Â Â Â Â Â Â }
> > +
> > +Â Â Â Â Â Â Â res.rc = 0;
> > +Â Â Â Â Â Â Â res.err = PEO_SUCCESS;
> > +Â Â Â Â Â Â Â return res;
> > +}
> > +
> > Â static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct perf_thread_map *threads,
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int start_cpu, int end_cpu)
> > @@ -1952,6 +2028,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct
> > perf_cpu_map *cpus,
> > Â Â Â Â Â Â Â Â int cpu, thread, nthreads;
> > Â Â Â Â Â Â Â Â int pid = -1, err, old_errno;
> > Â Â Â Â Â Â Â Â enum rlimit_action set_rlimit = NO_CHANGE;
> > +Â Â Â Â Â Â Â struct perf_event_open_result peo_res;
> > Â 
> > Â Â Â Â Â Â Â Â err = __evsel__prepare_open(evsel, cpus, threads);
> > Â Â Â Â Â Â Â Â if (err)
> > @@ -1979,67 +2056,22 @@ static int evsel__open_cpu(struct evsel *evsel, struct
> > perf_cpu_map *cpus,
> > Â Â Â Â Â Â Â Â for (cpu = start_cpu; cpu < end_cpu; cpu++) {
> > Â 
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â for (thread = 0; thread < nthreads; thread++) {
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int fd, group_fd;
> > Â retry_open:
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (thread >= nthreads)
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;
> > Â 
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (!evsel->cgrp && !evsel->core.system_wide)
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pid = perf_thread_map__pid(threads, thread);
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â group_fd = get_group_fd(evsel, cpu, thread);
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â test_attr__ready();
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pr_debug2_peo("sys_perf_event_open: pid %dÂ  cpu %dÂ 
> > group_fd %dÂ  flags %#lx",
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pid, cpus->map[cpu], group_fd, evsel-
> > >open_flags);
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â peo_res = perf_event_open(evsel, pid, cpu, thread,
> > cpus,
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â threads);
> > Â 
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â fd = sys_perf_event_open(&evsel->core.attr, pid, cpus-
> > >map[cpu],
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â group_fd, evsel->open_flags);
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â FD(evsel, cpu, thread) = fd;
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (fd < 0) {
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â err = -errno;
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pr_debug2_peo("\nsys_perf_event_open failed,
> > error %d\n",
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  err);
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â err = peo_res.rc;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â switch (peo_res.err) {
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â case PEO_SUCCESS:
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â set_rlimit = NO_CHANGE;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â continue;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â case PEO_FALLBACK:
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto try_fallback;
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â bpf_counter__install_pe(evsel, cpu, fd);
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (unlikely(test_attr__enabled)) {
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â test_attr__open(&evsel->core.attr, pid, cpus-
> > >map[cpu],
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â fd, group_fd, evsel-
> > >open_flags);
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pr_debug2_peo(" = %d\n", fd);
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (evsel->bpf_fd >= 0) {
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int evt_fd = fd;
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int bpf_fd = evsel->bpf_fd;
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â err = ioctl(evt_fd,
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  PERF_EVENT_IOC_SET_BPF,
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  bpf_fd);
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (err && errno != EEXIST) {
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pr_err("failed to attach bpf fd %d:
> > %s\n",
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  bpf_fd, strerror(errno));
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â err = -EINVAL;
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out_close;
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â set_rlimit = NO_CHANGE;
> > -
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /*
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  * If we succeeded but had to kill clockid, fail and
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  * have evsel__open_strerror() print us a nice error.
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  */
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (perf_missing_features.clockid ||
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  perf_missing_features.clockid_wrong) {
> > -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â err = -EINVAL;
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â default:
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â case PEO_ERROR:
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out_close;
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> > diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> > index 0a245afab2d87d74..8c9827a93ac001a7 100644
> > --- a/tools/perf/util/evsel.h
> > +++ b/tools/perf/util/evsel.h
> > @@ -282,6 +282,18 @@ int evsel__enable(struct evsel *evsel);
> > Â int evsel__disable(struct evsel *evsel);
> > Â int evsel__disable_cpu(struct evsel *evsel, int cpu);
> > Â 
> > +enum perf_event_open_err {
> > +Â Â Â Â Â Â Â PEO_SUCCESS,
> > +Â Â Â Â Â Â Â PEO_FALLBACK,
> > +Â Â Â Â Â Â Â PEO_ERROR
> > +};
> > +
> > +struct perf_event_open_result {
> > +Â Â Â Â Â Â Â enum perf_event_open_err err;
> > +Â Â Â Â Â Â Â int rc;
> > +Â Â Â Â Â Â Â int fd;
> > +};
> > +
> > Â int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int
> > cpu);
> > Â int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map
> > *threads);
> > Â int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
> > -- 
> > 2.31.1
> 



From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
X-Spam-Level: 
X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH,
	MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id C6B5AC433EF
	for <linux-perf-users@archiver.kernel.org>; Sat, 11 Sep 2021 19:10:24 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 92A206108B
	for <linux-perf-users@archiver.kernel.org>; Sat, 11 Sep 2021 19:10:24 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233563AbhIKTLg (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Sat, 11 Sep 2021 15:11:36 -0400
Received: from mail.kernel.org ([198.145.29.99]:53568 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S233412AbhIKTLf (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Sat, 11 Sep 2021 15:11:35 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 88DA6610A3;
        Sat, 11 Sep 2021 19:10:22 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1631387422;
        bh=3aQ1dPvmq5e4fEjLwL0tUxMhP5eHt4VNnDk2o9/eMow=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=q5GNEu3AFDSs3c2PvEuxQmOEJK28pu5uaien+PGu4O76tbWBa5JnTLc8BxPdisz5C
         c9804CAOsYA+aAfda5tXkIZhsMlnqk+ZFqyxJ1gydvcDSpPUg0MhZOwfS+wNrWIofK
         ga7KyrB4Zwvf2InzffeFGUi5izdWuVFtDDFMpZ1oE/mEQZ3ubzIXR6nwxmaoEERLBH
         tTAFDuDSg1iIc2aR2DgJnRy/7e+aoI3gkb5UlwcovkcFiw2wV7F50tJwb+zjq/bALL
         i8m4sV6F4L8x/gIBnNJh6ih7zhrQFNACWnJ2C8zFP+illWwzT+N5FAeF9hv3qCnDjJ
         tuRFNrPeofvwg==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 096904038F; Sat, 11 Sep 2021 16:10:20 -0300 (-03)
Date:   Sat, 11 Sep 2021 16:10:20 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>,
        Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 25/37] perf evsel: move event open in
 evsel__open_cpu to separate function
Message-ID: <YTz/HBuosvqOkvYE@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <74ac2eea14f45b2cbecffb509dd5f3cd523d4a9b.1629490974.git.rickyman7@gmail.com>
 <YS6JBBW3d4pmcy/U@kernel.org>
 <9506b14fe2965e4145c034715eb10e02f2137f7b.camel@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <9506b14fe2965e4145c034715eb10e02f2137f7b.camel@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Fri, Sep 03, 2021 at 11:52:18PM +0200, Riccardo Mancini escreveu:
> Hi Arnaldo,
> 
> thanks for your review and your suggestions, and also for the PRIu64 patch.
> 
> On Tue, 2021-08-31 at 16:54 -0300, Arnaldo Carvalho de Melo wrote:
> > Em Sat, Aug 21, 2021 at 11:19:31AM +0200, Riccardo Mancini escreveu:
> > > This is the final patch splitting evsel__open_cpu.
> > > This patch moves the entire loop code to a separate function, to be
> > > reused for the multithreaded code.
> > 
> > Are you going to use that 'enum perf_event_open_err' somewhere else?
> > I.e. is there a need to expose it in evsel.h?
> 
> Yes, in the next patch (26/37). It's being used to expose a function that just
> does the perf_event_open calls for an evsel. It needs to return such structure
> to provide information about the error (which return code, at which thread).
> 
> > 
> > I'm stopping at this patch to give the ones I merged so far some
> > testing, will now push it to tmp.perf/core.
> 
> I checked tmp.perf/core and it looks good to me.
> I also did some additional tests to check that fallback mechanisms where
> working:
> 
> check missing pid being ignored (rerun until warning is shown)
> $ sudo ./perf bench internals evlist-open-close -i10 -u $UID
> 
> check that weak group fallback is working
> $ sudo ./perf record -e '{cycles,cache-misses,cache-
> references,cpu_clk_unhalted.thread,cycles,cycles,cycles}:W' 
> 
> check that precision_ip fallback is working:
> edited perf-sys.h to make sys_perf_event_open fail if precision_ip > 2
> $ sudo ./perf record -e '{cycles,cs}:P'
> 
> 
> I've also run perf-test on my machine and it's passing too.
> I'm encounteirng one fail on the "BPF filter" test (42), which is present also
> in perf/core, so it should not be related to this patch.

Thanks! I'll try to resume work on it as soon as I have the plumbers
talk ready :-)

- Arnaldo
 
> Thanks,
> Riccardo
> 
> > 
> > - Arnaldo
> >  
> > > Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> > > ---
> > >  tools/perf/util/evsel.c | 142 ++++++++++++++++++++++++----------------
> > >  tools/perf/util/evsel.h |  12 ++++
> > >  2 files changed, 99 insertions(+), 55 deletions(-)
> > > 
> > > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > > index 2e95416b8320c6b9..e41f55a7a70ea630 100644
> > > --- a/tools/perf/util/evsel.c
> > > +++ b/tools/perf/util/evsel.c
> > > @@ -1945,6 +1945,82 @@ bool evsel__increase_rlimit(enum rlimit_action
> > > *set_rlimit)
> > >         return false;
> > >  }
> > >  
> > > +static struct perf_event_open_result perf_event_open(struct evsel *evsel,
> > > +                                       pid_t pid, int cpu, int thread,
> > > +                                       struct perf_cpu_map *cpus,
> > > +                                       struct perf_thread_map *threads)
> > > +{
> > > +       int fd, group_fd, rc;
> > > +       struct perf_event_open_result res;
> > > +
> > > +       if (!evsel->cgrp && !evsel->core.system_wide)
> > > +               pid = perf_thread_map__pid(threads, thread);
> > > +
> > > +       group_fd = get_group_fd(evsel, cpu, thread);
> > > +
> > > +       test_attr__ready();
> > > +
> > > +       pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d  group_fd %d  flags
> > > %#lx",
> > > +                       pid, cpus->map[cpu], group_fd, evsel->open_flags);
> > > +
> > > +       fd = sys_perf_event_open(&evsel->core.attr, pid, cpus->map[cpu],
> > > +                               group_fd, evsel->open_flags);
> > > +
> > > +       FD(evsel, cpu, thread) = fd;
> > > +       res.fd = fd;
> > > +
> > > +       if (fd < 0) {
> > > +               rc = -errno;
> > > +
> > > +               pr_debug2_peo("\nsys_perf_event_open failed, error %d\n",
> > > +                               rc);
> > > +               res.rc = rc;
> > > +               res.err = PEO_FALLBACK;
> > > +               return res;
> > > +       }
> > > +
> > > +       bpf_counter__install_pe(evsel, cpu, fd);
> > > +
> > > +       if (unlikely(test_attr__enabled)) {
> > > +               test_attr__open(&evsel->core.attr, pid,
> > > +                       cpus->map[cpu], fd,
> > > +                       group_fd, evsel->open_flags);
> > > +       }
> > > +
> > > +       pr_debug2_peo(" = %d\n", fd);
> > > +
> > > +       if (evsel->bpf_fd >= 0) {
> > > +               int evt_fd = fd;
> > > +               int bpf_fd = evsel->bpf_fd;
> > > +
> > > +               rc = ioctl(evt_fd,
> > > +                               PERF_EVENT_IOC_SET_BPF,
> > > +                               bpf_fd);
> > > +               if (rc && errno != EEXIST) {
> > > +                       pr_err("failed to attach bpf fd %d: %s\n",
> > > +                               bpf_fd, strerror(errno));
> > > +                       res.rc = -EINVAL;
> > > +                       res.err = PEO_ERROR;
> > > +                       return res;
> > > +               }
> > > +       }
> > > +
> > > +       /*
> > > +        * If we succeeded but had to kill clockid, fail and
> > > +        * have evsel__open_strerror() print us a nice error.
> > > +        */
> > > +       if (perf_missing_features.clockid ||
> > > +               perf_missing_features.clockid_wrong) {
> > > +               res.rc = -EINVAL;
> > > +               res.err = PEO_ERROR;
> > > +               return res;
> > > +       }
> > > +
> > > +       res.rc = 0;
> > > +       res.err = PEO_SUCCESS;
> > > +       return res;
> > > +}
> > > +
> > >  static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
> > >                 struct perf_thread_map *threads,
> > >                 int start_cpu, int end_cpu)
> > > @@ -1952,6 +2028,7 @@ static int evsel__open_cpu(struct evsel *evsel, struct
> > > perf_cpu_map *cpus,
> > >         int cpu, thread, nthreads;
> > >         int pid = -1, err, old_errno;
> > >         enum rlimit_action set_rlimit = NO_CHANGE;
> > > +       struct perf_event_open_result peo_res;
> > >  
> > >         err = __evsel__prepare_open(evsel, cpus, threads);
> > >         if (err)
> > > @@ -1979,67 +2056,22 @@ static int evsel__open_cpu(struct evsel *evsel, struct
> > > perf_cpu_map *cpus,
> > >         for (cpu = start_cpu; cpu < end_cpu; cpu++) {
> > >  
> > >                 for (thread = 0; thread < nthreads; thread++) {
> > > -                       int fd, group_fd;
> > >  retry_open:
> > >                         if (thread >= nthreads)
> > >                                 break;
> > >  
> > > -                       if (!evsel->cgrp && !evsel->core.system_wide)
> > > -                               pid = perf_thread_map__pid(threads, thread);
> > > -
> > > -                       group_fd = get_group_fd(evsel, cpu, thread);
> > > -
> > > -                       test_attr__ready();
> > > -
> > > -                       pr_debug2_peo("sys_perf_event_open: pid %d  cpu %d 
> > > group_fd %d  flags %#lx",
> > > -                               pid, cpus->map[cpu], group_fd, evsel-
> > > >open_flags);
> > > +                       peo_res = perf_event_open(evsel, pid, cpu, thread,
> > > cpus,
> > > +                                               threads);
> > >  
> > > -                       fd = sys_perf_event_open(&evsel->core.attr, pid, cpus-
> > > >map[cpu],
> > > -                                               group_fd, evsel->open_flags);
> > > -
> > > -                       FD(evsel, cpu, thread) = fd;
> > > -
> > > -                       if (fd < 0) {
> > > -                               err = -errno;
> > > -
> > > -                               pr_debug2_peo("\nsys_perf_event_open failed,
> > > error %d\n",
> > > -                                         err);
> > > +                       err = peo_res.rc;
> > > +                       switch (peo_res.err) {
> > > +                       case PEO_SUCCESS:
> > > +                               set_rlimit = NO_CHANGE;
> > > +                               continue;
> > > +                       case PEO_FALLBACK:
> > >                                 goto try_fallback;
> > > -                       }
> > > -
> > > -                       bpf_counter__install_pe(evsel, cpu, fd);
> > > -
> > > -                       if (unlikely(test_attr__enabled)) {
> > > -                               test_attr__open(&evsel->core.attr, pid, cpus-
> > > >map[cpu],
> > > -                                               fd, group_fd, evsel-
> > > >open_flags);
> > > -                       }
> > > -
> > > -                       pr_debug2_peo(" = %d\n", fd);
> > > -
> > > -                       if (evsel->bpf_fd >= 0) {
> > > -                               int evt_fd = fd;
> > > -                               int bpf_fd = evsel->bpf_fd;
> > > -
> > > -                               err = ioctl(evt_fd,
> > > -                                           PERF_EVENT_IOC_SET_BPF,
> > > -                                           bpf_fd);
> > > -                               if (err && errno != EEXIST) {
> > > -                                       pr_err("failed to attach bpf fd %d:
> > > %s\n",
> > > -                                              bpf_fd, strerror(errno));
> > > -                                       err = -EINVAL;
> > > -                                       goto out_close;
> > > -                               }
> > > -                       }
> > > -
> > > -                       set_rlimit = NO_CHANGE;
> > > -
> > > -                       /*
> > > -                        * If we succeeded but had to kill clockid, fail and
> > > -                        * have evsel__open_strerror() print us a nice error.
> > > -                        */
> > > -                       if (perf_missing_features.clockid ||
> > > -                           perf_missing_features.clockid_wrong) {
> > > -                               err = -EINVAL;
> > > +                       default:
> > > +                       case PEO_ERROR:
> > >                                 goto out_close;
> > >                         }
> > >                 }
> > > diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> > > index 0a245afab2d87d74..8c9827a93ac001a7 100644
> > > --- a/tools/perf/util/evsel.h
> > > +++ b/tools/perf/util/evsel.h
> > > @@ -282,6 +282,18 @@ int evsel__enable(struct evsel *evsel);
> > >  int evsel__disable(struct evsel *evsel);
> > >  int evsel__disable_cpu(struct evsel *evsel, int cpu);
> > >  
> > > +enum perf_event_open_err {
> > > +       PEO_SUCCESS,
> > > +       PEO_FALLBACK,
> > > +       PEO_ERROR
> > > +};
> > > +
> > > +struct perf_event_open_result {
> > > +       enum perf_event_open_err err;
> > > +       int rc;
> > > +       int fd;
> > > +};
> > > +
> > >  int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int
> > > cpu);
> > >  int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map
> > > *threads);
> > >  int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
> > > -- 
> > > 2.31.1
> > 
> 

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 1104BC433EF
	for <linux-perf-users@archiver.kernel.org>; Fri,  8 Oct 2021 14:29:20 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id DD19C610E5
	for <linux-perf-users@archiver.kernel.org>; Fri,  8 Oct 2021 14:29:19 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S237676AbhJHObO (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Fri, 8 Oct 2021 10:31:14 -0400
Received: from mail.kernel.org ([198.145.29.99]:42936 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S229756AbhJHObJ (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Fri, 8 Oct 2021 10:31:09 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 143D460EB6;
        Fri,  8 Oct 2021 14:29:14 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1633703354;
        bh=8okn7WP++OasUoqVVEB1PJg6oI3Q18MMY20pDXxku2M=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=XelR3LJXFLSXs7lgpCH8wyXJ/sZ7T24II38jHjkrAi3W37jkBXIK0VhPJ9BtWtRge
         bxBW3lHLheFQckuVxTfmnQYkoY9UFrZ51cXbofV7E5vEAXUOnaOzPeU5+1oOqvBLOU
         r82S7JJJ3JNSDoR/5zI9u+nPGk7Lvlh5P1tQCfqJCXGAgj6VtH4gDd8VrLVavFqT2W
         NjYuvC/eq4LpwX09kMHqjd/RSoGkogbetKJPd1DJbAHvwybn2CjxUd/2+higVD4xQ1
         MBHKUnYKvpwnRv69AjzrSevsvU1aWUgeQ28Gp0ZsewP5dz/gOqOaFD77hllbFyLbnL
         /uXsDmbLr+9pA==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id D994A410A1; Fri,  8 Oct 2021 11:29:10 -0300 (-03)
Date:   Fri, 8 Oct 2021 11:29:10 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 01/37] libperf cpumap: improve idx function
Message-ID: <YWBVtjS4qdnWKpK4@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <f1543c15797169c21e8b205a4a6751159180580d.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <f1543c15797169c21e8b205a4a6751159180580d.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:07AM +0200, Riccardo Mancini escreveu:
> >From commit 7074674e7338863e ("perf cpumap: Maintain cpumaps ordered
> and without dups"), perf_cpu_map elements are sorted in ascending order.
> 
> This patch improves the perf_cpu_map__idx function by using a binary
> search.

Thanks, applied.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/lib/perf/cpumap.c | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c
> index ca0215047c326af4..fb633272be3aaed9 100644
> --- a/tools/lib/perf/cpumap.c
> +++ b/tools/lib/perf/cpumap.c
> @@ -265,11 +265,18 @@ bool perf_cpu_map__empty(const struct perf_cpu_map *map)
>  
>  int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
>  {
> -	int i;
> +	int low = 0, high = cpus->nr, idx, cpu_at_idx;
>  
> -	for (i = 0; i < cpus->nr; ++i) {
> -		if (cpus->map[i] == cpu)
> -			return i;
> +	while (low < high) {
> +		idx = (low + high) / 2;
> +		cpu_at_idx = cpus->map[idx];
> +
> +		if (cpu_at_idx == cpu)
> +			return idx;
> +		else if (cpu_at_idx > cpu)
> +			high = idx;
> +		else
> +			low = idx+1;
>  	}
>  
>  	return -1;
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 2C54FC433F5
	for <linux-perf-users@archiver.kernel.org>; Fri,  8 Oct 2021 14:38:31 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id 0F21460F5C
	for <linux-perf-users@archiver.kernel.org>; Fri,  8 Oct 2021 14:38:31 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S230287AbhJHOkZ (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Fri, 8 Oct 2021 10:40:25 -0400
Received: from mail.kernel.org ([198.145.29.99]:46992 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S229756AbhJHOkY (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Fri, 8 Oct 2021 10:40:24 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 0585F60EB6;
        Fri,  8 Oct 2021 14:38:29 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1633703909;
        bh=sWrfbFTY7cx1134BJRPVHEJpmaXkRzVNn6Kio4zNSUo=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=L3PIfTAh2sLIVyWs1OvWeV68fHTS8b1OS59f5xXLGU1APEhkZyhcK/g0BfkkYdxAq
         E1UCre2ZayXXeQ0QYt4kJeG1aYuTJMa4uE1oGuxgOEcOonPxjd+RC2mrXChMv9Ina6
         XoU9hDg9KouiHTp0o5Sq7Lpas/eFusWYL7bAQmxZFuqZJ4a0IGzOF5JwbyCmtiDMoU
         sYi+2dYThtF/8o3X4j6IFydHtZ4mxtWytk5JBZzlVq+CkGgYuz/sYV0PBa2xoLHTPT
         fSsHnPM5Vct/OD9wYj5RLKerHZvZ5scFtOdpcwZ6/AGYB44CCv9Oq/inKCziF7LjiN
         ZXr7jC5Hhmapw==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 6FBAF410A1; Fri,  8 Oct 2021 11:38:26 -0300 (-03)
Date:   Fri, 8 Oct 2021 11:38:26 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Andi Kleen <ak@linux.intel.com>
Cc:     Riccardo Mancini <rickyman7@gmail.com>,
        Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 03/37] perf evlist: replace evsel__cpu_iter*
 functions with evsel__find_cpu()
Message-ID: <YWBX4slh2U7yNahE@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <5bfe13dd0985611285bb697987816ddc36e93e76.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <5bfe13dd0985611285bb697987816ddc36e93e76.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:09AM +0200, Riccardo Mancini escreveu:
> Commit a8cbe40fe9f4ba49 ("perf evsel: Add iterator to iterate over events
> ordered by CPU") introduced an iterator for evsel.core.cpus inside evsel
> which is used to iterate over evlist one CPU at a time.
> 
> However, this solution is hacky since it involves a mutable state in the
> evsel which is supposed to be unmodified during iteration.
> Sice checking that a CPU is within the evsel can be done quickly in
> O(logn) time, this patch replaces the aforementioned iterator with a
> simple check.

Andi, can you take a look at this and provide your Reviewed-by, please?

- Arnaldo
 
> Cc: Andi Kleen <ak@linux.intel.com>
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/builtin-stat.c | 24 +++++++++--------
>  tools/perf/util/evlist.c  | 54 +++++++++++----------------------------
>  tools/perf/util/evlist.h  |  5 +---
>  tools/perf/util/evsel.h   |  1 -
>  4 files changed, 30 insertions(+), 54 deletions(-)
> 
> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 84de61795e67bbb9..62dcc001c8f0a78b 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -407,7 +407,7 @@ static int read_affinity_counters(struct timespec *rs)
>  {
>  	struct evsel *counter;
>  	struct affinity affinity;
> -	int i, ncpus, cpu;
> +	int i, ncpus, cpu, cpu_idx;
>  
>  	if (all_counters_use_bpf)
>  		return 0;
> @@ -424,13 +424,14 @@ static int read_affinity_counters(struct timespec *rs)
>  		affinity__set(&affinity, cpu);
>  
>  		evlist__for_each_entry(evsel_list, counter) {
> -			if (evsel__cpu_iter_skip(counter, cpu))
> +			cpu_idx = evsel__find_cpu(counter, cpu);
> +			if (cpu_idx < 0)
>  				continue;
>  			if (evsel__is_bpf(counter))
>  				continue;
>  			if (!counter->err) {
>  				counter->err = read_counter_cpu(counter, rs,
> -								counter->cpu_iter - 1);
> +								cpu_idx);
>  			}
>  		}
>  	}
> @@ -789,7 +790,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
>  	const bool forks = (argc > 0);
>  	bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
>  	struct affinity affinity;
> -	int i, cpu, err;
> +	int i, cpu, cpu_idx, err;
>  	bool second_pass = false;
>  
>  	if (forks) {
> @@ -823,7 +824,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
>  		affinity__set(&affinity, cpu);
>  
>  		evlist__for_each_entry(evsel_list, counter) {
> -			if (evsel__cpu_iter_skip(counter, cpu))
> +			cpu_idx = evsel__find_cpu(counter, cpu);
> +			if (cpu_idx < 0)
>  				continue;
>  			if (counter->reset_group || counter->errored)
>  				continue;
> @@ -831,7 +833,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
>  				continue;
>  try_again:
>  			if (create_perf_stat_counter(counter, &stat_config, &target,
> -						     counter->cpu_iter - 1) < 0) {
> +						     cpu_idx) < 0) {
>  
>  				/*
>  				 * Weak group failed. We cannot just undo this here
> @@ -877,22 +879,24 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
>  			evlist__for_each_entry(evsel_list, counter) {
>  				if (!counter->reset_group && !counter->errored)
>  					continue;
> -				if (evsel__cpu_iter_skip_no_inc(counter, cpu))
> +				cpu_idx = evsel__find_cpu(counter, cpu);
> +				if (cpu_idx < 0)
>  					continue;
> -				perf_evsel__close_cpu(&counter->core, counter->cpu_iter);
> +				perf_evsel__close_cpu(&counter->core, cpu_idx);
>  			}
>  			/* Now reopen weak */
>  			evlist__for_each_entry(evsel_list, counter) {
>  				if (!counter->reset_group && !counter->errored)
>  					continue;
> -				if (evsel__cpu_iter_skip(counter, cpu))
> +				cpu_idx = evsel__find_cpu(counter, cpu);
> +				if (cpu_idx < 0)
>  					continue;
>  				if (!counter->reset_group)
>  					continue;
>  try_again_reset:
>  				pr_debug2("reopening weak %s\n", evsel__name(counter));
>  				if (create_perf_stat_counter(counter, &stat_config, &target,
> -							     counter->cpu_iter - 1) < 0) {
> +							     cpu_idx) < 0) {
>  
>  					switch (stat_handle_error(counter)) {
>  					case COUNTER_FATAL:
> diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
> index 47581a237c7a7848..3d55d9a53b9f4875 100644
> --- a/tools/perf/util/evlist.c
> +++ b/tools/perf/util/evlist.c
> @@ -341,36 +341,9 @@ static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
>  		return perf_thread_map__nr(evlist->core.threads);
>  }
>  
> -void evlist__cpu_iter_start(struct evlist *evlist)
> +int evsel__find_cpu(struct evsel *ev, int cpu)
>  {
> -	struct evsel *pos;
> -
> -	/*
> -	 * Reset the per evsel cpu_iter. This is needed because
> -	 * each evsel's cpumap may have a different index space,
> -	 * and some operations need the index to modify
> -	 * the FD xyarray (e.g. open, close)
> -	 */
> -	evlist__for_each_entry(evlist, pos)
> -		pos->cpu_iter = 0;
> -}
> -
> -bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
> -{
> -	if (ev->cpu_iter >= ev->core.cpus->nr)
> -		return true;
> -	if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
> -		return true;
> -	return false;
> -}
> -
> -bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
> -{
> -	if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
> -		ev->cpu_iter++;
> -		return false;
> -	}
> -	return true;
> +	return perf_cpu_map__idx(ev->core.cpus, cpu);
>  }
>  
>  static int evsel__strcmp(struct evsel *pos, char *evsel_name)
> @@ -400,7 +373,7 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
>  {
>  	struct evsel *pos;
>  	struct affinity affinity;
> -	int cpu, i, imm = 0;
> +	int cpu, i, imm = 0, cpu_idx;
>  	bool has_imm = false;
>  
>  	if (affinity__setup(&affinity) < 0)
> @@ -414,7 +387,8 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
>  			evlist__for_each_entry(evlist, pos) {
>  				if (evsel__strcmp(pos, evsel_name))
>  					continue;
> -				if (evsel__cpu_iter_skip(pos, cpu))
> +				cpu_idx = evsel__find_cpu(pos, cpu);
> +				if (cpu_idx < 0)
>  					continue;
>  				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
>  					continue;
> @@ -422,7 +396,7 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
>  					has_imm = true;
>  				if (pos->immediate != imm)
>  					continue;
> -				evsel__disable_cpu(pos, pos->cpu_iter - 1);
> +				evsel__disable_cpu(pos, cpu_idx);
>  			}
>  		}
>  		if (!has_imm)
> @@ -462,7 +436,7 @@ static void __evlist__enable(struct evlist *evlist, char *evsel_name)
>  {
>  	struct evsel *pos;
>  	struct affinity affinity;
> -	int cpu, i;
> +	int cpu, i, cpu_idx;
>  
>  	if (affinity__setup(&affinity) < 0)
>  		return;
> @@ -473,11 +447,12 @@ static void __evlist__enable(struct evlist *evlist, char *evsel_name)
>  		evlist__for_each_entry(evlist, pos) {
>  			if (evsel__strcmp(pos, evsel_name))
>  				continue;
> -			if (evsel__cpu_iter_skip(pos, cpu))
> +			cpu_idx = evsel__find_cpu(pos, cpu);
> +			if (cpu_idx < 0)
>  				continue;
>  			if (!evsel__is_group_leader(pos) || !pos->core.fd)
>  				continue;
> -			evsel__enable_cpu(pos, pos->cpu_iter - 1);
> +			evsel__enable_cpu(pos, cpu_idx);
>  		}
>  	}
>  	affinity__cleanup(&affinity);
> @@ -1264,7 +1239,7 @@ void evlist__close(struct evlist *evlist)
>  {
>  	struct evsel *evsel;
>  	struct affinity affinity;
> -	int cpu, i;
> +	int cpu, i, cpu_idx;
>  
>  	/*
>  	 * With perf record core.cpus is usually NULL.
> @@ -1282,9 +1257,10 @@ void evlist__close(struct evlist *evlist)
>  		affinity__set(&affinity, cpu);
>  
>  		evlist__for_each_entry_reverse(evlist, evsel) {
> -			if (evsel__cpu_iter_skip(evsel, cpu))
> -			    continue;
> -			perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
> +			cpu_idx = evsel__find_cpu(evsel, cpu);
> +			if (cpu_idx < 0)
> +				continue;
> +			perf_evsel__close_cpu(&evsel->core, cpu_idx);
>  		}
>  	}
>  	affinity__cleanup(&affinity);
> diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
> index 5c22383489ae4905..fde893170c7ba6d2 100644
> --- a/tools/perf/util/evlist.h
> +++ b/tools/perf/util/evlist.h
> @@ -310,15 +310,12 @@ void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
>  	__evlist__for_each_entry_safe(&(evlist)->core.entries, tmp, evsel)
>  
>  #define evlist__for_each_cpu(evlist, index, cpu)	\
> -	evlist__cpu_iter_start(evlist);			\
>  	perf_cpu_map__for_each_cpu (cpu, index, (evlist)->core.all_cpus)
>  
>  struct evsel *evlist__get_tracking_event(struct evlist *evlist);
>  void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel);
>  
> -void evlist__cpu_iter_start(struct evlist *evlist);
> -bool evsel__cpu_iter_skip(struct evsel *ev, int cpu);
> -bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu);
> +int evsel__find_cpu(struct evsel *ev, int cpu);
>  
>  struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
>  
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index 80383096d51c5f00..eabccce406886320 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -119,7 +119,6 @@ struct evsel {
>  	bool			errored;
>  	struct hashmap		*per_pkg_mask;
>  	int			err;
> -	int			cpu_iter;
>  	struct {
>  		evsel__sb_cb_t	*cb;
>  		void		*data;
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
	by smtp.lore.kernel.org (Postfix) with ESMTP id 062F3C433EF
	for <linux-perf-users@archiver.kernel.org>; Fri,  8 Oct 2021 14:46:07 +0000 (UTC)
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by mail.kernel.org (Postfix) with ESMTP id D7D9D61039
	for <linux-perf-users@archiver.kernel.org>; Fri,  8 Oct 2021 14:46:06 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S233614AbhJHOsB (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Fri, 8 Oct 2021 10:48:01 -0400
Received: from mail.kernel.org ([198.145.29.99]:49284 "EHLO mail.kernel.org"
        rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
        id S242503AbhJHOsA (ORCPT <rfc822;linux-perf-users@vger.kernel.org>);
        Fri, 8 Oct 2021 10:48:00 -0400
Received: by mail.kernel.org (Postfix) with ESMTPSA id 3C35660F58;
        Fri,  8 Oct 2021 14:46:05 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
        s=k20201202; t=1633704365;
        bh=KLXzbMi+/trg1z5YXY89GWNJjA4g33FvXuFqv1Rd9bk=;
        h=Date:From:To:Cc:Subject:References:In-Reply-To:From;
        b=ddXSJriJsdR6ETVhPhObOHdH7hjFweaOhrXLCWJulakA0+WToKl7r7EerGzIVXVbI
         Vt6k9vmXfP9F+Ne8ylZ0s0JYDsNlfu4+M49FqmDSLdejtiku+PiT9MuVZM4EJwx8Fk
         5rAc3nDFyMtBgmlh4py0I4Dyl3iEFeNv/I2j1yvDAkUB25oOJVnHUFXsIusCwpHpNm
         E/RvcXGueyiwVdp9RJsoxbMFPWcrBXEdVqX0qy3dWKAztgRjbe6NCdSG47MoL3XBl8
         WizvTt0hs/0scUlhne580XwLweqETUe5D4ipVixYMGMsuAgraxEjjMzOqRWKIZ4fUO
         atOFPRRye4P1g==
Received: by quaco.ghostprotocols.net (Postfix, from userid 1000)
        id 44465410A1; Fri,  8 Oct 2021 11:46:02 -0300 (-03)
Date:   Fri, 8 Oct 2021 11:46:02 -0300
From:   Arnaldo Carvalho de Melo <acme@kernel.org>
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Ian Rogers <irogers@google.com>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1 36/37] perf test/evlist-open-close: use inline
 func to convert timeval to usec
Message-ID: <YWBZqlg3hgsIWq+q@kernel.org>
References: <cover.1629490974.git.rickyman7@gmail.com>
 <b95035ec4a125355be8ea843f7275c4580da6398.1629490974.git.rickyman7@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <b95035ec4a125355be8ea843f7275c4580da6398.1629490974.git.rickyman7@gmail.com>
X-Url:  http://acmel.wordpress.com
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

Em Sat, Aug 21, 2021 at 11:19:42AM +0200, Riccardo Mancini escreveu:
> This patch introduces a new inline function to convert a timeval to
> usec.
> This function will be used also in the next patch.

Thanks, applied.

- Arnaldo

 
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/bench/evlist-open-close.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/bench/evlist-open-close.c b/tools/perf/bench/evlist-open-close.c
> index c18aa85725281795..00d0aef564f80d44 100644
> --- a/tools/perf/bench/evlist-open-close.c
> +++ b/tools/perf/bench/evlist-open-close.c
> @@ -26,6 +26,11 @@ static int iterations = 100;
>  static int nr_events = 1;
>  static const char *event_string = "dummy";
>  
> +static inline u64 timeval2usec(struct timeval *tv)
> +{
> +	return tv->tv_sec * USEC_PER_SEC + tv->tv_usec;
> +}
> +
>  static struct record_opts opts = {
>  	.sample_time	     = true,
>  	.mmap_pages	     = UINT_MAX,
> @@ -197,7 +202,7 @@ static int bench_evlist_open_close__run(char *evstr)
>  
>  		gettimeofday(&end, NULL);
>  		timersub(&end, &start, &diff);
> -		runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
> +		runtime_us = timeval2usec(&diff);
>  		update_stats(&time_stats, runtime_us);
>  
>  		evlist__delete(evlist);
> -- 
> 2.31.1

-- 

- Arnaldo

From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <linux-perf-users-owner@kernel.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on
	aws-us-west-2-korg-lkml-1.web.codeaurora.org
Received: from vger.kernel.org (vger.kernel.org [23.128.96.18])
	by smtp.lore.kernel.org (Postfix) with ESMTP id A8D17C433EF
	for <linux-perf-users@archiver.kernel.org>; Sat, 11 Dec 2021 00:21:06 +0000 (UTC)
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
        id S244133AbhLKAYl (ORCPT
        <rfc822;linux-perf-users@archiver.kernel.org>);
        Fri, 10 Dec 2021 19:24:41 -0500
Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54318 "EHLO
        lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org
        with ESMTP id S244085AbhLKAYk (ORCPT
        <rfc822;linux-perf-users@vger.kernel.org>);
        Fri, 10 Dec 2021 19:24:40 -0500
Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d])
        by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6BC8C061714
        for <linux-perf-users@vger.kernel.org>; Fri, 10 Dec 2021 16:21:04 -0800 (PST)
Received: by mail-il1-x12d.google.com with SMTP id d14so7535793ila.1
        for <linux-perf-users@vger.kernel.org>; Fri, 10 Dec 2021 16:21:04 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=google.com; s=20210112;
        h=mime-version:references:in-reply-to:from:date:message-id:subject:to
         :cc;
        bh=hQVK/fW2IHx2/eKTcMFa0xhiXhA/tPMPik28eRkL13o=;
        b=QaJzGZHASS1oX67CObMN87QSxGDyHUc5LDDXay3wq8QbJDc6kWWggxB03qbRamgMNz
         Qu+v+EEiG3B9eC6hoarkqGUIXYLbwwJRv47i6wzWZmabNpsrRvw6erGDm8mF3vBMvRLO
         1vZYkncNG5g3tF/xKw0RIV9AaMTbODDhqHz6rnKyCWSGdpf0WPrPXD/QDg9Y0OYlijHS
         3tm0fnr1FwoQDcTzUfq34M2vbbdr9msvvVxKz4y6+8NxkZrFDAV+1lDzRORVY2YsovDe
         uTWw8C1nzWFcJk9epDv5+2bHNQYKLmZgsEzN6z7Jrr6yyCqFCWNuG/vtW8hEUY7eml8H
         N11Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20210112;
        h=x-gm-message-state:mime-version:references:in-reply-to:from:date
         :message-id:subject:to:cc;
        bh=hQVK/fW2IHx2/eKTcMFa0xhiXhA/tPMPik28eRkL13o=;
        b=Ymo0GYcKygnrRAuznn9285+y3CeqEkD1NvKIBjWO7BjM/sHHFu2JVKLVLT8o5XY+NI
         kiPpT+QFTW8bXz0Vw/XB8GV0UmQ5HCWPHzJzB9Aq29wFz5aIKRbs6/DJTdSb8flTwzzw
         oS241EQjxlBaQ7fIp9M7sUo5Np1+35rqwHEAKTeagFhHjA5I7XgjTLZL+aJExIYnKpsH
         1KNGM9yOsJ4E+CxZZIhk36nEeyxjvZyhGlTFkzaaUubEDPz90KDmkMUdZ82w4NyaBqml
         HIBLwLfyz2xC4EAFufW2DThmjlDfz7VhXJekd6b3BGtXwrSFoLFydTbiVTF2xXDua+hH
         E4EQ==
X-Gm-Message-State: AOAM533L9UvtnsLLLr7xO7sN73qWZtRaAhv6u+eHgaawOntA8oMBMMJc
        3e5Lgzg5ajoU2dLUj0Mp4BlKyQqU/Gg4tXU3ANwR/w==
X-Google-Smtp-Source: ABdhPJyj9vh7tqRpnD47wWgXa/m77w147OhnzNh/Dmm7irKvVlej9YpLK9/wFIRcKbQ/uMBTzF5A1OTWDEbEZ1TwL4Q=
X-Received: by 2002:a05:6e02:931:: with SMTP id o17mr21339890ilt.174.1639182063829;
 Fri, 10 Dec 2021 16:21:03 -0800 (PST)
MIME-Version: 1.0
References: <cover.1629490974.git.rickyman7@gmail.com> <5bfe13dd0985611285bb697987816ddc36e93e76.1629490974.git.rickyman7@gmail.com>
In-Reply-To: <5bfe13dd0985611285bb697987816ddc36e93e76.1629490974.git.rickyman7@gmail.com>
From:   Ian Rogers <irogers@google.com>
Date:   Fri, 10 Dec 2021 16:20:52 -0800
Message-ID: <CAP-5=fV4aFHf1K+wuqJ19W1GPsj4fnO-EznLf=wL1z7nKf_tqg@mail.gmail.com>
Subject: Re: [RFC PATCH v1 03/37] perf evlist: replace evsel__cpu_iter*
 functions with evsel__find_cpu
To:     Riccardo Mancini <rickyman7@gmail.com>
Cc:     Arnaldo Carvalho de Melo <acme@kernel.org>,
        Namhyung Kim <namhyung@kernel.org>,
        Peter Zijlstra <peterz@infradead.org>,
        Ingo Molnar <mingo@redhat.com>,
        Mark Rutland <mark.rutland@arm.com>,
        Jiri Olsa <jolsa@redhat.com>, linux-kernel@vger.kernel.org,
        linux-perf-users@vger.kernel.org, Andi Kleen <ak@linux.intel.com>
Content-Type: text/plain; charset="UTF-8"
Precedence: bulk
List-ID: <linux-perf-users.vger.kernel.org>
X-Mailing-List: linux-perf-users@vger.kernel.org

On Sat, Aug 21, 2021 at 2:19 AM Riccardo Mancini <rickyman7@gmail.com> wrote:
>
> Commit a8cbe40fe9f4ba49 ("perf evsel: Add iterator to iterate over events
> ordered by CPU") introduced an iterator for evsel.core.cpus inside evsel
> which is used to iterate over evlist one CPU at a time.
>
> However, this solution is hacky since it involves a mutable state in the
> evsel which is supposed to be unmodified during iteration.
> Sice checking that a CPU is within the evsel can be done quickly in
> O(logn) time, this patch replaces the aforementioned iterator with a
> simple check.

I like this change. It ties in with the CPU vs index refactoring I'm
doing and expanding upon in:
https://lore.kernel.org/lkml/20211208024607.1784932-1-irogers@google.com/
The O(log(n)) look up of the CPU from the CPU map I think can be
improved, but it is good enough for now. We can improve upon it by
recognizing that the CPU we're looking for in the CPU map is 'cpu /
num_cpus' into the map - ie. don't binary search from the middle, but
start from somewhere near where the CPU will be. Something like
interpolation search [1] would do this in O(log(log(n)) time, but
because CPU maps are so regular in layout I'd expect it would be O(1)
for us.

That said, I think the right thing to do here is to introduce a new
struct which would behave like an STL style iterator. STL style
iterators are returned by begin and end functions. They implement a
next() routine as operator ++. Termination of the loop happens when
the iterator equals the end value. The current evlist affinity
iteration needs two loops. The outer loop is iterating over CPUs and
the caller needs to set affinity to the CPU. An inner loop iterates
over the evlist again, skipping until an appropriate evsel is
encountered for the outer loop's CPU. I think it would be better if we
had a single loop, and this loop is going to maintain two pieces of
state in the iterator, the current CPU and evsel. The loop start, next
and end will change the CPU affinity. The next routine will iterate
over every evsel for a CPU and then advance to the next CPU and so on
until all CPUs and the evsels are done. For something like:
$ perf stat -a -e cycles,power/energy-pkg/ -I 1000
The cpumasks are going to be all CPUs for cycles, and on my machine 0
and 18 for energy-pkg. So the iterator will be on CPU 0 with an evsel
for cycles, then for energy-pkg, next for CPU 1 it will have an evsel
for cycles, and so on until CPU 18. For CPU 18 it will be like CPU.0
and have the evsel for cycles and energy-pkg. For CPU 19 it is just
cycles again until you get to CPU 35.

In the API it'd be something like:

struct evlist_cpu_iterator {
  int cpu;
  struct evsel *evsel;
  struct evlist *container;
  struct affinity saved_affinity;
};

struct evlist_cpu_iterator evlist__cpu_iter_start(struct evlist *evlist);
void evlist_cpu_iterator__next(struct evlist_cpu_iterator *itr);
bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *itr);

The advantage of this API is that we know the CPU and evsel without a
lookup (as with the current code), the affinity is hidden in the
abstraction and we get rid of the global iterator state from evsel (as
with this patch) that makes parallelization impossible.

I'm working to add something like this into the cpumap refactoring
effort. That isn't looking to do parallelization but merely to get
correctness, as at the moment CPUs and CPU map indices are often
confused leading to crashes or incorrect behavior. I'm aware that this
will mean rebasing this patch series, but I think that will be easier
as the confusion over CPU and index will be ironed out in my changes.

Thanks,
Ian

[1] https://en.wikipedia.org/wiki/Interpolation_search


> Cc: Andi Kleen <ak@linux.intel.com>
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/builtin-stat.c | 24 +++++++++--------
>  tools/perf/util/evlist.c  | 54 +++++++++++----------------------------
>  tools/perf/util/evlist.h  |  5 +---
>  tools/perf/util/evsel.h   |  1 -
>  4 files changed, 30 insertions(+), 54 deletions(-)
>
> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 84de61795e67bbb9..62dcc001c8f0a78b 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -407,7 +407,7 @@ static int read_affinity_counters(struct timespec *rs)
>  {
>         struct evsel *counter;
>         struct affinity affinity;
> -       int i, ncpus, cpu;
> +       int i, ncpus, cpu, cpu_idx;
>
>         if (all_counters_use_bpf)
>                 return 0;
> @@ -424,13 +424,14 @@ static int read_affinity_counters(struct timespec *rs)
>                 affinity__set(&affinity, cpu);
>
>                 evlist__for_each_entry(evsel_list, counter) {
> -                       if (evsel__cpu_iter_skip(counter, cpu))
> +                       cpu_idx = evsel__find_cpu(counter, cpu);
> +                       if (cpu_idx < 0)
>                                 continue;
>                         if (evsel__is_bpf(counter))
>                                 continue;
>                         if (!counter->err) {
>                                 counter->err = read_counter_cpu(counter, rs,
> -                                                               counter->cpu_iter - 1);
> +                                                               cpu_idx);
>                         }
>                 }
>         }
> @@ -789,7 +790,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
>         const bool forks = (argc > 0);
>         bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
>         struct affinity affinity;
> -       int i, cpu, err;
> +       int i, cpu, cpu_idx, err;
>         bool second_pass = false;
>
>         if (forks) {
> @@ -823,7 +824,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
>                 affinity__set(&affinity, cpu);
>
>                 evlist__for_each_entry(evsel_list, counter) {
> -                       if (evsel__cpu_iter_skip(counter, cpu))
> +                       cpu_idx = evsel__find_cpu(counter, cpu);
> +                       if (cpu_idx < 0)
>                                 continue;
>                         if (counter->reset_group || counter->errored)
>                                 continue;
> @@ -831,7 +833,7 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
>                                 continue;
>  try_again:
>                         if (create_perf_stat_counter(counter, &stat_config, &target,
> -                                                    counter->cpu_iter - 1) < 0) {
> +                                                    cpu_idx) < 0) {
>
>                                 /*
>                                  * Weak group failed. We cannot just undo this here
> @@ -877,22 +879,24 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
>                         evlist__for_each_entry(evsel_list, counter) {
>                                 if (!counter->reset_group && !counter->errored)
>                                         continue;
> -                               if (evsel__cpu_iter_skip_no_inc(counter, cpu))
> +                               cpu_idx = evsel__find_cpu(counter, cpu);
> +                               if (cpu_idx < 0)
>                                         continue;
> -                               perf_evsel__close_cpu(&counter->core, counter->cpu_iter);
> +                               perf_evsel__close_cpu(&counter->core, cpu_idx);
>                         }
>                         /* Now reopen weak */
>                         evlist__for_each_entry(evsel_list, counter) {
>                                 if (!counter->reset_group && !counter->errored)
>                                         continue;
> -                               if (evsel__cpu_iter_skip(counter, cpu))
> +                               cpu_idx = evsel__find_cpu(counter, cpu);
> +                               if (cpu_idx < 0)
>                                         continue;
>                                 if (!counter->reset_group)
>                                         continue;
>  try_again_reset:
>                                 pr_debug2("reopening weak %s\n", evsel__name(counter));
>                                 if (create_perf_stat_counter(counter, &stat_config, &target,
> -                                                            counter->cpu_iter - 1) < 0) {
> +                                                            cpu_idx) < 0) {
>
>                                         switch (stat_handle_error(counter)) {
>                                         case COUNTER_FATAL:
> diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
> index 47581a237c7a7848..3d55d9a53b9f4875 100644
> --- a/tools/perf/util/evlist.c
> +++ b/tools/perf/util/evlist.c
> @@ -341,36 +341,9 @@ static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
>                 return perf_thread_map__nr(evlist->core.threads);
>  }
>
> -void evlist__cpu_iter_start(struct evlist *evlist)
> +int evsel__find_cpu(struct evsel *ev, int cpu)
>  {
> -       struct evsel *pos;
> -
> -       /*
> -        * Reset the per evsel cpu_iter. This is needed because
> -        * each evsel's cpumap may have a different index space,
> -        * and some operations need the index to modify
> -        * the FD xyarray (e.g. open, close)
> -        */
> -       evlist__for_each_entry(evlist, pos)
> -               pos->cpu_iter = 0;
> -}
> -
> -bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
> -{
> -       if (ev->cpu_iter >= ev->core.cpus->nr)
> -               return true;
> -       if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
> -               return true;
> -       return false;
> -}
> -
> -bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
> -{
> -       if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
> -               ev->cpu_iter++;
> -               return false;
> -       }
> -       return true;
> +       return perf_cpu_map__idx(ev->core.cpus, cpu);
>  }
>
>  static int evsel__strcmp(struct evsel *pos, char *evsel_name)
> @@ -400,7 +373,7 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
>  {
>         struct evsel *pos;
>         struct affinity affinity;
> -       int cpu, i, imm = 0;
> +       int cpu, i, imm = 0, cpu_idx;
>         bool has_imm = false;
>
>         if (affinity__setup(&affinity) < 0)
> @@ -414,7 +387,8 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
>                         evlist__for_each_entry(evlist, pos) {
>                                 if (evsel__strcmp(pos, evsel_name))
>                                         continue;
> -                               if (evsel__cpu_iter_skip(pos, cpu))
> +                               cpu_idx = evsel__find_cpu(pos, cpu);
> +                               if (cpu_idx < 0)
>                                         continue;
>                                 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
>                                         continue;
> @@ -422,7 +396,7 @@ static void __evlist__disable(struct evlist *evlist, char *evsel_name)
>                                         has_imm = true;
>                                 if (pos->immediate != imm)
>                                         continue;
> -                               evsel__disable_cpu(pos, pos->cpu_iter - 1);
> +                               evsel__disable_cpu(pos, cpu_idx);
>                         }
>                 }
>                 if (!has_imm)
> @@ -462,7 +436,7 @@ static void __evlist__enable(struct evlist *evlist, char *evsel_name)
>  {
>         struct evsel *pos;
>         struct affinity affinity;
> -       int cpu, i;
> +       int cpu, i, cpu_idx;
>
>         if (affinity__setup(&affinity) < 0)
>                 return;
> @@ -473,11 +447,12 @@ static void __evlist__enable(struct evlist *evlist, char *evsel_name)
>                 evlist__for_each_entry(evlist, pos) {
>                         if (evsel__strcmp(pos, evsel_name))
>                                 continue;
> -                       if (evsel__cpu_iter_skip(pos, cpu))
> +                       cpu_idx = evsel__find_cpu(pos, cpu);
> +                       if (cpu_idx < 0)
>                                 continue;
>                         if (!evsel__is_group_leader(pos) || !pos->core.fd)
>                                 continue;
> -                       evsel__enable_cpu(pos, pos->cpu_iter - 1);
> +                       evsel__enable_cpu(pos, cpu_idx);
>                 }
>         }
>         affinity__cleanup(&affinity);
> @@ -1264,7 +1239,7 @@ void evlist__close(struct evlist *evlist)
>  {
>         struct evsel *evsel;
>         struct affinity affinity;
> -       int cpu, i;
> +       int cpu, i, cpu_idx;
>
>         /*
>          * With perf record core.cpus is usually NULL.
> @@ -1282,9 +1257,10 @@ void evlist__close(struct evlist *evlist)
>                 affinity__set(&affinity, cpu);
>
>                 evlist__for_each_entry_reverse(evlist, evsel) {
> -                       if (evsel__cpu_iter_skip(evsel, cpu))
> -                           continue;
> -                       perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
> +                       cpu_idx = evsel__find_cpu(evsel, cpu);
> +                       if (cpu_idx < 0)
> +                               continue;
> +                       perf_evsel__close_cpu(&evsel->core, cpu_idx);
>                 }
>         }
>         affinity__cleanup(&affinity);
> diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
> index 5c22383489ae4905..fde893170c7ba6d2 100644
> --- a/tools/perf/util/evlist.h
> +++ b/tools/perf/util/evlist.h
> @@ -310,15 +310,12 @@ void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
>         __evlist__for_each_entry_safe(&(evlist)->core.entries, tmp, evsel)
>
>  #define evlist__for_each_cpu(evlist, index, cpu)       \
> -       evlist__cpu_iter_start(evlist);                 \
>         perf_cpu_map__for_each_cpu (cpu, index, (evlist)->core.all_cpus)
>
>  struct evsel *evlist__get_tracking_event(struct evlist *evlist);
>  void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel);
>
> -void evlist__cpu_iter_start(struct evlist *evlist);
> -bool evsel__cpu_iter_skip(struct evsel *ev, int cpu);
> -bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu);
> +int evsel__find_cpu(struct evsel *ev, int cpu);
>
>  struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
>
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index 80383096d51c5f00..eabccce406886320 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -119,7 +119,6 @@ struct evsel {
>         bool                    errored;
>         struct hashmap          *per_pkg_mask;
>         int                     err;
> -       int                     cpu_iter;
>         struct {
>                 evsel__sb_cb_t  *cb;
>                 void            *data;
> --
> 2.31.1
>

