^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 <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <perf/threadmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <perf/evsel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <internal/tests.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) static int libperf_print(enum libperf_print_level level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) const char *fmt, va_list ap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) return vfprintf(stderr, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static int test_stat_cpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct perf_cpu_map *cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct perf_evsel *evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct perf_event_attr attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) .type = PERF_TYPE_SOFTWARE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .config = PERF_COUNT_SW_CPU_CLOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int err, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) cpus = perf_cpu_map__new(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) __T("failed to create cpus", cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) evsel = perf_evsel__new(&attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) __T("failed to create evsel", evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) err = perf_evsel__open(evsel, cpus, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) __T("failed to open evsel", err == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) for (idx = 0; idx < perf_cpu_map__nr(cpus); idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct perf_counts_values counts = { .val = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) perf_evsel__read(evsel, idx, 0, &counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) __T("failed to read value for evsel", counts.val != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) perf_evsel__close(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) perf_evsel__delete(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) perf_cpu_map__put(cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int test_stat_thread(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct perf_counts_values counts = { .val = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct perf_thread_map *threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct perf_evsel *evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct perf_event_attr attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .type = PERF_TYPE_SOFTWARE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .config = PERF_COUNT_SW_TASK_CLOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) threads = perf_thread_map__new_dummy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) __T("failed to create threads", threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) perf_thread_map__set_pid(threads, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) evsel = perf_evsel__new(&attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) __T("failed to create evsel", evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) err = perf_evsel__open(evsel, NULL, threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) __T("failed to open evsel", err == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) perf_evsel__read(evsel, 0, 0, &counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __T("failed to read value for evsel", counts.val != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) perf_evsel__close(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) perf_evsel__delete(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) perf_thread_map__put(threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int test_stat_thread_enable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct perf_counts_values counts = { .val = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct perf_thread_map *threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct perf_evsel *evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct perf_event_attr attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .type = PERF_TYPE_SOFTWARE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .config = PERF_COUNT_SW_TASK_CLOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .disabled = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) threads = perf_thread_map__new_dummy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) __T("failed to create threads", threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) perf_thread_map__set_pid(threads, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) evsel = perf_evsel__new(&attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) __T("failed to create evsel", evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) err = perf_evsel__open(evsel, NULL, threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __T("failed to open evsel", err == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) perf_evsel__read(evsel, 0, 0, &counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) __T("failed to read value for evsel", counts.val == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) err = perf_evsel__enable(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) __T("failed to enable evsel", err == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) perf_evsel__read(evsel, 0, 0, &counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) __T("failed to read value for evsel", counts.val != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) err = perf_evsel__disable(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) __T("failed to enable evsel", err == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) perf_evsel__close(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) perf_evsel__delete(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) perf_thread_map__put(threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) __T_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) libperf_init(libperf_print);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) test_stat_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) test_stat_thread();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) test_stat_thread_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) __T_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return tests_failed == 0 ? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }