1 /* 2 * memset benchmark. 3 * 4 * Copyright (c) 2021, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #define _GNU_SOURCE 9 #include <stdint.h> 10 #include <stdio.h> 11 #include <string.h> 12 #include <assert.h> 13 #include "stringlib.h" 14 #include "benchlib.h" 15 16 #define ITERS 5000 17 #define ITERS2 20000000 18 #define ITERS3 1000000 19 #define NUM_TESTS 16384 20 #define MIN_SIZE 32768 21 #define MAX_SIZE (1024 * 1024) 22 23 static uint8_t a[MAX_SIZE + 4096] __attribute__((__aligned__(64))); 24 25 #define F(x) {#x, x}, 26 27 static const struct fun 28 { 29 const char *name; 30 void *(*fun)(void *, int, size_t); 31 } funtab[] = 32 { 33 #if __aarch64__ 34 F(__memset_aarch64) 35 #elif __arm__ 36 F(__memset_arm) 37 #endif 38 F(memset) 39 #undef F 40 {0, 0} 41 }; 42 43 typedef struct { uint32_t offset : 20, len : 12; } memset_test_t; 44 static memset_test_t test_arr[NUM_TESTS]; 45 46 typedef struct { uint16_t size; uint16_t freq; } freq_data_t; 47 typedef struct { uint8_t align; uint16_t freq; } align_data_t; 48 49 #define SIZE_NUM 65536 50 #define SIZE_MASK (SIZE_NUM-1) 51 static uint8_t len_arr[SIZE_NUM]; 52 53 /* Frequency data for memset sizes up to 4096 based on SPEC2017. */ 54 static freq_data_t memset_len_freq[] = 55 { 56 {40,28817}, {32,15336}, { 16,3823}, {296,3545}, { 24,3454}, { 8,1412}, 57 {292,1202}, { 48, 927}, { 12, 613}, { 11, 539}, {284, 493}, {108, 414}, 58 { 88, 380}, { 20, 295}, {312, 271}, { 72, 233}, { 2, 200}, { 4, 192}, 59 { 15, 180}, { 14, 174}, { 13, 160}, { 56, 151}, { 36, 144}, { 64, 140}, 60 {4095,133}, { 10, 130}, { 9, 124}, { 3, 124}, { 28, 120}, { 0, 118}, 61 {288, 110}, {1152, 96}, {104, 90}, { 1, 86}, {832, 76}, {248, 74}, 62 {1024, 69}, {120, 64}, {512, 63}, {384, 60}, { 6, 59}, { 80, 54}, 63 { 17, 50}, { 7, 49}, {520, 47}, {2048, 39}, {256, 37}, {864, 33}, 64 {1440, 28}, { 22, 27}, {2056, 24}, {260, 23}, { 68, 23}, { 5, 22}, 65 { 18, 21}, {200, 18}, {2120, 18}, { 60, 17}, { 52, 16}, {336, 15}, 66 { 44, 13}, {192, 13}, {160, 12}, {2064, 12}, {128, 12}, { 76, 11}, 67 {164, 11}, {152, 10}, {136, 9}, {488, 7}, { 96, 6}, {560, 6}, 68 {1016, 6}, {112, 5}, {232, 5}, {168, 5}, {952, 5}, {184, 5}, 69 {144, 4}, {252, 4}, { 84, 3}, {960, 3}, {3808, 3}, {244, 3}, 70 {280, 3}, {224, 3}, {156, 3}, {1088, 3}, {440, 3}, {216, 2}, 71 {304, 2}, { 23, 2}, { 25, 2}, { 26, 2}, {264, 2}, {328, 2}, 72 {1096, 2}, {240, 2}, {1104, 2}, {704, 2}, {1664, 2}, {360, 2}, 73 {808, 1}, {544, 1}, {236, 1}, {720, 1}, {368, 1}, {424, 1}, 74 {640, 1}, {1112, 1}, {552, 1}, {272, 1}, {776, 1}, {376, 1}, 75 { 92, 1}, {536, 1}, {824, 1}, {496, 1}, {760, 1}, {792, 1}, 76 {504, 1}, {344, 1}, {1816, 1}, {880, 1}, {176, 1}, {320, 1}, 77 {352, 1}, {2008, 1}, {208, 1}, {408, 1}, {228, 1}, {2072, 1}, 78 {568, 1}, {220, 1}, {616, 1}, {600, 1}, {392, 1}, {696, 1}, 79 {2144, 1}, {1280, 1}, {2136, 1}, {632, 1}, {584, 1}, {456, 1}, 80 {472, 1}, {3440, 1}, {2088, 1}, {680, 1}, {2928, 1}, {212, 1}, 81 {648, 1}, {1752, 1}, {664, 1}, {3512, 1}, {1032, 1}, {528, 1}, 82 {4072, 1}, {204, 1}, {2880, 1}, {3392, 1}, {712, 1}, { 59, 1}, 83 {736, 1}, {592, 1}, {2520, 1}, {744, 1}, {196, 1}, {172, 1}, 84 {728, 1}, {2040, 1}, {1192, 1}, {3600, 1}, {0, 0} 85 }; 86 87 #define ALIGN_NUM 1024 88 #define ALIGN_MASK (ALIGN_NUM-1) 89 static uint8_t align_arr[ALIGN_NUM]; 90 91 /* Alignment data for memset based on SPEC2017. */ 92 static align_data_t memset_align_freq[] = 93 { 94 {16, 338}, {8, 307}, {32, 148}, {64, 131}, {4, 72}, {1, 23}, {2, 5}, {0, 0} 95 }; 96 97 static void 98 init_memset_distribution (void) 99 { 100 int i, j, freq, size, n; 101 102 for (n = i = 0; (freq = memset_len_freq[i].freq) != 0; i++) 103 for (j = 0, size = memset_len_freq[i].size; j < freq; j++) 104 len_arr[n++] = size; 105 assert (n == SIZE_NUM); 106 107 for (n = i = 0; (freq = memset_align_freq[i].freq) != 0; i++) 108 for (j = 0, size = memset_align_freq[i].align; j < freq; j++) 109 align_arr[n++] = size - 1; 110 assert (n == ALIGN_NUM); 111 } 112 113 static size_t 114 init_memset (size_t max_size) 115 { 116 size_t total = 0; 117 /* Create a random set of memsets with the given size and alignment 118 distributions. */ 119 for (int i = 0; i < NUM_TESTS; i++) 120 { 121 test_arr[i].offset = (rand32 (0) & (max_size - 1)); 122 test_arr[i].offset &= ~align_arr[rand32 (0) & ALIGN_MASK]; 123 test_arr[i].len = len_arr[rand32 (0) & SIZE_MASK]; 124 total += test_arr[i].len; 125 } 126 127 return total; 128 } 129 130 131 int main (void) 132 { 133 init_memset_distribution (); 134 135 memset (a, 1, sizeof (a)); 136 137 printf("Random memset (bytes/ns):\n"); 138 for (int f = 0; funtab[f].name != 0; f++) 139 { 140 size_t total_size = 0; 141 uint64_t tsum = 0; 142 printf ("%22s ", funtab[f].name); 143 rand32 (0x12345678); 144 145 for (int size = MIN_SIZE; size <= MAX_SIZE; size *= 2) 146 { 147 size_t memset_size = init_memset (size) * ITERS; 148 149 for (int c = 0; c < NUM_TESTS; c++) 150 funtab[f].fun (a + test_arr[c].offset, 0, test_arr[c].len); 151 152 uint64_t t = clock_get_ns (); 153 for (int i = 0; i < ITERS; i++) 154 for (int c = 0; c < NUM_TESTS; c++) 155 funtab[f].fun (a + test_arr[c].offset, 0, test_arr[c].len); 156 t = clock_get_ns () - t; 157 total_size += memset_size; 158 tsum += t; 159 printf ("%dK: %.2f ", size / 1024, (double)memset_size / t); 160 } 161 printf( "avg %.2f\n", (double)total_size / tsum); 162 } 163 164 size_t total_size = 0; 165 uint64_t tsum = 0; 166 printf ("%22s ", "memset_call"); 167 rand32 (0x12345678); 168 169 for (int size = MIN_SIZE; size <= MAX_SIZE; size *= 2) 170 { 171 size_t memset_size = init_memset (size) * ITERS; 172 173 for (int c = 0; c < NUM_TESTS; c++) 174 memset (a + test_arr[c].offset, 0, test_arr[c].len); 175 176 uint64_t t = clock_get_ns (); 177 for (int i = 0; i < ITERS; i++) 178 for (int c = 0; c < NUM_TESTS; c++) 179 memset (a + test_arr[c].offset, 0, test_arr[c].len); 180 t = clock_get_ns () - t; 181 total_size += memset_size; 182 tsum += t; 183 printf ("%dK: %.2f ", size / 1024, (double)memset_size / t); 184 } 185 printf( "avg %.2f\n", (double)total_size / tsum); 186 187 188 printf ("\nMedium memset (bytes/ns):\n"); 189 for (int f = 0; funtab[f].name != 0; f++) 190 { 191 printf ("%22s ", funtab[f].name); 192 193 for (int size = 8; size <= 512; size *= 2) 194 { 195 uint64_t t = clock_get_ns (); 196 for (int i = 0; i < ITERS2; i++) 197 funtab[f].fun (a, 0, size); 198 t = clock_get_ns () - t; 199 printf ("%dB: %.2f ", size, (double)size * ITERS2 / t); 200 } 201 printf ("\n"); 202 } 203 204 printf ("%22s ", "memset_call"); 205 for (int size = 8; size <= 512; size *= 2) 206 { 207 uint64_t t = clock_get_ns (); 208 for (int i = 0; i < ITERS2; i++) 209 memset (a, 0, size); 210 t = clock_get_ns () - t; 211 printf ("%dB: %.2f ", size, (double)size * ITERS2 / t); 212 } 213 214 215 printf ("\nLarge memset (bytes/ns):\n"); 216 for (int f = 0; funtab[f].name != 0; f++) 217 { 218 printf ("%22s ", funtab[f].name); 219 220 for (int size = 1024; size <= 65536; size *= 2) 221 { 222 uint64_t t = clock_get_ns (); 223 for (int i = 0; i < ITERS3; i++) 224 funtab[f].fun (a, 0, size); 225 t = clock_get_ns () - t; 226 printf ("%dK: %.2f ", size / 1024, (double)size * ITERS3 / t); 227 } 228 printf ("\n"); 229 } 230 231 printf ("%22s ", "memset_call"); 232 for (int size = 1024; size <= 65536; size *= 2) 233 { 234 uint64_t t = clock_get_ns (); 235 for (int i = 0; i < ITERS3; i++) 236 memset (a, 0, size); 237 t = clock_get_ns () - t; 238 printf ("%dK: %.2f ", size / 1024, (double)size * ITERS3 / t); 239 } 240 printf ("\n\n"); 241 242 return 0; 243 } 244