Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 1 | #include "comm.h" |
| 2 | #include "util.h" |
| 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 5 | #include <linux/atomic.h> |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 6 | |
| 7 | struct comm_str { |
| 8 | char *str; |
| 9 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 10 | atomic_t refcnt; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 11 | }; |
| 12 | |
| 13 | /* Should perhaps be moved to struct machine */ |
| 14 | static struct rb_root comm_str_root; |
| 15 | |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 16 | static struct comm_str *comm_str__get(struct comm_str *cs) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 17 | { |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 18 | if (cs) |
| 19 | atomic_inc(&cs->refcnt); |
| 20 | return cs; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static void comm_str__put(struct comm_str *cs) |
| 24 | { |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 25 | if (cs && atomic_dec_and_test(&cs->refcnt)) { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 26 | rb_erase(&cs->rb_node, &comm_str_root); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 27 | zfree(&cs->str); |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 28 | free(cs); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static struct comm_str *comm_str__alloc(const char *str) |
| 33 | { |
| 34 | struct comm_str *cs; |
| 35 | |
| 36 | cs = zalloc(sizeof(*cs)); |
| 37 | if (!cs) |
| 38 | return NULL; |
| 39 | |
| 40 | cs->str = strdup(str); |
| 41 | if (!cs->str) { |
| 42 | free(cs); |
| 43 | return NULL; |
| 44 | } |
| 45 | |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 46 | atomic_set(&cs->refcnt, 0); |
| 47 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 48 | return cs; |
| 49 | } |
| 50 | |
| 51 | static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root) |
| 52 | { |
| 53 | struct rb_node **p = &root->rb_node; |
| 54 | struct rb_node *parent = NULL; |
| 55 | struct comm_str *iter, *new; |
| 56 | int cmp; |
| 57 | |
| 58 | while (*p != NULL) { |
| 59 | parent = *p; |
| 60 | iter = rb_entry(parent, struct comm_str, rb_node); |
| 61 | |
| 62 | cmp = strcmp(str, iter->str); |
| 63 | if (!cmp) |
| 64 | return iter; |
| 65 | |
| 66 | if (cmp < 0) |
| 67 | p = &(*p)->rb_left; |
| 68 | else |
| 69 | p = &(*p)->rb_right; |
| 70 | } |
| 71 | |
| 72 | new = comm_str__alloc(str); |
| 73 | if (!new) |
| 74 | return NULL; |
| 75 | |
| 76 | rb_link_node(&new->rb_node, parent, p); |
| 77 | rb_insert_color(&new->rb_node, root); |
| 78 | |
| 79 | return new; |
| 80 | } |
| 81 | |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 82 | struct comm *comm__new(const char *str, u64 timestamp, bool exec) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 83 | { |
| 84 | struct comm *comm = zalloc(sizeof(*comm)); |
| 85 | |
| 86 | if (!comm) |
| 87 | return NULL; |
| 88 | |
| 89 | comm->start = timestamp; |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 90 | comm->exec = exec; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 91 | |
| 92 | comm->comm_str = comm_str__findnew(str, &comm_str_root); |
| 93 | if (!comm->comm_str) { |
| 94 | free(comm); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | comm_str__get(comm->comm_str); |
| 99 | |
| 100 | return comm; |
| 101 | } |
| 102 | |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 103 | int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec) |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 104 | { |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 105 | struct comm_str *new, *old = comm->comm_str; |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 106 | |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 107 | new = comm_str__findnew(str, &comm_str_root); |
| 108 | if (!new) |
| 109 | return -ENOMEM; |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 110 | |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 111 | comm_str__get(new); |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 112 | comm_str__put(old); |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 113 | comm->comm_str = new; |
| 114 | comm->start = timestamp; |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 115 | if (exec) |
| 116 | comm->exec = true; |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 117 | |
| 118 | return 0; |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 121 | void comm__free(struct comm *comm) |
| 122 | { |
| 123 | comm_str__put(comm->comm_str); |
| 124 | free(comm); |
| 125 | } |
| 126 | |
| 127 | const char *comm__str(const struct comm *comm) |
| 128 | { |
| 129 | return comm->comm_str->str; |
| 130 | } |