evswitch.c 1.4 KB

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