From patchwork Thu Jan 26 21:36:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 48912 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp505163wrn; Thu, 26 Jan 2023 13:53:26 -0800 (PST) X-Google-Smtp-Source: AK7set9+WtqVucaLPe2WHmO7b0S3zPPtNGDAbcNfBQOGrHH2KHtO/BodZbph8GSKwkkH4hkIbF7m X-Received: by 2002:a17:902:ea0d:b0:196:266d:6a82 with SMTP id s13-20020a170902ea0d00b00196266d6a82mr10546056plg.2.1674770006548; Thu, 26 Jan 2023 13:53:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674770006; cv=none; d=google.com; s=arc-20160816; b=NZPm3lUfG3/1X6J0yu5OGSoKz2Y85BtiGo4wOt6VGvzR9yxzkmwvUDPkUJkQ/F7uJA uhzDPBPYDOGlM8dl/DInQM3f+QVUtXa+HfYi7otSjBIkB9KD8+5F3uVhgjVeFztmJwEb kpmbIeOsHPayxoYXskGQoN55tukm0Vfn+OIOC3zguwWEk44RftThAyjDzdVIrB4qiD5l f9CoeDpyAvMsH2aCXhT56kDqlTQZMLZ7n4Pm51kqU9MvqjnFIIH89eifMkXv3DSZCfG+ 69AbbOaXfEUpsaKMXB87CuQWZFmXPE1IBFcKyAVt6PiHSdelsdAOlkalfgOiWkRoc6gZ KiCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=jUJI1fdtuav4Cmxfz+SpScnfmvuJBA2rMIfNnsJ9wTg=; b=xLu16hs7hsUjFR5o9D6bZtZFFJ+B17eDXF11H4f3pepMzEd0jd+vgnvAFuBq4iRqXW HY07S8lfl5zTsR9o8AcmGIFs2bYosDzMGfuBmHBnyTMu7nyaC+vJDqOgcO4l0G83Hi8g t9XuzsiZgydxWO+Mb6812BeJfT38IlYJZpv2ClBwysQER87aCpk7eogXQpdAQDa6Qf6v SsR3AP4eZibJ9cnAf9WOgSnYb/jQJCTIrc3F8Bq92DKbI6cyOt1y+1UlilH992FauvMR c11PQ8mhostHKOm3ZNc/qvieWK52T8PkxvKHIzxBpQ+9ndI6DrKvvdr1s84LlNprW5vv 4WSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=faNLmwoG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s16-20020a170902b19000b001948720f6b8si2592621plr.66.2023.01.26.13.53.14; Thu, 26 Jan 2023 13:53:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=faNLmwoG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233060AbjAZVg3 (ORCPT + 99 others); Thu, 26 Jan 2023 16:36:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231627AbjAZVgV (ORCPT ); Thu, 26 Jan 2023 16:36:21 -0500 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 371FA74A7F; Thu, 26 Jan 2023 13:36:14 -0800 (PST) Received: by mail-pl1-x62d.google.com with SMTP id be8so3076608plb.7; Thu, 26 Jan 2023 13:36:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=jUJI1fdtuav4Cmxfz+SpScnfmvuJBA2rMIfNnsJ9wTg=; b=faNLmwoGlghGB/uwCA7ZXvD7dxUT4E53sDMJuugItaNjKqfGxziBpusCjGhdu5vo/V k4FLy+OSddKvuhjIIMRzbtZ9CWH54g5HPQ4ezpK4XrBBRKzmkwru2PDOD1wgc0dLdtO+ QUUnkuJzYYcTZKhiY2826e+lKZbyamSK+lfD2/bMr9bjtq/lc6D29SItbrhTTt4iyDti DLLeMq2SwYV8PkzVC0HLLPUGPqMh3u5a/8UdYWwQYCEwx8el9j06USj8ldFStl7Ynz9z 448mm16GWdlZRGueL2umT5EpQg8qfPyibzy/gSTLUs9AywF2Pyak9M641k1WWloxS7hq YuyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=jUJI1fdtuav4Cmxfz+SpScnfmvuJBA2rMIfNnsJ9wTg=; b=YFvB5ddl/9AqIZXTg+ilsMWN9CHrmS2GQJGh4RDdIvvy+F4/o0K88+3IgEaSEjZbrT x/Esdd7mb9e4pOhundnaLQ5UsNJ507Bw9qAHjdcrXhWIzkNPBWkriDrB1z4m7wDubS8G x84wfXpyg1HhfCOskwIFNRQQY0Wef2Hu4FKvFT0eoJDkGbY2ir6NDcM1MBkARRojqIPO gbeWBQC4RsIwloJYuIJQ/C45q7FjKKRQpfnsWz1phlxvoyNSW9tueQdezjm9JpmG/1Ul MqftAaBi9BZ241JtDsa5LW8XF1RFPRIX7pWxUrCROMJ/kJEJTke7CedkBuqFqwHc4eyW g7Nw== X-Gm-Message-State: AO0yUKUgB1ncVvCTUOKoS1RXgrIHZMLBAAtbXueVmUjapaniebBDc2Kl CxJi7hOZ8As0XiUB5qSdyFA= X-Received: by 2002:a17:902:d4c3:b0:196:349f:4df3 with SMTP id o3-20020a170902d4c300b00196349f4df3mr7211943plg.50.1674768973506; Thu, 26 Jan 2023 13:36:13 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:1f5d:eee8:d409:8a17]) by smtp.gmail.com with ESMTPSA id b13-20020a170902d50d00b00172fad607b3sm1408075plg.207.2023.01.26.13.36.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 13:36:12 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, Stephane Eranian Subject: [PATCH v2] perf script: Add 'cgroup' field for output Date: Thu, 26 Jan 2023 13:36:10 -0800 Message-Id: <20230126213610.3381147-1-namhyung@kernel.org> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog MIME-Version: 1.0 X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756116474617734279?= X-GMAIL-MSGID: =?utf-8?q?1756123634762480787?= There's no field for the cgroup, let's add one. To do that, users need to specify --all-cgroup option for perf record to capture the cgroup info. $ perf record --all-cgroups -- true $ perf script -F comm,pid,cgroup true 337112 /user.slice/user-657345.slice/user@657345.service/... true 337112 /user.slice/user-657345.slice/user@657345.service/... true 337112 /user.slice/user-657345.slice/user@657345.service/... true 337112 /user.slice/user-657345.slice/user@657345.service/... If it's recorded without the --all-cgroups, it'd complain. $ perf script -F comm,pid,cgroup Samples for 'cycles:u' event do not have CGROUP attribute set. Cannot print 'cgroup' field. Hint: run 'perf record --all-cgroups ...' Signed-off-by: Namhyung Kim --- tools/perf/Documentation/perf-script.txt | 5 ++++- tools/perf/builtin-script.c | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/tools/perf/Documentation/perf-script.txt b/tools/perf/Documentation/perf-script.txt index 8d77182fbf31..a2ebadc9d948 100644 --- a/tools/perf/Documentation/perf-script.txt +++ b/tools/perf/Documentation/perf-script.txt @@ -134,7 +134,7 @@ OPTIONS srcline, period, iregs, uregs, brstack, brstacksym, flags, bpf-output, brstackinsn, brstackinsnlen, brstackoff, callindent, insn, insnlen, synth, phys_addr, metric, misc, srccode, ipc, data_page_size, code_page_size, ins_lat, - machine_pid, vcpu. + machine_pid, vcpu, cgroup. Field list can be prepended with the type, trace, sw or hw, to indicate to which event type the field list applies. e.g., -F sw:comm,tid,time,ip,sym and -F trace:time,cpu,trace @@ -231,6 +231,9 @@ OPTIONS perf inject to insert a perf.data file recorded inside a virtual machine into a perf.data file recorded on the host at the same time. + The cgroup fields requires sample having the cgroup id which is saved + when "--all-cgroups" option is passed to 'perf record'. + Finally, a user may not set fields to none for all event types. i.e., -F "" is not allowed. diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 69394ac0a20d..731821fd0d3e 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -59,6 +59,7 @@ #include "util/dlfilter.h" #include "util/record.h" #include "util/util.h" +#include "util/cgroup.h" #include "perf.h" #include @@ -130,6 +131,7 @@ enum perf_output_field { PERF_OUTPUT_BRSTACKINSNLEN = 1ULL << 36, PERF_OUTPUT_MACHINE_PID = 1ULL << 37, PERF_OUTPUT_VCPU = 1ULL << 38, + PERF_OUTPUT_CGROUP = 1ULL << 39, }; struct perf_script { @@ -200,6 +202,7 @@ struct output_option { {.str = "brstackinsnlen", .field = PERF_OUTPUT_BRSTACKINSNLEN}, {.str = "machine_pid", .field = PERF_OUTPUT_MACHINE_PID}, {.str = "vcpu", .field = PERF_OUTPUT_VCPU}, + {.str = "cgroup", .field = PERF_OUTPUT_CGROUP}, }; enum { @@ -542,6 +545,12 @@ static int evsel__check_attr(struct evsel *evsel, struct perf_session *session) evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT_STRUCT, "WEIGHT_STRUCT", PERF_OUTPUT_INS_LAT)) return -EINVAL; + if (PRINT_FIELD(CGROUP) && + evsel__check_stype(evsel, PERF_SAMPLE_CGROUP, "CGROUP", PERF_OUTPUT_CGROUP)) { + pr_err("Hint: run 'perf record --all-cgroups ...'\n"); + return -EINVAL; + } + return 0; } @@ -2220,6 +2229,17 @@ static void process_event(struct perf_script *script, if (PRINT_FIELD(CODE_PAGE_SIZE)) fprintf(fp, " %s", get_page_size_name(sample->code_page_size, str)); + if (PRINT_FIELD(CGROUP)) { + const char *cgrp_name; + struct cgroup *cgrp = cgroup__find(machine->env, + sample->cgroup); + if (cgrp != NULL) + cgrp_name = cgrp->name; + else + cgrp_name = "unknown"; + fprintf(fp, " %s", cgrp_name); + } + perf_sample__fprintf_ipc(sample, attr, fp); fprintf(fp, "\n"); @@ -3856,7 +3876,7 @@ int cmd_script(int argc, const char **argv) "brstacksym,flags,data_src,weight,bpf-output,brstackinsn," "brstackinsnlen,brstackoff,callindent,insn,insnlen,synth," "phys_addr,metric,misc,srccode,ipc,tod,data_page_size," - "code_page_size,ins_lat", + "code_page_size,ins_lat,machine_pid,vcpu,cgroup", parse_output_fields), OPT_BOOLEAN('a', "all-cpus", &system_wide, "system-wide collection from all CPUs"),