From patchwork Fri Jun 24 23:13:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12895167 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: 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 E33D3C43334 for ; Fri, 24 Jun 2022 23:13:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231476AbiFXXNT (ORCPT ); Fri, 24 Jun 2022 19:13:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231151AbiFXXNR (ORCPT ); Fri, 24 Jun 2022 19:13:17 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34EAC69999; Fri, 24 Jun 2022 16:13:17 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id o18so3323885plg.2; Fri, 24 Jun 2022 16:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=W/W8LDN+tzFAEf2BPgwfAuc3ZnltgoEwHVT0bAe4brM=; b=WsF0G+VJ7ZJaU1x/sk1Qfm+U7PZteKatAdVE/LWM9L4N5BQMXcGGqMrKf+N3R9k+Vr uDnQNwc6BhjqQStOjG0837AISbMPODVUDT8hdU6jpv78kCkuVr51qcu14e7PqZu2nYPm 8SJsMJUwUQ0hlfRGfE0jcXU0aYW+JdqZocvrF6IWaRku1l1ngUyFDDIGSvX4Qhvi+Gdg E9hb9PkBuPRDfRn98raNarAuWaTJwwY0H0MOtuwjquWAjmA6Lc/wmBkBb8D4DPjVKCzE dSrCll6BLJVw7oDqeRivZ0j13r7CSxCEMy70swpUTFPeSJ5gynHeYJN5pwUpMGV06Lzd nljw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=W/W8LDN+tzFAEf2BPgwfAuc3ZnltgoEwHVT0bAe4brM=; b=5Wpj9nSWXD/04p8aTcK/+UIkqYrD5fX+fBUatPM9TWD0VMCsxfYRHV+Our7xtMiPDh ozL4h/vQqyJfOZMvkoyzo/C0BNo2a2JQiI1gOrGJlap4pxqoWo97YkCtU6DYlCrGxKti 8iyUye6JxRP3BTBNj7w3CrAfT2+MUzt06zW0P9cZLzycFCxzsO9gICh2P/3Znktly025 UYzxc8dwphy/YciYJHCTD96zIJzJtBFIEeaskdEIjsDzZi2vJL1I8Q+wNn1WzwAkEVxQ +JAgybi2cUS9QUvTjAWL/Bo73cNTq6XQVVzEr26HoCfg8mHZk5Gt2nOpORmyLgsZhx9x Cjaw== X-Gm-Message-State: AJIora9ASotTQC+im7YFASQ+rk4dFhY4rh1z97D9yY4H/l8jJDSrtxRj PmK8KDmricOqFUhn0OjzemLwX0+ZOAs17w== X-Google-Smtp-Source: AGRyM1t5gNy4v4K5UHhprgbyO6kecjEaSRwHg3Gm6BjqBiNSbVYJlSWnDzW0lsyjutiMi0S2TPglTg== X-Received: by 2002:a17:90b:1650:b0:1ec:b5e7:42ae with SMTP id il16-20020a17090b165000b001ecb5e742aemr1356755pjb.15.1656112396599; Fri, 24 Jun 2022 16:13:16 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:480:eeb0:3156:8fd:28f6]) by smtp.gmail.com with ESMTPSA id z19-20020aa78893000000b0050dc76281e0sm2242439pfe.186.2022.06.24.16.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jun 2022 16:13:16 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Milian Wolff , bpf@vger.kernel.org, Blake Jones Subject: [PATCH 1/6] perf offcpu: Fix a build failure on old kernels Date: Fri, 24 Jun 2022 16:13:08 -0700 Message-Id: <20220624231313.367909-2-namhyung@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220624231313.367909-1-namhyung@kernel.org> References: <20220624231313.367909-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Old kernels have task_struct which contains "state" field and newer kernels have "__state". While the get_task_state() in the BPF code handles that in some way, it assumed the current kernel has the new definition and it caused a build error on old kernels. We should not assume anything and access them carefully. Do not use the task struct directly and access them using new and old definitions in a row. Reported-by: Ian Rogers Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_skel/off_cpu.bpf.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_skel/off_cpu.bpf.c index 792ae2847080..cc6d7fd55118 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -71,6 +71,11 @@ struct { __uint(max_entries, 1); } cgroup_filter SEC(".maps"); +/* new kernel task_struct definition */ +struct task_struct___new { + long __state; +} __attribute__((preserve_access_index)); + /* old kernel task_struct definition */ struct task_struct___old { long state; @@ -93,14 +98,17 @@ const volatile bool uses_cgroup_v1 = false; */ static inline int get_task_state(struct task_struct *t) { - if (bpf_core_field_exists(t->__state)) - return BPF_CORE_READ(t, __state); + /* recast pointer to capture new type for compiler */ + struct task_struct___new *t_new = (void *)t; - /* recast pointer to capture task_struct___old type for compiler */ - struct task_struct___old *t_old = (void *)t; + if (bpf_core_field_exists(t_new->__state)) { + return BPF_CORE_READ(t_new, __state); + } else { + /* recast pointer to capture old type for compiler */ + struct task_struct___old *t_old = (void *)t; - /* now use old "state" name of the field */ - return BPF_CORE_READ(t_old, state); + return BPF_CORE_READ(t_old, state); + } } static inline __u64 get_cgroup_id(struct task_struct *t) From patchwork Fri Jun 24 23:13:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12895168 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: 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 C2AACC433EF for ; Fri, 24 Jun 2022 23:13:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231139AbiFXXNV (ORCPT ); Fri, 24 Jun 2022 19:13:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231723AbiFXXNT (ORCPT ); Fri, 24 Jun 2022 19:13:19 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67B7574E49; Fri, 24 Jun 2022 16:13:18 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id p14so3763206pfh.6; Fri, 24 Jun 2022 16:13:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=46ucjs6wKUKgiiGqtq8K2ec49qlzYcpw1UVL6CAVoBc=; b=HMW80cadZRW7ASnl1wG8H8XJt6AASQA5qr4+JUHj1SA3yYvuC4B/pKrx+hBypRpc49 pvUEqX4bFuDuQGUjr3ngm1wiAoON+/Hc5wINSxtxDgrZ9bH33hZ5/VpN65RGBhoGaDT+ ybMJz7Tak5mpc4F4JC+kNcoULKeEhsPdB73q6JUGRpAQzHfsCsDx5kKa2tCazsuANxJj wWHSzFsGSPY1sdSnxBCgk29r1ScltJmWcpBMD08JPQxV9gmNJ8xQbyJv9G+Z4zqXAVJF HJJdVGNBq6M880Mx3fV8zFr4BEEdlw6VLxrY20j+GXkaV08FK3Lmwr5irDbmJm2MsD9L jwMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=46ucjs6wKUKgiiGqtq8K2ec49qlzYcpw1UVL6CAVoBc=; b=eaMt3FRuLv/P+q+QsrLFVulqam7F9bDvy0c1RmoX0bUBcBVrFhPYwLWY4oNfxuUZCx Nic5lpQ5Bb2W18xpAzwAS4Ry8nKNWjeaHqpZj+6Ew7nCulrirlpmPpRxpo5Qx4dhjcun QVjRzzuPW6VTQZOmYl3WbUtOlyRHxouvs+z2/0Xhe4tVFTzpORIJqiSS/DyXyTKElCMw VjBPMeKAI/ZPiWN0msvcuNrj2POtEy53Rfg2aSwJW4jfA4T1pBx8LLYYhWrM1pk71VFG ccxq2osOgDzs6MO3T2FpYBj1kjjqhG+DVSSPBpKNT+fTEEPXOLnJ8Q5GsM4CDQkLd+Mw F7Tg== X-Gm-Message-State: AJIora/gQi00b3yIRIapiR3Rj82t6bt+PLXNRamtpPTrBkt1H7G1EdCR /Fw/lOd5dCcJS3P+Ja39fPQ= X-Google-Smtp-Source: AGRyM1vot/3/cqBz4ISehZyl8CPhUFQ3zIiY/9WXXYPmdF/F8kEOJFJtuJIVSrsNQOV1Nl/Uj+s1Iw== X-Received: by 2002:a05:6a00:2484:b0:51c:4ee0:b899 with SMTP id c4-20020a056a00248400b0051c4ee0b899mr1358635pfv.46.1656112397831; Fri, 24 Jun 2022 16:13:17 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:480:eeb0:3156:8fd:28f6]) by smtp.gmail.com with ESMTPSA id z19-20020aa78893000000b0050dc76281e0sm2242439pfe.186.2022.06.24.16.13.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jun 2022 16:13:17 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Milian Wolff , bpf@vger.kernel.org, Blake Jones Subject: [PATCH 2/6] perf offcpu: Accept allowed sample types only Date: Fri, 24 Jun 2022 16:13:09 -0700 Message-Id: <20220624231313.367909-3-namhyung@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220624231313.367909-1-namhyung@kernel.org> References: <20220624231313.367909-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org As offcpu-time event is synthesized at the end, it could not get the all the sample info. Define OFFCPU_SAMPLE_TYPES for allowed ones and mask out others in evsel__config() to prevent parse errors. Because perf sample parsing assumes a specific ordering with the sample types, setting unsupported one would make it fail to read data like perf record -d/--data. Fixes: edc41a1099c2 ("perf record: Enable off-cpu analysis with BPF") Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_off_cpu.c | 7 ++++++- tools/perf/util/evsel.c | 9 +++++++++ tools/perf/util/off_cpu.h | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index b73e84a02264..f289b7713598 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -265,6 +265,12 @@ int off_cpu_write(struct perf_session *session) sample_type = evsel->core.attr.sample_type; + if (sample_type & ~OFFCPU_SAMPLE_TYPES) { + pr_err("not supported sample type: %llx\n", + (unsigned long long)sample_type); + return -1; + } + if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) { if (evsel->core.id) sid = evsel->core.id[0]; @@ -319,7 +325,6 @@ int off_cpu_write(struct perf_session *session) } if (sample_type & PERF_SAMPLE_CGROUP) data.array[n++] = key.cgroup_id; - /* TODO: handle more sample types */ size = n * sizeof(u64); data.hdr.size = size; diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index ce499c5da8d7..094b0a9c0bc0 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -48,6 +48,7 @@ #include "util.h" #include "hashmap.h" #include "pmu-hybrid.h" +#include "off_cpu.h" #include "../perf-sys.h" #include "util/parse-branch-options.h" #include @@ -1102,6 +1103,11 @@ static void evsel__set_default_freq_period(struct record_opts *opts, } } +static bool evsel__is_offcpu_event(struct evsel *evsel) +{ + return evsel__is_bpf_output(evsel) && !strcmp(evsel->name, OFFCPU_EVENT); +} + /* * The enable_on_exec/disabled value strategy: * @@ -1366,6 +1372,9 @@ void evsel__config(struct evsel *evsel, struct record_opts *opts, */ if (evsel__is_dummy_event(evsel)) evsel__reset_sample_bit(evsel, BRANCH_STACK); + + if (evsel__is_offcpu_event(evsel)) + evsel->core.attr.sample_type &= OFFCPU_SAMPLE_TYPES; } int evsel__set_filter(struct evsel *evsel, const char *filter) diff --git a/tools/perf/util/off_cpu.h b/tools/perf/util/off_cpu.h index 548008f74d42..2dd67c60f211 100644 --- a/tools/perf/util/off_cpu.h +++ b/tools/perf/util/off_cpu.h @@ -1,6 +1,8 @@ #ifndef PERF_UTIL_OFF_CPU_H #define PERF_UTIL_OFF_CPU_H +#include + struct evlist; struct target; struct perf_session; @@ -8,6 +10,13 @@ struct record_opts; #define OFFCPU_EVENT "offcpu-time" +#define OFFCPU_SAMPLE_TYPES (PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | \ + PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ + PERF_SAMPLE_ID | PERF_SAMPLE_CPU | \ + PERF_SAMPLE_PERIOD | PERF_SAMPLE_CALLCHAIN | \ + PERF_SAMPLE_CGROUP) + + #ifdef HAVE_BPF_SKEL int off_cpu_prepare(struct evlist *evlist, struct target *target, struct record_opts *opts); From patchwork Fri Jun 24 23:13:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12895169 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: 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 14C4ECCA473 for ; Fri, 24 Jun 2022 23:13:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231862AbiFXXNW (ORCPT ); Fri, 24 Jun 2022 19:13:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231843AbiFXXNU (ORCPT ); Fri, 24 Jun 2022 19:13:20 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E70568C65; Fri, 24 Jun 2022 16:13:19 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id p5so4084632pjt.2; Fri, 24 Jun 2022 16:13:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KnJPmaWlDToBQM9U1zQwBdQVyYPeGOmIGquterwrDgQ=; b=NyHJ6KnqeEYbiRDXNNwB/Hl99JnyaaKg+UeEor8xdy8cxSXKYN5YBhBjeHJnwF3Oql es5pT1vWqm7Uo12Dm+k1RVxfr8g+Kdyvm1qKSKvMSodtYUMFthY3smGCzGTLVpPOeHMO 84Vj0crGq5REhD0bNeEmIMyLJSZQQF0G5DRClun6esMJPAdAj3ZZKgiPNEPgclRdrMHY 4x4E9eF+TFKlYimZ8TuB6PmtOBn3HPM2a0iqxAZ+A+w3pv0hFNLi7dyVV7FO/RxrSIKz tz8Ohlmq1JEJzwqOhO+7NUXDsByX2cJB089ZO1Zuo338fShojIWb+llpt72oHRdE5TeK 6t1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=KnJPmaWlDToBQM9U1zQwBdQVyYPeGOmIGquterwrDgQ=; b=CmNcVX3qaSCacGiBixEjkbqT0atGejlaWgH3eGeIuCTbkkbT3jaNLB9RJbpQR67prT mY2yCW5+OI7twTY7A4CX2lksg6GIc5+ngAp38/0wl2DrP0sTzYpzQR/5xd66gZxW67+A qzQo1yVbQSutok5r1JmAC+yy5Yj+a6yyYToQJb8sS3mtB8dkhpk8H/QJyE3J63X18wr7 2NRmoeh8gjEtXn4wg8bCCrLr1Od9wqHgSs9PlQLcPrSFb3C86ZxajQGLyNVtspOTWcBm u89nWPu7Ytpoo/rccDxI6LQdoGsN5axv07soPckAuSDDH+tJMf/lQSE8T4vcgdXOxVDc +zaA== X-Gm-Message-State: AJIora8C5rgPw7VEr0Tti8rG5McAHe7jiHvkkirdGZcbnVJcpOkgbAxy g+pY07H5M3DFknJb4CLoDGA= X-Google-Smtp-Source: AGRyM1vC1y8UQDdJI2oy2FLHKigAIUXDcRpHOl/PFjAWrSkCf/tXEt5wZ/xlwQ880SPuHRl/Lb7Bqg== X-Received: by 2002:a17:902:d586:b0:16a:24de:1964 with SMTP id k6-20020a170902d58600b0016a24de1964mr1519723plh.4.1656112399024; Fri, 24 Jun 2022 16:13:19 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:480:eeb0:3156:8fd:28f6]) by smtp.gmail.com with ESMTPSA id z19-20020aa78893000000b0050dc76281e0sm2242439pfe.186.2022.06.24.16.13.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jun 2022 16:13:18 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Milian Wolff , bpf@vger.kernel.org, Blake Jones Subject: [PATCH 3/6] perf offcpu: Check process id for the given workload Date: Fri, 24 Jun 2022 16:13:10 -0700 Message-Id: <20220624231313.367909-4-namhyung@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220624231313.367909-1-namhyung@kernel.org> References: <20220624231313.367909-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Current task filter checks task->pid which is different for each thread. But we want to profile all the threads in the process. So let's compare process id (or thread-group id: tgid) instead. Before: $ sudo perf record --off-cpu -- perf bench sched messaging -t $ sudo perf report --stat | grep -A1 offcpu offcpu-time stats: SAMPLE events: 2 After: $ sudo perf record --off-cpu -- perf bench sched messaging -t $ sudo perf report --stat | grep -A1 offcpu offcpu-time stats: SAMPLE events: 850 Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_off_cpu.c | 1 + tools/perf/util/bpf_skel/off_cpu.bpf.c | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index f289b7713598..7dbcb025da87 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -78,6 +78,7 @@ static void off_cpu_start(void *arg) u8 val = 1; skel->bss->has_task = 1; + skel->bss->uses_tgid = 1; fd = bpf_map__fd(skel->maps.task_filter); pid = perf_thread_map__pid(evlist->core.threads, 0); bpf_map_update_elem(fd, &pid, &val, BPF_ANY); diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_skel/off_cpu.bpf.c index cc6d7fd55118..143a8b7acf87 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -85,6 +85,7 @@ int enabled = 0; int has_cpu = 0; int has_task = 0; int has_cgroup = 0; +int uses_tgid = 0; const volatile bool has_prev_state = false; const volatile bool needs_cgroup = false; @@ -144,7 +145,12 @@ static inline int can_record(struct task_struct *t, int state) if (has_task) { __u8 *ok; - __u32 pid = t->pid; + __u32 pid; + + if (uses_tgid) + pid = t->tgid; + else + pid = t->pid; ok = bpf_map_lookup_elem(&task_filter, &pid); if (!ok) From patchwork Fri Jun 24 23:13:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12895170 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: 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 2DCE2CCA480 for ; Fri, 24 Jun 2022 23:13:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231875AbiFXXNX (ORCPT ); Fri, 24 Jun 2022 19:13:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231859AbiFXXNV (ORCPT ); Fri, 24 Jun 2022 19:13:21 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA0B77668E; Fri, 24 Jun 2022 16:13:20 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id c205so3758013pfc.7; Fri, 24 Jun 2022 16:13:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NkmjmN6FSfnJmt1stt/Bkpj+lZFeEf2ciR698Soh71w=; b=kcNAwSdGS5B3ObxhoRUCdgUdB4mir2HdpBJLtpiXUlHUBSBjpTC+3IC/UDbBuOnKCu yrJMn+I7cTuPil+3OhcFZDQOYICZVG61ck1C3a15nE5D/t0oZRooo0/4hyWG4yc/To6t TvqdOB0LUycVxXv7xbLfGeGudUIXQxrzSZL35s57N9EDD+EhxQyaLl5JxltR7zyiLpsi v+FYQIAwHAuQL4s+yk9ge4CFABEegBweVKPFL5AA0fm3N7MUoS6p2QLNXC19T4VmBLXh USifhSrNXdCcrCvzA9E7KxAuTQAXa2SZ0YHDe0sqXVHM6am3ySyPxC+CTG5UkKXZIMVJ 9+/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=NkmjmN6FSfnJmt1stt/Bkpj+lZFeEf2ciR698Soh71w=; b=rG9NxDh/rI+Z0439DsDCOYfa2D03z9blJePOVbXTUYiHTi6XFmUHn8rfyV6WIANp1c kHZ969N4ESZxzuTb6LlufXMX2XIfXsE8/KW5cq/d5o7NWu5n0e8haG/6C6v2crongoXK JyWgLGs8RCVqIfOGIZAoou5zJe/hWctH05N/fZWmel0IKIh4f868Xa7/BOiv0TeqyKvO xK8pjcL/D/6AK7a+Us6DmwOi9fgxdzUNv3ENR2bhdf5yPnxQV6dDFJfmuJPyhnvoD3zr g2+vn50kwZFSCursJUGebLTi5sO9vlSQ0TlfhdG3kQJ/SyNFWAgBPFpekOAPDC3rCzds 4AcA== X-Gm-Message-State: AJIora+ygj60/0qM9Na8m48yQwwKx3+FiTMRIRjPzV0YQ6i5i7r2pNnW BFkTt1jjWj365Y3pIqhLQzg= X-Google-Smtp-Source: AGRyM1sayYWpHceCnF7iUf4+huMUBB9FIaWEm+yuQFAXbepaGDm1CsdFqkkbXwisPPjJM3n9MtmhgA== X-Received: by 2002:a63:380f:0:b0:40c:67ae:91ad with SMTP id f15-20020a63380f000000b0040c67ae91admr1047766pga.544.1656112400193; Fri, 24 Jun 2022 16:13:20 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:480:eeb0:3156:8fd:28f6]) by smtp.gmail.com with ESMTPSA id z19-20020aa78893000000b0050dc76281e0sm2242439pfe.186.2022.06.24.16.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jun 2022 16:13:19 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Milian Wolff , bpf@vger.kernel.org, Blake Jones Subject: [PATCH 4/6] perf offcpu: Parse process id separately Date: Fri, 24 Jun 2022 16:13:11 -0700 Message-Id: <20220624231313.367909-5-namhyung@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220624231313.367909-1-namhyung@kernel.org> References: <20220624231313.367909-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The current target code uses thread id for tracking tasks because perf_events need to be opened for each task. But we can use tgid in BPF maps and check it easily. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_off_cpu.c | 45 +++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 7dbcb025da87..f7ee0c7a53f0 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -11,6 +11,7 @@ #include "util/cpumap.h" #include "util/thread_map.h" #include "util/cgroup.h" +#include "util/strlist.h" #include #include "bpf_skel/off_cpu.skel.h" @@ -125,6 +126,8 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, { int err, fd, i; int ncpus = 1, ntasks = 1, ncgrps = 1; + struct strlist *pid_slist = NULL; + struct str_node *pos; if (off_cpu_config(evlist) < 0) { pr_err("Failed to config off-cpu BPF event\n"); @@ -143,7 +146,26 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus); } - if (target__has_task(target)) { + if (target->pid) { + pid_slist = strlist__new(target->pid, NULL); + if (!pid_slist) { + pr_err("Failed to create a strlist for pid\n"); + return -1; + } + + ntasks = 0; + strlist__for_each_entry(pos, pid_slist) { + char *end_ptr; + int pid = strtol(pos->s, &end_ptr, 10); + + if (pid == INT_MIN || pid == INT_MAX || + (*end_ptr != '\0' && *end_ptr != ',')) + continue; + + ntasks++; + } + bpf_map__set_max_entries(skel->maps.task_filter, ntasks); + } else if (target__has_task(target)) { ntasks = perf_thread_map__nr(evlist->core.threads); bpf_map__set_max_entries(skel->maps.task_filter, ntasks); } @@ -185,7 +207,26 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, } } - if (target__has_task(target)) { + if (target->pid) { + u8 val = 1; + + skel->bss->has_task = 1; + skel->bss->uses_tgid = 1; + fd = bpf_map__fd(skel->maps.task_filter); + + strlist__for_each_entry(pos, pid_slist) { + char *end_ptr; + u32 tgid; + int pid = strtol(pos->s, &end_ptr, 10); + + if (pid == INT_MIN || pid == INT_MAX || + (*end_ptr != '\0' && *end_ptr != ',')) + continue; + + tgid = pid; + bpf_map_update_elem(fd, &tgid, &val, BPF_ANY); + } + } else if (target__has_task(target)) { u32 pid; u8 val = 1; From patchwork Fri Jun 24 23:13:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12895172 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: 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 35DF3C433EF for ; Fri, 24 Jun 2022 23:13:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231944AbiFXXNa (ORCPT ); Fri, 24 Jun 2022 19:13:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231910AbiFXXNX (ORCPT ); Fri, 24 Jun 2022 19:13:23 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEDAA69999; Fri, 24 Jun 2022 16:13:21 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id l2so2888048pjf.1; Fri, 24 Jun 2022 16:13:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dazC7TPyX4UMs7r+xqJJNBPVEDPVKb6BSLrk7hWQB1s=; b=Cn1BUdNqjGwnV6Ct77HE2u31hBD0aEauc1RcVcUw5C997bVzfHnJRVNb5EUxpwxjc2 VNKL5E4DFLMjXL/9HIk3XdOPxWKYye7gou97s10dttVQAJY4upglVa+BT6vtRpwLC/av clqYxzVK/XTUlpTIUUbCpUoI5l6tMQkKmwHRFthZqP4AXLGjz6uUsAFAlRxCroFM8g7Y dAUMVDVbAHiUyj9V7dL194TjW0anFxVzMHm8MADtqqcbfvpQTXAl6QFUnBq72by14Zih NQVa55oFDNe0niQ0AGprN+TvJy2K+5m47GtaIAb56iBenviyj2xp810VvSs8GjN8E/yl g3gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=dazC7TPyX4UMs7r+xqJJNBPVEDPVKb6BSLrk7hWQB1s=; b=jymjZS8qW+WksIDkz62N+Y7y1UbgTGCGFFnwjKv1CsPbPpxZ/T2GfTLr0dtAchlMyo 7C7+JK7pN1GP+ZpYec06uz/m3/MUTTJC9yPO6749IyB0rhv3KyvZt91KejRLfpIYAJnR iuinRuZLrtgogjY/VVemj+WWaobYd1BX8y9WYHl+FoZc+m5zxQj+VLun58owhoCdeKBM sHXTLR28C+GOZp5JW2L/V3T2+H9yOkwPH7A7K/GWTkJtSYDmmLOWrhbLs9zeum1moE8O KNRq+dV4xGBNByQ0jZLoEPcJ6BYx5Mxs896qjNcYSuyKuEdDwjldwbhY2L0e+C61L2aJ YnBw== X-Gm-Message-State: AJIora/1ORH1MF6fdB+QhoDjy0Zw63lsrgFw4EAT3PCmzEQveVMKZpPH MqHCIWT2IKo2b1fTFLmktEw= X-Google-Smtp-Source: AGRyM1vyBynoQjkTMnlXV2HF0vYM2fkcbLXBHJu+/G0mnw5G4tHCHEhOh8+zMQ7ldm8mPl8yXx8WMw== X-Received: by 2002:a17:903:2447:b0:16a:3b58:48dd with SMTP id l7-20020a170903244700b0016a3b5848ddmr1467413pls.120.1656112401325; Fri, 24 Jun 2022 16:13:21 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:480:eeb0:3156:8fd:28f6]) by smtp.gmail.com with ESMTPSA id z19-20020aa78893000000b0050dc76281e0sm2242439pfe.186.2022.06.24.16.13.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jun 2022 16:13:21 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Milian Wolff , bpf@vger.kernel.org, Blake Jones Subject: [PATCH 5/6] perf offcpu: Track child processes Date: Fri, 24 Jun 2022 16:13:12 -0700 Message-Id: <20220624231313.367909-6-namhyung@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220624231313.367909-1-namhyung@kernel.org> References: <20220624231313.367909-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org When -p option used or a workload is given, it needs to handle child processes. The perf_event can inherit those task events automatically. We can add a new BPF program in task_newtask tracepoint to track child processes. Before: $ sudo perf record --off-cpu -- perf bench sched messaging $ sudo perf report --stat | grep -A1 offcpu offcpu-time stats: SAMPLE events: 1 After: $ sudo perf record -a --off-cpu -- perf bench sched messaging $ sudo perf report --stat | grep -A1 offcpu offcpu-time stats: SAMPLE events: 856 Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_off_cpu.c | 7 ++++++ tools/perf/util/bpf_skel/off_cpu.bpf.c | 30 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index f7ee0c7a53f0..c257813e674e 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -17,6 +17,7 @@ #include "bpf_skel/off_cpu.skel.h" #define MAX_STACKS 32 +#define MAX_PROC 4096 /* we don't need actual timestamp, just want to put the samples at last */ #define OFF_CPU_TIMESTAMP (~0ull << 32) @@ -164,10 +165,16 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, ntasks++; } + + if (ntasks < MAX_PROC) + ntasks = MAX_PROC; + bpf_map__set_max_entries(skel->maps.task_filter, ntasks); } else if (target__has_task(target)) { ntasks = perf_thread_map__nr(evlist->core.threads); bpf_map__set_max_entries(skel->maps.task_filter, ntasks); + } else if (target__none(target)) { + bpf_map__set_max_entries(skel->maps.task_filter, MAX_PROC); } if (evlist__first(evlist)->cgrp) { diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_skel/off_cpu.bpf.c index 143a8b7acf87..c4ba2bcf179f 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -12,6 +12,9 @@ #define TASK_INTERRUPTIBLE 0x0001 #define TASK_UNINTERRUPTIBLE 0x0002 +/* create a new thread */ +#define CLONE_THREAD 0x10000 + #define MAX_STACKS 32 #define MAX_ENTRIES 102400 @@ -220,6 +223,33 @@ static int off_cpu_stat(u64 *ctx, struct task_struct *prev, return 0; } +SEC("tp_btf/task_newtask") +int on_newtask(u64 *ctx) +{ + struct task_struct *task; + u64 clone_flags; + u32 pid; + u8 val = 1; + + if (!uses_tgid) + return 0; + + task = (struct task_struct *)bpf_get_current_task(); + + pid = BPF_CORE_READ(task, tgid); + if (!bpf_map_lookup_elem(&task_filter, &pid)) + return 0; + + task = (struct task_struct *)ctx[0]; + clone_flags = ctx[1]; + + pid = task->tgid; + if (!(clone_flags & CLONE_THREAD)) + bpf_map_update_elem(&task_filter, &pid, &val, BPF_NOEXIST); + + return 0; +} + SEC("tp_btf/sched_switch") int on_switch(u64 *ctx) { From patchwork Fri Jun 24 23:13:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12895171 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: 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 A9678CCA47F for ; Fri, 24 Jun 2022 23:13:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231948AbiFXXNa (ORCPT ); Fri, 24 Jun 2022 19:13:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231448AbiFXXNY (ORCPT ); Fri, 24 Jun 2022 19:13:24 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 221CA828BE; Fri, 24 Jun 2022 16:13:23 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id 73-20020a17090a0fcf00b001eaee69f600so4152746pjz.1; Fri, 24 Jun 2022 16:13:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e0OScNlE18HyHUOTZpmKwBxiKtZtKgQ8l+wOWm74zcc=; b=C7kKu5Nihc7Y5fIzZY6y7afxmyewZAwzewVdotTbDM0udy0OWhZ57N7cqWE9Cel1WP kGjQ5tbZzXFE9hSGrN3Ct1F/Vxmyr23Hug/rM57FIm8kpFFUTKOyQYGQbiloFPjv8hm5 EBj8HWvkEUCs1vNCo398TLcz6PfV4KKiDXrbPQBh9MXsUfEdiOenmRDd48Mh5YXAnHV9 CcmPTq25lRT2p34jbZ8Mjbgs9bO1aNzLNOWDrFZlN6u7FxLurf+WOdoHlPDc7ZxBwZ+4 7pIjssp4pV6yhL0jUbT23OWUnaTVz2suc/Ng4noG9KMFy+lNEuywU287viUm1TzyIF5p KovA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=e0OScNlE18HyHUOTZpmKwBxiKtZtKgQ8l+wOWm74zcc=; b=yKv/2TOLR032rPuifC7jbeWwHVUfBHg6Hg1e68kxxeKWNZ2Z8VEzmgft+5/Rv2U8bd /Zk2OCMa66clKoJajLzAezsRlFsUK3PcGDuUsdm4fMmaklfNKr/OYwKdDjT8aXKRbo0A 0/qx9ktd0bHucvogWaaRwZNKzpRgy0zdW642/2w1pefiEILfBHjhepX6UA/cvrMGoDpY S/dT0+GTKSt/xXKjL/+0vpXXIbe0yVjSNg+2rwovus9ru3sAgsHpFgHQXiitINDbZ7p0 u1/IKEUnRZKQWWOQBfMYscTajJME9JabNW2jq1mbCe1Q0bwp3Rvz7UZIGyDpMrn/VOCh CKGg== X-Gm-Message-State: AJIora/L33t+/iobGIIFuLBrm0Ca60QS/+1/Y6hFWdGlzDzpEcWr7MtF +hW769dAyAMFFVbYzquNqsA= X-Google-Smtp-Source: AGRyM1uoqydzUkflGUJH5vKJEZWZz6YJEGSE7lzVV7BDhDU1LHEzSotNCs+wskxUP+NX2fMmauaXjw== X-Received: by 2002:a17:902:744c:b0:16a:1850:d055 with SMTP id e12-20020a170902744c00b0016a1850d055mr1434349plt.96.1656112402575; Fri, 24 Jun 2022 16:13:22 -0700 (PDT) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:480:eeb0:3156:8fd:28f6]) by smtp.gmail.com with ESMTPSA id z19-20020aa78893000000b0050dc76281e0sm2242439pfe.186.2022.06.24.16.13.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jun 2022 16:13:22 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Milian Wolff , bpf@vger.kernel.org, Blake Jones Subject: [PATCH 6/6] perf offcpu: Update offcpu test for child process Date: Fri, 24 Jun 2022 16:13:13 -0700 Message-Id: <20220624231313.367909-7-namhyung@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220624231313.367909-1-namhyung@kernel.org> References: <20220624231313.367909-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Record off-cpu data with perf bench sched messaging workload and count the number of offcpu-time events. Also update the test script not to run next tests if failed already and revise the error messages. $ sudo ./perf test offcpu -v 88: perf record offcpu profiling tests : --- start --- test child forked, pid 344780 Checking off-cpu privilege Basic off-cpu test Basic off-cpu test [Success] Child task off-cpu test Child task off-cpu test [Success] test child finished with 0 ---- end ---- perf record offcpu profiling tests: Ok Signed-off-by: Namhyung Kim --- tools/perf/tests/shell/record_offcpu.sh | 57 ++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/tools/perf/tests/shell/record_offcpu.sh b/tools/perf/tests/shell/record_offcpu.sh index 96e0739f7478..611b2d7c779b 100755 --- a/tools/perf/tests/shell/record_offcpu.sh +++ b/tools/perf/tests/shell/record_offcpu.sh @@ -19,20 +19,26 @@ trap_cleanup() { } trap trap_cleanup exit term int -test_offcpu() { - echo "Basic off-cpu test" +test_offcpu_priv() { + echo "Checking off-cpu privilege" + if [ `id -u` != 0 ] then - echo "Basic off-cpu test [Skipped permission]" + echo "off-cpu test [Skipped permission]" err=2 return fi - if perf record --off-cpu -o ${perfdata} --quiet true 2>&1 | grep BUILD_BPF_SKEL + if perf record --off-cpu -o /dev/null --quiet true 2>&1 | grep BUILD_BPF_SKEL then - echo "Basic off-cpu test [Skipped missing BPF support]" + echo "off-cpu test [Skipped missing BPF support]" err=2 return fi +} + +test_offcpu_basic() { + echo "Basic off-cpu test" + if ! perf record --off-cpu -e dummy -o ${perfdata} sleep 1 2> /dev/null then echo "Basic off-cpu test [Failed record]" @@ -41,7 +47,7 @@ test_offcpu() { fi if ! perf evlist -i ${perfdata} | grep -q "offcpu-time" then - echo "Basic off-cpu test [Failed record]" + echo "Basic off-cpu test [Failed no event]" err=1 return fi @@ -54,7 +60,44 @@ test_offcpu() { echo "Basic off-cpu test [Success]" } -test_offcpu +test_offcpu_child() { + echo "Child task off-cpu test" + + # perf bench sched messaging creates 400 processes + if ! perf record --off-cpu -e dummy -o ${perfdata} -- \ + perf bench sched messaging -g 10 > /dev/null 2&>1 + then + echo "Child task off-cpu test [Failed record]" + err=1 + return + fi + if ! perf evlist -i ${perfdata} | grep -q "offcpu-time" + then + echo "Child task off-cpu test [Failed no event]" + err=1 + return + fi + # each process waits for read and write, so it should be more than 800 events + if ! perf report -i ${perfdata} -s comm -q -n -t ';' | \ + awk -F ";" '{ if (NF > 3 && int($3) < 800) exit 1; }' + then + echo "Child task off-cpu test [Failed invalid output]" + err=1 + return + fi + echo "Child task off-cpu test [Success]" +} + + +test_offcpu_priv + +if [ $err = 0 ]; then + test_offcpu_basic +fi + +if [ $err = 0 ]; then + test_offcpu_child +fi cleanup exit $err