Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) /* For the CLR_() macros */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "evlist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "thread_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "util/mmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <perf/evlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <perf/mmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * This test will generate random numbers of calls to some getpid syscalls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * then establish an mmap for a group of events that are created to monitor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * the syscalls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * sample.id field to map back to its respective perf_evsel instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * Then it checks if the number of syscalls reported as perf events by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * the kernel corresponds to the number of syscalls made.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	union perf_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct perf_thread_map *threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct perf_cpu_map *cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct evlist *evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	cpu_set_t cpu_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	const char *syscall_names[] = { "getsid", "getppid", "getpgid", };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define nsyscalls ARRAY_SIZE(syscall_names)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	unsigned int nr_events[nsyscalls],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		     expected_nr_events[nsyscalls], i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct evsel *evsels[nsyscalls], *evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	char sbuf[STRERR_BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct mmap *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	threads = thread_map__new(-1, getpid(), UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (threads == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		pr_debug("thread_map__new\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	cpus = perf_cpu_map__new(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (cpus == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		pr_debug("perf_cpu_map__new\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		goto out_free_threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	CPU_ZERO(&cpu_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	CPU_SET(cpus->map[0], &cpu_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		pr_debug("sched_setaffinity() failed on CPU %d: %s ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			 cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		goto out_free_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	evlist = evlist__new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (evlist == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		pr_debug("perf_evlist__new\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		goto out_free_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	perf_evlist__set_maps(&evlist->core, cpus, threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	for (i = 0; i < nsyscalls; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		char name[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		evsels[i] = evsel__newtp("syscalls", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (IS_ERR(evsels[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			pr_debug("evsel__new(%s)\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		evsels[i]->core.attr.wakeup_events = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		evsel__set_sample_id(evsels[i], false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		evlist__add(evlist, evsels[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (evsel__open(evsels[i], cpus, threads) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			pr_debug("failed to open counter: %s, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				 str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		nr_events[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		expected_nr_events[i] = 1 + rand() % 127;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (evlist__mmap(evlist, 128) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		pr_debug("failed to mmap events: %d (%s)\n", errno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			 str_error_r(errno, sbuf, sizeof(sbuf)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	for (i = 0; i < nsyscalls; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		for (j = 0; j < expected_nr_events[i]; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			int foo = syscalls[i]();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			++foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	md = &evlist->mmap[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (perf_mmap__read_init(&md->core) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		goto out_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	while ((event = perf_mmap__read_event(&md->core)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		struct perf_sample sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		if (event->header.type != PERF_RECORD_SAMPLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			pr_debug("unexpected %s event\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 				 perf_event__name(event->header.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		err = perf_evlist__parse_sample(evlist, event, &sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			pr_err("Can't parse sample, err = %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		evsel = perf_evlist__id2evsel(evlist, sample.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		if (evsel == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			pr_debug("event with id %" PRIu64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 				 " doesn't map to an evsel\n", sample.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		nr_events[evsel->idx]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		perf_mmap__consume(&md->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	perf_mmap__read_done(&md->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) out_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	evlist__for_each_entry(evlist, evsel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			pr_debug("expected %d %s events, got %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				 expected_nr_events[evsel->idx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				 evsel__name(evsel), nr_events[evsel->idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			goto out_delete_evlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) out_delete_evlist:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	evlist__delete(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	cpus	= NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	threads = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) out_free_cpus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	perf_cpu_map__put(cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) out_free_threads:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	perf_thread_map__put(threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }