Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2372741rbb; Tue, 27 Feb 2024 22:32:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWzCL704Nec05OUa1m9hLGAMFG4efFXN2fnCC/1RLvMB1Kb86VLYOuK5AOfkvM+7UIGTshLApgd8qaacVk6BK85KLu0yRYxGCaduRHEQw== X-Google-Smtp-Source: AGHT+IHRbkc2jhdnP9Cxa9C0uOsxBPHp6hW7YxYcdAS4Iz2ylFc4uqU/EJD/K3LxZoeISrb5tsAd X-Received: by 2002:a17:902:e5cf:b0:1dc:fb5:6f36 with SMTP id u15-20020a170902e5cf00b001dc0fb56f36mr15392970plf.32.1709101965753; Tue, 27 Feb 2024 22:32:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709101965; cv=pass; d=google.com; s=arc-20160816; b=aGJ7y8Yl7epmc6fBuaE9DSDRw1Yf1jWa+148Acrp+AgDeuztMoun0ktk72DPDpl0E0 3Rx/NEzlbj8Krw24i8N+E4nK4GjkftXrfpvr0mKvqszu5UnSyb99NiqNcFmi/YoQgESf lAA9lUM+EUKWhFw0p429+PbHBcU07csp/p01W8r0q7xUsQbSDke5NEZrVg0bUWcvhKvG WIPSgmbo8xkixiq0Ud5fZR6d9ZCQ7KIosPjsRpp6Y/Y8BvYGFATI2DQSj/lFVTn9st84 nxYTv6jy26j9A/QCaBHL1/vf4sRZHjiCoFcuhQXjgIxzGHEtk3s47kvZ+HI3rF5N+E7x GIhg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=7kfropls9BSDYywJcFgAsbuoKFtsZgwR3FdIR6FHl8M=; fh=t7RL0Zr+G+hE4HF08lgJB7HfhByWOIAwOM/lrT7Hd4Q=; b=A5IpgCUd6k9xLlDLxw2CDpYclNi9J78eZxvFb5g/tlVV4w9PTBmlaQ5Ok+Lk8TTI/g 6smnMtS0AKSnVeb0mHRSbyze11nUyneyvxOnO0tbczgTHhj79BE2vvv1gGBlziP9bDdz 2zaKcOo6mUP4qY5DMq7016JqUn0CZv1SeonsIf+yryWFW1YW8s4yrWDroJZ/tQ3DKNPh Gr8IkA9MhZ3tfMEIXNGDEGgGvzJpsTbcFjKPggLRQ1y1fk0IBegvJ3kwtsJYhUB7cQj9 CcifBqdFBaUB/WRoU/a6X4FUBW9CILWcZIpNxZAHs2gA8mol+bbjNhv0oqAOraEwWR6u RI6A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aFlYRe1M; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-84564-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84564-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id m8-20020a170902db0800b001d9f2674003si2811061plx.121.2024.02.27.22.32.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 22:32:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84564-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aFlYRe1M; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-84564-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84564-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 70203286C2E for ; Wed, 28 Feb 2024 06:32:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3CDD022EE8; Wed, 28 Feb 2024 06:32:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aFlYRe1M" Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) (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 7449022086 for ; Wed, 28 Feb 2024 06:32:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709101955; cv=none; b=FKtRwkEPgagRyrm2CdSQ74VQIAATSUZvmLiMYdfexmWkZBsEoBK9C+2E/iHzshKC6xfYnWCmfXnwkHODNKvWKTccevVMu+YGZ4JgeNy+B2aeUxGl504VZwW1s7l5eSaf6Ec5X/lsj+4K9tHKPxsg9lFIHUnQHR//f+6sk3qy0A4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709101955; c=relaxed/simple; bh=QcQL8W30rO/wVOY4BowBdqac0u0A3vahCxu4ucWjwmA=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=EouRHBdnB9fBF0/zgFgaV/a8qoNBq/iCue6Z/IFF34idD/uBG28xQPhv2W+mV2JO+Rjmgnm62AsfFT/iOl94n5F5l/YDkiA2DkRZ6qGhxdA270WRSCb2d/Ul7EwJe7hDiUoFVAItF9VteK8FwKLZVNbOU9ZOU+Brggy0MoMSl5o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aFlYRe1M; arc=none smtp.client-ip=209.85.160.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-qt1-f172.google.com with SMTP id d75a77b69052e-42e7f5e24beso104051cf.1 for ; Tue, 27 Feb 2024 22:32:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709101951; x=1709706751; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=7kfropls9BSDYywJcFgAsbuoKFtsZgwR3FdIR6FHl8M=; b=aFlYRe1MCnA/UlzLsUj6aOXS0Ib37A8sSREoPu0QqSj9VEXwYlaIcqnv3SpwRzcHUk QUO7NzePXz9TvdZMlb1M3fIm/c2/elNKq2Lq1noE9GDbIEcf5mikmhAZhzI7j4KIg+Of 9RxWrMGcKEbcBnYDwyNbrictCIyOiZgSIb1s7ehKGTFtToasNTTyIhIT40u43/KNjJ1x b9TtdwWgv4TFwuS1fzS/IUua7tKP2k+gZtVa/Z1d5ANoau1Dwpn/k4gAbTHn9UkuGvcU SD4C7vx+FXBtnduKTL1VVE0Bi7PPwdL1/cScEsGntoCmM/7Q2K0nuvgMOI+r6DlGKNqL 0KNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709101951; x=1709706751; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7kfropls9BSDYywJcFgAsbuoKFtsZgwR3FdIR6FHl8M=; b=pVLEYlcqZAsbmRtXj4zg1dTLM2D7gCXUvWJieyFfH7o2n+OlXmPj4GL+LR+FT/5kFn 7FZSfetMpaKaWcOT5syHry2zaWla59798JFXvQrjyXquUYnUtA8FJJoVhjwCjbrkrR2f CWRpBS8VJlNxNH6JYpvPUXPVcVvSkcuc3agK6F/q6BCumlqWH1YHQ1VP1re6AwsX9hEa DIbC5n/vdkznxp+jIiT0vHIMlbdSq9H0Dm3FBdPOUcZpTsVjyo7cLzLCjaPmK3H1nCfh LQP1FRx1GJRgJIR339yLv2DXLPRFYPu1vUfv9MceEZpHEw0H8DqHb/Lh72ZQve22WmnU 02cg== X-Forwarded-Encrypted: i=1; AJvYcCUxArsoeJaBeTp7ZJ2qVWTNmLP6rhMVjltH0WrFRJ1slOQPZqnRMC/CMigOq9WtKkI6ijRSADtEpSm9nerOdnQUn6AsAqOFTDJUs+d+ X-Gm-Message-State: AOJu0YwEMk6dT1BSGfWSjfeiDAEUugCs6Phha7dFhWpak54Kr9wFvFM+ tPx1m+zYkWMUruABmbzfocWhsx8T2J6iu6t7kPTdlbDei8qlmqfGbQuUaj49JqmELRx4wSa3fvP qD4bcC/2ssH8KQy7jG/YFaNi8HdnrAbkMx9Mt X-Received: by 2002:a05:622a:4c6:b0:42e:3233:4924 with SMTP id q6-20020a05622a04c600b0042e32334924mr56931qtx.26.1709101951306; Tue, 27 Feb 2024 22:32:31 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240228053335.312776-1-namhyung@kernel.org> In-Reply-To: <20240228053335.312776-1-namhyung@kernel.org> From: Ian Rogers Date: Tue, 27 Feb 2024 22:32:17 -0800 Message-ID: Subject: Re: [PATCH v2] perf lock contention: Account contending locks too To: Namhyung Kim Cc: Arnaldo Carvalho de Melo , Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 27, 2024 at 9:33=E2=80=AFPM Namhyung Kim = wrote: > > Currently it accounts the contention using delta between timestamps in > lock:contention_begin and lock:contention_end tracepoints. But it means > the lock should see the both events during the monitoring period. > > Actually there are 4 cases that happen with the monitoring: > > monitoring period > / \ > | | > 1: B------+-----------------------+--------E > 2: B----+-------------E | > 3: | B-----------+----E > 4: | B-------------E | > | | > t0 t1 > > where B and E mean contention BEGIN and END, respectively. So it only > accounts the case 4 for now. It seems there's no way to handle the case > 1. The case 2 might be handled if it saved the timestamp (t0), but it > lacks the information from the B notably the flags which shows the lock > types. Also it could be a nested lock which it currently ignores. So > I think we should ignore the case 2. > > However we can handle the case 3 if we save the timestamp (t1) at the > end of the period. And then it can iterate the map entries in the > userspace and update the lock stat accordinly. > > Signed-off-by: Namhyung Kim Reviewed-by: Ian Rogers Thanks, Ian > --- > v2: add a comment on mark_end_timestamp (Ian) > > tools/perf/util/bpf_lock_contention.c | 120 ++++++++++++++++++ > .../perf/util/bpf_skel/lock_contention.bpf.c | 16 ++- > tools/perf/util/bpf_skel/lock_data.h | 7 + > 3 files changed, 136 insertions(+), 7 deletions(-) > > diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_= lock_contention.c > index 31ff19afc20c..9af76c6b2543 100644 > --- a/tools/perf/util/bpf_lock_contention.c > +++ b/tools/perf/util/bpf_lock_contention.c > @@ -179,6 +179,123 @@ int lock_contention_prepare(struct lock_contention = *con) > return 0; > } > > +/* > + * Run the BPF program directly using BPF_PROG_TEST_RUN to update the en= d > + * timestamp in ktime so that it can calculate delta easily. > + */ > +static void mark_end_timestamp(void) > +{ > + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, > + .flags =3D BPF_F_TEST_RUN_ON_CPU, > + ); > + int prog_fd =3D bpf_program__fd(skel->progs.end_timestamp); > + > + bpf_prog_test_run_opts(prog_fd, &opts); > +} > + > +static void update_lock_stat(int map_fd, int pid, u64 end_ts, > + enum lock_aggr_mode aggr_mode, > + struct tstamp_data *ts_data) > +{ > + u64 delta; > + struct contention_key stat_key =3D {}; > + struct contention_data stat_data; > + > + if (ts_data->timestamp >=3D end_ts) > + return; > + > + delta =3D end_ts - ts_data->timestamp; > + > + switch (aggr_mode) { > + case LOCK_AGGR_CALLER: > + stat_key.stack_id =3D ts_data->stack_id; > + break; > + case LOCK_AGGR_TASK: > + stat_key.pid =3D pid; > + break; > + case LOCK_AGGR_ADDR: > + stat_key.lock_addr_or_cgroup =3D ts_data->lock; > + break; > + case LOCK_AGGR_CGROUP: > + /* TODO */ > + return; > + default: > + return; > + } > + > + if (bpf_map_lookup_elem(map_fd, &stat_key, &stat_data) < 0) > + return; > + > + stat_data.total_time +=3D delta; > + stat_data.count++; > + > + if (delta > stat_data.max_time) > + stat_data.max_time =3D delta; > + if (delta < stat_data.min_time) > + stat_data.min_time =3D delta; > + > + bpf_map_update_elem(map_fd, &stat_key, &stat_data, BPF_EXIST); > +} > + > +/* > + * Account entries in the tstamp map (which didn't see the corresponding > + * lock:contention_end tracepoint) using end_ts. > + */ > +static void account_end_timestamp(struct lock_contention *con) > +{ > + int ts_fd, stat_fd; > + int *prev_key, key; > + u64 end_ts =3D skel->bss->end_ts; > + int total_cpus; > + enum lock_aggr_mode aggr_mode =3D con->aggr_mode; > + struct tstamp_data ts_data, *cpu_data; > + > + /* Iterate per-task tstamp map (key =3D TID) */ > + ts_fd =3D bpf_map__fd(skel->maps.tstamp); > + stat_fd =3D bpf_map__fd(skel->maps.lock_stat); > + > + prev_key =3D NULL; > + while (!bpf_map_get_next_key(ts_fd, prev_key, &key)) { > + if (bpf_map_lookup_elem(ts_fd, &key, &ts_data) =3D=3D 0) = { > + int pid =3D key; > + > + if (aggr_mode =3D=3D LOCK_AGGR_TASK && con->owner= ) > + pid =3D ts_data.flags; > + > + update_lock_stat(stat_fd, pid, end_ts, aggr_mode, > + &ts_data); > + } > + > + prev_key =3D &key; > + } > + > + /* Now it'll check per-cpu tstamp map which doesn't have TID. */ > + if (aggr_mode =3D=3D LOCK_AGGR_TASK || aggr_mode =3D=3D LOCK_AGGR= _CGROUP) > + return; > + > + total_cpus =3D cpu__max_cpu().cpu; > + ts_fd =3D bpf_map__fd(skel->maps.tstamp_cpu); > + > + cpu_data =3D calloc(total_cpus, sizeof(*cpu_data)); > + if (cpu_data =3D=3D NULL) > + return; > + > + prev_key =3D NULL; > + while (!bpf_map_get_next_key(ts_fd, prev_key, &key)) { > + if (bpf_map_lookup_elem(ts_fd, &key, cpu_data) < 0) > + goto next; > + > + for (int i =3D 0; i < total_cpus; i++) { > + update_lock_stat(stat_fd, -1, end_ts, aggr_mode, > + &cpu_data[i]); > + } > + > +next: > + prev_key =3D &key; > + } > + free(cpu_data); > +} > + > int lock_contention_start(void) > { > skel->bss->enabled =3D 1; > @@ -188,6 +305,7 @@ int lock_contention_start(void) > int lock_contention_stop(void) > { > skel->bss->enabled =3D 0; > + mark_end_timestamp(); > return 0; > } > > @@ -301,6 +419,8 @@ int lock_contention_read(struct lock_contention *con) > if (stack_trace =3D=3D NULL) > return -1; > > + account_end_timestamp(con); > + > if (con->aggr_mode =3D=3D LOCK_AGGR_TASK) { > struct thread *idle =3D __machine__findnew_thread(machine= , > /*pid=3D*= /0, > diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/= util/bpf_skel/lock_contention.bpf.c > index 95cd8414f6ef..fb54bd38e7d0 100644 > --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c > +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c > @@ -19,13 +19,6 @@ > #define LCB_F_PERCPU (1U << 4) > #define LCB_F_MUTEX (1U << 5) > > -struct tstamp_data { > - __u64 timestamp; > - __u64 lock; > - __u32 flags; > - __s32 stack_id; > -}; > - > /* callstack storage */ > struct { > __uint(type, BPF_MAP_TYPE_STACK_TRACE); > @@ -140,6 +133,8 @@ int perf_subsys_id =3D -1; > /* determine the key of lock stat */ > int aggr_mode; > > +__u64 end_ts; > + > /* error stat */ > int task_fail; > int stack_fail; > @@ -559,4 +554,11 @@ int BPF_PROG(collect_lock_syms) > return 0; > } > > +SEC("raw_tp/bpf_test_finish") > +int BPF_PROG(end_timestamp) > +{ > + end_ts =3D bpf_ktime_get_ns(); > + return 0; > +} > + > char LICENSE[] SEC("license") =3D "Dual BSD/GPL"; > diff --git a/tools/perf/util/bpf_skel/lock_data.h b/tools/perf/util/bpf_s= kel/lock_data.h > index 08482daf61be..36af11faad03 100644 > --- a/tools/perf/util/bpf_skel/lock_data.h > +++ b/tools/perf/util/bpf_skel/lock_data.h > @@ -3,6 +3,13 @@ > #ifndef UTIL_BPF_SKEL_LOCK_DATA_H > #define UTIL_BPF_SKEL_LOCK_DATA_H > > +struct tstamp_data { > + u64 timestamp; > + u64 lock; > + u32 flags; > + u32 stack_id; > +}; > + > struct contention_key { > u32 stack_id; > u32 pid; > -- > 2.44.0.rc1.240.g4c46232300-goog >