Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <stdlib.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 3 | #include <string.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4 | #include <sys/types.h> |
| 5 | #include <unistd.h> |
| 6 | #include <sys/prctl.h> |
| 7 | #include "tests.h" |
| 8 | #include "thread_map.h" |
| 9 | #include "debug.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 10 | #include "event.h" |
| 11 | #include "util/synthetic-events.h" |
| 12 | #include <linux/zalloc.h> |
| 13 | #include <perf/event.h> |
| 14 | |
| 15 | struct perf_sample; |
| 16 | struct perf_tool; |
| 17 | struct machine; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 18 | |
| 19 | #define NAME (const char *) "perf" |
| 20 | #define NAMEUL (unsigned long) NAME |
| 21 | |
| 22 | int test__thread_map(struct test *test __maybe_unused, int subtest __maybe_unused) |
| 23 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 24 | struct perf_thread_map *map; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 25 | |
| 26 | TEST_ASSERT_VAL("failed to set process name", |
| 27 | !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); |
| 28 | |
| 29 | /* test map on current pid */ |
| 30 | map = thread_map__new_by_pid(getpid()); |
| 31 | TEST_ASSERT_VAL("failed to alloc map", map); |
| 32 | |
| 33 | thread_map__read_comms(map); |
| 34 | |
| 35 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
| 36 | TEST_ASSERT_VAL("wrong pid", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 37 | perf_thread_map__pid(map, 0) == getpid()); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 38 | TEST_ASSERT_VAL("wrong comm", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 39 | perf_thread_map__comm(map, 0) && |
| 40 | !strcmp(perf_thread_map__comm(map, 0), NAME)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | TEST_ASSERT_VAL("wrong refcnt", |
| 42 | refcount_read(&map->refcnt) == 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 43 | perf_thread_map__put(map); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 44 | |
| 45 | /* test dummy pid */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 46 | map = perf_thread_map__new_dummy(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 47 | TEST_ASSERT_VAL("failed to alloc map", map); |
| 48 | |
| 49 | thread_map__read_comms(map); |
| 50 | |
| 51 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 52 | TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 53 | TEST_ASSERT_VAL("wrong comm", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 54 | perf_thread_map__comm(map, 0) && |
| 55 | !strcmp(perf_thread_map__comm(map, 0), "dummy")); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 56 | TEST_ASSERT_VAL("wrong refcnt", |
| 57 | refcount_read(&map->refcnt) == 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 58 | perf_thread_map__put(map); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int process_event(struct perf_tool *tool __maybe_unused, |
| 63 | union perf_event *event, |
| 64 | struct perf_sample *sample __maybe_unused, |
| 65 | struct machine *machine __maybe_unused) |
| 66 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 67 | struct perf_record_thread_map *map = &event->thread_map; |
| 68 | struct perf_thread_map *threads; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 69 | |
| 70 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
| 71 | TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid()); |
| 72 | TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME)); |
| 73 | |
| 74 | threads = thread_map__new_event(&event->thread_map); |
| 75 | TEST_ASSERT_VAL("failed to alloc map", threads); |
| 76 | |
| 77 | TEST_ASSERT_VAL("wrong nr", threads->nr == 1); |
| 78 | TEST_ASSERT_VAL("wrong pid", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 79 | perf_thread_map__pid(threads, 0) == getpid()); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 80 | TEST_ASSERT_VAL("wrong comm", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 81 | perf_thread_map__comm(threads, 0) && |
| 82 | !strcmp(perf_thread_map__comm(threads, 0), NAME)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 83 | TEST_ASSERT_VAL("wrong refcnt", |
| 84 | refcount_read(&threads->refcnt) == 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 85 | perf_thread_map__put(threads); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | int test__thread_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused) |
| 90 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 91 | struct perf_thread_map *threads; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 92 | |
| 93 | TEST_ASSERT_VAL("failed to set process name", |
| 94 | !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); |
| 95 | |
| 96 | /* test map on current pid */ |
| 97 | threads = thread_map__new_by_pid(getpid()); |
| 98 | TEST_ASSERT_VAL("failed to alloc map", threads); |
| 99 | |
| 100 | thread_map__read_comms(threads); |
| 101 | |
| 102 | TEST_ASSERT_VAL("failed to synthesize map", |
| 103 | !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL)); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int test__thread_map_remove(struct test *test __maybe_unused, int subtest __maybe_unused) |
| 109 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 110 | struct perf_thread_map *threads; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 111 | char *str; |
| 112 | int i; |
| 113 | |
| 114 | TEST_ASSERT_VAL("failed to allocate map string", |
| 115 | asprintf(&str, "%d,%d", getpid(), getppid()) >= 0); |
| 116 | |
| 117 | threads = thread_map__new_str(str, NULL, 0, false); |
| 118 | |
| 119 | TEST_ASSERT_VAL("failed to allocate thread_map", |
| 120 | threads); |
| 121 | |
| 122 | if (verbose > 0) |
| 123 | thread_map__fprintf(threads, stderr); |
| 124 | |
| 125 | TEST_ASSERT_VAL("failed to remove thread", |
| 126 | !thread_map__remove(threads, 0)); |
| 127 | |
| 128 | TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1); |
| 129 | |
| 130 | if (verbose > 0) |
| 131 | thread_map__fprintf(threads, stderr); |
| 132 | |
| 133 | TEST_ASSERT_VAL("failed to remove thread", |
| 134 | !thread_map__remove(threads, 0)); |
| 135 | |
| 136 | TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0); |
| 137 | |
| 138 | if (verbose > 0) |
| 139 | thread_map__fprintf(threads, stderr); |
| 140 | |
| 141 | TEST_ASSERT_VAL("failed to not remove thread", |
| 142 | thread_map__remove(threads, 0)); |
| 143 | |
| 144 | for (i = 0; i < threads->nr; i++) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 145 | zfree(&threads->map[i].comm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 146 | |
| 147 | free(threads); |
| 148 | return 0; |
| 149 | } |