Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <stdlib.h>
0003 #include <string.h>
0004 #include <sys/types.h>
0005 #include <unistd.h>
0006 #include <sys/prctl.h>
0007 #include "tests.h"
0008 #include "thread_map.h"
0009 #include "debug.h"
0010 #include "event.h"
0011 #include "util/synthetic-events.h"
0012 #include <linux/zalloc.h>
0013 #include <perf/event.h>
0014 
0015 struct perf_sample;
0016 struct perf_tool;
0017 struct machine;
0018 
0019 #define NAME    (const char *) "perf"
0020 #define NAMEUL  (unsigned long) NAME
0021 
0022 static int test__thread_map(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0023 {
0024     struct perf_thread_map *map;
0025 
0026     TEST_ASSERT_VAL("failed to set process name",
0027             !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
0028 
0029     /* test map on current pid */
0030     map = thread_map__new_by_pid(getpid());
0031     TEST_ASSERT_VAL("failed to alloc map", map);
0032 
0033     thread_map__read_comms(map);
0034 
0035     TEST_ASSERT_VAL("wrong nr", map->nr == 1);
0036     TEST_ASSERT_VAL("wrong pid",
0037             perf_thread_map__pid(map, 0) == getpid());
0038     TEST_ASSERT_VAL("wrong comm",
0039             perf_thread_map__comm(map, 0) &&
0040             !strcmp(perf_thread_map__comm(map, 0), NAME));
0041     TEST_ASSERT_VAL("wrong refcnt",
0042             refcount_read(&map->refcnt) == 1);
0043     perf_thread_map__put(map);
0044 
0045     /* test dummy pid */
0046     map = perf_thread_map__new_dummy();
0047     TEST_ASSERT_VAL("failed to alloc map", map);
0048 
0049     thread_map__read_comms(map);
0050 
0051     TEST_ASSERT_VAL("wrong nr", map->nr == 1);
0052     TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1);
0053     TEST_ASSERT_VAL("wrong comm",
0054             perf_thread_map__comm(map, 0) &&
0055             !strcmp(perf_thread_map__comm(map, 0), "dummy"));
0056     TEST_ASSERT_VAL("wrong refcnt",
0057             refcount_read(&map->refcnt) == 1);
0058     perf_thread_map__put(map);
0059     return 0;
0060 }
0061 
0062 static int process_event(struct perf_tool *tool __maybe_unused,
0063              union perf_event *event,
0064              struct perf_sample *sample __maybe_unused,
0065              struct machine *machine __maybe_unused)
0066 {
0067     struct perf_record_thread_map *map = &event->thread_map;
0068     struct perf_thread_map *threads;
0069 
0070     TEST_ASSERT_VAL("wrong nr",   map->nr == 1);
0071     TEST_ASSERT_VAL("wrong pid",  map->entries[0].pid == (u64) getpid());
0072     TEST_ASSERT_VAL("wrong comm", !strcmp(map->entries[0].comm, NAME));
0073 
0074     threads = thread_map__new_event(&event->thread_map);
0075     TEST_ASSERT_VAL("failed to alloc map", threads);
0076 
0077     TEST_ASSERT_VAL("wrong nr", threads->nr == 1);
0078     TEST_ASSERT_VAL("wrong pid",
0079             perf_thread_map__pid(threads, 0) == getpid());
0080     TEST_ASSERT_VAL("wrong comm",
0081             perf_thread_map__comm(threads, 0) &&
0082             !strcmp(perf_thread_map__comm(threads, 0), NAME));
0083     TEST_ASSERT_VAL("wrong refcnt",
0084             refcount_read(&threads->refcnt) == 1);
0085     perf_thread_map__put(threads);
0086     return 0;
0087 }
0088 
0089 static int test__thread_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0090 {
0091     struct perf_thread_map *threads;
0092 
0093     TEST_ASSERT_VAL("failed to set process name",
0094             !prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
0095 
0096     /* test map on current pid */
0097     threads = thread_map__new_by_pid(getpid());
0098     TEST_ASSERT_VAL("failed to alloc map", threads);
0099 
0100     thread_map__read_comms(threads);
0101 
0102     TEST_ASSERT_VAL("failed to synthesize map",
0103         !perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
0104 
0105     perf_thread_map__put(threads);
0106     return 0;
0107 }
0108 
0109 static int test__thread_map_remove(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0110 {
0111     struct perf_thread_map *threads;
0112     char *str;
0113 
0114     TEST_ASSERT_VAL("failed to allocate map string",
0115             asprintf(&str, "%d,%d", getpid(), getppid()) >= 0);
0116 
0117     threads = thread_map__new_str(str, NULL, 0, false);
0118     free(str);
0119 
0120     TEST_ASSERT_VAL("failed to allocate thread_map",
0121             threads);
0122 
0123     if (verbose > 0)
0124         thread_map__fprintf(threads, stderr);
0125 
0126     TEST_ASSERT_VAL("failed to remove thread",
0127             !thread_map__remove(threads, 0));
0128 
0129     TEST_ASSERT_VAL("thread_map count != 1", threads->nr == 1);
0130 
0131     if (verbose > 0)
0132         thread_map__fprintf(threads, stderr);
0133 
0134     TEST_ASSERT_VAL("failed to remove thread",
0135             !thread_map__remove(threads, 0));
0136 
0137     TEST_ASSERT_VAL("thread_map count != 0", threads->nr == 0);
0138 
0139     if (verbose > 0)
0140         thread_map__fprintf(threads, stderr);
0141 
0142     TEST_ASSERT_VAL("failed to not remove thread",
0143             thread_map__remove(threads, 0));
0144 
0145     perf_thread_map__put(threads);
0146     return 0;
0147 }
0148 
0149 DEFINE_SUITE("Thread map", thread_map);
0150 DEFINE_SUITE("Synthesize thread map", thread_map_synthesize);
0151 DEFINE_SUITE("Remove thread map", thread_map_remove);