perf lock: Add lock contention tracepoints record support
authorNamhyung Kim <namhyung@kernel.org>
Wed, 15 Jun 2022 16:32:19 +0000 (09:32 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 12 Jul 2022 12:55:22 +0000 (09:55 -0300)
When LOCKDEP and LOCK_STAT events are not available, it falls back to
record the new lock contention tracepoints.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Waiman Long <longman@redhat.com>
Cc: Will Deacon <will@kernel.org>
Link: https://lore.kernel.org/r/20220615163222.1275500-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-lock.c

index 2337b09dd2cd1da9a2a98f213302d5fd29fe0c8f..9e3b90cac505f7584c27ebf3eb159dccffaf868c 100644 (file)
@@ -516,17 +516,29 @@ alloc_failed:
 }
 
 struct trace_lock_handler {
+       /* it's used on CONFIG_LOCKDEP */
        int (*acquire_event)(struct evsel *evsel,
                             struct perf_sample *sample);
 
+       /* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
        int (*acquired_event)(struct evsel *evsel,
                              struct perf_sample *sample);
 
+       /* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
        int (*contended_event)(struct evsel *evsel,
                               struct perf_sample *sample);
 
+       /* it's used on CONFIG_LOCKDEP */
        int (*release_event)(struct evsel *evsel,
                             struct perf_sample *sample);
+
+       /* it's used when CONFIG_LOCKDEP is off */
+       int (*contention_begin_event)(struct evsel *evsel,
+                                     struct perf_sample *sample);
+
+       /* it's used when CONFIG_LOCKDEP is off */
+       int (*contention_end_event)(struct evsel *evsel,
+                                   struct perf_sample *sample);
 };
 
 static struct lock_seq_stat *get_seq(struct thread_stat *ts, u64 addr)
@@ -854,6 +866,20 @@ static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *
        return 0;
 }
 
+static int evsel__process_contention_begin(struct evsel *evsel, struct perf_sample *sample)
+{
+       if (trace_handler->contention_begin_event)
+               return trace_handler->contention_begin_event(evsel, sample);
+       return 0;
+}
+
+static int evsel__process_contention_end(struct evsel *evsel, struct perf_sample *sample)
+{
+       if (trace_handler->contention_end_event)
+               return trace_handler->contention_end_event(evsel, sample);
+       return 0;
+}
+
 static void print_bad_events(int bad, int total)
 {
        /* Output for debug, this have to be removed */
@@ -1062,6 +1088,11 @@ static const struct evsel_str_handler lock_tracepoints[] = {
        { "lock:lock_release",   evsel__process_lock_release,   }, /* CONFIG_LOCKDEP */
 };
 
+static const struct evsel_str_handler contention_tracepoints[] = {
+       { "lock:contention_begin", evsel__process_contention_begin, },
+       { "lock:contention_end",   evsel__process_contention_end,   },
+};
+
 static bool force;
 
 static int __cmd_report(bool display_info)
@@ -1125,20 +1156,41 @@ static int __cmd_record(int argc, const char **argv)
                "record", "-R", "-m", "1024", "-c", "1", "--synth", "task",
        };
        unsigned int rec_argc, i, j, ret;
+       unsigned int nr_tracepoints;
        const char **rec_argv;
+       bool has_lock_stat = true;
 
        for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
                if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
-                               pr_err("tracepoint %s is not enabled. "
-                                      "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
-                                      lock_tracepoints[i].name);
-                               return 1;
+                       pr_debug("tracepoint %s is not enabled. "
+                                "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
+                                lock_tracepoints[i].name);
+                       has_lock_stat = false;
+                       break;
+               }
+       }
+
+       if (has_lock_stat)
+               goto setup_args;
+
+       for (i = 0; i < ARRAY_SIZE(contention_tracepoints); i++) {
+               if (!is_valid_tracepoint(contention_tracepoints[i].name)) {
+                       pr_err("tracepoint %s is not enabled.\n",
+                              contention_tracepoints[i].name);
+                       return 1;
                }
        }
 
+setup_args:
        rec_argc = ARRAY_SIZE(record_args) + argc - 1;
+
+       if (has_lock_stat)
+               nr_tracepoints = ARRAY_SIZE(lock_tracepoints);
+       else
+               nr_tracepoints = ARRAY_SIZE(contention_tracepoints);
+
        /* factor of 2 is for -e in front of each tracepoint */
-       rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
+       rec_argc += 2 * nr_tracepoints;
 
        rec_argv = calloc(rec_argc + 1, sizeof(char *));
        if (!rec_argv)
@@ -1147,9 +1199,19 @@ static int __cmd_record(int argc, const char **argv)
        for (i = 0; i < ARRAY_SIZE(record_args); i++)
                rec_argv[i] = strdup(record_args[i]);
 
-       for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
+       for (j = 0; j < nr_tracepoints; j++) {
+               const char *ev_name;
+
+               if (has_lock_stat)
+                       ev_name = strdup(lock_tracepoints[j].name);
+               else
+                       ev_name = strdup(contention_tracepoints[j].name);
+
+               if (!ev_name)
+                       return -ENOMEM;
+
                rec_argv[i++] = "-e";
-               rec_argv[i++] = strdup(lock_tracepoints[j].name);
+               rec_argv[i++] = ev_name;
        }
 
        for (j = 1; j < (unsigned int)argc; j++, i++)