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