~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/tests/thread-map.c

Version: ~ [ linux-6.2-rc3 ] ~ [ linux-6.1.5 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.87 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.162 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.228 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.269 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.302 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <stdlib.h>
  3 #include <string.h>
  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"
 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;
 18 
 19 #define NAME    (const char *) "perf"
 20 #define NAMEUL  (unsigned long) NAME
 21 
 22 static int test__thread_map(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
 23 {
 24         struct perf_thread_map *map;
 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",
 37                         perf_thread_map__pid(map, 0) == getpid());
 38         TEST_ASSERT_VAL("wrong comm",
 39                         perf_thread_map__comm(map, 0) &&
 40                         !strcmp(perf_thread_map__comm(map, 0), NAME));
 41         TEST_ASSERT_VAL("wrong refcnt",
 42                         refcount_read(&map->refcnt) == 1);
 43         perf_thread_map__put(map);
 44 
 45         /* test dummy pid */
 46         map = perf_thread_map__new_dummy();
 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);
 52         TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1);
 53         TEST_ASSERT_VAL("wrong comm",
 54                         perf_thread_map__comm(map, 0) &&
 55                         !strcmp(perf_thread_map__comm(map, 0), "dummy"));
 56         TEST_ASSERT_VAL("wrong refcnt",
 57                         refcount_read(&map->refcnt) == 1);
 58         perf_thread_map__put(map);
 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 {
 67         struct perf_record_thread_map *map = &event->thread_map;
 68         struct perf_thread_map *threads;
 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",
 79                         perf_thread_map__pid(threads, 0) == getpid());
 80         TEST_ASSERT_VAL("wrong comm",
 81                         perf_thread_map__comm(threads, 0) &&
 82                         !strcmp(perf_thread_map__comm(threads, 0), NAME));
 83         TEST_ASSERT_VAL("wrong refcnt",
 84                         refcount_read(&threads->refcnt) == 1);
 85         perf_thread_map__put(threads);
 86         return 0;
 87 }
 88 
 89 static int test__thread_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
 90 {
 91         struct perf_thread_map *threads;
 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         perf_thread_map__put(threads);
106         return 0;
107 }
108 
109 static int test__thread_map_remove(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
110 {
111         struct perf_thread_map *threads;
112         char *str;
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         free(str);
119 
120         TEST_ASSERT_VAL("failed to allocate thread_map",
121                         threads);
122 
123         if (verbose > 0)
124                 thread_map__fprintf(threads, stderr);
125 
126         TEST_ASSERT_VAL("failed to remove thread",
127                         !thread_map__remove(threads, 0));
128 
129         TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1);
130 
131         if (verbose > 0)
132                 thread_map__fprintf(threads, stderr);
133 
134         TEST_ASSERT_VAL("failed to remove thread",
135                         !thread_map__remove(threads, 0));
136 
137         TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0);
138 
139         if (verbose > 0)
140                 thread_map__fprintf(threads, stderr);
141 
142         TEST_ASSERT_VAL("failed to not remove thread",
143                         thread_map__remove(threads, 0));
144 
145         perf_thread_map__put(threads);
146         return 0;
147 }
148 
149 DEFINE_SUITE("Thread map", thread_map);
150 DEFINE_SUITE("Synthesize thread map", thread_map_synthesize);
151 DEFINE_SUITE("Remove thread map", thread_map_remove);
152 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp