hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/tools/perf/util/thread_map.c
....@@ -12,9 +12,10 @@
1212 #include "strlist.h"
1313 #include <string.h>
1414 #include <api/fs/fs.h>
15
+#include <linux/string.h>
16
+#include <linux/zalloc.h>
1517 #include "asm/bug.h"
1618 #include "thread_map.h"
17
-#include "util.h"
1819 #include "debug.h"
1920 #include "event.h"
2021
....@@ -27,34 +28,11 @@
2728 return 1;
2829 }
2930
30
-static void thread_map__reset(struct thread_map *map, int start, int nr)
31
+#define thread_map__alloc(__nr) perf_thread_map__realloc(NULL, __nr)
32
+
33
+struct perf_thread_map *thread_map__new_by_pid(pid_t pid)
3134 {
32
- size_t size = (nr - start) * sizeof(map->map[0]);
33
-
34
- memset(&map->map[start], 0, size);
35
- map->err_thread = -1;
36
-}
37
-
38
-static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
39
-{
40
- size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
41
- int start = map ? map->nr : 0;
42
-
43
- map = realloc(map, size);
44
- /*
45
- * We only realloc to add more items, let's reset new items.
46
- */
47
- if (map)
48
- thread_map__reset(map, start, nr);
49
-
50
- return map;
51
-}
52
-
53
-#define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
54
-
55
-struct thread_map *thread_map__new_by_pid(pid_t pid)
56
-{
57
- struct thread_map *threads;
35
+ struct perf_thread_map *threads;
5836 char name[256];
5937 int items;
6038 struct dirent **namelist = NULL;
....@@ -68,7 +46,7 @@
6846 threads = thread_map__alloc(items);
6947 if (threads != NULL) {
7048 for (i = 0; i < items; i++)
71
- thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
49
+ perf_thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
7250 threads->nr = items;
7351 refcount_set(&threads->refcnt, 1);
7452 }
....@@ -80,12 +58,12 @@
8058 return threads;
8159 }
8260
83
-struct thread_map *thread_map__new_by_tid(pid_t tid)
61
+struct perf_thread_map *thread_map__new_by_tid(pid_t tid)
8462 {
85
- struct thread_map *threads = thread_map__alloc(1);
63
+ struct perf_thread_map *threads = thread_map__alloc(1);
8664
8765 if (threads != NULL) {
88
- thread_map__set_pid(threads, 0, tid);
66
+ perf_thread_map__set_pid(threads, 0, tid);
8967 threads->nr = 1;
9068 refcount_set(&threads->refcnt, 1);
9169 }
....@@ -93,13 +71,13 @@
9371 return threads;
9472 }
9573
96
-static struct thread_map *__thread_map__new_all_cpus(uid_t uid)
74
+static struct perf_thread_map *__thread_map__new_all_cpus(uid_t uid)
9775 {
9876 DIR *proc;
9977 int max_threads = 32, items, i;
10078 char path[NAME_MAX + 1 + 6];
10179 struct dirent *dirent, **namelist = NULL;
102
- struct thread_map *threads = thread_map__alloc(max_threads);
80
+ struct perf_thread_map *threads = thread_map__alloc(max_threads);
10381
10482 if (threads == NULL)
10583 goto out;
....@@ -139,9 +117,9 @@
139117 }
140118
141119 if (grow) {
142
- struct thread_map *tmp;
120
+ struct perf_thread_map *tmp;
143121
144
- tmp = thread_map__realloc(threads, max_threads);
122
+ tmp = perf_thread_map__realloc(threads, max_threads);
145123 if (tmp == NULL)
146124 goto out_free_namelist;
147125
....@@ -149,8 +127,8 @@
149127 }
150128
151129 for (i = 0; i < items; i++) {
152
- thread_map__set_pid(threads, threads->nr + i,
153
- atoi(namelist[i]->d_name));
130
+ perf_thread_map__set_pid(threads, threads->nr + i,
131
+ atoi(namelist[i]->d_name));
154132 }
155133
156134 for (i = 0; i < items; i++)
....@@ -179,17 +157,17 @@
179157 goto out_closedir;
180158 }
181159
182
-struct thread_map *thread_map__new_all_cpus(void)
160
+struct perf_thread_map *thread_map__new_all_cpus(void)
183161 {
184162 return __thread_map__new_all_cpus(UINT_MAX);
185163 }
186164
187
-struct thread_map *thread_map__new_by_uid(uid_t uid)
165
+struct perf_thread_map *thread_map__new_by_uid(uid_t uid)
188166 {
189167 return __thread_map__new_all_cpus(uid);
190168 }
191169
192
-struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
170
+struct perf_thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
193171 {
194172 if (pid != -1)
195173 return thread_map__new_by_pid(pid);
....@@ -200,9 +178,9 @@
200178 return thread_map__new_by_tid(tid);
201179 }
202180
203
-static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
181
+static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
204182 {
205
- struct thread_map *threads = NULL, *nt;
183
+ struct perf_thread_map *threads = NULL, *nt;
206184 char name[256];
207185 int items, total_tasks = 0;
208186 struct dirent **namelist = NULL;
....@@ -232,14 +210,14 @@
232210 goto out_free_threads;
233211
234212 total_tasks += items;
235
- nt = thread_map__realloc(threads, total_tasks);
213
+ nt = perf_thread_map__realloc(threads, total_tasks);
236214 if (nt == NULL)
237215 goto out_free_namelist;
238216
239217 threads = nt;
240218
241219 for (i = 0; i < items; i++) {
242
- thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
220
+ perf_thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
243221 zfree(&namelist[i]);
244222 }
245223 threads->nr = total_tasks;
....@@ -262,21 +240,9 @@
262240 goto out;
263241 }
264242
265
-struct thread_map *thread_map__new_dummy(void)
243
+struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
266244 {
267
- struct thread_map *threads = thread_map__alloc(1);
268
-
269
- if (threads != NULL) {
270
- thread_map__set_pid(threads, 0, -1);
271
- threads->nr = 1;
272
- refcount_set(&threads->refcnt, 1);
273
- }
274
- return threads;
275
-}
276
-
277
-struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
278
-{
279
- struct thread_map *threads = NULL, *nt;
245
+ struct perf_thread_map *threads = NULL, *nt;
280246 int ntasks = 0;
281247 pid_t tid, prev_tid = INT_MAX;
282248 char *end_ptr;
....@@ -286,7 +252,7 @@
286252
287253 /* perf-stat expects threads to be generated even if tid not given */
288254 if (!tid_str)
289
- return thread_map__new_dummy();
255
+ return perf_thread_map__new_dummy();
290256
291257 slist = strlist__new(tid_str, &slist_config);
292258 if (!slist)
....@@ -303,13 +269,13 @@
303269 continue;
304270
305271 ntasks++;
306
- nt = thread_map__realloc(threads, ntasks);
272
+ nt = perf_thread_map__realloc(threads, ntasks);
307273
308274 if (nt == NULL)
309275 goto out_free_threads;
310276
311277 threads = nt;
312
- thread_map__set_pid(threads, ntasks - 1, tid);
278
+ perf_thread_map__set_pid(threads, ntasks - 1, tid);
313279 threads->nr = ntasks;
314280 }
315281 out:
....@@ -323,7 +289,7 @@
323289 goto out;
324290 }
325291
326
-struct thread_map *thread_map__new_str(const char *pid, const char *tid,
292
+struct perf_thread_map *thread_map__new_str(const char *pid, const char *tid,
327293 uid_t uid, bool all_threads)
328294 {
329295 if (pid)
....@@ -338,39 +304,13 @@
338304 return thread_map__new_by_tid_str(tid);
339305 }
340306
341
-static void thread_map__delete(struct thread_map *threads)
342
-{
343
- if (threads) {
344
- int i;
345
-
346
- WARN_ONCE(refcount_read(&threads->refcnt) != 0,
347
- "thread map refcnt unbalanced\n");
348
- for (i = 0; i < threads->nr; i++)
349
- free(thread_map__comm(threads, i));
350
- free(threads);
351
- }
352
-}
353
-
354
-struct thread_map *thread_map__get(struct thread_map *map)
355
-{
356
- if (map)
357
- refcount_inc(&map->refcnt);
358
- return map;
359
-}
360
-
361
-void thread_map__put(struct thread_map *map)
362
-{
363
- if (map && refcount_dec_and_test(&map->refcnt))
364
- thread_map__delete(map);
365
-}
366
-
367
-size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
307
+size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp)
368308 {
369309 int i;
370310 size_t printed = fprintf(fp, "%d thread%s: ",
371311 threads->nr, threads->nr > 1 ? "s" : "");
372312 for (i = 0; i < threads->nr; ++i)
373
- printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
313
+ printed += fprintf(fp, "%s%d", i ? ", " : "", perf_thread_map__pid(threads, i));
374314
375315 return printed + fprintf(fp, "\n");
376316 }
....@@ -392,16 +332,16 @@
392332 * mark the end of the string.
393333 */
394334 (*comm)[size] = 0;
395
- rtrim(*comm);
335
+ strim(*comm);
396336 }
397337
398338 free(path);
399339 return err;
400340 }
401341
402
-static void comm_init(struct thread_map *map, int i)
342
+static void comm_init(struct perf_thread_map *map, int i)
403343 {
404
- pid_t pid = thread_map__pid(map, i);
344
+ pid_t pid = perf_thread_map__pid(map, i);
405345 char *comm = NULL;
406346
407347 /* dummy pid comm initialization */
....@@ -420,7 +360,7 @@
420360 map->map[i].comm = comm;
421361 }
422362
423
-void thread_map__read_comms(struct thread_map *threads)
363
+void thread_map__read_comms(struct perf_thread_map *threads)
424364 {
425365 int i;
426366
....@@ -428,24 +368,24 @@
428368 comm_init(threads, i);
429369 }
430370
431
-static void thread_map__copy_event(struct thread_map *threads,
432
- struct thread_map_event *event)
371
+static void thread_map__copy_event(struct perf_thread_map *threads,
372
+ struct perf_record_thread_map *event)
433373 {
434374 unsigned i;
435375
436376 threads->nr = (int) event->nr;
437377
438378 for (i = 0; i < event->nr; i++) {
439
- thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
379
+ perf_thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
440380 threads->map[i].comm = strndup(event->entries[i].comm, 16);
441381 }
442382
443383 refcount_set(&threads->refcnt, 1);
444384 }
445385
446
-struct thread_map *thread_map__new_event(struct thread_map_event *event)
386
+struct perf_thread_map *thread_map__new_event(struct perf_record_thread_map *event)
447387 {
448
- struct thread_map *threads;
388
+ struct perf_thread_map *threads;
449389
450390 threads = thread_map__alloc(event->nr);
451391 if (threads)
....@@ -454,7 +394,7 @@
454394 return threads;
455395 }
456396
457
-bool thread_map__has(struct thread_map *threads, pid_t pid)
397
+bool thread_map__has(struct perf_thread_map *threads, pid_t pid)
458398 {
459399 int i;
460400
....@@ -466,7 +406,7 @@
466406 return false;
467407 }
468408
469
-int thread_map__remove(struct thread_map *threads, int idx)
409
+int thread_map__remove(struct perf_thread_map *threads, int idx)
470410 {
471411 int i;
472412
....@@ -479,7 +419,7 @@
479419 /*
480420 * Free the 'idx' item and shift the rest up.
481421 */
482
- free(threads->map[idx].comm);
422
+ zfree(&threads->map[idx].comm);
483423
484424 for (i = idx; i < threads->nr - 1; i++)
485425 threads->map[i] = threads->map[i + 1];