Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2074143rdb; Thu, 7 Dec 2023 18:28:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IE7CtPRYKB+gKcsma7NEhc6AtXQmq176tA/1wsdKPROO/kqsMtSQ0lHD1HrsWv8B8Bnh2lt X-Received: by 2002:a05:6e02:164b:b0:35e:6c38:cd21 with SMTP id v11-20020a056e02164b00b0035e6c38cd21mr492959ilu.90.1702002520344; Thu, 07 Dec 2023 18:28:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702002520; cv=none; d=google.com; s=arc-20160816; b=AQrWLLZhG4yKrq+D1Xv1OEeoIQ7zWL4gLo74u+aC3k3kUOJpaOLyWuoCIhcU4PMDEr H54pLAMsMYbQ9ImN3I/wgUm6mjTMCmmQHEpJQGKi/I/m8MVLLTzL31D+P0qS2+osT6n5 TqaJ4PNBaHHpwnUtwlw8lkikbKN6D0vgegHAwF01GPqope18JStN+tPN29J8LuYf/89O /Afn+8GZENENVNElDhbaR3SxcM+54dZt5jZaeOfHt5aDgbxYUZkMpAY02NoIUk6Sg4kQ YMgd7H6xsa23fjDsaoeU9zWMsP4VAJLRwS1WQ3C+TWseDcGeJuQ8yairUriYG8jusRRI jr1w== 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=IzrTp4CVflWPKS8RfWxv0dB/to3ENLCGnIZ/8i2cW0k=; fh=Eu3OfQ0ohnhl6u2myNPCo9midvMFa9y2PC9Dj+pizlY=; b=0B20xQXoz3pwuh8JGrXFvd3rpgkXA53DXEKH7tE5nvMb+RHBi1yebykFgY0TUiwVwU LefbVa6ZR7IM8n02Z9C4MCHhh/K+zUxpX2BuBCBLD7FOGh0366H9UX5M4iGuJqauM3C5 Gma+4RVEF3gedIu2B1nUDK/Xf6Bx4/M4OKhPMIuI9+QWaHA92gGNhIjdWUgXgu8oaSi3 DyWBykovh4h7LamLQ/TFZ5IdBWzSUs+aB5WM7BqA1eQpSCJD7kQPJ5ObMCG7Zw6ZDp9v FZARNPBXgXpJXao7I4x5F3I/ypot/adlANz3rGGHfpGgon5w6guykjxL5B1+EUZ3SZvM UExg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Ee3CTp94; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id u15-20020a65670f000000b005bd28c9bb23si700096pgf.308.2023.12.07.18.28.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 18:28:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Ee3CTp94; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id C4FBD80E073A; Thu, 7 Dec 2023 18:28:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1572987AbjLHC2N (ORCPT + 99 others); Thu, 7 Dec 2023 21:28:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229531AbjLHC2M (ORCPT ); Thu, 7 Dec 2023 21:28:12 -0500 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C63291719; Thu, 7 Dec 2023 18:28:16 -0800 (PST) Received: by mail-oi1-x235.google.com with SMTP id 5614622812f47-3b9b90f8708so1090067b6e.2; Thu, 07 Dec 2023 18:28:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702002496; x=1702607296; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=IzrTp4CVflWPKS8RfWxv0dB/to3ENLCGnIZ/8i2cW0k=; b=Ee3CTp9419g0D3S0i9PeoAx6AXAvoS1ERZ8jnbMT6zCkQY001Uh51Xd058cz/GJjRy 80SvmtGj3F3QjoDefwRHRS2qgFLLbEwSHzYKHXS+4zDJ4xwR9+YzWoXV4Ds4rI7N9dZ3 EHs9YnJPKZO5ZINmIHOENyqOvxUG3n876yQKKJNgy2AVOEpw79pJPV0UsiMfYMcmd6Q/ Fb/d6Um7ucW09Jpa8vumcgPgiy2lwh0Xwlo3DLp2PwgKMB2hTskTJJwEE8Mq/N3Dyzg/ JWRnvcfe5Z0/T35AEequumG10D69zMFFHLd6iSI0hcha+Qktnu2dhr+OyjFx9xOFqkTa DD/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702002496; x=1702607296; 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=IzrTp4CVflWPKS8RfWxv0dB/to3ENLCGnIZ/8i2cW0k=; b=AlaxCbKbm/JQvX1C24V2qV8nqv7psxGWMvN5O+MYYDpzGysnrC1y2Mv354dtcXk++v cLkubwrhvbEJvqmC9trvVfPUtw3JnJC+PuNkXokvA6GAmF4b7X7fEdLzT+1sOFdnk+Fo t8bXykpkKQeifVu402g5p4wlUgBMAaJ6m1r4IxCxt56+RzD9QcPqT0av7bk9pRM0vEj6 LXdR9SMinPrDPFTxkI54lBf32LOCExH/Pt6RcqUDN50OLhDs7+StGJADte6pwyeZ67bl ouxBiKXrV3oJjbmPEMwqrbPumpo7WzTftsgRA7DWTphrkvGh3TLVAYXxX8A9zUXc/Ui7 0n9Q== X-Gm-Message-State: AOJu0Yyqp++600DQmlv5edYCMB45QufTSYskYSfEHLxz1gnhHpNHaaaN Hx4phYUf1w6rF2rentdzrJU= X-Received: by 2002:a54:419a:0:b0:3b9:eb71:ee6f with SMTP id 26-20020a54419a000000b003b9eb71ee6fmr93857oiy.61.1702002495941; Thu, 07 Dec 2023 18:28:15 -0800 (PST) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:42e0:c5f1:ea67:a613:5fe]) by smtp.gmail.com with ESMTPSA id du6-20020a056a002b4600b006ce97bd5d04sm482330pfb.140.2023.12.07.18.28.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 18:28:15 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , bpf@vger.kernel.org Subject: [PATCH] perf lock contention: Account contending locks too Date: Thu, 7 Dec 2023 18:28:13 -0800 Message-ID: <20231208022813.219673-1-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.5 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_BLOCKED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 07 Dec 2023 18:28:26 -0800 (PST) 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 --- tools/perf/util/bpf_lock_contention.c | 116 ++++++++++++++++++ .../perf/util/bpf_skel/lock_contention.bpf.c | 16 +-- tools/perf/util/bpf_skel/lock_data.h | 7 ++ 3 files changed, 132 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index e105245eb905..2476459bf2ef 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -178,6 +178,119 @@ int lock_contention_prepare(struct lock_contention *con) return 0; } +static void mark_end_timestamp(void) +{ + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, + .flags = BPF_F_TEST_RUN_ON_CPU, + ); + int prog_fd = 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 = {}; + struct contention_data stat_data; + + if (ts_data->timestamp >= end_ts) + return; + + delta = end_ts - ts_data->timestamp; + + switch (aggr_mode) { + case LOCK_AGGR_CALLER: + stat_key.stack_id = ts_data->stack_id; + break; + case LOCK_AGGR_TASK: + stat_key.pid = pid; + break; + case LOCK_AGGR_ADDR: + stat_key.lock_addr_or_cgroup = 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 += delta; + stat_data.count++; + + if (delta > stat_data.max_time) + stat_data.max_time = delta; + if (delta < stat_data.min_time) + stat_data.min_time = 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 = skel->bss->end_ts; + int total_cpus; + enum lock_aggr_mode aggr_mode = con->aggr_mode; + struct tstamp_data ts_data, *cpu_data; + + /* Iterate per-task tstamp map (key = TID) */ + ts_fd = bpf_map__fd(skel->maps.tstamp); + stat_fd = bpf_map__fd(skel->maps.lock_stat); + + prev_key = NULL; + while (!bpf_map_get_next_key(ts_fd, prev_key, &key)) { + if (bpf_map_lookup_elem(ts_fd, &key, &ts_data) == 0) { + int pid = key; + + if (aggr_mode == LOCK_AGGR_TASK && con->owner) + pid = ts_data.flags; + + update_lock_stat(stat_fd, pid, end_ts, aggr_mode, + &ts_data); + } + + prev_key = &key; + } + + /* Now it'll check per-cpu tstamp map which doesn't have TID. */ + if (aggr_mode == LOCK_AGGR_TASK || aggr_mode == LOCK_AGGR_CGROUP) + return; + + total_cpus = cpu__max_cpu().cpu; + ts_fd = bpf_map__fd(skel->maps.tstamp_cpu); + + cpu_data = calloc(total_cpus, sizeof(*cpu_data)); + if (cpu_data == NULL) + return; + + prev_key = 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 = 0; i < total_cpus; i++) { + update_lock_stat(stat_fd, -1, end_ts, aggr_mode, + &cpu_data[i]); + } + +next: + prev_key = &key; + } + free(cpu_data); +} + int lock_contention_start(void) { skel->bss->enabled = 1; @@ -187,6 +300,7 @@ int lock_contention_start(void) int lock_contention_stop(void) { skel->bss->enabled = 0; + mark_end_timestamp(); return 0; } @@ -300,6 +414,8 @@ int lock_contention_read(struct lock_contention *con) if (stack_trace == NULL) return -1; + account_end_timestamp(con); + if (con->aggr_mode == LOCK_AGGR_TASK) { struct thread *idle = __machine__findnew_thread(machine, /*pid=*/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 = -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 = bpf_ktime_get_ns(); + return 0; +} + char LICENSE[] SEC("license") = "Dual BSD/GPL"; diff --git a/tools/perf/util/bpf_skel/lock_data.h b/tools/perf/util/bpf_skel/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.43.0.472.g3155946c3a-goog