Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 
0003 /*
0004  * Copyright 2018 IBM Corporation.
0005  */
0006 
0007 #define __SANE_USERSPACE_TYPES__
0008 
0009 #include <sys/types.h>
0010 #include <stdint.h>
0011 #include <malloc.h>
0012 #include <unistd.h>
0013 #include <stdlib.h>
0014 #include <string.h>
0015 #include <stdio.h>
0016 #include "utils.h"
0017 #include "flush_utils.h"
0018 
0019 
0020 int rfi_flush_test(void)
0021 {
0022     char *p;
0023     int repetitions = 10;
0024     int fd, passes = 0, iter, rc = 0;
0025     struct perf_event_read v;
0026     __u64 l1d_misses_total = 0;
0027     unsigned long iterations = 100000, zero_size = 24 * 1024;
0028     unsigned long l1d_misses_expected;
0029     int rfi_flush_orig, rfi_flush;
0030     int have_entry_flush, entry_flush_orig;
0031 
0032     SKIP_IF(geteuid() != 0);
0033 
0034     // The PMU event we use only works on Power7 or later
0035     SKIP_IF(!have_hwcap(PPC_FEATURE_ARCH_2_06));
0036 
0037     if (read_debugfs_file("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
0038         perror("Unable to read powerpc/rfi_flush debugfs file");
0039         SKIP_IF(1);
0040     }
0041 
0042     if (read_debugfs_file("powerpc/entry_flush", &entry_flush_orig) < 0) {
0043         have_entry_flush = 0;
0044     } else {
0045         have_entry_flush = 1;
0046 
0047         if (entry_flush_orig != 0) {
0048             if (write_debugfs_file("powerpc/entry_flush", 0) < 0) {
0049                 perror("error writing to powerpc/entry_flush debugfs file");
0050                 return 1;
0051             }
0052         }
0053     }
0054 
0055     rfi_flush = rfi_flush_orig;
0056 
0057     fd = perf_event_open_counter(PERF_TYPE_HW_CACHE, PERF_L1D_READ_MISS_CONFIG, -1);
0058     FAIL_IF(fd < 0);
0059 
0060     p = (char *)memalign(zero_size, CACHELINE_SIZE);
0061 
0062     FAIL_IF(perf_event_enable(fd));
0063 
0064     // disable L1 prefetching
0065     set_dscr(1);
0066 
0067     iter = repetitions;
0068 
0069     /*
0070      * We expect to see l1d miss for each cacheline access when rfi_flush
0071      * is set. Allow a small variation on this.
0072      */
0073     l1d_misses_expected = iterations * (zero_size / CACHELINE_SIZE - 2);
0074 
0075 again:
0076     FAIL_IF(perf_event_reset(fd));
0077 
0078     syscall_loop(p, iterations, zero_size);
0079 
0080     FAIL_IF(read(fd, &v, sizeof(v)) != sizeof(v));
0081 
0082     if (rfi_flush && v.l1d_misses >= l1d_misses_expected)
0083         passes++;
0084     else if (!rfi_flush && v.l1d_misses < (l1d_misses_expected / 2))
0085         passes++;
0086 
0087     l1d_misses_total += v.l1d_misses;
0088 
0089     while (--iter)
0090         goto again;
0091 
0092     if (passes < repetitions) {
0093         printf("FAIL (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d failures]\n",
0094                rfi_flush, l1d_misses_total, rfi_flush ? '<' : '>',
0095                rfi_flush ? repetitions * l1d_misses_expected :
0096                repetitions * l1d_misses_expected / 2,
0097                repetitions - passes, repetitions);
0098         rc = 1;
0099     } else
0100         printf("PASS (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d pass]\n",
0101                rfi_flush, l1d_misses_total, rfi_flush ? '>' : '<',
0102                rfi_flush ? repetitions * l1d_misses_expected :
0103                repetitions * l1d_misses_expected / 2,
0104                passes, repetitions);
0105 
0106     if (rfi_flush == rfi_flush_orig) {
0107         rfi_flush = !rfi_flush_orig;
0108         if (write_debugfs_file("powerpc/rfi_flush", rfi_flush) < 0) {
0109             perror("error writing to powerpc/rfi_flush debugfs file");
0110             return 1;
0111         }
0112         iter = repetitions;
0113         l1d_misses_total = 0;
0114         passes = 0;
0115         goto again;
0116     }
0117 
0118     perf_event_disable(fd);
0119     close(fd);
0120 
0121     set_dscr(0);
0122 
0123     if (write_debugfs_file("powerpc/rfi_flush", rfi_flush_orig) < 0) {
0124         perror("unable to restore original value of powerpc/rfi_flush debugfs file");
0125         return 1;
0126     }
0127 
0128     if (have_entry_flush) {
0129         if (write_debugfs_file("powerpc/entry_flush", entry_flush_orig) < 0) {
0130             perror("unable to restore original value of powerpc/entry_flush "
0131                    "debugfs file");
0132             return 1;
0133         }
0134     }
0135 
0136     return rc;
0137 }
0138 
0139 int main(int argc, char *argv[])
0140 {
0141     return test_harness(rfi_flush_test, "rfi_flush_test");
0142 }