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