]> git.itanic.dy.fi Git - linux-stable/commitdiff
perf lock contention: Prepare to handle cgroups
authorNamhyung Kim <namhyung@kernel.org>
Wed, 6 Sep 2023 17:49:00 +0000 (10:49 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 12 Sep 2023 20:32:00 +0000 (17:32 -0300)
Save cgroup info and display cgroup names if requested.  This is a
preparation for the next patch.

Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Hao Luo <haoluo@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <song@kernel.org>
Cc: bpf@vger.kernel.org
Link: https://lore.kernel.org/r/20230906174903.346486-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-lock.c
tools/perf/util/bpf_lock_contention.c
tools/perf/util/lock-contention.h

index b141f2134274061039115ae7dd2a44ed7431b8b8..06430980dfd75b86dfeb9c2c6b5178d4d52fdb6e 100644 (file)
@@ -2040,6 +2040,7 @@ static int __cmd_contention(int argc, const char **argv)
                .filters = &filters,
                .save_callstack = needs_callstack(),
                .owner = show_lock_owner,
+               .cgroups = RB_ROOT,
        };
 
        lockhash_table = calloc(LOCKHASH_SIZE, sizeof(*lockhash_table));
@@ -2158,7 +2159,7 @@ static int __cmd_contention(int argc, const char **argv)
 out_delete:
        lock_filter_finish();
        evlist__delete(con.evlist);
-       lock_contention_finish();
+       lock_contention_finish(&con);
        perf_session__delete(session);
        zfree(&lockhash_table);
        return err;
index e7dddf0127bce4d35df5abcee13cd693c3949b1f..c6bd7c9b2d571eecb78f4f1d4de8b11b39d35137 100644 (file)
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include "util/cgroup.h"
 #include "util/debug.h"
 #include "util/evlist.h"
 #include "util/machine.h"
@@ -151,6 +152,10 @@ int lock_contention_prepare(struct lock_contention *con)
        skel->bss->needs_callstack = con->save_callstack;
        skel->bss->lock_owner = con->owner;
 
+       if (con->use_cgroup) {
+               read_all_cgroups(&con->cgroups);
+       }
+
        bpf_program__set_autoload(skel->progs.collect_lock_syms, false);
 
        lock_contention_bpf__attach(skel);
@@ -222,6 +227,17 @@ static const char *lock_contention_get_name(struct lock_contention *con,
                return "";
        }
 
+       if (con->use_cgroup) {
+               u64 cgrp_id = key->lock_addr;
+               struct cgroup *cgrp = __cgroup__find(&con->cgroups, cgrp_id);
+
+               if (cgrp)
+                       return cgrp->name;
+
+               snprintf(name_buf, sizeof(name_buf), "cgroup:%lu", cgrp_id);
+               return name_buf;
+       }
+
        /* LOCK_AGGR_CALLER: skip lock internal functions */
        while (machine__is_lock_function(machine, stack_trace[idx]) &&
               idx < con->max_stack - 1)
@@ -364,12 +380,20 @@ int lock_contention_read(struct lock_contention *con)
        return err;
 }
 
-int lock_contention_finish(void)
+int lock_contention_finish(struct lock_contention *con)
 {
        if (skel) {
                skel->bss->enabled = 0;
                lock_contention_bpf__destroy(skel);
        }
 
+       while (!RB_EMPTY_ROOT(&con->cgroups)) {
+               struct rb_node *node = rb_first(&con->cgroups);
+               struct cgroup *cgrp = rb_entry(node, struct cgroup, node);
+
+               rb_erase(node, &con->cgroups);
+               cgroup__put(cgrp);
+       }
+
        return 0;
 }
index fa16532c971cb24d86ad970d074f9fd211129045..70423966d7782be73bea75cd98b41f0579c13f09 100644 (file)
@@ -136,6 +136,7 @@ struct lock_contention {
        struct hlist_head *result;
        struct lock_filter *filters;
        struct lock_contention_fails fails;
+       struct rb_root cgroups;
        unsigned long map_nr_entries;
        int max_stack;
        int stack_skip;
@@ -143,6 +144,7 @@ struct lock_contention {
        int owner;
        int nr_filtered;
        bool save_callstack;
+       bool use_cgroup;
 };
 
 #ifdef HAVE_BPF_SKEL
@@ -151,7 +153,7 @@ int lock_contention_prepare(struct lock_contention *con);
 int lock_contention_start(void);
 int lock_contention_stop(void);
 int lock_contention_read(struct lock_contention *con);
-int lock_contention_finish(void);
+int lock_contention_finish(struct lock_contention *con);
 
 #else  /* !HAVE_BPF_SKEL */
 
@@ -162,7 +164,10 @@ static inline int lock_contention_prepare(struct lock_contention *con __maybe_un
 
 static inline int lock_contention_start(void) { return 0; }
 static inline int lock_contention_stop(void) { return 0; }
-static inline int lock_contention_finish(void) { return 0; }
+static inline int lock_contention_finish(struct lock_contention *con __maybe_unused)
+{
+       return 0;
+}
 
 static inline int lock_contention_read(struct lock_contention *con __maybe_unused)
 {