xref: /freebsd/contrib/arm-optimized-routines/string/bench/memset.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
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__(4096)));
24 
25 #define DOTEST(STR,TESTFN)			\
26   printf (STR);					\
27   RUN (TESTFN, memset);				\
28   RUNA64 (TESTFN, __memset_aarch64);		\
29   RUNSVE (TESTFN, __memset_aarch64_sve);	\
30   RUNMOPS (TESTFN, __memset_mops);		\
31   RUNA32 (TESTFN, __memset_arm);		\
32   printf ("\n");
33 
34 typedef struct { uint32_t offset : 20, len : 12; } memset_test_t;
35 static memset_test_t test_arr[NUM_TESTS];
36 
37 typedef struct { uint16_t size; uint16_t freq; } freq_data_t;
38 typedef struct { uint8_t align; uint16_t freq; } align_data_t;
39 
40 #define SIZE_NUM 65536
41 #define SIZE_MASK (SIZE_NUM-1)
42 static uint8_t len_arr[SIZE_NUM];
43 
44 /* Frequency data for memset sizes up to 4096 based on SPEC2017.  */
45 static freq_data_t memset_len_freq[] =
46 {
47 {40,28817}, {32,15336}, { 16,3823}, {296,3545}, { 24,3454}, {  8,1412},
48 {292,1202}, { 48, 927}, { 12, 613}, { 11, 539}, {284, 493}, {108, 414},
49 { 88, 380}, { 20, 295}, {312, 271}, { 72, 233}, {  2, 200}, {  4, 192},
50 { 15, 180}, { 14, 174}, { 13, 160}, { 56, 151}, { 36, 144}, { 64, 140},
51 {4095,133}, { 10, 130}, {  9, 124}, {  3, 124}, { 28, 120}, {  0, 118},
52 {288, 110}, {1152, 96}, {104,  90}, {  1,  86}, {832,  76}, {248,  74},
53 {1024, 69}, {120,  64}, {512,  63}, {384,  60}, {  6,  59}, { 80,  54},
54 { 17,  50}, {  7,  49}, {520,  47}, {2048, 39}, {256,  37}, {864,  33},
55 {1440, 28}, { 22,  27}, {2056, 24}, {260,  23}, { 68,  23}, {  5,  22},
56 { 18,  21}, {200,  18}, {2120, 18}, { 60,  17}, { 52,  16}, {336,  15},
57 { 44,  13}, {192,  13}, {160,  12}, {2064, 12}, {128,  12}, { 76,  11},
58 {164,  11}, {152,  10}, {136,   9}, {488,   7}, { 96,   6}, {560,   6},
59 {1016,  6}, {112,   5}, {232,   5}, {168,   5}, {952,   5}, {184,   5},
60 {144,   4}, {252,   4}, { 84,   3}, {960,   3}, {3808,  3}, {244,   3},
61 {280,   3}, {224,   3}, {156,   3}, {1088,  3}, {440,   3}, {216,   2},
62 {304,   2}, { 23,   2}, { 25,   2}, { 26,   2}, {264,   2}, {328,   2},
63 {1096,  2}, {240,   2}, {1104,  2}, {704,   2}, {1664,  2}, {360,   2},
64 {808,   1}, {544,   1}, {236,   1}, {720,   1}, {368,   1}, {424,   1},
65 {640,   1}, {1112,  1}, {552,   1}, {272,   1}, {776,   1}, {376,   1},
66 { 92,   1}, {536,   1}, {824,   1}, {496,   1}, {760,   1}, {792,   1},
67 {504,   1}, {344,   1}, {1816,  1}, {880,   1}, {176,   1}, {320,   1},
68 {352,   1}, {2008,  1}, {208,   1}, {408,   1}, {228,   1}, {2072,  1},
69 {568,   1}, {220,   1}, {616,   1}, {600,   1}, {392,   1}, {696,   1},
70 {2144,  1}, {1280,  1}, {2136,  1}, {632,   1}, {584,   1}, {456,   1},
71 {472,   1}, {3440,  1}, {2088,  1}, {680,   1}, {2928,  1}, {212,   1},
72 {648,   1}, {1752,  1}, {664,   1}, {3512,  1}, {1032,  1}, {528,   1},
73 {4072,  1}, {204,   1}, {2880,  1}, {3392,  1}, {712,   1}, { 59,   1},
74 {736,   1}, {592,   1}, {2520,  1}, {744,   1}, {196,   1}, {172,   1},
75 {728,   1}, {2040,  1}, {1192,  1}, {3600,  1}, {0, 0}
76 };
77 
78 #define ALIGN_NUM 1024
79 #define ALIGN_MASK (ALIGN_NUM-1)
80 static uint8_t align_arr[ALIGN_NUM];
81 
82 /* Alignment data for memset based on SPEC2017.  */
83 static align_data_t memset_align_freq[] =
84 {
85  {16, 338}, {8, 307}, {32, 148}, {64, 131}, {4, 72}, {1, 23}, {2, 5}, {0, 0}
86 };
87 
88 static void
89 init_memset_distribution (void)
90 {
91   int i, j, freq, size, n;
92 
93   for (n = i = 0; (freq = memset_len_freq[i].freq) != 0; i++)
94     for (j = 0, size = memset_len_freq[i].size; j < freq; j++)
95       len_arr[n++] = size;
96   assert (n == SIZE_NUM);
97 
98   for (n = i = 0; (freq = memset_align_freq[i].freq) != 0; i++)
99     for (j = 0, size = memset_align_freq[i].align; j < freq; j++)
100       align_arr[n++] = size - 1;
101   assert (n == ALIGN_NUM);
102 }
103 
104 static size_t
105 init_memset (size_t max_size)
106 {
107   size_t total = 0;
108   /* Create a random set of memsets with the given size and alignment
109      distributions.  */
110   for (int i = 0; i < NUM_TESTS; i++)
111     {
112       test_arr[i].offset = (rand32 (0) & (max_size - 1));
113       test_arr[i].offset &= ~align_arr[rand32 (0) & ALIGN_MASK];
114       test_arr[i].len = len_arr[rand32 (0) & SIZE_MASK];
115       total += test_arr[i].len;
116     }
117 
118   return total;
119 }
120 
121 static void inline __attribute ((always_inline))
122 memset_random (const char *name, void *(*set)(void *, int, size_t))
123 {
124   uint64_t total_size = 0;
125   uint64_t tsum = 0;
126   printf ("%22s ", name);
127   rand32 (0x12345678);
128 
129   for (int size = MIN_SIZE; size <= MAX_SIZE; size *= 2)
130     {
131       uint64_t memset_size = init_memset (size) * ITERS;
132 
133       for (int c = 0; c < NUM_TESTS; c++)
134 	set (a + test_arr[c].offset, 0, test_arr[c].len);
135 
136       uint64_t t = clock_get_ns ();
137       for (int i = 0; i < ITERS; i++)
138 	for (int c = 0; c < NUM_TESTS; c++)
139 	  set (a + test_arr[c].offset, 0, test_arr[c].len);
140       t = clock_get_ns () - t;
141       total_size += memset_size;
142       tsum += t;
143       printf ("%dK: %5.2f ", size / 1024, (double)memset_size / t);
144     }
145   printf( "avg %5.2f\n", (double)total_size / tsum);
146 }
147 
148 static void inline __attribute ((always_inline))
149 memset_medium (const char *name, void *(*set)(void *, int, size_t))
150 {
151   printf ("%22s ", name);
152 
153   for (int size = 8; size <= 512; size *= 2)
154     {
155       uint64_t t = clock_get_ns ();
156       for (int i = 0; i < ITERS2; i++)
157 	set (a, 0, size);
158       t = clock_get_ns () - t;
159       printf ("%dB: %5.2f ", size, (double)size * ITERS2 / t);
160     }
161   printf ("\n");
162 }
163 
164 static void inline __attribute ((always_inline))
165 memset_large (const char *name, void *(*set)(void *, int, size_t))
166 {
167   printf ("%22s ", name);
168 
169   for (int size = 1024; size <= 65536; size *= 2)
170     {
171       uint64_t t = clock_get_ns ();
172       for (int i = 0; i < ITERS3; i++)
173 	set (a, 0, size);
174       t = clock_get_ns () - t;
175       printf ("%dKB: %6.2f ", size / 1024, (double)size * ITERS3 / t);
176     }
177   printf ("\n");
178 }
179 
180 int main (void)
181 {
182   init_memset_distribution ();
183 
184   memset (a, 1, sizeof (a));
185 
186   DOTEST ("Random memset (bytes/ns):\n", memset_random);
187   DOTEST ("Medium memset (bytes/ns):\n", memset_medium);
188   DOTEST ("Large memset (bytes/ns):\n", memset_large);
189   return 0;
190 }
191