diff options
| -rw-r--r-- | parse-events.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/parse-events.c b/parse-events.c index 424b367..818be40 100644 --- a/parse-events.c +++ b/parse-events.c | |||
| @@ -135,7 +135,6 @@ int pevent_pid_is_registered(struct pevent *pevent, int pid) | |||
| 135 | if (!pid) | 135 | if (!pid) |
| 136 | return 1; | 136 | return 1; |
| 137 | 137 | ||
| 138 | |||
| 139 | if (!pevent->cmdlines) | 138 | if (!pevent->cmdlines) |
| 140 | cmdline_init(pevent); | 139 | cmdline_init(pevent); |
| 141 | 140 | ||
| @@ -149,6 +148,46 @@ int pevent_pid_is_registered(struct pevent *pevent, int pid) | |||
| 149 | return 0; | 148 | return 0; |
| 150 | } | 149 | } |
| 151 | 150 | ||
| 151 | /* | ||
| 152 | * If the command lines have been converted to an array, then | ||
| 153 | * we must add this pid. This is much slower than when cmdlines | ||
| 154 | * are added before the array is initialized. | ||
| 155 | */ | ||
| 156 | static int add_new_comm(struct pevent *pevent, char *comm, int pid) | ||
| 157 | { | ||
| 158 | struct cmdline *cmdlines = pevent->cmdlines; | ||
| 159 | const struct cmdline *cmdline; | ||
| 160 | struct cmdline key; | ||
| 161 | |||
| 162 | if (!pid) | ||
| 163 | return 0; | ||
| 164 | |||
| 165 | /* avoid duplicates */ | ||
| 166 | key.pid = pid; | ||
| 167 | |||
| 168 | cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, | ||
| 169 | sizeof(*pevent->cmdlines), cmdline_cmp); | ||
| 170 | if (cmdline) { | ||
| 171 | errno = EEXIST; | ||
| 172 | return -1; | ||
| 173 | } | ||
| 174 | |||
| 175 | cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1)); | ||
| 176 | if (!cmdlines) { | ||
| 177 | errno = ENOMEM; | ||
| 178 | return -1; | ||
| 179 | } | ||
| 180 | |||
| 181 | cmdlines[pevent->cmdline_count].pid = pid; | ||
| 182 | cmdlines[pevent->cmdline_count].comm = comm; | ||
| 183 | pevent->cmdline_count++; | ||
| 184 | |||
| 185 | qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp); | ||
| 186 | pevent->cmdlines = cmdlines; | ||
| 187 | |||
| 188 | return 0; | ||
| 189 | } | ||
| 190 | |||
| 152 | /** | 191 | /** |
| 153 | * pevent_register_comm - register a pid / comm mapping | 192 | * pevent_register_comm - register a pid / comm mapping |
| 154 | * @pevent: handle for the pevent | 193 | * @pevent: handle for the pevent |
| @@ -163,6 +202,9 @@ int pevent_register_comm(struct pevent *pevent, char *comm, int pid) | |||
| 163 | { | 202 | { |
| 164 | struct cmdline_list *item; | 203 | struct cmdline_list *item; |
| 165 | 204 | ||
| 205 | if (pevent->cmdlines) | ||
| 206 | return add_new_comm(pevent, comm, pid); | ||
| 207 | |||
| 166 | item = malloc_or_die(sizeof(*item)); | 208 | item = malloc_or_die(sizeof(*item)); |
| 167 | item->comm = comm; | 209 | item->comm = comm; |
| 168 | item->pid = pid; | 210 | item->pid = pid; |
