aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/perf/tests/builtin-test.c4
-rw-r--r--tools/perf/tests/tests.h1
-rw-r--r--tools/perf/tests/thread-map.c44
-rw-r--r--tools/perf/util/thread_map.c22
-rw-r--r--tools/perf/util/thread_map.h1
5 files changed, 72 insertions, 0 deletions
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 23605202d4a1..a77dcc0d24e3 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -186,6 +186,10 @@ static struct test generic_tests[] = {
186 .func = test__thread_map_synthesize, 186 .func = test__thread_map_synthesize,
187 }, 187 },
188 { 188 {
189 .desc = "Remove thread map",
190 .func = test__thread_map_remove,
191 },
192 {
189 .desc = "Synthesize cpu map", 193 .desc = "Synthesize cpu map",
190 .func = test__cpu_map_synthesize, 194 .func = test__cpu_map_synthesize,
191 }, 195 },
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 0d7b251305af..a512f0c8ff5b 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -80,6 +80,7 @@ const char *test__bpf_subtest_get_desc(int subtest);
80int test__bpf_subtest_get_nr(void); 80int test__bpf_subtest_get_nr(void);
81int test_session_topology(int subtest); 81int test_session_topology(int subtest);
82int test__thread_map_synthesize(int subtest); 82int test__thread_map_synthesize(int subtest);
83int test__thread_map_remove(int subtest);
83int test__cpu_map_synthesize(int subtest); 84int test__cpu_map_synthesize(int subtest);
84int test__synthesize_stat_config(int subtest); 85int test__synthesize_stat_config(int subtest);
85int test__synthesize_stat(int subtest); 86int test__synthesize_stat(int subtest);
diff --git a/tools/perf/tests/thread-map.c b/tools/perf/tests/thread-map.c
index cee2a2cdc933..a4a4b4625ac3 100644
--- a/tools/perf/tests/thread-map.c
+++ b/tools/perf/tests/thread-map.c
@@ -1,3 +1,4 @@
1#include <stdlib.h>
1#include <sys/types.h> 2#include <sys/types.h>
2#include <unistd.h> 3#include <unistd.h>
3#include <sys/prctl.h> 4#include <sys/prctl.h>
@@ -93,3 +94,46 @@ int test__thread_map_synthesize(int subtest __maybe_unused)
93 94
94 return 0; 95 return 0;
95} 96}
97
98int test__thread_map_remove(int subtest __maybe_unused)
99{
100 struct thread_map *threads;
101 char *str;
102 int i;
103
104 TEST_ASSERT_VAL("failed to allocate map string",
105 asprintf(&str, "%d,%d", getpid(), getppid()) >= 0);
106
107 threads = thread_map__new_str(str, NULL, 0);
108
109 TEST_ASSERT_VAL("failed to allocate thread_map",
110 threads);
111
112 if (verbose)
113 thread_map__fprintf(threads, stderr);
114
115 TEST_ASSERT_VAL("failed to remove thread",
116 !thread_map__remove(threads, 0));
117
118 TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1);
119
120 if (verbose)
121 thread_map__fprintf(threads, stderr);
122
123 TEST_ASSERT_VAL("failed to remove thread",
124 !thread_map__remove(threads, 0));
125
126 TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0);
127
128 if (verbose)
129 thread_map__fprintf(threads, stderr);
130
131 TEST_ASSERT_VAL("failed to not remove thread",
132 thread_map__remove(threads, 0));
133
134 for (i = 0; i < threads->nr; i++)
135 free(threads->map[i].comm);
136
137 free(threads);
138 return 0;
139}
diff --git a/tools/perf/util/thread_map.c b/tools/perf/util/thread_map.c
index 40585f5b7027..f9eab200fd75 100644
--- a/tools/perf/util/thread_map.c
+++ b/tools/perf/util/thread_map.c
@@ -448,3 +448,25 @@ bool thread_map__has(struct thread_map *threads, pid_t pid)
448 448
449 return false; 449 return false;
450} 450}
451
452int thread_map__remove(struct thread_map *threads, int idx)
453{
454 int i;
455
456 if (threads->nr < 1)
457 return -EINVAL;
458
459 if (idx >= threads->nr)
460 return -EINVAL;
461
462 /*
463 * Free the 'idx' item and shift the rest up.
464 */
465 free(threads->map[idx].comm);
466
467 for (i = idx; i < threads->nr - 1; i++)
468 threads->map[i] = threads->map[i + 1];
469
470 threads->nr--;
471 return 0;
472}
diff --git a/tools/perf/util/thread_map.h b/tools/perf/util/thread_map.h
index bd3b971588da..ea0ef08c6303 100644
--- a/tools/perf/util/thread_map.h
+++ b/tools/perf/util/thread_map.h
@@ -58,4 +58,5 @@ static inline char *thread_map__comm(struct thread_map *map, int thread)
58 58
59void thread_map__read_comms(struct thread_map *threads); 59void thread_map__read_comms(struct thread_map *threads);
60bool thread_map__has(struct thread_map *threads, pid_t pid); 60bool thread_map__has(struct thread_map *threads, pid_t pid);
61int thread_map__remove(struct thread_map *threads, int idx);
61#endif /* __PERF_THREAD_MAP_H */ 62#endif /* __PERF_THREAD_MAP_H */