Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6055661ybl; Sun, 22 Dec 2019 22:09:54 -0800 (PST) X-Google-Smtp-Source: APXvYqwJ18fUg75OGPu4bN4DlK7/7iTpS0p3IO66LYsNrBi47W8wWNauVc4lnguHuHmR2pc8Sj0G X-Received: by 2002:a05:6830:18e3:: with SMTP id d3mr25004623otf.190.1577081394863; Sun, 22 Dec 2019 22:09:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577081394; cv=none; d=google.com; s=arc-20160816; b=VIwTt8u5qhsdx1WcKgcDodLoJ0LaFsCMxeKMSryDIhTmcJ513bW5bbdzrOKFVQgOC2 kKvhScf2HoIdT/xIwNNG+hbB1zX9McE8Jz7+Z6EScBjkuJQaXHj0hGNz8jiwL0VFgXgV 9YKJpFOSBWSp36TW3+Can7k52Yhu5h7hLOQ3CVSa6MG28ZAMR+ZCwhRBEluRD3wNd5t0 VJW40gbG4u/DKsy5fUqy8r0T+vbinL9za8OYtayZ0nJEMM3XyarCQRbnwKnishFn/NcT FouqgZ8UTgYAm2itogP8J6inRE6OckD1FSB1orJNymZ6XtMy7OXYzq2tEDkuK8B2EqA1 8MfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=N3CM13jo62axLkrf6XuA97Thp4ETYRb/qw3VqpYsyHo=; b=tIzBvZhHgJNl5ow3ad1ObAjH7WTuYA0xaAEwHCqvbJnrOTZTAim4XmTm8DdPusU7TX EBdhOiK87Wv5zn/fgcJZAK7TflT1OsVAh+rFVRMShR6YpAYG32aBexZtQwUm+UK+34SP gZxIUt+S/vw6i6V+q4u76KNF6LkzJwrIT4VisbRswQYUrh8WnU/jtRWYhBtJNFOayxf4 9QUtIhdDuzIHb/01BEazgFiMPCPVkeDv0LVEqV6PbtHi3rfz/9Hp5vKPf1Q3w3lldFze quaPM9T7qQw8NFMj6ZWHgegslcPUR4mVm1qJSBPuStl3SLqpYNdavJdbIWtcYKJPmvlR uPGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=T2gAhlQQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i205si9352751oih.257.2019.12.22.22.09.30; Sun, 22 Dec 2019 22:09:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=T2gAhlQQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726067AbfLWGIL (ORCPT + 99 others); Mon, 23 Dec 2019 01:08:11 -0500 Received: from mail-pj1-f68.google.com ([209.85.216.68]:53989 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725822AbfLWGIL (ORCPT ); Mon, 23 Dec 2019 01:08:11 -0500 Received: by mail-pj1-f68.google.com with SMTP id n96so6981737pjc.3; Sun, 22 Dec 2019 22:08:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=N3CM13jo62axLkrf6XuA97Thp4ETYRb/qw3VqpYsyHo=; b=T2gAhlQQjMq2WijcpADuqCm3cSn3t8PjmhRVMClIZ9Vu8sBLFCNRi7eei0WYYUlkIL dsPK7fT2pzzpDqlMrniUbK5ptWt2ZlAVZ7BgdZQ6X/ULpdyqD1YwMISg9l1C5mqoEt6a YSn8UzIx0pnPSy8e1WOikULjAx/0lBDmL7VacZNCaJNOsBaKwCB3HEfKdkLTEhZ7Obso FRj0xqgOwZDZui8je5Qbyl0YdovmSOauHYoA0Xr/Ppz0uZKOpI4bvkXqcExtZBIFxdm8 rYxvuHivQ4uvVBCRh9IgiJKHMaELe4mVIvmhasi03QM8a/VwosUdgXtOqP+W4OeEMjXR S53g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=N3CM13jo62axLkrf6XuA97Thp4ETYRb/qw3VqpYsyHo=; b=Pch5ev7mIHSPBQ0ZP4nswibW0BxD/JKljIxAnTzqLOFdcMegCC+jDrPMvEtNVrxVN3 YHnbIfW3WLzY+RYGajQffzWtUfeTvfCFO8ezyaRyLOqbOZ5eg9Il8k7SJUoww1MQt2I2 IkRcCWrlCSHr67nhW/tftsHl5uKYn4HroeXnSw8WOLnMHmJhmokTQ0UbSUmnNgsAfiR7 kqvcVbJwkm5tjXLznFIY1GgQuFm5UsxkCXbdUUwIKb4eHZUgbpIiQy+MMS5RTH1ACOLQ qz4f/mhDVPJ//VKCK7Keqscw7Vr8Xpn8c1daGoOSH0fqc3deUO4NIOvFRYCQNUBCYml3 26Iw== X-Gm-Message-State: APjAAAW4vmJNoxjX5VA4/Yp3yEmIRmOI3DVJSHUGpROST9jYcEp+keLq KvdTa2RaBgHNb9H5P1Bn/38= X-Received: by 2002:a17:902:6bcc:: with SMTP id m12mr27969307plt.272.1577081290159; Sun, 22 Dec 2019 22:08:10 -0800 (PST) Received: from gaurie.seo.corp.google.com ([2401:fa00:d:1:4eb0:a5ef:3975:7440]) by smtp.gmail.com with ESMTPSA id p185sm22978212pfg.61.2019.12.22.22.08.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Dec 2019 22:08:09 -0800 (PST) From: Namhyung Kim To: Ingo Molnar , Peter Zijlstra , Arnaldo Carvalho de Melo Cc: Jiri Olsa , Alexander Shishkin , Mark Rutland , Stephane Eranian , LKML , linux-perf-users@vger.kernel.org, Tejun Heo , Li Zefan , Johannes Weiner , Adrian Hunter Subject: [PATCHSET 0/9] perf: Improve cgroup profiling (v3) Date: Mon, 23 Dec 2019 15:07:50 +0900 Message-Id: <20191223060759.841176-1-namhyung@kernel.org> X-Mailer: git-send-email 2.24.1.735.g03f4e72817-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, This work is to improve cgroup profiling in perf. Currently it only supports profiling tasks in a specific cgroup and there's no way to identify which cgroup the current sample belongs to. So I added PERF_SAMPLE_CGROUP to add cgroup id into each sample. It's a 64-bit integer having file handle of the cgroup. And kernel also generates PERF_RECORD_CGROUP event for new groups to correlate the cgroup id and cgroup name (path in the cgroup filesystem). The cgroup id can be read from userspace by name_to_handle_at() system call so it can synthesize the CGROUP event for existing groups. So why do we want this? Systems running a large number of jobs in different cgroups want to profiling such jobs precisely. This includes container hosting systems widely used today. Currently perf supports namespace tracking but the systems may not use (cgroup) namespace for their jobs. Also it'd be more intuitive to see cgroup names (as they're given by user or sysadmin) rather than numeric cgroup/namespace id even if they use the namespaces. From Stephane Eranian: > In data centers you care about attributing samples to a job not such > much to a process. A job may have multiple processes which may come > and go. The cgroup on the other hand stays around for the entire > lifetime of the job. It is much easier to map a cgroup name to a > particular job than it is to map a pid back to a job name, > especially for offline post-processing. Note that this only works for "perf_event" cgroups (obviously) so if users are still using cgroup-v1 interface, they need to have same hierarchy for subsystem(s) want to profile with it. * Changes from v2: - remove path_len from cgroup_event - bail out if kernel doesn't support cgroup sampling - add some description in the Kconfig * Changes from v1: - use new cgroup id (= file handle) The testing result looks something like this: [root@qemu build]# ./perf record --all-cgroups ./cgtest [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.009 MB perf.data (150 samples) ] [root@qemu build]# ./perf report -s cgroup,comm --stdio # To display the perf.data header info, please use --header/--header-only options. # # # Total Lost Samples: 0 # # Samples: 150 of event 'cpu-clock:pppH' # Event count (approx.): 37500000 # # Overhead cgroup Command # ........ .......... ....... # 32.00% /sub/cgrp2 looper2 28.00% /sub/cgrp1 looper1 25.33% /sub looper0 4.00% / cgtest 4.00% /sub cgtest 3.33% /sub/cgrp2 cgtest 2.67% /sub/cgrp1 cgtest 0.67% / looper0 The test program (cgtest) follows. Thanks, Namhyung Cc: Tejun Heo Cc: Li Zefan Cc: Johannes Weiner Cc: Adrian Hunter -------8<-----------------------------------------8<---------------- #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include char cgbase[] = "/sys/fs/cgroup/perf_event"; void mkcgrp(char *name) { char buf[256]; snprintf(buf, sizeof(buf), "%s%s", cgbase, name); if (mkdir(buf, 0755) < 0) perror("mkdir"); } void rmcgrp(char *name) { char buf[256]; snprintf(buf, sizeof(buf), "%s%s", cgbase, name); if (rmdir(buf) < 0) perror("rmdir"); } void setcgrp(char *name) { char buf[256]; int fd; snprintf(buf, sizeof(buf), "%s%s/tasks", cgbase, name); fd = open(buf, O_WRONLY); if (fd > 0) { if (write(fd, "0\n", 2) != 2) perror("write"); close(fd); } } void create_sub_cgroup(int idx) { char buf[128]; snprintf(buf, sizeof(buf), "/sub/cgrp%d", idx); mkcgrp(buf); } void remove_sub_cgroup(int idx) { char buf[128]; snprintf(buf, sizeof(buf), "/sub/cgrp%d", idx); rmcgrp(buf); } void set_sub_cgroup(int idx) { char buf[128]; snprintf(buf, sizeof(buf), "/sub/cgrp%d", idx); setcgrp(buf); } void set_task_name(int idx) { char buf[16]; snprintf(buf, sizeof(buf), "looper%d", idx); prctl(PR_SET_NAME, buf, 0, 0, 0); } void loop(unsigned max) { volatile unsigned int count = 1; while (count++ != max) { asm volatile ("pause"); } } void worker(int idx, unsigned cnt, int new_ns) { int oldns; create_sub_cgroup(idx); set_sub_cgroup(idx); if (new_ns) { if (unshare(CLONE_NEWCGROUP) < 0) perror("unshare"); #if 0 /* FIXME */ if (unshare(CLONE_NEWNS) < 0) perror("unshare"); if (mount("none", "/sys", NULL, MS_REMOUNT | MS_REC | MS_SLAVE, NULL) < 0) perror("mount --make-rslave"); sleep(1); if (umount("/sys/fs/cgroup/perf_event") < 0) perror("umount"); if (mount("cgroup", "/sys/fs/cgroup/perf_event", "cgroup", MS_NODEV | MS_NOEXEC | MS_NOSUID, "perf_event") < 0) perror("mount again"); #endif } if (fork() == 0) { set_task_name(idx); loop(cnt); exit(0); } wait(NULL); } int main(int argc, char *argv[]) { int i, nr = 2; int new_ns = 1; unsigned cnt = 1000000; int fd; if (argc > 1) nr = atoi(argv[1]); if (argc > 2) cnt = atoi(argv[2]); if (argc > 3) new_ns = atoi(argv[3]); mkcgrp("/sub"); setcgrp("/sub"); for (i = 0; i < nr; i++) { if (fork() == 0) { worker(i+1, cnt, new_ns); exit(0); } } set_task_name(0); loop(cnt); for (i = 0; i < nr; i++) wait(NULL); for (i = 0; i < nr; i++) remove_sub_cgroup(i+1); setcgrp("/"); rmcgrp("/sub"); return 0; } -------8<-----------------------------------------8<---------------- Namhyung Kim (9): perf/core: Add PERF_RECORD_CGROUP event perf/core: Add PERF_SAMPLE_CGROUP feature perf tools: Basic support for CGROUP event perf tools: Maintain cgroup hierarchy perf report: Add 'cgroup' sort key perf record: Support synthesizing cgroup events perf record: Add --all-cgroups option perf top: Add --all-cgroups option perf script: Add --show-cgroup-events option include/linux/perf_event.h | 1 + include/uapi/linux/perf_event.h | 16 ++- init/Kconfig | 3 +- kernel/events/core.c | 133 ++++++++++++++++++++++ tools/include/uapi/linux/perf_event.h | 16 ++- tools/perf/Documentation/perf-record.txt | 5 +- tools/perf/Documentation/perf-report.txt | 1 + tools/perf/Documentation/perf-script.txt | 3 + tools/perf/Documentation/perf-top.txt | 4 + tools/perf/builtin-diff.c | 1 + tools/perf/builtin-record.c | 10 ++ tools/perf/builtin-report.c | 1 + tools/perf/builtin-script.c | 41 +++++++ tools/perf/builtin-top.c | 9 ++ tools/perf/lib/include/perf/event.h | 7 ++ tools/perf/util/cgroup.c | 75 +++++++++++- tools/perf/util/cgroup.h | 16 ++- tools/perf/util/event.c | 19 ++++ tools/perf/util/event.h | 6 + tools/perf/util/evsel.c | 17 ++- tools/perf/util/evsel.h | 1 + tools/perf/util/hist.c | 7 ++ tools/perf/util/hist.h | 1 + tools/perf/util/machine.c | 19 ++++ tools/perf/util/machine.h | 3 + tools/perf/util/perf_event_attr_fprintf.c | 2 + tools/perf/util/record.h | 1 + tools/perf/util/session.c | 8 ++ tools/perf/util/sort.c | 31 +++++ tools/perf/util/sort.h | 2 + tools/perf/util/synthetic-events.c | 119 +++++++++++++++++++ tools/perf/util/synthetic-events.h | 1 + tools/perf/util/tool.h | 2 + 33 files changed, 568 insertions(+), 13 deletions(-) -- 2.24.1.735.g03f4e72817-goog