~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/resctrl/fill_buf.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/testing/selftests/resctrl/fill_buf.c (Architecture i386) and /tools/testing/selftests/resctrl/fill_buf.c (Architecture m68k)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * fill_buf benchmark                               3  * fill_buf benchmark
  4  *                                                  4  *
  5  * Copyright (C) 2018 Intel Corporation             5  * Copyright (C) 2018 Intel Corporation
  6  *                                                  6  *
  7  * Authors:                                         7  * Authors:
  8  *    Sai Praneeth Prakhya <sai.praneeth.prakh      8  *    Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
  9  *    Fenghua Yu <fenghua.yu@intel.com>             9  *    Fenghua Yu <fenghua.yu@intel.com>
 10  */                                                10  */
 11 #include <stdio.h>                                 11 #include <stdio.h>
 12 #include <unistd.h>                                12 #include <unistd.h>
 13 #include <stdlib.h>                                13 #include <stdlib.h>
 14 #include <sys/types.h>                             14 #include <sys/types.h>
 15 #include <sys/wait.h>                              15 #include <sys/wait.h>
 16 #include <inttypes.h>                              16 #include <inttypes.h>
 17 #include <string.h>                                17 #include <string.h>
 18                                                    18 
 19 #include "resctrl.h"                               19 #include "resctrl.h"
 20                                                    20 
 21 #define CL_SIZE                 (64)               21 #define CL_SIZE                 (64)
 22 #define PAGE_SIZE               (4 * 1024)         22 #define PAGE_SIZE               (4 * 1024)
 23 #define MB                      (1024 * 1024)      23 #define MB                      (1024 * 1024)
 24                                                    24 
 25 static void sb(void)                               25 static void sb(void)
 26 {                                                  26 {
 27 #if defined(__i386) || defined(__x86_64)           27 #if defined(__i386) || defined(__x86_64)
 28         asm volatile("sfence\n\t"                  28         asm volatile("sfence\n\t"
 29                      : : : "memory");              29                      : : : "memory");
 30 #endif                                             30 #endif
 31 }                                                  31 }
 32                                                    32 
 33 static void cl_flush(void *p)                      33 static void cl_flush(void *p)
 34 {                                                  34 {
 35 #if defined(__i386) || defined(__x86_64)           35 #if defined(__i386) || defined(__x86_64)
 36         asm volatile("clflush (%0)\n\t"            36         asm volatile("clflush (%0)\n\t"
 37                      : : "r"(p) : "memory");       37                      : : "r"(p) : "memory");
 38 #endif                                             38 #endif
 39 }                                                  39 }
 40                                                    40 
 41 void mem_flush(unsigned char *buf, size_t buf_     41 void mem_flush(unsigned char *buf, size_t buf_size)
 42 {                                                  42 {
 43         unsigned char *cp = buf;                   43         unsigned char *cp = buf;
 44         size_t i = 0;                              44         size_t i = 0;
 45                                                    45 
 46         buf_size = buf_size / CL_SIZE; /* mem      46         buf_size = buf_size / CL_SIZE; /* mem size in cache lines */
 47                                                    47 
 48         for (i = 0; i < buf_size; i++)             48         for (i = 0; i < buf_size; i++)
 49                 cl_flush(&cp[i * CL_SIZE]);        49                 cl_flush(&cp[i * CL_SIZE]);
 50                                                    50 
 51         sb();                                      51         sb();
 52 }                                                  52 }
 53                                                    53 
 54 /*                                                 54 /*
 55  * Buffer index step advance to workaround HW      55  * Buffer index step advance to workaround HW prefetching interfering with
 56  * the measurements.                               56  * the measurements.
 57  *                                                 57  *
 58  * Must be a prime to step through all indexes     58  * Must be a prime to step through all indexes of the buffer.
 59  *                                                 59  *
 60  * Some primes work better than others on some     60  * Some primes work better than others on some architectures (from MBA/MBM
 61  * result stability point of view).                61  * result stability point of view).
 62  */                                                62  */
 63 #define FILL_IDX_MULT   23                         63 #define FILL_IDX_MULT   23
 64                                                    64 
 65 static int fill_one_span_read(unsigned char *b     65 static int fill_one_span_read(unsigned char *buf, size_t buf_size)
 66 {                                                  66 {
 67         unsigned int size = buf_size / (CL_SIZ     67         unsigned int size = buf_size / (CL_SIZE / 2);
 68         unsigned int i, idx = 0;                   68         unsigned int i, idx = 0;
 69         unsigned char sum = 0;                     69         unsigned char sum = 0;
 70                                                    70 
 71         /*                                         71         /*
 72          * Read the buffer in an order that is     72          * Read the buffer in an order that is unexpected by HW prefetching
 73          * optimizations to prevent them inter     73          * optimizations to prevent them interfering with the caching pattern.
 74          *                                         74          *
 75          * The read order is (in terms of halv     75          * The read order is (in terms of halves of cachelines):
 76          *      i * FILL_IDX_MULT % size           76          *      i * FILL_IDX_MULT % size
 77          * The formula is open-coded below to      77          * The formula is open-coded below to avoiding modulo inside the loop
 78          * as it improves MBA/MBM result stabi     78          * as it improves MBA/MBM result stability on some architectures.
 79          */                                        79          */
 80         for (i = 0; i < size; i++) {               80         for (i = 0; i < size; i++) {
 81                 sum += buf[idx * (CL_SIZE / 2)     81                 sum += buf[idx * (CL_SIZE / 2)];
 82                                                    82 
 83                 idx += FILL_IDX_MULT;              83                 idx += FILL_IDX_MULT;
 84                 while (idx >= size)                84                 while (idx >= size)
 85                         idx -= size;               85                         idx -= size;
 86         }                                          86         }
 87                                                    87 
 88         return sum;                                88         return sum;
 89 }                                                  89 }
 90                                                    90 
 91 static void fill_one_span_write(unsigned char      91 static void fill_one_span_write(unsigned char *buf, size_t buf_size)
 92 {                                                  92 {
 93         unsigned char *end_ptr = buf + buf_siz     93         unsigned char *end_ptr = buf + buf_size;
 94         unsigned char *p;                          94         unsigned char *p;
 95                                                    95 
 96         p = buf;                                   96         p = buf;
 97         while (p < end_ptr) {                      97         while (p < end_ptr) {
 98                 *p = '1';                          98                 *p = '1';
 99                 p += (CL_SIZE / 2);                99                 p += (CL_SIZE / 2);
100         }                                         100         }
101 }                                                 101 }
102                                                   102 
103 void fill_cache_read(unsigned char *buf, size_    103 void fill_cache_read(unsigned char *buf, size_t buf_size, bool once)
104 {                                                 104 {
105         int ret = 0;                              105         int ret = 0;
106                                                   106 
107         while (1) {                               107         while (1) {
108                 ret = fill_one_span_read(buf,     108                 ret = fill_one_span_read(buf, buf_size);
109                 if (once)                         109                 if (once)
110                         break;                    110                         break;
111         }                                         111         }
112                                                   112 
113         /* Consume read result so that reading    113         /* Consume read result so that reading memory is not optimized out. */
114         *value_sink = ret;                        114         *value_sink = ret;
115 }                                                 115 }
116                                                   116 
117 static void fill_cache_write(unsigned char *bu    117 static void fill_cache_write(unsigned char *buf, size_t buf_size, bool once)
118 {                                                 118 {
119         while (1) {                               119         while (1) {
120                 fill_one_span_write(buf, buf_s    120                 fill_one_span_write(buf, buf_size);
121                 if (once)                         121                 if (once)
122                         break;                    122                         break;
123         }                                         123         }
124 }                                                 124 }
125                                                   125 
126 unsigned char *alloc_buffer(size_t buf_size, i    126 unsigned char *alloc_buffer(size_t buf_size, int memflush)
127 {                                                 127 {
128         void *buf = NULL;                         128         void *buf = NULL;
129         uint64_t *p64;                            129         uint64_t *p64;
130         size_t s64;                               130         size_t s64;
131         int ret;                                  131         int ret;
132                                                   132 
133         ret = posix_memalign(&buf, PAGE_SIZE,     133         ret = posix_memalign(&buf, PAGE_SIZE, buf_size);
134         if (ret < 0)                              134         if (ret < 0)
135                 return NULL;                      135                 return NULL;
136                                                   136 
137         /* Initialize the buffer */               137         /* Initialize the buffer */
138         p64 = buf;                                138         p64 = buf;
139         s64 = buf_size / sizeof(uint64_t);        139         s64 = buf_size / sizeof(uint64_t);
140                                                   140 
141         while (s64 > 0) {                         141         while (s64 > 0) {
142                 *p64 = (uint64_t)rand();          142                 *p64 = (uint64_t)rand();
143                 p64 += (CL_SIZE / sizeof(uint6    143                 p64 += (CL_SIZE / sizeof(uint64_t));
144                 s64 -= (CL_SIZE / sizeof(uint6    144                 s64 -= (CL_SIZE / sizeof(uint64_t));
145         }                                         145         }
146                                                   146 
147         /* Flush the memory before using to av    147         /* Flush the memory before using to avoid "cache hot pages" effect */
148         if (memflush)                             148         if (memflush)
149                 mem_flush(buf, buf_size);         149                 mem_flush(buf, buf_size);
150                                                   150 
151         return buf;                               151         return buf;
152 }                                                 152 }
153                                                   153 
154 int run_fill_buf(size_t buf_size, int memflush    154 int run_fill_buf(size_t buf_size, int memflush, int op, bool once)
155 {                                                 155 {
156         unsigned char *buf;                       156         unsigned char *buf;
157                                                   157 
158         buf = alloc_buffer(buf_size, memflush)    158         buf = alloc_buffer(buf_size, memflush);
159         if (!buf)                                 159         if (!buf)
160                 return -1;                        160                 return -1;
161                                                   161 
162         if (op == 0)                              162         if (op == 0)
163                 fill_cache_read(buf, buf_size,    163                 fill_cache_read(buf, buf_size, once);
164         else                                      164         else
165                 fill_cache_write(buf, buf_size    165                 fill_cache_write(buf, buf_size, once);
166         free(buf);                                166         free(buf);
167                                                   167 
168         return 0;                                 168         return 0;
169 }                                                 169 }
170                                                   170 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php