Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Test support for libpfm4 event encodings.
0004  *
0005  * Copyright 2020 Google LLC.
0006  */
0007 #include "tests.h"
0008 #include "util/debug.h"
0009 #include "util/evlist.h"
0010 #include "util/pfm.h"
0011 
0012 #include <linux/kernel.h>
0013 
0014 #ifdef HAVE_LIBPFM
0015 static int count_pfm_events(struct perf_evlist *evlist)
0016 {
0017     struct perf_evsel *evsel;
0018     int count = 0;
0019 
0020     perf_evlist__for_each_entry(evlist, evsel) {
0021         count++;
0022     }
0023     return count;
0024 }
0025 
0026 static int test__pfm_events(struct test_suite *test __maybe_unused,
0027                 int subtest __maybe_unused)
0028 {
0029     struct evlist *evlist;
0030     struct option opt;
0031     size_t i;
0032     const struct {
0033         const char *events;
0034         int nr_events;
0035     } table[] = {
0036         {
0037             .events = "",
0038             .nr_events = 0,
0039         },
0040         {
0041             .events = "instructions",
0042             .nr_events = 1,
0043         },
0044         {
0045             .events = "instructions,cycles",
0046             .nr_events = 2,
0047         },
0048         {
0049             .events = "stereolab",
0050             .nr_events = 0,
0051         },
0052         {
0053             .events = "instructions,instructions",
0054             .nr_events = 2,
0055         },
0056         {
0057             .events = "stereolab,instructions",
0058             .nr_events = 0,
0059         },
0060         {
0061             .events = "instructions,stereolab",
0062             .nr_events = 1,
0063         },
0064     };
0065 
0066     for (i = 0; i < ARRAY_SIZE(table); i++) {
0067         evlist = evlist__new();
0068         if (evlist == NULL)
0069             return -ENOMEM;
0070 
0071         opt.value = evlist;
0072         parse_libpfm_events_option(&opt,
0073                     table[i].events,
0074                     0);
0075         TEST_ASSERT_EQUAL(table[i].events,
0076                 count_pfm_events(&evlist->core),
0077                 table[i].nr_events);
0078         TEST_ASSERT_EQUAL(table[i].events,
0079                 evlist->core.nr_groups,
0080                 0);
0081 
0082         evlist__delete(evlist);
0083     }
0084     return 0;
0085 }
0086 
0087 static int test__pfm_group(struct test_suite *test __maybe_unused,
0088                int subtest __maybe_unused)
0089 {
0090     struct evlist *evlist;
0091     struct option opt;
0092     size_t i;
0093     const struct {
0094         const char *events;
0095         int nr_events;
0096         int nr_groups;
0097     } table[] = {
0098         {
0099             .events = "{},",
0100             .nr_events = 0,
0101             .nr_groups = 0,
0102         },
0103         {
0104             .events = "{instructions}",
0105             .nr_events = 1,
0106             .nr_groups = 1,
0107         },
0108         {
0109             .events = "{instructions},{}",
0110             .nr_events = 1,
0111             .nr_groups = 1,
0112         },
0113         {
0114             .events = "{},{instructions}",
0115             .nr_events = 1,
0116             .nr_groups = 1,
0117         },
0118         {
0119             .events = "{instructions},{instructions}",
0120             .nr_events = 2,
0121             .nr_groups = 2,
0122         },
0123         {
0124             .events = "{instructions,cycles},{instructions,cycles}",
0125             .nr_events = 4,
0126             .nr_groups = 2,
0127         },
0128         {
0129             .events = "{stereolab}",
0130             .nr_events = 0,
0131             .nr_groups = 0,
0132         },
0133         {
0134             .events =
0135             "{instructions,cycles},{instructions,stereolab}",
0136             .nr_events = 3,
0137             .nr_groups = 1,
0138         },
0139         {
0140             .events = "instructions}",
0141             .nr_events = 1,
0142             .nr_groups = 0,
0143         },
0144         {
0145             .events = "{{instructions}}",
0146             .nr_events = 0,
0147             .nr_groups = 0,
0148         },
0149     };
0150 
0151     for (i = 0; i < ARRAY_SIZE(table); i++) {
0152         evlist = evlist__new();
0153         if (evlist == NULL)
0154             return -ENOMEM;
0155 
0156         opt.value = evlist;
0157         parse_libpfm_events_option(&opt,
0158                     table[i].events,
0159                     0);
0160         TEST_ASSERT_EQUAL(table[i].events,
0161                 count_pfm_events(&evlist->core),
0162                 table[i].nr_events);
0163         TEST_ASSERT_EQUAL(table[i].events,
0164                 evlist->core.nr_groups,
0165                 table[i].nr_groups);
0166 
0167         evlist__delete(evlist);
0168     }
0169     return 0;
0170 }
0171 #else
0172 static int test__pfm_events(struct test_suite *test __maybe_unused,
0173                 int subtest __maybe_unused)
0174 {
0175     return TEST_SKIP;
0176 }
0177 
0178 static int test__pfm_group(struct test_suite *test __maybe_unused,
0179                int subtest __maybe_unused)
0180 {
0181     return TEST_SKIP;
0182 }
0183 #endif
0184 
0185 static struct test_case pfm_tests[] = {
0186     TEST_CASE_REASON("test of individual --pfm-events", pfm_events, "not compiled in"),
0187     TEST_CASE_REASON("test groups of --pfm-events", pfm_group, "not compiled in"),
0188     { .name = NULL, }
0189 };
0190 
0191 struct test_suite suite__pfm = {
0192     .desc = "Test libpfm4 support",
0193     .test_cases = pfm_tests,
0194 };