Revert "perf augmented_syscalls: Drop 'write', 'poll' for testing without self pid...
[linux/fpc-iii.git] / tools / perf / util / comm.c
blob31279a7bd919616370d9bcececb34dc0efef271a
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"
11 struct comm_str {
12 char *str;
13 struct rb_node rb_node;
14 refcount_t refcnt;
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,};
21 static struct comm_str *comm_str__get(struct comm_str *cs)
23 if (cs && refcount_inc_not_zero(&cs->refcnt))
24 return cs;
26 return NULL;
29 static void comm_str__put(struct comm_str *cs)
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);
40 static struct comm_str *comm_str__alloc(const char *str)
42 struct comm_str *cs;
44 cs = zalloc(sizeof(*cs));
45 if (!cs)
46 return NULL;
48 cs->str = strdup(str);
49 if (!cs->str) {
50 free(cs);
51 return NULL;
54 refcount_set(&cs->refcnt, 1);
56 return cs;
59 static
60 struct comm_str *__comm_str__findnew(const char *str, struct rb_root *root)
62 struct rb_node **p = &root->rb_node;
63 struct rb_node *parent = NULL;
64 struct comm_str *iter, *new;
65 int cmp;
67 while (*p != NULL) {
68 parent = *p;
69 iter = rb_entry(parent, struct comm_str, rb_node);
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.
76 cmp = strcmp(str, iter->str);
77 if (!cmp && comm_str__get(iter))
78 return iter;
80 if (cmp < 0)
81 p = &(*p)->rb_left;
82 else
83 p = &(*p)->rb_right;
86 new = comm_str__alloc(str);
87 if (!new)
88 return NULL;
90 rb_link_node(&new->rb_node, parent, p);
91 rb_insert_color(&new->rb_node, root);
93 return new;
96 static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
98 struct comm_str *cs;
100 down_write(&comm_str_lock);
101 cs = __comm_str__findnew(str, root);
102 up_write(&comm_str_lock);
104 return cs;
107 struct comm *comm__new(const char *str, u64 timestamp, bool exec)
109 struct comm *comm = zalloc(sizeof(*comm));
111 if (!comm)
112 return NULL;
114 comm->start = timestamp;
115 comm->exec = exec;
117 comm->comm_str = comm_str__findnew(str, &comm_str_root);
118 if (!comm->comm_str) {
119 free(comm);
120 return NULL;
123 return comm;
126 int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
128 struct comm_str *new, *old = comm->comm_str;
130 new = comm_str__findnew(str, &comm_str_root);
131 if (!new)
132 return -ENOMEM;
134 comm_str__put(old);
135 comm->comm_str = new;
136 comm->start = timestamp;
137 if (exec)
138 comm->exec = true;
140 return 0;
143 void comm__free(struct comm *comm)
145 comm_str__put(comm->comm_str);
146 free(comm);
149 const char *comm__str(const struct comm *comm)
151 return comm->comm_str->str;