Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 1 | #include <stdlib.h> |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 2 | #include <sys/types.h> |
| 3 | #include <unistd.h> |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 4 | #include <sys/prctl.h> |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 5 | #include "tests.h" |
| 6 | #include "thread_map.h" |
| 7 | #include "debug.h" |
| 8 | |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 9 | #define NAME (const char *) "perf" |
| 10 | #define NAMEUL (unsigned long) NAME |
| 11 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame^] | 12 | 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] | 13 | { |
| 14 | struct thread_map *map; |
| 15 | |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 16 | TEST_ASSERT_VAL("failed to set process name", |
| 17 | !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); |
| 18 | |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 19 | /* test map on current pid */ |
| 20 | map = thread_map__new_by_pid(getpid()); |
| 21 | TEST_ASSERT_VAL("failed to alloc map", map); |
| 22 | |
| 23 | thread_map__read_comms(map); |
| 24 | |
| 25 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
| 26 | TEST_ASSERT_VAL("wrong pid", |
| 27 | thread_map__pid(map, 0) == getpid()); |
| 28 | TEST_ASSERT_VAL("wrong comm", |
| 29 | thread_map__comm(map, 0) && |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 30 | !strcmp(thread_map__comm(map, 0), NAME)); |
Jiri Olsa | 35318d2 | 2015-07-21 14:31:21 +0200 | [diff] [blame] | 31 | TEST_ASSERT_VAL("wrong refcnt", |
Elena Reshetova | 364fed3 | 2017-02-21 17:35:03 +0200 | [diff] [blame] | 32 | refcount_read(&map->refcnt) == 1); |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 33 | thread_map__put(map); |
| 34 | |
| 35 | /* test dummy pid */ |
| 36 | map = thread_map__new_dummy(); |
| 37 | TEST_ASSERT_VAL("failed to alloc map", map); |
| 38 | |
| 39 | thread_map__read_comms(map); |
| 40 | |
| 41 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
| 42 | TEST_ASSERT_VAL("wrong pid", thread_map__pid(map, 0) == -1); |
| 43 | TEST_ASSERT_VAL("wrong comm", |
| 44 | thread_map__comm(map, 0) && |
| 45 | !strcmp(thread_map__comm(map, 0), "dummy")); |
Jiri Olsa | 35318d2 | 2015-07-21 14:31:21 +0200 | [diff] [blame] | 46 | TEST_ASSERT_VAL("wrong refcnt", |
Elena Reshetova | 364fed3 | 2017-02-21 17:35:03 +0200 | [diff] [blame] | 47 | refcount_read(&map->refcnt) == 1); |
Jiri Olsa | 134aa44 | 2015-06-26 11:29:08 +0200 | [diff] [blame] | 48 | thread_map__put(map); |
| 49 | return 0; |
| 50 | } |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 51 | |
| 52 | static int process_event(struct perf_tool *tool __maybe_unused, |
| 53 | union perf_event *event, |
| 54 | struct perf_sample *sample __maybe_unused, |
| 55 | struct machine *machine __maybe_unused) |
| 56 | { |
| 57 | struct thread_map_event *map = &event->thread_map; |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 58 | struct thread_map *threads; |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 59 | |
| 60 | TEST_ASSERT_VAL("wrong nr", map->nr == 1); |
| 61 | TEST_ASSERT_VAL("wrong pid", map->entries[0].pid == (u64) getpid()); |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 62 | TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME)); |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 63 | |
| 64 | threads = thread_map__new_event(&event->thread_map); |
| 65 | TEST_ASSERT_VAL("failed to alloc map", threads); |
| 66 | |
| 67 | TEST_ASSERT_VAL("wrong nr", threads->nr == 1); |
| 68 | TEST_ASSERT_VAL("wrong pid", |
| 69 | thread_map__pid(threads, 0) == getpid()); |
| 70 | TEST_ASSERT_VAL("wrong comm", |
| 71 | thread_map__comm(threads, 0) && |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 72 | !strcmp(thread_map__comm(threads, 0), NAME)); |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 73 | TEST_ASSERT_VAL("wrong refcnt", |
Elena Reshetova | 364fed3 | 2017-02-21 17:35:03 +0200 | [diff] [blame] | 74 | refcount_read(&threads->refcnt) == 1); |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 75 | thread_map__put(threads); |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame^] | 79 | 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] | 80 | { |
| 81 | struct thread_map *threads; |
| 82 | |
Jiri Olsa | 8fbc38a | 2016-06-28 13:29:03 +0200 | [diff] [blame] | 83 | TEST_ASSERT_VAL("failed to set process name", |
| 84 | !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0)); |
| 85 | |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 86 | /* test map on current pid */ |
| 87 | threads = thread_map__new_by_pid(getpid()); |
| 88 | TEST_ASSERT_VAL("failed to alloc map", threads); |
| 89 | |
| 90 | thread_map__read_comms(threads); |
| 91 | |
| 92 | TEST_ASSERT_VAL("failed to synthesize map", |
| 93 | !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL)); |
| 94 | |
| 95 | return 0; |
| 96 | } |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 97 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame^] | 98 | 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] | 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 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 112 | if (verbose > 0) |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 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 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 120 | if (verbose > 0) |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 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 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 128 | if (verbose > 0) |
Jiri Olsa | 38af91f | 2016-12-12 11:35:41 +0100 | [diff] [blame] | 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 | } |