xref: /openbmc/linux/tools/perf/util/comm.c (revision c23b8e7a)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "comm.h"
3 #include "util.h"
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <linux/refcount.h>
9 #include "rwsem.h"
10 
11 struct comm_str {
12 	char *str;
13 	struct rb_node rb_node;
14 	refcount_t refcnt;
15 };
16 
17 /* Should perhaps be moved to struct machine */
18 static struct rb_root comm_str_root;
19 static struct rw_semaphore comm_str_lock = {.lock = PTHREAD_RWLOCK_INITIALIZER,};
20 
21 static struct comm_str *comm_str__get(struct comm_str *cs)
22 {
23 	if (cs && refcount_inc_not_zero(&cs->refcnt))
24 		return cs;
25 
26 	return NULL;
27 }
28 
29 static void comm_str__put(struct comm_str *cs)
30 {
31 	if (cs && refcount_dec_and_test(&cs->refcnt)) {
32 		down_write(&comm_str_lock);
33 		rb_erase(&cs->rb_node, &comm_str_root);
34 		up_write(&comm_str_lock);
35 		zfree(&cs->str);
36 		free(cs);
37 	}
38 }
39 
40 static struct comm_str *comm_str__alloc(const char *str)
41 {
42 	struct comm_str *cs;
43 
44 	cs = zalloc(sizeof(*cs));
45 	if (!cs)
46 		return NULL;
47 
48 	cs->str = strdup(str);
49 	if (!cs->str) {
50 		free(cs);
51 		return NULL;
52 	}
53 
54 	refcount_set(&cs->refcnt, 1);
55 
56 	return cs;
57 }
58 
59 static
60 struct comm_str *__comm_str__findnew(const char *str, struct rb_root *root)
61 {
62 	struct rb_node **p = &root->rb_node;
63 	struct rb_node *parent = NULL;
64 	struct comm_str *iter, *new;
65 	int cmp;
66 
67 	while (*p != NULL) {
68 		parent = *p;
69 		iter = rb_entry(parent, struct comm_str, rb_node);
70 
71 		/*
72 		 * If we race with comm_str__put, iter->refcnt is 0
73 		 * and it will be removed within comm_str__put call
74 		 * shortly, ignore it in this search.
75 		 */
76 		cmp = strcmp(str, iter->str);
77 		if (!cmp && comm_str__get(iter))
78 			return iter;
79 
80 		if (cmp < 0)
81 			p = &(*p)->rb_left;
82 		else
83 			p = &(*p)->rb_right;
84 	}
85 
86 	new = comm_str__alloc(str);
87 	if (!new)
88 		return NULL;
89 
90 	rb_link_node(&new->rb_node, parent, p);
91 	rb_insert_color(&new->rb_node, root);
92 
93 	return new;
94 }
95 
96 static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
97 {
98 	struct comm_str *cs;
99 
100 	down_write(&comm_str_lock);
101 	cs = __comm_str__findnew(str, root);
102 	up_write(&comm_str_lock);
103 
104 	return cs;
105 }
106 
107 struct comm *comm__new(const char *str, u64 timestamp, bool exec)
108 {
109 	struct comm *comm = zalloc(sizeof(*comm));
110 
111 	if (!comm)
112 		return NULL;
113 
114 	comm->start = timestamp;
115 	comm->exec = exec;
116 
117 	comm->comm_str = comm_str__findnew(str, &comm_str_root);
118 	if (!comm->comm_str) {
119 		free(comm);
120 		return NULL;
121 	}
122 
123 	return comm;
124 }
125 
126 int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
127 {
128 	struct comm_str *new, *old = comm->comm_str;
129 
130 	new = comm_str__findnew(str, &comm_str_root);
131 	if (!new)
132 		return -ENOMEM;
133 
134 	comm_str__put(old);
135 	comm->comm_str = new;
136 	comm->start = timestamp;
137 	if (exec)
138 		comm->exec = true;
139 
140 	return 0;
141 }
142 
143 void comm__free(struct comm *comm)
144 {
145 	comm_str__put(comm->comm_str);
146 	free(comm);
147 }
148 
149 const char *comm__str(const struct comm *comm)
150 {
151 	return comm->comm_str->str;
152 }
153