From patchwork Thu Aug 11 18:54:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12941693 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 83DCCC25B0D for ; Thu, 11 Aug 2022 18:55:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236154AbiHKSzF (ORCPT ); Thu, 11 Aug 2022 14:55:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236123AbiHKSzD (ORCPT ); Thu, 11 Aug 2022 14:55:03 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C68DF9E2E5; Thu, 11 Aug 2022 11:55:02 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id r69so11319691pgr.2; Thu, 11 Aug 2022 11:55:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc; bh=WtJpt6VjUpmNbd0Og7BJIOmYxw2DFH3snhDEN9l99a0=; b=TKvT2eZoIF4Er8ODkh9Scdq5A7tQtynpOVTBi0Yfc/eP0ohZ9E+jTc1rMZzqXgp0ku CgD6Z1ZO3IeHH1l9rsN077LAV/GVipoxncuWpqJaOzQoFhCxKcblvIa66mam3riG4jpn qBw2xSPd+1G/6cGrPb2/Cpnr/PCcDNDy+s1oE72ooyeA0tGjGO0DHJt6pa1y+qQGZhmk ugiDvX9bEJL5lwWEGKCFLhgb8+luyRPIlWW6ZoDngSM0H7XaXXTroq2MAqcELleVPNbG p3jK/bM1/nVuhh9D7bTIvqeqe5yb185088cRlCXF37HUytrBLB8UfKHejduMtWQ95oYL zotA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc; bh=WtJpt6VjUpmNbd0Og7BJIOmYxw2DFH3snhDEN9l99a0=; b=ZQ9G7abceKPbKMyFtMhttEQQVZDrzNrSX8p5aDktvU14B74d4HHNTAWIE/rOQygPEB O8U9Rl0eg9hoGiL5IYjeNWFEAJnmDKV9kMHGVETW+mYifye0jiVdQe2XVFJ2qF8w78NH 4TuY2X+1A40H1F10T8pYlECYPYdcKSOygAp9jLMJMAseKxA26xFz9UPzW0/yVb2ki1in EZKu0CFkGeRd2fab7EQDM6RkouIvp6CcVn6l/Ws+pa0M5Izf54w92TQ8Qk7EidYYxt+M IpKmP8zBWZtr3qSfALN6kUgZWOpGJXCXUIgsfrwvKImdyoT2cxq9Nb9FT6Ps/aBjts3n O5gQ== X-Gm-Message-State: ACgBeo2c2X+SLgsrs7/VN5vM3vbI3d7YU2Evji1jdyq6jLHh0eG741YJ kqZfS6Xm/vOqfOOU1zFYBh8= X-Google-Smtp-Source: AA6agR7+6aoUVKsVdYjJMX77vmkDYuDzq2vWas4Rjt3TB6S1REeANJI2K+AMGJHrCStnpj9MJ5bW1w== X-Received: by 2002:a63:4948:0:b0:41c:d9ed:72d7 with SMTP id y8-20020a634948000000b0041cd9ed72d7mr324892pgk.246.1660244102266; Thu, 11 Aug 2022 11:55:02 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:cefb:475d:dd6d:a1e2]) by smtp.gmail.com with ESMTPSA id r12-20020a6560cc000000b0041975999455sm66314pgv.75.2022.08.11.11.55.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Aug 2022 11:55:01 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Peter Zijlstra , Ingo Molnar , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Blake Jones , Milian Wolff , bpf@vger.kernel.org Subject: [PATCH 1/4] perf offcpu: Check process id for the given workload Date: Thu, 11 Aug 2022 11:54:53 -0700 Message-Id: <20220811185456.194721-2-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog In-Reply-To: <20220811185456.194721-1-namhyung@kernel.org> References: <20220811185456.194721-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 Thu Aug 11 18:54:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12941695 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 B777DC25B06 for ; Thu, 11 Aug 2022 18:55:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236169AbiHKSzG (ORCPT ); Thu, 11 Aug 2022 14:55:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236148AbiHKSzF (ORCPT ); Thu, 11 Aug 2022 14:55:05 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 912B09E2E7; Thu, 11 Aug 2022 11:55:04 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id y141so17227600pfb.7; Thu, 11 Aug 2022 11:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc; bh=2cod6Z7LibdeMn8uoQb1Rhaw5oJrEcLjiUxCrov59Is=; b=fJprRh7mUoC6596up0r7/10cQGnTTsJaaZ00ABFvuZMY2ee0sBzztjKBHcG6AZ5kLn SOupl0lSWwR0G0e2SiJ4hvS9/1CAdLNd5dGShuV0b4W7bjfuh6jo6K9DiHFFZIMBzr4f QO5/HrsTUq5bEkv+qbm+DmEwOodt3X1SrAaVswbDiK3ehKhzaqZwJB97DlUFaF7Y3b4g aqJh6FPRtquWg+Lp0U8fbK+o1c7914o8jOrR93pKkl4wjn8ArpUL3cDNkvjjhno3c7g/ DAfEbMVQtLHuseBakimcbYt6wN7tCTjznq899p9O+3ikpfcDAmBZa7EbNntvSrYukwS5 CngQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc; bh=2cod6Z7LibdeMn8uoQb1Rhaw5oJrEcLjiUxCrov59Is=; b=L4PgvUdBqzWq8wx79cjwHcvcBF+z9cp6q93IS4AAbkkGg13KKhwQ2ef4gVxt2PUrj0 bUErPl8YfgraBXXHLSsCBAFMRTfkpGcdQkX/PU70DgNlr+iPd0ZiLIc4hCDVltTHHlx3 VZ/Oo7ThTWPWKRjfnX5qRsEaOQ5Jc20zu7MB+FlqBRzwS7hiDH/KCX6qZ/Leeyr0V+VV YOLY2i1++k4HySgfNz66023eRcSzaAo25lqt+oGvCzVlaxO1cufqogbfjJ04eBxDM5Z3 D8B7V5YrMTNZsS+FYQX6nsiVVXdYkYQwRiZ+siv6/a2Cm20zBlH6eItyJXnt15l+06zC nzpA== X-Gm-Message-State: ACgBeo0yXeucE0+2fnvRbuaZtphJZP8Wn8t5HmlPhOYwlFCtNgFnEDpW FbJaYFwssJuIX6UIe8ygtdo= X-Google-Smtp-Source: AA6agR6PhZYOveICAttaIRAzc9X4mUErwc/Eq67S/Mco+gQ0auKvrydAhb25F1duRW92L5pBM+CF5A== X-Received: by 2002:a63:224a:0:b0:41e:1d36:5063 with SMTP id t10-20020a63224a000000b0041e1d365063mr306352pgm.568.1660244104049; Thu, 11 Aug 2022 11:55:04 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:cefb:475d:dd6d:a1e2]) by smtp.gmail.com with ESMTPSA id r12-20020a6560cc000000b0041975999455sm66314pgv.75.2022.08.11.11.55.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Aug 2022 11:55:03 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Peter Zijlstra , Ingo Molnar , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Blake Jones , Milian Wolff , bpf@vger.kernel.org Subject: [PATCH 2/4] perf offcpu: Parse process id separately Date: Thu, 11 Aug 2022 11:54:54 -0700 Message-Id: <20220811185456.194721-3-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog In-Reply-To: <20220811185456.194721-1-namhyung@kernel.org> References: <20220811185456.194721-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 Thu Aug 11 18:54:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12941696 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 42133C19F2A for ; Thu, 11 Aug 2022 18:55:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236179AbiHKSzJ (ORCPT ); Thu, 11 Aug 2022 14:55:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236110AbiHKSzH (ORCPT ); Thu, 11 Aug 2022 14:55:07 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9421B9E2E7; Thu, 11 Aug 2022 11:55:06 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id q19so17233948pfg.8; Thu, 11 Aug 2022 11:55:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc; bh=lCwq5ptfVT6LA/OJ3S7YeqvIhPQ7ZVzO5sJK2ii/XJA=; b=PuQje8v+mJN+T9RJtHJg/4IQ6kE3CnrzpBouN5NWQjC1Rkxwdtyp/to31A4iovW4Zx PXmk1IBFBXidjjRwPUmJsbzS4ITqDd6Ennt96eyM4xXvkzNMSz1yNKN0xYNZfZIIlKVU YqcK9ljrRJ0xn4vEiSqxeo4shdpYEtOrS1VgjZ97JBquwqlRjuxN1ntMsJyVIcUpQ9bx zhxMdUbLjrGMJtnp8CO8l5qTyzcs90YF233w6TfVBs377aXQ1VQuyccCKFG8ME/fzjE1 GeqFj3lKggIMbD98X/vbvP7Xe0JLBT90RKjCh9bMznwqk+hd/4pCq0S+3Fsk4JjTKgEj xh/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc; bh=lCwq5ptfVT6LA/OJ3S7YeqvIhPQ7ZVzO5sJK2ii/XJA=; b=VAHy5MCdbzodV4rCflaJKdKjSbgWDOTdoij/z8jc0U5nFjNB6QKNDoRFwUHGLwBajb cyTm32/V5gCoGaOyIrvVB/RN0OZwx2TRTMxaN29qJ314yoiGYo8aUjdj7LiIfUiT+rxZ 4tlyURsLD4eOBST3tILSeJdNypfNO3Wp30pV9ppn34uxezli7lQnILTH9pyg3nNCidk7 8F3zTkMgIY6bJPJo3avYMX28mOsifa31diWR3Ul8ek0vvaGtocprQZ2RAfos/cOeJqPt fS9pSFkrMcYOZbovSdo09z2vVCUnf4s10dM3tItfrZjSFQiKTN0gMLtuJe9HjF5vDJBX +iSA== X-Gm-Message-State: ACgBeo1MnTw0BuvQXmIwBXwptmI3LKWtORfOrbdtme7h6rhimi8b1vxu bOf7yEEwoiNfLF1kJ6ZL554= X-Google-Smtp-Source: AA6agR6CzMsUihjP+aqaSObhyzOI/xQIfPZO3KdsCjV3xCSULFBtq6xYSXZnZojXiCGKh/k1adIfNg== X-Received: by 2002:a63:8bc8:0:b0:41c:6800:d4e9 with SMTP id j191-20020a638bc8000000b0041c6800d4e9mr300643pge.45.1660244105953; Thu, 11 Aug 2022 11:55:05 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:cefb:475d:dd6d:a1e2]) by smtp.gmail.com with ESMTPSA id r12-20020a6560cc000000b0041975999455sm66314pgv.75.2022.08.11.11.55.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Aug 2022 11:55:05 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Peter Zijlstra , Ingo Molnar , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Blake Jones , Milian Wolff , bpf@vger.kernel.org Subject: [PATCH 3/4] perf offcpu: Track child processes Date: Thu, 11 Aug 2022 11:54:55 -0700 Message-Id: <20220811185456.194721-4-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog In-Reply-To: <20220811185456.194721-1-namhyung@kernel.org> References: <20220811185456.194721-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 Thu Aug 11 18:54:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12941697 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 727BBC19F2A for ; Thu, 11 Aug 2022 18:55:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236217AbiHKSzO (ORCPT ); Thu, 11 Aug 2022 14:55:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236204AbiHKSzJ (ORCPT ); Thu, 11 Aug 2022 14:55:09 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C4459F0DF; Thu, 11 Aug 2022 11:55:09 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id y1so12059669plb.2; Thu, 11 Aug 2022 11:55:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc; bh=A5A/jLi0WcII7JTh+ce1YUU4pX6WGLPaKY+QG9OJx+Y=; b=hSSvWCNT0mhtq5O8MW2RmbvXGefSAXmpNlzwEDir3msLd9VqFSyHBJnZfsZyzyA8TP oI+IIHJ5eIk5JofkBZuHDXKypYnccBMkYgJDAZqAGCxueTUp4InMZr2TKRzunMuu+TIW RnwKy42VNe3x1Y+AjS+0oPHfMFSKRlI/wOzf4/NkRehJTVPyw0vuk14J5vFqsHJ5ygvA WDc+6/TT+nOXvbtGJ/+6KSYkyjSUlE2/B+HuoyfU9h+plgocawo1YprSOxrZO5HmYNfh z+ccZFeQkqnKMdifvu+eGPgQKrYslJi5cWUWKYsC8+FhDZIuvuT0Qql1aa1H6moUnLdL MijA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc; bh=A5A/jLi0WcII7JTh+ce1YUU4pX6WGLPaKY+QG9OJx+Y=; b=3Y5GCm3vMeOxN0EFQzl3SbH5TZHURZ75iR8/78+mVx4GVjsTCPBNn83oAp0aW6MNBl UyQKF/wm7hkm1iwcX39AcV9iDtoJD5C9D6T1n5fltOoR8N7c0QqSW6M2OV8xByYU3pSM 5IAv9esnm2SNhrdUibHaIHpiNY0Yihqns5iLlm5wfaoN+jXd/eNQAhL5jU5nydd10w7+ uqZLLpc4CbJ7pc6VAmcidNwxu3K55mgCfYFhRh/FDWLp9asA1lfasZR76htTQjzNGJ1W HPi6OY9lXGD5iqq+IR1C8I2KqwhoyWcG/NsbVsKGzqxx8ojrZJ8CfejlUok6eCtWKyTP 3dIw== X-Gm-Message-State: ACgBeo0qi+63sWnOetGv4G3MV9sa6yr7VcktxRaWXf7hWa6pbPidjBcn GqZAsyGhEZBVGd85caoWB6M= X-Google-Smtp-Source: AA6agR4i2jONflU/i+NZC+0uurdYePRiA7PI9bX3pNhfGJJCx1FdcYBFLY8rq3Qn/qIm5DAsOQJPeA== X-Received: by 2002:a17:902:e84d:b0:16d:c9a0:e502 with SMTP id t13-20020a170902e84d00b0016dc9a0e502mr529747plg.125.1660244108465; Thu, 11 Aug 2022 11:55:08 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:cefb:475d:dd6d:a1e2]) by smtp.gmail.com with ESMTPSA id r12-20020a6560cc000000b0041975999455sm66314pgv.75.2022.08.11.11.55.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Aug 2022 11:55:07 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Peter Zijlstra , Ingo Molnar , LKML , Ian Rogers , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , Blake Jones , Milian Wolff , bpf@vger.kernel.org Subject: [PATCH 4/4] perf offcpu: Update offcpu test for child process Date: Thu, 11 Aug 2022 11:54:56 -0700 Message-Id: <20220811185456.194721-5-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog In-Reply-To: <20220811185456.194721-1-namhyung@kernel.org> References: <20220811185456.194721-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..d2eba583a2ac 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 ';' --percent-limit=90 | \ + 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