1 #include "evlist.h" 2 #include "evsel.h" 3 #include "parse-events.h" 4 #include "tests.h" 5 #include "debug.h" 6 #include <errno.h> 7 #include <linux/kernel.h> 8 9 static int perf_evsel__roundtrip_cache_name_test(void) 10 { 11 char name[128]; 12 int type, op, err = 0, ret = 0, i, idx; 13 struct perf_evsel *evsel; 14 struct perf_evlist *evlist = perf_evlist__new(); 15 16 if (evlist == NULL) 17 return -ENOMEM; 18 19 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 20 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 21 /* skip invalid cache type */ 22 if (!perf_evsel__is_cache_op_valid(type, op)) 23 continue; 24 25 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 26 __perf_evsel__hw_cache_type_op_res_name(type, op, i, 27 name, sizeof(name)); 28 err = parse_events(evlist, name, NULL); 29 if (err) 30 ret = err; 31 } 32 } 33 } 34 35 idx = 0; 36 evsel = perf_evlist__first(evlist); 37 38 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 39 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 40 /* skip invalid cache type */ 41 if (!perf_evsel__is_cache_op_valid(type, op)) 42 continue; 43 44 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 45 __perf_evsel__hw_cache_type_op_res_name(type, op, i, 46 name, sizeof(name)); 47 if (evsel->idx != idx) 48 continue; 49 50 ++idx; 51 52 if (strcmp(perf_evsel__name(evsel), name)) { 53 pr_debug("%s != %s\n", perf_evsel__name(evsel), name); 54 ret = -1; 55 } 56 57 evsel = perf_evsel__next(evsel); 58 } 59 } 60 } 61 62 perf_evlist__delete(evlist); 63 return ret; 64 } 65 66 static int __perf_evsel__name_array_test(const char *names[], int nr_names) 67 { 68 int i, err; 69 struct perf_evsel *evsel; 70 struct perf_evlist *evlist = perf_evlist__new(); 71 72 if (evlist == NULL) 73 return -ENOMEM; 74 75 for (i = 0; i < nr_names; ++i) { 76 err = parse_events(evlist, names[i], NULL); 77 if (err) { 78 pr_debug("failed to parse event '%s', err %d\n", 79 names[i], err); 80 goto out_delete_evlist; 81 } 82 } 83 84 err = 0; 85 evlist__for_each_entry(evlist, evsel) { 86 if (strcmp(perf_evsel__name(evsel), names[evsel->idx])) { 87 --err; 88 pr_debug("%s != %s\n", perf_evsel__name(evsel), names[evsel->idx]); 89 } 90 } 91 92 out_delete_evlist: 93 perf_evlist__delete(evlist); 94 return err; 95 } 96 97 #define perf_evsel__name_array_test(names) \ 98 __perf_evsel__name_array_test(names, ARRAY_SIZE(names)) 99 100 int test__perf_evsel__roundtrip_name_test(int subtest __maybe_unused) 101 { 102 int err = 0, ret = 0; 103 104 err = perf_evsel__name_array_test(perf_evsel__hw_names); 105 if (err) 106 ret = err; 107 108 err = __perf_evsel__name_array_test(perf_evsel__sw_names, 109 PERF_COUNT_SW_DUMMY + 1); 110 if (err) 111 ret = err; 112 113 err = perf_evsel__roundtrip_cache_name_test(); 114 if (err) 115 ret = err; 116 117 return ret; 118 } 119