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 static 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 static void *malloc_and_init_memory(size_t buf_size) 55 * Buffer index step advance to workaround HW !! 55 { 56 * the measurements. !! 56 void *p = NULL; 57 * !! 57 uint64_t *p64; 58 * Must be a prime to step through all indexes !! 58 size_t s64; 59 * !! 59 int ret; 60 * Some primes work better than others on some !! 60 61 * result stability point of view). !! 61 ret = posix_memalign(&p, PAGE_SIZE, buf_size); 62 */ !! 62 if (ret < 0) 63 #define FILL_IDX_MULT 23 !! 63 return NULL; >> 64 >> 65 p64 = (uint64_t *)p; >> 66 s64 = buf_size / sizeof(uint64_t); >> 67 >> 68 while (s64 > 0) { >> 69 *p64 = (uint64_t)rand(); >> 70 p64 += (CL_SIZE / sizeof(uint64_t)); >> 71 s64 -= (CL_SIZE / sizeof(uint64_t)); >> 72 } >> 73 >> 74 return p; >> 75 } 64 76 65 static int fill_one_span_read(unsigned char *b 77 static int fill_one_span_read(unsigned char *buf, size_t buf_size) 66 { 78 { 67 unsigned int size = buf_size / (CL_SIZ !! 79 unsigned char *end_ptr = buf + buf_size; 68 unsigned int i, idx = 0; !! 80 unsigned char sum, *p; 69 unsigned char sum = 0; !! 81 70 !! 82 sum = 0; 71 /* !! 83 p = buf; 72 * Read the buffer in an order that is !! 84 while (p < end_ptr) { 73 * optimizations to prevent them inter !! 85 sum += *p; 74 * !! 86 p += (CL_SIZE / 2); 75 * The read order is (in terms of halv << 76 * i * FILL_IDX_MULT % size << 77 * The formula is open-coded below to << 78 * as it improves MBA/MBM result stabi << 79 */ << 80 for (i = 0; i < size; i++) { << 81 sum += buf[idx * (CL_SIZE / 2) << 82 << 83 idx += FILL_IDX_MULT; << 84 while (idx >= size) << 85 idx -= size; << 86 } 87 } 87 88 88 return sum; 89 return sum; 89 } 90 } 90 91 91 static void fill_one_span_write(unsigned char 92 static void fill_one_span_write(unsigned char *buf, size_t buf_size) 92 { 93 { 93 unsigned char *end_ptr = buf + buf_siz 94 unsigned char *end_ptr = buf + buf_size; 94 unsigned char *p; 95 unsigned char *p; 95 96 96 p = buf; 97 p = buf; 97 while (p < end_ptr) { 98 while (p < end_ptr) { 98 *p = '1'; 99 *p = '1'; 99 p += (CL_SIZE / 2); 100 p += (CL_SIZE / 2); 100 } 101 } 101 } 102 } 102 103 103 void fill_cache_read(unsigned char *buf, size_ !! 104 static int fill_cache_read(unsigned char *buf, size_t buf_size, bool once) 104 { 105 { 105 int ret = 0; 106 int ret = 0; >> 107 FILE *fp; 106 108 107 while (1) { 109 while (1) { 108 ret = fill_one_span_read(buf, 110 ret = fill_one_span_read(buf, buf_size); 109 if (once) 111 if (once) 110 break; 112 break; 111 } 113 } 112 114 113 /* Consume read result so that reading 115 /* Consume read result so that reading memory is not optimized out. */ 114 *value_sink = ret; !! 116 fp = fopen("/dev/null", "w"); >> 117 if (!fp) { >> 118 perror("Unable to write to /dev/null"); >> 119 return -1; >> 120 } >> 121 fprintf(fp, "Sum: %d ", ret); >> 122 fclose(fp); >> 123 >> 124 return 0; 115 } 125 } 116 126 117 static void fill_cache_write(unsigned char *bu !! 127 static int fill_cache_write(unsigned char *buf, size_t buf_size, bool once) 118 { 128 { 119 while (1) { 129 while (1) { 120 fill_one_span_write(buf, buf_s 130 fill_one_span_write(buf, buf_size); 121 if (once) 131 if (once) 122 break; 132 break; 123 } 133 } >> 134 >> 135 return 0; 124 } 136 } 125 137 126 unsigned char *alloc_buffer(size_t buf_size, i !! 138 static int fill_cache(size_t buf_size, int memflush, int op, bool once) 127 { 139 { 128 void *buf = NULL; !! 140 unsigned char *buf; 129 uint64_t *p64; << 130 size_t s64; << 131 int ret; 141 int ret; 132 142 133 ret = posix_memalign(&buf, PAGE_SIZE, !! 143 buf = malloc_and_init_memory(buf_size); 134 if (ret < 0) !! 144 if (!buf) 135 return NULL; !! 145 return -1; 136 << 137 /* Initialize the buffer */ << 138 p64 = buf; << 139 s64 = buf_size / sizeof(uint64_t); << 140 << 141 while (s64 > 0) { << 142 *p64 = (uint64_t)rand(); << 143 p64 += (CL_SIZE / sizeof(uint6 << 144 s64 -= (CL_SIZE / sizeof(uint6 << 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 if (op == 0) >> 152 ret = fill_cache_read(buf, buf_size, once); >> 153 else >> 154 ret = fill_cache_write(buf, buf_size, once); >> 155 >> 156 free(buf); >> 157 >> 158 if (ret) { >> 159 printf("\n Error in fill cache read/write...\n"); >> 160 return -1; >> 161 } >> 162 >> 163 >> 164 return 0; 152 } 165 } 153 166 154 int run_fill_buf(size_t buf_size, int memflush !! 167 int run_fill_buf(size_t span, int memflush, int op, bool once) 155 { 168 { 156 unsigned char *buf; !! 169 size_t cache_size = span; >> 170 int ret; 157 171 158 buf = alloc_buffer(buf_size, memflush) !! 172 ret = fill_cache(cache_size, memflush, op, once); 159 if (!buf) !! 173 if (ret) { >> 174 printf("\n Error in fill cache\n"); 160 return -1; 175 return -1; 161 !! 176 } 162 if (op == 0) << 163 fill_cache_read(buf, buf_size, << 164 else << 165 fill_cache_write(buf, buf_size << 166 free(buf); << 167 177 168 return 0; 178 return 0; 169 } 179 } 170 180
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.