entry_flush.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 IBM Corporation.
  4. */
  5. #define __SANE_USERSPACE_TYPES__
  6. #include <sys/types.h>
  7. #include <stdint.h>
  8. #include <malloc.h>
  9. #include <unistd.h>
  10. #include <signal.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include "utils.h"
  15. #include "flush_utils.h"
  16. int entry_flush_test(void)
  17. {
  18. char *p;
  19. int repetitions = 10;
  20. int fd, passes = 0, iter, rc = 0;
  21. struct perf_event_read v;
  22. __u64 l1d_misses_total = 0;
  23. unsigned long iterations = 100000, zero_size = 24 * 1024;
  24. unsigned long l1d_misses_expected;
  25. int rfi_flush_orig;
  26. int entry_flush, entry_flush_orig;
  27. SKIP_IF(geteuid() != 0);
  28. // The PMU event we use only works on Power7 or later
  29. SKIP_IF(!have_hwcap(PPC_FEATURE_ARCH_2_06));
  30. if (read_debugfs_int("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
  31. perror("Unable to read powerpc/rfi_flush debugfs file");
  32. SKIP_IF(1);
  33. }
  34. if (read_debugfs_int("powerpc/entry_flush", &entry_flush_orig) < 0) {
  35. perror("Unable to read powerpc/entry_flush debugfs file");
  36. SKIP_IF(1);
  37. }
  38. if (rfi_flush_orig != 0) {
  39. if (write_debugfs_int("powerpc/rfi_flush", 0) < 0) {
  40. perror("error writing to powerpc/rfi_flush debugfs file");
  41. FAIL_IF(1);
  42. }
  43. }
  44. entry_flush = entry_flush_orig;
  45. fd = perf_event_open_counter(PERF_TYPE_HW_CACHE, PERF_L1D_READ_MISS_CONFIG, -1);
  46. FAIL_IF(fd < 0);
  47. p = (char *)memalign(zero_size, CACHELINE_SIZE);
  48. FAIL_IF(perf_event_enable(fd));
  49. // disable L1 prefetching
  50. set_dscr(1);
  51. iter = repetitions;
  52. /*
  53. * We expect to see l1d miss for each cacheline access when entry_flush
  54. * is set. Allow a small variation on this.
  55. */
  56. l1d_misses_expected = iterations * (zero_size / CACHELINE_SIZE - 2);
  57. again:
  58. FAIL_IF(perf_event_reset(fd));
  59. syscall_loop(p, iterations, zero_size);
  60. FAIL_IF(read(fd, &v, sizeof(v)) != sizeof(v));
  61. if (entry_flush && v.l1d_misses >= l1d_misses_expected)
  62. passes++;
  63. else if (!entry_flush && v.l1d_misses < (l1d_misses_expected / 2))
  64. passes++;
  65. l1d_misses_total += v.l1d_misses;
  66. while (--iter)
  67. goto again;
  68. if (passes < repetitions) {
  69. printf("FAIL (L1D misses with entry_flush=%d: %llu %c %lu) [%d/%d failures]\n",
  70. entry_flush, l1d_misses_total, entry_flush ? '<' : '>',
  71. entry_flush ? repetitions * l1d_misses_expected :
  72. repetitions * l1d_misses_expected / 2,
  73. repetitions - passes, repetitions);
  74. rc = 1;
  75. } else {
  76. printf("PASS (L1D misses with entry_flush=%d: %llu %c %lu) [%d/%d pass]\n",
  77. entry_flush, l1d_misses_total, entry_flush ? '>' : '<',
  78. entry_flush ? repetitions * l1d_misses_expected :
  79. repetitions * l1d_misses_expected / 2,
  80. passes, repetitions);
  81. }
  82. if (entry_flush == entry_flush_orig) {
  83. entry_flush = !entry_flush_orig;
  84. if (write_debugfs_int("powerpc/entry_flush", entry_flush) < 0) {
  85. perror("error writing to powerpc/entry_flush debugfs file");
  86. return 1;
  87. }
  88. iter = repetitions;
  89. l1d_misses_total = 0;
  90. passes = 0;
  91. goto again;
  92. }
  93. perf_event_disable(fd);
  94. close(fd);
  95. set_dscr(0);
  96. if (write_debugfs_int("powerpc/rfi_flush", rfi_flush_orig) < 0) {
  97. perror("unable to restore original value of powerpc/rfi_flush debugfs file");
  98. return 1;
  99. }
  100. if (write_debugfs_int("powerpc/entry_flush", entry_flush_orig) < 0) {
  101. perror("unable to restore original value of powerpc/entry_flush debugfs file");
  102. return 1;
  103. }
  104. return rc;
  105. }
  106. int main(int argc, char *argv[])
  107. {
  108. return test_harness(entry_flush_test, "entry_flush_test");
  109. }