evswitch.c 1.5 KB

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