Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 2 | #include <stdlib.h> |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 3 | #include <sys/types.h> |
| 4 | #include <unistd.h> |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 5 | #include <sys/prctl.h> |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 6 | #include "tests.h" |
| 7 | #include "thread_map.h" |
| 8 | #include "debug.h" |
| 9 | |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 10 | #define NAME (const char *) "perf" |
| 11 | #define NAMEUL (unsigned long) NAME |
| 12 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 13 | int test__thread_map(struct test *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 14 | { |
| 15 | struct thread_map *map; |
| 16 | |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 17 | TEST_ASSERT_VAL("failed to set process name", |
| 18 | !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); |
| 19 | |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 20 | /* test map on current pid */ |
| 21 | map = thread_map__new_by_pid(getpid()); |
| 22 | TEST_ASSERT_VAL("failed to alloc map", map); |
| 23 | |
| 24 | thread_map__read_comms(map); |
| 25 | |
| 26 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
| 27 | TEST_ASSERT_VAL("wrong pid", |
| 28 | thread_map__pid(map, 0) == getpid()); |
| 29 | TEST_ASSERT_VAL("wrong comm", |
| 30 | thread_map__comm(map, 0) && |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 31 | !strcmp(thread_map__comm(map, 0), NAME)); |
Jiri Olsa | 35318d2 | 2015-07-21 14:31:21 +0200 | [diff] [blame] | 32 | TEST_ASSERT_VAL("wrong refcnt", |
Elena Reshetova | 364fed3 | 2017-02-21 17:35:03 +0200 | [diff] [blame] | 33 | refcount_read(&map->refcnt) == 1); |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 34 | thread_map__put(map); |
| 35 | |
| 36 | /* test dummy pid */ |
| 37 | map = thread_map__new_dummy(); |
| 38 | TEST_ASSERT_VAL("failed to alloc map", map); |
| 39 | |
| 40 | thread_map__read_comms(map); |
| 41 | |
| 42 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
| 43 | TEST_ASSERT_VAL("wrong pid", thread_map__pid(map, 0) == -1); |
| 44 | TEST_ASSERT_VAL("wrong comm", |
| 45 | thread_map__comm(map, 0) && |
| 46 | !strcmp(thread_map__comm(map, 0), "dummy")); |
Jiri Olsa | 35318d2 | 2015-07-21 14:31:21 +0200 | [diff] [blame] | 47 | TEST_ASSERT_VAL("wrong refcnt", |
Elena Reshetova | 364fed3 | 2017-02-21 17:35:03 +0200 | [diff] [blame] | 48 | refcount_read(&map->refcnt) == 1); |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 49 | thread_map__put(map); |
| 50 | return 0; |
| 51 | } |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 52 | |
| 53 | static int process_event(struct perf_tool *tool __maybe_unused, |
| 54 | union perf_event *event, |
| 55 | struct perf_sample *sample __maybe_unused, |
| 56 | struct machine *machine __maybe_unused) |
| 57 | { |
| 58 | struct thread_map_event *map = &event->thread_map; |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 59 | struct thread_map *threads; |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 60 | |
| 61 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
| 62 | TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid()); |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 63 | TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME)); |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 64 | |
| 65 | threads = thread_map__new_event(&event->thread_map); |
| 66 | TEST_ASSERT_VAL("failed to alloc map", threads); |
| 67 | |
| 68 | TEST_ASSERT_VAL("wrong nr", threads->nr == 1); |
| 69 | TEST_ASSERT_VAL("wrong pid", |
| 70 | thread_map__pid(threads, 0) == getpid()); |
| 71 | TEST_ASSERT_VAL("wrong comm", |
| 72 | thread_map__comm(threads, 0) && |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 73 | !strcmp(thread_map__comm(threads, 0), NAME)); |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 74 | TEST_ASSERT_VAL("wrong refcnt", |
Elena Reshetova | 364fed3 | 2017-02-21 17:35:03 +0200 | [diff] [blame] | 75 | refcount_read(&threads->refcnt) == 1); |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 76 | thread_map__put(threads); |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 80 | int test__thread_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 81 | { |
| 82 | struct thread_map *threads; |
| 83 | |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 84 | TEST_ASSERT_VAL("failed to set process name", |
| 85 | !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); |
| 86 | |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 87 | /* test map on current pid */ |
| 88 | threads = thread_map__new_by_pid(getpid()); |
| 89 | TEST_ASSERT_VAL("failed to alloc map", threads); |
| 90 | |
| 91 | thread_map__read_comms(threads); |
| 92 | |
| 93 | TEST_ASSERT_VAL("failed to synthesize map", |
| 94 | !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL)); |
| 95 | |
| 96 | return 0; |
| 97 | } |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 98 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 99 | int test__thread_map_remove(struct test *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 100 | { |
| 101 | struct thread_map *threads; |
| 102 | char *str; |
| 103 | int i; |
| 104 | |
| 105 | TEST_ASSERT_VAL("failed to allocate map string", |
| 106 | asprintf(&str, "%d,%d", getpid(), getppid()) >= 0); |
| 107 | |
| 108 | threads = thread_map__new_str(str, NULL, 0); |
| 109 | |
| 110 | TEST_ASSERT_VAL("failed to allocate thread_map", |
| 111 | threads); |
| 112 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 113 | if (verbose > 0) |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 114 | thread_map__fprintf(threads, stderr); |
| 115 | |
| 116 | TEST_ASSERT_VAL("failed to remove thread", |
| 117 | !thread_map__remove(threads, 0)); |
| 118 | |
| 119 | TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1); |
| 120 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 121 | if (verbose > 0) |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 122 | thread_map__fprintf(threads, stderr); |
| 123 | |
| 124 | TEST_ASSERT_VAL("failed to remove thread", |
| 125 | !thread_map__remove(threads, 0)); |
| 126 | |
| 127 | TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0); |
| 128 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 129 | if (verbose > 0) |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 130 | thread_map__fprintf(threads, stderr); |
| 131 | |
| 132 | TEST_ASSERT_VAL("failed to not remove thread", |
| 133 | thread_map__remove(threads, 0)); |
| 134 | |
| 135 | for (i = 0; i < threads->nr; i++) |
| 136 | free(threads->map[i].comm); |
| 137 | |
| 138 | free(threads); |
| 139 | return 0; |
| 140 | } |