0001
0002 #include <errno.h>
0003 #include <inttypes.h>
0004 #include <unistd.h>
0005 #include <stdlib.h>
0006 #include <signal.h>
0007 #include <sys/mman.h>
0008 #include <linux/string.h>
0009
0010 #include "tests.h"
0011 #include "util/debug.h"
0012 #include "util/evsel.h"
0013 #include "util/evlist.h"
0014 #include "util/cpumap.h"
0015 #include "util/mmap.h"
0016 #include "util/thread_map.h"
0017 #include <perf/evlist.h>
0018 #include <perf/mmap.h>
0019
0020 #define NR_LOOPS 10000000
0021
0022
0023
0024
0025
0026
0027 static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
0028 {
0029 int i, err = -1;
0030 volatile int tmp = 0;
0031 u64 total_periods = 0;
0032 int nr_samples = 0;
0033 char sbuf[STRERR_BUFSIZE];
0034 union perf_event *event;
0035 struct evsel *evsel;
0036 struct evlist *evlist;
0037 struct perf_event_attr attr = {
0038 .type = PERF_TYPE_SOFTWARE,
0039 .config = clock_id,
0040 .sample_type = PERF_SAMPLE_PERIOD,
0041 .exclude_kernel = 1,
0042 .disabled = 1,
0043 .freq = 1,
0044 };
0045 struct perf_cpu_map *cpus = NULL;
0046 struct perf_thread_map *threads = NULL;
0047 struct mmap *md;
0048
0049 attr.sample_freq = 500;
0050
0051 evlist = evlist__new();
0052 if (evlist == NULL) {
0053 pr_debug("evlist__new\n");
0054 return -1;
0055 }
0056
0057 evsel = evsel__new(&attr);
0058 if (evsel == NULL) {
0059 pr_debug("evsel__new\n");
0060 goto out_delete_evlist;
0061 }
0062 evlist__add(evlist, evsel);
0063
0064 cpus = perf_cpu_map__dummy_new();
0065 threads = thread_map__new_by_tid(getpid());
0066 if (!cpus || !threads) {
0067 err = -ENOMEM;
0068 pr_debug("Not enough memory to create thread/cpu maps\n");
0069 goto out_delete_evlist;
0070 }
0071
0072 perf_evlist__set_maps(&evlist->core, cpus, threads);
0073
0074 if (evlist__open(evlist)) {
0075 const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
0076
0077 err = -errno;
0078 pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n",
0079 str_error_r(errno, sbuf, sizeof(sbuf)),
0080 knob, (u64)attr.sample_freq);
0081 goto out_delete_evlist;
0082 }
0083
0084 err = evlist__mmap(evlist, 128);
0085 if (err < 0) {
0086 pr_debug("failed to mmap event: %d (%s)\n", errno,
0087 str_error_r(errno, sbuf, sizeof(sbuf)));
0088 goto out_delete_evlist;
0089 }
0090
0091 evlist__enable(evlist);
0092
0093
0094 for (i = 0; i < NR_LOOPS; i++)
0095 tmp++;
0096
0097 evlist__disable(evlist);
0098
0099 md = &evlist->mmap[0];
0100 if (perf_mmap__read_init(&md->core) < 0)
0101 goto out_init;
0102
0103 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
0104 struct perf_sample sample;
0105
0106 if (event->header.type != PERF_RECORD_SAMPLE)
0107 goto next_event;
0108
0109 err = evlist__parse_sample(evlist, event, &sample);
0110 if (err < 0) {
0111 pr_debug("Error during parse sample\n");
0112 goto out_delete_evlist;
0113 }
0114
0115 total_periods += sample.period;
0116 nr_samples++;
0117 next_event:
0118 perf_mmap__consume(&md->core);
0119 }
0120 perf_mmap__read_done(&md->core);
0121
0122 out_init:
0123 if ((u64) nr_samples == total_periods) {
0124 pr_debug("All (%d) samples have period value of 1!\n",
0125 nr_samples);
0126 err = -1;
0127 }
0128
0129 out_delete_evlist:
0130 perf_cpu_map__put(cpus);
0131 perf_thread_map__put(threads);
0132 evlist__delete(evlist);
0133 return err;
0134 }
0135
0136 static int test__sw_clock_freq(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0137 {
0138 int ret;
0139
0140 ret = __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK);
0141 if (!ret)
0142 ret = __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK);
0143
0144 return ret;
0145 }
0146
0147 DEFINE_SUITE("Software clock events period values", sw_clock_freq);