Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <perf/threadmap.h>
0003 #include <stdlib.h>
0004 #include <linux/refcount.h>
0005 #include <internal/threadmap.h>
0006 #include <string.h>
0007 #include <asm/bug.h>
0008 #include <stdio.h>
0009 
0010 static void perf_thread_map__reset(struct perf_thread_map *map, int start, int nr)
0011 {
0012     size_t size = (nr - start) * sizeof(map->map[0]);
0013 
0014     memset(&map->map[start], 0, size);
0015     map->err_thread = -1;
0016 }
0017 
0018 struct perf_thread_map *perf_thread_map__realloc(struct perf_thread_map *map, int nr)
0019 {
0020     size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
0021     int start = map ? map->nr : 0;
0022 
0023     map = realloc(map, size);
0024     /*
0025      * We only realloc to add more items, let's reset new items.
0026      */
0027     if (map)
0028         perf_thread_map__reset(map, start, nr);
0029 
0030     return map;
0031 }
0032 
0033 #define thread_map__alloc(__nr) perf_thread_map__realloc(NULL, __nr)
0034 
0035 void perf_thread_map__set_pid(struct perf_thread_map *map, int idx, pid_t pid)
0036 {
0037     map->map[idx].pid = pid;
0038 }
0039 
0040 char *perf_thread_map__comm(struct perf_thread_map *map, int idx)
0041 {
0042     return map->map[idx].comm;
0043 }
0044 
0045 struct perf_thread_map *perf_thread_map__new_array(int nr_threads, pid_t *array)
0046 {
0047     struct perf_thread_map *threads = thread_map__alloc(nr_threads);
0048     int i;
0049 
0050     if (!threads)
0051         return NULL;
0052 
0053     for (i = 0; i < nr_threads; i++)
0054         perf_thread_map__set_pid(threads, i, array ? array[i] : -1);
0055 
0056     threads->nr = nr_threads;
0057     refcount_set(&threads->refcnt, 1);
0058 
0059     return threads;
0060 }
0061 
0062 struct perf_thread_map *perf_thread_map__new_dummy(void)
0063 {
0064     return perf_thread_map__new_array(1, NULL);
0065 }
0066 
0067 static void perf_thread_map__delete(struct perf_thread_map *threads)
0068 {
0069     if (threads) {
0070         int i;
0071 
0072         WARN_ONCE(refcount_read(&threads->refcnt) != 0,
0073               "thread map refcnt unbalanced\n");
0074         for (i = 0; i < threads->nr; i++)
0075             free(perf_thread_map__comm(threads, i));
0076         free(threads);
0077     }
0078 }
0079 
0080 struct perf_thread_map *perf_thread_map__get(struct perf_thread_map *map)
0081 {
0082     if (map)
0083         refcount_inc(&map->refcnt);
0084     return map;
0085 }
0086 
0087 void perf_thread_map__put(struct perf_thread_map *map)
0088 {
0089     if (map && refcount_dec_and_test(&map->refcnt))
0090         perf_thread_map__delete(map);
0091 }
0092 
0093 int perf_thread_map__nr(struct perf_thread_map *threads)
0094 {
0095     return threads ? threads->nr : 1;
0096 }
0097 
0098 pid_t perf_thread_map__pid(struct perf_thread_map *map, int idx)
0099 {
0100     return map->map[idx].pid;
0101 }