xref: /linux/tools/perf/util/evswitch.c (revision 7b8e9264f55a9c320f398e337d215e68cca50131)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2019, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 
4 #include <errno.h>
5 #include "evswitch.h"
6 #include "evlist.h"
7 
8 bool evswitch__discard(struct evswitch *evswitch, struct evsel *evsel)
9 {
10 	if (evswitch->on && evswitch->discarding) {
11 		if (evswitch->on != evsel)
12 			return true;
13 
14 		evswitch->discarding = false;
15 
16 		if (!evswitch->show_on_off_events)
17 			return true;
18 
19 		return false;
20 	}
21 
22 	if (evswitch->off && !evswitch->discarding) {
23 		if (evswitch->off != evsel)
24 			return false;
25 
26 		evswitch->discarding = true;
27 
28 		if (!evswitch->show_on_off_events)
29 			return true;
30 	}
31 
32 	return false;
33 }
34 
35 static int evswitch__fprintf_enoent(FILE *fp, const char *evtype, const char *evname)
36 {
37 	int printed = fprintf(fp, "ERROR: switch-%s event not found (%s)\n", evtype, evname);
38 
39 	return printed += fprintf(fp, "HINT:  use 'perf evlist' to see the available event names\n");
40 }
41 
42 int evswitch__init(struct evswitch *evswitch, struct evlist *evlist, FILE *fp)
43 {
44 	if (evswitch->on_name) {
45 		evswitch->on = evlist__find_evsel_by_str(evlist, evswitch->on_name);
46 		if (evswitch->on == NULL) {
47 			evswitch__fprintf_enoent(fp, "on", evswitch->on_name);
48 			return -ENOENT;
49 		}
50 		evswitch->discarding = true;
51 	}
52 
53 	if (evswitch->off_name) {
54 		evswitch->off = evlist__find_evsel_by_str(evlist, evswitch->off_name);
55 		if (evswitch->off == NULL) {
56 			evswitch__fprintf_enoent(fp, "off", evswitch->off_name);
57 			return -ENOENT;
58 		}
59 	}
60 
61 	return 0;
62 }
63