perf lock: Make rb_tree helper functions generic
authorChun-Tse Shao <ctshao@google.com>
Thu, 27 Feb 2025 00:28:55 +0000 (16:28 -0800)
committerNamhyung Kim <namhyung@kernel.org>
Fri, 28 Feb 2025 08:29:37 +0000 (00:29 -0800)
The rb_tree helper functions can be reused for parsing `owner_lock_stat`
into rb tree for sorting.

Signed-off-by: Chun-Tse Shao <ctshao@google.com>
Tested-by: Athira Rajeev <atrajeev@linux.ibm.com>
Link: https://lore.kernel.org/r/20250227003359.732948-4-ctshao@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/builtin-lock.c

index 5d405cd8e696d21b3e0c70f2b74bcde12f83d6e7..9bebc186286f586fc1364fbb10a95ac49184921a 100644 (file)
@@ -418,16 +418,13 @@ static void combine_lock_stats(struct lock_stat *st)
        rb_insert_color(&st->rb, &sorted);
 }
 
-static void insert_to_result(struct lock_stat *st,
-                            int (*bigger)(struct lock_stat *, struct lock_stat *))
+static void insert_to(struct rb_root *rr, struct lock_stat *st,
+                     int (*bigger)(struct lock_stat *, struct lock_stat *))
 {
-       struct rb_node **rb = &result.rb_node;
+       struct rb_node **rb = &rr->rb_node;
        struct rb_node *parent = NULL;
        struct lock_stat *p;
 
-       if (combine_locks && st->combined)
-               return;
-
        while (*rb) {
                p = container_of(*rb, struct lock_stat, rb);
                parent = *rb;
@@ -439,13 +436,21 @@ static void insert_to_result(struct lock_stat *st,
        }
 
        rb_link_node(&st->rb, parent, rb);
-       rb_insert_color(&st->rb, &result);
+       rb_insert_color(&st->rb, rr);
 }
 
-/* returns left most element of result, and erase it */
-static struct lock_stat *pop_from_result(void)
+static inline void insert_to_result(struct lock_stat *st,
+                                   int (*bigger)(struct lock_stat *,
+                                                 struct lock_stat *))
+{
+       if (combine_locks && st->combined)
+               return;
+       insert_to(&result, st, bigger);
+}
+
+static inline struct lock_stat *pop_from(struct rb_root *rr)
 {
-       struct rb_node *node = result.rb_node;
+       struct rb_node *node = rr->rb_node;
 
        if (!node)
                return NULL;
@@ -453,8 +458,15 @@ static struct lock_stat *pop_from_result(void)
        while (node->rb_left)
                node = node->rb_left;
 
-       rb_erase(node, &result);
+       rb_erase(node, rr);
        return container_of(node, struct lock_stat, rb);
+
+}
+
+/* returns left most element of result, and erase it */
+static struct lock_stat *pop_from_result(void)
+{
+       return pop_from(&result);
 }
 
 struct trace_lock_handler {