From nobody Fri Dec 19 04:06:48 2025 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 482CD82C7E for ; Thu, 11 Jul 2024 12:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720700227; cv=none; b=rGIQvd7ZtU0ZHWY7IAdx9n7I1vGCVILk4v46npemB92ou7BPyVWWZMPGukWVzK5HVkkFk6hVrAxhK/AhE8sC6AlkRYxU8D8VE+SbsHZrlVzt3r1JDMZytq3bTNdhUuXT0kij+cL+rIuRCm59tjqvh5K9CV+DWd56SSSNIE87yVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720700227; c=relaxed/simple; bh=Bpm6/V6MDQFK3+fB6ShwWGUjksTwvserU1Szp0+Gc14=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=oeeNu1CcCxilQjwq3lVGxBE6ZQjApXNy4QFLPBpHjXtOCn/JKuBYxgzf6ItNSa2uzibWbSZkdTWtOkVY1YWD16up4l9SLAvMVcsssRK+tSmIAMVP+bu2IZYS0cnSYfLX49dmT3Gw6IVrxcfK6R/xIx+WGFg0vlYN9xsdckpdo4M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com; spf=fail smtp.mailfrom=broadcom.com; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b=RJCFoes+; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=broadcom.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="RJCFoes+" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1fb1c918860so13360155ad.1 for ; Thu, 11 Jul 2024 05:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1720700226; x=1721305026; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=2pG6NifEEeNfHkGMBqMcDY/jdLpNHGUhc4OZl9qCkHI=; b=RJCFoes+QwAmRmLxSJqHOAibQJN1eR2QeCyexZ0jaVR0CdCRoq4Em4UiPs2nEx1jea RILHhB+leQIF1lQHzKODY/bUm7JkqxYiCJbKKk/HfgbJD472c41cxD2vbITM83UlJYUr 44HLVuU3LBHk6E+YmYmOXzsXJNaSd4DCBCE1k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720700226; x=1721305026; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2pG6NifEEeNfHkGMBqMcDY/jdLpNHGUhc4OZl9qCkHI=; b=ttJVRmfuWicz3umROSUZ4Smdu3KTDEigbmeDhVkjRTRLr8L11bMB6AjzHyUjHzAgt/ Cw+nbYOmNOX27NO9LAw++GvKjhxnerZ3pxTDSzKwqmqSOMPYyoVicmKLLH+gpmMcWNxP YEBXF8LCHW4RhfOdou3C8PAb/jjAnzgNkBin+mfDWQg9fD6pM0WdH+VGAXyaH4TctZs7 5KwtNTI4qE/L2RH8az9MBBxN9aWTSSqDNVyuqUQ7j90zWx15wupyE5BonyAU2tbxEuiU uDTdTDJM6feNhqE0B/UbSIdOFB7XM4mvZEz5oNOjKiewnpbZFpcjeBWicJMKZmd1vHjV s5TA== X-Forwarded-Encrypted: i=1; AJvYcCVfqBA9bLHgxopQwCcSA3+1KomwcjNnzAvOgUJnSwjVlpC+OP0/4h2lgdraE4B9Z+YvDxWotqkHEZmqWTOCrZLkSdHCg0aV4x4thlC+ X-Gm-Message-State: AOJu0YzB4aMffS4/jlYne8AVIZ4ywHbXt59mniKwTq35uo63urAqs4VC T/IqE+6lDHvkI5x5fTDEbl5dEUUDHGRi9NcC60dcQmh1e42B65Y4+j1tGC7v/A== X-Google-Smtp-Source: AGHT+IHecHqT1oxxMCbOg1AOShzm2j71EEhKZQqKfq5DBpHatwzqR87qgWQdjHRuhZJRKeBdAtPcPw== X-Received: by 2002:a17:903:2449:b0:1f8:44f8:a357 with SMTP id d9443c01a7336-1fbdba235a6mr35954355ad.17.1720700225602; Thu, 11 Jul 2024 05:17:05 -0700 (PDT) Received: from akaher-virtual-machine.eng.vmware.com ([66.170.99.2]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fbb6a10111sm49211985ad.20.2024.07.11.05.17.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jul 2024 05:17:05 -0700 (PDT) From: Ajay Kaher To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org Cc: mark.rutland@arm.com, rostedt@goodmis.org, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, yangjihong1@huawei.com, zegao2021@gmail.com, leo.yan@linux.dev, asmadeus@codewreck.org, siyanteng@loongson.cn, sunhaiyong@loongson.cn, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, ajay.kaher@broadcom.com, alexey.makhalov@broadcom.com, vasavi.sirnapalli@broadcom.com Subject: [PATCH RFC 1/3] perf/core: add logic to collect off-cpu sample Date: Thu, 11 Jul 2024 17:46:17 +0530 Message-Id: <1720700179-22839-2-git-send-email-ajay.kaher@broadcom.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1720700179-22839-1-git-send-email-ajay.kaher@broadcom.com> References: <1720700179-22839-1-git-send-email-ajay.kaher@broadcom.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" following logics has been added to collect the off-cpu sample: - 'task_pt_regs(current)' has been used to capture registers status off-cpu sample. - off-cpu time represent the time period for which the target process not occupying the cpu cycles. And calculate as: off-cpu time =3D swap-in time - swap-out time Signed-off-by: Ajay Kaher --- include/linux/perf_event.h | 16 ++++++++++++++++ include/uapi/linux/perf_event.h | 3 ++- kernel/events/core.c | 27 ++++++++++++++++++++++----- tools/include/uapi/linux/perf_event.h | 3 ++- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index a5304ae8c654..09dc3f695974 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -599,6 +599,11 @@ enum perf_event_state { PERF_EVENT_STATE_ACTIVE =3D 1, }; =20 +enum perf_sample_cpu { + PERF_SAMPLE_ON_CPU, + PERF_SAMPLE_OFF_CPU, +}; + struct file; struct perf_sample_data; =20 @@ -828,6 +833,7 @@ struct perf_event { void *security; #endif struct list_head sb_list; + u64 stop_time; =20 /* * Certain events gets forwarded to another pmu internally by over- @@ -1301,6 +1307,16 @@ static inline u32 perf_sample_data_size(struct perf_= sample_data *data, return size; } =20 +static inline void perf_sample_set_off_cpu(struct perf_sample_data *data) +{ + data->cpu_entry.reserved =3D PERF_SAMPLE_OFF_CPU; +} + +static inline void perf_sample_set_on_cpu(struct perf_sample_data *data) +{ + data->cpu_entry.reserved =3D PERF_SAMPLE_ON_CPU; +} + /* * Clear all bitfields in the perf_branch_entry. * The to and from fields are not cleared because they are diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_even= t.h index 3a64499b0f5d..9d0a23ab2549 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -460,7 +460,8 @@ struct perf_event_attr { inherit_thread : 1, /* children only inherit if cloned with CLONE_THR= EAD */ remove_on_exec : 1, /* event is removed from task on exec */ sigtrap : 1, /* send synchronous SIGTRAP on event */ - __reserved_1 : 26; + off_cpu : 1, /* include off_cpu sample */ + __reserved_1 : 25; =20 union { __u32 wakeup_events; /* wakeup every n events */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 8f908f077935..e71eb7936134 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7168,10 +7168,8 @@ static void __perf_event_header__init_id(struct perf= _sample_data *data, if (sample_type & PERF_SAMPLE_STREAM_ID) data->stream_id =3D event->id; =20 - if (sample_type & PERF_SAMPLE_CPU) { - data->cpu_entry.cpu =3D raw_smp_processor_id(); - data->cpu_entry.reserved =3D 0; - } + if (sample_type & PERF_SAMPLE_CPU) + data->cpu_entry.cpu =3D raw_smp_processor_id(); } =20 void perf_event_header__init_id(struct perf_event_header *header, @@ -11083,8 +11081,8 @@ static enum hrtimer_restart perf_swevent_hrtimer(st= ruct hrtimer *hrtimer) return HRTIMER_NORESTART; =20 event->pmu->read(event); - perf_sample_data_init(&data, 0, event->hw.last_period); + perf_sample_set_on_cpu(&data); regs =3D get_irq_regs(); =20 if (regs && !perf_exclude_event(event, regs)) { @@ -11099,6 +11097,18 @@ static enum hrtimer_restart perf_swevent_hrtimer(s= truct hrtimer *hrtimer) return ret; } =20 +static void perf_swevent_offCPUSample(struct perf_event *event, u64 period) +{ + struct perf_sample_data data; + struct pt_regs *regs; + + event->pmu->read(event); + perf_sample_data_init(&data, 0, period); + perf_sample_set_off_cpu(&data); + regs =3D task_pt_regs(current); + __perf_event_overflow(event, 1, &data, regs); +} + static void perf_swevent_start_hrtimer(struct perf_event *event) { struct hw_perf_event *hwc =3D &event->hw; @@ -11107,6 +11117,11 @@ static void perf_swevent_start_hrtimer(struct perf= _event *event) if (!is_sampling_event(event)) return; =20 + if (event->attr.off_cpu && event->stop_time && hwc->sample_period) { + perf_swevent_offCPUSample(event, perf_clock() - event->stop_time); + event->stop_time =3D 0; + } + period =3D local64_read(&hwc->period_left); if (period) { if (period < 0) @@ -11128,6 +11143,7 @@ static void perf_swevent_cancel_hrtimer(struct perf= _event *event) ktime_t remaining =3D hrtimer_get_remaining(&hwc->hrtimer); local64_set(&hwc->period_left, ktime_to_ns(remaining)); =20 + event->stop_time =3D perf_clock(); hrtimer_cancel(&hwc->hrtimer); } } @@ -11139,6 +11155,7 @@ static void perf_swevent_init_hrtimer(struct perf_e= vent *event) if (!is_sampling_event(event)) return; =20 + event->stop_time =3D 0; hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); hwc->hrtimer.function =3D perf_swevent_hrtimer; =20 diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/lin= ux/perf_event.h index 3a64499b0f5d..9d0a23ab2549 100644 --- a/tools/include/uapi/linux/perf_event.h +++ b/tools/include/uapi/linux/perf_event.h @@ -460,7 +460,8 @@ struct perf_event_attr { inherit_thread : 1, /* children only inherit if cloned with CLONE_THR= EAD */ remove_on_exec : 1, /* event is removed from task on exec */ sigtrap : 1, /* send synchronous SIGTRAP on event */ - __reserved_1 : 26; + off_cpu : 1, /* include off_cpu sample */ + __reserved_1 : 25; =20 union { __u32 wakeup_events; /* wakeup every n events */ --=20 2.39.0 From nobody Fri Dec 19 04:06:48 2025 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0AEA615B153 for ; Thu, 11 Jul 2024 12:17:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720700230; cv=none; b=eMNEsADndHMiiY5j/PLREpwjC2pXj3LgD+PrKdTKBs8DHMI1KetGyydmoyhFP+8YvUH2PaAnJK4eX9kyYWP6guBh15kRasovS/YxVkwVRoPYSmFi0YMeC0QUexR9eJHczF6IrEcXOik3H7Ymr1Yqil39HGpLHt+dTaBIBkfgliU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720700230; c=relaxed/simple; bh=RcAtOY3BqIjsxZ/EkLx9cbSXMAG2HJ+eMA8VPs9/m08=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=aEaUlixJZcNfmm62tUxETqsHmmAZKjNK2bpwMfXpFt8jxUh2NdOmG9+1QKM68gpPK7j5IyqlJxkNFhHlY5k4JpZpdzE8tJOaKvCjgeXUm9/khB0ifAv/IbpALeMoNkDul9rfcWWDDOK2qZI/b3P7xfBciiiK8jeEDEG09V2NAqQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com; spf=fail smtp.mailfrom=broadcom.com; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b=LVHadjte; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=broadcom.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="LVHadjte" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1fb3b7d0d3aso4671445ad.2 for ; Thu, 11 Jul 2024 05:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1720700228; x=1721305028; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=QMWLbotKK9Kcv+yDqQHrYwDupJwtZdSLIhFGpeisVqk=; b=LVHadjte/h7XYlTcwcYI9U+/gzwWtnQc/9wrfJ5esFeOoj/GDcPtmIZSShn8qst4rq MIdmt/L5PoQA6qXOfD6NQ0ay7vIr/Y3S0c8FPj1PTSBkEr71aARovXGvY8d/kpjyc28f Sar2DaP7hMflHtT2m0TS+rqh48xsbHd5mfqKs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720700228; x=1721305028; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QMWLbotKK9Kcv+yDqQHrYwDupJwtZdSLIhFGpeisVqk=; b=wIYwco/iRZVKk9368obmG/iuaGz3ciLwslZoFT7ykOQ4zbl2EK3W3Y4unrFKjA1JKz sK77Ow23EjCo73adm4mqiJCVcgb7sVyS0hiok9cJqdrNxALBHE4Hvt7A2fx7s++VMLqZ liym4unppVXEuC4FYb9ElYndNie0V0t1rzxODFuvnHXwXyEgbKtPlXKGVnIyaJTOSHCY wYEe8N0304CPuiXIXsxa8UCXOsQdQI1jZalyuXvwq0bqo/XrKl1VAC2ZiEi+i9DHnzqr bdIsvM1sp3a53kUJqqNwDZ5+sA3/Ezfqixj359oT6+fEZKMI+r0PDYU7yIbNAH/7l/U1 aSNA== X-Forwarded-Encrypted: i=1; AJvYcCUGxFnZI4SkzRaJA8POluPMBaK0Z2a4+ufueWTUf3ETlRTdV5rNq30UNsca894OnC84eDw1dCtErefmd0H4rC0dPiXkfzLEXp3HBmCV X-Gm-Message-State: AOJu0YyhUtPGNTue/FbeFHrymgYRwKPwOPn7+r9ffOT1ecOiRUcOIPpC rNxGfgEV+K9MWPxDozdw1q9fMyr9KGKLXb6VW+Pg7If4F8BlLTWdZavc3lxmKg== X-Google-Smtp-Source: AGHT+IEay4VzDWGc7cNbJqKwvD6xCyKm+QF0n/B7mqHkBPs8hMJ1zCGnny6ZKJS0v/G/jgh4m0FgQw== X-Received: by 2002:a17:902:e54c:b0:1f7:1b08:dda9 with SMTP id d9443c01a7336-1fbb6cdac37mr68496965ad.8.1720700228186; Thu, 11 Jul 2024 05:17:08 -0700 (PDT) Received: from akaher-virtual-machine.eng.vmware.com ([66.170.99.2]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fbb6a10111sm49211985ad.20.2024.07.11.05.17.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jul 2024 05:17:07 -0700 (PDT) From: Ajay Kaher To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org Cc: mark.rutland@arm.com, rostedt@goodmis.org, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, yangjihong1@huawei.com, zegao2021@gmail.com, leo.yan@linux.dev, asmadeus@codewreck.org, siyanteng@loongson.cn, sunhaiyong@loongson.cn, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, ajay.kaher@broadcom.com, alexey.makhalov@broadcom.com, vasavi.sirnapalli@broadcom.com Subject: [PATCH RFC 2/3] perf/record: add options --off-cpu-kernel Date: Thu, 11 Jul 2024 17:46:18 +0530 Message-Id: <1720700179-22839-3-git-send-email-ajay.kaher@broadcom.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1720700179-22839-1-git-send-email-ajay.kaher@broadcom.com> References: <1720700179-22839-1-git-send-email-ajay.kaher@broadcom.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" --off-cpu-kernel to collect off cpu samples using on/off cpu time. Signed-off-by: Ajay Kaher --- tools/perf/builtin-record.c | 2 ++ tools/perf/util/evsel.c | 2 ++ tools/perf/util/record.h | 1 + 3 files changed, 5 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 0a8ba1323d64..5be172537330 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -3571,6 +3571,8 @@ static struct option __record_options[] =3D { "write collected trace data into several data files using parallel = threads", record__parse_threads), OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"), + OPT_BOOLEAN(0, "off-cpu-kernel", &record.opts.off_cpu_kernel, + "Enable kernel based off-cpu analysis"), OPT_END() }; =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 4f818ab6b662..8ba890a5ac6e 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -862,6 +862,8 @@ static void __evsel__config_callchain(struct evsel *evs= el, struct record_opts *o attr->exclude_callchain_user =3D 1; if (opts->user_callchains) attr->exclude_callchain_kernel =3D 1; + if (opts->off_cpu_kernel) + attr->off_cpu =3D 1; if (param->record_mode =3D=3D CALLCHAIN_LBR) { if (!opts->branch_stack) { if (attr->exclude_user) { diff --git a/tools/perf/util/record.h b/tools/perf/util/record.h index a6566134e09e..cfa5e34b78ad 100644 --- a/tools/perf/util/record.h +++ b/tools/perf/util/record.h @@ -52,6 +52,7 @@ struct record_opts { bool kcore; bool text_poke; bool build_id; + bool off_cpu_kernel; unsigned int freq; unsigned int mmap_pages; unsigned int auxtrace_mmap_pages; --=20 2.39.0 From nobody Fri Dec 19 04:06:48 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9286615E5DF for ; Thu, 11 Jul 2024 12:17:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720700233; cv=none; b=UID6sxQGAg9Z2DL72axJQq51FHVfj9aIzl/vKYgFrq8VXJgEgqTfJbukda12HSGshPs5eUwv6HHteGmst9DyULd0ePIEmSenFTGc9nNU6E3WhrKYgg9XChXe0Ft7OMJwcCjPK0LL1S79Vpp2lgZoTcl6PYExIKEXQtkfVDt7QO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720700233; c=relaxed/simple; bh=LylSGN00CZycrIeZtCZWrilto+Oh9Zgg0qJLpjORVCA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=t3QZW2x+P5sZlyJ6EmVGxBN6NBJkHDRjNj1ujgKr4cTiDP8m3rR7vSRW59xAjomj/VTKxHxeLK8eAjz5xEQs9CqpVm8Zoxk4D8dmRS1gWy5HiB2bOxR+bx4U4cfTncLtwSaughJXfWSOueZdawRt8HQ2Xj+eEtQlqZwVwRmZBQA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com; spf=fail smtp.mailfrom=broadcom.com; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b=KK7LuUy+; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=broadcom.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=broadcom.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.b="KK7LuUy+" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-70b6003c3d2so501652b3a.0 for ; Thu, 11 Jul 2024 05:17:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1720700231; x=1721305031; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=zNUgfMo2gXZSNLPHfh654aj1KCXo46JCf2k9E+T7+E0=; b=KK7LuUy+DBSzLjLX10CbOSk3k555My2TQvb9zodLgY7dCbEGe6XkgA/6vtkyZqmWXh cljPNDwaETAJYPd8Hz4Bkqqry/FvvVN9fmzEtPOz9r92yOIgX+yMwvEAc0G80zAKUcD+ LzohAkmFSEWr5MenVgG3kzLakMUPFiNikVz5A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720700231; x=1721305031; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zNUgfMo2gXZSNLPHfh654aj1KCXo46JCf2k9E+T7+E0=; b=aGCEKjK7NI0Y2yfgiwgo6vdo6h9ETM+KbfzkPv0/echiiP2UM6zSYgzOFFIsSeEu+4 piCAqPXRvJsgmefC41flQEqFsZdwDYNxRiMMdu7OkkfhwQnqysNeVSRh1F/uSOiJxW3D Jfp68Ib5rKKJ7MVGiIYDpGv1CHnvGeMu9OIvAIVRmYsXVxU0mFwsap2j0UYzTJyk0eLE 4Vwl1rCjwaaTWMNZ48EmcyHhKhyB9UxeMqHqz5GuQXJ1cTezGhHkmEe2MJ10Vr3VRNo+ pR4XTApnrnJz1SVr4LuOgDRDteOKc+Fg78e4XEwhTE0QQJ40SzWQYezEhiZKtteeSVzC zO6Q== X-Forwarded-Encrypted: i=1; AJvYcCWsUmsS50bS9vA5GtsAA21IOv+a+te3ZeJZaBnI/h671No4wtmQuVTPHe44MvcaOEVIa60bkKr+ivd6YHN3/tPP/mFm5Gk0/ZX8Q2Pm X-Gm-Message-State: AOJu0YwiQySZkWV4U20/wJHKFiUkA0pDAieyMDE0uRRL/kKo9+PLrr9B pbvUXuvJRxPm5p+sK+aKTX7Z4KUl9xOwJ/musGVCLGtYNwScMzw+dEcCXQ5ezBVx+UbKTV1PyyT 6c5lI X-Google-Smtp-Source: AGHT+IFNkZ5F02HmXU6UCANTg5DiTRJQwTFLrtdtbLiaqEloiR+jcdq1tO98nmpNd2EYHR1M29lc+Q== X-Received: by 2002:a05:6a20:734c:b0:1c0:f6b7:a897 with SMTP id adf61e73a8af0-1c2982285f2mr9806840637.32.1720700230784; Thu, 11 Jul 2024 05:17:10 -0700 (PDT) Received: from akaher-virtual-machine.eng.vmware.com ([66.170.99.2]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fbb6a10111sm49211985ad.20.2024.07.11.05.17.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jul 2024 05:17:10 -0700 (PDT) From: Ajay Kaher To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org Cc: mark.rutland@arm.com, rostedt@goodmis.org, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, yangjihong1@huawei.com, zegao2021@gmail.com, leo.yan@linux.dev, asmadeus@codewreck.org, siyanteng@loongson.cn, sunhaiyong@loongson.cn, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, ajay.kaher@broadcom.com, alexey.makhalov@broadcom.com, vasavi.sirnapalli@broadcom.com Subject: [PATCH RFC 3/3] perf/report: add off-cpu samples Date: Thu, 11 Jul 2024 17:46:19 +0530 Message-Id: <1720700179-22839-4-git-send-email-ajay.kaher@broadcom.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1720700179-22839-1-git-send-email-ajay.kaher@broadcom.com> References: <1720700179-22839-1-git-send-email-ajay.kaher@broadcom.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" off-cpu samples represent the time period for which the target process not occupying the cpu cycles. In following example, perf has collected 15 off-cpu samples and program was running on cpu for 27%: Samples: 24 of 'task-clock:ppp', 15 of 'offcpu', Event count: ~9150831908 (= 73% offcpu) +73.77% 73.77% a.out libc.so.6 [.] clock_nanosleep <-- off-c= pu sample +24.04% 24.04% a.out [vdso] [.] __vdso_gettimeofday <-- on-cp= u sample Signed-off-by: Ajay Kaher --- tools/perf/util/events_stats.h | 2 ++ tools/perf/util/evsel.c | 2 ++ tools/perf/util/hist.c | 31 ++++++++++++++++++++++++++++--- tools/perf/util/hist.h | 1 + tools/perf/util/sample.h | 1 + 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/events_stats.h b/tools/perf/util/events_stats.h index 8fecc9fbaecc..7bb3cf1ab835 100644 --- a/tools/perf/util/events_stats.h +++ b/tools/perf/util/events_stats.h @@ -44,8 +44,10 @@ struct events_stats { =20 struct hists_stats { u64 total_period; + u64 total_period_off_cpu; u64 total_non_filtered_period; u32 nr_samples; + u64 nr_samples_off_cpu; u32 nr_non_filtered_samples; u32 nr_lost_samples; }; diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 8ba890a5ac6e..ea41586474e3 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1146,6 +1146,7 @@ void evsel__config(struct evsel *evsel, struct record= _opts *opts, attr->write_backward =3D opts->overwrite ? 1 : 0; attr->read_format =3D PERF_FORMAT_LOST; =20 + evsel__set_sample_bit(evsel, CPU); evsel__set_sample_bit(evsel, IP); evsel__set_sample_bit(evsel, TID); =20 @@ -2438,6 +2439,7 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, u.val32[0] =3D bswap_32(u.val32[0]); } =20 + data->off_cpu =3D u.val32[1]; data->cpu =3D u.val32[0]; array++; } diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index 2e9e193179dd..251333e0b021 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c @@ -23,6 +23,7 @@ #include "thread.h" #include "block-info.h" #include "ui/progress.h" +#include "ui/util.h" #include #include #include @@ -725,6 +726,7 @@ __hists__add_entry(struct hists *hists, .socket =3D al->socket, .cpu =3D al->cpu, .cpumode =3D al->cpumode, + .off_cpu =3D sample->off_cpu, .ip =3D al->addr, .level =3D al->level, .code_page_size =3D sample->code_page_size, @@ -1076,6 +1078,8 @@ iter_add_single_cumulative_entry(struct hist_entry_it= er *iter, callchain_cursor_commit(get_tls_callchain_cursor()); =20 hists__inc_nr_samples(hists, he->filtered); + if (sample->off_cpu) + ++hists->stats.nr_samples_off_cpu; =20 return err; } @@ -1740,6 +1744,7 @@ void hists__reset_stats(struct hists *hists) { hists->nr_entries =3D 0; hists->stats.total_period =3D 0; + hists->stats.total_period_off_cpu =3D 0; =20 hists__reset_filter_stats(hists); } @@ -1757,6 +1762,9 @@ void hists__inc_stats(struct hists *hists, struct his= t_entry *h) =20 hists->nr_entries++; hists->stats.total_period +=3D h->stat.period; + + if (h->off_cpu) + hists->stats.total_period_off_cpu +=3D h->stat.period; } =20 static void hierarchy_recalc_total_periods(struct hists *hists) @@ -2745,14 +2753,20 @@ int __hists__scnprintf_title(struct hists *hists, c= har *bf, size_t size, bool sh struct thread *thread =3D hists->thread_filter; int socket_id =3D hists->socket_filter; unsigned long nr_samples =3D hists->stats.nr_samples; + unsigned long nr_samples_off_cpu =3D hists->stats.nr_samples_off_cpu; u64 nr_events =3D hists->stats.total_period; + int nr_events_off_cpu_percentage =3D (hists->stats.total_period_off_cpu *= 100) / nr_events; struct evsel *evsel =3D hists_to_evsel(hists); const char *ev_name =3D evsel__name(evsel); char buf[512], sample_freq_str[64] =3D ""; + char oncpu_str[128] =3D ""; + char offcpu_str[128] =3D ""; + char offcpu_percentage_str[128] =3D ""; size_t buflen =3D sizeof(buf); char ref[30] =3D " show reference callgraph, "; bool enable_ref =3D false; =20 + if (symbol_conf.filter_relative) { nr_samples =3D hists->stats.nr_non_filtered_samples; nr_events =3D hists->stats.total_non_filtered_period; @@ -2785,10 +2799,21 @@ int __hists__scnprintf_title(struct hists *hists, c= har *bf, size_t size, bool sh scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->co= re.attr.sample_freq); =20 nr_samples =3D convert_unit(nr_samples, &unit); + + scnprintf(oncpu_str, sizeof(oncpu_str), "%lu%c of '%s',", + nr_samples - nr_samples_off_cpu, unit, ev_name); + + if (evsel->core.attr.off_cpu) { + scnprintf(offcpu_str, sizeof(offcpu_str), "%lu%c of '%s',", + nr_samples_off_cpu, unit, "offcpu"); + scnprintf(offcpu_percentage_str, sizeof(offcpu_percentage_str), + "(%d%% offcpu)", nr_events_off_cpu_percentage); + } + printed =3D scnprintf(bf, size, - "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64, - nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "", - ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events); + "Samples: %s %s %s%sEvent count: ~%" PRIu64 " %s", + oncpu_str, offcpu_str, sample_freq_str, enable_ref ? ref : " ", + nr_events, offcpu_percentage_str); =20 =20 if (hists->uid_filter_str) diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h index 8fb3bdd29188..c64a07ce92fb 100644 --- a/tools/perf/util/hist.h +++ b/tools/perf/util/hist.h @@ -236,6 +236,7 @@ struct hist_entry { /* We are added by hists__add_dummy_entry. */ bool dummy; bool leaf; + bool off_cpu; =20 char level; u8 filtered; diff --git a/tools/perf/util/sample.h b/tools/perf/util/sample.h index 70b2c3135555..59b0951f4718 100644 --- a/tools/perf/util/sample.h +++ b/tools/perf/util/sample.h @@ -109,6 +109,7 @@ struct perf_sample { u16 retire_lat; }; bool no_hw_idx; /* No hw_idx collected in branch_stack */ + bool off_cpu; char insn[MAX_INSN]; void *raw_data; struct ip_callchain *callchain; --=20 2.39.0